blob: b1142c472f8f202c2b5898aedafafaeea950439b [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 Moolenaar8299df92004-07-10 09:47:34 +000089static int cmdline_paste __ARGS((int regname, int literally));
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
106static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname));
107# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
108static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000109static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000110# endif
111#endif
112
113#ifdef FEAT_CMDWIN
114static int ex_window __ARGS((void));
115#endif
116
117/*
118 * getcmdline() - accept a command line starting with firstc.
119 *
120 * firstc == ':' get ":" command line.
121 * firstc == '/' or '?' get search pattern
122 * firstc == '=' get expression
123 * firstc == '@' get text for input() function
124 * firstc == '>' get text for debug mode
125 * firstc == NUL get text for :insert command
126 * firstc == -1 like NUL, and break on CTRL-C
127 *
128 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
129 * command line.
130 *
131 * Careful: getcmdline() can be called recursively!
132 *
133 * Return pointer to allocated string if there is a commandline, NULL
134 * otherwise.
135 */
136/*ARGSUSED*/
137 char_u *
138getcmdline(firstc, count, indent)
139 int firstc;
140 long count; /* only used for incremental search */
141 int indent; /* indent for inside conditionals */
142{
143 int c;
144 int i;
145 int j;
146 int gotesc = FALSE; /* TRUE when <ESC> just typed */
147 int do_abbr; /* when TRUE check for abbr. */
148#ifdef FEAT_CMDHIST
149 char_u *lookfor = NULL; /* string to match */
150 int hiscnt; /* current history line in use */
151 int histype; /* history type to be used */
152#endif
153#ifdef FEAT_SEARCH_EXTRA
154 pos_T old_cursor;
155 colnr_T old_curswant;
156 colnr_T old_leftcol;
157 linenr_T old_topline;
158# ifdef FEAT_DIFF
159 int old_topfill;
160# endif
161 linenr_T old_botline;
162 int did_incsearch = FALSE;
163 int incsearch_postponed = FALSE;
164#endif
165 int did_wild_list = FALSE; /* did wild_list() recently */
166 int wim_index = 0; /* index in wim_flags[] */
167 int res;
168 int save_msg_scroll = msg_scroll;
169 int save_State = State; /* remember State when called */
170 int some_key_typed = FALSE; /* one of the keys was typed */
171#ifdef FEAT_MOUSE
172 /* mouse drag and release events are ignored, unless they are
173 * preceded with a mouse down event */
174 int ignore_drag_release = TRUE;
175#endif
176#ifdef FEAT_EVAL
177 int break_ctrl_c = FALSE;
178#endif
179 expand_T xpc;
180 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000181#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
182 /* Everything that may work recursively should save and restore the
183 * current command line in save_ccline. That includes update_screen(), a
184 * custom status line may invoke ":normal". */
185 struct cmdline_info save_ccline;
186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187
188#ifdef FEAT_SNIFF
189 want_sniff_request = 0;
190#endif
191#ifdef FEAT_EVAL
192 if (firstc == -1)
193 {
194 firstc = NUL;
195 break_ctrl_c = TRUE;
196 }
197#endif
198#ifdef FEAT_RIGHTLEFT
199 /* start without Hebrew mapping for a command line */
200 if (firstc == ':' || firstc == '=' || firstc == '>')
201 cmd_hkmap = 0;
202#endif
203
204 ccline.overstrike = FALSE; /* always start in insert mode */
205#ifdef FEAT_SEARCH_EXTRA
206 old_cursor = curwin->w_cursor; /* needs to be restored later */
207 old_curswant = curwin->w_curswant;
208 old_leftcol = curwin->w_leftcol;
209 old_topline = curwin->w_topline;
210# ifdef FEAT_DIFF
211 old_topfill = curwin->w_topfill;
212# endif
213 old_botline = curwin->w_botline;
214#endif
215
216 /*
217 * set some variables for redrawcmd()
218 */
219 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000220 ccline.cmdindent = (firstc > 0 ? indent : 0);
221
222 /* alloc initial ccline.cmdbuff */
223 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 if (ccline.cmdbuff == NULL)
225 return NULL; /* out of memory */
226 ccline.cmdlen = ccline.cmdpos = 0;
227 ccline.cmdbuff[0] = NUL;
228
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000229 /* autoindent for :insert and :append */
230 if (firstc <= 0)
231 {
232 copy_spaces(ccline.cmdbuff, indent);
233 ccline.cmdbuff[indent] = NUL;
234 ccline.cmdpos = indent;
235 ccline.cmdspos = indent;
236 ccline.cmdlen = indent;
237 }
238
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 ExpandInit(&xpc);
240
241#ifdef FEAT_RIGHTLEFT
242 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
243 && (firstc == '/' || firstc == '?'))
244 cmdmsg_rl = TRUE;
245 else
246 cmdmsg_rl = FALSE;
247#endif
248
249 redir_off = TRUE; /* don't redirect the typed command */
250 if (!cmd_silent)
251 {
252 i = msg_scrolled;
253 msg_scrolled = 0; /* avoid wait_return message */
254 gotocmdline(TRUE);
255 msg_scrolled += i;
256 redrawcmdprompt(); /* draw prompt or indent */
257 set_cmdspos();
258 }
259 xpc.xp_context = EXPAND_NOTHING;
260 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000261#ifndef BACKSLASH_IN_FILENAME
262 xpc.xp_shell = FALSE;
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000265#if defined(FEAT_EVAL)
266 if (ccline.input_fn)
267 {
268 xpc.xp_context = ccline.xp_context;
269 xpc.xp_pattern = ccline.cmdbuff;
270 xpc.xp_arg = ccline.xp_arg;
271 }
272#endif
273
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 /*
275 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
276 * doing ":@0" when register 0 doesn't contain a CR.
277 */
278 msg_scroll = FALSE;
279
280 State = CMDLINE;
281
282 if (firstc == '/' || firstc == '?' || firstc == '@')
283 {
284 /* Use ":lmap" mappings for search pattern and input(). */
285 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
286 b_im_ptr = &curbuf->b_p_iminsert;
287 else
288 b_im_ptr = &curbuf->b_p_imsearch;
289 if (*b_im_ptr == B_IMODE_LMAP)
290 State |= LANGMAP;
291#ifdef USE_IM_CONTROL
292 im_set_active(*b_im_ptr == B_IMODE_IM);
293#endif
294 }
295#ifdef USE_IM_CONTROL
296 else if (p_imcmdline)
297 im_set_active(TRUE);
298#endif
299
300#ifdef FEAT_MOUSE
301 setmouse();
302#endif
303#ifdef CURSOR_SHAPE
304 ui_cursor_shape(); /* may show different cursor shape */
305#endif
306
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000307 /* When inside an autocommand for writing "exiting" may be set and
308 * terminal mode set to cooked. Need to set raw mode here then. */
309 settmode(TMODE_RAW);
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311#ifdef FEAT_CMDHIST
312 init_history();
313 hiscnt = hislen; /* set hiscnt to impossible history value */
314 histype = hist_char2type(firstc);
315#endif
316
317#ifdef FEAT_DIGRAPHS
318 do_digraph(-1); /* init digraph typahead */
319#endif
320
321 /*
322 * Collect the command string, handling editing keys.
323 */
324 for (;;)
325 {
326#ifdef USE_ON_FLY_SCROLL
327 dont_scroll = FALSE; /* allow scrolling here */
328#endif
329 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
330
331 cursorcmd(); /* set the cursor on the right spot */
332 c = safe_vgetc();
333 if (KeyTyped)
334 {
335 some_key_typed = TRUE;
336#ifdef FEAT_RIGHTLEFT
337 if (cmd_hkmap)
338 c = hkmap(c);
339# ifdef FEAT_FKMAP
340 if (cmd_fkmap)
341 c = cmdl_fkmap(c);
342# endif
343 if (cmdmsg_rl && !KeyStuffed)
344 {
345 /* Invert horizontal movements and operations. Only when
346 * typed by the user directly, not when the result of a
347 * mapping. */
348 switch (c)
349 {
350 case K_RIGHT: c = K_LEFT; break;
351 case K_S_RIGHT: c = K_S_LEFT; break;
352 case K_C_RIGHT: c = K_C_LEFT; break;
353 case K_LEFT: c = K_RIGHT; break;
354 case K_S_LEFT: c = K_S_RIGHT; break;
355 case K_C_LEFT: c = K_C_RIGHT; break;
356 }
357 }
358#endif
359 }
360
361 /*
362 * Ignore got_int when CTRL-C was typed here.
363 * Don't ignore it in :global, we really need to break then, e.g., for
364 * ":g/pat/normal /pat" (without the <CR>).
365 * Don't ignore it for the input() function.
366 */
367 if ((c == Ctrl_C
368#ifdef UNIX
369 || c == intr_char
370#endif
371 )
372#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
373 && firstc != '@'
374#endif
375#ifdef FEAT_EVAL
376 && !break_ctrl_c
377#endif
378 && !global_busy)
379 got_int = FALSE;
380
381#ifdef FEAT_CMDHIST
382 /* free old command line when finished moving around in the history
383 * list */
384 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000385 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000386 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 && c != K_PAGEDOWN && c != K_PAGEUP
388 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000389 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000390 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
391 {
392 vim_free(lookfor);
393 lookfor = NULL;
394 }
395#endif
396
397 /*
398 * <S-Tab> works like CTRL-P (unless 'wc' is <S-Tab>).
399 */
400 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles != -1)
401 c = Ctrl_P;
402
403#ifdef FEAT_WILDMENU
404 /* Special translations for 'wildmenu' */
405 if (did_wild_list && p_wmnu)
406 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000407 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000409 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 c = Ctrl_N;
411 }
412 /* Hitting CR after "emenu Name.": complete submenu */
413 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
414 && ccline.cmdpos > 1
415 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
416 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
417 && (c == '\n' || c == '\r' || c == K_KENTER))
418 c = K_DOWN;
419#endif
420
421 /* free expanded names when finished walking through matches */
422 if (xpc.xp_numfiles != -1
423 && !(c == p_wc && KeyTyped) && c != p_wcm
424 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
425 && c != Ctrl_L)
426 {
427 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
428 did_wild_list = FALSE;
429#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000430 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#endif
432 xpc.xp_context = EXPAND_NOTHING;
433 wim_index = 0;
434#ifdef FEAT_WILDMENU
435 if (p_wmnu && wild_menu_showing != 0)
436 {
437 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000438 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000439
440 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000441 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442
443 if (wild_menu_showing == WM_SCROLLED)
444 {
445 /* Entered command line, move it up */
446 cmdline_row--;
447 redrawcmd();
448 }
449 else if (save_p_ls != -1)
450 {
451 /* restore 'laststatus' and 'winminheight' */
452 p_ls = save_p_ls;
453 p_wmh = save_p_wmh;
454 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000455 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000457 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 redrawcmd();
459 save_p_ls = -1;
460 }
461 else
462 {
463# ifdef FEAT_VERTSPLIT
464 win_redraw_last_status(topframe);
465# else
466 lastwin->w_redr_status = TRUE;
467# endif
468 redraw_statuslines();
469 }
470 KeyTyped = skt;
471 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000472 if (ccline.input_fn)
473 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 }
475#endif
476 }
477
478#ifdef FEAT_WILDMENU
479 /* Special translations for 'wildmenu' */
480 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
481 {
482 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000483 if (ccline.cmdbuff[ccline.cmdpos - 1] == '.' && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000485 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000486 {
487 /* Hitting <Up>: Remove one submenu name in front of the
488 * cursor */
489 int found = FALSE;
490
491 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
492 i = 0;
493 while (--j > 0)
494 {
495 /* check for start of menu name */
496 if (ccline.cmdbuff[j] == ' '
497 && ccline.cmdbuff[j - 1] != '\\')
498 {
499 i = j + 1;
500 break;
501 }
502 /* check for start of submenu name */
503 if (ccline.cmdbuff[j] == '.'
504 && ccline.cmdbuff[j - 1] != '\\')
505 {
506 if (found)
507 {
508 i = j + 1;
509 break;
510 }
511 else
512 found = TRUE;
513 }
514 }
515 if (i > 0)
516 cmdline_del(i);
517 c = p_wc;
518 xpc.xp_context = EXPAND_NOTHING;
519 }
520 }
521 if (xpc.xp_context == EXPAND_FILES && p_wmnu)
522 {
523 char_u upseg[5];
524
525 upseg[0] = PATHSEP;
526 upseg[1] = '.';
527 upseg[2] = '.';
528 upseg[3] = PATHSEP;
529 upseg[4] = NUL;
530
531 if (ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000532 && c == K_DOWN
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 && (ccline.cmdbuff[ccline.cmdpos - 2] != '.'
534 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
535 {
536 /* go down a directory */
537 c = p_wc;
538 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000539 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 /* If in a direct ancestor, strip off one ../ to go down */
542 int found = FALSE;
543
544 j = ccline.cmdpos;
545 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
546 while (--j > i)
547 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000548#ifdef FEAT_MBYTE
549 if (has_mbyte)
550 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 if (vim_ispathsep(ccline.cmdbuff[j]))
553 {
554 found = TRUE;
555 break;
556 }
557 }
558 if (found
559 && ccline.cmdbuff[j - 1] == '.'
560 && ccline.cmdbuff[j - 2] == '.'
561 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
562 {
563 cmdline_del(j - 2);
564 c = p_wc;
565 }
566 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000567 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 {
569 /* go up a directory */
570 int found = FALSE;
571
572 j = ccline.cmdpos - 1;
573 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
574 while (--j > i)
575 {
576#ifdef FEAT_MBYTE
577 if (has_mbyte)
578 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
579#endif
580 if (vim_ispathsep(ccline.cmdbuff[j])
581#ifdef BACKSLASH_IN_FILENAME
582 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
583 == NULL
584#endif
585 )
586 {
587 if (found)
588 {
589 i = j + 1;
590 break;
591 }
592 else
593 found = TRUE;
594 }
595 }
596
597 if (!found)
598 j = i;
599 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
600 j += 4;
601 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
602 && j == i)
603 j += 3;
604 else
605 j = 0;
606 if (j > 0)
607 {
608 /* TODO this is only for DOS/UNIX systems - need to put in
609 * machine-specific stuff here and in upseg init */
610 cmdline_del(j);
611 put_on_cmdline(upseg + 1, 3, FALSE);
612 }
613 else if (ccline.cmdpos > i)
614 cmdline_del(i);
615 c = p_wc;
616 }
617 }
618#if 0 /* If enabled <Down> on a file takes you _completely_ out of wildmenu */
619 if (p_wmnu
620 && (xpc.xp_context == EXPAND_FILES
621 || xpc.xp_context == EXPAND_MENUNAMES)
622 && (c == K_UP || c == K_DOWN))
623 xpc.xp_context = EXPAND_NOTHING;
624#endif
625
626#endif /* FEAT_WILDMENU */
627
628 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
629 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
630 if (c == Ctrl_BSL)
631 {
632 ++no_mapping;
633 ++allow_keys;
634 c = safe_vgetc();
635 --no_mapping;
636 --allow_keys;
637 /* CTRL-\ e doesn't work when obtaining an expression. */
638 if (c != Ctrl_N && c != Ctrl_G
639 && (c != 'e' || ccline.cmdfirstc == '='))
640 {
641 vungetc(c);
642 c = Ctrl_BSL;
643 }
644#ifdef FEAT_EVAL
645 else if (c == 'e')
646 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000647 char_u *p = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648
649 /*
650 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000651 * Need to save and restore the current command line, to be
652 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 */
654 if (ccline.cmdpos == ccline.cmdlen)
655 new_cmdpos = 99999; /* keep it at the end */
656 else
657 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000658
659 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000661 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if (c == '=')
663 {
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000664 /* Need to save and restore ccline. And set cmdline_busy
665 * to avoid nasty things like going to another buffer when
666 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000667 save_cmdline(&save_ccline);
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000668 ++cmdline_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 p = get_expr_line();
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000670 --cmdline_busy;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000671 restore_cmdline(&save_ccline);
672
673 if (p != NULL && realloc_cmdbuff((int)STRLEN(p) + 1) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
675 ccline.cmdlen = STRLEN(p);
676 STRCPY(ccline.cmdbuff, p);
677 vim_free(p);
678
679 /* Restore the cursor or use the position set with
680 * set_cmdline_pos(). */
681 if (new_cmdpos > ccline.cmdlen)
682 ccline.cmdpos = ccline.cmdlen;
683 else
684 ccline.cmdpos = new_cmdpos;
685
686 KeyTyped = FALSE; /* Don't do p_wc completion. */
687 redrawcmd();
688 goto cmdline_changed;
689 }
690 }
691 beep_flush();
692 c = ESC;
693 }
694#endif
695 else
696 {
697 if (c == Ctrl_G && p_im && restart_edit == 0)
698 restart_edit = 'a';
699 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
700 in history */
701 goto returncmd; /* back to Normal mode */
702 }
703 }
704
705#ifdef FEAT_CMDWIN
706 if (c == cedit_key || c == K_CMDWIN)
707 {
708 /*
709 * Open a window to edit the command line (and history).
710 */
711 c = ex_window();
712 some_key_typed = TRUE;
713 }
714# ifdef FEAT_DIGRAPHS
715 else
716# endif
717#endif
718#ifdef FEAT_DIGRAPHS
719 c = do_digraph(c);
720#endif
721
722 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
723 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
724 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000725 /* In Ex mode a backslash escapes a newline. */
726 if (exmode_active
727 && c != ESC
728 && ccline.cmdpos > 0
729 && ccline.cmdpos == ccline.cmdlen
730 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000732 if (c == K_KENTER)
733 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000735 else
736 {
737 gotesc = FALSE; /* Might have typed ESC previously, don't
738 truncate the cmdline now. */
739 if (ccheck_abbr(c + ABBR_OFF))
740 goto cmdline_changed;
741 if (!cmd_silent)
742 {
743 windgoto(msg_row, 0);
744 out_flush();
745 }
746 break;
747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 }
749
750 /*
751 * Completion for 'wildchar' or 'wildcharm' key.
752 * - hitting <ESC> twice means: abandon command line.
753 * - wildcard expansion is only done when the 'wildchar' key is really
754 * typed, not when it comes from a macro
755 */
756 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
757 {
758 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
759 {
760 /* if 'wildmode' contains "list" may still need to list */
761 if (xpc.xp_numfiles > 1
762 && !did_wild_list
763 && (wim_flags[wim_index] & WIM_LIST))
764 {
765 (void)showmatches(&xpc, FALSE);
766 redrawcmd();
767 did_wild_list = TRUE;
768 }
769 if (wim_flags[wim_index] & WIM_LONGEST)
770 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
771 else if (wim_flags[wim_index] & WIM_FULL)
772 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
773 else
774 res = OK; /* don't insert 'wildchar' now */
775 }
776 else /* typed p_wc first time */
777 {
778 wim_index = 0;
779 j = ccline.cmdpos;
780 /* if 'wildmode' first contains "longest", get longest
781 * common part */
782 if (wim_flags[0] & WIM_LONGEST)
783 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
784 else
785 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP);
786
787 /* if interrupted while completing, behave like it failed */
788 if (got_int)
789 {
790 (void)vpeekc(); /* remove <C-C> from input stream */
791 got_int = FALSE; /* don't abandon the command line */
792 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
793#ifdef FEAT_WILDMENU
794 xpc.xp_context = EXPAND_NOTHING;
795#endif
796 goto cmdline_changed;
797 }
798
799 /* when more than one match, and 'wildmode' first contains
800 * "list", or no change and 'wildmode' contains "longest,list",
801 * list all matches */
802 if (res == OK && xpc.xp_numfiles > 1)
803 {
804 /* a "longest" that didn't do anything is skipped (but not
805 * "list:longest") */
806 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
807 wim_index = 1;
808 if ((wim_flags[wim_index] & WIM_LIST)
809#ifdef FEAT_WILDMENU
810 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
811#endif
812 )
813 {
814 if (!(wim_flags[0] & WIM_LONGEST))
815 {
816#ifdef FEAT_WILDMENU
817 int p_wmnu_save = p_wmnu;
818 p_wmnu = 0;
819#endif
820 nextwild(&xpc, WILD_PREV, 0); /* remove match */
821#ifdef FEAT_WILDMENU
822 p_wmnu = p_wmnu_save;
823#endif
824 }
825#ifdef FEAT_WILDMENU
826 (void)showmatches(&xpc, p_wmnu
827 && ((wim_flags[wim_index] & WIM_LIST) == 0));
828#else
829 (void)showmatches(&xpc, FALSE);
830#endif
831 redrawcmd();
832 did_wild_list = TRUE;
833 if (wim_flags[wim_index] & WIM_LONGEST)
834 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP);
835 else if (wim_flags[wim_index] & WIM_FULL)
836 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP);
837 }
838 else
839 vim_beep();
840 }
841#ifdef FEAT_WILDMENU
842 else if (xpc.xp_numfiles == -1)
843 xpc.xp_context = EXPAND_NOTHING;
844#endif
845 }
846 if (wim_index < 3)
847 ++wim_index;
848 if (c == ESC)
849 gotesc = TRUE;
850 if (res == OK)
851 goto cmdline_changed;
852 }
853
854 gotesc = FALSE;
855
856 /* <S-Tab> goes to last match, in a clumsy way */
857 if (c == K_S_TAB && KeyTyped)
858 {
859 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0) == OK
860 && nextwild(&xpc, WILD_PREV, 0) == OK
861 && nextwild(&xpc, WILD_PREV, 0) == OK)
862 goto cmdline_changed;
863 }
864
865 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
866 c = NL;
867
868 do_abbr = TRUE; /* default: check for abbreviation */
869
870 /*
871 * Big switch for a typed command line character.
872 */
873 switch (c)
874 {
875 case K_BS:
876 case Ctrl_H:
877 case K_DEL:
878 case K_KDEL:
879 case Ctrl_W:
880#ifdef FEAT_FKMAP
881 if (cmd_fkmap && c == K_BS)
882 c = K_DEL;
883#endif
884 if (c == K_KDEL)
885 c = K_DEL;
886
887 /*
888 * delete current character is the same as backspace on next
889 * character, except at end of line
890 */
891 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
892 ++ccline.cmdpos;
893#ifdef FEAT_MBYTE
894 if (has_mbyte && c == K_DEL)
895 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
896 ccline.cmdbuff + ccline.cmdpos);
897#endif
898 if (ccline.cmdpos > 0)
899 {
900 char_u *p;
901
902 j = ccline.cmdpos;
903 p = ccline.cmdbuff + j;
904#ifdef FEAT_MBYTE
905 if (has_mbyte)
906 {
907 p = mb_prevptr(ccline.cmdbuff, p);
908 if (c == Ctrl_W)
909 {
910 while (p > ccline.cmdbuff && vim_isspace(*p))
911 p = mb_prevptr(ccline.cmdbuff, p);
912 i = mb_get_class(p);
913 while (p > ccline.cmdbuff && mb_get_class(p) == i)
914 p = mb_prevptr(ccline.cmdbuff, p);
915 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000916 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 }
918 }
919 else
920#endif
921 if (c == Ctrl_W)
922 {
923 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
924 --p;
925 i = vim_iswordc(p[-1]);
926 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
927 && vim_iswordc(p[-1]) == i)
928 --p;
929 }
930 else
931 --p;
932 ccline.cmdpos = (int)(p - ccline.cmdbuff);
933 ccline.cmdlen -= j - ccline.cmdpos;
934 i = ccline.cmdpos;
935 while (i < ccline.cmdlen)
936 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
937
938 /* Truncate at the end, required for multi-byte chars. */
939 ccline.cmdbuff[ccline.cmdlen] = NUL;
940 redrawcmd();
941 }
942 else if (ccline.cmdlen == 0 && c != Ctrl_W
943 && ccline.cmdprompt == NULL && indent == 0)
944 {
945 /* In ex and debug mode it doesn't make sense to return. */
946 if (exmode_active
947#ifdef FEAT_EVAL
948 || ccline.cmdfirstc == '>'
949#endif
950 )
951 goto cmdline_not_changed;
952
953 vim_free(ccline.cmdbuff); /* no commandline to return */
954 ccline.cmdbuff = NULL;
955 if (!cmd_silent)
956 {
957#ifdef FEAT_RIGHTLEFT
958 if (cmdmsg_rl)
959 msg_col = Columns;
960 else
961#endif
962 msg_col = 0;
963 msg_putchar(' '); /* delete ':' */
964 }
965 redraw_cmdline = TRUE;
966 goto returncmd; /* back to cmd mode */
967 }
968 goto cmdline_changed;
969
970 case K_INS:
971 case K_KINS:
972#ifdef FEAT_FKMAP
973 /* if Farsi mode set, we are in reverse insert mode -
974 Do not change the mode */
975 if (cmd_fkmap)
976 beep_flush();
977 else
978#endif
979 ccline.overstrike = !ccline.overstrike;
980#ifdef CURSOR_SHAPE
981 ui_cursor_shape(); /* may show different cursor shape */
982#endif
983 goto cmdline_not_changed;
984
985 case Ctrl_HAT:
986 if (map_to_exists_mode((char_u *)"", LANGMAP))
987 {
988 /* ":lmap" mappings exists, toggle use of mappings. */
989 State ^= LANGMAP;
990#ifdef USE_IM_CONTROL
991 im_set_active(FALSE); /* Disable input method */
992#endif
993 if (b_im_ptr != NULL)
994 {
995 if (State & LANGMAP)
996 *b_im_ptr = B_IMODE_LMAP;
997 else
998 *b_im_ptr = B_IMODE_NONE;
999 }
1000 }
1001#ifdef USE_IM_CONTROL
1002 else
1003 {
1004 /* There are no ":lmap" mappings, toggle IM. When
1005 * 'imdisable' is set don't try getting the status, it's
1006 * always off. */
1007 if ((p_imdisable && b_im_ptr != NULL)
1008 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1009 {
1010 im_set_active(FALSE); /* Disable input method */
1011 if (b_im_ptr != NULL)
1012 *b_im_ptr = B_IMODE_NONE;
1013 }
1014 else
1015 {
1016 im_set_active(TRUE); /* Enable input method */
1017 if (b_im_ptr != NULL)
1018 *b_im_ptr = B_IMODE_IM;
1019 }
1020 }
1021#endif
1022 if (b_im_ptr != NULL)
1023 {
1024 if (b_im_ptr == &curbuf->b_p_iminsert)
1025 set_iminsert_global();
1026 else
1027 set_imsearch_global();
1028 }
1029#ifdef CURSOR_SHAPE
1030 ui_cursor_shape(); /* may show different cursor shape */
1031#endif
1032#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1033 /* Show/unshow value of 'keymap' in status lines later. */
1034 status_redraw_curbuf();
1035#endif
1036 goto cmdline_not_changed;
1037
1038/* case '@': only in very old vi */
1039 case Ctrl_U:
1040 /* delete all characters left of the cursor */
1041 j = ccline.cmdpos;
1042 ccline.cmdlen -= j;
1043 i = ccline.cmdpos = 0;
1044 while (i < ccline.cmdlen)
1045 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1046 /* Truncate at the end, required for multi-byte chars. */
1047 ccline.cmdbuff[ccline.cmdlen] = NUL;
1048 redrawcmd();
1049 goto cmdline_changed;
1050
1051#ifdef FEAT_CLIPBOARD
1052 case Ctrl_Y:
1053 /* Copy the modeless selection, if there is one. */
1054 if (clip_star.state != SELECT_CLEARED)
1055 {
1056 if (clip_star.state == SELECT_DONE)
1057 clip_copy_modeless_selection(TRUE);
1058 goto cmdline_not_changed;
1059 }
1060 break;
1061#endif
1062
1063 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1064 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001065 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001066 * ":normal" runs out of characters. */
1067 if (exmode_active
1068#ifdef FEAT_EX_EXTRA
1069 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1070#endif
1071 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 goto cmdline_not_changed;
1073
1074 gotesc = TRUE; /* will free ccline.cmdbuff after
1075 putting it in history */
1076 goto returncmd; /* back to cmd mode */
1077
1078 case Ctrl_R: /* insert register */
1079#ifdef USE_ON_FLY_SCROLL
1080 dont_scroll = TRUE; /* disallow scrolling here */
1081#endif
1082 putcmdline('"', TRUE);
1083 ++no_mapping;
1084 i = c = safe_vgetc(); /* CTRL-R <char> */
1085 if (i == Ctrl_O)
1086 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1087 if (i == Ctrl_R)
1088 c = safe_vgetc(); /* CTRL-R CTRL-R <char> */
1089 --no_mapping;
1090#ifdef FEAT_EVAL
1091 /*
1092 * Insert the result of an expression.
1093 * Need to save the current command line, to be able to enter
1094 * a new one...
1095 */
1096 new_cmdpos = -1;
1097 if (c == '=')
1098 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1100 {
1101 beep_flush();
1102 c = ESC;
1103 }
1104 else
1105 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001106 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001108 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110 }
1111#endif
1112 if (c != ESC) /* use ESC to cancel inserting register */
1113 {
1114 cmdline_paste(c, i == Ctrl_R);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001115
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001116#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001117 /* When there was a serious error abort getting the
1118 * command line. */
1119 if (aborting())
1120 {
1121 gotesc = TRUE; /* will free ccline.cmdbuff after
1122 putting it in history */
1123 goto returncmd; /* back to cmd mode */
1124 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 KeyTyped = FALSE; /* Don't do p_wc completion. */
1127#ifdef FEAT_EVAL
1128 if (new_cmdpos >= 0)
1129 {
1130 /* set_cmdline_pos() was used */
1131 if (new_cmdpos > ccline.cmdlen)
1132 ccline.cmdpos = ccline.cmdlen;
1133 else
1134 ccline.cmdpos = new_cmdpos;
1135 }
1136#endif
1137 }
1138 redrawcmd();
1139 goto cmdline_changed;
1140
1141 case Ctrl_D:
1142 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1143 break; /* Use ^D as normal char instead */
1144
1145 redrawcmd();
1146 continue; /* don't do incremental search now */
1147
1148 case K_RIGHT:
1149 case K_S_RIGHT:
1150 case K_C_RIGHT:
1151 do
1152 {
1153 if (ccline.cmdpos >= ccline.cmdlen)
1154 break;
1155 i = cmdline_charsize(ccline.cmdpos);
1156 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1157 break;
1158 ccline.cmdspos += i;
1159#ifdef FEAT_MBYTE
1160 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001161 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 + ccline.cmdpos);
1163 else
1164#endif
1165 ++ccline.cmdpos;
1166 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001167 while ((c == K_S_RIGHT || c == K_C_RIGHT
1168 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1170#ifdef FEAT_MBYTE
1171 if (has_mbyte)
1172 set_cmdspos_cursor();
1173#endif
1174 goto cmdline_not_changed;
1175
1176 case K_LEFT:
1177 case K_S_LEFT:
1178 case K_C_LEFT:
1179 do
1180 {
1181 if (ccline.cmdpos == 0)
1182 break;
1183 --ccline.cmdpos;
1184#ifdef FEAT_MBYTE
1185 if (has_mbyte) /* move to first byte of char */
1186 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1187 ccline.cmdbuff + ccline.cmdpos);
1188#endif
1189 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1190 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001191 while ((c == K_S_LEFT || c == K_C_LEFT
1192 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1194#ifdef FEAT_MBYTE
1195 if (has_mbyte)
1196 set_cmdspos_cursor();
1197#endif
1198 goto cmdline_not_changed;
1199
1200 case K_IGNORE:
1201 goto cmdline_not_changed; /* Ignore mouse */
1202
Bram Moolenaar4770d092006-01-12 23:22:24 +00001203#ifdef FEAT_GUI_W32
1204 /* On Win32 ignore <M-F4>, we get it when closing the window was
1205 * cancelled. */
1206 case K_F4:
1207 if (mod_mask == MOD_MASK_ALT)
1208 {
1209 redrawcmd(); /* somehow the cmdline is cleared */
1210 goto cmdline_not_changed;
1211 }
1212 break;
1213#endif
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215#ifdef FEAT_MOUSE
1216 case K_MIDDLEDRAG:
1217 case K_MIDDLERELEASE:
1218 goto cmdline_not_changed; /* Ignore mouse */
1219
1220 case K_MIDDLEMOUSE:
1221# ifdef FEAT_GUI
1222 /* When GUI is active, also paste when 'mouse' is empty */
1223 if (!gui.in_use)
1224# endif
1225 if (!mouse_has(MOUSE_COMMAND))
1226 goto cmdline_not_changed; /* Ignore mouse */
1227#ifdef FEAT_CLIPBOARD
1228 if (clip_star.available)
1229 cmdline_paste('*', TRUE);
1230 else
1231#endif
1232 cmdline_paste(0, TRUE);
1233 redrawcmd();
1234 goto cmdline_changed;
1235
1236#ifdef FEAT_DND
1237 case K_DROP:
1238 cmdline_paste('~', TRUE);
1239 redrawcmd();
1240 goto cmdline_changed;
1241#endif
1242
1243 case K_LEFTDRAG:
1244 case K_LEFTRELEASE:
1245 case K_RIGHTDRAG:
1246 case K_RIGHTRELEASE:
1247 /* Ignore drag and release events when the button-down wasn't
1248 * seen before. */
1249 if (ignore_drag_release)
1250 goto cmdline_not_changed;
1251 /* FALLTHROUGH */
1252 case K_LEFTMOUSE:
1253 case K_RIGHTMOUSE:
1254 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1255 ignore_drag_release = TRUE;
1256 else
1257 ignore_drag_release = FALSE;
1258# ifdef FEAT_GUI
1259 /* When GUI is active, also move when 'mouse' is empty */
1260 if (!gui.in_use)
1261# endif
1262 if (!mouse_has(MOUSE_COMMAND))
1263 goto cmdline_not_changed; /* Ignore mouse */
1264# ifdef FEAT_CLIPBOARD
1265 if (mouse_row < cmdline_row && clip_star.available)
1266 {
1267 int button, is_click, is_drag;
1268
1269 /*
1270 * Handle modeless selection.
1271 */
1272 button = get_mouse_button(KEY2TERMCAP1(c),
1273 &is_click, &is_drag);
1274 if (mouse_model_popup() && button == MOUSE_LEFT
1275 && (mod_mask & MOD_MASK_SHIFT))
1276 {
1277 /* Translate shift-left to right button. */
1278 button = MOUSE_RIGHT;
1279 mod_mask &= ~MOD_MASK_SHIFT;
1280 }
1281 clip_modeless(button, is_click, is_drag);
1282 goto cmdline_not_changed;
1283 }
1284# endif
1285
1286 set_cmdspos();
1287 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1288 ++ccline.cmdpos)
1289 {
1290 i = cmdline_charsize(ccline.cmdpos);
1291 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1292 && mouse_col < ccline.cmdspos % Columns + i)
1293 break;
1294#ifdef FEAT_MBYTE
1295 if (has_mbyte)
1296 {
1297 /* Count ">" for double-wide char that doesn't fit. */
1298 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001299 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 + ccline.cmdpos) - 1;
1301 }
1302#endif
1303 ccline.cmdspos += i;
1304 }
1305 goto cmdline_not_changed;
1306
1307 /* Mouse scroll wheel: ignored here */
1308 case K_MOUSEDOWN:
1309 case K_MOUSEUP:
1310 /* Alternate buttons ignored here */
1311 case K_X1MOUSE:
1312 case K_X1DRAG:
1313 case K_X1RELEASE:
1314 case K_X2MOUSE:
1315 case K_X2DRAG:
1316 case K_X2RELEASE:
1317 goto cmdline_not_changed;
1318
1319#endif /* FEAT_MOUSE */
1320
1321#ifdef FEAT_GUI
1322 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1323 case K_LEFTRELEASE_NM:
1324 goto cmdline_not_changed;
1325
1326 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001327 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 {
1329 gui_do_scroll();
1330 redrawcmd();
1331 }
1332 goto cmdline_not_changed;
1333
1334 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001335 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 {
1337 gui_do_horiz_scroll();
1338 redrawcmd();
1339 }
1340 goto cmdline_not_changed;
1341#endif
1342 case K_SELECT: /* end of Select mode mapping - ignore */
1343 goto cmdline_not_changed;
1344
1345 case Ctrl_B: /* begin of command line */
1346 case K_HOME:
1347 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 case K_S_HOME:
1349 case K_C_HOME:
1350 ccline.cmdpos = 0;
1351 set_cmdspos();
1352 goto cmdline_not_changed;
1353
1354 case Ctrl_E: /* end of command line */
1355 case K_END:
1356 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 case K_S_END:
1358 case K_C_END:
1359 ccline.cmdpos = ccline.cmdlen;
1360 set_cmdspos_cursor();
1361 goto cmdline_not_changed;
1362
1363 case Ctrl_A: /* all matches */
1364 if (nextwild(&xpc, WILD_ALL, 0) == FAIL)
1365 break;
1366 goto cmdline_changed;
1367
1368 case Ctrl_L: /* longest common part */
1369 if (nextwild(&xpc, WILD_LONGEST, 0) == FAIL)
1370 break;
1371 goto cmdline_changed;
1372
1373 case Ctrl_N: /* next match */
1374 case Ctrl_P: /* previous match */
1375 if (xpc.xp_numfiles > 0)
1376 {
1377 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, 0)
1378 == FAIL)
1379 break;
1380 goto cmdline_changed;
1381 }
1382
1383#ifdef FEAT_CMDHIST
1384 case K_UP:
1385 case K_DOWN:
1386 case K_S_UP:
1387 case K_S_DOWN:
1388 case K_PAGEUP:
1389 case K_KPAGEUP:
1390 case K_PAGEDOWN:
1391 case K_KPAGEDOWN:
1392 if (hislen == 0 || firstc == NUL) /* no history */
1393 goto cmdline_not_changed;
1394
1395 i = hiscnt;
1396
1397 /* save current command string so it can be restored later */
1398 if (lookfor == NULL)
1399 {
1400 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1401 goto cmdline_not_changed;
1402 lookfor[ccline.cmdpos] = NUL;
1403 }
1404
1405 j = (int)STRLEN(lookfor);
1406 for (;;)
1407 {
1408 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001409 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001410 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 {
1412 if (hiscnt == hislen) /* first time */
1413 hiscnt = hisidx[histype];
1414 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1415 hiscnt = hislen - 1;
1416 else if (hiscnt != hisidx[histype] + 1)
1417 --hiscnt;
1418 else /* at top of list */
1419 {
1420 hiscnt = i;
1421 break;
1422 }
1423 }
1424 else /* one step forwards */
1425 {
1426 /* on last entry, clear the line */
1427 if (hiscnt == hisidx[histype])
1428 {
1429 hiscnt = hislen;
1430 break;
1431 }
1432
1433 /* not on a history line, nothing to do */
1434 if (hiscnt == hislen)
1435 break;
1436 if (hiscnt == hislen - 1) /* wrap around */
1437 hiscnt = 0;
1438 else
1439 ++hiscnt;
1440 }
1441 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1442 {
1443 hiscnt = i;
1444 break;
1445 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001446 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001447 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 || STRNCMP(history[histype][hiscnt].hisstr,
1449 lookfor, (size_t)j) == 0)
1450 break;
1451 }
1452
1453 if (hiscnt != i) /* jumped to other entry */
1454 {
1455 char_u *p;
1456 int len;
1457 int old_firstc;
1458
1459 vim_free(ccline.cmdbuff);
1460 if (hiscnt == hislen)
1461 p = lookfor; /* back to the old one */
1462 else
1463 p = history[histype][hiscnt].hisstr;
1464
1465 if (histype == HIST_SEARCH
1466 && p != lookfor
1467 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1468 {
1469 /* Correct for the separator character used when
1470 * adding the history entry vs the one used now.
1471 * First loop: count length.
1472 * Second loop: copy the characters. */
1473 for (i = 0; i <= 1; ++i)
1474 {
1475 len = 0;
1476 for (j = 0; p[j] != NUL; ++j)
1477 {
1478 /* Replace old sep with new sep, unless it is
1479 * escaped. */
1480 if (p[j] == old_firstc
1481 && (j == 0 || p[j - 1] != '\\'))
1482 {
1483 if (i > 0)
1484 ccline.cmdbuff[len] = firstc;
1485 }
1486 else
1487 {
1488 /* Escape new sep, unless it is already
1489 * escaped. */
1490 if (p[j] == firstc
1491 && (j == 0 || p[j - 1] != '\\'))
1492 {
1493 if (i > 0)
1494 ccline.cmdbuff[len] = '\\';
1495 ++len;
1496 }
1497 if (i > 0)
1498 ccline.cmdbuff[len] = p[j];
1499 }
1500 ++len;
1501 }
1502 if (i == 0)
1503 {
1504 alloc_cmdbuff(len);
1505 if (ccline.cmdbuff == NULL)
1506 goto returncmd;
1507 }
1508 }
1509 ccline.cmdbuff[len] = NUL;
1510 }
1511 else
1512 {
1513 alloc_cmdbuff((int)STRLEN(p));
1514 if (ccline.cmdbuff == NULL)
1515 goto returncmd;
1516 STRCPY(ccline.cmdbuff, p);
1517 }
1518
1519 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1520 redrawcmd();
1521 goto cmdline_changed;
1522 }
1523 beep_flush();
1524 goto cmdline_not_changed;
1525#endif
1526
1527 case Ctrl_V:
1528 case Ctrl_Q:
1529#ifdef FEAT_MOUSE
1530 ignore_drag_release = TRUE;
1531#endif
1532 putcmdline('^', TRUE);
1533 c = get_literal(); /* get next (two) character(s) */
1534 do_abbr = FALSE; /* don't do abbreviation now */
1535#ifdef FEAT_MBYTE
1536 /* may need to remove ^ when composing char was typed */
1537 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1538 {
1539 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1540 msg_putchar(' ');
1541 cursorcmd();
1542 }
1543#endif
1544 break;
1545
1546#ifdef FEAT_DIGRAPHS
1547 case Ctrl_K:
1548#ifdef FEAT_MOUSE
1549 ignore_drag_release = TRUE;
1550#endif
1551 putcmdline('?', TRUE);
1552#ifdef USE_ON_FLY_SCROLL
1553 dont_scroll = TRUE; /* disallow scrolling here */
1554#endif
1555 c = get_digraph(TRUE);
1556 if (c != NUL)
1557 break;
1558
1559 redrawcmd();
1560 goto cmdline_not_changed;
1561#endif /* FEAT_DIGRAPHS */
1562
1563#ifdef FEAT_RIGHTLEFT
1564 case Ctrl__: /* CTRL-_: switch language mode */
1565 if (!p_ari)
1566 break;
1567#ifdef FEAT_FKMAP
1568 if (p_altkeymap)
1569 {
1570 cmd_fkmap = !cmd_fkmap;
1571 if (cmd_fkmap) /* in Farsi always in Insert mode */
1572 ccline.overstrike = FALSE;
1573 }
1574 else /* Hebrew is default */
1575#endif
1576 cmd_hkmap = !cmd_hkmap;
1577 goto cmdline_not_changed;
1578#endif
1579
1580 default:
1581#ifdef UNIX
1582 if (c == intr_char)
1583 {
1584 gotesc = TRUE; /* will free ccline.cmdbuff after
1585 putting it in history */
1586 goto returncmd; /* back to Normal mode */
1587 }
1588#endif
1589 /*
1590 * Normal character with no special meaning. Just set mod_mask
1591 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1592 * the string <S-Space>. This should only happen after ^V.
1593 */
1594 if (!IS_SPECIAL(c))
1595 mod_mask = 0x0;
1596 break;
1597 }
1598 /*
1599 * End of switch on command line character.
1600 * We come here if we have a normal character.
1601 */
1602
1603 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1604#ifdef FEAT_MBYTE
1605 /* Add ABBR_OFF for characters above 0x100, this is
1606 * what check_abbr() expects. */
1607 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1608#endif
1609 c))
1610 goto cmdline_changed;
1611
1612 /*
1613 * put the character in the command line
1614 */
1615 if (IS_SPECIAL(c) || mod_mask != 0)
1616 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1617 else
1618 {
1619#ifdef FEAT_MBYTE
1620 if (has_mbyte)
1621 {
1622 j = (*mb_char2bytes)(c, IObuff);
1623 IObuff[j] = NUL; /* exclude composing chars */
1624 put_on_cmdline(IObuff, j, TRUE);
1625 }
1626 else
1627#endif
1628 {
1629 IObuff[0] = c;
1630 put_on_cmdline(IObuff, 1, TRUE);
1631 }
1632 }
1633 goto cmdline_changed;
1634
1635/*
1636 * This part implements incremental searches for "/" and "?"
1637 * Jump to cmdline_not_changed when a character has been read but the command
1638 * line did not change. Then we only search and redraw if something changed in
1639 * the past.
1640 * Jump to cmdline_changed when the command line did change.
1641 * (Sorry for the goto's, I know it is ugly).
1642 */
1643cmdline_not_changed:
1644#ifdef FEAT_SEARCH_EXTRA
1645 if (!incsearch_postponed)
1646 continue;
1647#endif
1648
1649cmdline_changed:
1650#ifdef FEAT_SEARCH_EXTRA
1651 /*
1652 * 'incsearch' highlighting.
1653 */
1654 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1655 {
1656 /* if there is a character waiting, search and redraw later */
1657 if (char_avail())
1658 {
1659 incsearch_postponed = TRUE;
1660 continue;
1661 }
1662 incsearch_postponed = FALSE;
1663 curwin->w_cursor = old_cursor; /* start at old position */
1664
1665 /* If there is no command line, don't do anything */
1666 if (ccline.cmdlen == 0)
1667 i = 0;
1668 else
1669 {
1670 cursor_off(); /* so the user knows we're busy */
1671 out_flush();
1672 ++emsg_off; /* So it doesn't beep if bad expr */
1673 i = do_search(NULL, firstc, ccline.cmdbuff, count,
1674 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK);
1675 --emsg_off;
1676 /* if interrupted while searching, behave like it failed */
1677 if (got_int)
1678 {
1679 (void)vpeekc(); /* remove <C-C> from input stream */
1680 got_int = FALSE; /* don't abandon the command line */
1681 i = 0;
1682 }
1683 else if (char_avail())
1684 /* cancelled searching because a char was typed */
1685 incsearch_postponed = TRUE;
1686 }
1687 if (i)
1688 highlight_match = TRUE; /* highlight position */
1689 else
1690 highlight_match = FALSE; /* remove highlight */
1691
1692 /* first restore the old curwin values, so the screen is
1693 * positioned in the same way as the actual search command */
1694 curwin->w_leftcol = old_leftcol;
1695 curwin->w_topline = old_topline;
1696# ifdef FEAT_DIFF
1697 curwin->w_topfill = old_topfill;
1698# endif
1699 curwin->w_botline = old_botline;
1700 changed_cline_bef_curs();
1701 update_topline();
1702
1703 if (i != 0)
1704 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001705 pos_T save_pos = curwin->w_cursor;
1706
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 /*
1708 * First move cursor to end of match, then to start. This
1709 * moves the whole match onto the screen when 'nowrap' is set.
1710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 curwin->w_cursor.lnum += search_match_lines;
1712 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001713 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1714 {
1715 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1716 coladvance((colnr_T)MAXCOL);
1717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 validate_cursor();
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001719 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 }
1721 validate_cursor();
1722
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001723 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 update_screen(NOT_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001725 restore_cmdline(&save_ccline);
1726
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 msg_starthere();
1728 redrawcmdline();
1729 did_incsearch = TRUE;
1730 }
1731#else /* FEAT_SEARCH_EXTRA */
1732 ;
1733#endif
1734
1735#ifdef FEAT_RIGHTLEFT
1736 if (cmdmsg_rl
1737# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001738 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739# endif
1740 )
1741 /* Always redraw the whole command line to fix shaping and
1742 * right-left typing. Not efficient, but it works. */
1743 redrawcmd();
1744#endif
1745 }
1746
1747returncmd:
1748
1749#ifdef FEAT_RIGHTLEFT
1750 cmdmsg_rl = FALSE;
1751#endif
1752
1753#ifdef FEAT_FKMAP
1754 cmd_fkmap = 0;
1755#endif
1756
1757 ExpandCleanup(&xpc);
1758
1759#ifdef FEAT_SEARCH_EXTRA
1760 if (did_incsearch)
1761 {
1762 curwin->w_cursor = old_cursor;
1763 curwin->w_curswant = old_curswant;
1764 curwin->w_leftcol = old_leftcol;
1765 curwin->w_topline = old_topline;
1766# ifdef FEAT_DIFF
1767 curwin->w_topfill = old_topfill;
1768# endif
1769 curwin->w_botline = old_botline;
1770 highlight_match = FALSE;
1771 validate_cursor(); /* needed for TAB */
1772 redraw_later(NOT_VALID);
1773 }
1774#endif
1775
1776 if (ccline.cmdbuff != NULL)
1777 {
1778 /*
1779 * Put line in history buffer (":" and "=" only when it was typed).
1780 */
1781#ifdef FEAT_CMDHIST
1782 if (ccline.cmdlen && firstc != NUL
1783 && (some_key_typed || histype == HIST_SEARCH))
1784 {
1785 add_to_history(histype, ccline.cmdbuff, TRUE,
1786 histype == HIST_SEARCH ? firstc : NUL);
1787 if (firstc == ':')
1788 {
1789 vim_free(new_last_cmdline);
1790 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1791 }
1792 }
1793#endif
1794
1795 if (gotesc) /* abandon command line */
1796 {
1797 vim_free(ccline.cmdbuff);
1798 ccline.cmdbuff = NULL;
1799 if (msg_scrolled == 0)
1800 compute_cmdrow();
1801 MSG("");
1802 redraw_cmdline = TRUE;
1803 }
1804 }
1805
1806 /*
1807 * If the screen was shifted up, redraw the whole screen (later).
1808 * If the line is too long, clear it, so ruler and shown command do
1809 * not get printed in the middle of it.
1810 */
1811 msg_check();
1812 msg_scroll = save_msg_scroll;
1813 redir_off = FALSE;
1814
1815 /* When the command line was typed, no need for a wait-return prompt. */
1816 if (some_key_typed)
1817 need_wait_return = FALSE;
1818
1819 State = save_State;
1820#ifdef USE_IM_CONTROL
1821 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1822 im_save_status(b_im_ptr);
1823 im_set_active(FALSE);
1824#endif
1825#ifdef FEAT_MOUSE
1826 setmouse();
1827#endif
1828#ifdef CURSOR_SHAPE
1829 ui_cursor_shape(); /* may show different cursor shape */
1830#endif
1831
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001832 {
1833 char_u *p = ccline.cmdbuff;
1834
1835 /* Make ccline empty, getcmdline() may try to use it. */
1836 ccline.cmdbuff = NULL;
1837 return p;
1838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839}
1840
1841#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1842/*
1843 * Get a command line with a prompt.
1844 * This is prepared to be called recursively from getcmdline() (e.g. by
1845 * f_input() when evaluating an expression from CTRL-R =).
1846 * Returns the command line in allocated memory, or NULL.
1847 */
1848 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001849getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 int firstc;
1851 char_u *prompt; /* command line prompt */
1852 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001853 int xp_context; /* type of expansion */
1854 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855{
1856 char_u *s;
1857 struct cmdline_info save_ccline;
1858 int msg_col_save = msg_col;
1859
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001860 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 ccline.cmdprompt = prompt;
1862 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001863# ifdef FEAT_EVAL
1864 ccline.xp_context = xp_context;
1865 ccline.xp_arg = xp_arg;
1866 ccline.input_fn = (firstc == '@');
1867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001869 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 /* Restore msg_col, the prompt from input() may have changed it. */
1871 msg_col = msg_col_save;
1872
1873 return s;
1874}
1875#endif
1876
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001877/*
1878 * Return TRUE when the command line is being edited. That means the current
1879 * buffer and window can't be changed.
1880 */
1881 int
1882editing_cmdline()
1883{
1884#ifdef FEAT_CMDWIN
1885 if (cmdwin_type != 0)
1886 return TRUE;
1887#endif
1888 return cmdline_busy;
1889}
1890
1891/*
1892 * Give an error message for a command that isn't allowed while the cmdline
1893 * window is open or editing the cmdline in another way.
1894 */
1895 void
1896editing_cmdline_msg()
1897{
1898#ifdef FEAT_CMDWIN
1899 if (cmdwin_type != 0)
1900 EMSG(_(e_cmdwin));
1901 else
1902#endif
1903 EMSG(_(e_secure));
1904}
1905
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 static int
1907cmdline_charsize(idx)
1908 int idx;
1909{
1910#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1911 if (cmdline_star > 0) /* showing '*', always 1 position */
1912 return 1;
1913#endif
1914 return ptr2cells(ccline.cmdbuff + idx);
1915}
1916
1917/*
1918 * Compute the offset of the cursor on the command line for the prompt and
1919 * indent.
1920 */
1921 static void
1922set_cmdspos()
1923{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001924 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 ccline.cmdspos = 1 + ccline.cmdindent;
1926 else
1927 ccline.cmdspos = 0 + ccline.cmdindent;
1928}
1929
1930/*
1931 * Compute the screen position for the cursor on the command line.
1932 */
1933 static void
1934set_cmdspos_cursor()
1935{
1936 int i, m, c;
1937
1938 set_cmdspos();
1939 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001940 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001942 if (m < 0) /* overflow, Columns or Rows at weird value */
1943 m = MAXCOL;
1944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 else
1946 m = MAXCOL;
1947 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1948 {
1949 c = cmdline_charsize(i);
1950#ifdef FEAT_MBYTE
1951 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1952 if (has_mbyte)
1953 correct_cmdspos(i, c);
1954#endif
1955 /* If the cmdline doesn't fit, put cursor on last visible char. */
1956 if ((ccline.cmdspos += c) >= m)
1957 {
1958 ccline.cmdpos = i - 1;
1959 ccline.cmdspos -= c;
1960 break;
1961 }
1962#ifdef FEAT_MBYTE
1963 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001964 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#endif
1966 }
1967}
1968
1969#ifdef FEAT_MBYTE
1970/*
1971 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1972 * character that doesn't fit, so that a ">" must be displayed.
1973 */
1974 static void
1975correct_cmdspos(idx, cells)
1976 int idx;
1977 int cells;
1978{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001979 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1981 && ccline.cmdspos % Columns + cells > Columns)
1982 ccline.cmdspos++;
1983}
1984#endif
1985
1986/*
1987 * Get an Ex command line for the ":" command.
1988 */
1989/* ARGSUSED */
1990 char_u *
1991getexline(c, dummy, indent)
1992 int c; /* normally ':', NUL for ":append" */
1993 void *dummy; /* cookie not used */
1994 int indent; /* indent for inside conditionals */
1995{
1996 /* When executing a register, remove ':' that's in front of each line. */
1997 if (exec_from_reg && vpeekc() == ':')
1998 (void)vgetc();
1999 return getcmdline(c, 1L, indent);
2000}
2001
2002/*
2003 * Get an Ex command line for Ex mode.
2004 * In Ex mode we only use the OS supplied line editing features and no
2005 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002006 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 */
2008/* ARGSUSED */
2009 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002010getexmodeline(promptc, dummy, indent)
2011 int promptc; /* normally ':', NUL for ":append" and '?' for
2012 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 void *dummy; /* cookie not used */
2014 int indent; /* indent for inside conditionals */
2015{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002016 garray_T line_ga;
2017 char_u *pend;
2018 int startcol = 0;
2019 int c1;
2020 int escaped = FALSE; /* CTRL-V typed */
2021 int vcol = 0;
2022 char_u *p;
2023 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024
2025 /* Switch cursor on now. This avoids that it happens after the "\n", which
2026 * confuses the system function that computes tabstops. */
2027 cursor_on();
2028
2029 /* always start in column 0; write a newline if necessary */
2030 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002031 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002033 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002035 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002036 if (p_prompt)
2037 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 while (indent-- > 0)
2039 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
2042
2043 ga_init2(&line_ga, 1, 30);
2044
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002045 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002046 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002047 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002048 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002049 while (indent >= 8)
2050 {
2051 ga_append(&line_ga, TAB);
2052 msg_puts((char_u *)" ");
2053 indent -= 8;
2054 }
2055 while (indent-- > 0)
2056 {
2057 ga_append(&line_ga, ' ');
2058 msg_putchar(' ');
2059 }
2060 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002061 ++no_mapping;
2062 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 /*
2065 * Get the line, one character at a time.
2066 */
2067 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002068 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 {
2070 if (ga_grow(&line_ga, 40) == FAIL)
2071 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002072 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002074 /* Get one character at a time. Don't use inchar(), it can't handle
2075 * special characters. */
2076 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077
2078 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002079 * Handle line editing.
2080 * Previously this was left to the system, putting the terminal in
2081 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002083 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002085 msg_putchar('\n');
2086 break;
2087 }
2088
2089 if (!escaped)
2090 {
2091 /* CR typed means "enter", which is NL */
2092 if (c1 == '\r')
2093 c1 = '\n';
2094
2095 if (c1 == BS || c1 == K_BS
2096 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002098 if (line_ga.ga_len > 0)
2099 {
2100 --line_ga.ga_len;
2101 goto redraw;
2102 }
2103 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002106 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002108 msg_col = startcol;
2109 msg_clr_eos();
2110 line_ga.ga_len = 0;
2111 continue;
2112 }
2113
2114 if (c1 == Ctrl_T)
2115 {
2116 p = (char_u *)line_ga.ga_data;
2117 p[line_ga.ga_len] = NUL;
2118 indent = get_indent_str(p, 8);
2119 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2120add_indent:
2121 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002123 char_u *s = skipwhite(p);
2124
2125 ga_grow(&line_ga, 1);
2126 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2127 *s = ' ';
2128 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002130redraw:
2131 /* redraw the line */
2132 msg_col = startcol;
2133 windgoto(msg_row, msg_col);
2134 vcol = 0;
2135 for (p = (char_u *)line_ga.ga_data;
2136 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002138 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002140 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002142 msg_putchar(' ');
2143 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002145 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002147 msg_outtrans_len(p, 1);
2148 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
2150 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002151 msg_clr_eos();
2152 continue;
2153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002155 if (c1 == Ctrl_D)
2156 {
2157 /* Delete one shiftwidth. */
2158 p = (char_u *)line_ga.ga_data;
2159 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002161 if (prev_char == '^')
2162 ex_keep_indent = TRUE;
2163 indent = 0;
2164 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else
2167 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002168 p[line_ga.ga_len] = NUL;
2169 indent = get_indent_str(p, 8);
2170 --indent;
2171 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002173 while (get_indent_str(p, 8) > indent)
2174 {
2175 char_u *s = skipwhite(p);
2176
2177 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2178 --line_ga.ga_len;
2179 }
2180 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002182
2183 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2184 {
2185 escaped = TRUE;
2186 continue;
2187 }
2188
2189 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2190 if (IS_SPECIAL(c1))
2191 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002193
2194 if (IS_SPECIAL(c1))
2195 c1 = '?';
2196 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2197 prev_char = c1;
2198 if (c1 == '\n')
2199 msg_putchar('\n');
2200 else if (c1 == TAB)
2201 {
2202 /* Don't use chartabsize(), 'ts' can be different */
2203 do
2204 {
2205 msg_putchar(' ');
2206 } while (++vcol % 8);
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002210 msg_outtrans_len(
2211 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2212 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 ++line_ga.ga_len;
2215 escaped = FALSE;
2216
2217 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002218 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002219
2220 /* we are done when a NL is entered, but not when it comes after a
2221 * backslash */
2222 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2223 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002226 --pend;
2227 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 }
2230 }
2231
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002232 --no_mapping;
2233 --allow_keys;
2234
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 /* make following messages go to the next line */
2236 msg_didout = FALSE;
2237 msg_col = 0;
2238 if (msg_row < Rows - 1)
2239 ++msg_row;
2240 emsg_on_display = FALSE; /* don't want ui_delay() */
2241
2242 if (got_int)
2243 ga_clear(&line_ga);
2244
2245 return (char_u *)line_ga.ga_data;
2246}
2247
Bram Moolenaare344bea2005-09-01 20:46:49 +00002248# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2249 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250/*
2251 * Return TRUE if ccline.overstrike is on.
2252 */
2253 int
2254cmdline_overstrike()
2255{
2256 return ccline.overstrike;
2257}
2258
2259/*
2260 * Return TRUE if the cursor is at the end of the cmdline.
2261 */
2262 int
2263cmdline_at_end()
2264{
2265 return (ccline.cmdpos >= ccline.cmdlen);
2266}
2267#endif
2268
Bram Moolenaar9372a112005-12-06 19:59:18 +00002269#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270/*
2271 * Return the virtual column number at the current cursor position.
2272 * This is used by the IM code to obtain the start of the preedit string.
2273 */
2274 colnr_T
2275cmdline_getvcol_cursor()
2276{
2277 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2278 return MAXCOL;
2279
2280# ifdef FEAT_MBYTE
2281 if (has_mbyte)
2282 {
2283 colnr_T col;
2284 int i = 0;
2285
2286 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002287 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288
2289 return col;
2290 }
2291 else
2292# endif
2293 return ccline.cmdpos;
2294}
2295#endif
2296
2297#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2298/*
2299 * If part of the command line is an IM preedit string, redraw it with
2300 * IM feedback attributes. The cursor position is restored after drawing.
2301 */
2302 static void
2303redrawcmd_preedit()
2304{
2305 if ((State & CMDLINE)
2306 && xic != NULL
2307 && im_get_status()
2308 && !p_imdisable
2309 && im_is_preediting())
2310 {
2311 int cmdpos = 0;
2312 int cmdspos;
2313 int old_row;
2314 int old_col;
2315 colnr_T col;
2316
2317 old_row = msg_row;
2318 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002319 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321# ifdef FEAT_MBYTE
2322 if (has_mbyte)
2323 {
2324 for (col = 0; col < preedit_start_col
2325 && cmdpos < ccline.cmdlen; ++col)
2326 {
2327 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002328 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
2330 }
2331 else
2332# endif
2333 {
2334 cmdspos += preedit_start_col;
2335 cmdpos += preedit_start_col;
2336 }
2337
2338 msg_row = cmdline_row + (cmdspos / (int)Columns);
2339 msg_col = cmdspos % (int)Columns;
2340 if (msg_row >= Rows)
2341 msg_row = Rows - 1;
2342
2343 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2344 {
2345 int char_len;
2346 int char_attr;
2347
2348 char_attr = im_get_feedback_attr(col);
2349 if (char_attr < 0)
2350 break; /* end of preedit string */
2351
2352# ifdef FEAT_MBYTE
2353 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002354 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 else
2356# endif
2357 char_len = 1;
2358
2359 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2360 cmdpos += char_len;
2361 }
2362
2363 msg_row = old_row;
2364 msg_col = old_col;
2365 }
2366}
2367#endif /* FEAT_XIM && FEAT_GUI_GTK */
2368
2369/*
2370 * Allocate a new command line buffer.
2371 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2372 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2373 */
2374 static void
2375alloc_cmdbuff(len)
2376 int len;
2377{
2378 /*
2379 * give some extra space to avoid having to allocate all the time
2380 */
2381 if (len < 80)
2382 len = 100;
2383 else
2384 len += 20;
2385
2386 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2387 ccline.cmdbufflen = len;
2388}
2389
2390/*
2391 * Re-allocate the command line to length len + something extra.
2392 * return FAIL for failure, OK otherwise
2393 */
2394 static int
2395realloc_cmdbuff(len)
2396 int len;
2397{
2398 char_u *p;
2399
2400 p = ccline.cmdbuff;
2401 alloc_cmdbuff(len); /* will get some more */
2402 if (ccline.cmdbuff == NULL) /* out of memory */
2403 {
2404 ccline.cmdbuff = p; /* keep the old one */
2405 return FAIL;
2406 }
2407 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2408 vim_free(p);
2409 return OK;
2410}
2411
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002412#if defined(FEAT_ARABIC) || defined(PROTO)
2413static char_u *arshape_buf = NULL;
2414
2415# if defined(EXITFREE) || defined(PROTO)
2416 void
2417free_cmdline_buf()
2418{
2419 vim_free(arshape_buf);
2420}
2421# endif
2422#endif
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424/*
2425 * Draw part of the cmdline at the current cursor position. But draw stars
2426 * when cmdline_star is TRUE.
2427 */
2428 static void
2429draw_cmdline(start, len)
2430 int start;
2431 int len;
2432{
2433#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2434 int i;
2435
2436 if (cmdline_star > 0)
2437 for (i = 0; i < len; ++i)
2438 {
2439 msg_putchar('*');
2440# ifdef FEAT_MBYTE
2441 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002442 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443# endif
2444 }
2445 else
2446#endif
2447#ifdef FEAT_ARABIC
2448 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2449 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 static int buflen = 0;
2451 char_u *p;
2452 int j;
2453 int newlen = 0;
2454 int mb_l;
2455 int pc, pc1;
2456 int prev_c = 0;
2457 int prev_c1 = 0;
2458 int u8c, u8c_c1, u8c_c2;
2459 int nc = 0;
2460 int dummy;
2461
2462 /*
2463 * Do arabic shaping into a temporary buffer. This is very
2464 * inefficient!
2465 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002466 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 {
2468 /* Re-allocate the buffer. We keep it around to avoid a lot of
2469 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002470 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002471 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002472 arshape_buf = alloc(buflen);
2473 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 return; /* out of memory */
2475 }
2476
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002477 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2478 {
2479 /* Prepend a space to draw the leading composing char on. */
2480 arshape_buf[0] = ' ';
2481 newlen = 1;
2482 }
2483
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 for (j = start; j < start + len; j += mb_l)
2485 {
2486 p = ccline.cmdbuff + j;
2487 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002488 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (ARABIC_CHAR(u8c))
2490 {
2491 /* Do Arabic shaping. */
2492 if (cmdmsg_rl)
2493 {
2494 /* displaying from right to left */
2495 pc = prev_c;
2496 pc1 = prev_c1;
2497 prev_c1 = u8c_c1;
2498 if (j + mb_l >= start + len)
2499 nc = NUL;
2500 else
2501 nc = utf_ptr2char(p + mb_l);
2502 }
2503 else
2504 {
2505 /* displaying from left to right */
2506 if (j + mb_l >= start + len)
2507 pc = NUL;
2508 else
2509 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2510 start + len - j - mb_l);
2511 nc = prev_c;
2512 }
2513 prev_c = u8c;
2514
2515 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2516
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002517 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 if (u8c_c1 != 0)
2519 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002520 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002522 newlen += (*mb_char2bytes)(u8c_c2,
2523 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
2525 }
2526 else
2527 {
2528 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002529 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 newlen += mb_l;
2531 }
2532 }
2533
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002534 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 }
2536 else
2537#endif
2538 msg_outtrans_len(ccline.cmdbuff + start, len);
2539}
2540
2541/*
2542 * Put a character on the command line. Shifts the following text to the
2543 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2544 * "c" must be printable (fit in one display cell)!
2545 */
2546 void
2547putcmdline(c, shift)
2548 int c;
2549 int shift;
2550{
2551 if (cmd_silent)
2552 return;
2553 msg_no_more = TRUE;
2554 msg_putchar(c);
2555 if (shift)
2556 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2557 msg_no_more = FALSE;
2558 cursorcmd();
2559}
2560
2561/*
2562 * Undo a putcmdline(c, FALSE).
2563 */
2564 void
2565unputcmdline()
2566{
2567 if (cmd_silent)
2568 return;
2569 msg_no_more = TRUE;
2570 if (ccline.cmdlen == ccline.cmdpos)
2571 msg_putchar(' ');
2572 else
2573 draw_cmdline(ccline.cmdpos, 1);
2574 msg_no_more = FALSE;
2575 cursorcmd();
2576}
2577
2578/*
2579 * Put the given string, of the given length, onto the command line.
2580 * If len is -1, then STRLEN() is used to calculate the length.
2581 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2582 * part will be redrawn, otherwise it will not. If this function is called
2583 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2584 * called afterwards.
2585 */
2586 int
2587put_on_cmdline(str, len, redraw)
2588 char_u *str;
2589 int len;
2590 int redraw;
2591{
2592 int retval;
2593 int i;
2594 int m;
2595 int c;
2596
2597 if (len < 0)
2598 len = (int)STRLEN(str);
2599
2600 /* Check if ccline.cmdbuff needs to be longer */
2601 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2602 retval = realloc_cmdbuff(ccline.cmdlen + len);
2603 else
2604 retval = OK;
2605 if (retval == OK)
2606 {
2607 if (!ccline.overstrike)
2608 {
2609 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2610 ccline.cmdbuff + ccline.cmdpos,
2611 (size_t)(ccline.cmdlen - ccline.cmdpos));
2612 ccline.cmdlen += len;
2613 }
2614 else
2615 {
2616#ifdef FEAT_MBYTE
2617 if (has_mbyte)
2618 {
2619 /* Count nr of characters in the new string. */
2620 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002621 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 ++m;
2623 /* Count nr of bytes in cmdline that are overwritten by these
2624 * characters. */
2625 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002626 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 --m;
2628 if (i < ccline.cmdlen)
2629 {
2630 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2631 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2632 ccline.cmdlen += ccline.cmdpos + len - i;
2633 }
2634 else
2635 ccline.cmdlen = ccline.cmdpos + len;
2636 }
2637 else
2638#endif
2639 if (ccline.cmdpos + len > ccline.cmdlen)
2640 ccline.cmdlen = ccline.cmdpos + len;
2641 }
2642 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2643 ccline.cmdbuff[ccline.cmdlen] = NUL;
2644
2645#ifdef FEAT_MBYTE
2646 if (enc_utf8)
2647 {
2648 /* When the inserted text starts with a composing character,
2649 * backup to the character before it. There could be two of them.
2650 */
2651 i = 0;
2652 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2653 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2654 {
2655 i = (*mb_head_off)(ccline.cmdbuff,
2656 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2657 ccline.cmdpos -= i;
2658 len += i;
2659 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2660 }
2661# ifdef FEAT_ARABIC
2662 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2663 {
2664 /* Check the previous character for Arabic combining pair. */
2665 i = (*mb_head_off)(ccline.cmdbuff,
2666 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2667 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2668 + ccline.cmdpos - i), c))
2669 {
2670 ccline.cmdpos -= i;
2671 len += i;
2672 }
2673 else
2674 i = 0;
2675 }
2676# endif
2677 if (i != 0)
2678 {
2679 /* Also backup the cursor position. */
2680 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2681 ccline.cmdspos -= i;
2682 msg_col -= i;
2683 if (msg_col < 0)
2684 {
2685 msg_col += Columns;
2686 --msg_row;
2687 }
2688 }
2689 }
2690#endif
2691
2692 if (redraw && !cmd_silent)
2693 {
2694 msg_no_more = TRUE;
2695 i = cmdline_row;
2696 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2697 /* Avoid clearing the rest of the line too often. */
2698 if (cmdline_row != i || ccline.overstrike)
2699 msg_clr_eos();
2700 msg_no_more = FALSE;
2701 }
2702#ifdef FEAT_FKMAP
2703 /*
2704 * If we are in Farsi command mode, the character input must be in
2705 * Insert mode. So do not advance the cmdpos.
2706 */
2707 if (!cmd_fkmap)
2708#endif
2709 {
2710 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002711 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002713 if (m < 0) /* overflow, Columns or Rows at weird value */
2714 m = MAXCOL;
2715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 else
2717 m = MAXCOL;
2718 for (i = 0; i < len; ++i)
2719 {
2720 c = cmdline_charsize(ccline.cmdpos);
2721#ifdef FEAT_MBYTE
2722 /* count ">" for a double-wide char that doesn't fit. */
2723 if (has_mbyte)
2724 correct_cmdspos(ccline.cmdpos, c);
2725#endif
2726 /* Stop cursor at the end of the screen */
2727 if (ccline.cmdspos + c >= m)
2728 break;
2729 ccline.cmdspos += c;
2730#ifdef FEAT_MBYTE
2731 if (has_mbyte)
2732 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002733 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (c > len - i - 1)
2735 c = len - i - 1;
2736 ccline.cmdpos += c;
2737 i += c;
2738 }
2739#endif
2740 ++ccline.cmdpos;
2741 }
2742 }
2743 }
2744 if (redraw)
2745 msg_check();
2746 return retval;
2747}
2748
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002749static struct cmdline_info prev_ccline;
2750static int prev_ccline_used = FALSE;
2751
2752/*
2753 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2754 * and overwrite it. But get_cmdline_str() may need it, thus make it
2755 * available globally in prev_ccline.
2756 */
2757 static void
2758save_cmdline(ccp)
2759 struct cmdline_info *ccp;
2760{
2761 if (!prev_ccline_used)
2762 {
2763 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2764 prev_ccline_used = TRUE;
2765 }
2766 *ccp = prev_ccline;
2767 prev_ccline = ccline;
2768 ccline.cmdbuff = NULL;
2769 ccline.cmdprompt = NULL;
2770}
2771
2772/*
2773 * Resture ccline after it has been saved with save_cmdline().
2774 */
2775 static void
2776restore_cmdline(ccp)
2777 struct cmdline_info *ccp;
2778{
2779 ccline = prev_ccline;
2780 prev_ccline = *ccp;
2781}
2782
Bram Moolenaar8299df92004-07-10 09:47:34 +00002783/*
2784 * paste a yank register into the command line.
2785 * used by CTRL-R command in command-line mode
2786 * insert_reg() can't be used here, because special characters from the
2787 * register contents will be interpreted as commands.
2788 *
2789 * return FAIL for failure, OK otherwise
2790 */
2791 static int
2792cmdline_paste(regname, literally)
2793 int regname;
2794 int literally; /* Insert text literally instead of "as typed" */
2795{
2796 long i;
2797 char_u *arg;
2798 int allocated;
2799 struct cmdline_info save_ccline;
2800
2801 /* check for valid regname; also accept special characters for CTRL-R in
2802 * the command line */
2803 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2804 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2805 return FAIL;
2806
2807 /* A register containing CTRL-R can cause an endless loop. Allow using
2808 * CTRL-C to break the loop. */
2809 line_breakcheck();
2810 if (got_int)
2811 return FAIL;
2812
2813#ifdef FEAT_CLIPBOARD
2814 regname = may_get_selection(regname);
2815#endif
2816
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002817 /* Need to save and restore ccline. And set cmdline_busy to avoid nasty
2818 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002819 save_cmdline(&save_ccline);
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002820 ++cmdline_busy;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002821 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002822 --cmdline_busy;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002823 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002824
2825 if (i)
2826 {
2827 /* Got the value of a special register in "arg". */
2828 if (arg == NULL)
2829 return FAIL;
2830 cmdline_paste_str(arg, literally);
2831 if (allocated)
2832 vim_free(arg);
2833 return OK;
2834 }
2835
2836 return cmdline_paste_reg(regname, literally);
2837}
2838
2839/*
2840 * Put a string on the command line.
2841 * When "literally" is TRUE, insert literally.
2842 * When "literally" is FALSE, insert as typed, but don't leave the command
2843 * line.
2844 */
2845 void
2846cmdline_paste_str(s, literally)
2847 char_u *s;
2848 int literally;
2849{
2850 int c, cv;
2851
2852 if (literally)
2853 put_on_cmdline(s, -1, TRUE);
2854 else
2855 while (*s != NUL)
2856 {
2857 cv = *s;
2858 if (cv == Ctrl_V && s[1])
2859 ++s;
2860#ifdef FEAT_MBYTE
2861 if (has_mbyte)
2862 {
2863 c = mb_ptr2char(s);
2864 s += mb_char2len(c);
2865 }
2866 else
2867#endif
2868 c = *s++;
2869 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2870#ifdef UNIX
2871 || c == intr_char
2872#endif
2873 || (c == Ctrl_BSL && *s == Ctrl_N))
2874 stuffcharReadbuff(Ctrl_V);
2875 stuffcharReadbuff(c);
2876 }
2877}
2878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879#ifdef FEAT_WILDMENU
2880/*
2881 * Delete characters on the command line, from "from" to the current
2882 * position.
2883 */
2884 static void
2885cmdline_del(from)
2886 int from;
2887{
2888 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2889 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2890 ccline.cmdlen -= ccline.cmdpos - from;
2891 ccline.cmdpos = from;
2892}
2893#endif
2894
2895/*
2896 * this fuction is called when the screen size changes and with incremental
2897 * search
2898 */
2899 void
2900redrawcmdline()
2901{
2902 if (cmd_silent)
2903 return;
2904 need_wait_return = FALSE;
2905 compute_cmdrow();
2906 redrawcmd();
2907 cursorcmd();
2908}
2909
2910 static void
2911redrawcmdprompt()
2912{
2913 int i;
2914
2915 if (cmd_silent)
2916 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002917 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 msg_putchar(ccline.cmdfirstc);
2919 if (ccline.cmdprompt != NULL)
2920 {
2921 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2922 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2923 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002924 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 --ccline.cmdindent;
2926 }
2927 else
2928 for (i = ccline.cmdindent; i > 0; --i)
2929 msg_putchar(' ');
2930}
2931
2932/*
2933 * Redraw what is currently on the command line.
2934 */
2935 void
2936redrawcmd()
2937{
2938 if (cmd_silent)
2939 return;
2940
2941 msg_start();
2942 redrawcmdprompt();
2943
2944 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2945 msg_no_more = TRUE;
2946 draw_cmdline(0, ccline.cmdlen);
2947 msg_clr_eos();
2948 msg_no_more = FALSE;
2949
2950 set_cmdspos_cursor();
2951
2952 /*
2953 * An emsg() before may have set msg_scroll. This is used in normal mode,
2954 * in cmdline mode we can reset them now.
2955 */
2956 msg_scroll = FALSE; /* next message overwrites cmdline */
2957
2958 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2959 * in cmdline mode */
2960 skip_redraw = FALSE;
2961}
2962
2963 void
2964compute_cmdrow()
2965{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002966 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 cmdline_row = Rows - 1;
2968 else
2969 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2970 + W_STATUS_HEIGHT(lastwin);
2971}
2972
2973 static void
2974cursorcmd()
2975{
2976 if (cmd_silent)
2977 return;
2978
2979#ifdef FEAT_RIGHTLEFT
2980 if (cmdmsg_rl)
2981 {
2982 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2983 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2984 if (msg_row <= 0)
2985 msg_row = Rows - 1;
2986 }
2987 else
2988#endif
2989 {
2990 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2991 msg_col = ccline.cmdspos % (int)Columns;
2992 if (msg_row >= Rows)
2993 msg_row = Rows - 1;
2994 }
2995
2996 windgoto(msg_row, msg_col);
2997#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2998 redrawcmd_preedit();
2999#endif
3000#ifdef MCH_CURSOR_SHAPE
3001 mch_update_cursor();
3002#endif
3003}
3004
3005 void
3006gotocmdline(clr)
3007 int clr;
3008{
3009 msg_start();
3010#ifdef FEAT_RIGHTLEFT
3011 if (cmdmsg_rl)
3012 msg_col = Columns - 1;
3013 else
3014#endif
3015 msg_col = 0; /* always start in column 0 */
3016 if (clr) /* clear the bottom line(s) */
3017 msg_clr_eos(); /* will reset clear_cmdline */
3018 windgoto(cmdline_row, 0);
3019}
3020
3021/*
3022 * Check the word in front of the cursor for an abbreviation.
3023 * Called when the non-id character "c" has been entered.
3024 * When an abbreviation is recognized it is removed from the text with
3025 * backspaces and the replacement string is inserted, followed by "c".
3026 */
3027 static int
3028ccheck_abbr(c)
3029 int c;
3030{
3031 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3032 return FALSE;
3033
3034 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3035}
3036
3037/*
3038 * Return FAIL if this is not an appropriate context in which to do
3039 * completion of anything, return OK if it is (even if there are no matches).
3040 * For the caller, this means that the character is just passed through like a
3041 * normal character (instead of being expanded). This allows :s/^I^D etc.
3042 */
3043 static int
3044nextwild(xp, type, options)
3045 expand_T *xp;
3046 int type;
3047 int options; /* extra options for ExpandOne() */
3048{
3049 int i, j;
3050 char_u *p1;
3051 char_u *p2;
3052 int oldlen;
3053 int difflen;
3054 int v;
3055
3056 if (xp->xp_numfiles == -1)
3057 {
3058 set_expand_context(xp);
3059 cmd_showtail = expand_showtail(xp);
3060 }
3061
3062 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3063 {
3064 beep_flush();
3065 return OK; /* Something illegal on command line */
3066 }
3067 if (xp->xp_context == EXPAND_NOTHING)
3068 {
3069 /* Caller can use the character as a normal char instead */
3070 return FAIL;
3071 }
3072
3073 MSG_PUTS("..."); /* show that we are busy */
3074 out_flush();
3075
3076 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3077 oldlen = ccline.cmdpos - i;
3078
3079 if (type == WILD_NEXT || type == WILD_PREV)
3080 {
3081 /*
3082 * Get next/previous match for a previous expanded pattern.
3083 */
3084 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3085 }
3086 else
3087 {
3088 /*
3089 * Translate string into pattern and expand it.
3090 */
3091 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3092 p2 = NULL;
3093 else
3094 {
3095 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3096 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3097 |options, type);
3098 vim_free(p1);
3099 /* longest match: make sure it is not shorter (happens with :help */
3100 if (p2 != NULL && type == WILD_LONGEST)
3101 {
3102 for (j = 0; j < oldlen; ++j)
3103 if (ccline.cmdbuff[i + j] == '*'
3104 || ccline.cmdbuff[i + j] == '?')
3105 break;
3106 if ((int)STRLEN(p2) < j)
3107 {
3108 vim_free(p2);
3109 p2 = NULL;
3110 }
3111 }
3112 }
3113 }
3114
3115 if (p2 != NULL && !got_int)
3116 {
3117 difflen = (int)STRLEN(p2) - oldlen;
3118 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3119 {
3120 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3121 xp->xp_pattern = ccline.cmdbuff + i;
3122 }
3123 else
3124 v = OK;
3125 if (v == OK)
3126 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003127 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3128 &ccline.cmdbuff[ccline.cmdpos],
3129 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3130 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 ccline.cmdlen += difflen;
3132 ccline.cmdpos += difflen;
3133 }
3134 }
3135 vim_free(p2);
3136
3137 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003138 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139
3140 /* When expanding a ":map" command and no matches are found, assume that
3141 * the key is supposed to be inserted literally */
3142 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3143 return FAIL;
3144
3145 if (xp->xp_numfiles <= 0 && p2 == NULL)
3146 beep_flush();
3147 else if (xp->xp_numfiles == 1)
3148 /* free expanded pattern */
3149 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3150
3151 return OK;
3152}
3153
3154/*
3155 * Do wildcard expansion on the string 'str'.
3156 * Chars that should not be expanded must be preceded with a backslash.
3157 * Return a pointer to alloced memory containing the new string.
3158 * Return NULL for failure.
3159 *
3160 * Results are cached in xp->xp_files and xp->xp_numfiles.
3161 *
3162 * mode = WILD_FREE: just free previously expanded matches
3163 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3164 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3165 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3166 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3167 * mode = WILD_ALL: return all matches concatenated
3168 * mode = WILD_LONGEST: return longest matched part
3169 *
3170 * options = WILD_LIST_NOTFOUND: list entries without a match
3171 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3172 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3173 * options = WILD_NO_BEEP: Don't beep for multiple matches
3174 * options = WILD_ADD_SLASH: add a slash after directory names
3175 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3176 * options = WILD_SILENT: don't print warning messages
3177 * options = WILD_ESCAPE: put backslash before special chars
3178 *
3179 * The variables xp->xp_context and xp->xp_backslash must have been set!
3180 */
3181 char_u *
3182ExpandOne(xp, str, orig, options, mode)
3183 expand_T *xp;
3184 char_u *str;
3185 char_u *orig; /* allocated copy of original of expanded string */
3186 int options;
3187 int mode;
3188{
3189 char_u *ss = NULL;
3190 static int findex;
3191 static char_u *orig_save = NULL; /* kept value of orig */
3192 int i;
3193 long_u len;
3194 int non_suf_match; /* number without matching suffix */
3195
3196 /*
3197 * first handle the case of using an old match
3198 */
3199 if (mode == WILD_NEXT || mode == WILD_PREV)
3200 {
3201 if (xp->xp_numfiles > 0)
3202 {
3203 if (mode == WILD_PREV)
3204 {
3205 if (findex == -1)
3206 findex = xp->xp_numfiles;
3207 --findex;
3208 }
3209 else /* mode == WILD_NEXT */
3210 ++findex;
3211
3212 /*
3213 * When wrapping around, return the original string, set findex to
3214 * -1.
3215 */
3216 if (findex < 0)
3217 {
3218 if (orig_save == NULL)
3219 findex = xp->xp_numfiles - 1;
3220 else
3221 findex = -1;
3222 }
3223 if (findex >= xp->xp_numfiles)
3224 {
3225 if (orig_save == NULL)
3226 findex = 0;
3227 else
3228 findex = -1;
3229 }
3230#ifdef FEAT_WILDMENU
3231 if (p_wmnu)
3232 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3233 findex, cmd_showtail);
3234#endif
3235 if (findex == -1)
3236 return vim_strsave(orig_save);
3237 return vim_strsave(xp->xp_files[findex]);
3238 }
3239 else
3240 return NULL;
3241 }
3242
3243/* free old names */
3244 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3245 {
3246 FreeWild(xp->xp_numfiles, xp->xp_files);
3247 xp->xp_numfiles = -1;
3248 vim_free(orig_save);
3249 orig_save = NULL;
3250 }
3251 findex = 0;
3252
3253 if (mode == WILD_FREE) /* only release file name */
3254 return NULL;
3255
3256 if (xp->xp_numfiles == -1)
3257 {
3258 vim_free(orig_save);
3259 orig_save = orig;
3260
3261 /*
3262 * Do the expansion.
3263 */
3264 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3265 options) == FAIL)
3266 {
3267#ifdef FNAME_ILLEGAL
3268 /* Illegal file name has been silently skipped. But when there
3269 * are wildcards, the real problem is that there was no match,
3270 * causing the pattern to be added, which has illegal characters.
3271 */
3272 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3273 EMSG2(_(e_nomatch2), str);
3274#endif
3275 }
3276 else if (xp->xp_numfiles == 0)
3277 {
3278 if (!(options & WILD_SILENT))
3279 EMSG2(_(e_nomatch2), str);
3280 }
3281 else
3282 {
3283 /* Escape the matches for use on the command line. */
3284 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3285
3286 /*
3287 * Check for matching suffixes in file names.
3288 */
3289 if (mode != WILD_ALL && mode != WILD_LONGEST)
3290 {
3291 if (xp->xp_numfiles)
3292 non_suf_match = xp->xp_numfiles;
3293 else
3294 non_suf_match = 1;
3295 if ((xp->xp_context == EXPAND_FILES
3296 || xp->xp_context == EXPAND_DIRECTORIES)
3297 && xp->xp_numfiles > 1)
3298 {
3299 /*
3300 * More than one match; check suffix.
3301 * The files will have been sorted on matching suffix in
3302 * expand_wildcards, only need to check the first two.
3303 */
3304 non_suf_match = 0;
3305 for (i = 0; i < 2; ++i)
3306 if (match_suffix(xp->xp_files[i]))
3307 ++non_suf_match;
3308 }
3309 if (non_suf_match != 1)
3310 {
3311 /* Can we ever get here unless it's while expanding
3312 * interactively? If not, we can get rid of this all
3313 * together. Don't really want to wait for this message
3314 * (and possibly have to hit return to continue!).
3315 */
3316 if (!(options & WILD_SILENT))
3317 EMSG(_(e_toomany));
3318 else if (!(options & WILD_NO_BEEP))
3319 beep_flush();
3320 }
3321 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3322 ss = vim_strsave(xp->xp_files[0]);
3323 }
3324 }
3325 }
3326
3327 /* Find longest common part */
3328 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3329 {
3330 for (len = 0; xp->xp_files[0][len]; ++len)
3331 {
3332 for (i = 0; i < xp->xp_numfiles; ++i)
3333 {
3334#ifdef CASE_INSENSITIVE_FILENAME
3335 if (xp->xp_context == EXPAND_DIRECTORIES
3336 || xp->xp_context == EXPAND_FILES
3337 || xp->xp_context == EXPAND_BUFFERS)
3338 {
3339 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3340 TOLOWER_LOC(xp->xp_files[0][len]))
3341 break;
3342 }
3343 else
3344#endif
3345 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3346 break;
3347 }
3348 if (i < xp->xp_numfiles)
3349 {
3350 if (!(options & WILD_NO_BEEP))
3351 vim_beep();
3352 break;
3353 }
3354 }
3355 ss = alloc((unsigned)len + 1);
3356 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003357 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 findex = -1; /* next p_wc gets first one */
3359 }
3360
3361 /* Concatenate all matching names */
3362 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3363 {
3364 len = 0;
3365 for (i = 0; i < xp->xp_numfiles; ++i)
3366 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3367 ss = lalloc(len, TRUE);
3368 if (ss != NULL)
3369 {
3370 *ss = NUL;
3371 for (i = 0; i < xp->xp_numfiles; ++i)
3372 {
3373 STRCAT(ss, xp->xp_files[i]);
3374 if (i != xp->xp_numfiles - 1)
3375 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3376 }
3377 }
3378 }
3379
3380 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3381 ExpandCleanup(xp);
3382
3383 return ss;
3384}
3385
3386/*
3387 * Prepare an expand structure for use.
3388 */
3389 void
3390ExpandInit(xp)
3391 expand_T *xp;
3392{
3393 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003394#ifndef BACKSLASH_IN_FILENAME
3395 xp->xp_shell = FALSE;
3396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 xp->xp_numfiles = -1;
3398 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003399#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3400 xp->xp_arg = NULL;
3401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402}
3403
3404/*
3405 * Cleanup an expand structure after use.
3406 */
3407 void
3408ExpandCleanup(xp)
3409 expand_T *xp;
3410{
3411 if (xp->xp_numfiles >= 0)
3412 {
3413 FreeWild(xp->xp_numfiles, xp->xp_files);
3414 xp->xp_numfiles = -1;
3415 }
3416}
3417
3418 void
3419ExpandEscape(xp, str, numfiles, files, options)
3420 expand_T *xp;
3421 char_u *str;
3422 int numfiles;
3423 char_u **files;
3424 int options;
3425{
3426 int i;
3427 char_u *p;
3428
3429 /*
3430 * May change home directory back to "~"
3431 */
3432 if (options & WILD_HOME_REPLACE)
3433 tilde_replace(str, numfiles, files);
3434
3435 if (options & WILD_ESCAPE)
3436 {
3437 if (xp->xp_context == EXPAND_FILES
3438 || xp->xp_context == EXPAND_BUFFERS
3439 || xp->xp_context == EXPAND_DIRECTORIES)
3440 {
3441 /*
3442 * Insert a backslash into a file name before a space, \, %, #
3443 * and wildmatch characters, except '~'.
3444 */
3445 for (i = 0; i < numfiles; ++i)
3446 {
3447 /* for ":set path=" we need to escape spaces twice */
3448 if (xp->xp_backslash == XP_BS_THREE)
3449 {
3450 p = vim_strsave_escaped(files[i], (char_u *)" ");
3451 if (p != NULL)
3452 {
3453 vim_free(files[i]);
3454 files[i] = p;
3455#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3456 p = vim_strsave_escaped(files[i], (char_u *)" ");
3457 if (p != NULL)
3458 {
3459 vim_free(files[i]);
3460 files[i] = p;
3461 }
3462#endif
3463 }
3464 }
3465#ifdef BACKSLASH_IN_FILENAME
3466 {
3467 char_u buf[20];
3468 int j = 0;
3469
3470 /* Don't escape '[' and '{' if they are in 'isfname'. */
3471 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3472 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3473 buf[j++] = *p;
3474 buf[j] = NUL;
3475 p = vim_strsave_escaped(files[i], buf);
3476 }
3477#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003478 p = vim_strsave_escaped(files[i],
3479 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480#endif
3481 if (p != NULL)
3482 {
3483 vim_free(files[i]);
3484 files[i] = p;
3485 }
3486
3487 /* If 'str' starts with "\~", replace "~" at start of
3488 * files[i] with "\~". */
3489 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003490 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 }
3492 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003493
3494 /* If the first file starts with a '+' escape it. Otherwise it
3495 * could be seen as "+cmd". */
3496 if (*files[0] == '+')
3497 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499 else if (xp->xp_context == EXPAND_TAGS)
3500 {
3501 /*
3502 * Insert a backslash before characters in a tag name that
3503 * would terminate the ":tag" command.
3504 */
3505 for (i = 0; i < numfiles; ++i)
3506 {
3507 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3508 if (p != NULL)
3509 {
3510 vim_free(files[i]);
3511 files[i] = p;
3512 }
3513 }
3514 }
3515 }
3516}
3517
3518/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003519 * Put a backslash before the file name in "pp", which is in allocated memory.
3520 */
3521 static void
3522escape_fname(pp)
3523 char_u **pp;
3524{
3525 char_u *p;
3526
3527 p = alloc((unsigned)(STRLEN(*pp) + 2));
3528 if (p != NULL)
3529 {
3530 p[0] = '\\';
3531 STRCPY(p + 1, *pp);
3532 vim_free(*pp);
3533 *pp = p;
3534 }
3535}
3536
3537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 * For each file name in files[num_files]:
3539 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3540 */
3541 void
3542tilde_replace(orig_pat, num_files, files)
3543 char_u *orig_pat;
3544 int num_files;
3545 char_u **files;
3546{
3547 int i;
3548 char_u *p;
3549
3550 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3551 {
3552 for (i = 0; i < num_files; ++i)
3553 {
3554 p = home_replace_save(NULL, files[i]);
3555 if (p != NULL)
3556 {
3557 vim_free(files[i]);
3558 files[i] = p;
3559 }
3560 }
3561 }
3562}
3563
3564/*
3565 * Show all matches for completion on the command line.
3566 * Returns EXPAND_NOTHING when the character that triggered expansion should
3567 * be inserted like a normal character.
3568 */
3569/*ARGSUSED*/
3570 static int
3571showmatches(xp, wildmenu)
3572 expand_T *xp;
3573 int wildmenu;
3574{
3575#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3576 int num_files;
3577 char_u **files_found;
3578 int i, j, k;
3579 int maxlen;
3580 int lines;
3581 int columns;
3582 char_u *p;
3583 int lastlen;
3584 int attr;
3585 int showtail;
3586
3587 if (xp->xp_numfiles == -1)
3588 {
3589 set_expand_context(xp);
3590 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3591 &num_files, &files_found);
3592 showtail = expand_showtail(xp);
3593 if (i != EXPAND_OK)
3594 return i;
3595
3596 }
3597 else
3598 {
3599 num_files = xp->xp_numfiles;
3600 files_found = xp->xp_files;
3601 showtail = cmd_showtail;
3602 }
3603
3604#ifdef FEAT_WILDMENU
3605 if (!wildmenu)
3606 {
3607#endif
3608 msg_didany = FALSE; /* lines_left will be set */
3609 msg_start(); /* prepare for paging */
3610 msg_putchar('\n');
3611 out_flush();
3612 cmdline_row = msg_row;
3613 msg_didany = FALSE; /* lines_left will be set again */
3614 msg_start(); /* prepare for paging */
3615#ifdef FEAT_WILDMENU
3616 }
3617#endif
3618
3619 if (got_int)
3620 got_int = FALSE; /* only int. the completion, not the cmd line */
3621#ifdef FEAT_WILDMENU
3622 else if (wildmenu)
3623 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3624#endif
3625 else
3626 {
3627 /* find the length of the longest file name */
3628 maxlen = 0;
3629 for (i = 0; i < num_files; ++i)
3630 {
3631 if (!showtail && (xp->xp_context == EXPAND_FILES
3632 || xp->xp_context == EXPAND_BUFFERS))
3633 {
3634 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3635 j = vim_strsize(NameBuff);
3636 }
3637 else
3638 j = vim_strsize(L_SHOWFILE(i));
3639 if (j > maxlen)
3640 maxlen = j;
3641 }
3642
3643 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3644 lines = num_files;
3645 else
3646 {
3647 /* compute the number of columns and lines for the listing */
3648 maxlen += 2; /* two spaces between file names */
3649 columns = ((int)Columns + 2) / maxlen;
3650 if (columns < 1)
3651 columns = 1;
3652 lines = (num_files + columns - 1) / columns;
3653 }
3654
3655 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3656
3657 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3658 {
3659 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3660 msg_clr_eos();
3661 msg_advance(maxlen - 3);
3662 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3663 }
3664
3665 /* list the files line by line */
3666 for (i = 0; i < lines; ++i)
3667 {
3668 lastlen = 999;
3669 for (k = i; k < num_files; k += lines)
3670 {
3671 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3672 {
3673 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3674 p = files_found[k] + STRLEN(files_found[k]) + 1;
3675 msg_advance(maxlen + 1);
3676 msg_puts(p);
3677 msg_advance(maxlen + 3);
3678 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3679 break;
3680 }
3681 for (j = maxlen - lastlen; --j >= 0; )
3682 msg_putchar(' ');
3683 if (xp->xp_context == EXPAND_FILES
3684 || xp->xp_context == EXPAND_BUFFERS)
3685 {
3686 /* highlight directories */
3687 j = (mch_isdir(files_found[k]));
3688 if (showtail)
3689 p = L_SHOWFILE(k);
3690 else
3691 {
3692 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3693 TRUE);
3694 p = NameBuff;
3695 }
3696 }
3697 else
3698 {
3699 j = FALSE;
3700 p = L_SHOWFILE(k);
3701 }
3702 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3703 }
3704 if (msg_col > 0) /* when not wrapped around */
3705 {
3706 msg_clr_eos();
3707 msg_putchar('\n');
3708 }
3709 out_flush(); /* show one line at a time */
3710 if (got_int)
3711 {
3712 got_int = FALSE;
3713 break;
3714 }
3715 }
3716
3717 /*
3718 * we redraw the command below the lines that we have just listed
3719 * This is a bit tricky, but it saves a lot of screen updating.
3720 */
3721 cmdline_row = msg_row; /* will put it back later */
3722 }
3723
3724 if (xp->xp_numfiles == -1)
3725 FreeWild(num_files, files_found);
3726
3727 return EXPAND_OK;
3728}
3729
3730/*
3731 * Private gettail for showmatches() (and win_redr_status_matches()):
3732 * Find tail of file name path, but ignore trailing "/".
3733 */
3734 char_u *
3735sm_gettail(s)
3736 char_u *s;
3737{
3738 char_u *p;
3739 char_u *t = s;
3740 int had_sep = FALSE;
3741
3742 for (p = s; *p != NUL; )
3743 {
3744 if (vim_ispathsep(*p)
3745#ifdef BACKSLASH_IN_FILENAME
3746 && !rem_backslash(p)
3747#endif
3748 )
3749 had_sep = TRUE;
3750 else if (had_sep)
3751 {
3752 t = p;
3753 had_sep = FALSE;
3754 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003755 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 return t;
3758}
3759
3760/*
3761 * Return TRUE if we only need to show the tail of completion matches.
3762 * When not completing file names or there is a wildcard in the path FALSE is
3763 * returned.
3764 */
3765 static int
3766expand_showtail(xp)
3767 expand_T *xp;
3768{
3769 char_u *s;
3770 char_u *end;
3771
3772 /* When not completing file names a "/" may mean something different. */
3773 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3774 return FALSE;
3775
3776 end = gettail(xp->xp_pattern);
3777 if (end == xp->xp_pattern) /* there is no path separator */
3778 return FALSE;
3779
3780 for (s = xp->xp_pattern; s < end; s++)
3781 {
3782 /* Skip escaped wildcards. Only when the backslash is not a path
3783 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3784 if (rem_backslash(s))
3785 ++s;
3786 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3787 return FALSE;
3788 }
3789 return TRUE;
3790}
3791
3792/*
3793 * Prepare a string for expansion.
3794 * When expanding file names: The string will be used with expand_wildcards().
3795 * Copy the file name into allocated memory and add a '*' at the end.
3796 * When expanding other names: The string will be used with regcomp(). Copy
3797 * the name into allocated memory and prepend "^".
3798 */
3799 char_u *
3800addstar(fname, len, context)
3801 char_u *fname;
3802 int len;
3803 int context; /* EXPAND_FILES etc. */
3804{
3805 char_u *retval;
3806 int i, j;
3807 int new_len;
3808 char_u *tail;
3809
3810 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3811 {
3812 /*
3813 * Matching will be done internally (on something other than files).
3814 * So we convert the file-matching-type wildcards into our kind for
3815 * use with vim_regcomp(). First work out how long it will be:
3816 */
3817
3818 /* For help tags the translation is done in find_help_tags().
3819 * For a tag pattern starting with "/" no translation is needed. */
3820 if (context == EXPAND_HELP
3821 || context == EXPAND_COLORS
3822 || context == EXPAND_COMPILER
3823 || (context == EXPAND_TAGS && fname[0] == '/'))
3824 retval = vim_strnsave(fname, len);
3825 else
3826 {
3827 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3828 for (i = 0; i < len; i++)
3829 {
3830 if (fname[i] == '*' || fname[i] == '~')
3831 new_len++; /* '*' needs to be replaced by ".*"
3832 '~' needs to be replaced by "\~" */
3833
3834 /* Buffer names are like file names. "." should be literal */
3835 if (context == EXPAND_BUFFERS && fname[i] == '.')
3836 new_len++; /* "." becomes "\." */
3837
3838 /* Custom expansion takes care of special things, match
3839 * backslashes literally (perhaps also for other types?) */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003840 if ((context == EXPAND_USER_DEFINED ||
3841 context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 new_len++; /* '\' becomes "\\" */
3843 }
3844 retval = alloc(new_len);
3845 if (retval != NULL)
3846 {
3847 retval[0] = '^';
3848 j = 1;
3849 for (i = 0; i < len; i++, j++)
3850 {
3851 /* Skip backslash. But why? At least keep it for custom
3852 * expansion. */
3853 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003854 && context != EXPAND_USER_LIST
3855 && fname[i] == '\\'
3856 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 break;
3858
3859 switch (fname[i])
3860 {
3861 case '*': retval[j++] = '.';
3862 break;
3863 case '~': retval[j++] = '\\';
3864 break;
3865 case '?': retval[j] = '.';
3866 continue;
3867 case '.': if (context == EXPAND_BUFFERS)
3868 retval[j++] = '\\';
3869 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003870 case '\\': if (context == EXPAND_USER_DEFINED
3871 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 retval[j++] = '\\';
3873 break;
3874 }
3875 retval[j] = fname[i];
3876 }
3877 retval[j] = NUL;
3878 }
3879 }
3880 }
3881 else
3882 {
3883 retval = alloc(len + 4);
3884 if (retval != NULL)
3885 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003886 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 /*
3889 * Don't add a star to ~, ~user, $var or `cmd`.
3890 * ~ would be at the start of the file name, but not the tail.
3891 * $ could be anywhere in the tail.
3892 * ` could be anywhere in the file name.
3893 */
3894 tail = gettail(retval);
3895 if ((*retval != '~' || tail != retval)
3896 && vim_strchr(tail, '$') == NULL
3897 && vim_strchr(retval, '`') == NULL)
3898 retval[len++] = '*';
3899 retval[len] = NUL;
3900 }
3901 }
3902 return retval;
3903}
3904
3905/*
3906 * Must parse the command line so far to work out what context we are in.
3907 * Completion can then be done based on that context.
3908 * This routine sets the variables:
3909 * xp->xp_pattern The start of the pattern to be expanded within
3910 * the command line (ends at the cursor).
3911 * xp->xp_context The type of thing to expand. Will be one of:
3912 *
3913 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3914 * the command line, like an unknown command. Caller
3915 * should beep.
3916 * EXPAND_NOTHING Unrecognised context for completion, use char like
3917 * a normal char, rather than for completion. eg
3918 * :s/^I/
3919 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3920 * it.
3921 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3922 * EXPAND_FILES After command with XFILE set, or after setting
3923 * with P_EXPAND set. eg :e ^I, :w>>^I
3924 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3925 * when we know only directories are of interest. eg
3926 * :set dir=^I
3927 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3928 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3929 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3930 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3931 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3932 * EXPAND_EVENTS Complete event names
3933 * EXPAND_SYNTAX Complete :syntax command arguments
3934 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3935 * EXPAND_AUGROUP Complete autocommand group names
3936 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3937 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3938 * eg :unmap a^I , :cunab x^I
3939 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3940 * eg :call sub^I
3941 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3942 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3943 * names in expressions, eg :while s^I
3944 * EXPAND_ENV_VARS Complete environment variable names
3945 */
3946 static void
3947set_expand_context(xp)
3948 expand_T *xp;
3949{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003950 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 if (ccline.cmdfirstc != ':'
3952#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003953 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003954 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955#endif
3956 )
3957 {
3958 xp->xp_context = EXPAND_NOTHING;
3959 return;
3960 }
3961 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3962}
3963
3964 void
3965set_cmd_context(xp, str, len, col)
3966 expand_T *xp;
3967 char_u *str; /* start of command line */
3968 int len; /* length of command line (excl. NUL) */
3969 int col; /* position of cursor */
3970{
3971 int old_char = NUL;
3972 char_u *nextcomm;
3973
3974 /*
3975 * Avoid a UMR warning from Purify, only save the character if it has been
3976 * written before.
3977 */
3978 if (col < len)
3979 old_char = str[col];
3980 str[col] = NUL;
3981 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003982
3983#ifdef FEAT_EVAL
3984 if (ccline.cmdfirstc == '=')
3985 /* pass CMD_SIZE because there is no real command */
3986 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003987 else if (ccline.input_fn)
3988 {
3989 xp->xp_context = ccline.xp_context;
3990 xp->xp_pattern = ccline.cmdbuff;
3991 xp->xp_arg = ccline.xp_arg;
3992 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003993 else
3994#endif
3995 while (nextcomm != NULL)
3996 nextcomm = set_one_cmd_context(xp, nextcomm);
3997
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 str[col] = old_char;
3999}
4000
4001/*
4002 * Expand the command line "str" from context "xp".
4003 * "xp" must have been set by set_cmd_context().
4004 * xp->xp_pattern points into "str", to where the text that is to be expanded
4005 * starts.
4006 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4007 * cursor.
4008 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4009 * key that triggered expansion literally.
4010 * Returns EXPAND_OK otherwise.
4011 */
4012 int
4013expand_cmdline(xp, str, col, matchcount, matches)
4014 expand_T *xp;
4015 char_u *str; /* start of command line */
4016 int col; /* position of cursor */
4017 int *matchcount; /* return: nr of matches */
4018 char_u ***matches; /* return: array of pointers to matches */
4019{
4020 char_u *file_str = NULL;
4021
4022 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4023 {
4024 beep_flush();
4025 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4026 }
4027 if (xp->xp_context == EXPAND_NOTHING)
4028 {
4029 /* Caller can use the character as a normal char instead */
4030 return EXPAND_NOTHING;
4031 }
4032
4033 /* add star to file name, or convert to regexp if not exp. files. */
4034 file_str = addstar(xp->xp_pattern,
4035 (int)(str + col - xp->xp_pattern), xp->xp_context);
4036 if (file_str == NULL)
4037 return EXPAND_UNSUCCESSFUL;
4038
4039 /* find all files that match the description */
4040 if (ExpandFromContext(xp, file_str, matchcount, matches,
4041 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4042 {
4043 *matchcount = 0;
4044 *matches = NULL;
4045 }
4046 vim_free(file_str);
4047
4048 return EXPAND_OK;
4049}
4050
4051#ifdef FEAT_MULTI_LANG
4052/*
4053 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4054 */
4055static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4056
4057 static void
4058cleanup_help_tags(num_file, file)
4059 int num_file;
4060 char_u **file;
4061{
4062 int i, j;
4063 int len;
4064
4065 for (i = 0; i < num_file; ++i)
4066 {
4067 len = (int)STRLEN(file[i]) - 3;
4068 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4069 {
4070 /* Sorting on priority means the same item in another language may
4071 * be anywhere. Search all items for a match up to the "@en". */
4072 for (j = 0; j < num_file; ++j)
4073 if (j != i
4074 && (int)STRLEN(file[j]) == len + 3
4075 && STRNCMP(file[i], file[j], len + 1) == 0)
4076 break;
4077 if (j == num_file)
4078 file[i][len] = NUL;
4079 }
4080 }
4081}
4082#endif
4083
4084/*
4085 * Do the expansion based on xp->xp_context and "pat".
4086 */
4087 static int
4088ExpandFromContext(xp, pat, num_file, file, options)
4089 expand_T *xp;
4090 char_u *pat;
4091 int *num_file;
4092 char_u ***file;
4093 int options;
4094{
4095#ifdef FEAT_CMDL_COMPL
4096 regmatch_T regmatch;
4097#endif
4098 int ret;
4099 int flags;
4100
4101 flags = EW_DIR; /* include directories */
4102 if (options & WILD_LIST_NOTFOUND)
4103 flags |= EW_NOTFOUND;
4104 if (options & WILD_ADD_SLASH)
4105 flags |= EW_ADDSLASH;
4106 if (options & WILD_KEEP_ALL)
4107 flags |= EW_KEEPALL;
4108 if (options & WILD_SILENT)
4109 flags |= EW_SILENT;
4110
4111 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4112 {
4113 /*
4114 * Expand file or directory names.
4115 */
4116 int free_pat = FALSE;
4117 int i;
4118
4119 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4120 * space */
4121 if (xp->xp_backslash != XP_BS_NONE)
4122 {
4123 free_pat = TRUE;
4124 pat = vim_strsave(pat);
4125 for (i = 0; pat[i]; ++i)
4126 if (pat[i] == '\\')
4127 {
4128 if (xp->xp_backslash == XP_BS_THREE
4129 && pat[i + 1] == '\\'
4130 && pat[i + 2] == '\\'
4131 && pat[i + 3] == ' ')
4132 STRCPY(pat + i, pat + i + 3);
4133 if (xp->xp_backslash == XP_BS_ONE
4134 && pat[i + 1] == ' ')
4135 STRCPY(pat + i, pat + i + 1);
4136 }
4137 }
4138
4139 if (xp->xp_context == EXPAND_FILES)
4140 flags |= EW_FILE;
4141 else
4142 flags = (flags | EW_DIR) & ~EW_FILE;
4143 ret = expand_wildcards(1, &pat, num_file, file, flags);
4144 if (free_pat)
4145 vim_free(pat);
4146 return ret;
4147 }
4148
4149 *file = (char_u **)"";
4150 *num_file = 0;
4151 if (xp->xp_context == EXPAND_HELP)
4152 {
4153 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4154 {
4155#ifdef FEAT_MULTI_LANG
4156 cleanup_help_tags(*num_file, *file);
4157#endif
4158 return OK;
4159 }
4160 return FAIL;
4161 }
4162
4163#ifndef FEAT_CMDL_COMPL
4164 return FAIL;
4165#else
4166 if (xp->xp_context == EXPAND_OLD_SETTING)
4167 return ExpandOldSetting(num_file, file);
4168 if (xp->xp_context == EXPAND_BUFFERS)
4169 return ExpandBufnames(pat, num_file, file, options);
4170 if (xp->xp_context == EXPAND_TAGS
4171 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4172 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4173 if (xp->xp_context == EXPAND_COLORS)
4174 return ExpandRTDir(pat, num_file, file, "colors");
4175 if (xp->xp_context == EXPAND_COMPILER)
4176 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004177# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4178 if (xp->xp_context == EXPAND_USER_LIST)
4179 return ExpandUserList(xp, num_file, file);
4180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181
4182 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4183 if (regmatch.regprog == NULL)
4184 return FAIL;
4185
4186 /* set ignore-case according to p_ic, p_scs and pat */
4187 regmatch.rm_ic = ignorecase(pat);
4188
4189 if (xp->xp_context == EXPAND_SETTINGS
4190 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4191 ret = ExpandSettings(xp, &regmatch, num_file, file);
4192 else if (xp->xp_context == EXPAND_MAPPINGS)
4193 ret = ExpandMappings(&regmatch, num_file, file);
4194# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4195 else if (xp->xp_context == EXPAND_USER_DEFINED)
4196 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4197# endif
4198 else
4199 {
4200 static struct expgen
4201 {
4202 int context;
4203 char_u *((*func)__ARGS((expand_T *, int)));
4204 int ic;
4205 } tab[] =
4206 {
4207 {EXPAND_COMMANDS, get_command_name, FALSE},
4208#ifdef FEAT_USR_CMDS
4209 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4210 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4211 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4212 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4213#endif
4214#ifdef FEAT_EVAL
4215 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4216 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4217 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4218 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4219#endif
4220#ifdef FEAT_MENU
4221 {EXPAND_MENUS, get_menu_name, FALSE},
4222 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4223#endif
4224#ifdef FEAT_SYN_HL
4225 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4226#endif
4227 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4228#ifdef FEAT_AUTOCMD
4229 {EXPAND_EVENTS, get_event_name, TRUE},
4230 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4231#endif
4232#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4233 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4234 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4235#endif
4236 {EXPAND_ENV_VARS, get_env_name, TRUE},
4237 };
4238 int i;
4239
4240 /*
4241 * Find a context in the table and call the ExpandGeneric() with the
4242 * right function to do the expansion.
4243 */
4244 ret = FAIL;
4245 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4246 if (xp->xp_context == tab[i].context)
4247 {
4248 if (tab[i].ic)
4249 regmatch.rm_ic = TRUE;
4250 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4251 break;
4252 }
4253 }
4254
4255 vim_free(regmatch.regprog);
4256
4257 return ret;
4258#endif /* FEAT_CMDL_COMPL */
4259}
4260
4261#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4262/*
4263 * Expand a list of names.
4264 *
4265 * Generic function for command line completion. It calls a function to
4266 * obtain strings, one by one. The strings are matched against a regexp
4267 * program. Matching strings are copied into an array, which is returned.
4268 *
4269 * Returns OK when no problems encountered, FAIL for error (out of memory).
4270 */
4271 int
4272ExpandGeneric(xp, regmatch, num_file, file, func)
4273 expand_T *xp;
4274 regmatch_T *regmatch;
4275 int *num_file;
4276 char_u ***file;
4277 char_u *((*func)__ARGS((expand_T *, int)));
4278 /* returns a string from the list */
4279{
4280 int i;
4281 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004282 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 char_u *str;
4284
4285 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004286 * round == 0: count the number of matching names
4287 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004289 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 {
4291 for (i = 0; ; ++i)
4292 {
4293 str = (*func)(xp, i);
4294 if (str == NULL) /* end of list */
4295 break;
4296 if (*str == NUL) /* skip empty strings */
4297 continue;
4298
4299 if (vim_regexec(regmatch, str, (colnr_T)0))
4300 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004301 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 {
4303 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4304 (*file)[count] = str;
4305#ifdef FEAT_MENU
4306 if (func == get_menu_names && str != NULL)
4307 {
4308 /* test for separator added by get_menu_names() */
4309 str += STRLEN(str) - 1;
4310 if (*str == '\001')
4311 *str = '.';
4312 }
4313#endif
4314 }
4315 ++count;
4316 }
4317 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004318 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 if (count == 0)
4321 return OK;
4322 *num_file = count;
4323 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4324 if (*file == NULL)
4325 {
4326 *file = (char_u **)"";
4327 return FAIL;
4328 }
4329 count = 0;
4330 }
4331 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004332
4333 /* Sort the results. */
4334 sort_strings(*file, *num_file);
4335
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 return OK;
4337}
4338
4339# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004340static 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));
4341
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004343 * call "user_expand_func()" to invoke a user defined VimL function and return
4344 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004346 static void *
4347call_user_expand_func(user_expand_func, xp, num_file, file)
4348 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 int *num_file;
4351 char_u ***file;
4352{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004353 char_u keep;
4354 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004357 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004358 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359
4360 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004361 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 *num_file = 0;
4363 *file = NULL;
4364
4365 keep = ccline.cmdbuff[ccline.cmdlen];
4366 ccline.cmdbuff[ccline.cmdlen] = 0;
4367 sprintf((char *)num, "%d", ccline.cmdpos);
4368 args[0] = xp->xp_pattern;
4369 args[1] = ccline.cmdbuff;
4370 args[2] = num;
4371
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004372 /* Save the cmdline, we don't know what the function may do. */
4373 save_ccline = ccline;
4374 ccline.cmdbuff = NULL;
4375 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004377
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004378 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004379
4380 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004384
4385 return ret;
4386}
4387
4388/*
4389 * Expand names with a function defined by the user.
4390 */
4391 static int
4392ExpandUserDefined(xp, regmatch, num_file, file)
4393 expand_T *xp;
4394 regmatch_T *regmatch;
4395 int *num_file;
4396 char_u ***file;
4397{
4398 char_u *retstr;
4399 char_u *s;
4400 char_u *e;
4401 char_u keep;
4402 garray_T ga;
4403
4404 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4405 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 return FAIL;
4407
4408 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004409 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 {
4411 e = vim_strchr(s, '\n');
4412 if (e == NULL)
4413 e = s + STRLEN(s);
4414 keep = *e;
4415 *e = 0;
4416
4417 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4418 {
4419 *e = keep;
4420 if (*e != NUL)
4421 ++e;
4422 continue;
4423 }
4424
4425 if (ga_grow(&ga, 1) == FAIL)
4426 break;
4427
4428 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4429 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430
4431 *e = keep;
4432 if (*e != NUL)
4433 ++e;
4434 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004435 vim_free(retstr);
4436 *file = ga.ga_data;
4437 *num_file = ga.ga_len;
4438 return OK;
4439}
4440
4441/*
4442 * Expand names with a list returned by a function defined by the user.
4443 */
4444 static int
4445ExpandUserList(xp, num_file, file)
4446 expand_T *xp;
4447 int *num_file;
4448 char_u ***file;
4449{
4450 list_T *retlist;
4451 listitem_T *li;
4452 garray_T ga;
4453
4454 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4455 if (retlist == NULL)
4456 return FAIL;
4457
4458 ga_init2(&ga, (int)sizeof(char *), 3);
4459 /* Loop over the items in the list. */
4460 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4461 {
4462 if (li->li_tv.v_type != VAR_STRING)
4463 continue; /* Skip non-string items */
4464
4465 if (ga_grow(&ga, 1) == FAIL)
4466 break;
4467
4468 ((char_u **)ga.ga_data)[ga.ga_len] =
4469 vim_strsave(li->li_tv.vval.v_string);
4470 ++ga.ga_len;
4471 }
4472 list_unref(retlist);
4473
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 *file = ga.ga_data;
4475 *num_file = ga.ga_len;
4476 return OK;
4477}
4478#endif
4479
4480/*
4481 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4482 * or compiler names.
4483 */
4484 static int
4485ExpandRTDir(pat, num_file, file, dirname)
4486 char_u *pat;
4487 int *num_file;
4488 char_u ***file;
4489 char *dirname; /* "colors" or "compiler" */
4490{
4491 char_u *all;
4492 char_u *s;
4493 char_u *e;
4494 garray_T ga;
4495
4496 *num_file = 0;
4497 *file = NULL;
4498 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4499 if (s == NULL)
4500 return FAIL;
4501 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4502 all = globpath(p_rtp, s);
4503 vim_free(s);
4504 if (all == NULL)
4505 return FAIL;
4506
4507 ga_init2(&ga, (int)sizeof(char *), 3);
4508 for (s = all; *s != NUL; s = e)
4509 {
4510 e = vim_strchr(s, '\n');
4511 if (e == NULL)
4512 e = s + STRLEN(s);
4513 if (ga_grow(&ga, 1) == FAIL)
4514 break;
4515 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4516 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004517 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 if (*s == '\n' || vim_ispathsep(*s))
4519 break;
4520 ++s;
4521 ((char_u **)ga.ga_data)[ga.ga_len] =
4522 vim_strnsave(s, (int)(e - s - 4));
4523 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525 if (*e != NUL)
4526 ++e;
4527 }
4528 vim_free(all);
4529 *file = ga.ga_data;
4530 *num_file = ga.ga_len;
4531 return OK;
4532}
4533
4534#endif
4535
4536#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4537/*
4538 * Expand "file" for all comma-separated directories in "path".
4539 * Returns an allocated string with all matches concatenated, separated by
4540 * newlines. Returns NULL for an error or no matches.
4541 */
4542 char_u *
4543globpath(path, file)
4544 char_u *path;
4545 char_u *file;
4546{
4547 expand_T xpc;
4548 char_u *buf;
4549 garray_T ga;
4550 int i;
4551 int len;
4552 int num_p;
4553 char_u **p;
4554 char_u *cur = NULL;
4555
4556 buf = alloc(MAXPATHL);
4557 if (buf == NULL)
4558 return NULL;
4559
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004560 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004562
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 ga_init2(&ga, 1, 100);
4564
4565 /* Loop over all entries in {path}. */
4566 while (*path != NUL)
4567 {
4568 /* Copy one item of the path to buf[] and concatenate the file name. */
4569 copy_option_part(&path, buf, MAXPATHL, ",");
4570 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4571 {
4572 add_pathsep(buf);
4573 STRCAT(buf, file);
4574 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4575 && num_p > 0)
4576 {
4577 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4578 for (len = 0, i = 0; i < num_p; ++i)
4579 len += (long_u)STRLEN(p[i]) + 1;
4580
4581 /* Concatenate new results to previous ones. */
4582 if (ga_grow(&ga, len) == OK)
4583 {
4584 cur = (char_u *)ga.ga_data + ga.ga_len;
4585 for (i = 0; i < num_p; ++i)
4586 {
4587 STRCPY(cur, p[i]);
4588 cur += STRLEN(p[i]);
4589 *cur++ = '\n';
4590 }
4591 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
4593 FreeWild(num_p, p);
4594 }
4595 }
4596 }
4597 if (cur != NULL)
4598 *--cur = 0; /* Replace trailing newline with NUL */
4599
4600 vim_free(buf);
4601 return (char_u *)ga.ga_data;
4602}
4603
4604#endif
4605
4606#if defined(FEAT_CMDHIST) || defined(PROTO)
4607
4608/*********************************
4609 * Command line history stuff *
4610 *********************************/
4611
4612/*
4613 * Translate a history character to the associated type number.
4614 */
4615 static int
4616hist_char2type(c)
4617 int c;
4618{
4619 if (c == ':')
4620 return HIST_CMD;
4621 if (c == '=')
4622 return HIST_EXPR;
4623 if (c == '@')
4624 return HIST_INPUT;
4625 if (c == '>')
4626 return HIST_DEBUG;
4627 return HIST_SEARCH; /* must be '?' or '/' */
4628}
4629
4630/*
4631 * Table of history names.
4632 * These names are used in :history and various hist...() functions.
4633 * It is sufficient to give the significant prefix of a history name.
4634 */
4635
4636static char *(history_names[]) =
4637{
4638 "cmd",
4639 "search",
4640 "expr",
4641 "input",
4642 "debug",
4643 NULL
4644};
4645
4646/*
4647 * init_history() - Initialize the command line history.
4648 * Also used to re-allocate the history when the size changes.
4649 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004650 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651init_history()
4652{
4653 int newlen; /* new length of history table */
4654 histentry_T *temp;
4655 int i;
4656 int j;
4657 int type;
4658
4659 /*
4660 * If size of history table changed, reallocate it
4661 */
4662 newlen = (int)p_hi;
4663 if (newlen != hislen) /* history length changed */
4664 {
4665 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4666 {
4667 if (newlen)
4668 {
4669 temp = (histentry_T *)lalloc(
4670 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4671 if (temp == NULL) /* out of memory! */
4672 {
4673 if (type == 0) /* first one: just keep the old length */
4674 {
4675 newlen = hislen;
4676 break;
4677 }
4678 /* Already changed one table, now we can only have zero
4679 * length for all tables. */
4680 newlen = 0;
4681 type = -1;
4682 continue;
4683 }
4684 }
4685 else
4686 temp = NULL;
4687 if (newlen == 0 || temp != NULL)
4688 {
4689 if (hisidx[type] < 0) /* there are no entries yet */
4690 {
4691 for (i = 0; i < newlen; ++i)
4692 {
4693 temp[i].hisnum = 0;
4694 temp[i].hisstr = NULL;
4695 }
4696 }
4697 else if (newlen > hislen) /* array becomes bigger */
4698 {
4699 for (i = 0; i <= hisidx[type]; ++i)
4700 temp[i] = history[type][i];
4701 j = i;
4702 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4703 {
4704 temp[i].hisnum = 0;
4705 temp[i].hisstr = NULL;
4706 }
4707 for ( ; j < hislen; ++i, ++j)
4708 temp[i] = history[type][j];
4709 }
4710 else /* array becomes smaller or 0 */
4711 {
4712 j = hisidx[type];
4713 for (i = newlen - 1; ; --i)
4714 {
4715 if (i >= 0) /* copy newest entries */
4716 temp[i] = history[type][j];
4717 else /* remove older entries */
4718 vim_free(history[type][j].hisstr);
4719 if (--j < 0)
4720 j = hislen - 1;
4721 if (j == hisidx[type])
4722 break;
4723 }
4724 hisidx[type] = newlen - 1;
4725 }
4726 vim_free(history[type]);
4727 history[type] = temp;
4728 }
4729 }
4730 hislen = newlen;
4731 }
4732}
4733
4734/*
4735 * Check if command line 'str' is already in history.
4736 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4737 */
4738 static int
4739in_history(type, str, move_to_front)
4740 int type;
4741 char_u *str;
4742 int move_to_front; /* Move the entry to the front if it exists */
4743{
4744 int i;
4745 int last_i = -1;
4746
4747 if (hisidx[type] < 0)
4748 return FALSE;
4749 i = hisidx[type];
4750 do
4751 {
4752 if (history[type][i].hisstr == NULL)
4753 return FALSE;
4754 if (STRCMP(str, history[type][i].hisstr) == 0)
4755 {
4756 if (!move_to_front)
4757 return TRUE;
4758 last_i = i;
4759 break;
4760 }
4761 if (--i < 0)
4762 i = hislen - 1;
4763 } while (i != hisidx[type]);
4764
4765 if (last_i >= 0)
4766 {
4767 str = history[type][i].hisstr;
4768 while (i != hisidx[type])
4769 {
4770 if (++i >= hislen)
4771 i = 0;
4772 history[type][last_i] = history[type][i];
4773 last_i = i;
4774 }
4775 history[type][i].hisstr = str;
4776 history[type][i].hisnum = ++hisnum[type];
4777 return TRUE;
4778 }
4779 return FALSE;
4780}
4781
4782/*
4783 * Convert history name (from table above) to its HIST_ equivalent.
4784 * When "name" is empty, return "cmd" history.
4785 * Returns -1 for unknown history name.
4786 */
4787 int
4788get_histtype(name)
4789 char_u *name;
4790{
4791 int i;
4792 int len = (int)STRLEN(name);
4793
4794 /* No argument: use current history. */
4795 if (len == 0)
4796 return hist_char2type(ccline.cmdfirstc);
4797
4798 for (i = 0; history_names[i] != NULL; ++i)
4799 if (STRNICMP(name, history_names[i], len) == 0)
4800 return i;
4801
4802 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4803 return hist_char2type(name[0]);
4804
4805 return -1;
4806}
4807
4808static int last_maptick = -1; /* last seen maptick */
4809
4810/*
4811 * Add the given string to the given history. If the string is already in the
4812 * history then it is moved to the front. "histype" may be one of he HIST_
4813 * values.
4814 */
4815 void
4816add_to_history(histype, new_entry, in_map, sep)
4817 int histype;
4818 char_u *new_entry;
4819 int in_map; /* consider maptick when inside a mapping */
4820 int sep; /* separator character used (search hist) */
4821{
4822 histentry_T *hisptr;
4823 int len;
4824
4825 if (hislen == 0) /* no history */
4826 return;
4827
4828 /*
4829 * Searches inside the same mapping overwrite each other, so that only
4830 * the last line is kept. Be careful not to remove a line that was moved
4831 * down, only lines that were added.
4832 */
4833 if (histype == HIST_SEARCH && in_map)
4834 {
4835 if (maptick == last_maptick)
4836 {
4837 /* Current line is from the same mapping, remove it */
4838 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4839 vim_free(hisptr->hisstr);
4840 hisptr->hisstr = NULL;
4841 hisptr->hisnum = 0;
4842 --hisnum[histype];
4843 if (--hisidx[HIST_SEARCH] < 0)
4844 hisidx[HIST_SEARCH] = hislen - 1;
4845 }
4846 last_maptick = -1;
4847 }
4848 if (!in_history(histype, new_entry, TRUE))
4849 {
4850 if (++hisidx[histype] == hislen)
4851 hisidx[histype] = 0;
4852 hisptr = &history[histype][hisidx[histype]];
4853 vim_free(hisptr->hisstr);
4854
4855 /* Store the separator after the NUL of the string. */
4856 len = STRLEN(new_entry);
4857 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4858 if (hisptr->hisstr != NULL)
4859 hisptr->hisstr[len + 1] = sep;
4860
4861 hisptr->hisnum = ++hisnum[histype];
4862 if (histype == HIST_SEARCH && in_map)
4863 last_maptick = maptick;
4864 }
4865}
4866
4867#if defined(FEAT_EVAL) || defined(PROTO)
4868
4869/*
4870 * Get identifier of newest history entry.
4871 * "histype" may be one of the HIST_ values.
4872 */
4873 int
4874get_history_idx(histype)
4875 int histype;
4876{
4877 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4878 || hisidx[histype] < 0)
4879 return -1;
4880
4881 return history[histype][hisidx[histype]].hisnum;
4882}
4883
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004884static struct cmdline_info *get_ccline_ptr __ARGS((void));
4885
4886/*
4887 * Get pointer to the command line info to use. cmdline_paste() may clear
4888 * ccline and put the previous value in prev_ccline.
4889 */
4890 static struct cmdline_info *
4891get_ccline_ptr()
4892{
4893 if ((State & CMDLINE) == 0)
4894 return NULL;
4895 if (ccline.cmdbuff != NULL)
4896 return &ccline;
4897 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4898 return &prev_ccline;
4899 return NULL;
4900}
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902/*
4903 * Get the current command line in allocated memory.
4904 * Only works when the command line is being edited.
4905 * Returns NULL when something is wrong.
4906 */
4907 char_u *
4908get_cmdline_str()
4909{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004910 struct cmdline_info *p = get_ccline_ptr();
4911
4912 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004914 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915}
4916
4917/*
4918 * Get the current command line position, counted in bytes.
4919 * Zero is the first position.
4920 * Only works when the command line is being edited.
4921 * Returns -1 when something is wrong.
4922 */
4923 int
4924get_cmdline_pos()
4925{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004926 struct cmdline_info *p = get_ccline_ptr();
4927
4928 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004930 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931}
4932
4933/*
4934 * Set the command line byte position to "pos". Zero is the first position.
4935 * Only works when the command line is being edited.
4936 * Returns 1 when failed, 0 when OK.
4937 */
4938 int
4939set_cmdline_pos(pos)
4940 int pos;
4941{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004942 struct cmdline_info *p = get_ccline_ptr();
4943
4944 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 return 1;
4946
4947 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4948 * changed the command line. */
4949 if (pos < 0)
4950 new_cmdpos = 0;
4951 else
4952 new_cmdpos = pos;
4953 return 0;
4954}
4955
4956/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004957 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00004958 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004959 * Only works when the command line is being edited.
4960 * Returns NUL when something is wrong.
4961 */
4962 int
4963get_cmdline_type()
4964{
4965 struct cmdline_info *p = get_ccline_ptr();
4966
4967 if (p == NULL)
4968 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00004969 if (p->cmdfirstc == NUL)
4970 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004971 return p->cmdfirstc;
4972}
4973
4974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 * Calculate history index from a number:
4976 * num > 0: seen as identifying number of a history entry
4977 * num < 0: relative position in history wrt newest entry
4978 * "histype" may be one of the HIST_ values.
4979 */
4980 static int
4981calc_hist_idx(histype, num)
4982 int histype;
4983 int num;
4984{
4985 int i;
4986 histentry_T *hist;
4987 int wrapped = FALSE;
4988
4989 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4990 || (i = hisidx[histype]) < 0 || num == 0)
4991 return -1;
4992
4993 hist = history[histype];
4994 if (num > 0)
4995 {
4996 while (hist[i].hisnum > num)
4997 if (--i < 0)
4998 {
4999 if (wrapped)
5000 break;
5001 i += hislen;
5002 wrapped = TRUE;
5003 }
5004 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5005 return i;
5006 }
5007 else if (-num <= hislen)
5008 {
5009 i += num + 1;
5010 if (i < 0)
5011 i += hislen;
5012 if (hist[i].hisstr != NULL)
5013 return i;
5014 }
5015 return -1;
5016}
5017
5018/*
5019 * Get a history entry by its index.
5020 * "histype" may be one of the HIST_ values.
5021 */
5022 char_u *
5023get_history_entry(histype, idx)
5024 int histype;
5025 int idx;
5026{
5027 idx = calc_hist_idx(histype, idx);
5028 if (idx >= 0)
5029 return history[histype][idx].hisstr;
5030 else
5031 return (char_u *)"";
5032}
5033
5034/*
5035 * Clear all entries of a history.
5036 * "histype" may be one of the HIST_ values.
5037 */
5038 int
5039clr_history(histype)
5040 int histype;
5041{
5042 int i;
5043 histentry_T *hisptr;
5044
5045 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5046 {
5047 hisptr = history[histype];
5048 for (i = hislen; i--;)
5049 {
5050 vim_free(hisptr->hisstr);
5051 hisptr->hisnum = 0;
5052 hisptr++->hisstr = NULL;
5053 }
5054 hisidx[histype] = -1; /* mark history as cleared */
5055 hisnum[histype] = 0; /* reset identifier counter */
5056 return OK;
5057 }
5058 return FAIL;
5059}
5060
5061/*
5062 * Remove all entries matching {str} from a history.
5063 * "histype" may be one of the HIST_ values.
5064 */
5065 int
5066del_history_entry(histype, str)
5067 int histype;
5068 char_u *str;
5069{
5070 regmatch_T regmatch;
5071 histentry_T *hisptr;
5072 int idx;
5073 int i;
5074 int last;
5075 int found = FALSE;
5076
5077 regmatch.regprog = NULL;
5078 regmatch.rm_ic = FALSE; /* always match case */
5079 if (hislen != 0
5080 && histype >= 0
5081 && histype < HIST_COUNT
5082 && *str != NUL
5083 && (idx = hisidx[histype]) >= 0
5084 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5085 != NULL)
5086 {
5087 i = last = idx;
5088 do
5089 {
5090 hisptr = &history[histype][i];
5091 if (hisptr->hisstr == NULL)
5092 break;
5093 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5094 {
5095 found = TRUE;
5096 vim_free(hisptr->hisstr);
5097 hisptr->hisstr = NULL;
5098 hisptr->hisnum = 0;
5099 }
5100 else
5101 {
5102 if (i != last)
5103 {
5104 history[histype][last] = *hisptr;
5105 hisptr->hisstr = NULL;
5106 hisptr->hisnum = 0;
5107 }
5108 if (--last < 0)
5109 last += hislen;
5110 }
5111 if (--i < 0)
5112 i += hislen;
5113 } while (i != idx);
5114 if (history[histype][idx].hisstr == NULL)
5115 hisidx[histype] = -1;
5116 }
5117 vim_free(regmatch.regprog);
5118 return found;
5119}
5120
5121/*
5122 * Remove an indexed entry from a history.
5123 * "histype" may be one of the HIST_ values.
5124 */
5125 int
5126del_history_idx(histype, idx)
5127 int histype;
5128 int idx;
5129{
5130 int i, j;
5131
5132 i = calc_hist_idx(histype, idx);
5133 if (i < 0)
5134 return FALSE;
5135 idx = hisidx[histype];
5136 vim_free(history[histype][i].hisstr);
5137
5138 /* When deleting the last added search string in a mapping, reset
5139 * last_maptick, so that the last added search string isn't deleted again.
5140 */
5141 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5142 last_maptick = -1;
5143
5144 while (i != idx)
5145 {
5146 j = (i + 1) % hislen;
5147 history[histype][i] = history[histype][j];
5148 i = j;
5149 }
5150 history[histype][i].hisstr = NULL;
5151 history[histype][i].hisnum = 0;
5152 if (--i < 0)
5153 i += hislen;
5154 hisidx[histype] = i;
5155 return TRUE;
5156}
5157
5158#endif /* FEAT_EVAL */
5159
5160#if defined(FEAT_CRYPT) || defined(PROTO)
5161/*
5162 * Very specific function to remove the value in ":set key=val" from the
5163 * history.
5164 */
5165 void
5166remove_key_from_history()
5167{
5168 char_u *p;
5169 int i;
5170
5171 i = hisidx[HIST_CMD];
5172 if (i < 0)
5173 return;
5174 p = history[HIST_CMD][i].hisstr;
5175 if (p != NULL)
5176 for ( ; *p; ++p)
5177 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5178 {
5179 p = vim_strchr(p + 3, '=');
5180 if (p == NULL)
5181 break;
5182 ++p;
5183 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5184 if (p[i] == '\\' && p[i + 1])
5185 ++i;
5186 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5187 --p;
5188 }
5189}
5190#endif
5191
5192#endif /* FEAT_CMDHIST */
5193
5194#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5195/*
5196 * Get indices "num1,num2" that specify a range within a list (not a range of
5197 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5198 * Returns OK if parsed successfully, otherwise FAIL.
5199 */
5200 int
5201get_list_range(str, num1, num2)
5202 char_u **str;
5203 int *num1;
5204 int *num2;
5205{
5206 int len;
5207 int first = FALSE;
5208 long num;
5209
5210 *str = skipwhite(*str);
5211 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5212 {
5213 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5214 *str += len;
5215 *num1 = (int)num;
5216 first = TRUE;
5217 }
5218 *str = skipwhite(*str);
5219 if (**str == ',') /* parse "to" part of range */
5220 {
5221 *str = skipwhite(*str + 1);
5222 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5223 if (len > 0)
5224 {
5225 *num2 = (int)num;
5226 *str = skipwhite(*str + len);
5227 }
5228 else if (!first) /* no number given at all */
5229 return FAIL;
5230 }
5231 else if (first) /* only one number given */
5232 *num2 = *num1;
5233 return OK;
5234}
5235#endif
5236
5237#if defined(FEAT_CMDHIST) || defined(PROTO)
5238/*
5239 * :history command - print a history
5240 */
5241 void
5242ex_history(eap)
5243 exarg_T *eap;
5244{
5245 histentry_T *hist;
5246 int histype1 = HIST_CMD;
5247 int histype2 = HIST_CMD;
5248 int hisidx1 = 1;
5249 int hisidx2 = -1;
5250 int idx;
5251 int i, j, k;
5252 char_u *end;
5253 char_u *arg = eap->arg;
5254
5255 if (hislen == 0)
5256 {
5257 MSG(_("'history' option is zero"));
5258 return;
5259 }
5260
5261 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5262 {
5263 end = arg;
5264 while (ASCII_ISALPHA(*end)
5265 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5266 end++;
5267 i = *end;
5268 *end = NUL;
5269 histype1 = get_histtype(arg);
5270 if (histype1 == -1)
5271 {
5272 if (STRICMP(arg, "all") == 0)
5273 {
5274 histype1 = 0;
5275 histype2 = HIST_COUNT-1;
5276 }
5277 else
5278 {
5279 *end = i;
5280 EMSG(_(e_trailing));
5281 return;
5282 }
5283 }
5284 else
5285 histype2 = histype1;
5286 *end = i;
5287 }
5288 else
5289 end = arg;
5290 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5291 {
5292 EMSG(_(e_trailing));
5293 return;
5294 }
5295
5296 for (; !got_int && histype1 <= histype2; ++histype1)
5297 {
5298 STRCPY(IObuff, "\n # ");
5299 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5300 MSG_PUTS_TITLE(IObuff);
5301 idx = hisidx[histype1];
5302 hist = history[histype1];
5303 j = hisidx1;
5304 k = hisidx2;
5305 if (j < 0)
5306 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5307 if (k < 0)
5308 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5309 if (idx >= 0 && j <= k)
5310 for (i = idx + 1; !got_int; ++i)
5311 {
5312 if (i == hislen)
5313 i = 0;
5314 if (hist[i].hisstr != NULL
5315 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5316 {
5317 msg_putchar('\n');
5318 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5319 hist[i].hisnum);
5320 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5321 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5322 (int)Columns - 10);
5323 else
5324 STRCAT(IObuff, hist[i].hisstr);
5325 msg_outtrans(IObuff);
5326 out_flush();
5327 }
5328 if (i == idx)
5329 break;
5330 }
5331 }
5332}
5333#endif
5334
5335#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5336static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5337static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5338static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5339static int viminfo_add_at_front = FALSE;
5340
5341static int hist_type2char __ARGS((int type, int use_question));
5342
5343/*
5344 * Translate a history type number to the associated character.
5345 */
5346 static int
5347hist_type2char(type, use_question)
5348 int type;
5349 int use_question; /* use '?' instead of '/' */
5350{
5351 if (type == HIST_CMD)
5352 return ':';
5353 if (type == HIST_SEARCH)
5354 {
5355 if (use_question)
5356 return '?';
5357 else
5358 return '/';
5359 }
5360 if (type == HIST_EXPR)
5361 return '=';
5362 return '@';
5363}
5364
5365/*
5366 * Prepare for reading the history from the viminfo file.
5367 * This allocates history arrays to store the read history lines.
5368 */
5369 void
5370prepare_viminfo_history(asklen)
5371 int asklen;
5372{
5373 int i;
5374 int num;
5375 int type;
5376 int len;
5377
5378 init_history();
5379 viminfo_add_at_front = (asklen != 0);
5380 if (asklen > hislen)
5381 asklen = hislen;
5382
5383 for (type = 0; type < HIST_COUNT; ++type)
5384 {
5385 /*
5386 * Count the number of empty spaces in the history list. If there are
5387 * more spaces available than we request, then fill them up.
5388 */
5389 for (i = 0, num = 0; i < hislen; i++)
5390 if (history[type][i].hisstr == NULL)
5391 num++;
5392 len = asklen;
5393 if (num > len)
5394 len = num;
5395 if (len <= 0)
5396 viminfo_history[type] = NULL;
5397 else
5398 viminfo_history[type] =
5399 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5400 if (viminfo_history[type] == NULL)
5401 len = 0;
5402 viminfo_hislen[type] = len;
5403 viminfo_hisidx[type] = 0;
5404 }
5405}
5406
5407/*
5408 * Accept a line from the viminfo, store it in the history array when it's
5409 * new.
5410 */
5411 int
5412read_viminfo_history(virp)
5413 vir_T *virp;
5414{
5415 int type;
5416 long_u len;
5417 char_u *val;
5418 char_u *p;
5419
5420 type = hist_char2type(virp->vir_line[0]);
5421 if (viminfo_hisidx[type] < viminfo_hislen[type])
5422 {
5423 val = viminfo_readstring(virp, 1, TRUE);
5424 if (val != NULL && *val != NUL)
5425 {
5426 if (!in_history(type, val + (type == HIST_SEARCH),
5427 viminfo_add_at_front))
5428 {
5429 /* Need to re-allocate to append the separator byte. */
5430 len = STRLEN(val);
5431 p = lalloc(len + 2, TRUE);
5432 if (p != NULL)
5433 {
5434 if (type == HIST_SEARCH)
5435 {
5436 /* Search entry: Move the separator from the first
5437 * column to after the NUL. */
5438 mch_memmove(p, val + 1, (size_t)len);
5439 p[len] = (*val == ' ' ? NUL : *val);
5440 }
5441 else
5442 {
5443 /* Not a search entry: No separator in the viminfo
5444 * file, add a NUL separator. */
5445 mch_memmove(p, val, (size_t)len + 1);
5446 p[len + 1] = NUL;
5447 }
5448 viminfo_history[type][viminfo_hisidx[type]++] = p;
5449 }
5450 }
5451 }
5452 vim_free(val);
5453 }
5454 return viminfo_readline(virp);
5455}
5456
5457 void
5458finish_viminfo_history()
5459{
5460 int idx;
5461 int i;
5462 int type;
5463
5464 for (type = 0; type < HIST_COUNT; ++type)
5465 {
5466 if (history[type] == NULL)
5467 return;
5468 idx = hisidx[type] + viminfo_hisidx[type];
5469 if (idx >= hislen)
5470 idx -= hislen;
5471 else if (idx < 0)
5472 idx = hislen - 1;
5473 if (viminfo_add_at_front)
5474 hisidx[type] = idx;
5475 else
5476 {
5477 if (hisidx[type] == -1)
5478 hisidx[type] = hislen - 1;
5479 do
5480 {
5481 if (history[type][idx].hisstr != NULL)
5482 break;
5483 if (++idx == hislen)
5484 idx = 0;
5485 } while (idx != hisidx[type]);
5486 if (idx != hisidx[type] && --idx < 0)
5487 idx = hislen - 1;
5488 }
5489 for (i = 0; i < viminfo_hisidx[type]; i++)
5490 {
5491 vim_free(history[type][idx].hisstr);
5492 history[type][idx].hisstr = viminfo_history[type][i];
5493 if (--idx < 0)
5494 idx = hislen - 1;
5495 }
5496 idx += 1;
5497 idx %= hislen;
5498 for (i = 0; i < viminfo_hisidx[type]; i++)
5499 {
5500 history[type][idx++].hisnum = ++hisnum[type];
5501 idx %= hislen;
5502 }
5503 vim_free(viminfo_history[type]);
5504 viminfo_history[type] = NULL;
5505 }
5506}
5507
5508 void
5509write_viminfo_history(fp)
5510 FILE *fp;
5511{
5512 int i;
5513 int type;
5514 int num_saved;
5515 char_u *p;
5516 int c;
5517
5518 init_history();
5519 if (hislen == 0)
5520 return;
5521 for (type = 0; type < HIST_COUNT; ++type)
5522 {
5523 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5524 if (num_saved == 0)
5525 continue;
5526 if (num_saved < 0) /* Use default */
5527 num_saved = hislen;
5528 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5529 type == HIST_CMD ? _("Command Line") :
5530 type == HIST_SEARCH ? _("Search String") :
5531 type == HIST_EXPR ? _("Expression") :
5532 _("Input Line"));
5533 if (num_saved > hislen)
5534 num_saved = hislen;
5535 i = hisidx[type];
5536 if (i >= 0)
5537 while (num_saved--)
5538 {
5539 p = history[type][i].hisstr;
5540 if (p != NULL)
5541 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005542 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 /* For the search history: put the separator in the second
5544 * column; use a space if there isn't one. */
5545 if (type == HIST_SEARCH)
5546 {
5547 c = p[STRLEN(p) + 1];
5548 putc(c == NUL ? ' ' : c, fp);
5549 }
5550 viminfo_writestring(fp, p);
5551 }
5552 if (--i < 0)
5553 i = hislen - 1;
5554 }
5555 }
5556}
5557#endif /* FEAT_VIMINFO */
5558
5559#if defined(FEAT_FKMAP) || defined(PROTO)
5560/*
5561 * Write a character at the current cursor+offset position.
5562 * It is directly written into the command buffer block.
5563 */
5564 void
5565cmd_pchar(c, offset)
5566 int c, offset;
5567{
5568 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5569 {
5570 EMSG(_("E198: cmd_pchar beyond the command length"));
5571 return;
5572 }
5573 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5574 ccline.cmdbuff[ccline.cmdlen] = NUL;
5575}
5576
5577 int
5578cmd_gchar(offset)
5579 int offset;
5580{
5581 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5582 {
5583 /* EMSG(_("cmd_gchar beyond the command length")); */
5584 return NUL;
5585 }
5586 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5587}
5588#endif
5589
5590#if defined(FEAT_CMDWIN) || defined(PROTO)
5591/*
5592 * Open a window on the current command line and history. Allow editing in
5593 * the window. Returns when the window is closed.
5594 * Returns:
5595 * CR if the command is to be executed
5596 * Ctrl_C if it is to be abandoned
5597 * K_IGNORE if editing continues
5598 */
5599 static int
5600ex_window()
5601{
5602 struct cmdline_info save_ccline;
5603 buf_T *old_curbuf = curbuf;
5604 win_T *old_curwin = curwin;
5605 buf_T *bp;
5606 win_T *wp;
5607 int i;
5608 linenr_T lnum;
5609 int histtype;
5610 garray_T winsizes;
5611 char_u typestr[2];
5612 int save_restart_edit = restart_edit;
5613 int save_State = State;
5614 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005615#ifdef FEAT_RIGHTLEFT
5616 int save_cmdmsg_rl = cmdmsg_rl;
5617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619 /* Can't do this recursively. Can't do it when typing a password. */
5620 if (cmdwin_type != 0
5621# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5622 || cmdline_star > 0
5623# endif
5624 )
5625 {
5626 beep_flush();
5627 return K_IGNORE;
5628 }
5629
5630 /* Save current window sizes. */
5631 win_size_save(&winsizes);
5632
5633# ifdef FEAT_AUTOCMD
5634 /* Don't execute autocommands while creating the window. */
5635 ++autocmd_block;
5636# endif
5637 /* Create a window for the command-line buffer. */
5638 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5639 {
5640 beep_flush();
5641 return K_IGNORE;
5642 }
5643 cmdwin_type = ccline.cmdfirstc;
5644 if (cmdwin_type == NUL)
5645 cmdwin_type = '-';
5646
5647 /* Create the command-line buffer empty. */
5648 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5649 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5650 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5651 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5652 curbuf->b_p_ma = TRUE;
5653# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005654 curwin->w_p_rl = cmdmsg_rl;
5655 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656# endif
5657# ifdef FEAT_SCROLLBIND
5658 curwin->w_p_scb = FALSE;
5659# endif
5660
5661# ifdef FEAT_AUTOCMD
5662 /* Do execute autocommands for setting the filetype (load syntax). */
5663 --autocmd_block;
5664# endif
5665
Bram Moolenaar46152342005-09-07 21:18:43 +00005666 /* Showing the prompt may have set need_wait_return, reset it. */
5667 need_wait_return = FALSE;
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 histtype = hist_char2type(ccline.cmdfirstc);
5670 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5671 {
5672 if (p_wc == TAB)
5673 {
5674 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5675 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5676 }
5677 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5678 }
5679
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005680 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5681 * sets 'textwidth' to 78). */
5682 curbuf->b_p_tw = 0;
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 /* Fill the buffer with the history. */
5685 init_history();
5686 if (hislen > 0)
5687 {
5688 i = hisidx[histtype];
5689 if (i >= 0)
5690 {
5691 lnum = 0;
5692 do
5693 {
5694 if (++i == hislen)
5695 i = 0;
5696 if (history[histtype][i].hisstr != NULL)
5697 ml_append(lnum++, history[histtype][i].hisstr,
5698 (colnr_T)0, FALSE);
5699 }
5700 while (i != hisidx[histtype]);
5701 }
5702 }
5703
5704 /* Replace the empty last line with the current command-line and put the
5705 * cursor there. */
5706 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5707 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5708 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005709 changed_line_abv_curs();
5710 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 redraw_later(NOT_VALID);
5712
5713 /* Save the command line info, can be used recursively. */
5714 save_ccline = ccline;
5715 ccline.cmdbuff = NULL;
5716 ccline.cmdprompt = NULL;
5717
5718 /* No Ex mode here! */
5719 exmode_active = 0;
5720
5721 State = NORMAL;
5722# ifdef FEAT_MOUSE
5723 setmouse();
5724# endif
5725
5726# ifdef FEAT_AUTOCMD
5727 /* Trigger CmdwinEnter autocommands. */
5728 typestr[0] = cmdwin_type;
5729 typestr[1] = NUL;
5730 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5731# endif
5732
5733 i = RedrawingDisabled;
5734 RedrawingDisabled = 0;
5735
5736 /*
5737 * Call the main loop until <CR> or CTRL-C is typed.
5738 */
5739 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005740 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741
5742 RedrawingDisabled = i;
5743
5744# ifdef FEAT_AUTOCMD
5745 /* Trigger CmdwinLeave autocommands. */
5746 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5747# endif
5748
5749 /* Restore the comand line info. */
5750 ccline = save_ccline;
5751 cmdwin_type = 0;
5752
5753 exmode_active = save_exmode;
5754
5755 /* Safety check: The old window or buffer was deleted: It's a a bug when
5756 * this happens! */
5757 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5758 {
5759 cmdwin_result = Ctrl_C;
5760 EMSG(_("E199: Active window or buffer deleted"));
5761 }
5762 else
5763 {
5764# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5765 /* autocmds may abort script processing */
5766 if (aborting() && cmdwin_result != K_IGNORE)
5767 cmdwin_result = Ctrl_C;
5768# endif
5769 /* Set the new command line from the cmdline buffer. */
5770 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005771 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005773 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5774
5775 if (histtype == HIST_CMD)
5776 {
5777 /* Execute the command directly. */
5778 ccline.cmdbuff = vim_strsave((char_u *)p);
5779 cmdwin_result = CAR;
5780 }
5781 else
5782 {
5783 /* First need to cancel what we were doing. */
5784 ccline.cmdbuff = NULL;
5785 stuffcharReadbuff(':');
5786 stuffReadbuff((char_u *)p);
5787 stuffcharReadbuff(CAR);
5788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 }
5790 else if (cmdwin_result == K_XF2) /* :qa typed */
5791 {
5792 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5793 cmdwin_result = CAR;
5794 }
5795 else
5796 ccline.cmdbuff = vim_strsave(ml_get_curline());
5797 if (ccline.cmdbuff == NULL)
5798 cmdwin_result = Ctrl_C;
5799 else
5800 {
5801 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5802 ccline.cmdbufflen = ccline.cmdlen + 1;
5803 ccline.cmdpos = curwin->w_cursor.col;
5804 if (ccline.cmdpos > ccline.cmdlen)
5805 ccline.cmdpos = ccline.cmdlen;
5806 if (cmdwin_result == K_IGNORE)
5807 {
5808 set_cmdspos_cursor();
5809 redrawcmd();
5810 }
5811 }
5812
5813# ifdef FEAT_AUTOCMD
5814 /* Don't execute autocommands while deleting the window. */
5815 ++autocmd_block;
5816# endif
5817 wp = curwin;
5818 bp = curbuf;
5819 win_goto(old_curwin);
5820 win_close(wp, TRUE);
5821 close_buffer(NULL, bp, DOBUF_WIPE);
5822
5823 /* Restore window sizes. */
5824 win_size_restore(&winsizes);
5825
5826# ifdef FEAT_AUTOCMD
5827 --autocmd_block;
5828# endif
5829 }
5830
5831 ga_clear(&winsizes);
5832 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005833# ifdef FEAT_RIGHTLEFT
5834 cmdmsg_rl = save_cmdmsg_rl;
5835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837 State = save_State;
5838# ifdef FEAT_MOUSE
5839 setmouse();
5840# endif
5841
5842 return cmdwin_result;
5843}
5844#endif /* FEAT_CMDWIN */
5845
5846/*
5847 * Used for commands that either take a simple command string argument, or:
5848 * cmd << endmarker
5849 * {script}
5850 * endmarker
5851 * Returns a pointer to allocated memory with {script} or NULL.
5852 */
5853 char_u *
5854script_get(eap, cmd)
5855 exarg_T *eap;
5856 char_u *cmd;
5857{
5858 char_u *theline;
5859 char *end_pattern = NULL;
5860 char dot[] = ".";
5861 garray_T ga;
5862
5863 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5864 return NULL;
5865
5866 ga_init2(&ga, 1, 0x400);
5867
5868 if (cmd[2] != NUL)
5869 end_pattern = (char *)skipwhite(cmd + 2);
5870 else
5871 end_pattern = dot;
5872
5873 for (;;)
5874 {
5875 theline = eap->getline(
5876#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005877 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878#endif
5879 NUL, eap->cookie, 0);
5880
5881 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5882 break;
5883
5884 ga_concat(&ga, theline);
5885 ga_append(&ga, '\n');
5886 vim_free(theline);
5887 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005888 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 return (char_u *)ga.ga_data;
5891}