blob: 030be38ea23982a47b9f5547152a670d50cb0c1c [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 */
37 int input_fn; /* Invoked for input() function */
38# 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;
271 xpc.xp_arg = ccline.xp_arg;
272 }
273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 /*
276 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
277 * doing ":@0" when register 0 doesn't contain a CR.
278 */
279 msg_scroll = FALSE;
280
281 State = CMDLINE;
282
283 if (firstc == '/' || firstc == '?' || firstc == '@')
284 {
285 /* Use ":lmap" mappings for search pattern and input(). */
286 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
287 b_im_ptr = &curbuf->b_p_iminsert;
288 else
289 b_im_ptr = &curbuf->b_p_imsearch;
290 if (*b_im_ptr == B_IMODE_LMAP)
291 State |= LANGMAP;
292#ifdef USE_IM_CONTROL
293 im_set_active(*b_im_ptr == B_IMODE_IM);
294#endif
295 }
296#ifdef USE_IM_CONTROL
297 else if (p_imcmdline)
298 im_set_active(TRUE);
299#endif
300
301#ifdef FEAT_MOUSE
302 setmouse();
303#endif
304#ifdef CURSOR_SHAPE
305 ui_cursor_shape(); /* may show different cursor shape */
306#endif
307
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000308 /* When inside an autocommand for writing "exiting" may be set and
309 * terminal mode set to cooked. Need to set raw mode here then. */
310 settmode(TMODE_RAW);
311
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312#ifdef FEAT_CMDHIST
313 init_history();
314 hiscnt = hislen; /* set hiscnt to impossible history value */
315 histype = hist_char2type(firstc);
316#endif
317
318#ifdef FEAT_DIGRAPHS
319 do_digraph(-1); /* init digraph typahead */
320#endif
321
322 /*
323 * Collect the command string, handling editing keys.
324 */
325 for (;;)
326 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000327 redir_off = TRUE; /* Don't redirect the typed command.
328 Repeated, because a ":redir" inside
329 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330#ifdef USE_ON_FLY_SCROLL
331 dont_scroll = FALSE; /* allow scrolling here */
332#endif
333 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
334
335 cursorcmd(); /* set the cursor on the right spot */
336 c = safe_vgetc();
337 if (KeyTyped)
338 {
339 some_key_typed = TRUE;
340#ifdef FEAT_RIGHTLEFT
341 if (cmd_hkmap)
342 c = hkmap(c);
343# ifdef FEAT_FKMAP
344 if (cmd_fkmap)
345 c = cmdl_fkmap(c);
346# endif
347 if (cmdmsg_rl && !KeyStuffed)
348 {
349 /* Invert horizontal movements and operations. Only when
350 * typed by the user directly, not when the result of a
351 * mapping. */
352 switch (c)
353 {
354 case K_RIGHT: c = K_LEFT; break;
355 case K_S_RIGHT: c = K_S_LEFT; break;
356 case K_C_RIGHT: c = K_C_LEFT; break;
357 case K_LEFT: c = K_RIGHT; break;
358 case K_S_LEFT: c = K_S_RIGHT; break;
359 case K_C_LEFT: c = K_C_RIGHT; break;
360 }
361 }
362#endif
363 }
364
365 /*
366 * Ignore got_int when CTRL-C was typed here.
367 * Don't ignore it in :global, we really need to break then, e.g., for
368 * ":g/pat/normal /pat" (without the <CR>).
369 * Don't ignore it for the input() function.
370 */
371 if ((c == Ctrl_C
372#ifdef UNIX
373 || c == intr_char
374#endif
375 )
376#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
377 && firstc != '@'
378#endif
379#ifdef FEAT_EVAL
380 && !break_ctrl_c
381#endif
382 && !global_busy)
383 got_int = FALSE;
384
385#ifdef FEAT_CMDHIST
386 /* free old command line when finished moving around in the history
387 * list */
388 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000389 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000390 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 && c != K_PAGEDOWN && c != K_PAGEUP
392 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000393 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
395 {
396 vim_free(lookfor);
397 lookfor = NULL;
398 }
399#endif
400
401 /*
402 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
403 */
404 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
405 c = Ctrl_P;
406
407#ifdef FEAT_WILDMENU
408 /* Special translations for 'wildmenu' */
409 if (did_wild_list && p_wmnu)
410 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000411 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000413 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 c = Ctrl_N;
415 }
416 /* Hitting CR after "emenu Name.": complete submenu */
417 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
418 && ccline.cmdpos > 1
419 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
420 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
421 && (c == '\n' || c == '\r' || c == K_KENTER))
422 c = K_DOWN;
423#endif
424
425 /* free expanded names when finished walking through matches */
426 if (xpc.xp_numfiles != -1
427 && !(c == p_wc && KeyTyped) && c != p_wcm
428 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
429 && c != Ctrl_L)
430 {
431 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
432 did_wild_list = FALSE;
433#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000434 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435#endif
436 xpc.xp_context = EXPAND_NOTHING;
437 wim_index = 0;
438#ifdef FEAT_WILDMENU
439 if (p_wmnu && wild_menu_showing != 0)
440 {
441 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000442 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000443
444 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000445 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446
447 if (wild_menu_showing == WM_SCROLLED)
448 {
449 /* Entered command line, move it up */
450 cmdline_row--;
451 redrawcmd();
452 }
453 else if (save_p_ls != -1)
454 {
455 /* restore 'laststatus' and 'winminheight' */
456 p_ls = save_p_ls;
457 p_wmh = save_p_wmh;
458 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000459 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000461 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462 redrawcmd();
463 save_p_ls = -1;
464 }
465 else
466 {
467# ifdef FEAT_VERTSPLIT
468 win_redraw_last_status(topframe);
469# else
470 lastwin->w_redr_status = TRUE;
471# endif
472 redraw_statuslines();
473 }
474 KeyTyped = skt;
475 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000476 if (ccline.input_fn)
477 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 }
479#endif
480 }
481
482#ifdef FEAT_WILDMENU
483 /* Special translations for 'wildmenu' */
484 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
485 {
486 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000487 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000489 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 {
491 /* Hitting <Up>: Remove one submenu name in front of the
492 * cursor */
493 int found = FALSE;
494
495 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
496 i = 0;
497 while (--j > 0)
498 {
499 /* check for start of menu name */
500 if (ccline.cmdbuff[j] == ' '
501 && ccline.cmdbuff[j - 1] != '\\')
502 {
503 i = j + 1;
504 break;
505 }
506 /* check for start of submenu name */
507 if (ccline.cmdbuff[j] == '.'
508 && ccline.cmdbuff[j - 1] != '\\')
509 {
510 if (found)
511 {
512 i = j + 1;
513 break;
514 }
515 else
516 found = TRUE;
517 }
518 }
519 if (i > 0)
520 cmdline_del(i);
521 c = p_wc;
522 xpc.xp_context = EXPAND_NOTHING;
523 }
524 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000525 if ((xpc.xp_context == EXPAND_FILES
526 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
528 char_u upseg[5];
529
530 upseg[0] = PATHSEP;
531 upseg[1] = '.';
532 upseg[2] = '.';
533 upseg[3] = PATHSEP;
534 upseg[4] = NUL;
535
536 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000537 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
539 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
540 {
541 /* go down a directory */
542 c = p_wc;
543 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000544 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000545 {
546 /* If in a direct ancestor, strip off one ../ to go down */
547 int found = FALSE;
548
549 j = ccline.cmdpos;
550 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
551 while (--j > i)
552 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000553#ifdef FEAT_MBYTE
554 if (has_mbyte)
555 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (vim_ispathsep(ccline.cmdbuff[j]))
558 {
559 found = TRUE;
560 break;
561 }
562 }
563 if (found
564 && ccline.cmdbuff[j - 1] == '.'
565 && ccline.cmdbuff[j - 2] == '.'
566 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
567 {
568 cmdline_del(j - 2);
569 c = p_wc;
570 }
571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 /* go up a directory */
575 int found = FALSE;
576
577 j = ccline.cmdpos - 1;
578 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
579 while (--j > i)
580 {
581#ifdef FEAT_MBYTE
582 if (has_mbyte)
583 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
584#endif
585 if (vim_ispathsep(ccline.cmdbuff[j])
586#ifdef BACKSLASH_IN_FILENAME
587 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
588 == NULL
589#endif
590 )
591 {
592 if (found)
593 {
594 i = j + 1;
595 break;
596 }
597 else
598 found = TRUE;
599 }
600 }
601
602 if (!found)
603 j = i;
604 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
605 j += 4;
606 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
607 && j == i)
608 j += 3;
609 else
610 j = 0;
611 if (j > 0)
612 {
613 /* TODO this is only for DOS/UNIX systems - need to put in
614 * machine-specific stuff here and in upseg init */
615 cmdline_del(j);
616 put_on_cmdline(upseg + 1, 3, FALSE);
617 }
618 else if (ccline.cmdpos > i)
619 cmdline_del(i);
620 c = p_wc;
621 }
622 }
623#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
624 if (p_wmnu
625 && (xpc.xp_context == EXPAND_FILES
626 || xpc.xp_context == EXPAND_MENUNAMES)
627 && (c == K_UP || c == K_DOWN))
628 xpc.xp_context = EXPAND_NOTHING;
629#endif
630
631#endif /* FEAT_WILDMENU */
632
633 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
634 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
635 if (c == Ctrl_BSL)
636 {
637 ++no_mapping;
638 ++allow_keys;
639 c = safe_vgetc();
640 --no_mapping;
641 --allow_keys;
642 /* CTRL-\ e doesn't work when obtaining an expression. */
643 if (c != Ctrl_N && c != Ctrl_G
644 && (c != 'e' || ccline.cmdfirstc == '='))
645 {
646 vungetc(c);
647 c = Ctrl_BSL;
648 }
649#ifdef FEAT_EVAL
650 else if (c == 'e')
651 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000652 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653
654 /*
655 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000656 * Need to save and restore the current command line, to be
657 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 */
659 if (ccline.cmdpos == ccline.cmdlen)
660 new_cmdpos = 99999; /* keep it at the end */
661 else
662 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000663
664 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000666 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 if (c == '=')
668 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000669 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000670 * to avoid nasty things like going to another buffer when
671 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000672 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000673 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000675 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000676 restore_cmdline(&save_ccline);
677
678 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000680 ccline.cmdlen = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 STRCPY(ccline.cmdbuff, p);
682 vim_free(p);
683
684 /* Restore the cursor or use the position set with
685 * set_cmdline_pos(). */
686 if (new_cmdpos > ccline.cmdlen)
687 ccline.cmdpos = ccline.cmdlen;
688 else
689 ccline.cmdpos = new_cmdpos;
690
691 KeyTyped = FALSE; /* Don't do p_wc completion. */
692 redrawcmd();
693 goto cmdline_changed;
694 }
695 }
696 beep_flush();
697 c = ESC;
698 }
699#endif
700 else
701 {
702 if (c == Ctrl_G && p_im && restart_edit == 0)
703 restart_edit = 'a';
704 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
705 in history */
706 goto returncmd; /* back to Normal mode */
707 }
708 }
709
710#ifdef FEAT_CMDWIN
711 if (c == cedit_key || c == K_CMDWIN)
712 {
713 /*
714 * Open a window to edit the command line (and history).
715 */
716 c = ex_window();
717 some_key_typed = TRUE;
718 }
719# ifdef FEAT_DIGRAPHS
720 else
721# endif
722#endif
723#ifdef FEAT_DIGRAPHS
724 c = do_digraph(c);
725#endif
726
727 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
728 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
729 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000730 /* In Ex mode a backslash escapes a newline. */
731 if (exmode_active
732 && c != ESC
733 && ccline.cmdpos > 0
734 && ccline.cmdpos == ccline.cmdlen
735 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000737 if (c == K_KENTER)
738 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000740 else
741 {
742 gotesc = FALSE; /* Might have typed ESC previously, don't
743 truncate the cmdline now. */
744 if (ccheck_abbr(c + ABBR_OFF))
745 goto cmdline_changed;
746 if (!cmd_silent)
747 {
748 windgoto(msg_row, 0);
749 out_flush();
750 }
751 break;
752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754
755 /*
756 * Completion for 'wildchar' or 'wildcharm' key.
757 * - hitting <ESC> twice means: abandon command line.
758 * - wildcard expansion is only done when the 'wildchar' key is really
759 * typed, not when it comes from a macro
760 */
761 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
762 {
763 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
764 {
765 /* if 'wildmode' contains "list" may still need to list */
766 if (xpc.xp_numfiles > 1
767 && !did_wild_list
768 && (wim_flags[wim_index] & WIM_LIST))
769 {
770 (void)showmatches(&xpc, FALSE);
771 redrawcmd();
772 did_wild_list = TRUE;
773 }
774 if (wim_flags[wim_index] & WIM_LONGEST)
775 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
776 else if (wim_flags[wim_index] & WIM_FULL)
777 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
778 else
779 res = OK; /* don't insert 'wildchar' now */
780 }
781 else /* typed p_wc first time */
782 {
783 wim_index = 0;
784 j = ccline.cmdpos;
785 /* if 'wildmode' first contains "longest", get longest
786 * common part */
787 if (wim_flags[0] & WIM_LONGEST)
788 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
789 else
790 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
791
792 /* if interrupted while completing, behave like it failed */
793 if (got_int)
794 {
795 (void)vpeekc(); /* remove <C-C> from input stream */
796 got_int = FALSE; /* don't abandon the command line */
797 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
798#ifdef FEAT_WILDMENU
799 xpc.xp_context = EXPAND_NOTHING;
800#endif
801 goto cmdline_changed;
802 }
803
804 /* when more than one match, and 'wildmode' first contains
805 * "list", or no change and 'wildmode' contains "longest,list",
806 * list all matches */
807 if (res == OK && xpc.xp_numfiles > 1)
808 {
809 /* a "longest" that didn't do anything is skipped (but not
810 * "list:longest") */
811 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
812 wim_index = 1;
813 if ((wim_flags[wim_index] & WIM_LIST)
814#ifdef FEAT_WILDMENU
815 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
816#endif
817 )
818 {
819 if (!(wim_flags[0] & WIM_LONGEST))
820 {
821#ifdef FEAT_WILDMENU
822 int p_wmnu_save = p_wmnu;
823 p_wmnu = 0;
824#endif
825 nextwild(&xpc, WILD_PREV, 0); /* remove match */
826#ifdef FEAT_WILDMENU
827 p_wmnu = p_wmnu_save;
828#endif
829 }
830#ifdef FEAT_WILDMENU
831 (void)showmatches(&xpc, p_wmnu
832 && ((wim_flags[wim_index] & WIM_LIST) == 0));
833#else
834 (void)showmatches(&xpc, FALSE);
835#endif
836 redrawcmd();
837 did_wild_list = TRUE;
838 if (wim_flags[wim_index] & WIM_LONGEST)
839 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
840 else if (wim_flags[wim_index] & WIM_FULL)
841 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
842 }
843 else
844 vim_beep();
845 }
846#ifdef FEAT_WILDMENU
847 else if (xpc.xp_numfiles == -1)
848 xpc.xp_context = EXPAND_NOTHING;
849#endif
850 }
851 if (wim_index < 3)
852 ++wim_index;
853 if (c == ESC)
854 gotesc = TRUE;
855 if (res == OK)
856 goto cmdline_changed;
857 }
858
859 gotesc = FALSE;
860
861 /* <S-Tab> goes to last match, in a clumsy way */
862 if (c == K_S_TAB && KeyTyped)
863 {
864 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
865 && nextwild(&xpc, WILD_PREV, 0) == OK
866 && nextwild(&xpc, WILD_PREV, 0) == OK)
867 goto cmdline_changed;
868 }
869
870 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
871 c = NL;
872
873 do_abbr = TRUE; /* default: check for abbreviation */
874
875 /*
876 * Big switch for a typed command line character.
877 */
878 switch (c)
879 {
880 case K_BS:
881 case Ctrl_H:
882 case K_DEL:
883 case K_KDEL:
884 case Ctrl_W:
885#ifdef FEAT_FKMAP
886 if (cmd_fkmap && c == K_BS)
887 c = K_DEL;
888#endif
889 if (c == K_KDEL)
890 c = K_DEL;
891
892 /*
893 * delete current character is the same as backspace on next
894 * character, except at end of line
895 */
896 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
897 ++ccline.cmdpos;
898#ifdef FEAT_MBYTE
899 if (has_mbyte && c == K_DEL)
900 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
901 ccline.cmdbuff + ccline.cmdpos);
902#endif
903 if (ccline.cmdpos > 0)
904 {
905 char_u *p;
906
907 j = ccline.cmdpos;
908 p = ccline.cmdbuff + j;
909#ifdef FEAT_MBYTE
910 if (has_mbyte)
911 {
912 p = mb_prevptr(ccline.cmdbuff, p);
913 if (c == Ctrl_W)
914 {
915 while (p > ccline.cmdbuff && vim_isspace(*p))
916 p = mb_prevptr(ccline.cmdbuff, p);
917 i = mb_get_class(p);
918 while (p > ccline.cmdbuff && mb_get_class(p) == i)
919 p = mb_prevptr(ccline.cmdbuff, p);
920 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000921 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 }
923 }
924 else
925#endif
926 if (c == Ctrl_W)
927 {
928 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
929 --p;
930 i = vim_iswordc(p[-1]);
931 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
932 && vim_iswordc(p[-1]) == i)
933 --p;
934 }
935 else
936 --p;
937 ccline.cmdpos = (int)(p - ccline.cmdbuff);
938 ccline.cmdlen -= j - ccline.cmdpos;
939 i = ccline.cmdpos;
940 while (i < ccline.cmdlen)
941 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
942
943 /* Truncate at the end, required for multi-byte chars. */
944 ccline.cmdbuff[ccline.cmdlen] = NUL;
945 redrawcmd();
946 }
947 else if (ccline.cmdlen == 0 && c != Ctrl_W
948 && ccline.cmdprompt == NULL && indent == 0)
949 {
950 /* In ex and debug mode it doesn't make sense to return. */
951 if (exmode_active
952#ifdef FEAT_EVAL
953 || ccline.cmdfirstc == '>'
954#endif
955 )
956 goto cmdline_not_changed;
957
958 vim_free(ccline.cmdbuff); /* no commandline to return */
959 ccline.cmdbuff = NULL;
960 if (!cmd_silent)
961 {
962#ifdef FEAT_RIGHTLEFT
963 if (cmdmsg_rl)
964 msg_col = Columns;
965 else
966#endif
967 msg_col = 0;
968 msg_putchar(' '); /* delete ':' */
969 }
970 redraw_cmdline = TRUE;
971 goto returncmd; /* back to cmd mode */
972 }
973 goto cmdline_changed;
974
975 case K_INS:
976 case K_KINS:
977#ifdef FEAT_FKMAP
978 /* if Farsi mode set, we are in reverse insert mode -
979 Do not change the mode */
980 if (cmd_fkmap)
981 beep_flush();
982 else
983#endif
984 ccline.overstrike = !ccline.overstrike;
985#ifdef CURSOR_SHAPE
986 ui_cursor_shape(); /* may show different cursor shape */
987#endif
988 goto cmdline_not_changed;
989
990 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +0000991 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 {
993 /* ":lmap" mappings exists, toggle use of mappings. */
994 State ^= LANGMAP;
995#ifdef USE_IM_CONTROL
996 im_set_active(FALSE); /* Disable input method */
997#endif
998 if (b_im_ptr != NULL)
999 {
1000 if (State & LANGMAP)
1001 *b_im_ptr = B_IMODE_LMAP;
1002 else
1003 *b_im_ptr = B_IMODE_NONE;
1004 }
1005 }
1006#ifdef USE_IM_CONTROL
1007 else
1008 {
1009 /* There are no ":lmap" mappings, toggle IM. When
1010 * 'imdisable' is set don't try getting the status, it's
1011 * always off. */
1012 if ((p_imdisable && b_im_ptr != NULL)
1013 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1014 {
1015 im_set_active(FALSE); /* Disable input method */
1016 if (b_im_ptr != NULL)
1017 *b_im_ptr = B_IMODE_NONE;
1018 }
1019 else
1020 {
1021 im_set_active(TRUE); /* Enable input method */
1022 if (b_im_ptr != NULL)
1023 *b_im_ptr = B_IMODE_IM;
1024 }
1025 }
1026#endif
1027 if (b_im_ptr != NULL)
1028 {
1029 if (b_im_ptr == &curbuf->b_p_iminsert)
1030 set_iminsert_global();
1031 else
1032 set_imsearch_global();
1033 }
1034#ifdef CURSOR_SHAPE
1035 ui_cursor_shape(); /* may show different cursor shape */
1036#endif
1037#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1038 /* Show/unshow value of 'keymap' in status lines later. */
1039 status_redraw_curbuf();
1040#endif
1041 goto cmdline_not_changed;
1042
1043/* case '@': only in very old vi */
1044 case Ctrl_U:
1045 /* delete all characters left of the cursor */
1046 j = ccline.cmdpos;
1047 ccline.cmdlen -= j;
1048 i = ccline.cmdpos = 0;
1049 while (i < ccline.cmdlen)
1050 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1051 /* Truncate at the end, required for multi-byte chars. */
1052 ccline.cmdbuff[ccline.cmdlen] = NUL;
1053 redrawcmd();
1054 goto cmdline_changed;
1055
1056#ifdef FEAT_CLIPBOARD
1057 case Ctrl_Y:
1058 /* Copy the modeless selection, if there is one. */
1059 if (clip_star.state != SELECT_CLEARED)
1060 {
1061 if (clip_star.state == SELECT_DONE)
1062 clip_copy_modeless_selection(TRUE);
1063 goto cmdline_not_changed;
1064 }
1065 break;
1066#endif
1067
1068 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1069 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001070 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001071 * ":normal" runs out of characters. */
1072 if (exmode_active
1073#ifdef FEAT_EX_EXTRA
1074 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1075#endif
1076 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 goto cmdline_not_changed;
1078
1079 gotesc = TRUE; /* will free ccline.cmdbuff after
1080 putting it in history */
1081 goto returncmd; /* back to cmd mode */
1082
1083 case Ctrl_R: /* insert register */
1084#ifdef USE_ON_FLY_SCROLL
1085 dont_scroll = TRUE; /* disallow scrolling here */
1086#endif
1087 putcmdline('"', TRUE);
1088 ++no_mapping;
1089 i = c = safe_vgetc(); /* CTRL-R <char> */
1090 if (i == Ctrl_O)
1091 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1092 if (i == Ctrl_R)
1093 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1094 --no_mapping;
1095#ifdef FEAT_EVAL
1096 /*
1097 * Insert the result of an expression.
1098 * Need to save the current command line, to be able to enter
1099 * a new one...
1100 */
1101 new_cmdpos = -1;
1102 if (c == '=')
1103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1105 {
1106 beep_flush();
1107 c = ESC;
1108 }
1109 else
1110 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001111 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001113 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115 }
1116#endif
1117 if (c != ESC) /* use ESC to cancel inserting register */
1118 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001119 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001120
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001121#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001122 /* When there was a serious error abort getting the
1123 * command line. */
1124 if (aborting())
1125 {
1126 gotesc = TRUE; /* will free ccline.cmdbuff after
1127 putting it in history */
1128 goto returncmd; /* back to cmd mode */
1129 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 KeyTyped = FALSE; /* Don't do p_wc completion. */
1132#ifdef FEAT_EVAL
1133 if (new_cmdpos >= 0)
1134 {
1135 /* set_cmdline_pos() was used */
1136 if (new_cmdpos > ccline.cmdlen)
1137 ccline.cmdpos = ccline.cmdlen;
1138 else
1139 ccline.cmdpos = new_cmdpos;
1140 }
1141#endif
1142 }
1143 redrawcmd();
1144 goto cmdline_changed;
1145
1146 case Ctrl_D:
1147 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1148 break; /* Use ^D as normal char instead */
1149
1150 redrawcmd();
1151 continue; /* don't do incremental search now */
1152
1153 case K_RIGHT:
1154 case K_S_RIGHT:
1155 case K_C_RIGHT:
1156 do
1157 {
1158 if (ccline.cmdpos >= ccline.cmdlen)
1159 break;
1160 i = cmdline_charsize(ccline.cmdpos);
1161 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1162 break;
1163 ccline.cmdspos += i;
1164#ifdef FEAT_MBYTE
1165 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001166 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 + ccline.cmdpos);
1168 else
1169#endif
1170 ++ccline.cmdpos;
1171 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001172 while ((c == K_S_RIGHT || c == K_C_RIGHT
1173 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1175#ifdef FEAT_MBYTE
1176 if (has_mbyte)
1177 set_cmdspos_cursor();
1178#endif
1179 goto cmdline_not_changed;
1180
1181 case K_LEFT:
1182 case K_S_LEFT:
1183 case K_C_LEFT:
1184 do
1185 {
1186 if (ccline.cmdpos == 0)
1187 break;
1188 --ccline.cmdpos;
1189#ifdef FEAT_MBYTE
1190 if (has_mbyte) /* move to first byte of char */
1191 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1192 ccline.cmdbuff + ccline.cmdpos);
1193#endif
1194 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1195 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001196 while ((c == K_S_LEFT || c == K_C_LEFT
1197 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1199#ifdef FEAT_MBYTE
1200 if (has_mbyte)
1201 set_cmdspos_cursor();
1202#endif
1203 goto cmdline_not_changed;
1204
1205 case K_IGNORE:
1206 goto cmdline_not_changed; /* Ignore mouse */
1207
Bram Moolenaar4770d092006-01-12 23:22:24 +00001208#ifdef FEAT_GUI_W32
1209 /* On Win32 ignore <M-F4>, we get it when closing the window was
1210 * cancelled. */
1211 case K_F4:
1212 if (mod_mask == MOD_MASK_ALT)
1213 {
1214 redrawcmd(); /* somehow the cmdline is cleared */
1215 goto cmdline_not_changed;
1216 }
1217 break;
1218#endif
1219
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220#ifdef FEAT_MOUSE
1221 case K_MIDDLEDRAG:
1222 case K_MIDDLERELEASE:
1223 goto cmdline_not_changed; /* Ignore mouse */
1224
1225 case K_MIDDLEMOUSE:
1226# ifdef FEAT_GUI
1227 /* When GUI is active, also paste when 'mouse' is empty */
1228 if (!gui.in_use)
1229# endif
1230 if (!mouse_has(MOUSE_COMMAND))
1231 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001232# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001234 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001236# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001237 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 redrawcmd();
1239 goto cmdline_changed;
1240
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001241# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001243 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 redrawcmd();
1245 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001246# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247
1248 case K_LEFTDRAG:
1249 case K_LEFTRELEASE:
1250 case K_RIGHTDRAG:
1251 case K_RIGHTRELEASE:
1252 /* Ignore drag and release events when the button-down wasn't
1253 * seen before. */
1254 if (ignore_drag_release)
1255 goto cmdline_not_changed;
1256 /* FALLTHROUGH */
1257 case K_LEFTMOUSE:
1258 case K_RIGHTMOUSE:
1259 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1260 ignore_drag_release = TRUE;
1261 else
1262 ignore_drag_release = FALSE;
1263# ifdef FEAT_GUI
1264 /* When GUI is active, also move when 'mouse' is empty */
1265 if (!gui.in_use)
1266# endif
1267 if (!mouse_has(MOUSE_COMMAND))
1268 goto cmdline_not_changed; /* Ignore mouse */
1269# ifdef FEAT_CLIPBOARD
1270 if (mouse_row < cmdline_row && clip_star.available)
1271 {
1272 int button, is_click, is_drag;
1273
1274 /*
1275 * Handle modeless selection.
1276 */
1277 button = get_mouse_button(KEY2TERMCAP1(c),
1278 &is_click, &is_drag);
1279 if (mouse_model_popup() && button == MOUSE_LEFT
1280 && (mod_mask & MOD_MASK_SHIFT))
1281 {
1282 /* Translate shift-left to right button. */
1283 button = MOUSE_RIGHT;
1284 mod_mask &= ~MOD_MASK_SHIFT;
1285 }
1286 clip_modeless(button, is_click, is_drag);
1287 goto cmdline_not_changed;
1288 }
1289# endif
1290
1291 set_cmdspos();
1292 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1293 ++ccline.cmdpos)
1294 {
1295 i = cmdline_charsize(ccline.cmdpos);
1296 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1297 && mouse_col < ccline.cmdspos % Columns + i)
1298 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001299# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (has_mbyte)
1301 {
1302 /* Count ">" for double-wide char that doesn't fit. */
1303 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001304 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 + ccline.cmdpos) - 1;
1306 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 ccline.cmdspos += i;
1309 }
1310 goto cmdline_not_changed;
1311
1312 /* Mouse scroll wheel: ignored here */
1313 case K_MOUSEDOWN:
1314 case K_MOUSEUP:
1315 /* Alternate buttons ignored here */
1316 case K_X1MOUSE:
1317 case K_X1DRAG:
1318 case K_X1RELEASE:
1319 case K_X2MOUSE:
1320 case K_X2DRAG:
1321 case K_X2RELEASE:
1322 goto cmdline_not_changed;
1323
1324#endif /* FEAT_MOUSE */
1325
1326#ifdef FEAT_GUI
1327 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1328 case K_LEFTRELEASE_NM:
1329 goto cmdline_not_changed;
1330
1331 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001332 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 gui_do_scroll();
1335 redrawcmd();
1336 }
1337 goto cmdline_not_changed;
1338
1339 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001340 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 gui_do_horiz_scroll();
1343 redrawcmd();
1344 }
1345 goto cmdline_not_changed;
1346#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001347#ifdef FEAT_GUI_TABLINE
1348 case K_TABLINE:
1349 case K_TABMENU:
1350 /* Don't want to change any tabs here. Make sure the same tab
1351 * is still selected. */
1352 if (gui_use_tabline())
1353 gui_mch_set_curtab(tabpage_index(curtab));
1354 goto cmdline_not_changed;
1355#endif
1356
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 case K_SELECT: /* end of Select mode mapping - ignore */
1358 goto cmdline_not_changed;
1359
1360 case Ctrl_B: /* begin of command line */
1361 case K_HOME:
1362 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 case K_S_HOME:
1364 case K_C_HOME:
1365 ccline.cmdpos = 0;
1366 set_cmdspos();
1367 goto cmdline_not_changed;
1368
1369 case Ctrl_E: /* end of command line */
1370 case K_END:
1371 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 case K_S_END:
1373 case K_C_END:
1374 ccline.cmdpos = ccline.cmdlen;
1375 set_cmdspos_cursor();
1376 goto cmdline_not_changed;
1377
1378 case Ctrl_A: /* all matches */
1379 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1380 break;
1381 goto cmdline_changed;
1382
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001383 case Ctrl_L:
1384#ifdef FEAT_SEARCH_EXTRA
1385 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1386 {
1387 /* Add a character from under the cursor for 'incsearch' */
1388 if (did_incsearch
1389 && !equalpos(curwin->w_cursor, old_cursor))
1390 {
1391 c = gchar_cursor();
1392 if (c != NUL)
1393 break;
1394 }
1395 goto cmdline_not_changed;
1396 }
1397#endif
1398
1399 /* completion: longest common part */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1401 break;
1402 goto cmdline_changed;
1403
1404 case Ctrl_N: /* next match */
1405 case Ctrl_P: /* previous match */
1406 if (xpc.xp_numfiles > 0)
1407 {
1408 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1409 == FAIL)
1410 break;
1411 goto cmdline_changed;
1412 }
1413
1414#ifdef FEAT_CMDHIST
1415 case K_UP:
1416 case K_DOWN:
1417 case K_S_UP:
1418 case K_S_DOWN:
1419 case K_PAGEUP:
1420 case K_KPAGEUP:
1421 case K_PAGEDOWN:
1422 case K_KPAGEDOWN:
1423 if (hislen == 0 || firstc == NUL) /* no history */
1424 goto cmdline_not_changed;
1425
1426 i = hiscnt;
1427
1428 /* save current command string so it can be restored later */
1429 if (lookfor == NULL)
1430 {
1431 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1432 goto cmdline_not_changed;
1433 lookfor[ccline.cmdpos] = NUL;
1434 }
1435
1436 j = (int)STRLEN(lookfor);
1437 for (;;)
1438 {
1439 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001440 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001441 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 {
1443 if (hiscnt == hislen) /* first time */
1444 hiscnt = hisidx[histype];
1445 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1446 hiscnt = hislen - 1;
1447 else if (hiscnt != hisidx[histype] + 1)
1448 --hiscnt;
1449 else /* at top of list */
1450 {
1451 hiscnt = i;
1452 break;
1453 }
1454 }
1455 else /* one step forwards */
1456 {
1457 /* on last entry, clear the line */
1458 if (hiscnt == hisidx[histype])
1459 {
1460 hiscnt = hislen;
1461 break;
1462 }
1463
1464 /* not on a history line, nothing to do */
1465 if (hiscnt == hislen)
1466 break;
1467 if (hiscnt == hislen - 1) /* wrap around */
1468 hiscnt = 0;
1469 else
1470 ++hiscnt;
1471 }
1472 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1473 {
1474 hiscnt = i;
1475 break;
1476 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001477 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001478 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 || STRNCMP(history[histype][hiscnt].hisstr,
1480 lookfor, (size_t)j) == 0)
1481 break;
1482 }
1483
1484 if (hiscnt != i) /* jumped to other entry */
1485 {
1486 char_u *p;
1487 int len;
1488 int old_firstc;
1489
1490 vim_free(ccline.cmdbuff);
1491 if (hiscnt == hislen)
1492 p = lookfor; /* back to the old one */
1493 else
1494 p = history[histype][hiscnt].hisstr;
1495
1496 if (histype == HIST_SEARCH
1497 && p != lookfor
1498 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1499 {
1500 /* Correct for the separator character used when
1501 * adding the history entry vs the one used now.
1502 * First loop: count length.
1503 * Second loop: copy the characters. */
1504 for (i = 0; i <= 1; ++i)
1505 {
1506 len = 0;
1507 for (j = 0; p[j] != NUL; ++j)
1508 {
1509 /* Replace old sep with new sep, unless it is
1510 * escaped. */
1511 if (p[j] == old_firstc
1512 && (j == 0 || p[j - 1] != '\\'))
1513 {
1514 if (i > 0)
1515 ccline.cmdbuff[len] = firstc;
1516 }
1517 else
1518 {
1519 /* Escape new sep, unless it is already
1520 * escaped. */
1521 if (p[j] == firstc
1522 && (j == 0 || p[j - 1] != '\\'))
1523 {
1524 if (i > 0)
1525 ccline.cmdbuff[len] = '\\';
1526 ++len;
1527 }
1528 if (i > 0)
1529 ccline.cmdbuff[len] = p[j];
1530 }
1531 ++len;
1532 }
1533 if (i == 0)
1534 {
1535 alloc_cmdbuff(len);
1536 if (ccline.cmdbuff == NULL)
1537 goto returncmd;
1538 }
1539 }
1540 ccline.cmdbuff[len] = NUL;
1541 }
1542 else
1543 {
1544 alloc_cmdbuff((int)STRLEN(p));
1545 if (ccline.cmdbuff == NULL)
1546 goto returncmd;
1547 STRCPY(ccline.cmdbuff, p);
1548 }
1549
1550 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1551 redrawcmd();
1552 goto cmdline_changed;
1553 }
1554 beep_flush();
1555 goto cmdline_not_changed;
1556#endif
1557
1558 case Ctrl_V:
1559 case Ctrl_Q:
1560#ifdef FEAT_MOUSE
1561 ignore_drag_release = TRUE;
1562#endif
1563 putcmdline('^', TRUE);
1564 c = get_literal(); /* get next (two) character(s) */
1565 do_abbr = FALSE; /* don't do abbreviation now */
1566#ifdef FEAT_MBYTE
1567 /* may need to remove ^ when composing char was typed */
1568 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1569 {
1570 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1571 msg_putchar(' ');
1572 cursorcmd();
1573 }
1574#endif
1575 break;
1576
1577#ifdef FEAT_DIGRAPHS
1578 case Ctrl_K:
1579#ifdef FEAT_MOUSE
1580 ignore_drag_release = TRUE;
1581#endif
1582 putcmdline('?', TRUE);
1583#ifdef USE_ON_FLY_SCROLL
1584 dont_scroll = TRUE; /* disallow scrolling here */
1585#endif
1586 c = get_digraph(TRUE);
1587 if (c != NUL)
1588 break;
1589
1590 redrawcmd();
1591 goto cmdline_not_changed;
1592#endif /* FEAT_DIGRAPHS */
1593
1594#ifdef FEAT_RIGHTLEFT
1595 case Ctrl__: /* CTRL-_: switch language mode */
1596 if (!p_ari)
1597 break;
1598#ifdef FEAT_FKMAP
1599 if (p_altkeymap)
1600 {
1601 cmd_fkmap = !cmd_fkmap;
1602 if (cmd_fkmap) /* in Farsi always in Insert mode */
1603 ccline.overstrike = FALSE;
1604 }
1605 else /* Hebrew is default */
1606#endif
1607 cmd_hkmap = !cmd_hkmap;
1608 goto cmdline_not_changed;
1609#endif
1610
1611 default:
1612#ifdef UNIX
1613 if (c == intr_char)
1614 {
1615 gotesc = TRUE; /* will free ccline.cmdbuff after
1616 putting it in history */
1617 goto returncmd; /* back to Normal mode */
1618 }
1619#endif
1620 /*
1621 * Normal character with no special meaning. Just set mod_mask
1622 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1623 * the string <S-Space>. This should only happen after ^V.
1624 */
1625 if (!IS_SPECIAL(c))
1626 mod_mask = 0x0;
1627 break;
1628 }
1629 /*
1630 * End of switch on command line character.
1631 * We come here if we have a normal character.
1632 */
1633
1634 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1635#ifdef FEAT_MBYTE
1636 /* Add ABBR_OFF for characters above 0x100, this is
1637 * what check_abbr() expects. */
1638 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1639#endif
1640 c))
1641 goto cmdline_changed;
1642
1643 /*
1644 * put the character in the command line
1645 */
1646 if (IS_SPECIAL(c) || mod_mask != 0)
1647 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1648 else
1649 {
1650#ifdef FEAT_MBYTE
1651 if (has_mbyte)
1652 {
1653 j = (*mb_char2bytes)(c, IObuff);
1654 IObuff[j] = NUL; /* exclude composing chars */
1655 put_on_cmdline(IObuff, j, TRUE);
1656 }
1657 else
1658#endif
1659 {
1660 IObuff[0] = c;
1661 put_on_cmdline(IObuff, 1, TRUE);
1662 }
1663 }
1664 goto cmdline_changed;
1665
1666/*
1667 * This part implements incremental searches for "/" and "?"
1668 * Jump to cmdline_not_changed when a character has been read but the command
1669 * line did not change. Then we only search and redraw if something changed in
1670 * the past.
1671 * Jump to cmdline_changed when the command line did change.
1672 * (Sorry for the goto's, I know it is ugly).
1673 */
1674cmdline_not_changed:
1675#ifdef FEAT_SEARCH_EXTRA
1676 if (!incsearch_postponed)
1677 continue;
1678#endif
1679
1680cmdline_changed:
1681#ifdef FEAT_SEARCH_EXTRA
1682 /*
1683 * 'incsearch' highlighting.
1684 */
1685 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1686 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001687 pos_T end_pos;
1688
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 /* if there is a character waiting, search and redraw later */
1690 if (char_avail())
1691 {
1692 incsearch_postponed = TRUE;
1693 continue;
1694 }
1695 incsearch_postponed = FALSE;
1696 curwin->w_cursor = old_cursor; /* start at old position */
1697
1698 /* If there is no command line, don't do anything */
1699 if (ccline.cmdlen == 0)
1700 i = 0;
1701 else
1702 {
1703 cursor_off(); /* so the user knows we're busy */
1704 out_flush();
1705 ++emsg_off; /* So it doesn't beep if bad expr */
1706 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1707 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1708 --emsg_off;
1709 /* if interrupted while searching, behave like it failed */
1710 if (got_int)
1711 {
1712 (void)vpeekc(); /* remove <C-C> from input stream */
1713 got_int = FALSE; /* don't abandon the command line */
1714 i = 0;
1715 }
1716 else if (char_avail())
1717 /* cancelled searching because a char was typed */
1718 incsearch_postponed = TRUE;
1719 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001720 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 highlight_match = TRUE; /* highlight position */
1722 else
1723 highlight_match = FALSE; /* remove highlight */
1724
1725 /* first restore the old curwin values, so the screen is
1726 * positioned in the same way as the actual search command */
1727 curwin->w_leftcol = old_leftcol;
1728 curwin->w_topline = old_topline;
1729# ifdef FEAT_DIFF
1730 curwin->w_topfill = old_topfill;
1731# endif
1732 curwin->w_botline = old_botline;
1733 changed_cline_bef_curs();
1734 update_topline();
1735
1736 if (i != 0)
1737 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001738 pos_T save_pos = curwin->w_cursor;
1739
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001741 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 * moves the whole match onto the screen when 'nowrap' is set.
1743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 curwin->w_cursor.lnum += search_match_lines;
1745 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001746 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1747 {
1748 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1749 coladvance((colnr_T)MAXCOL);
1750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001752 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001753 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001755 else
1756 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001757
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001759# ifdef FEAT_WINDOWS
1760 /* May redraw the status line to show the cursor position. */
1761 if (p_ru && curwin->w_status_height > 0)
1762 curwin->w_redr_status = TRUE;
1763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001765 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001766 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001767 restore_cmdline(&save_ccline);
1768
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001769 /* Leave it at the end to make CTRL-R CTRL-W work. */
1770 if (i != 0)
1771 curwin->w_cursor = end_pos;
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 msg_starthere();
1774 redrawcmdline();
1775 did_incsearch = TRUE;
1776 }
1777#else /* FEAT_SEARCH_EXTRA */
1778 ;
1779#endif
1780
1781#ifdef FEAT_RIGHTLEFT
1782 if (cmdmsg_rl
1783# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001784 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785# endif
1786 )
1787 /* Always redraw the whole command line to fix shaping and
1788 * right-left typing. Not efficient, but it works. */
1789 redrawcmd();
1790#endif
1791 }
1792
1793returncmd:
1794
1795#ifdef FEAT_RIGHTLEFT
1796 cmdmsg_rl = FALSE;
1797#endif
1798
1799#ifdef FEAT_FKMAP
1800 cmd_fkmap = 0;
1801#endif
1802
1803 ExpandCleanup(&xpc);
1804
1805#ifdef FEAT_SEARCH_EXTRA
1806 if (did_incsearch)
1807 {
1808 curwin->w_cursor = old_cursor;
1809 curwin->w_curswant = old_curswant;
1810 curwin->w_leftcol = old_leftcol;
1811 curwin->w_topline = old_topline;
1812# ifdef FEAT_DIFF
1813 curwin->w_topfill = old_topfill;
1814# endif
1815 curwin->w_botline = old_botline;
1816 highlight_match = FALSE;
1817 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001818 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 }
1820#endif
1821
1822 if (ccline.cmdbuff != NULL)
1823 {
1824 /*
1825 * Put line in history buffer (":" and "=" only when it was typed).
1826 */
1827#ifdef FEAT_CMDHIST
1828 if (ccline.cmdlen && firstc != NUL
1829 && (some_key_typed || histype == HIST_SEARCH))
1830 {
1831 add_to_history(histype, ccline.cmdbuff, TRUE,
1832 histype == HIST_SEARCH ? firstc : NUL);
1833 if (firstc == ':')
1834 {
1835 vim_free(new_last_cmdline);
1836 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1837 }
1838 }
1839#endif
1840
1841 if (gotesc) /* abandon command line */
1842 {
1843 vim_free(ccline.cmdbuff);
1844 ccline.cmdbuff = NULL;
1845 if (msg_scrolled == 0)
1846 compute_cmdrow();
1847 MSG("");
1848 redraw_cmdline = TRUE;
1849 }
1850 }
1851
1852 /*
1853 * If the screen was shifted up, redraw the whole screen (later).
1854 * If the line is too long, clear it, so ruler and shown command do
1855 * not get printed in the middle of it.
1856 */
1857 msg_check();
1858 msg_scroll = save_msg_scroll;
1859 redir_off = FALSE;
1860
1861 /* When the command line was typed, no need for a wait-return prompt. */
1862 if (some_key_typed)
1863 need_wait_return = FALSE;
1864
1865 State = save_State;
1866#ifdef USE_IM_CONTROL
1867 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1868 im_save_status(b_im_ptr);
1869 im_set_active(FALSE);
1870#endif
1871#ifdef FEAT_MOUSE
1872 setmouse();
1873#endif
1874#ifdef CURSOR_SHAPE
1875 ui_cursor_shape(); /* may show different cursor shape */
1876#endif
1877
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001878 {
1879 char_u *p = ccline.cmdbuff;
1880
1881 /* Make ccline empty, getcmdline() may try to use it. */
1882 ccline.cmdbuff = NULL;
1883 return p;
1884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885}
1886
1887#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1888/*
1889 * Get a command line with a prompt.
1890 * This is prepared to be called recursively from getcmdline() (e.g. by
1891 * f_input() when evaluating an expression from CTRL-R =).
1892 * Returns the command line in allocated memory, or NULL.
1893 */
1894 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001895getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 int firstc;
1897 char_u *prompt; /* command line prompt */
1898 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001899 int xp_context; /* type of expansion */
1900 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 char_u *s;
1903 struct cmdline_info save_ccline;
1904 int msg_col_save = msg_col;
1905
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001906 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 ccline.cmdprompt = prompt;
1908 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001909# ifdef FEAT_EVAL
1910 ccline.xp_context = xp_context;
1911 ccline.xp_arg = xp_arg;
1912 ccline.input_fn = (firstc == '@');
1913# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001915 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 /* Restore msg_col, the prompt from input() may have changed it. */
1917 msg_col = msg_col_save;
1918
1919 return s;
1920}
1921#endif
1922
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001923/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001924 * Return TRUE when the text must not be changed and we can't switch to
1925 * another window or buffer. Used when editing the command line, evaluating
1926 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001927 */
1928 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001929text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001930{
1931#ifdef FEAT_CMDWIN
1932 if (cmdwin_type != 0)
1933 return TRUE;
1934#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001935 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001936}
1937
1938/*
1939 * Give an error message for a command that isn't allowed while the cmdline
1940 * window is open or editing the cmdline in another way.
1941 */
1942 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001943text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001944{
1945#ifdef FEAT_CMDWIN
1946 if (cmdwin_type != 0)
1947 EMSG(_(e_cmdwin));
1948 else
1949#endif
1950 EMSG(_(e_secure));
1951}
1952
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#if defined(FEAT_AUTOCMD) || defined(PROTO)
1954/*
1955 * Check if "curbuf_lock" is set and return TRUE when it is and give an error
1956 * message.
1957 */
1958 int
1959curbuf_locked()
1960{
1961 if (curbuf_lock > 0)
1962 {
1963 EMSG(_("E788: Not allowed to edit another buffer now"));
1964 return TRUE;
1965 }
1966 return FALSE;
1967}
1968#endif
1969
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 static int
1971cmdline_charsize(idx)
1972 int idx;
1973{
1974#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1975 if (cmdline_star > 0) /* showing '*', always 1 position */
1976 return 1;
1977#endif
1978 return ptr2cells(ccline.cmdbuff + idx);
1979}
1980
1981/*
1982 * Compute the offset of the cursor on the command line for the prompt and
1983 * indent.
1984 */
1985 static void
1986set_cmdspos()
1987{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001988 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 ccline.cmdspos = 1 + ccline.cmdindent;
1990 else
1991 ccline.cmdspos = 0 + ccline.cmdindent;
1992}
1993
1994/*
1995 * Compute the screen position for the cursor on the command line.
1996 */
1997 static void
1998set_cmdspos_cursor()
1999{
2000 int i, m, c;
2001
2002 set_cmdspos();
2003 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002004 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002006 if (m < 0) /* overflow, Columns or Rows at weird value */
2007 m = MAXCOL;
2008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 else
2010 m = MAXCOL;
2011 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2012 {
2013 c = cmdline_charsize(i);
2014#ifdef FEAT_MBYTE
2015 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2016 if (has_mbyte)
2017 correct_cmdspos(i, c);
2018#endif
2019 /* If the cmdline doesn't fit, put cursor on last visible char. */
2020 if ((ccline.cmdspos += c) >= m)
2021 {
2022 ccline.cmdpos = i - 1;
2023 ccline.cmdspos -= c;
2024 break;
2025 }
2026#ifdef FEAT_MBYTE
2027 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002028 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029#endif
2030 }
2031}
2032
2033#ifdef FEAT_MBYTE
2034/*
2035 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2036 * character that doesn't fit, so that a ">" must be displayed.
2037 */
2038 static void
2039correct_cmdspos(idx, cells)
2040 int idx;
2041 int cells;
2042{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002043 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2045 && ccline.cmdspos % Columns + cells > Columns)
2046 ccline.cmdspos++;
2047}
2048#endif
2049
2050/*
2051 * Get an Ex command line for the ":" command.
2052 */
2053/* ARGSUSED */
2054 char_u *
2055getexline(c, dummy, indent)
2056 int c; /* normally ':', NUL for ":append" */
2057 void *dummy; /* cookie not used */
2058 int indent; /* indent for inside conditionals */
2059{
2060 /* When executing a register, remove ':' that's in front of each line. */
2061 if (exec_from_reg && vpeekc() == ':')
2062 (void)vgetc();
2063 return getcmdline(c, 1L, indent);
2064}
2065
2066/*
2067 * Get an Ex command line for Ex mode.
2068 * In Ex mode we only use the OS supplied line editing features and no
2069 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002070 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 */
2072/* ARGSUSED */
2073 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074getexmodeline(promptc, dummy, indent)
2075 int promptc; /* normally ':', NUL for ":append" and '?' for
2076 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 void *dummy; /* cookie not used */
2078 int indent; /* indent for inside conditionals */
2079{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 garray_T line_ga;
2081 char_u *pend;
2082 int startcol = 0;
2083 int c1;
2084 int escaped = FALSE; /* CTRL-V typed */
2085 int vcol = 0;
2086 char_u *p;
2087 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088
2089 /* Switch cursor on now. This avoids that it happens after the "\n", which
2090 * confuses the system function that computes tabstops. */
2091 cursor_on();
2092
2093 /* always start in column 0; write a newline if necessary */
2094 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002095 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002097 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002099 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002100 if (p_prompt)
2101 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 while (indent-- > 0)
2103 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
2107 ga_init2(&line_ga, 1, 30);
2108
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002109 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002110 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002111 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002112 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002113 while (indent >= 8)
2114 {
2115 ga_append(&line_ga, TAB);
2116 msg_puts((char_u *)" ");
2117 indent -= 8;
2118 }
2119 while (indent-- > 0)
2120 {
2121 ga_append(&line_ga, ' ');
2122 msg_putchar(' ');
2123 }
2124 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002125 ++no_mapping;
2126 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002127
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 /*
2129 * Get the line, one character at a time.
2130 */
2131 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002132 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 {
2134 if (ga_grow(&line_ga, 40) == FAIL)
2135 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002136 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002138 /* Get one character at a time. Don't use inchar(), it can't handle
2139 * special characters. */
2140 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141
2142 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143 * Handle line editing.
2144 * Previously this was left to the system, putting the terminal in
2145 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002149 msg_putchar('\n');
2150 break;
2151 }
2152
2153 if (!escaped)
2154 {
2155 /* CR typed means "enter", which is NL */
2156 if (c1 == '\r')
2157 c1 = '\n';
2158
2159 if (c1 == BS || c1 == K_BS
2160 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 if (line_ga.ga_len > 0)
2163 {
2164 --line_ga.ga_len;
2165 goto redraw;
2166 }
2167 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002170 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002172 msg_col = startcol;
2173 msg_clr_eos();
2174 line_ga.ga_len = 0;
2175 continue;
2176 }
2177
2178 if (c1 == Ctrl_T)
2179 {
2180 p = (char_u *)line_ga.ga_data;
2181 p[line_ga.ga_len] = NUL;
2182 indent = get_indent_str(p, 8);
2183 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2184add_indent:
2185 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 char_u *s = skipwhite(p);
2188
2189 ga_grow(&line_ga, 1);
2190 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2191 *s = ' ';
2192 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194redraw:
2195 /* redraw the line */
2196 msg_col = startcol;
2197 windgoto(msg_row, msg_col);
2198 vcol = 0;
2199 for (p = (char_u *)line_ga.ga_data;
2200 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002202 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002204 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 msg_putchar(' ');
2207 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 msg_outtrans_len(p, 1);
2212 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
2214 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 msg_clr_eos();
2216 continue;
2217 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219 if (c1 == Ctrl_D)
2220 {
2221 /* Delete one shiftwidth. */
2222 p = (char_u *)line_ga.ga_data;
2223 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002225 if (prev_char == '^')
2226 ex_keep_indent = TRUE;
2227 indent = 0;
2228 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230 else
2231 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 p[line_ga.ga_len] = NUL;
2233 indent = get_indent_str(p, 8);
2234 --indent;
2235 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002237 while (get_indent_str(p, 8) > indent)
2238 {
2239 char_u *s = skipwhite(p);
2240
2241 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2242 --line_ga.ga_len;
2243 }
2244 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246
2247 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2248 {
2249 escaped = TRUE;
2250 continue;
2251 }
2252
2253 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2254 if (IS_SPECIAL(c1))
2255 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257
2258 if (IS_SPECIAL(c1))
2259 c1 = '?';
2260 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2261 prev_char = c1;
2262 if (c1 == '\n')
2263 msg_putchar('\n');
2264 else if (c1 == TAB)
2265 {
2266 /* Don't use chartabsize(), 'ts' can be different */
2267 do
2268 {
2269 msg_putchar(' ');
2270 } while (++vcol % 8);
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002274 msg_outtrans_len(
2275 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2276 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002278 ++line_ga.ga_len;
2279 escaped = FALSE;
2280
2281 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002282 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002283
2284 /* we are done when a NL is entered, but not when it comes after a
2285 * backslash */
2286 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2287 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002290 --pend;
2291 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 }
2294 }
2295
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296 --no_mapping;
2297 --allow_keys;
2298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 /* make following messages go to the next line */
2300 msg_didout = FALSE;
2301 msg_col = 0;
2302 if (msg_row < Rows - 1)
2303 ++msg_row;
2304 emsg_on_display = FALSE; /* don't want ui_delay() */
2305
2306 if (got_int)
2307 ga_clear(&line_ga);
2308
2309 return (char_u *)line_ga.ga_data;
2310}
2311
Bram Moolenaare344bea2005-09-01 20:46:49 +00002312# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2313 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314/*
2315 * Return TRUE if ccline.overstrike is on.
2316 */
2317 int
2318cmdline_overstrike()
2319{
2320 return ccline.overstrike;
2321}
2322
2323/*
2324 * Return TRUE if the cursor is at the end of the cmdline.
2325 */
2326 int
2327cmdline_at_end()
2328{
2329 return (ccline.cmdpos >= ccline.cmdlen);
2330}
2331#endif
2332
Bram Moolenaar9372a112005-12-06 19:59:18 +00002333#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334/*
2335 * Return the virtual column number at the current cursor position.
2336 * This is used by the IM code to obtain the start of the preedit string.
2337 */
2338 colnr_T
2339cmdline_getvcol_cursor()
2340{
2341 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2342 return MAXCOL;
2343
2344# ifdef FEAT_MBYTE
2345 if (has_mbyte)
2346 {
2347 colnr_T col;
2348 int i = 0;
2349
2350 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002351 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
2353 return col;
2354 }
2355 else
2356# endif
2357 return ccline.cmdpos;
2358}
2359#endif
2360
2361#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2362/*
2363 * If part of the command line is an IM preedit string, redraw it with
2364 * IM feedback attributes. The cursor position is restored after drawing.
2365 */
2366 static void
2367redrawcmd_preedit()
2368{
2369 if ((State & CMDLINE)
2370 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002371 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 && !p_imdisable
2373 && im_is_preediting())
2374 {
2375 int cmdpos = 0;
2376 int cmdspos;
2377 int old_row;
2378 int old_col;
2379 colnr_T col;
2380
2381 old_row = msg_row;
2382 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002383 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
2385# ifdef FEAT_MBYTE
2386 if (has_mbyte)
2387 {
2388 for (col = 0; col < preedit_start_col
2389 && cmdpos < ccline.cmdlen; ++col)
2390 {
2391 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002392 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 }
2394 }
2395 else
2396# endif
2397 {
2398 cmdspos += preedit_start_col;
2399 cmdpos += preedit_start_col;
2400 }
2401
2402 msg_row = cmdline_row + (cmdspos / (int)Columns);
2403 msg_col = cmdspos % (int)Columns;
2404 if (msg_row >= Rows)
2405 msg_row = Rows - 1;
2406
2407 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2408 {
2409 int char_len;
2410 int char_attr;
2411
2412 char_attr = im_get_feedback_attr(col);
2413 if (char_attr < 0)
2414 break; /* end of preedit string */
2415
2416# ifdef FEAT_MBYTE
2417 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002418 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 else
2420# endif
2421 char_len = 1;
2422
2423 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2424 cmdpos += char_len;
2425 }
2426
2427 msg_row = old_row;
2428 msg_col = old_col;
2429 }
2430}
2431#endif /* FEAT_XIM && FEAT_GUI_GTK */
2432
2433/*
2434 * Allocate a new command line buffer.
2435 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2436 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2437 */
2438 static void
2439alloc_cmdbuff(len)
2440 int len;
2441{
2442 /*
2443 * give some extra space to avoid having to allocate all the time
2444 */
2445 if (len < 80)
2446 len = 100;
2447 else
2448 len += 20;
2449
2450 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2451 ccline.cmdbufflen = len;
2452}
2453
2454/*
2455 * Re-allocate the command line to length len + something extra.
2456 * return FAIL for failure, OK otherwise
2457 */
2458 static int
2459realloc_cmdbuff(len)
2460 int len;
2461{
2462 char_u *p;
2463
2464 p = ccline.cmdbuff;
2465 alloc_cmdbuff(len); /* will get some more */
2466 if (ccline.cmdbuff == NULL) /* out of memory */
2467 {
2468 ccline.cmdbuff = p; /* keep the old one */
2469 return FAIL;
2470 }
2471 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2472 vim_free(p);
2473 return OK;
2474}
2475
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002476#if defined(FEAT_ARABIC) || defined(PROTO)
2477static char_u *arshape_buf = NULL;
2478
2479# if defined(EXITFREE) || defined(PROTO)
2480 void
2481free_cmdline_buf()
2482{
2483 vim_free(arshape_buf);
2484}
2485# endif
2486#endif
2487
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488/*
2489 * Draw part of the cmdline at the current cursor position. But draw stars
2490 * when cmdline_star is TRUE.
2491 */
2492 static void
2493draw_cmdline(start, len)
2494 int start;
2495 int len;
2496{
2497#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2498 int i;
2499
2500 if (cmdline_star > 0)
2501 for (i = 0; i < len; ++i)
2502 {
2503 msg_putchar('*');
2504# ifdef FEAT_MBYTE
2505 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002506 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507# endif
2508 }
2509 else
2510#endif
2511#ifdef FEAT_ARABIC
2512 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 static int buflen = 0;
2515 char_u *p;
2516 int j;
2517 int newlen = 0;
2518 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002519 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 int prev_c = 0;
2521 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002522 int u8c;
2523 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525
2526 /*
2527 * Do arabic shaping into a temporary buffer. This is very
2528 * inefficient!
2529 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002530 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 /* Re-allocate the buffer. We keep it around to avoid a lot of
2533 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002534 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002535 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002536 arshape_buf = alloc(buflen);
2537 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 return; /* out of memory */
2539 }
2540
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002541 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2542 {
2543 /* Prepend a space to draw the leading composing char on. */
2544 arshape_buf[0] = ' ';
2545 newlen = 1;
2546 }
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 for (j = start; j < start + len; j += mb_l)
2549 {
2550 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002551 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002552 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (ARABIC_CHAR(u8c))
2554 {
2555 /* Do Arabic shaping. */
2556 if (cmdmsg_rl)
2557 {
2558 /* displaying from right to left */
2559 pc = prev_c;
2560 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002561 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (j + mb_l >= start + len)
2563 nc = NUL;
2564 else
2565 nc = utf_ptr2char(p + mb_l);
2566 }
2567 else
2568 {
2569 /* displaying from left to right */
2570 if (j + mb_l >= start + len)
2571 pc = NUL;
2572 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002573 {
2574 int pcc[MAX_MCO];
2575
2576 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002578 pc1 = pcc[0];
2579 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 nc = prev_c;
2581 }
2582 prev_c = u8c;
2583
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002584 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002586 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002587 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002589 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2590 if (u8cc[1] != 0)
2591 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002592 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 }
2594 }
2595 else
2596 {
2597 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002598 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 newlen += mb_l;
2600 }
2601 }
2602
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002603 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 }
2605 else
2606#endif
2607 msg_outtrans_len(ccline.cmdbuff + start, len);
2608}
2609
2610/*
2611 * Put a character on the command line. Shifts the following text to the
2612 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2613 * "c" must be printable (fit in one display cell)!
2614 */
2615 void
2616putcmdline(c, shift)
2617 int c;
2618 int shift;
2619{
2620 if (cmd_silent)
2621 return;
2622 msg_no_more = TRUE;
2623 msg_putchar(c);
2624 if (shift)
2625 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2626 msg_no_more = FALSE;
2627 cursorcmd();
2628}
2629
2630/*
2631 * Undo a putcmdline(c, FALSE).
2632 */
2633 void
2634unputcmdline()
2635{
2636 if (cmd_silent)
2637 return;
2638 msg_no_more = TRUE;
2639 if (ccline.cmdlen == ccline.cmdpos)
2640 msg_putchar(' ');
2641 else
2642 draw_cmdline(ccline.cmdpos, 1);
2643 msg_no_more = FALSE;
2644 cursorcmd();
2645}
2646
2647/*
2648 * Put the given string, of the given length, onto the command line.
2649 * If len is -1, then STRLEN() is used to calculate the length.
2650 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2651 * part will be redrawn, otherwise it will not. If this function is called
2652 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2653 * called afterwards.
2654 */
2655 int
2656put_on_cmdline(str, len, redraw)
2657 char_u *str;
2658 int len;
2659 int redraw;
2660{
2661 int retval;
2662 int i;
2663 int m;
2664 int c;
2665
2666 if (len < 0)
2667 len = (int)STRLEN(str);
2668
2669 /* Check if ccline.cmdbuff needs to be longer */
2670 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2671 retval = realloc_cmdbuff(ccline.cmdlen + len);
2672 else
2673 retval = OK;
2674 if (retval == OK)
2675 {
2676 if (!ccline.overstrike)
2677 {
2678 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2679 ccline.cmdbuff + ccline.cmdpos,
2680 (size_t)(ccline.cmdlen - ccline.cmdpos));
2681 ccline.cmdlen += len;
2682 }
2683 else
2684 {
2685#ifdef FEAT_MBYTE
2686 if (has_mbyte)
2687 {
2688 /* Count nr of characters in the new string. */
2689 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002690 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 ++m;
2692 /* Count nr of bytes in cmdline that are overwritten by these
2693 * characters. */
2694 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002695 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 --m;
2697 if (i < ccline.cmdlen)
2698 {
2699 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2700 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2701 ccline.cmdlen += ccline.cmdpos + len - i;
2702 }
2703 else
2704 ccline.cmdlen = ccline.cmdpos + len;
2705 }
2706 else
2707#endif
2708 if (ccline.cmdpos + len > ccline.cmdlen)
2709 ccline.cmdlen = ccline.cmdpos + len;
2710 }
2711 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2712 ccline.cmdbuff[ccline.cmdlen] = NUL;
2713
2714#ifdef FEAT_MBYTE
2715 if (enc_utf8)
2716 {
2717 /* When the inserted text starts with a composing character,
2718 * backup to the character before it. There could be two of them.
2719 */
2720 i = 0;
2721 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2722 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2723 {
2724 i = (*mb_head_off)(ccline.cmdbuff,
2725 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2726 ccline.cmdpos -= i;
2727 len += i;
2728 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2729 }
2730# ifdef FEAT_ARABIC
2731 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2732 {
2733 /* Check the previous character for Arabic combining pair. */
2734 i = (*mb_head_off)(ccline.cmdbuff,
2735 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2736 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2737 + ccline.cmdpos - i), c))
2738 {
2739 ccline.cmdpos -= i;
2740 len += i;
2741 }
2742 else
2743 i = 0;
2744 }
2745# endif
2746 if (i != 0)
2747 {
2748 /* Also backup the cursor position. */
2749 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2750 ccline.cmdspos -= i;
2751 msg_col -= i;
2752 if (msg_col < 0)
2753 {
2754 msg_col += Columns;
2755 --msg_row;
2756 }
2757 }
2758 }
2759#endif
2760
2761 if (redraw && !cmd_silent)
2762 {
2763 msg_no_more = TRUE;
2764 i = cmdline_row;
2765 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2766 /* Avoid clearing the rest of the line too often. */
2767 if (cmdline_row != i || ccline.overstrike)
2768 msg_clr_eos();
2769 msg_no_more = FALSE;
2770 }
2771#ifdef FEAT_FKMAP
2772 /*
2773 * If we are in Farsi command mode, the character input must be in
2774 * Insert mode. So do not advance the cmdpos.
2775 */
2776 if (!cmd_fkmap)
2777#endif
2778 {
2779 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002780 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002782 if (m < 0) /* overflow, Columns or Rows at weird value */
2783 m = MAXCOL;
2784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 else
2786 m = MAXCOL;
2787 for (i = 0; i < len; ++i)
2788 {
2789 c = cmdline_charsize(ccline.cmdpos);
2790#ifdef FEAT_MBYTE
2791 /* count ">" for a double-wide char that doesn't fit. */
2792 if (has_mbyte)
2793 correct_cmdspos(ccline.cmdpos, c);
2794#endif
2795 /* Stop cursor at the end of the screen */
2796 if (ccline.cmdspos + c >= m)
2797 break;
2798 ccline.cmdspos += c;
2799#ifdef FEAT_MBYTE
2800 if (has_mbyte)
2801 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002802 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 if (c > len - i - 1)
2804 c = len - i - 1;
2805 ccline.cmdpos += c;
2806 i += c;
2807 }
2808#endif
2809 ++ccline.cmdpos;
2810 }
2811 }
2812 }
2813 if (redraw)
2814 msg_check();
2815 return retval;
2816}
2817
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002818static struct cmdline_info prev_ccline;
2819static int prev_ccline_used = FALSE;
2820
2821/*
2822 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2823 * and overwrite it. But get_cmdline_str() may need it, thus make it
2824 * available globally in prev_ccline.
2825 */
2826 static void
2827save_cmdline(ccp)
2828 struct cmdline_info *ccp;
2829{
2830 if (!prev_ccline_used)
2831 {
2832 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2833 prev_ccline_used = TRUE;
2834 }
2835 *ccp = prev_ccline;
2836 prev_ccline = ccline;
2837 ccline.cmdbuff = NULL;
2838 ccline.cmdprompt = NULL;
2839}
2840
2841/*
2842 * Resture ccline after it has been saved with save_cmdline().
2843 */
2844 static void
2845restore_cmdline(ccp)
2846 struct cmdline_info *ccp;
2847{
2848 ccline = prev_ccline;
2849 prev_ccline = *ccp;
2850}
2851
Bram Moolenaar5a305422006-04-28 22:38:25 +00002852#if defined(FEAT_EVAL) || defined(PROTO)
2853/*
2854 * Save the command line into allocated memory. Returns a pointer to be
2855 * passed to restore_cmdline_alloc() later.
2856 * Returns NULL when failed.
2857 */
2858 char_u *
2859save_cmdline_alloc()
2860{
2861 struct cmdline_info *p;
2862
2863 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
2864 if (p != NULL)
2865 save_cmdline(p);
2866 return (char_u *)p;
2867}
2868
2869/*
2870 * Restore the command line from the return value of save_cmdline_alloc().
2871 */
2872 void
2873restore_cmdline_alloc(p)
2874 char_u *p;
2875{
2876 if (p != NULL)
2877 {
2878 restore_cmdline((struct cmdline_info *)p);
2879 vim_free(p);
2880 }
2881}
2882#endif
2883
Bram Moolenaar8299df92004-07-10 09:47:34 +00002884/*
2885 * paste a yank register into the command line.
2886 * used by CTRL-R command in command-line mode
2887 * insert_reg() can't be used here, because special characters from the
2888 * register contents will be interpreted as commands.
2889 *
2890 * return FAIL for failure, OK otherwise
2891 */
2892 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002893cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00002894 int regname;
2895 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002896 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00002897{
2898 long i;
2899 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002900 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002901 int allocated;
2902 struct cmdline_info save_ccline;
2903
2904 /* check for valid regname; also accept special characters for CTRL-R in
2905 * the command line */
2906 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2907 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2908 return FAIL;
2909
2910 /* A register containing CTRL-R can cause an endless loop. Allow using
2911 * CTRL-C to break the loop. */
2912 line_breakcheck();
2913 if (got_int)
2914 return FAIL;
2915
2916#ifdef FEAT_CLIPBOARD
2917 regname = may_get_selection(regname);
2918#endif
2919
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002920 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002921 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002922 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002923 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002924 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002925 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002926 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002927
2928 if (i)
2929 {
2930 /* Got the value of a special register in "arg". */
2931 if (arg == NULL)
2932 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002933
2934 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
2935 * part of the word. */
2936 p = arg;
2937 if (p_is && regname == Ctrl_W)
2938 {
2939 char_u *w;
2940 int len;
2941
2942 /* Locate start of last word in the cmd buffer. */
2943 for (w = ccline.cmdbuff + ccline.cmdlen; w > ccline.cmdbuff; )
2944 {
2945#ifdef FEAT_MBYTE
2946 if (has_mbyte)
2947 {
2948 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
2949 if (!vim_iswordc(mb_ptr2char(w - len)))
2950 break;
2951 w -= len;
2952 }
2953 else
2954#endif
2955 {
2956 if (!vim_iswordc(w[-1]))
2957 break;
2958 --w;
2959 }
2960 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002961 len = (int)((ccline.cmdbuff + ccline.cmdlen) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002962 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
2963 p += len;
2964 }
2965
2966 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002967 if (allocated)
2968 vim_free(arg);
2969 return OK;
2970 }
2971
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00002972 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002973}
2974
2975/*
2976 * Put a string on the command line.
2977 * When "literally" is TRUE, insert literally.
2978 * When "literally" is FALSE, insert as typed, but don't leave the command
2979 * line.
2980 */
2981 void
2982cmdline_paste_str(s, literally)
2983 char_u *s;
2984 int literally;
2985{
2986 int c, cv;
2987
2988 if (literally)
2989 put_on_cmdline(s, -1, TRUE);
2990 else
2991 while (*s != NUL)
2992 {
2993 cv = *s;
2994 if (cv == Ctrl_V && s[1])
2995 ++s;
2996#ifdef FEAT_MBYTE
2997 if (has_mbyte)
2998 {
2999 c = mb_ptr2char(s);
3000 s += mb_char2len(c);
3001 }
3002 else
3003#endif
3004 c = *s++;
3005 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
3006#ifdef UNIX
3007 || c == intr_char
3008#endif
3009 || (c == Ctrl_BSL && *s == Ctrl_N))
3010 stuffcharReadbuff(Ctrl_V);
3011 stuffcharReadbuff(c);
3012 }
3013}
3014
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015#ifdef FEAT_WILDMENU
3016/*
3017 * Delete characters on the command line, from "from" to the current
3018 * position.
3019 */
3020 static void
3021cmdline_del(from)
3022 int from;
3023{
3024 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3025 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3026 ccline.cmdlen -= ccline.cmdpos - from;
3027 ccline.cmdpos = from;
3028}
3029#endif
3030
3031/*
3032 * this fuction is called when the screen size changes and with incremental
3033 * search
3034 */
3035 void
3036redrawcmdline()
3037{
3038 if (cmd_silent)
3039 return;
3040 need_wait_return = FALSE;
3041 compute_cmdrow();
3042 redrawcmd();
3043 cursorcmd();
3044}
3045
3046 static void
3047redrawcmdprompt()
3048{
3049 int i;
3050
3051 if (cmd_silent)
3052 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003053 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 msg_putchar(ccline.cmdfirstc);
3055 if (ccline.cmdprompt != NULL)
3056 {
3057 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3058 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3059 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003060 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 --ccline.cmdindent;
3062 }
3063 else
3064 for (i = ccline.cmdindent; i > 0; --i)
3065 msg_putchar(' ');
3066}
3067
3068/*
3069 * Redraw what is currently on the command line.
3070 */
3071 void
3072redrawcmd()
3073{
3074 if (cmd_silent)
3075 return;
3076
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003077 /* when 'incsearch' is set there may be no command line while redrawing */
3078 if (ccline.cmdbuff == NULL)
3079 {
3080 windgoto(cmdline_row, 0);
3081 msg_clr_eos();
3082 return;
3083 }
3084
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 msg_start();
3086 redrawcmdprompt();
3087
3088 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3089 msg_no_more = TRUE;
3090 draw_cmdline(0, ccline.cmdlen);
3091 msg_clr_eos();
3092 msg_no_more = FALSE;
3093
3094 set_cmdspos_cursor();
3095
3096 /*
3097 * An emsg() before may have set msg_scroll. This is used in normal mode,
3098 * in cmdline mode we can reset them now.
3099 */
3100 msg_scroll = FALSE; /* next message overwrites cmdline */
3101
3102 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3103 * in cmdline mode */
3104 skip_redraw = FALSE;
3105}
3106
3107 void
3108compute_cmdrow()
3109{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003110 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 cmdline_row = Rows - 1;
3112 else
3113 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3114 + W_STATUS_HEIGHT(lastwin);
3115}
3116
3117 static void
3118cursorcmd()
3119{
3120 if (cmd_silent)
3121 return;
3122
3123#ifdef FEAT_RIGHTLEFT
3124 if (cmdmsg_rl)
3125 {
3126 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3127 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3128 if (msg_row <= 0)
3129 msg_row = Rows - 1;
3130 }
3131 else
3132#endif
3133 {
3134 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3135 msg_col = ccline.cmdspos % (int)Columns;
3136 if (msg_row >= Rows)
3137 msg_row = Rows - 1;
3138 }
3139
3140 windgoto(msg_row, msg_col);
3141#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3142 redrawcmd_preedit();
3143#endif
3144#ifdef MCH_CURSOR_SHAPE
3145 mch_update_cursor();
3146#endif
3147}
3148
3149 void
3150gotocmdline(clr)
3151 int clr;
3152{
3153 msg_start();
3154#ifdef FEAT_RIGHTLEFT
3155 if (cmdmsg_rl)
3156 msg_col = Columns - 1;
3157 else
3158#endif
3159 msg_col = 0; /* always start in column 0 */
3160 if (clr) /* clear the bottom line(s) */
3161 msg_clr_eos(); /* will reset clear_cmdline */
3162 windgoto(cmdline_row, 0);
3163}
3164
3165/*
3166 * Check the word in front of the cursor for an abbreviation.
3167 * Called when the non-id character "c" has been entered.
3168 * When an abbreviation is recognized it is removed from the text with
3169 * backspaces and the replacement string is inserted, followed by "c".
3170 */
3171 static int
3172ccheck_abbr(c)
3173 int c;
3174{
3175 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3176 return FALSE;
3177
3178 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3179}
3180
3181/*
3182 * Return FAIL if this is not an appropriate context in which to do
3183 * completion of anything, return OK if it is (even if there are no matches).
3184 * For the caller, this means that the character is just passed through like a
3185 * normal character (instead of being expanded). This allows :s/^I^D etc.
3186 */
3187 static int
3188nextwild(xp, type, options)
3189 expand_T *xp;
3190 int type;
3191 int options; /* extra options for ExpandOne() */
3192{
3193 int i, j;
3194 char_u *p1;
3195 char_u *p2;
3196 int oldlen;
3197 int difflen;
3198 int v;
3199
3200 if (xp->xp_numfiles == -1)
3201 {
3202 set_expand_context(xp);
3203 cmd_showtail = expand_showtail(xp);
3204 }
3205
3206 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3207 {
3208 beep_flush();
3209 return OK; /* Something illegal on command line */
3210 }
3211 if (xp->xp_context == EXPAND_NOTHING)
3212 {
3213 /* Caller can use the character as a normal char instead */
3214 return FAIL;
3215 }
3216
3217 MSG_PUTS("..."); /* show that we are busy */
3218 out_flush();
3219
3220 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3221 oldlen = ccline.cmdpos - i;
3222
3223 if (type == WILD_NEXT || type == WILD_PREV)
3224 {
3225 /*
3226 * Get next/previous match for a previous expanded pattern.
3227 */
3228 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3229 }
3230 else
3231 {
3232 /*
3233 * Translate string into pattern and expand it.
3234 */
3235 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3236 p2 = NULL;
3237 else
3238 {
3239 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3240 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3241 |options, type);
3242 vim_free(p1);
3243 /* longest match: make sure it is not shorter (happens with :help */
3244 if (p2 != NULL && type == WILD_LONGEST)
3245 {
3246 for (j = 0; j < oldlen; ++j)
3247 if (ccline.cmdbuff[i + j] == '*'
3248 || ccline.cmdbuff[i + j] == '?')
3249 break;
3250 if ((int)STRLEN(p2) < j)
3251 {
3252 vim_free(p2);
3253 p2 = NULL;
3254 }
3255 }
3256 }
3257 }
3258
3259 if (p2 != NULL && !got_int)
3260 {
3261 difflen = (int)STRLEN(p2) - oldlen;
3262 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3263 {
3264 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3265 xp->xp_pattern = ccline.cmdbuff + i;
3266 }
3267 else
3268 v = OK;
3269 if (v == OK)
3270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003271 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3272 &ccline.cmdbuff[ccline.cmdpos],
3273 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3274 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 ccline.cmdlen += difflen;
3276 ccline.cmdpos += difflen;
3277 }
3278 }
3279 vim_free(p2);
3280
3281 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003282 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283
3284 /* When expanding a ":map" command and no matches are found, assume that
3285 * the key is supposed to be inserted literally */
3286 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3287 return FAIL;
3288
3289 if (xp->xp_numfiles <= 0 && p2 == NULL)
3290 beep_flush();
3291 else if (xp->xp_numfiles == 1)
3292 /* free expanded pattern */
3293 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3294
3295 return OK;
3296}
3297
3298/*
3299 * Do wildcard expansion on the string 'str'.
3300 * Chars that should not be expanded must be preceded with a backslash.
3301 * Return a pointer to alloced memory containing the new string.
3302 * Return NULL for failure.
3303 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003304 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3305 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 *
3307 * mode = WILD_FREE: just free previously expanded matches
3308 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3309 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3310 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3311 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3312 * mode = WILD_ALL: return all matches concatenated
3313 * mode = WILD_LONGEST: return longest matched part
3314 *
3315 * options = WILD_LIST_NOTFOUND: list entries without a match
3316 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3317 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3318 * options = WILD_NO_BEEP: Don't beep for multiple matches
3319 * options = WILD_ADD_SLASH: add a slash after directory names
3320 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3321 * options = WILD_SILENT: don't print warning messages
3322 * options = WILD_ESCAPE: put backslash before special chars
3323 *
3324 * The variables xp->xp_context and xp->xp_backslash must have been set!
3325 */
3326 char_u *
3327ExpandOne(xp, str, orig, options, mode)
3328 expand_T *xp;
3329 char_u *str;
3330 char_u *orig; /* allocated copy of original of expanded string */
3331 int options;
3332 int mode;
3333{
3334 char_u *ss = NULL;
3335 static int findex;
3336 static char_u *orig_save = NULL; /* kept value of orig */
3337 int i;
3338 long_u len;
3339 int non_suf_match; /* number without matching suffix */
3340
3341 /*
3342 * first handle the case of using an old match
3343 */
3344 if (mode == WILD_NEXT || mode == WILD_PREV)
3345 {
3346 if (xp->xp_numfiles > 0)
3347 {
3348 if (mode == WILD_PREV)
3349 {
3350 if (findex == -1)
3351 findex = xp->xp_numfiles;
3352 --findex;
3353 }
3354 else /* mode == WILD_NEXT */
3355 ++findex;
3356
3357 /*
3358 * When wrapping around, return the original string, set findex to
3359 * -1.
3360 */
3361 if (findex < 0)
3362 {
3363 if (orig_save == NULL)
3364 findex = xp->xp_numfiles - 1;
3365 else
3366 findex = -1;
3367 }
3368 if (findex >= xp->xp_numfiles)
3369 {
3370 if (orig_save == NULL)
3371 findex = 0;
3372 else
3373 findex = -1;
3374 }
3375#ifdef FEAT_WILDMENU
3376 if (p_wmnu)
3377 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3378 findex, cmd_showtail);
3379#endif
3380 if (findex == -1)
3381 return vim_strsave(orig_save);
3382 return vim_strsave(xp->xp_files[findex]);
3383 }
3384 else
3385 return NULL;
3386 }
3387
3388/* free old names */
3389 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3390 {
3391 FreeWild(xp->xp_numfiles, xp->xp_files);
3392 xp->xp_numfiles = -1;
3393 vim_free(orig_save);
3394 orig_save = NULL;
3395 }
3396 findex = 0;
3397
3398 if (mode == WILD_FREE) /* only release file name */
3399 return NULL;
3400
3401 if (xp->xp_numfiles == -1)
3402 {
3403 vim_free(orig_save);
3404 orig_save = orig;
3405
3406 /*
3407 * Do the expansion.
3408 */
3409 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3410 options) == FAIL)
3411 {
3412#ifdef FNAME_ILLEGAL
3413 /* Illegal file name has been silently skipped. But when there
3414 * are wildcards, the real problem is that there was no match,
3415 * causing the pattern to be added, which has illegal characters.
3416 */
3417 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3418 EMSG2(_(e_nomatch2), str);
3419#endif
3420 }
3421 else if (xp->xp_numfiles == 0)
3422 {
3423 if (!(options & WILD_SILENT))
3424 EMSG2(_(e_nomatch2), str);
3425 }
3426 else
3427 {
3428 /* Escape the matches for use on the command line. */
3429 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3430
3431 /*
3432 * Check for matching suffixes in file names.
3433 */
3434 if (mode != WILD_ALL && mode != WILD_LONGEST)
3435 {
3436 if (xp->xp_numfiles)
3437 non_suf_match = xp->xp_numfiles;
3438 else
3439 non_suf_match = 1;
3440 if ((xp->xp_context == EXPAND_FILES
3441 || xp->xp_context == EXPAND_DIRECTORIES)
3442 && xp->xp_numfiles > 1)
3443 {
3444 /*
3445 * More than one match; check suffix.
3446 * The files will have been sorted on matching suffix in
3447 * expand_wildcards, only need to check the first two.
3448 */
3449 non_suf_match = 0;
3450 for (i = 0; i < 2; ++i)
3451 if (match_suffix(xp->xp_files[i]))
3452 ++non_suf_match;
3453 }
3454 if (non_suf_match != 1)
3455 {
3456 /* Can we ever get here unless it's while expanding
3457 * interactively? If not, we can get rid of this all
3458 * together. Don't really want to wait for this message
3459 * (and possibly have to hit return to continue!).
3460 */
3461 if (!(options & WILD_SILENT))
3462 EMSG(_(e_toomany));
3463 else if (!(options & WILD_NO_BEEP))
3464 beep_flush();
3465 }
3466 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3467 ss = vim_strsave(xp->xp_files[0]);
3468 }
3469 }
3470 }
3471
3472 /* Find longest common part */
3473 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3474 {
3475 for (len = 0; xp->xp_files[0][len]; ++len)
3476 {
3477 for (i = 0; i < xp->xp_numfiles; ++i)
3478 {
3479#ifdef CASE_INSENSITIVE_FILENAME
3480 if (xp->xp_context == EXPAND_DIRECTORIES
3481 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003482 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 || xp->xp_context == EXPAND_BUFFERS)
3484 {
3485 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3486 TOLOWER_LOC(xp->xp_files[0][len]))
3487 break;
3488 }
3489 else
3490#endif
3491 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3492 break;
3493 }
3494 if (i < xp->xp_numfiles)
3495 {
3496 if (!(options & WILD_NO_BEEP))
3497 vim_beep();
3498 break;
3499 }
3500 }
3501 ss = alloc((unsigned)len + 1);
3502 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003503 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 findex = -1; /* next p_wc gets first one */
3505 }
3506
3507 /* Concatenate all matching names */
3508 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3509 {
3510 len = 0;
3511 for (i = 0; i < xp->xp_numfiles; ++i)
3512 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3513 ss = lalloc(len, TRUE);
3514 if (ss != NULL)
3515 {
3516 *ss = NUL;
3517 for (i = 0; i < xp->xp_numfiles; ++i)
3518 {
3519 STRCAT(ss, xp->xp_files[i]);
3520 if (i != xp->xp_numfiles - 1)
3521 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3522 }
3523 }
3524 }
3525
3526 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3527 ExpandCleanup(xp);
3528
3529 return ss;
3530}
3531
3532/*
3533 * Prepare an expand structure for use.
3534 */
3535 void
3536ExpandInit(xp)
3537 expand_T *xp;
3538{
3539 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003540#ifndef BACKSLASH_IN_FILENAME
3541 xp->xp_shell = FALSE;
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 xp->xp_numfiles = -1;
3544 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003545#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3546 xp->xp_arg = NULL;
3547#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548}
3549
3550/*
3551 * Cleanup an expand structure after use.
3552 */
3553 void
3554ExpandCleanup(xp)
3555 expand_T *xp;
3556{
3557 if (xp->xp_numfiles >= 0)
3558 {
3559 FreeWild(xp->xp_numfiles, xp->xp_files);
3560 xp->xp_numfiles = -1;
3561 }
3562}
3563
3564 void
3565ExpandEscape(xp, str, numfiles, files, options)
3566 expand_T *xp;
3567 char_u *str;
3568 int numfiles;
3569 char_u **files;
3570 int options;
3571{
3572 int i;
3573 char_u *p;
3574
3575 /*
3576 * May change home directory back to "~"
3577 */
3578 if (options & WILD_HOME_REPLACE)
3579 tilde_replace(str, numfiles, files);
3580
3581 if (options & WILD_ESCAPE)
3582 {
3583 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003584 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 || xp->xp_context == EXPAND_BUFFERS
3586 || xp->xp_context == EXPAND_DIRECTORIES)
3587 {
3588 /*
3589 * Insert a backslash into a file name before a space, \, %, #
3590 * and wildmatch characters, except '~'.
3591 */
3592 for (i = 0; i < numfiles; ++i)
3593 {
3594 /* for ":set path=" we need to escape spaces twice */
3595 if (xp->xp_backslash == XP_BS_THREE)
3596 {
3597 p = vim_strsave_escaped(files[i], (char_u *)" ");
3598 if (p != NULL)
3599 {
3600 vim_free(files[i]);
3601 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003602#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 p = vim_strsave_escaped(files[i], (char_u *)" ");
3604 if (p != NULL)
3605 {
3606 vim_free(files[i]);
3607 files[i] = p;
3608 }
3609#endif
3610 }
3611 }
3612#ifdef BACKSLASH_IN_FILENAME
3613 {
3614 char_u buf[20];
3615 int j = 0;
3616
3617 /* Don't escape '[' and '{' if they are in 'isfname'. */
3618 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3619 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3620 buf[j++] = *p;
3621 buf[j] = NUL;
3622 p = vim_strsave_escaped(files[i], buf);
3623 }
3624#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003625 p = vim_strsave_escaped(files[i],
3626 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627#endif
3628 if (p != NULL)
3629 {
3630 vim_free(files[i]);
3631 files[i] = p;
3632 }
3633
3634 /* If 'str' starts with "\~", replace "~" at start of
3635 * files[i] with "\~". */
3636 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003637 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003640
3641 /* If the first file starts with a '+' escape it. Otherwise it
3642 * could be seen as "+cmd". */
3643 if (*files[0] == '+')
3644 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646 else if (xp->xp_context == EXPAND_TAGS)
3647 {
3648 /*
3649 * Insert a backslash before characters in a tag name that
3650 * would terminate the ":tag" command.
3651 */
3652 for (i = 0; i < numfiles; ++i)
3653 {
3654 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3655 if (p != NULL)
3656 {
3657 vim_free(files[i]);
3658 files[i] = p;
3659 }
3660 }
3661 }
3662 }
3663}
3664
3665/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003666 * Put a backslash before the file name in "pp", which is in allocated memory.
3667 */
3668 static void
3669escape_fname(pp)
3670 char_u **pp;
3671{
3672 char_u *p;
3673
3674 p = alloc((unsigned)(STRLEN(*pp) + 2));
3675 if (p != NULL)
3676 {
3677 p[0] = '\\';
3678 STRCPY(p + 1, *pp);
3679 vim_free(*pp);
3680 *pp = p;
3681 }
3682}
3683
3684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 * For each file name in files[num_files]:
3686 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3687 */
3688 void
3689tilde_replace(orig_pat, num_files, files)
3690 char_u *orig_pat;
3691 int num_files;
3692 char_u **files;
3693{
3694 int i;
3695 char_u *p;
3696
3697 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3698 {
3699 for (i = 0; i < num_files; ++i)
3700 {
3701 p = home_replace_save(NULL, files[i]);
3702 if (p != NULL)
3703 {
3704 vim_free(files[i]);
3705 files[i] = p;
3706 }
3707 }
3708 }
3709}
3710
3711/*
3712 * Show all matches for completion on the command line.
3713 * Returns EXPAND_NOTHING when the character that triggered expansion should
3714 * be inserted like a normal character.
3715 */
3716/*ARGSUSED*/
3717 static int
3718showmatches(xp, wildmenu)
3719 expand_T *xp;
3720 int wildmenu;
3721{
3722#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3723 int num_files;
3724 char_u **files_found;
3725 int i, j, k;
3726 int maxlen;
3727 int lines;
3728 int columns;
3729 char_u *p;
3730 int lastlen;
3731 int attr;
3732 int showtail;
3733
3734 if (xp->xp_numfiles == -1)
3735 {
3736 set_expand_context(xp);
3737 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3738 &num_files, &files_found);
3739 showtail = expand_showtail(xp);
3740 if (i != EXPAND_OK)
3741 return i;
3742
3743 }
3744 else
3745 {
3746 num_files = xp->xp_numfiles;
3747 files_found = xp->xp_files;
3748 showtail = cmd_showtail;
3749 }
3750
3751#ifdef FEAT_WILDMENU
3752 if (!wildmenu)
3753 {
3754#endif
3755 msg_didany = FALSE; /* lines_left will be set */
3756 msg_start(); /* prepare for paging */
3757 msg_putchar('\n');
3758 out_flush();
3759 cmdline_row = msg_row;
3760 msg_didany = FALSE; /* lines_left will be set again */
3761 msg_start(); /* prepare for paging */
3762#ifdef FEAT_WILDMENU
3763 }
3764#endif
3765
3766 if (got_int)
3767 got_int = FALSE; /* only int. the completion, not the cmd line */
3768#ifdef FEAT_WILDMENU
3769 else if (wildmenu)
3770 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3771#endif
3772 else
3773 {
3774 /* find the length of the longest file name */
3775 maxlen = 0;
3776 for (i = 0; i < num_files; ++i)
3777 {
3778 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003779 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 || xp->xp_context == EXPAND_BUFFERS))
3781 {
3782 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3783 j = vim_strsize(NameBuff);
3784 }
3785 else
3786 j = vim_strsize(L_SHOWFILE(i));
3787 if (j > maxlen)
3788 maxlen = j;
3789 }
3790
3791 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3792 lines = num_files;
3793 else
3794 {
3795 /* compute the number of columns and lines for the listing */
3796 maxlen += 2; /* two spaces between file names */
3797 columns = ((int)Columns + 2) / maxlen;
3798 if (columns < 1)
3799 columns = 1;
3800 lines = (num_files + columns - 1) / columns;
3801 }
3802
3803 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3804
3805 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3806 {
3807 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3808 msg_clr_eos();
3809 msg_advance(maxlen - 3);
3810 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3811 }
3812
3813 /* list the files line by line */
3814 for (i = 0; i < lines; ++i)
3815 {
3816 lastlen = 999;
3817 for (k = i; k < num_files; k += lines)
3818 {
3819 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3820 {
3821 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3822 p = files_found[k] + STRLEN(files_found[k]) + 1;
3823 msg_advance(maxlen + 1);
3824 msg_puts(p);
3825 msg_advance(maxlen + 3);
3826 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3827 break;
3828 }
3829 for (j = maxlen - lastlen; --j >= 0; )
3830 msg_putchar(' ');
3831 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003832 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 || xp->xp_context == EXPAND_BUFFERS)
3834 {
3835 /* highlight directories */
3836 j = (mch_isdir(files_found[k]));
3837 if (showtail)
3838 p = L_SHOWFILE(k);
3839 else
3840 {
3841 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3842 TRUE);
3843 p = NameBuff;
3844 }
3845 }
3846 else
3847 {
3848 j = FALSE;
3849 p = L_SHOWFILE(k);
3850 }
3851 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3852 }
3853 if (msg_col > 0) /* when not wrapped around */
3854 {
3855 msg_clr_eos();
3856 msg_putchar('\n');
3857 }
3858 out_flush(); /* show one line at a time */
3859 if (got_int)
3860 {
3861 got_int = FALSE;
3862 break;
3863 }
3864 }
3865
3866 /*
3867 * we redraw the command below the lines that we have just listed
3868 * This is a bit tricky, but it saves a lot of screen updating.
3869 */
3870 cmdline_row = msg_row; /* will put it back later */
3871 }
3872
3873 if (xp->xp_numfiles == -1)
3874 FreeWild(num_files, files_found);
3875
3876 return EXPAND_OK;
3877}
3878
3879/*
3880 * Private gettail for showmatches() (and win_redr_status_matches()):
3881 * Find tail of file name path, but ignore trailing "/".
3882 */
3883 char_u *
3884sm_gettail(s)
3885 char_u *s;
3886{
3887 char_u *p;
3888 char_u *t = s;
3889 int had_sep = FALSE;
3890
3891 for (p = s; *p != NUL; )
3892 {
3893 if (vim_ispathsep(*p)
3894#ifdef BACKSLASH_IN_FILENAME
3895 && !rem_backslash(p)
3896#endif
3897 )
3898 had_sep = TRUE;
3899 else if (had_sep)
3900 {
3901 t = p;
3902 had_sep = FALSE;
3903 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003904 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 }
3906 return t;
3907}
3908
3909/*
3910 * Return TRUE if we only need to show the tail of completion matches.
3911 * When not completing file names or there is a wildcard in the path FALSE is
3912 * returned.
3913 */
3914 static int
3915expand_showtail(xp)
3916 expand_T *xp;
3917{
3918 char_u *s;
3919 char_u *end;
3920
3921 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003922 if (xp->xp_context != EXPAND_FILES
3923 && xp->xp_context != EXPAND_SHELLCMD
3924 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 return FALSE;
3926
3927 end = gettail(xp->xp_pattern);
3928 if (end == xp->xp_pattern) /* there is no path separator */
3929 return FALSE;
3930
3931 for (s = xp->xp_pattern; s < end; s++)
3932 {
3933 /* Skip escaped wildcards. Only when the backslash is not a path
3934 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3935 if (rem_backslash(s))
3936 ++s;
3937 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3938 return FALSE;
3939 }
3940 return TRUE;
3941}
3942
3943/*
3944 * Prepare a string for expansion.
3945 * When expanding file names: The string will be used with expand_wildcards().
3946 * Copy the file name into allocated memory and add a '*' at the end.
3947 * When expanding other names: The string will be used with regcomp(). Copy
3948 * the name into allocated memory and prepend "^".
3949 */
3950 char_u *
3951addstar(fname, len, context)
3952 char_u *fname;
3953 int len;
3954 int context; /* EXPAND_FILES etc. */
3955{
3956 char_u *retval;
3957 int i, j;
3958 int new_len;
3959 char_u *tail;
3960
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003961 if (context != EXPAND_FILES
3962 && context != EXPAND_SHELLCMD
3963 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 {
3965 /*
3966 * Matching will be done internally (on something other than files).
3967 * So we convert the file-matching-type wildcards into our kind for
3968 * use with vim_regcomp(). First work out how long it will be:
3969 */
3970
3971 /* For help tags the translation is done in find_help_tags().
3972 * For a tag pattern starting with "/" no translation is needed. */
3973 if (context == EXPAND_HELP
3974 || context == EXPAND_COLORS
3975 || context == EXPAND_COMPILER
3976 || (context == EXPAND_TAGS && fname[0] == '/'))
3977 retval = vim_strnsave(fname, len);
3978 else
3979 {
3980 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3981 for (i = 0; i < len; i++)
3982 {
3983 if (fname[i] == '*' || fname[i] == '~')
3984 new_len++; /* '*' needs to be replaced by ".*"
3985 '~' needs to be replaced by "\~" */
3986
3987 /* Buffer names are like file names. "." should be literal */
3988 if (context == EXPAND_BUFFERS && fname[i] == '.')
3989 new_len++; /* "." becomes "\." */
3990
3991 /* Custom expansion takes care of special things, match
3992 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003993 if ((context == EXPAND_USER_DEFINED
3994 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 new_len++; /* '\' becomes "\\" */
3996 }
3997 retval = alloc(new_len);
3998 if (retval != NULL)
3999 {
4000 retval[0] = '^';
4001 j = 1;
4002 for (i = 0; i < len; i++, j++)
4003 {
4004 /* Skip backslash. But why? At least keep it for custom
4005 * expansion. */
4006 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004007 && context != EXPAND_USER_LIST
4008 && fname[i] == '\\'
4009 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 break;
4011
4012 switch (fname[i])
4013 {
4014 case '*': retval[j++] = '.';
4015 break;
4016 case '~': retval[j++] = '\\';
4017 break;
4018 case '?': retval[j] = '.';
4019 continue;
4020 case '.': if (context == EXPAND_BUFFERS)
4021 retval[j++] = '\\';
4022 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004023 case '\\': if (context == EXPAND_USER_DEFINED
4024 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 retval[j++] = '\\';
4026 break;
4027 }
4028 retval[j] = fname[i];
4029 }
4030 retval[j] = NUL;
4031 }
4032 }
4033 }
4034 else
4035 {
4036 retval = alloc(len + 4);
4037 if (retval != NULL)
4038 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004039 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004042 * Don't add a star to *, ~, ~user, $var or `cmd`.
4043 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 * ~ would be at the start of the file name, but not the tail.
4045 * $ could be anywhere in the tail.
4046 * ` could be anywhere in the file name.
4047 */
4048 tail = gettail(retval);
4049 if ((*retval != '~' || tail != retval)
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004050 && (len == 0 || retval[len - 1] != '*')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 && vim_strchr(tail, '$') == NULL
4052 && vim_strchr(retval, '`') == NULL)
4053 retval[len++] = '*';
4054 retval[len] = NUL;
4055 }
4056 }
4057 return retval;
4058}
4059
4060/*
4061 * Must parse the command line so far to work out what context we are in.
4062 * Completion can then be done based on that context.
4063 * This routine sets the variables:
4064 * xp->xp_pattern The start of the pattern to be expanded within
4065 * the command line (ends at the cursor).
4066 * xp->xp_context The type of thing to expand. Will be one of:
4067 *
4068 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4069 * the command line, like an unknown command. Caller
4070 * should beep.
4071 * EXPAND_NOTHING Unrecognised context for completion, use char like
4072 * a normal char, rather than for completion. eg
4073 * :s/^I/
4074 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4075 * it.
4076 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4077 * EXPAND_FILES After command with XFILE set, or after setting
4078 * with P_EXPAND set. eg :e ^I, :w>>^I
4079 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4080 * when we know only directories are of interest. eg
4081 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004082 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4084 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4085 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4086 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4087 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4088 * EXPAND_EVENTS Complete event names
4089 * EXPAND_SYNTAX Complete :syntax command arguments
4090 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4091 * EXPAND_AUGROUP Complete autocommand group names
4092 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4093 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4094 * eg :unmap a^I , :cunab x^I
4095 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4096 * eg :call sub^I
4097 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4098 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4099 * names in expressions, eg :while s^I
4100 * EXPAND_ENV_VARS Complete environment variable names
4101 */
4102 static void
4103set_expand_context(xp)
4104 expand_T *xp;
4105{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004106 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 if (ccline.cmdfirstc != ':'
4108#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004109 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004110 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#endif
4112 )
4113 {
4114 xp->xp_context = EXPAND_NOTHING;
4115 return;
4116 }
4117 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4118}
4119
4120 void
4121set_cmd_context(xp, str, len, col)
4122 expand_T *xp;
4123 char_u *str; /* start of command line */
4124 int len; /* length of command line (excl. NUL) */
4125 int col; /* position of cursor */
4126{
4127 int old_char = NUL;
4128 char_u *nextcomm;
4129
4130 /*
4131 * Avoid a UMR warning from Purify, only save the character if it has been
4132 * written before.
4133 */
4134 if (col < len)
4135 old_char = str[col];
4136 str[col] = NUL;
4137 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004138
4139#ifdef FEAT_EVAL
4140 if (ccline.cmdfirstc == '=')
4141 /* pass CMD_SIZE because there is no real command */
4142 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004143 else if (ccline.input_fn)
4144 {
4145 xp->xp_context = ccline.xp_context;
4146 xp->xp_pattern = ccline.cmdbuff;
4147 xp->xp_arg = ccline.xp_arg;
4148 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004149 else
4150#endif
4151 while (nextcomm != NULL)
4152 nextcomm = set_one_cmd_context(xp, nextcomm);
4153
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 str[col] = old_char;
4155}
4156
4157/*
4158 * Expand the command line "str" from context "xp".
4159 * "xp" must have been set by set_cmd_context().
4160 * xp->xp_pattern points into "str", to where the text that is to be expanded
4161 * starts.
4162 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4163 * cursor.
4164 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4165 * key that triggered expansion literally.
4166 * Returns EXPAND_OK otherwise.
4167 */
4168 int
4169expand_cmdline(xp, str, col, matchcount, matches)
4170 expand_T *xp;
4171 char_u *str; /* start of command line */
4172 int col; /* position of cursor */
4173 int *matchcount; /* return: nr of matches */
4174 char_u ***matches; /* return: array of pointers to matches */
4175{
4176 char_u *file_str = NULL;
4177
4178 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4179 {
4180 beep_flush();
4181 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4182 }
4183 if (xp->xp_context == EXPAND_NOTHING)
4184 {
4185 /* Caller can use the character as a normal char instead */
4186 return EXPAND_NOTHING;
4187 }
4188
4189 /* add star to file name, or convert to regexp if not exp. files. */
4190 file_str = addstar(xp->xp_pattern,
4191 (int)(str + col - xp->xp_pattern), xp->xp_context);
4192 if (file_str == NULL)
4193 return EXPAND_UNSUCCESSFUL;
4194
4195 /* find all files that match the description */
4196 if (ExpandFromContext(xp, file_str, matchcount, matches,
4197 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4198 {
4199 *matchcount = 0;
4200 *matches = NULL;
4201 }
4202 vim_free(file_str);
4203
4204 return EXPAND_OK;
4205}
4206
4207#ifdef FEAT_MULTI_LANG
4208/*
4209 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4210 */
4211static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4212
4213 static void
4214cleanup_help_tags(num_file, file)
4215 int num_file;
4216 char_u **file;
4217{
4218 int i, j;
4219 int len;
4220
4221 for (i = 0; i < num_file; ++i)
4222 {
4223 len = (int)STRLEN(file[i]) - 3;
4224 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4225 {
4226 /* Sorting on priority means the same item in another language may
4227 * be anywhere. Search all items for a match up to the "@en". */
4228 for (j = 0; j < num_file; ++j)
4229 if (j != i
4230 && (int)STRLEN(file[j]) == len + 3
4231 && STRNCMP(file[i], file[j], len + 1) == 0)
4232 break;
4233 if (j == num_file)
4234 file[i][len] = NUL;
4235 }
4236 }
4237}
4238#endif
4239
4240/*
4241 * Do the expansion based on xp->xp_context and "pat".
4242 */
4243 static int
4244ExpandFromContext(xp, pat, num_file, file, options)
4245 expand_T *xp;
4246 char_u *pat;
4247 int *num_file;
4248 char_u ***file;
4249 int options;
4250{
4251#ifdef FEAT_CMDL_COMPL
4252 regmatch_T regmatch;
4253#endif
4254 int ret;
4255 int flags;
4256
4257 flags = EW_DIR; /* include directories */
4258 if (options & WILD_LIST_NOTFOUND)
4259 flags |= EW_NOTFOUND;
4260 if (options & WILD_ADD_SLASH)
4261 flags |= EW_ADDSLASH;
4262 if (options & WILD_KEEP_ALL)
4263 flags |= EW_KEEPALL;
4264 if (options & WILD_SILENT)
4265 flags |= EW_SILENT;
4266
4267 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4268 {
4269 /*
4270 * Expand file or directory names.
4271 */
4272 int free_pat = FALSE;
4273 int i;
4274
4275 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4276 * space */
4277 if (xp->xp_backslash != XP_BS_NONE)
4278 {
4279 free_pat = TRUE;
4280 pat = vim_strsave(pat);
4281 for (i = 0; pat[i]; ++i)
4282 if (pat[i] == '\\')
4283 {
4284 if (xp->xp_backslash == XP_BS_THREE
4285 && pat[i + 1] == '\\'
4286 && pat[i + 2] == '\\'
4287 && pat[i + 3] == ' ')
4288 STRCPY(pat + i, pat + i + 3);
4289 if (xp->xp_backslash == XP_BS_ONE
4290 && pat[i + 1] == ' ')
4291 STRCPY(pat + i, pat + i + 1);
4292 }
4293 }
4294
4295 if (xp->xp_context == EXPAND_FILES)
4296 flags |= EW_FILE;
4297 else
4298 flags = (flags | EW_DIR) & ~EW_FILE;
4299 ret = expand_wildcards(1, &pat, num_file, file, flags);
4300 if (free_pat)
4301 vim_free(pat);
4302 return ret;
4303 }
4304
4305 *file = (char_u **)"";
4306 *num_file = 0;
4307 if (xp->xp_context == EXPAND_HELP)
4308 {
4309 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4310 {
4311#ifdef FEAT_MULTI_LANG
4312 cleanup_help_tags(*num_file, *file);
4313#endif
4314 return OK;
4315 }
4316 return FAIL;
4317 }
4318
4319#ifndef FEAT_CMDL_COMPL
4320 return FAIL;
4321#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004322 if (xp->xp_context == EXPAND_SHELLCMD)
4323 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 if (xp->xp_context == EXPAND_OLD_SETTING)
4325 return ExpandOldSetting(num_file, file);
4326 if (xp->xp_context == EXPAND_BUFFERS)
4327 return ExpandBufnames(pat, num_file, file, options);
4328 if (xp->xp_context == EXPAND_TAGS
4329 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4330 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4331 if (xp->xp_context == EXPAND_COLORS)
4332 return ExpandRTDir(pat, num_file, file, "colors");
4333 if (xp->xp_context == EXPAND_COMPILER)
4334 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004335# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4336 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004337 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
4340 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4341 if (regmatch.regprog == NULL)
4342 return FAIL;
4343
4344 /* set ignore-case according to p_ic, p_scs and pat */
4345 regmatch.rm_ic = ignorecase(pat);
4346
4347 if (xp->xp_context == EXPAND_SETTINGS
4348 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4349 ret = ExpandSettings(xp, &regmatch, num_file, file);
4350 else if (xp->xp_context == EXPAND_MAPPINGS)
4351 ret = ExpandMappings(&regmatch, num_file, file);
4352# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4353 else if (xp->xp_context == EXPAND_USER_DEFINED)
4354 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4355# endif
4356 else
4357 {
4358 static struct expgen
4359 {
4360 int context;
4361 char_u *((*func)__ARGS((expand_T *, int)));
4362 int ic;
4363 } tab[] =
4364 {
4365 {EXPAND_COMMANDS, get_command_name, FALSE},
4366#ifdef FEAT_USR_CMDS
4367 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4368 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4369 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4370 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4371#endif
4372#ifdef FEAT_EVAL
4373 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4374 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4375 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4376 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4377#endif
4378#ifdef FEAT_MENU
4379 {EXPAND_MENUS, get_menu_name, FALSE},
4380 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4381#endif
4382#ifdef FEAT_SYN_HL
4383 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4384#endif
4385 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4386#ifdef FEAT_AUTOCMD
4387 {EXPAND_EVENTS, get_event_name, TRUE},
4388 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4389#endif
4390#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4391 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4392 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4393#endif
4394 {EXPAND_ENV_VARS, get_env_name, TRUE},
4395 };
4396 int i;
4397
4398 /*
4399 * Find a context in the table and call the ExpandGeneric() with the
4400 * right function to do the expansion.
4401 */
4402 ret = FAIL;
4403 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4404 if (xp->xp_context == tab[i].context)
4405 {
4406 if (tab[i].ic)
4407 regmatch.rm_ic = TRUE;
4408 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4409 break;
4410 }
4411 }
4412
4413 vim_free(regmatch.regprog);
4414
4415 return ret;
4416#endif /* FEAT_CMDL_COMPL */
4417}
4418
4419#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4420/*
4421 * Expand a list of names.
4422 *
4423 * Generic function for command line completion. It calls a function to
4424 * obtain strings, one by one. The strings are matched against a regexp
4425 * program. Matching strings are copied into an array, which is returned.
4426 *
4427 * Returns OK when no problems encountered, FAIL for error (out of memory).
4428 */
4429 int
4430ExpandGeneric(xp, regmatch, num_file, file, func)
4431 expand_T *xp;
4432 regmatch_T *regmatch;
4433 int *num_file;
4434 char_u ***file;
4435 char_u *((*func)__ARGS((expand_T *, int)));
4436 /* returns a string from the list */
4437{
4438 int i;
4439 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004440 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 char_u *str;
4442
4443 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004444 * round == 0: count the number of matching names
4445 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004447 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 for (i = 0; ; ++i)
4450 {
4451 str = (*func)(xp, i);
4452 if (str == NULL) /* end of list */
4453 break;
4454 if (*str == NUL) /* skip empty strings */
4455 continue;
4456
4457 if (vim_regexec(regmatch, str, (colnr_T)0))
4458 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004459 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
4461 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4462 (*file)[count] = str;
4463#ifdef FEAT_MENU
4464 if (func == get_menu_names && str != NULL)
4465 {
4466 /* test for separator added by get_menu_names() */
4467 str += STRLEN(str) - 1;
4468 if (*str == '\001')
4469 *str = '.';
4470 }
4471#endif
4472 }
4473 ++count;
4474 }
4475 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004476 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
4478 if (count == 0)
4479 return OK;
4480 *num_file = count;
4481 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4482 if (*file == NULL)
4483 {
4484 *file = (char_u **)"";
4485 return FAIL;
4486 }
4487 count = 0;
4488 }
4489 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004490
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004491 /* Sort the results. Keep menu's in the specified order. */
4492 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
4493 sort_strings(*file, *num_file);
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 return OK;
4496}
4497
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004498/*
4499 * Complete a shell command.
4500 * Returns FAIL or OK;
4501 */
4502 static int
4503expand_shellcmd(filepat, num_file, file, flagsarg)
4504 char_u *filepat; /* pattern to match with command names */
4505 int *num_file; /* return: number of matches */
4506 char_u ***file; /* return: array with matches */
4507 int flagsarg; /* EW_ flags */
4508{
4509 char_u *pat;
4510 int i;
4511 char_u *path;
4512 int mustfree = FALSE;
4513 garray_T ga;
4514 char_u *buf = alloc(MAXPATHL);
4515 size_t l;
4516 char_u *s, *e;
4517 int flags = flagsarg;
4518 int ret;
4519
4520 if (buf == NULL)
4521 return FAIL;
4522
4523 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4524 * space */
4525 pat = vim_strsave(filepat);
4526 for (i = 0; pat[i]; ++i)
4527 if (pat[i] == '\\' && pat[i + 1] == ' ')
4528 STRCPY(pat + i, pat + i + 1);
4529
4530 flags |= EW_FILE | EW_EXEC;
4531
4532 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004533 if (mch_isFullName(pat))
4534 path = (char_u *)" ";
4535 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004536 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4537 path = (char_u *)".";
4538 else
4539 path = vim_getenv((char_u *)"PATH", &mustfree);
4540
4541 /*
4542 * Go over all directories in $PATH. Expand matches in that directory and
4543 * collect them in "ga".
4544 */
4545 ga_init2(&ga, (int)sizeof(char *), 10);
4546 for (s = path; *s != NUL; s = e)
4547 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004548 if (*s == ' ')
4549 ++s; /* Skip space used for absolute path name. */
4550
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004551#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4552 e = vim_strchr(s, ';');
4553#else
4554 e = vim_strchr(s, ':');
4555#endif
4556 if (e == NULL)
4557 e = s + STRLEN(s);
4558
4559 l = e - s;
4560 if (l > MAXPATHL - 5)
4561 break;
4562 vim_strncpy(buf, s, l);
4563 add_pathsep(buf);
4564 l = STRLEN(buf);
4565 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4566
4567 /* Expand matches in one directory of $PATH. */
4568 ret = expand_wildcards(1, &buf, num_file, file, flags);
4569 if (ret == OK)
4570 {
4571 if (ga_grow(&ga, *num_file) == FAIL)
4572 FreeWild(*num_file, *file);
4573 else
4574 {
4575 for (i = 0; i < *num_file; ++i)
4576 {
4577 s = (*file)[i];
4578 if (STRLEN(s) > l)
4579 {
4580 /* Remove the path again. */
4581 mch_memmove(s, s + l, STRLEN(s + l) + 1);
4582 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4583 }
4584 else
4585 vim_free(s);
4586 }
4587 vim_free(*file);
4588 }
4589 }
4590 if (*e != NUL)
4591 ++e;
4592 }
4593 *file = ga.ga_data;
4594 *num_file = ga.ga_len;
4595
4596 vim_free(buf);
4597 vim_free(pat);
4598 if (mustfree)
4599 vim_free(path);
4600 return OK;
4601}
4602
4603
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004605static 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));
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004608 * call "user_expand_func()" to invoke a user defined VimL function and return
4609 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004611 static void *
4612call_user_expand_func(user_expand_func, xp, num_file, file)
4613 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 int *num_file;
4616 char_u ***file;
4617{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004618 char_u keep;
4619 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004622 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004623 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624
4625 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004626 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 *num_file = 0;
4628 *file = NULL;
4629
4630 keep = ccline.cmdbuff[ccline.cmdlen];
4631 ccline.cmdbuff[ccline.cmdlen] = 0;
4632 sprintf((char *)num, "%d", ccline.cmdpos);
4633 args[0] = xp->xp_pattern;
4634 args[1] = ccline.cmdbuff;
4635 args[2] = num;
4636
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004637 /* Save the cmdline, we don't know what the function may do. */
4638 save_ccline = ccline;
4639 ccline.cmdbuff = NULL;
4640 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004642
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004643 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004644
4645 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004647
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004649
4650 return ret;
4651}
4652
4653/*
4654 * Expand names with a function defined by the user.
4655 */
4656 static int
4657ExpandUserDefined(xp, regmatch, num_file, file)
4658 expand_T *xp;
4659 regmatch_T *regmatch;
4660 int *num_file;
4661 char_u ***file;
4662{
4663 char_u *retstr;
4664 char_u *s;
4665 char_u *e;
4666 char_u keep;
4667 garray_T ga;
4668
4669 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4670 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 return FAIL;
4672
4673 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004674 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
4676 e = vim_strchr(s, '\n');
4677 if (e == NULL)
4678 e = s + STRLEN(s);
4679 keep = *e;
4680 *e = 0;
4681
4682 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4683 {
4684 *e = keep;
4685 if (*e != NUL)
4686 ++e;
4687 continue;
4688 }
4689
4690 if (ga_grow(&ga, 1) == FAIL)
4691 break;
4692
4693 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4694 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 *e = keep;
4697 if (*e != NUL)
4698 ++e;
4699 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004700 vim_free(retstr);
4701 *file = ga.ga_data;
4702 *num_file = ga.ga_len;
4703 return OK;
4704}
4705
4706/*
4707 * Expand names with a list returned by a function defined by the user.
4708 */
4709 static int
4710ExpandUserList(xp, num_file, file)
4711 expand_T *xp;
4712 int *num_file;
4713 char_u ***file;
4714{
4715 list_T *retlist;
4716 listitem_T *li;
4717 garray_T ga;
4718
4719 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4720 if (retlist == NULL)
4721 return FAIL;
4722
4723 ga_init2(&ga, (int)sizeof(char *), 3);
4724 /* Loop over the items in the list. */
4725 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4726 {
4727 if (li->li_tv.v_type != VAR_STRING)
4728 continue; /* Skip non-string items */
4729
4730 if (ga_grow(&ga, 1) == FAIL)
4731 break;
4732
4733 ((char_u **)ga.ga_data)[ga.ga_len] =
4734 vim_strsave(li->li_tv.vval.v_string);
4735 ++ga.ga_len;
4736 }
4737 list_unref(retlist);
4738
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 *file = ga.ga_data;
4740 *num_file = ga.ga_len;
4741 return OK;
4742}
4743#endif
4744
4745/*
4746 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4747 * or compiler names.
4748 */
4749 static int
4750ExpandRTDir(pat, num_file, file, dirname)
4751 char_u *pat;
4752 int *num_file;
4753 char_u ***file;
4754 char *dirname; /* "colors" or "compiler" */
4755{
4756 char_u *all;
4757 char_u *s;
4758 char_u *e;
4759 garray_T ga;
4760
4761 *num_file = 0;
4762 *file = NULL;
4763 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4764 if (s == NULL)
4765 return FAIL;
4766 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4767 all = globpath(p_rtp, s);
4768 vim_free(s);
4769 if (all == NULL)
4770 return FAIL;
4771
4772 ga_init2(&ga, (int)sizeof(char *), 3);
4773 for (s = all; *s != NUL; s = e)
4774 {
4775 e = vim_strchr(s, '\n');
4776 if (e == NULL)
4777 e = s + STRLEN(s);
4778 if (ga_grow(&ga, 1) == FAIL)
4779 break;
4780 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4781 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004782 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if (*s == '\n' || vim_ispathsep(*s))
4784 break;
4785 ++s;
4786 ((char_u **)ga.ga_data)[ga.ga_len] =
4787 vim_strnsave(s, (int)(e - s - 4));
4788 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
4790 if (*e != NUL)
4791 ++e;
4792 }
4793 vim_free(all);
4794 *file = ga.ga_data;
4795 *num_file = ga.ga_len;
4796 return OK;
4797}
4798
4799#endif
4800
4801#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4802/*
4803 * Expand "file" for all comma-separated directories in "path".
4804 * Returns an allocated string with all matches concatenated, separated by
4805 * newlines. Returns NULL for an error or no matches.
4806 */
4807 char_u *
4808globpath(path, file)
4809 char_u *path;
4810 char_u *file;
4811{
4812 expand_T xpc;
4813 char_u *buf;
4814 garray_T ga;
4815 int i;
4816 int len;
4817 int num_p;
4818 char_u **p;
4819 char_u *cur = NULL;
4820
4821 buf = alloc(MAXPATHL);
4822 if (buf == NULL)
4823 return NULL;
4824
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004825 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004827
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 ga_init2(&ga, 1, 100);
4829
4830 /* Loop over all entries in {path}. */
4831 while (*path != NUL)
4832 {
4833 /* Copy one item of the path to buf[] and concatenate the file name. */
4834 copy_option_part(&path, buf, MAXPATHL, ",");
4835 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4836 {
4837 add_pathsep(buf);
4838 STRCAT(buf, file);
4839 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4840 && num_p > 0)
4841 {
4842 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4843 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004844 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
4846 /* Concatenate new results to previous ones. */
4847 if (ga_grow(&ga, len) == OK)
4848 {
4849 cur = (char_u *)ga.ga_data + ga.ga_len;
4850 for (i = 0; i < num_p; ++i)
4851 {
4852 STRCPY(cur, p[i]);
4853 cur += STRLEN(p[i]);
4854 *cur++ = '\n';
4855 }
4856 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 }
4858 FreeWild(num_p, p);
4859 }
4860 }
4861 }
4862 if (cur != NULL)
4863 *--cur = 0; /* Replace trailing newline with NUL */
4864
4865 vim_free(buf);
4866 return (char_u *)ga.ga_data;
4867}
4868
4869#endif
4870
4871#if defined(FEAT_CMDHIST) || defined(PROTO)
4872
4873/*********************************
4874 * Command line history stuff *
4875 *********************************/
4876
4877/*
4878 * Translate a history character to the associated type number.
4879 */
4880 static int
4881hist_char2type(c)
4882 int c;
4883{
4884 if (c == ':')
4885 return HIST_CMD;
4886 if (c == '=')
4887 return HIST_EXPR;
4888 if (c == '@')
4889 return HIST_INPUT;
4890 if (c == '>')
4891 return HIST_DEBUG;
4892 return HIST_SEARCH; /* must be '?' or '/' */
4893}
4894
4895/*
4896 * Table of history names.
4897 * These names are used in :history and various hist...() functions.
4898 * It is sufficient to give the significant prefix of a history name.
4899 */
4900
4901static char *(history_names[]) =
4902{
4903 "cmd",
4904 "search",
4905 "expr",
4906 "input",
4907 "debug",
4908 NULL
4909};
4910
4911/*
4912 * init_history() - Initialize the command line history.
4913 * Also used to re-allocate the history when the size changes.
4914 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004915 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916init_history()
4917{
4918 int newlen; /* new length of history table */
4919 histentry_T *temp;
4920 int i;
4921 int j;
4922 int type;
4923
4924 /*
4925 * If size of history table changed, reallocate it
4926 */
4927 newlen = (int)p_hi;
4928 if (newlen != hislen) /* history length changed */
4929 {
4930 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4931 {
4932 if (newlen)
4933 {
4934 temp = (histentry_T *)lalloc(
4935 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4936 if (temp == NULL) /* out of memory! */
4937 {
4938 if (type == 0) /* first one: just keep the old length */
4939 {
4940 newlen = hislen;
4941 break;
4942 }
4943 /* Already changed one table, now we can only have zero
4944 * length for all tables. */
4945 newlen = 0;
4946 type = -1;
4947 continue;
4948 }
4949 }
4950 else
4951 temp = NULL;
4952 if (newlen == 0 || temp != NULL)
4953 {
4954 if (hisidx[type] < 0) /* there are no entries yet */
4955 {
4956 for (i = 0; i < newlen; ++i)
4957 {
4958 temp[i].hisnum = 0;
4959 temp[i].hisstr = NULL;
4960 }
4961 }
4962 else if (newlen > hislen) /* array becomes bigger */
4963 {
4964 for (i = 0; i <= hisidx[type]; ++i)
4965 temp[i] = history[type][i];
4966 j = i;
4967 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4968 {
4969 temp[i].hisnum = 0;
4970 temp[i].hisstr = NULL;
4971 }
4972 for ( ; j < hislen; ++i, ++j)
4973 temp[i] = history[type][j];
4974 }
4975 else /* array becomes smaller or 0 */
4976 {
4977 j = hisidx[type];
4978 for (i = newlen - 1; ; --i)
4979 {
4980 if (i >= 0) /* copy newest entries */
4981 temp[i] = history[type][j];
4982 else /* remove older entries */
4983 vim_free(history[type][j].hisstr);
4984 if (--j < 0)
4985 j = hislen - 1;
4986 if (j == hisidx[type])
4987 break;
4988 }
4989 hisidx[type] = newlen - 1;
4990 }
4991 vim_free(history[type]);
4992 history[type] = temp;
4993 }
4994 }
4995 hislen = newlen;
4996 }
4997}
4998
4999/*
5000 * Check if command line 'str' is already in history.
5001 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5002 */
5003 static int
5004in_history(type, str, move_to_front)
5005 int type;
5006 char_u *str;
5007 int move_to_front; /* Move the entry to the front if it exists */
5008{
5009 int i;
5010 int last_i = -1;
5011
5012 if (hisidx[type] < 0)
5013 return FALSE;
5014 i = hisidx[type];
5015 do
5016 {
5017 if (history[type][i].hisstr == NULL)
5018 return FALSE;
5019 if (STRCMP(str, history[type][i].hisstr) == 0)
5020 {
5021 if (!move_to_front)
5022 return TRUE;
5023 last_i = i;
5024 break;
5025 }
5026 if (--i < 0)
5027 i = hislen - 1;
5028 } while (i != hisidx[type]);
5029
5030 if (last_i >= 0)
5031 {
5032 str = history[type][i].hisstr;
5033 while (i != hisidx[type])
5034 {
5035 if (++i >= hislen)
5036 i = 0;
5037 history[type][last_i] = history[type][i];
5038 last_i = i;
5039 }
5040 history[type][i].hisstr = str;
5041 history[type][i].hisnum = ++hisnum[type];
5042 return TRUE;
5043 }
5044 return FALSE;
5045}
5046
5047/*
5048 * Convert history name (from table above) to its HIST_ equivalent.
5049 * When "name" is empty, return "cmd" history.
5050 * Returns -1 for unknown history name.
5051 */
5052 int
5053get_histtype(name)
5054 char_u *name;
5055{
5056 int i;
5057 int len = (int)STRLEN(name);
5058
5059 /* No argument: use current history. */
5060 if (len == 0)
5061 return hist_char2type(ccline.cmdfirstc);
5062
5063 for (i = 0; history_names[i] != NULL; ++i)
5064 if (STRNICMP(name, history_names[i], len) == 0)
5065 return i;
5066
5067 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5068 return hist_char2type(name[0]);
5069
5070 return -1;
5071}
5072
5073static int last_maptick = -1; /* last seen maptick */
5074
5075/*
5076 * Add the given string to the given history. If the string is already in the
5077 * history then it is moved to the front. "histype" may be one of he HIST_
5078 * values.
5079 */
5080 void
5081add_to_history(histype, new_entry, in_map, sep)
5082 int histype;
5083 char_u *new_entry;
5084 int in_map; /* consider maptick when inside a mapping */
5085 int sep; /* separator character used (search hist) */
5086{
5087 histentry_T *hisptr;
5088 int len;
5089
5090 if (hislen == 0) /* no history */
5091 return;
5092
5093 /*
5094 * Searches inside the same mapping overwrite each other, so that only
5095 * the last line is kept. Be careful not to remove a line that was moved
5096 * down, only lines that were added.
5097 */
5098 if (histype == HIST_SEARCH && in_map)
5099 {
5100 if (maptick == last_maptick)
5101 {
5102 /* Current line is from the same mapping, remove it */
5103 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5104 vim_free(hisptr->hisstr);
5105 hisptr->hisstr = NULL;
5106 hisptr->hisnum = 0;
5107 --hisnum[histype];
5108 if (--hisidx[HIST_SEARCH] < 0)
5109 hisidx[HIST_SEARCH] = hislen - 1;
5110 }
5111 last_maptick = -1;
5112 }
5113 if (!in_history(histype, new_entry, TRUE))
5114 {
5115 if (++hisidx[histype] == hislen)
5116 hisidx[histype] = 0;
5117 hisptr = &history[histype][hisidx[histype]];
5118 vim_free(hisptr->hisstr);
5119
5120 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005121 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5123 if (hisptr->hisstr != NULL)
5124 hisptr->hisstr[len + 1] = sep;
5125
5126 hisptr->hisnum = ++hisnum[histype];
5127 if (histype == HIST_SEARCH && in_map)
5128 last_maptick = maptick;
5129 }
5130}
5131
5132#if defined(FEAT_EVAL) || defined(PROTO)
5133
5134/*
5135 * Get identifier of newest history entry.
5136 * "histype" may be one of the HIST_ values.
5137 */
5138 int
5139get_history_idx(histype)
5140 int histype;
5141{
5142 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5143 || hisidx[histype] < 0)
5144 return -1;
5145
5146 return history[histype][hisidx[histype]].hisnum;
5147}
5148
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005149static struct cmdline_info *get_ccline_ptr __ARGS((void));
5150
5151/*
5152 * Get pointer to the command line info to use. cmdline_paste() may clear
5153 * ccline and put the previous value in prev_ccline.
5154 */
5155 static struct cmdline_info *
5156get_ccline_ptr()
5157{
5158 if ((State & CMDLINE) == 0)
5159 return NULL;
5160 if (ccline.cmdbuff != NULL)
5161 return &ccline;
5162 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5163 return &prev_ccline;
5164 return NULL;
5165}
5166
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167/*
5168 * Get the current command line in allocated memory.
5169 * Only works when the command line is being edited.
5170 * Returns NULL when something is wrong.
5171 */
5172 char_u *
5173get_cmdline_str()
5174{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005175 struct cmdline_info *p = get_ccline_ptr();
5176
5177 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005179 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180}
5181
5182/*
5183 * Get the current command line position, counted in bytes.
5184 * Zero is the first position.
5185 * Only works when the command line is being edited.
5186 * Returns -1 when something is wrong.
5187 */
5188 int
5189get_cmdline_pos()
5190{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005191 struct cmdline_info *p = get_ccline_ptr();
5192
5193 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005195 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196}
5197
5198/*
5199 * Set the command line byte position to "pos". Zero is the first position.
5200 * Only works when the command line is being edited.
5201 * Returns 1 when failed, 0 when OK.
5202 */
5203 int
5204set_cmdline_pos(pos)
5205 int pos;
5206{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005207 struct cmdline_info *p = get_ccline_ptr();
5208
5209 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 return 1;
5211
5212 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5213 * changed the command line. */
5214 if (pos < 0)
5215 new_cmdpos = 0;
5216 else
5217 new_cmdpos = pos;
5218 return 0;
5219}
5220
5221/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005222 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005223 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005224 * Only works when the command line is being edited.
5225 * Returns NUL when something is wrong.
5226 */
5227 int
5228get_cmdline_type()
5229{
5230 struct cmdline_info *p = get_ccline_ptr();
5231
5232 if (p == NULL)
5233 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005234 if (p->cmdfirstc == NUL)
5235 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005236 return p->cmdfirstc;
5237}
5238
5239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 * Calculate history index from a number:
5241 * num > 0: seen as identifying number of a history entry
5242 * num < 0: relative position in history wrt newest entry
5243 * "histype" may be one of the HIST_ values.
5244 */
5245 static int
5246calc_hist_idx(histype, num)
5247 int histype;
5248 int num;
5249{
5250 int i;
5251 histentry_T *hist;
5252 int wrapped = FALSE;
5253
5254 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5255 || (i = hisidx[histype]) < 0 || num == 0)
5256 return -1;
5257
5258 hist = history[histype];
5259 if (num > 0)
5260 {
5261 while (hist[i].hisnum > num)
5262 if (--i < 0)
5263 {
5264 if (wrapped)
5265 break;
5266 i += hislen;
5267 wrapped = TRUE;
5268 }
5269 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5270 return i;
5271 }
5272 else if (-num <= hislen)
5273 {
5274 i += num + 1;
5275 if (i < 0)
5276 i += hislen;
5277 if (hist[i].hisstr != NULL)
5278 return i;
5279 }
5280 return -1;
5281}
5282
5283/*
5284 * Get a history entry by its index.
5285 * "histype" may be one of the HIST_ values.
5286 */
5287 char_u *
5288get_history_entry(histype, idx)
5289 int histype;
5290 int idx;
5291{
5292 idx = calc_hist_idx(histype, idx);
5293 if (idx >= 0)
5294 return history[histype][idx].hisstr;
5295 else
5296 return (char_u *)"";
5297}
5298
5299/*
5300 * Clear all entries of a history.
5301 * "histype" may be one of the HIST_ values.
5302 */
5303 int
5304clr_history(histype)
5305 int histype;
5306{
5307 int i;
5308 histentry_T *hisptr;
5309
5310 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5311 {
5312 hisptr = history[histype];
5313 for (i = hislen; i--;)
5314 {
5315 vim_free(hisptr->hisstr);
5316 hisptr->hisnum = 0;
5317 hisptr++->hisstr = NULL;
5318 }
5319 hisidx[histype] = -1; /* mark history as cleared */
5320 hisnum[histype] = 0; /* reset identifier counter */
5321 return OK;
5322 }
5323 return FAIL;
5324}
5325
5326/*
5327 * Remove all entries matching {str} from a history.
5328 * "histype" may be one of the HIST_ values.
5329 */
5330 int
5331del_history_entry(histype, str)
5332 int histype;
5333 char_u *str;
5334{
5335 regmatch_T regmatch;
5336 histentry_T *hisptr;
5337 int idx;
5338 int i;
5339 int last;
5340 int found = FALSE;
5341
5342 regmatch.regprog = NULL;
5343 regmatch.rm_ic = FALSE; /* always match case */
5344 if (hislen != 0
5345 && histype >= 0
5346 && histype < HIST_COUNT
5347 && *str != NUL
5348 && (idx = hisidx[histype]) >= 0
5349 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5350 != NULL)
5351 {
5352 i = last = idx;
5353 do
5354 {
5355 hisptr = &history[histype][i];
5356 if (hisptr->hisstr == NULL)
5357 break;
5358 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5359 {
5360 found = TRUE;
5361 vim_free(hisptr->hisstr);
5362 hisptr->hisstr = NULL;
5363 hisptr->hisnum = 0;
5364 }
5365 else
5366 {
5367 if (i != last)
5368 {
5369 history[histype][last] = *hisptr;
5370 hisptr->hisstr = NULL;
5371 hisptr->hisnum = 0;
5372 }
5373 if (--last < 0)
5374 last += hislen;
5375 }
5376 if (--i < 0)
5377 i += hislen;
5378 } while (i != idx);
5379 if (history[histype][idx].hisstr == NULL)
5380 hisidx[histype] = -1;
5381 }
5382 vim_free(regmatch.regprog);
5383 return found;
5384}
5385
5386/*
5387 * Remove an indexed entry from a history.
5388 * "histype" may be one of the HIST_ values.
5389 */
5390 int
5391del_history_idx(histype, idx)
5392 int histype;
5393 int idx;
5394{
5395 int i, j;
5396
5397 i = calc_hist_idx(histype, idx);
5398 if (i < 0)
5399 return FALSE;
5400 idx = hisidx[histype];
5401 vim_free(history[histype][i].hisstr);
5402
5403 /* When deleting the last added search string in a mapping, reset
5404 * last_maptick, so that the last added search string isn't deleted again.
5405 */
5406 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5407 last_maptick = -1;
5408
5409 while (i != idx)
5410 {
5411 j = (i + 1) % hislen;
5412 history[histype][i] = history[histype][j];
5413 i = j;
5414 }
5415 history[histype][i].hisstr = NULL;
5416 history[histype][i].hisnum = 0;
5417 if (--i < 0)
5418 i += hislen;
5419 hisidx[histype] = i;
5420 return TRUE;
5421}
5422
5423#endif /* FEAT_EVAL */
5424
5425#if defined(FEAT_CRYPT) || defined(PROTO)
5426/*
5427 * Very specific function to remove the value in ":set key=val" from the
5428 * history.
5429 */
5430 void
5431remove_key_from_history()
5432{
5433 char_u *p;
5434 int i;
5435
5436 i = hisidx[HIST_CMD];
5437 if (i < 0)
5438 return;
5439 p = history[HIST_CMD][i].hisstr;
5440 if (p != NULL)
5441 for ( ; *p; ++p)
5442 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5443 {
5444 p = vim_strchr(p + 3, '=');
5445 if (p == NULL)
5446 break;
5447 ++p;
5448 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5449 if (p[i] == '\\' && p[i + 1])
5450 ++i;
5451 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5452 --p;
5453 }
5454}
5455#endif
5456
5457#endif /* FEAT_CMDHIST */
5458
5459#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5460/*
5461 * Get indices "num1,num2" that specify a range within a list (not a range of
5462 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5463 * Returns OK if parsed successfully, otherwise FAIL.
5464 */
5465 int
5466get_list_range(str, num1, num2)
5467 char_u **str;
5468 int *num1;
5469 int *num2;
5470{
5471 int len;
5472 int first = FALSE;
5473 long num;
5474
5475 *str = skipwhite(*str);
5476 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5477 {
5478 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5479 *str += len;
5480 *num1 = (int)num;
5481 first = TRUE;
5482 }
5483 *str = skipwhite(*str);
5484 if (**str == ',') /* parse "to" part of range */
5485 {
5486 *str = skipwhite(*str + 1);
5487 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5488 if (len > 0)
5489 {
5490 *num2 = (int)num;
5491 *str = skipwhite(*str + len);
5492 }
5493 else if (!first) /* no number given at all */
5494 return FAIL;
5495 }
5496 else if (first) /* only one number given */
5497 *num2 = *num1;
5498 return OK;
5499}
5500#endif
5501
5502#if defined(FEAT_CMDHIST) || defined(PROTO)
5503/*
5504 * :history command - print a history
5505 */
5506 void
5507ex_history(eap)
5508 exarg_T *eap;
5509{
5510 histentry_T *hist;
5511 int histype1 = HIST_CMD;
5512 int histype2 = HIST_CMD;
5513 int hisidx1 = 1;
5514 int hisidx2 = -1;
5515 int idx;
5516 int i, j, k;
5517 char_u *end;
5518 char_u *arg = eap->arg;
5519
5520 if (hislen == 0)
5521 {
5522 MSG(_("'history' option is zero"));
5523 return;
5524 }
5525
5526 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5527 {
5528 end = arg;
5529 while (ASCII_ISALPHA(*end)
5530 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5531 end++;
5532 i = *end;
5533 *end = NUL;
5534 histype1 = get_histtype(arg);
5535 if (histype1 == -1)
5536 {
5537 if (STRICMP(arg, "all") == 0)
5538 {
5539 histype1 = 0;
5540 histype2 = HIST_COUNT-1;
5541 }
5542 else
5543 {
5544 *end = i;
5545 EMSG(_(e_trailing));
5546 return;
5547 }
5548 }
5549 else
5550 histype2 = histype1;
5551 *end = i;
5552 }
5553 else
5554 end = arg;
5555 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5556 {
5557 EMSG(_(e_trailing));
5558 return;
5559 }
5560
5561 for (; !got_int && histype1 <= histype2; ++histype1)
5562 {
5563 STRCPY(IObuff, "\n # ");
5564 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5565 MSG_PUTS_TITLE(IObuff);
5566 idx = hisidx[histype1];
5567 hist = history[histype1];
5568 j = hisidx1;
5569 k = hisidx2;
5570 if (j < 0)
5571 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5572 if (k < 0)
5573 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5574 if (idx >= 0 && j <= k)
5575 for (i = idx + 1; !got_int; ++i)
5576 {
5577 if (i == hislen)
5578 i = 0;
5579 if (hist[i].hisstr != NULL
5580 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5581 {
5582 msg_putchar('\n');
5583 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5584 hist[i].hisnum);
5585 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5586 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5587 (int)Columns - 10);
5588 else
5589 STRCAT(IObuff, hist[i].hisstr);
5590 msg_outtrans(IObuff);
5591 out_flush();
5592 }
5593 if (i == idx)
5594 break;
5595 }
5596 }
5597}
5598#endif
5599
5600#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5601static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5602static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5603static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5604static int viminfo_add_at_front = FALSE;
5605
5606static int hist_type2char __ARGS((int type, int use_question));
5607
5608/*
5609 * Translate a history type number to the associated character.
5610 */
5611 static int
5612hist_type2char(type, use_question)
5613 int type;
5614 int use_question; /* use '?' instead of '/' */
5615{
5616 if (type == HIST_CMD)
5617 return ':';
5618 if (type == HIST_SEARCH)
5619 {
5620 if (use_question)
5621 return '?';
5622 else
5623 return '/';
5624 }
5625 if (type == HIST_EXPR)
5626 return '=';
5627 return '@';
5628}
5629
5630/*
5631 * Prepare for reading the history from the viminfo file.
5632 * This allocates history arrays to store the read history lines.
5633 */
5634 void
5635prepare_viminfo_history(asklen)
5636 int asklen;
5637{
5638 int i;
5639 int num;
5640 int type;
5641 int len;
5642
5643 init_history();
5644 viminfo_add_at_front = (asklen != 0);
5645 if (asklen > hislen)
5646 asklen = hislen;
5647
5648 for (type = 0; type < HIST_COUNT; ++type)
5649 {
5650 /*
5651 * Count the number of empty spaces in the history list. If there are
5652 * more spaces available than we request, then fill them up.
5653 */
5654 for (i = 0, num = 0; i < hislen; i++)
5655 if (history[type][i].hisstr == NULL)
5656 num++;
5657 len = asklen;
5658 if (num > len)
5659 len = num;
5660 if (len <= 0)
5661 viminfo_history[type] = NULL;
5662 else
5663 viminfo_history[type] =
5664 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5665 if (viminfo_history[type] == NULL)
5666 len = 0;
5667 viminfo_hislen[type] = len;
5668 viminfo_hisidx[type] = 0;
5669 }
5670}
5671
5672/*
5673 * Accept a line from the viminfo, store it in the history array when it's
5674 * new.
5675 */
5676 int
5677read_viminfo_history(virp)
5678 vir_T *virp;
5679{
5680 int type;
5681 long_u len;
5682 char_u *val;
5683 char_u *p;
5684
5685 type = hist_char2type(virp->vir_line[0]);
5686 if (viminfo_hisidx[type] < viminfo_hislen[type])
5687 {
5688 val = viminfo_readstring(virp, 1, TRUE);
5689 if (val != NULL && *val != NUL)
5690 {
5691 if (!in_history(type, val + (type == HIST_SEARCH),
5692 viminfo_add_at_front))
5693 {
5694 /* Need to re-allocate to append the separator byte. */
5695 len = STRLEN(val);
5696 p = lalloc(len + 2, TRUE);
5697 if (p != NULL)
5698 {
5699 if (type == HIST_SEARCH)
5700 {
5701 /* Search entry: Move the separator from the first
5702 * column to after the NUL. */
5703 mch_memmove(p, val + 1, (size_t)len);
5704 p[len] = (*val == ' ' ? NUL : *val);
5705 }
5706 else
5707 {
5708 /* Not a search entry: No separator in the viminfo
5709 * file, add a NUL separator. */
5710 mch_memmove(p, val, (size_t)len + 1);
5711 p[len + 1] = NUL;
5712 }
5713 viminfo_history[type][viminfo_hisidx[type]++] = p;
5714 }
5715 }
5716 }
5717 vim_free(val);
5718 }
5719 return viminfo_readline(virp);
5720}
5721
5722 void
5723finish_viminfo_history()
5724{
5725 int idx;
5726 int i;
5727 int type;
5728
5729 for (type = 0; type < HIST_COUNT; ++type)
5730 {
5731 if (history[type] == NULL)
5732 return;
5733 idx = hisidx[type] + viminfo_hisidx[type];
5734 if (idx >= hislen)
5735 idx -= hislen;
5736 else if (idx < 0)
5737 idx = hislen - 1;
5738 if (viminfo_add_at_front)
5739 hisidx[type] = idx;
5740 else
5741 {
5742 if (hisidx[type] == -1)
5743 hisidx[type] = hislen - 1;
5744 do
5745 {
5746 if (history[type][idx].hisstr != NULL)
5747 break;
5748 if (++idx == hislen)
5749 idx = 0;
5750 } while (idx != hisidx[type]);
5751 if (idx != hisidx[type] && --idx < 0)
5752 idx = hislen - 1;
5753 }
5754 for (i = 0; i < viminfo_hisidx[type]; i++)
5755 {
5756 vim_free(history[type][idx].hisstr);
5757 history[type][idx].hisstr = viminfo_history[type][i];
5758 if (--idx < 0)
5759 idx = hislen - 1;
5760 }
5761 idx += 1;
5762 idx %= hislen;
5763 for (i = 0; i < viminfo_hisidx[type]; i++)
5764 {
5765 history[type][idx++].hisnum = ++hisnum[type];
5766 idx %= hislen;
5767 }
5768 vim_free(viminfo_history[type]);
5769 viminfo_history[type] = NULL;
5770 }
5771}
5772
5773 void
5774write_viminfo_history(fp)
5775 FILE *fp;
5776{
5777 int i;
5778 int type;
5779 int num_saved;
5780 char_u *p;
5781 int c;
5782
5783 init_history();
5784 if (hislen == 0)
5785 return;
5786 for (type = 0; type < HIST_COUNT; ++type)
5787 {
5788 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5789 if (num_saved == 0)
5790 continue;
5791 if (num_saved < 0) /* Use default */
5792 num_saved = hislen;
5793 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5794 type == HIST_CMD ? _("Command Line") :
5795 type == HIST_SEARCH ? _("Search String") :
5796 type == HIST_EXPR ? _("Expression") :
5797 _("Input Line"));
5798 if (num_saved > hislen)
5799 num_saved = hislen;
5800 i = hisidx[type];
5801 if (i >= 0)
5802 while (num_saved--)
5803 {
5804 p = history[type][i].hisstr;
5805 if (p != NULL)
5806 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005807 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 /* For the search history: put the separator in the second
5809 * column; use a space if there isn't one. */
5810 if (type == HIST_SEARCH)
5811 {
5812 c = p[STRLEN(p) + 1];
5813 putc(c == NUL ? ' ' : c, fp);
5814 }
5815 viminfo_writestring(fp, p);
5816 }
5817 if (--i < 0)
5818 i = hislen - 1;
5819 }
5820 }
5821}
5822#endif /* FEAT_VIMINFO */
5823
5824#if defined(FEAT_FKMAP) || defined(PROTO)
5825/*
5826 * Write a character at the current cursor+offset position.
5827 * It is directly written into the command buffer block.
5828 */
5829 void
5830cmd_pchar(c, offset)
5831 int c, offset;
5832{
5833 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5834 {
5835 EMSG(_("E198: cmd_pchar beyond the command length"));
5836 return;
5837 }
5838 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5839 ccline.cmdbuff[ccline.cmdlen] = NUL;
5840}
5841
5842 int
5843cmd_gchar(offset)
5844 int offset;
5845{
5846 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5847 {
5848 /* EMSG(_("cmd_gchar beyond the command length")); */
5849 return NUL;
5850 }
5851 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5852}
5853#endif
5854
5855#if defined(FEAT_CMDWIN) || defined(PROTO)
5856/*
5857 * Open a window on the current command line and history. Allow editing in
5858 * the window. Returns when the window is closed.
5859 * Returns:
5860 * CR if the command is to be executed
5861 * Ctrl_C if it is to be abandoned
5862 * K_IGNORE if editing continues
5863 */
5864 static int
5865ex_window()
5866{
5867 struct cmdline_info save_ccline;
5868 buf_T *old_curbuf = curbuf;
5869 win_T *old_curwin = curwin;
5870 buf_T *bp;
5871 win_T *wp;
5872 int i;
5873 linenr_T lnum;
5874 int histtype;
5875 garray_T winsizes;
5876 char_u typestr[2];
5877 int save_restart_edit = restart_edit;
5878 int save_State = State;
5879 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005880#ifdef FEAT_RIGHTLEFT
5881 int save_cmdmsg_rl = cmdmsg_rl;
5882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
5884 /* Can't do this recursively. Can't do it when typing a password. */
5885 if (cmdwin_type != 0
5886# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5887 || cmdline_star > 0
5888# endif
5889 )
5890 {
5891 beep_flush();
5892 return K_IGNORE;
5893 }
5894
5895 /* Save current window sizes. */
5896 win_size_save(&winsizes);
5897
5898# ifdef FEAT_AUTOCMD
5899 /* Don't execute autocommands while creating the window. */
5900 ++autocmd_block;
5901# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005902 /* don't use a new tab page */
5903 cmdmod.tab = 0;
5904
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 /* Create a window for the command-line buffer. */
5906 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5907 {
5908 beep_flush();
5909 return K_IGNORE;
5910 }
5911 cmdwin_type = ccline.cmdfirstc;
5912 if (cmdwin_type == NUL)
5913 cmdwin_type = '-';
5914
5915 /* Create the command-line buffer empty. */
5916 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5917 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5918 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5919 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5920 curbuf->b_p_ma = TRUE;
5921# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005922 curwin->w_p_rl = cmdmsg_rl;
5923 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924# endif
5925# ifdef FEAT_SCROLLBIND
5926 curwin->w_p_scb = FALSE;
5927# endif
5928
5929# ifdef FEAT_AUTOCMD
5930 /* Do execute autocommands for setting the filetype (load syntax). */
5931 --autocmd_block;
5932# endif
5933
Bram Moolenaar46152342005-09-07 21:18:43 +00005934 /* Showing the prompt may have set need_wait_return, reset it. */
5935 need_wait_return = FALSE;
5936
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 histtype = hist_char2type(ccline.cmdfirstc);
5938 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5939 {
5940 if (p_wc == TAB)
5941 {
5942 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5943 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5944 }
5945 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5946 }
5947
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005948 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5949 * sets 'textwidth' to 78). */
5950 curbuf->b_p_tw = 0;
5951
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 /* Fill the buffer with the history. */
5953 init_history();
5954 if (hislen > 0)
5955 {
5956 i = hisidx[histtype];
5957 if (i >= 0)
5958 {
5959 lnum = 0;
5960 do
5961 {
5962 if (++i == hislen)
5963 i = 0;
5964 if (history[histtype][i].hisstr != NULL)
5965 ml_append(lnum++, history[histtype][i].hisstr,
5966 (colnr_T)0, FALSE);
5967 }
5968 while (i != hisidx[histtype]);
5969 }
5970 }
5971
5972 /* Replace the empty last line with the current command-line and put the
5973 * cursor there. */
5974 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5975 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5976 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005977 changed_line_abv_curs();
5978 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00005979 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980
5981 /* Save the command line info, can be used recursively. */
5982 save_ccline = ccline;
5983 ccline.cmdbuff = NULL;
5984 ccline.cmdprompt = NULL;
5985
5986 /* No Ex mode here! */
5987 exmode_active = 0;
5988
5989 State = NORMAL;
5990# ifdef FEAT_MOUSE
5991 setmouse();
5992# endif
5993
5994# ifdef FEAT_AUTOCMD
5995 /* Trigger CmdwinEnter autocommands. */
5996 typestr[0] = cmdwin_type;
5997 typestr[1] = NUL;
5998 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00005999 if (restart_edit != 0) /* autocmd with ":startinsert" */
6000 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001# endif
6002
6003 i = RedrawingDisabled;
6004 RedrawingDisabled = 0;
6005
6006 /*
6007 * Call the main loop until <CR> or CTRL-C is typed.
6008 */
6009 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006010 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011
6012 RedrawingDisabled = i;
6013
6014# ifdef FEAT_AUTOCMD
6015 /* Trigger CmdwinLeave autocommands. */
6016 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6017# endif
6018
6019 /* Restore the comand line info. */
6020 ccline = save_ccline;
6021 cmdwin_type = 0;
6022
6023 exmode_active = save_exmode;
6024
6025 /* Safety check: The old window or buffer was deleted: It's a a bug when
6026 * this happens! */
6027 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6028 {
6029 cmdwin_result = Ctrl_C;
6030 EMSG(_("E199: Active window or buffer deleted"));
6031 }
6032 else
6033 {
6034# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6035 /* autocmds may abort script processing */
6036 if (aborting() && cmdwin_result != K_IGNORE)
6037 cmdwin_result = Ctrl_C;
6038# endif
6039 /* Set the new command line from the cmdline buffer. */
6040 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006041 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006043 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6044
6045 if (histtype == HIST_CMD)
6046 {
6047 /* Execute the command directly. */
6048 ccline.cmdbuff = vim_strsave((char_u *)p);
6049 cmdwin_result = CAR;
6050 }
6051 else
6052 {
6053 /* First need to cancel what we were doing. */
6054 ccline.cmdbuff = NULL;
6055 stuffcharReadbuff(':');
6056 stuffReadbuff((char_u *)p);
6057 stuffcharReadbuff(CAR);
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 }
6060 else if (cmdwin_result == K_XF2) /* :qa typed */
6061 {
6062 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6063 cmdwin_result = CAR;
6064 }
6065 else
6066 ccline.cmdbuff = vim_strsave(ml_get_curline());
6067 if (ccline.cmdbuff == NULL)
6068 cmdwin_result = Ctrl_C;
6069 else
6070 {
6071 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6072 ccline.cmdbufflen = ccline.cmdlen + 1;
6073 ccline.cmdpos = curwin->w_cursor.col;
6074 if (ccline.cmdpos > ccline.cmdlen)
6075 ccline.cmdpos = ccline.cmdlen;
6076 if (cmdwin_result == K_IGNORE)
6077 {
6078 set_cmdspos_cursor();
6079 redrawcmd();
6080 }
6081 }
6082
6083# ifdef FEAT_AUTOCMD
6084 /* Don't execute autocommands while deleting the window. */
6085 ++autocmd_block;
6086# endif
6087 wp = curwin;
6088 bp = curbuf;
6089 win_goto(old_curwin);
6090 win_close(wp, TRUE);
6091 close_buffer(NULL, bp, DOBUF_WIPE);
6092
6093 /* Restore window sizes. */
6094 win_size_restore(&winsizes);
6095
6096# ifdef FEAT_AUTOCMD
6097 --autocmd_block;
6098# endif
6099 }
6100
6101 ga_clear(&winsizes);
6102 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006103# ifdef FEAT_RIGHTLEFT
6104 cmdmsg_rl = save_cmdmsg_rl;
6105# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
6107 State = save_State;
6108# ifdef FEAT_MOUSE
6109 setmouse();
6110# endif
6111
6112 return cmdwin_result;
6113}
6114#endif /* FEAT_CMDWIN */
6115
6116/*
6117 * Used for commands that either take a simple command string argument, or:
6118 * cmd << endmarker
6119 * {script}
6120 * endmarker
6121 * Returns a pointer to allocated memory with {script} or NULL.
6122 */
6123 char_u *
6124script_get(eap, cmd)
6125 exarg_T *eap;
6126 char_u *cmd;
6127{
6128 char_u *theline;
6129 char *end_pattern = NULL;
6130 char dot[] = ".";
6131 garray_T ga;
6132
6133 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6134 return NULL;
6135
6136 ga_init2(&ga, 1, 0x400);
6137
6138 if (cmd[2] != NUL)
6139 end_pattern = (char *)skipwhite(cmd + 2);
6140 else
6141 end_pattern = dot;
6142
6143 for (;;)
6144 {
6145 theline = eap->getline(
6146#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006147 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148#endif
6149 NUL, eap->cookie, 0);
6150
6151 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
6152 break;
6153
6154 ga_concat(&ga, theline);
6155 ga_append(&ga, '\n');
6156 vim_free(theline);
6157 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006158 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 return (char_u *)ga.ga_data;
6161}