blob: bac600cf5f5bfeb5d3abbef4bd1cf40e5447392f [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 Moolenaarb71eaae2006-01-20 23:10:18 +0000664 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000665 * 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 Moolenaarb71eaae2006-01-20 23:10:18 +0000668 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000670 --textlock;
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/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001878 * Return TRUE when the text must not be changed and we can't switch to
1879 * another window or buffer. Used when editing the command line, evaluating
1880 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001881 */
1882 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001883text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001884{
1885#ifdef FEAT_CMDWIN
1886 if (cmdwin_type != 0)
1887 return TRUE;
1888#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001889 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001890}
1891
1892/*
1893 * Give an error message for a command that isn't allowed while the cmdline
1894 * window is open or editing the cmdline in another way.
1895 */
1896 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001897text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001898{
1899#ifdef FEAT_CMDWIN
1900 if (cmdwin_type != 0)
1901 EMSG(_(e_cmdwin));
1902 else
1903#endif
1904 EMSG(_(e_secure));
1905}
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 static int
1908cmdline_charsize(idx)
1909 int idx;
1910{
1911#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
1912 if (cmdline_star > 0) /* showing '*', always 1 position */
1913 return 1;
1914#endif
1915 return ptr2cells(ccline.cmdbuff + idx);
1916}
1917
1918/*
1919 * Compute the offset of the cursor on the command line for the prompt and
1920 * indent.
1921 */
1922 static void
1923set_cmdspos()
1924{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001925 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 ccline.cmdspos = 1 + ccline.cmdindent;
1927 else
1928 ccline.cmdspos = 0 + ccline.cmdindent;
1929}
1930
1931/*
1932 * Compute the screen position for the cursor on the command line.
1933 */
1934 static void
1935set_cmdspos_cursor()
1936{
1937 int i, m, c;
1938
1939 set_cmdspos();
1940 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001941 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001943 if (m < 0) /* overflow, Columns or Rows at weird value */
1944 m = MAXCOL;
1945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 else
1947 m = MAXCOL;
1948 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
1949 {
1950 c = cmdline_charsize(i);
1951#ifdef FEAT_MBYTE
1952 /* Count ">" for double-wide multi-byte char that doesn't fit. */
1953 if (has_mbyte)
1954 correct_cmdspos(i, c);
1955#endif
1956 /* If the cmdline doesn't fit, put cursor on last visible char. */
1957 if ((ccline.cmdspos += c) >= m)
1958 {
1959 ccline.cmdpos = i - 1;
1960 ccline.cmdspos -= c;
1961 break;
1962 }
1963#ifdef FEAT_MBYTE
1964 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001965 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#endif
1967 }
1968}
1969
1970#ifdef FEAT_MBYTE
1971/*
1972 * Check if the character at "idx", which is "cells" wide, is a multi-byte
1973 * character that doesn't fit, so that a ">" must be displayed.
1974 */
1975 static void
1976correct_cmdspos(idx, cells)
1977 int idx;
1978 int cells;
1979{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001980 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
1982 && ccline.cmdspos % Columns + cells > Columns)
1983 ccline.cmdspos++;
1984}
1985#endif
1986
1987/*
1988 * Get an Ex command line for the ":" command.
1989 */
1990/* ARGSUSED */
1991 char_u *
1992getexline(c, dummy, indent)
1993 int c; /* normally ':', NUL for ":append" */
1994 void *dummy; /* cookie not used */
1995 int indent; /* indent for inside conditionals */
1996{
1997 /* When executing a register, remove ':' that's in front of each line. */
1998 if (exec_from_reg && vpeekc() == ':')
1999 (void)vgetc();
2000 return getcmdline(c, 1L, indent);
2001}
2002
2003/*
2004 * Get an Ex command line for Ex mode.
2005 * In Ex mode we only use the OS supplied line editing features and no
2006 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002007 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 */
2009/* ARGSUSED */
2010 char_u *
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002011getexmodeline(promptc, dummy, indent)
2012 int promptc; /* normally ':', NUL for ":append" and '?' for
2013 :s prompt */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 void *dummy; /* cookie not used */
2015 int indent; /* indent for inside conditionals */
2016{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002017 garray_T line_ga;
2018 char_u *pend;
2019 int startcol = 0;
2020 int c1;
2021 int escaped = FALSE; /* CTRL-V typed */
2022 int vcol = 0;
2023 char_u *p;
2024 int prev_char = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025
2026 /* Switch cursor on now. This avoids that it happens after the "\n", which
2027 * confuses the system function that computes tabstops. */
2028 cursor_on();
2029
2030 /* always start in column 0; write a newline if necessary */
2031 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002032 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002034 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002036 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002037 if (p_prompt)
2038 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 while (indent-- > 0)
2040 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 }
2043
2044 ga_init2(&line_ga, 1, 30);
2045
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002046 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002047 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002048 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002049 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002050 while (indent >= 8)
2051 {
2052 ga_append(&line_ga, TAB);
2053 msg_puts((char_u *)" ");
2054 indent -= 8;
2055 }
2056 while (indent-- > 0)
2057 {
2058 ga_append(&line_ga, ' ');
2059 msg_putchar(' ');
2060 }
2061 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002062 ++no_mapping;
2063 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 /*
2066 * Get the line, one character at a time.
2067 */
2068 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002069 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
2071 if (ga_grow(&line_ga, 40) == FAIL)
2072 break;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002073 pend = (char_u *)line_ga.ga_data + line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002075 /* Get one character at a time. Don't use inchar(), it can't handle
2076 * special characters. */
2077 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078
2079 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002080 * Handle line editing.
2081 * Previously this was left to the system, putting the terminal in
2082 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002084 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002086 msg_putchar('\n');
2087 break;
2088 }
2089
2090 if (!escaped)
2091 {
2092 /* CR typed means "enter", which is NL */
2093 if (c1 == '\r')
2094 c1 = '\n';
2095
2096 if (c1 == BS || c1 == K_BS
2097 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002099 if (line_ga.ga_len > 0)
2100 {
2101 --line_ga.ga_len;
2102 goto redraw;
2103 }
2104 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002107 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002109 msg_col = startcol;
2110 msg_clr_eos();
2111 line_ga.ga_len = 0;
2112 continue;
2113 }
2114
2115 if (c1 == Ctrl_T)
2116 {
2117 p = (char_u *)line_ga.ga_data;
2118 p[line_ga.ga_len] = NUL;
2119 indent = get_indent_str(p, 8);
2120 indent += curbuf->b_p_sw - indent % curbuf->b_p_sw;
2121add_indent:
2122 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002124 char_u *s = skipwhite(p);
2125
2126 ga_grow(&line_ga, 1);
2127 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2128 *s = ' ';
2129 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002131redraw:
2132 /* redraw the line */
2133 msg_col = startcol;
2134 windgoto(msg_row, msg_col);
2135 vcol = 0;
2136 for (p = (char_u *)line_ga.ga_data;
2137 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002139 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002141 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002143 msg_putchar(' ');
2144 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002146 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002148 msg_outtrans_len(p, 1);
2149 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002152 msg_clr_eos();
2153 continue;
2154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002156 if (c1 == Ctrl_D)
2157 {
2158 /* Delete one shiftwidth. */
2159 p = (char_u *)line_ga.ga_data;
2160 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002162 if (prev_char == '^')
2163 ex_keep_indent = TRUE;
2164 indent = 0;
2165 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166 }
2167 else
2168 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002169 p[line_ga.ga_len] = NUL;
2170 indent = get_indent_str(p, 8);
2171 --indent;
2172 indent -= indent % curbuf->b_p_sw;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002174 while (get_indent_str(p, 8) > indent)
2175 {
2176 char_u *s = skipwhite(p);
2177
2178 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2179 --line_ga.ga_len;
2180 }
2181 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183
2184 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2185 {
2186 escaped = TRUE;
2187 continue;
2188 }
2189
2190 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2191 if (IS_SPECIAL(c1))
2192 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002194
2195 if (IS_SPECIAL(c1))
2196 c1 = '?';
2197 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2198 prev_char = c1;
2199 if (c1 == '\n')
2200 msg_putchar('\n');
2201 else if (c1 == TAB)
2202 {
2203 /* Don't use chartabsize(), 'ts' can be different */
2204 do
2205 {
2206 msg_putchar(' ');
2207 } while (++vcol % 8);
2208 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002211 msg_outtrans_len(
2212 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2213 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 ++line_ga.ga_len;
2216 escaped = FALSE;
2217
2218 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002219 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002220
2221 /* we are done when a NL is entered, but not when it comes after a
2222 * backslash */
2223 if (line_ga.ga_len > 0 && pend[-1] == '\n'
2224 && (line_ga.ga_len <= 1 || pend[-2] != '\\'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 --line_ga.ga_len;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002227 --pend;
2228 *pend = NUL;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 }
2231 }
2232
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002233 --no_mapping;
2234 --allow_keys;
2235
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 /* make following messages go to the next line */
2237 msg_didout = FALSE;
2238 msg_col = 0;
2239 if (msg_row < Rows - 1)
2240 ++msg_row;
2241 emsg_on_display = FALSE; /* don't want ui_delay() */
2242
2243 if (got_int)
2244 ga_clear(&line_ga);
2245
2246 return (char_u *)line_ga.ga_data;
2247}
2248
Bram Moolenaare344bea2005-09-01 20:46:49 +00002249# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2250 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251/*
2252 * Return TRUE if ccline.overstrike is on.
2253 */
2254 int
2255cmdline_overstrike()
2256{
2257 return ccline.overstrike;
2258}
2259
2260/*
2261 * Return TRUE if the cursor is at the end of the cmdline.
2262 */
2263 int
2264cmdline_at_end()
2265{
2266 return (ccline.cmdpos >= ccline.cmdlen);
2267}
2268#endif
2269
Bram Moolenaar9372a112005-12-06 19:59:18 +00002270#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271/*
2272 * Return the virtual column number at the current cursor position.
2273 * This is used by the IM code to obtain the start of the preedit string.
2274 */
2275 colnr_T
2276cmdline_getvcol_cursor()
2277{
2278 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2279 return MAXCOL;
2280
2281# ifdef FEAT_MBYTE
2282 if (has_mbyte)
2283 {
2284 colnr_T col;
2285 int i = 0;
2286
2287 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002288 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
2290 return col;
2291 }
2292 else
2293# endif
2294 return ccline.cmdpos;
2295}
2296#endif
2297
2298#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2299/*
2300 * If part of the command line is an IM preedit string, redraw it with
2301 * IM feedback attributes. The cursor position is restored after drawing.
2302 */
2303 static void
2304redrawcmd_preedit()
2305{
2306 if ((State & CMDLINE)
2307 && xic != NULL
2308 && im_get_status()
2309 && !p_imdisable
2310 && im_is_preediting())
2311 {
2312 int cmdpos = 0;
2313 int cmdspos;
2314 int old_row;
2315 int old_col;
2316 colnr_T col;
2317
2318 old_row = msg_row;
2319 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002320 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321
2322# ifdef FEAT_MBYTE
2323 if (has_mbyte)
2324 {
2325 for (col = 0; col < preedit_start_col
2326 && cmdpos < ccline.cmdlen; ++col)
2327 {
2328 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002329 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 }
2331 }
2332 else
2333# endif
2334 {
2335 cmdspos += preedit_start_col;
2336 cmdpos += preedit_start_col;
2337 }
2338
2339 msg_row = cmdline_row + (cmdspos / (int)Columns);
2340 msg_col = cmdspos % (int)Columns;
2341 if (msg_row >= Rows)
2342 msg_row = Rows - 1;
2343
2344 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2345 {
2346 int char_len;
2347 int char_attr;
2348
2349 char_attr = im_get_feedback_attr(col);
2350 if (char_attr < 0)
2351 break; /* end of preedit string */
2352
2353# ifdef FEAT_MBYTE
2354 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002355 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 else
2357# endif
2358 char_len = 1;
2359
2360 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2361 cmdpos += char_len;
2362 }
2363
2364 msg_row = old_row;
2365 msg_col = old_col;
2366 }
2367}
2368#endif /* FEAT_XIM && FEAT_GUI_GTK */
2369
2370/*
2371 * Allocate a new command line buffer.
2372 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2373 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2374 */
2375 static void
2376alloc_cmdbuff(len)
2377 int len;
2378{
2379 /*
2380 * give some extra space to avoid having to allocate all the time
2381 */
2382 if (len < 80)
2383 len = 100;
2384 else
2385 len += 20;
2386
2387 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2388 ccline.cmdbufflen = len;
2389}
2390
2391/*
2392 * Re-allocate the command line to length len + something extra.
2393 * return FAIL for failure, OK otherwise
2394 */
2395 static int
2396realloc_cmdbuff(len)
2397 int len;
2398{
2399 char_u *p;
2400
2401 p = ccline.cmdbuff;
2402 alloc_cmdbuff(len); /* will get some more */
2403 if (ccline.cmdbuff == NULL) /* out of memory */
2404 {
2405 ccline.cmdbuff = p; /* keep the old one */
2406 return FAIL;
2407 }
2408 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen + 1);
2409 vim_free(p);
2410 return OK;
2411}
2412
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002413#if defined(FEAT_ARABIC) || defined(PROTO)
2414static char_u *arshape_buf = NULL;
2415
2416# if defined(EXITFREE) || defined(PROTO)
2417 void
2418free_cmdline_buf()
2419{
2420 vim_free(arshape_buf);
2421}
2422# endif
2423#endif
2424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425/*
2426 * Draw part of the cmdline at the current cursor position. But draw stars
2427 * when cmdline_star is TRUE.
2428 */
2429 static void
2430draw_cmdline(start, len)
2431 int start;
2432 int len;
2433{
2434#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2435 int i;
2436
2437 if (cmdline_star > 0)
2438 for (i = 0; i < len; ++i)
2439 {
2440 msg_putchar('*');
2441# ifdef FEAT_MBYTE
2442 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002443 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444# endif
2445 }
2446 else
2447#endif
2448#ifdef FEAT_ARABIC
2449 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2450 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 static int buflen = 0;
2452 char_u *p;
2453 int j;
2454 int newlen = 0;
2455 int mb_l;
2456 int pc, pc1;
2457 int prev_c = 0;
2458 int prev_c1 = 0;
2459 int u8c, u8c_c1, u8c_c2;
2460 int nc = 0;
2461 int dummy;
2462
2463 /*
2464 * Do arabic shaping into a temporary buffer. This is very
2465 * inefficient!
2466 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002467 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 {
2469 /* Re-allocate the buffer. We keep it around to avoid a lot of
2470 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002471 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002472 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002473 arshape_buf = alloc(buflen);
2474 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return; /* out of memory */
2476 }
2477
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002478 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2479 {
2480 /* Prepend a space to draw the leading composing char on. */
2481 arshape_buf[0] = ' ';
2482 newlen = 1;
2483 }
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 for (j = start; j < start + len; j += mb_l)
2486 {
2487 p = ccline.cmdbuff + j;
2488 u8c = utfc_ptr2char_len(p, &u8c_c1, &u8c_c2, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002489 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (ARABIC_CHAR(u8c))
2491 {
2492 /* Do Arabic shaping. */
2493 if (cmdmsg_rl)
2494 {
2495 /* displaying from right to left */
2496 pc = prev_c;
2497 pc1 = prev_c1;
2498 prev_c1 = u8c_c1;
2499 if (j + mb_l >= start + len)
2500 nc = NUL;
2501 else
2502 nc = utf_ptr2char(p + mb_l);
2503 }
2504 else
2505 {
2506 /* displaying from left to right */
2507 if (j + mb_l >= start + len)
2508 pc = NUL;
2509 else
2510 pc = utfc_ptr2char_len(p + mb_l, &pc1, &dummy,
2511 start + len - j - mb_l);
2512 nc = prev_c;
2513 }
2514 prev_c = u8c;
2515
2516 u8c = arabic_shape(u8c, NULL, &u8c_c1, pc, pc1, nc);
2517
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002518 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 if (u8c_c1 != 0)
2520 {
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002521 newlen += (*mb_char2bytes)(u8c_c1, arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (u8c_c2 != 0)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002523 newlen += (*mb_char2bytes)(u8c_c2,
2524 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 }
2526 }
2527 else
2528 {
2529 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002530 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 newlen += mb_l;
2532 }
2533 }
2534
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002535 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 }
2537 else
2538#endif
2539 msg_outtrans_len(ccline.cmdbuff + start, len);
2540}
2541
2542/*
2543 * Put a character on the command line. Shifts the following text to the
2544 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2545 * "c" must be printable (fit in one display cell)!
2546 */
2547 void
2548putcmdline(c, shift)
2549 int c;
2550 int shift;
2551{
2552 if (cmd_silent)
2553 return;
2554 msg_no_more = TRUE;
2555 msg_putchar(c);
2556 if (shift)
2557 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2558 msg_no_more = FALSE;
2559 cursorcmd();
2560}
2561
2562/*
2563 * Undo a putcmdline(c, FALSE).
2564 */
2565 void
2566unputcmdline()
2567{
2568 if (cmd_silent)
2569 return;
2570 msg_no_more = TRUE;
2571 if (ccline.cmdlen == ccline.cmdpos)
2572 msg_putchar(' ');
2573 else
2574 draw_cmdline(ccline.cmdpos, 1);
2575 msg_no_more = FALSE;
2576 cursorcmd();
2577}
2578
2579/*
2580 * Put the given string, of the given length, onto the command line.
2581 * If len is -1, then STRLEN() is used to calculate the length.
2582 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2583 * part will be redrawn, otherwise it will not. If this function is called
2584 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2585 * called afterwards.
2586 */
2587 int
2588put_on_cmdline(str, len, redraw)
2589 char_u *str;
2590 int len;
2591 int redraw;
2592{
2593 int retval;
2594 int i;
2595 int m;
2596 int c;
2597
2598 if (len < 0)
2599 len = (int)STRLEN(str);
2600
2601 /* Check if ccline.cmdbuff needs to be longer */
2602 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
2603 retval = realloc_cmdbuff(ccline.cmdlen + len);
2604 else
2605 retval = OK;
2606 if (retval == OK)
2607 {
2608 if (!ccline.overstrike)
2609 {
2610 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2611 ccline.cmdbuff + ccline.cmdpos,
2612 (size_t)(ccline.cmdlen - ccline.cmdpos));
2613 ccline.cmdlen += len;
2614 }
2615 else
2616 {
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 {
2620 /* Count nr of characters in the new string. */
2621 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002622 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ++m;
2624 /* Count nr of bytes in cmdline that are overwritten by these
2625 * characters. */
2626 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002627 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 --m;
2629 if (i < ccline.cmdlen)
2630 {
2631 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2632 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2633 ccline.cmdlen += ccline.cmdpos + len - i;
2634 }
2635 else
2636 ccline.cmdlen = ccline.cmdpos + len;
2637 }
2638 else
2639#endif
2640 if (ccline.cmdpos + len > ccline.cmdlen)
2641 ccline.cmdlen = ccline.cmdpos + len;
2642 }
2643 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2644 ccline.cmdbuff[ccline.cmdlen] = NUL;
2645
2646#ifdef FEAT_MBYTE
2647 if (enc_utf8)
2648 {
2649 /* When the inserted text starts with a composing character,
2650 * backup to the character before it. There could be two of them.
2651 */
2652 i = 0;
2653 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2654 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2655 {
2656 i = (*mb_head_off)(ccline.cmdbuff,
2657 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2658 ccline.cmdpos -= i;
2659 len += i;
2660 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2661 }
2662# ifdef FEAT_ARABIC
2663 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2664 {
2665 /* Check the previous character for Arabic combining pair. */
2666 i = (*mb_head_off)(ccline.cmdbuff,
2667 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2668 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2669 + ccline.cmdpos - i), c))
2670 {
2671 ccline.cmdpos -= i;
2672 len += i;
2673 }
2674 else
2675 i = 0;
2676 }
2677# endif
2678 if (i != 0)
2679 {
2680 /* Also backup the cursor position. */
2681 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2682 ccline.cmdspos -= i;
2683 msg_col -= i;
2684 if (msg_col < 0)
2685 {
2686 msg_col += Columns;
2687 --msg_row;
2688 }
2689 }
2690 }
2691#endif
2692
2693 if (redraw && !cmd_silent)
2694 {
2695 msg_no_more = TRUE;
2696 i = cmdline_row;
2697 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2698 /* Avoid clearing the rest of the line too often. */
2699 if (cmdline_row != i || ccline.overstrike)
2700 msg_clr_eos();
2701 msg_no_more = FALSE;
2702 }
2703#ifdef FEAT_FKMAP
2704 /*
2705 * If we are in Farsi command mode, the character input must be in
2706 * Insert mode. So do not advance the cmdpos.
2707 */
2708 if (!cmd_fkmap)
2709#endif
2710 {
2711 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002712 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002714 if (m < 0) /* overflow, Columns or Rows at weird value */
2715 m = MAXCOL;
2716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 else
2718 m = MAXCOL;
2719 for (i = 0; i < len; ++i)
2720 {
2721 c = cmdline_charsize(ccline.cmdpos);
2722#ifdef FEAT_MBYTE
2723 /* count ">" for a double-wide char that doesn't fit. */
2724 if (has_mbyte)
2725 correct_cmdspos(ccline.cmdpos, c);
2726#endif
2727 /* Stop cursor at the end of the screen */
2728 if (ccline.cmdspos + c >= m)
2729 break;
2730 ccline.cmdspos += c;
2731#ifdef FEAT_MBYTE
2732 if (has_mbyte)
2733 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002734 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (c > len - i - 1)
2736 c = len - i - 1;
2737 ccline.cmdpos += c;
2738 i += c;
2739 }
2740#endif
2741 ++ccline.cmdpos;
2742 }
2743 }
2744 }
2745 if (redraw)
2746 msg_check();
2747 return retval;
2748}
2749
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002750static struct cmdline_info prev_ccline;
2751static int prev_ccline_used = FALSE;
2752
2753/*
2754 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2755 * and overwrite it. But get_cmdline_str() may need it, thus make it
2756 * available globally in prev_ccline.
2757 */
2758 static void
2759save_cmdline(ccp)
2760 struct cmdline_info *ccp;
2761{
2762 if (!prev_ccline_used)
2763 {
2764 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2765 prev_ccline_used = TRUE;
2766 }
2767 *ccp = prev_ccline;
2768 prev_ccline = ccline;
2769 ccline.cmdbuff = NULL;
2770 ccline.cmdprompt = NULL;
2771}
2772
2773/*
2774 * Resture ccline after it has been saved with save_cmdline().
2775 */
2776 static void
2777restore_cmdline(ccp)
2778 struct cmdline_info *ccp;
2779{
2780 ccline = prev_ccline;
2781 prev_ccline = *ccp;
2782}
2783
Bram Moolenaar8299df92004-07-10 09:47:34 +00002784/*
2785 * paste a yank register into the command line.
2786 * used by CTRL-R command in command-line mode
2787 * insert_reg() can't be used here, because special characters from the
2788 * register contents will be interpreted as commands.
2789 *
2790 * return FAIL for failure, OK otherwise
2791 */
2792 static int
2793cmdline_paste(regname, literally)
2794 int regname;
2795 int literally; /* Insert text literally instead of "as typed" */
2796{
2797 long i;
2798 char_u *arg;
2799 int allocated;
2800 struct cmdline_info save_ccline;
2801
2802 /* check for valid regname; also accept special characters for CTRL-R in
2803 * the command line */
2804 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
2805 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
2806 return FAIL;
2807
2808 /* A register containing CTRL-R can cause an endless loop. Allow using
2809 * CTRL-C to break the loop. */
2810 line_breakcheck();
2811 if (got_int)
2812 return FAIL;
2813
2814#ifdef FEAT_CLIPBOARD
2815 regname = may_get_selection(regname);
2816#endif
2817
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002818 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002819 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002820 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002821 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00002822 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002823 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002824 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00002825
2826 if (i)
2827 {
2828 /* Got the value of a special register in "arg". */
2829 if (arg == NULL)
2830 return FAIL;
2831 cmdline_paste_str(arg, literally);
2832 if (allocated)
2833 vim_free(arg);
2834 return OK;
2835 }
2836
2837 return cmdline_paste_reg(regname, literally);
2838}
2839
2840/*
2841 * Put a string on the command line.
2842 * When "literally" is TRUE, insert literally.
2843 * When "literally" is FALSE, insert as typed, but don't leave the command
2844 * line.
2845 */
2846 void
2847cmdline_paste_str(s, literally)
2848 char_u *s;
2849 int literally;
2850{
2851 int c, cv;
2852
2853 if (literally)
2854 put_on_cmdline(s, -1, TRUE);
2855 else
2856 while (*s != NUL)
2857 {
2858 cv = *s;
2859 if (cv == Ctrl_V && s[1])
2860 ++s;
2861#ifdef FEAT_MBYTE
2862 if (has_mbyte)
2863 {
2864 c = mb_ptr2char(s);
2865 s += mb_char2len(c);
2866 }
2867 else
2868#endif
2869 c = *s++;
2870 if (cv == Ctrl_V || c == ESC || c == Ctrl_C || c == CAR || c == NL
2871#ifdef UNIX
2872 || c == intr_char
2873#endif
2874 || (c == Ctrl_BSL && *s == Ctrl_N))
2875 stuffcharReadbuff(Ctrl_V);
2876 stuffcharReadbuff(c);
2877 }
2878}
2879
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880#ifdef FEAT_WILDMENU
2881/*
2882 * Delete characters on the command line, from "from" to the current
2883 * position.
2884 */
2885 static void
2886cmdline_del(from)
2887 int from;
2888{
2889 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
2890 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
2891 ccline.cmdlen -= ccline.cmdpos - from;
2892 ccline.cmdpos = from;
2893}
2894#endif
2895
2896/*
2897 * this fuction is called when the screen size changes and with incremental
2898 * search
2899 */
2900 void
2901redrawcmdline()
2902{
2903 if (cmd_silent)
2904 return;
2905 need_wait_return = FALSE;
2906 compute_cmdrow();
2907 redrawcmd();
2908 cursorcmd();
2909}
2910
2911 static void
2912redrawcmdprompt()
2913{
2914 int i;
2915
2916 if (cmd_silent)
2917 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002918 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 msg_putchar(ccline.cmdfirstc);
2920 if (ccline.cmdprompt != NULL)
2921 {
2922 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
2923 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
2924 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002925 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 --ccline.cmdindent;
2927 }
2928 else
2929 for (i = ccline.cmdindent; i > 0; --i)
2930 msg_putchar(' ');
2931}
2932
2933/*
2934 * Redraw what is currently on the command line.
2935 */
2936 void
2937redrawcmd()
2938{
2939 if (cmd_silent)
2940 return;
2941
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00002942 /* when 'incsearch' is set there may be no command line while redrawing */
2943 if (ccline.cmdbuff == NULL)
2944 {
2945 windgoto(cmdline_row, 0);
2946 msg_clr_eos();
2947 return;
2948 }
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 msg_start();
2951 redrawcmdprompt();
2952
2953 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2954 msg_no_more = TRUE;
2955 draw_cmdline(0, ccline.cmdlen);
2956 msg_clr_eos();
2957 msg_no_more = FALSE;
2958
2959 set_cmdspos_cursor();
2960
2961 /*
2962 * An emsg() before may have set msg_scroll. This is used in normal mode,
2963 * in cmdline mode we can reset them now.
2964 */
2965 msg_scroll = FALSE; /* next message overwrites cmdline */
2966
2967 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2968 * in cmdline mode */
2969 skip_redraw = FALSE;
2970}
2971
2972 void
2973compute_cmdrow()
2974{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002975 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 cmdline_row = Rows - 1;
2977 else
2978 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2979 + W_STATUS_HEIGHT(lastwin);
2980}
2981
2982 static void
2983cursorcmd()
2984{
2985 if (cmd_silent)
2986 return;
2987
2988#ifdef FEAT_RIGHTLEFT
2989 if (cmdmsg_rl)
2990 {
2991 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2992 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2993 if (msg_row <= 0)
2994 msg_row = Rows - 1;
2995 }
2996 else
2997#endif
2998 {
2999 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3000 msg_col = ccline.cmdspos % (int)Columns;
3001 if (msg_row >= Rows)
3002 msg_row = Rows - 1;
3003 }
3004
3005 windgoto(msg_row, msg_col);
3006#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3007 redrawcmd_preedit();
3008#endif
3009#ifdef MCH_CURSOR_SHAPE
3010 mch_update_cursor();
3011#endif
3012}
3013
3014 void
3015gotocmdline(clr)
3016 int clr;
3017{
3018 msg_start();
3019#ifdef FEAT_RIGHTLEFT
3020 if (cmdmsg_rl)
3021 msg_col = Columns - 1;
3022 else
3023#endif
3024 msg_col = 0; /* always start in column 0 */
3025 if (clr) /* clear the bottom line(s) */
3026 msg_clr_eos(); /* will reset clear_cmdline */
3027 windgoto(cmdline_row, 0);
3028}
3029
3030/*
3031 * Check the word in front of the cursor for an abbreviation.
3032 * Called when the non-id character "c" has been entered.
3033 * When an abbreviation is recognized it is removed from the text with
3034 * backspaces and the replacement string is inserted, followed by "c".
3035 */
3036 static int
3037ccheck_abbr(c)
3038 int c;
3039{
3040 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3041 return FALSE;
3042
3043 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3044}
3045
3046/*
3047 * Return FAIL if this is not an appropriate context in which to do
3048 * completion of anything, return OK if it is (even if there are no matches).
3049 * For the caller, this means that the character is just passed through like a
3050 * normal character (instead of being expanded). This allows :s/^I^D etc.
3051 */
3052 static int
3053nextwild(xp, type, options)
3054 expand_T *xp;
3055 int type;
3056 int options; /* extra options for ExpandOne() */
3057{
3058 int i, j;
3059 char_u *p1;
3060 char_u *p2;
3061 int oldlen;
3062 int difflen;
3063 int v;
3064
3065 if (xp->xp_numfiles == -1)
3066 {
3067 set_expand_context(xp);
3068 cmd_showtail = expand_showtail(xp);
3069 }
3070
3071 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3072 {
3073 beep_flush();
3074 return OK; /* Something illegal on command line */
3075 }
3076 if (xp->xp_context == EXPAND_NOTHING)
3077 {
3078 /* Caller can use the character as a normal char instead */
3079 return FAIL;
3080 }
3081
3082 MSG_PUTS("..."); /* show that we are busy */
3083 out_flush();
3084
3085 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3086 oldlen = ccline.cmdpos - i;
3087
3088 if (type == WILD_NEXT || type == WILD_PREV)
3089 {
3090 /*
3091 * Get next/previous match for a previous expanded pattern.
3092 */
3093 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3094 }
3095 else
3096 {
3097 /*
3098 * Translate string into pattern and expand it.
3099 */
3100 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3101 p2 = NULL;
3102 else
3103 {
3104 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3105 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3106 |options, type);
3107 vim_free(p1);
3108 /* longest match: make sure it is not shorter (happens with :help */
3109 if (p2 != NULL && type == WILD_LONGEST)
3110 {
3111 for (j = 0; j < oldlen; ++j)
3112 if (ccline.cmdbuff[i + j] == '*'
3113 || ccline.cmdbuff[i + j] == '?')
3114 break;
3115 if ((int)STRLEN(p2) < j)
3116 {
3117 vim_free(p2);
3118 p2 = NULL;
3119 }
3120 }
3121 }
3122 }
3123
3124 if (p2 != NULL && !got_int)
3125 {
3126 difflen = (int)STRLEN(p2) - oldlen;
3127 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3128 {
3129 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3130 xp->xp_pattern = ccline.cmdbuff + i;
3131 }
3132 else
3133 v = OK;
3134 if (v == OK)
3135 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003136 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3137 &ccline.cmdbuff[ccline.cmdpos],
3138 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3139 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 ccline.cmdlen += difflen;
3141 ccline.cmdpos += difflen;
3142 }
3143 }
3144 vim_free(p2);
3145
3146 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003147 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148
3149 /* When expanding a ":map" command and no matches are found, assume that
3150 * the key is supposed to be inserted literally */
3151 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3152 return FAIL;
3153
3154 if (xp->xp_numfiles <= 0 && p2 == NULL)
3155 beep_flush();
3156 else if (xp->xp_numfiles == 1)
3157 /* free expanded pattern */
3158 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3159
3160 return OK;
3161}
3162
3163/*
3164 * Do wildcard expansion on the string 'str'.
3165 * Chars that should not be expanded must be preceded with a backslash.
3166 * Return a pointer to alloced memory containing the new string.
3167 * Return NULL for failure.
3168 *
3169 * Results are cached in xp->xp_files and xp->xp_numfiles.
3170 *
3171 * mode = WILD_FREE: just free previously expanded matches
3172 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3173 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3174 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3175 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3176 * mode = WILD_ALL: return all matches concatenated
3177 * mode = WILD_LONGEST: return longest matched part
3178 *
3179 * options = WILD_LIST_NOTFOUND: list entries without a match
3180 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3181 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3182 * options = WILD_NO_BEEP: Don't beep for multiple matches
3183 * options = WILD_ADD_SLASH: add a slash after directory names
3184 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3185 * options = WILD_SILENT: don't print warning messages
3186 * options = WILD_ESCAPE: put backslash before special chars
3187 *
3188 * The variables xp->xp_context and xp->xp_backslash must have been set!
3189 */
3190 char_u *
3191ExpandOne(xp, str, orig, options, mode)
3192 expand_T *xp;
3193 char_u *str;
3194 char_u *orig; /* allocated copy of original of expanded string */
3195 int options;
3196 int mode;
3197{
3198 char_u *ss = NULL;
3199 static int findex;
3200 static char_u *orig_save = NULL; /* kept value of orig */
3201 int i;
3202 long_u len;
3203 int non_suf_match; /* number without matching suffix */
3204
3205 /*
3206 * first handle the case of using an old match
3207 */
3208 if (mode == WILD_NEXT || mode == WILD_PREV)
3209 {
3210 if (xp->xp_numfiles > 0)
3211 {
3212 if (mode == WILD_PREV)
3213 {
3214 if (findex == -1)
3215 findex = xp->xp_numfiles;
3216 --findex;
3217 }
3218 else /* mode == WILD_NEXT */
3219 ++findex;
3220
3221 /*
3222 * When wrapping around, return the original string, set findex to
3223 * -1.
3224 */
3225 if (findex < 0)
3226 {
3227 if (orig_save == NULL)
3228 findex = xp->xp_numfiles - 1;
3229 else
3230 findex = -1;
3231 }
3232 if (findex >= xp->xp_numfiles)
3233 {
3234 if (orig_save == NULL)
3235 findex = 0;
3236 else
3237 findex = -1;
3238 }
3239#ifdef FEAT_WILDMENU
3240 if (p_wmnu)
3241 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3242 findex, cmd_showtail);
3243#endif
3244 if (findex == -1)
3245 return vim_strsave(orig_save);
3246 return vim_strsave(xp->xp_files[findex]);
3247 }
3248 else
3249 return NULL;
3250 }
3251
3252/* free old names */
3253 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3254 {
3255 FreeWild(xp->xp_numfiles, xp->xp_files);
3256 xp->xp_numfiles = -1;
3257 vim_free(orig_save);
3258 orig_save = NULL;
3259 }
3260 findex = 0;
3261
3262 if (mode == WILD_FREE) /* only release file name */
3263 return NULL;
3264
3265 if (xp->xp_numfiles == -1)
3266 {
3267 vim_free(orig_save);
3268 orig_save = orig;
3269
3270 /*
3271 * Do the expansion.
3272 */
3273 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3274 options) == FAIL)
3275 {
3276#ifdef FNAME_ILLEGAL
3277 /* Illegal file name has been silently skipped. But when there
3278 * are wildcards, the real problem is that there was no match,
3279 * causing the pattern to be added, which has illegal characters.
3280 */
3281 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3282 EMSG2(_(e_nomatch2), str);
3283#endif
3284 }
3285 else if (xp->xp_numfiles == 0)
3286 {
3287 if (!(options & WILD_SILENT))
3288 EMSG2(_(e_nomatch2), str);
3289 }
3290 else
3291 {
3292 /* Escape the matches for use on the command line. */
3293 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3294
3295 /*
3296 * Check for matching suffixes in file names.
3297 */
3298 if (mode != WILD_ALL && mode != WILD_LONGEST)
3299 {
3300 if (xp->xp_numfiles)
3301 non_suf_match = xp->xp_numfiles;
3302 else
3303 non_suf_match = 1;
3304 if ((xp->xp_context == EXPAND_FILES
3305 || xp->xp_context == EXPAND_DIRECTORIES)
3306 && xp->xp_numfiles > 1)
3307 {
3308 /*
3309 * More than one match; check suffix.
3310 * The files will have been sorted on matching suffix in
3311 * expand_wildcards, only need to check the first two.
3312 */
3313 non_suf_match = 0;
3314 for (i = 0; i < 2; ++i)
3315 if (match_suffix(xp->xp_files[i]))
3316 ++non_suf_match;
3317 }
3318 if (non_suf_match != 1)
3319 {
3320 /* Can we ever get here unless it's while expanding
3321 * interactively? If not, we can get rid of this all
3322 * together. Don't really want to wait for this message
3323 * (and possibly have to hit return to continue!).
3324 */
3325 if (!(options & WILD_SILENT))
3326 EMSG(_(e_toomany));
3327 else if (!(options & WILD_NO_BEEP))
3328 beep_flush();
3329 }
3330 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3331 ss = vim_strsave(xp->xp_files[0]);
3332 }
3333 }
3334 }
3335
3336 /* Find longest common part */
3337 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3338 {
3339 for (len = 0; xp->xp_files[0][len]; ++len)
3340 {
3341 for (i = 0; i < xp->xp_numfiles; ++i)
3342 {
3343#ifdef CASE_INSENSITIVE_FILENAME
3344 if (xp->xp_context == EXPAND_DIRECTORIES
3345 || xp->xp_context == EXPAND_FILES
3346 || xp->xp_context == EXPAND_BUFFERS)
3347 {
3348 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3349 TOLOWER_LOC(xp->xp_files[0][len]))
3350 break;
3351 }
3352 else
3353#endif
3354 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3355 break;
3356 }
3357 if (i < xp->xp_numfiles)
3358 {
3359 if (!(options & WILD_NO_BEEP))
3360 vim_beep();
3361 break;
3362 }
3363 }
3364 ss = alloc((unsigned)len + 1);
3365 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003366 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 findex = -1; /* next p_wc gets first one */
3368 }
3369
3370 /* Concatenate all matching names */
3371 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3372 {
3373 len = 0;
3374 for (i = 0; i < xp->xp_numfiles; ++i)
3375 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3376 ss = lalloc(len, TRUE);
3377 if (ss != NULL)
3378 {
3379 *ss = NUL;
3380 for (i = 0; i < xp->xp_numfiles; ++i)
3381 {
3382 STRCAT(ss, xp->xp_files[i]);
3383 if (i != xp->xp_numfiles - 1)
3384 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3385 }
3386 }
3387 }
3388
3389 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3390 ExpandCleanup(xp);
3391
3392 return ss;
3393}
3394
3395/*
3396 * Prepare an expand structure for use.
3397 */
3398 void
3399ExpandInit(xp)
3400 expand_T *xp;
3401{
3402 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003403#ifndef BACKSLASH_IN_FILENAME
3404 xp->xp_shell = FALSE;
3405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 xp->xp_numfiles = -1;
3407 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003408#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3409 xp->xp_arg = NULL;
3410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
3414 * Cleanup an expand structure after use.
3415 */
3416 void
3417ExpandCleanup(xp)
3418 expand_T *xp;
3419{
3420 if (xp->xp_numfiles >= 0)
3421 {
3422 FreeWild(xp->xp_numfiles, xp->xp_files);
3423 xp->xp_numfiles = -1;
3424 }
3425}
3426
3427 void
3428ExpandEscape(xp, str, numfiles, files, options)
3429 expand_T *xp;
3430 char_u *str;
3431 int numfiles;
3432 char_u **files;
3433 int options;
3434{
3435 int i;
3436 char_u *p;
3437
3438 /*
3439 * May change home directory back to "~"
3440 */
3441 if (options & WILD_HOME_REPLACE)
3442 tilde_replace(str, numfiles, files);
3443
3444 if (options & WILD_ESCAPE)
3445 {
3446 if (xp->xp_context == EXPAND_FILES
3447 || xp->xp_context == EXPAND_BUFFERS
3448 || xp->xp_context == EXPAND_DIRECTORIES)
3449 {
3450 /*
3451 * Insert a backslash into a file name before a space, \, %, #
3452 * and wildmatch characters, except '~'.
3453 */
3454 for (i = 0; i < numfiles; ++i)
3455 {
3456 /* for ":set path=" we need to escape spaces twice */
3457 if (xp->xp_backslash == XP_BS_THREE)
3458 {
3459 p = vim_strsave_escaped(files[i], (char_u *)" ");
3460 if (p != NULL)
3461 {
3462 vim_free(files[i]);
3463 files[i] = p;
3464#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3465 p = vim_strsave_escaped(files[i], (char_u *)" ");
3466 if (p != NULL)
3467 {
3468 vim_free(files[i]);
3469 files[i] = p;
3470 }
3471#endif
3472 }
3473 }
3474#ifdef BACKSLASH_IN_FILENAME
3475 {
3476 char_u buf[20];
3477 int j = 0;
3478
3479 /* Don't escape '[' and '{' if they are in 'isfname'. */
3480 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3481 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3482 buf[j++] = *p;
3483 buf[j] = NUL;
3484 p = vim_strsave_escaped(files[i], buf);
3485 }
3486#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003487 p = vim_strsave_escaped(files[i],
3488 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489#endif
3490 if (p != NULL)
3491 {
3492 vim_free(files[i]);
3493 files[i] = p;
3494 }
3495
3496 /* If 'str' starts with "\~", replace "~" at start of
3497 * files[i] with "\~". */
3498 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003499 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 }
3501 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003502
3503 /* If the first file starts with a '+' escape it. Otherwise it
3504 * could be seen as "+cmd". */
3505 if (*files[0] == '+')
3506 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 }
3508 else if (xp->xp_context == EXPAND_TAGS)
3509 {
3510 /*
3511 * Insert a backslash before characters in a tag name that
3512 * would terminate the ":tag" command.
3513 */
3514 for (i = 0; i < numfiles; ++i)
3515 {
3516 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3517 if (p != NULL)
3518 {
3519 vim_free(files[i]);
3520 files[i] = p;
3521 }
3522 }
3523 }
3524 }
3525}
3526
3527/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003528 * Put a backslash before the file name in "pp", which is in allocated memory.
3529 */
3530 static void
3531escape_fname(pp)
3532 char_u **pp;
3533{
3534 char_u *p;
3535
3536 p = alloc((unsigned)(STRLEN(*pp) + 2));
3537 if (p != NULL)
3538 {
3539 p[0] = '\\';
3540 STRCPY(p + 1, *pp);
3541 vim_free(*pp);
3542 *pp = p;
3543 }
3544}
3545
3546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 * For each file name in files[num_files]:
3548 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3549 */
3550 void
3551tilde_replace(orig_pat, num_files, files)
3552 char_u *orig_pat;
3553 int num_files;
3554 char_u **files;
3555{
3556 int i;
3557 char_u *p;
3558
3559 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3560 {
3561 for (i = 0; i < num_files; ++i)
3562 {
3563 p = home_replace_save(NULL, files[i]);
3564 if (p != NULL)
3565 {
3566 vim_free(files[i]);
3567 files[i] = p;
3568 }
3569 }
3570 }
3571}
3572
3573/*
3574 * Show all matches for completion on the command line.
3575 * Returns EXPAND_NOTHING when the character that triggered expansion should
3576 * be inserted like a normal character.
3577 */
3578/*ARGSUSED*/
3579 static int
3580showmatches(xp, wildmenu)
3581 expand_T *xp;
3582 int wildmenu;
3583{
3584#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3585 int num_files;
3586 char_u **files_found;
3587 int i, j, k;
3588 int maxlen;
3589 int lines;
3590 int columns;
3591 char_u *p;
3592 int lastlen;
3593 int attr;
3594 int showtail;
3595
3596 if (xp->xp_numfiles == -1)
3597 {
3598 set_expand_context(xp);
3599 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3600 &num_files, &files_found);
3601 showtail = expand_showtail(xp);
3602 if (i != EXPAND_OK)
3603 return i;
3604
3605 }
3606 else
3607 {
3608 num_files = xp->xp_numfiles;
3609 files_found = xp->xp_files;
3610 showtail = cmd_showtail;
3611 }
3612
3613#ifdef FEAT_WILDMENU
3614 if (!wildmenu)
3615 {
3616#endif
3617 msg_didany = FALSE; /* lines_left will be set */
3618 msg_start(); /* prepare for paging */
3619 msg_putchar('\n');
3620 out_flush();
3621 cmdline_row = msg_row;
3622 msg_didany = FALSE; /* lines_left will be set again */
3623 msg_start(); /* prepare for paging */
3624#ifdef FEAT_WILDMENU
3625 }
3626#endif
3627
3628 if (got_int)
3629 got_int = FALSE; /* only int. the completion, not the cmd line */
3630#ifdef FEAT_WILDMENU
3631 else if (wildmenu)
3632 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3633#endif
3634 else
3635 {
3636 /* find the length of the longest file name */
3637 maxlen = 0;
3638 for (i = 0; i < num_files; ++i)
3639 {
3640 if (!showtail && (xp->xp_context == EXPAND_FILES
3641 || xp->xp_context == EXPAND_BUFFERS))
3642 {
3643 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3644 j = vim_strsize(NameBuff);
3645 }
3646 else
3647 j = vim_strsize(L_SHOWFILE(i));
3648 if (j > maxlen)
3649 maxlen = j;
3650 }
3651
3652 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3653 lines = num_files;
3654 else
3655 {
3656 /* compute the number of columns and lines for the listing */
3657 maxlen += 2; /* two spaces between file names */
3658 columns = ((int)Columns + 2) / maxlen;
3659 if (columns < 1)
3660 columns = 1;
3661 lines = (num_files + columns - 1) / columns;
3662 }
3663
3664 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3665
3666 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3667 {
3668 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3669 msg_clr_eos();
3670 msg_advance(maxlen - 3);
3671 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3672 }
3673
3674 /* list the files line by line */
3675 for (i = 0; i < lines; ++i)
3676 {
3677 lastlen = 999;
3678 for (k = i; k < num_files; k += lines)
3679 {
3680 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3681 {
3682 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3683 p = files_found[k] + STRLEN(files_found[k]) + 1;
3684 msg_advance(maxlen + 1);
3685 msg_puts(p);
3686 msg_advance(maxlen + 3);
3687 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3688 break;
3689 }
3690 for (j = maxlen - lastlen; --j >= 0; )
3691 msg_putchar(' ');
3692 if (xp->xp_context == EXPAND_FILES
3693 || xp->xp_context == EXPAND_BUFFERS)
3694 {
3695 /* highlight directories */
3696 j = (mch_isdir(files_found[k]));
3697 if (showtail)
3698 p = L_SHOWFILE(k);
3699 else
3700 {
3701 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3702 TRUE);
3703 p = NameBuff;
3704 }
3705 }
3706 else
3707 {
3708 j = FALSE;
3709 p = L_SHOWFILE(k);
3710 }
3711 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3712 }
3713 if (msg_col > 0) /* when not wrapped around */
3714 {
3715 msg_clr_eos();
3716 msg_putchar('\n');
3717 }
3718 out_flush(); /* show one line at a time */
3719 if (got_int)
3720 {
3721 got_int = FALSE;
3722 break;
3723 }
3724 }
3725
3726 /*
3727 * we redraw the command below the lines that we have just listed
3728 * This is a bit tricky, but it saves a lot of screen updating.
3729 */
3730 cmdline_row = msg_row; /* will put it back later */
3731 }
3732
3733 if (xp->xp_numfiles == -1)
3734 FreeWild(num_files, files_found);
3735
3736 return EXPAND_OK;
3737}
3738
3739/*
3740 * Private gettail for showmatches() (and win_redr_status_matches()):
3741 * Find tail of file name path, but ignore trailing "/".
3742 */
3743 char_u *
3744sm_gettail(s)
3745 char_u *s;
3746{
3747 char_u *p;
3748 char_u *t = s;
3749 int had_sep = FALSE;
3750
3751 for (p = s; *p != NUL; )
3752 {
3753 if (vim_ispathsep(*p)
3754#ifdef BACKSLASH_IN_FILENAME
3755 && !rem_backslash(p)
3756#endif
3757 )
3758 had_sep = TRUE;
3759 else if (had_sep)
3760 {
3761 t = p;
3762 had_sep = FALSE;
3763 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003764 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 }
3766 return t;
3767}
3768
3769/*
3770 * Return TRUE if we only need to show the tail of completion matches.
3771 * When not completing file names or there is a wildcard in the path FALSE is
3772 * returned.
3773 */
3774 static int
3775expand_showtail(xp)
3776 expand_T *xp;
3777{
3778 char_u *s;
3779 char_u *end;
3780
3781 /* When not completing file names a "/" may mean something different. */
3782 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3783 return FALSE;
3784
3785 end = gettail(xp->xp_pattern);
3786 if (end == xp->xp_pattern) /* there is no path separator */
3787 return FALSE;
3788
3789 for (s = xp->xp_pattern; s < end; s++)
3790 {
3791 /* Skip escaped wildcards. Only when the backslash is not a path
3792 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3793 if (rem_backslash(s))
3794 ++s;
3795 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3796 return FALSE;
3797 }
3798 return TRUE;
3799}
3800
3801/*
3802 * Prepare a string for expansion.
3803 * When expanding file names: The string will be used with expand_wildcards().
3804 * Copy the file name into allocated memory and add a '*' at the end.
3805 * When expanding other names: The string will be used with regcomp(). Copy
3806 * the name into allocated memory and prepend "^".
3807 */
3808 char_u *
3809addstar(fname, len, context)
3810 char_u *fname;
3811 int len;
3812 int context; /* EXPAND_FILES etc. */
3813{
3814 char_u *retval;
3815 int i, j;
3816 int new_len;
3817 char_u *tail;
3818
3819 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3820 {
3821 /*
3822 * Matching will be done internally (on something other than files).
3823 * So we convert the file-matching-type wildcards into our kind for
3824 * use with vim_regcomp(). First work out how long it will be:
3825 */
3826
3827 /* For help tags the translation is done in find_help_tags().
3828 * For a tag pattern starting with "/" no translation is needed. */
3829 if (context == EXPAND_HELP
3830 || context == EXPAND_COLORS
3831 || context == EXPAND_COMPILER
3832 || (context == EXPAND_TAGS && fname[0] == '/'))
3833 retval = vim_strnsave(fname, len);
3834 else
3835 {
3836 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3837 for (i = 0; i < len; i++)
3838 {
3839 if (fname[i] == '*' || fname[i] == '~')
3840 new_len++; /* '*' needs to be replaced by ".*"
3841 '~' needs to be replaced by "\~" */
3842
3843 /* Buffer names are like file names. "." should be literal */
3844 if (context == EXPAND_BUFFERS && fname[i] == '.')
3845 new_len++; /* "." becomes "\." */
3846
3847 /* Custom expansion takes care of special things, match
3848 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003849 if ((context == EXPAND_USER_DEFINED
3850 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 new_len++; /* '\' becomes "\\" */
3852 }
3853 retval = alloc(new_len);
3854 if (retval != NULL)
3855 {
3856 retval[0] = '^';
3857 j = 1;
3858 for (i = 0; i < len; i++, j++)
3859 {
3860 /* Skip backslash. But why? At least keep it for custom
3861 * expansion. */
3862 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003863 && context != EXPAND_USER_LIST
3864 && fname[i] == '\\'
3865 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 break;
3867
3868 switch (fname[i])
3869 {
3870 case '*': retval[j++] = '.';
3871 break;
3872 case '~': retval[j++] = '\\';
3873 break;
3874 case '?': retval[j] = '.';
3875 continue;
3876 case '.': if (context == EXPAND_BUFFERS)
3877 retval[j++] = '\\';
3878 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003879 case '\\': if (context == EXPAND_USER_DEFINED
3880 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 retval[j++] = '\\';
3882 break;
3883 }
3884 retval[j] = fname[i];
3885 }
3886 retval[j] = NUL;
3887 }
3888 }
3889 }
3890 else
3891 {
3892 retval = alloc(len + 4);
3893 if (retval != NULL)
3894 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003895 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896
3897 /*
3898 * Don't add a star to ~, ~user, $var or `cmd`.
3899 * ~ would be at the start of the file name, but not the tail.
3900 * $ could be anywhere in the tail.
3901 * ` could be anywhere in the file name.
3902 */
3903 tail = gettail(retval);
3904 if ((*retval != '~' || tail != retval)
3905 && vim_strchr(tail, '$') == NULL
3906 && vim_strchr(retval, '`') == NULL)
3907 retval[len++] = '*';
3908 retval[len] = NUL;
3909 }
3910 }
3911 return retval;
3912}
3913
3914/*
3915 * Must parse the command line so far to work out what context we are in.
3916 * Completion can then be done based on that context.
3917 * This routine sets the variables:
3918 * xp->xp_pattern The start of the pattern to be expanded within
3919 * the command line (ends at the cursor).
3920 * xp->xp_context The type of thing to expand. Will be one of:
3921 *
3922 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3923 * the command line, like an unknown command. Caller
3924 * should beep.
3925 * EXPAND_NOTHING Unrecognised context for completion, use char like
3926 * a normal char, rather than for completion. eg
3927 * :s/^I/
3928 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3929 * it.
3930 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3931 * EXPAND_FILES After command with XFILE set, or after setting
3932 * with P_EXPAND set. eg :e ^I, :w>>^I
3933 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3934 * when we know only directories are of interest. eg
3935 * :set dir=^I
3936 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3937 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3938 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3939 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3940 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3941 * EXPAND_EVENTS Complete event names
3942 * EXPAND_SYNTAX Complete :syntax command arguments
3943 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3944 * EXPAND_AUGROUP Complete autocommand group names
3945 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3946 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3947 * eg :unmap a^I , :cunab x^I
3948 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3949 * eg :call sub^I
3950 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3951 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3952 * names in expressions, eg :while s^I
3953 * EXPAND_ENV_VARS Complete environment variable names
3954 */
3955 static void
3956set_expand_context(xp)
3957 expand_T *xp;
3958{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003959 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (ccline.cmdfirstc != ':'
3961#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003962 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003963 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#endif
3965 )
3966 {
3967 xp->xp_context = EXPAND_NOTHING;
3968 return;
3969 }
3970 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3971}
3972
3973 void
3974set_cmd_context(xp, str, len, col)
3975 expand_T *xp;
3976 char_u *str; /* start of command line */
3977 int len; /* length of command line (excl. NUL) */
3978 int col; /* position of cursor */
3979{
3980 int old_char = NUL;
3981 char_u *nextcomm;
3982
3983 /*
3984 * Avoid a UMR warning from Purify, only save the character if it has been
3985 * written before.
3986 */
3987 if (col < len)
3988 old_char = str[col];
3989 str[col] = NUL;
3990 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003991
3992#ifdef FEAT_EVAL
3993 if (ccline.cmdfirstc == '=')
3994 /* pass CMD_SIZE because there is no real command */
3995 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003996 else if (ccline.input_fn)
3997 {
3998 xp->xp_context = ccline.xp_context;
3999 xp->xp_pattern = ccline.cmdbuff;
4000 xp->xp_arg = ccline.xp_arg;
4001 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004002 else
4003#endif
4004 while (nextcomm != NULL)
4005 nextcomm = set_one_cmd_context(xp, nextcomm);
4006
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 str[col] = old_char;
4008}
4009
4010/*
4011 * Expand the command line "str" from context "xp".
4012 * "xp" must have been set by set_cmd_context().
4013 * xp->xp_pattern points into "str", to where the text that is to be expanded
4014 * starts.
4015 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4016 * cursor.
4017 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4018 * key that triggered expansion literally.
4019 * Returns EXPAND_OK otherwise.
4020 */
4021 int
4022expand_cmdline(xp, str, col, matchcount, matches)
4023 expand_T *xp;
4024 char_u *str; /* start of command line */
4025 int col; /* position of cursor */
4026 int *matchcount; /* return: nr of matches */
4027 char_u ***matches; /* return: array of pointers to matches */
4028{
4029 char_u *file_str = NULL;
4030
4031 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4032 {
4033 beep_flush();
4034 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4035 }
4036 if (xp->xp_context == EXPAND_NOTHING)
4037 {
4038 /* Caller can use the character as a normal char instead */
4039 return EXPAND_NOTHING;
4040 }
4041
4042 /* add star to file name, or convert to regexp if not exp. files. */
4043 file_str = addstar(xp->xp_pattern,
4044 (int)(str + col - xp->xp_pattern), xp->xp_context);
4045 if (file_str == NULL)
4046 return EXPAND_UNSUCCESSFUL;
4047
4048 /* find all files that match the description */
4049 if (ExpandFromContext(xp, file_str, matchcount, matches,
4050 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4051 {
4052 *matchcount = 0;
4053 *matches = NULL;
4054 }
4055 vim_free(file_str);
4056
4057 return EXPAND_OK;
4058}
4059
4060#ifdef FEAT_MULTI_LANG
4061/*
4062 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4063 */
4064static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4065
4066 static void
4067cleanup_help_tags(num_file, file)
4068 int num_file;
4069 char_u **file;
4070{
4071 int i, j;
4072 int len;
4073
4074 for (i = 0; i < num_file; ++i)
4075 {
4076 len = (int)STRLEN(file[i]) - 3;
4077 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4078 {
4079 /* Sorting on priority means the same item in another language may
4080 * be anywhere. Search all items for a match up to the "@en". */
4081 for (j = 0; j < num_file; ++j)
4082 if (j != i
4083 && (int)STRLEN(file[j]) == len + 3
4084 && STRNCMP(file[i], file[j], len + 1) == 0)
4085 break;
4086 if (j == num_file)
4087 file[i][len] = NUL;
4088 }
4089 }
4090}
4091#endif
4092
4093/*
4094 * Do the expansion based on xp->xp_context and "pat".
4095 */
4096 static int
4097ExpandFromContext(xp, pat, num_file, file, options)
4098 expand_T *xp;
4099 char_u *pat;
4100 int *num_file;
4101 char_u ***file;
4102 int options;
4103{
4104#ifdef FEAT_CMDL_COMPL
4105 regmatch_T regmatch;
4106#endif
4107 int ret;
4108 int flags;
4109
4110 flags = EW_DIR; /* include directories */
4111 if (options & WILD_LIST_NOTFOUND)
4112 flags |= EW_NOTFOUND;
4113 if (options & WILD_ADD_SLASH)
4114 flags |= EW_ADDSLASH;
4115 if (options & WILD_KEEP_ALL)
4116 flags |= EW_KEEPALL;
4117 if (options & WILD_SILENT)
4118 flags |= EW_SILENT;
4119
4120 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4121 {
4122 /*
4123 * Expand file or directory names.
4124 */
4125 int free_pat = FALSE;
4126 int i;
4127
4128 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4129 * space */
4130 if (xp->xp_backslash != XP_BS_NONE)
4131 {
4132 free_pat = TRUE;
4133 pat = vim_strsave(pat);
4134 for (i = 0; pat[i]; ++i)
4135 if (pat[i] == '\\')
4136 {
4137 if (xp->xp_backslash == XP_BS_THREE
4138 && pat[i + 1] == '\\'
4139 && pat[i + 2] == '\\'
4140 && pat[i + 3] == ' ')
4141 STRCPY(pat + i, pat + i + 3);
4142 if (xp->xp_backslash == XP_BS_ONE
4143 && pat[i + 1] == ' ')
4144 STRCPY(pat + i, pat + i + 1);
4145 }
4146 }
4147
4148 if (xp->xp_context == EXPAND_FILES)
4149 flags |= EW_FILE;
4150 else
4151 flags = (flags | EW_DIR) & ~EW_FILE;
4152 ret = expand_wildcards(1, &pat, num_file, file, flags);
4153 if (free_pat)
4154 vim_free(pat);
4155 return ret;
4156 }
4157
4158 *file = (char_u **)"";
4159 *num_file = 0;
4160 if (xp->xp_context == EXPAND_HELP)
4161 {
4162 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4163 {
4164#ifdef FEAT_MULTI_LANG
4165 cleanup_help_tags(*num_file, *file);
4166#endif
4167 return OK;
4168 }
4169 return FAIL;
4170 }
4171
4172#ifndef FEAT_CMDL_COMPL
4173 return FAIL;
4174#else
4175 if (xp->xp_context == EXPAND_OLD_SETTING)
4176 return ExpandOldSetting(num_file, file);
4177 if (xp->xp_context == EXPAND_BUFFERS)
4178 return ExpandBufnames(pat, num_file, file, options);
4179 if (xp->xp_context == EXPAND_TAGS
4180 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4181 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4182 if (xp->xp_context == EXPAND_COLORS)
4183 return ExpandRTDir(pat, num_file, file, "colors");
4184 if (xp->xp_context == EXPAND_COMPILER)
4185 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004186# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4187 if (xp->xp_context == EXPAND_USER_LIST)
4188 return ExpandUserList(xp, num_file, file);
4189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
4191 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4192 if (regmatch.regprog == NULL)
4193 return FAIL;
4194
4195 /* set ignore-case according to p_ic, p_scs and pat */
4196 regmatch.rm_ic = ignorecase(pat);
4197
4198 if (xp->xp_context == EXPAND_SETTINGS
4199 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4200 ret = ExpandSettings(xp, &regmatch, num_file, file);
4201 else if (xp->xp_context == EXPAND_MAPPINGS)
4202 ret = ExpandMappings(&regmatch, num_file, file);
4203# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4204 else if (xp->xp_context == EXPAND_USER_DEFINED)
4205 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4206# endif
4207 else
4208 {
4209 static struct expgen
4210 {
4211 int context;
4212 char_u *((*func)__ARGS((expand_T *, int)));
4213 int ic;
4214 } tab[] =
4215 {
4216 {EXPAND_COMMANDS, get_command_name, FALSE},
4217#ifdef FEAT_USR_CMDS
4218 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4219 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4220 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4221 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4222#endif
4223#ifdef FEAT_EVAL
4224 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4225 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4226 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4227 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4228#endif
4229#ifdef FEAT_MENU
4230 {EXPAND_MENUS, get_menu_name, FALSE},
4231 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4232#endif
4233#ifdef FEAT_SYN_HL
4234 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4235#endif
4236 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4237#ifdef FEAT_AUTOCMD
4238 {EXPAND_EVENTS, get_event_name, TRUE},
4239 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4240#endif
4241#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4242 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4243 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4244#endif
4245 {EXPAND_ENV_VARS, get_env_name, TRUE},
4246 };
4247 int i;
4248
4249 /*
4250 * Find a context in the table and call the ExpandGeneric() with the
4251 * right function to do the expansion.
4252 */
4253 ret = FAIL;
4254 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4255 if (xp->xp_context == tab[i].context)
4256 {
4257 if (tab[i].ic)
4258 regmatch.rm_ic = TRUE;
4259 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4260 break;
4261 }
4262 }
4263
4264 vim_free(regmatch.regprog);
4265
4266 return ret;
4267#endif /* FEAT_CMDL_COMPL */
4268}
4269
4270#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4271/*
4272 * Expand a list of names.
4273 *
4274 * Generic function for command line completion. It calls a function to
4275 * obtain strings, one by one. The strings are matched against a regexp
4276 * program. Matching strings are copied into an array, which is returned.
4277 *
4278 * Returns OK when no problems encountered, FAIL for error (out of memory).
4279 */
4280 int
4281ExpandGeneric(xp, regmatch, num_file, file, func)
4282 expand_T *xp;
4283 regmatch_T *regmatch;
4284 int *num_file;
4285 char_u ***file;
4286 char_u *((*func)__ARGS((expand_T *, int)));
4287 /* returns a string from the list */
4288{
4289 int i;
4290 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004291 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 char_u *str;
4293
4294 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004295 * round == 0: count the number of matching names
4296 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004298 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 {
4300 for (i = 0; ; ++i)
4301 {
4302 str = (*func)(xp, i);
4303 if (str == NULL) /* end of list */
4304 break;
4305 if (*str == NUL) /* skip empty strings */
4306 continue;
4307
4308 if (vim_regexec(regmatch, str, (colnr_T)0))
4309 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004310 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 {
4312 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4313 (*file)[count] = str;
4314#ifdef FEAT_MENU
4315 if (func == get_menu_names && str != NULL)
4316 {
4317 /* test for separator added by get_menu_names() */
4318 str += STRLEN(str) - 1;
4319 if (*str == '\001')
4320 *str = '.';
4321 }
4322#endif
4323 }
4324 ++count;
4325 }
4326 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004327 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 {
4329 if (count == 0)
4330 return OK;
4331 *num_file = count;
4332 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4333 if (*file == NULL)
4334 {
4335 *file = (char_u **)"";
4336 return FAIL;
4337 }
4338 count = 0;
4339 }
4340 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004341
4342 /* Sort the results. */
4343 sort_strings(*file, *num_file);
4344
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 return OK;
4346}
4347
4348# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004349static 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));
4350
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004352 * call "user_expand_func()" to invoke a user defined VimL function and return
4353 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004355 static void *
4356call_user_expand_func(user_expand_func, xp, num_file, file)
4357 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 int *num_file;
4360 char_u ***file;
4361{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004362 char_u keep;
4363 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004366 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004367 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
4369 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004370 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 *num_file = 0;
4372 *file = NULL;
4373
4374 keep = ccline.cmdbuff[ccline.cmdlen];
4375 ccline.cmdbuff[ccline.cmdlen] = 0;
4376 sprintf((char *)num, "%d", ccline.cmdpos);
4377 args[0] = xp->xp_pattern;
4378 args[1] = ccline.cmdbuff;
4379 args[2] = num;
4380
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004381 /* Save the cmdline, we don't know what the function may do. */
4382 save_ccline = ccline;
4383 ccline.cmdbuff = NULL;
4384 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004386
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004387 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004388
4389 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004391
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004393
4394 return ret;
4395}
4396
4397/*
4398 * Expand names with a function defined by the user.
4399 */
4400 static int
4401ExpandUserDefined(xp, regmatch, num_file, file)
4402 expand_T *xp;
4403 regmatch_T *regmatch;
4404 int *num_file;
4405 char_u ***file;
4406{
4407 char_u *retstr;
4408 char_u *s;
4409 char_u *e;
4410 char_u keep;
4411 garray_T ga;
4412
4413 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4414 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FAIL;
4416
4417 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004418 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 {
4420 e = vim_strchr(s, '\n');
4421 if (e == NULL)
4422 e = s + STRLEN(s);
4423 keep = *e;
4424 *e = 0;
4425
4426 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4427 {
4428 *e = keep;
4429 if (*e != NUL)
4430 ++e;
4431 continue;
4432 }
4433
4434 if (ga_grow(&ga, 1) == FAIL)
4435 break;
4436
4437 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4438 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 *e = keep;
4441 if (*e != NUL)
4442 ++e;
4443 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004444 vim_free(retstr);
4445 *file = ga.ga_data;
4446 *num_file = ga.ga_len;
4447 return OK;
4448}
4449
4450/*
4451 * Expand names with a list returned by a function defined by the user.
4452 */
4453 static int
4454ExpandUserList(xp, num_file, file)
4455 expand_T *xp;
4456 int *num_file;
4457 char_u ***file;
4458{
4459 list_T *retlist;
4460 listitem_T *li;
4461 garray_T ga;
4462
4463 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4464 if (retlist == NULL)
4465 return FAIL;
4466
4467 ga_init2(&ga, (int)sizeof(char *), 3);
4468 /* Loop over the items in the list. */
4469 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4470 {
4471 if (li->li_tv.v_type != VAR_STRING)
4472 continue; /* Skip non-string items */
4473
4474 if (ga_grow(&ga, 1) == FAIL)
4475 break;
4476
4477 ((char_u **)ga.ga_data)[ga.ga_len] =
4478 vim_strsave(li->li_tv.vval.v_string);
4479 ++ga.ga_len;
4480 }
4481 list_unref(retlist);
4482
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 *file = ga.ga_data;
4484 *num_file = ga.ga_len;
4485 return OK;
4486}
4487#endif
4488
4489/*
4490 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4491 * or compiler names.
4492 */
4493 static int
4494ExpandRTDir(pat, num_file, file, dirname)
4495 char_u *pat;
4496 int *num_file;
4497 char_u ***file;
4498 char *dirname; /* "colors" or "compiler" */
4499{
4500 char_u *all;
4501 char_u *s;
4502 char_u *e;
4503 garray_T ga;
4504
4505 *num_file = 0;
4506 *file = NULL;
4507 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4508 if (s == NULL)
4509 return FAIL;
4510 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4511 all = globpath(p_rtp, s);
4512 vim_free(s);
4513 if (all == NULL)
4514 return FAIL;
4515
4516 ga_init2(&ga, (int)sizeof(char *), 3);
4517 for (s = all; *s != NUL; s = e)
4518 {
4519 e = vim_strchr(s, '\n');
4520 if (e == NULL)
4521 e = s + STRLEN(s);
4522 if (ga_grow(&ga, 1) == FAIL)
4523 break;
4524 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4525 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004526 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (*s == '\n' || vim_ispathsep(*s))
4528 break;
4529 ++s;
4530 ((char_u **)ga.ga_data)[ga.ga_len] =
4531 vim_strnsave(s, (int)(e - s - 4));
4532 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 }
4534 if (*e != NUL)
4535 ++e;
4536 }
4537 vim_free(all);
4538 *file = ga.ga_data;
4539 *num_file = ga.ga_len;
4540 return OK;
4541}
4542
4543#endif
4544
4545#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4546/*
4547 * Expand "file" for all comma-separated directories in "path".
4548 * Returns an allocated string with all matches concatenated, separated by
4549 * newlines. Returns NULL for an error or no matches.
4550 */
4551 char_u *
4552globpath(path, file)
4553 char_u *path;
4554 char_u *file;
4555{
4556 expand_T xpc;
4557 char_u *buf;
4558 garray_T ga;
4559 int i;
4560 int len;
4561 int num_p;
4562 char_u **p;
4563 char_u *cur = NULL;
4564
4565 buf = alloc(MAXPATHL);
4566 if (buf == NULL)
4567 return NULL;
4568
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004569 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004571
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 ga_init2(&ga, 1, 100);
4573
4574 /* Loop over all entries in {path}. */
4575 while (*path != NUL)
4576 {
4577 /* Copy one item of the path to buf[] and concatenate the file name. */
4578 copy_option_part(&path, buf, MAXPATHL, ",");
4579 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4580 {
4581 add_pathsep(buf);
4582 STRCAT(buf, file);
4583 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4584 && num_p > 0)
4585 {
4586 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4587 for (len = 0, i = 0; i < num_p; ++i)
4588 len += (long_u)STRLEN(p[i]) + 1;
4589
4590 /* Concatenate new results to previous ones. */
4591 if (ga_grow(&ga, len) == OK)
4592 {
4593 cur = (char_u *)ga.ga_data + ga.ga_len;
4594 for (i = 0; i < num_p; ++i)
4595 {
4596 STRCPY(cur, p[i]);
4597 cur += STRLEN(p[i]);
4598 *cur++ = '\n';
4599 }
4600 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602 FreeWild(num_p, p);
4603 }
4604 }
4605 }
4606 if (cur != NULL)
4607 *--cur = 0; /* Replace trailing newline with NUL */
4608
4609 vim_free(buf);
4610 return (char_u *)ga.ga_data;
4611}
4612
4613#endif
4614
4615#if defined(FEAT_CMDHIST) || defined(PROTO)
4616
4617/*********************************
4618 * Command line history stuff *
4619 *********************************/
4620
4621/*
4622 * Translate a history character to the associated type number.
4623 */
4624 static int
4625hist_char2type(c)
4626 int c;
4627{
4628 if (c == ':')
4629 return HIST_CMD;
4630 if (c == '=')
4631 return HIST_EXPR;
4632 if (c == '@')
4633 return HIST_INPUT;
4634 if (c == '>')
4635 return HIST_DEBUG;
4636 return HIST_SEARCH; /* must be '?' or '/' */
4637}
4638
4639/*
4640 * Table of history names.
4641 * These names are used in :history and various hist...() functions.
4642 * It is sufficient to give the significant prefix of a history name.
4643 */
4644
4645static char *(history_names[]) =
4646{
4647 "cmd",
4648 "search",
4649 "expr",
4650 "input",
4651 "debug",
4652 NULL
4653};
4654
4655/*
4656 * init_history() - Initialize the command line history.
4657 * Also used to re-allocate the history when the size changes.
4658 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004659 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660init_history()
4661{
4662 int newlen; /* new length of history table */
4663 histentry_T *temp;
4664 int i;
4665 int j;
4666 int type;
4667
4668 /*
4669 * If size of history table changed, reallocate it
4670 */
4671 newlen = (int)p_hi;
4672 if (newlen != hislen) /* history length changed */
4673 {
4674 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4675 {
4676 if (newlen)
4677 {
4678 temp = (histentry_T *)lalloc(
4679 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4680 if (temp == NULL) /* out of memory! */
4681 {
4682 if (type == 0) /* first one: just keep the old length */
4683 {
4684 newlen = hislen;
4685 break;
4686 }
4687 /* Already changed one table, now we can only have zero
4688 * length for all tables. */
4689 newlen = 0;
4690 type = -1;
4691 continue;
4692 }
4693 }
4694 else
4695 temp = NULL;
4696 if (newlen == 0 || temp != NULL)
4697 {
4698 if (hisidx[type] < 0) /* there are no entries yet */
4699 {
4700 for (i = 0; i < newlen; ++i)
4701 {
4702 temp[i].hisnum = 0;
4703 temp[i].hisstr = NULL;
4704 }
4705 }
4706 else if (newlen > hislen) /* array becomes bigger */
4707 {
4708 for (i = 0; i <= hisidx[type]; ++i)
4709 temp[i] = history[type][i];
4710 j = i;
4711 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4712 {
4713 temp[i].hisnum = 0;
4714 temp[i].hisstr = NULL;
4715 }
4716 for ( ; j < hislen; ++i, ++j)
4717 temp[i] = history[type][j];
4718 }
4719 else /* array becomes smaller or 0 */
4720 {
4721 j = hisidx[type];
4722 for (i = newlen - 1; ; --i)
4723 {
4724 if (i >= 0) /* copy newest entries */
4725 temp[i] = history[type][j];
4726 else /* remove older entries */
4727 vim_free(history[type][j].hisstr);
4728 if (--j < 0)
4729 j = hislen - 1;
4730 if (j == hisidx[type])
4731 break;
4732 }
4733 hisidx[type] = newlen - 1;
4734 }
4735 vim_free(history[type]);
4736 history[type] = temp;
4737 }
4738 }
4739 hislen = newlen;
4740 }
4741}
4742
4743/*
4744 * Check if command line 'str' is already in history.
4745 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4746 */
4747 static int
4748in_history(type, str, move_to_front)
4749 int type;
4750 char_u *str;
4751 int move_to_front; /* Move the entry to the front if it exists */
4752{
4753 int i;
4754 int last_i = -1;
4755
4756 if (hisidx[type] < 0)
4757 return FALSE;
4758 i = hisidx[type];
4759 do
4760 {
4761 if (history[type][i].hisstr == NULL)
4762 return FALSE;
4763 if (STRCMP(str, history[type][i].hisstr) == 0)
4764 {
4765 if (!move_to_front)
4766 return TRUE;
4767 last_i = i;
4768 break;
4769 }
4770 if (--i < 0)
4771 i = hislen - 1;
4772 } while (i != hisidx[type]);
4773
4774 if (last_i >= 0)
4775 {
4776 str = history[type][i].hisstr;
4777 while (i != hisidx[type])
4778 {
4779 if (++i >= hislen)
4780 i = 0;
4781 history[type][last_i] = history[type][i];
4782 last_i = i;
4783 }
4784 history[type][i].hisstr = str;
4785 history[type][i].hisnum = ++hisnum[type];
4786 return TRUE;
4787 }
4788 return FALSE;
4789}
4790
4791/*
4792 * Convert history name (from table above) to its HIST_ equivalent.
4793 * When "name" is empty, return "cmd" history.
4794 * Returns -1 for unknown history name.
4795 */
4796 int
4797get_histtype(name)
4798 char_u *name;
4799{
4800 int i;
4801 int len = (int)STRLEN(name);
4802
4803 /* No argument: use current history. */
4804 if (len == 0)
4805 return hist_char2type(ccline.cmdfirstc);
4806
4807 for (i = 0; history_names[i] != NULL; ++i)
4808 if (STRNICMP(name, history_names[i], len) == 0)
4809 return i;
4810
4811 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4812 return hist_char2type(name[0]);
4813
4814 return -1;
4815}
4816
4817static int last_maptick = -1; /* last seen maptick */
4818
4819/*
4820 * Add the given string to the given history. If the string is already in the
4821 * history then it is moved to the front. "histype" may be one of he HIST_
4822 * values.
4823 */
4824 void
4825add_to_history(histype, new_entry, in_map, sep)
4826 int histype;
4827 char_u *new_entry;
4828 int in_map; /* consider maptick when inside a mapping */
4829 int sep; /* separator character used (search hist) */
4830{
4831 histentry_T *hisptr;
4832 int len;
4833
4834 if (hislen == 0) /* no history */
4835 return;
4836
4837 /*
4838 * Searches inside the same mapping overwrite each other, so that only
4839 * the last line is kept. Be careful not to remove a line that was moved
4840 * down, only lines that were added.
4841 */
4842 if (histype == HIST_SEARCH && in_map)
4843 {
4844 if (maptick == last_maptick)
4845 {
4846 /* Current line is from the same mapping, remove it */
4847 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4848 vim_free(hisptr->hisstr);
4849 hisptr->hisstr = NULL;
4850 hisptr->hisnum = 0;
4851 --hisnum[histype];
4852 if (--hisidx[HIST_SEARCH] < 0)
4853 hisidx[HIST_SEARCH] = hislen - 1;
4854 }
4855 last_maptick = -1;
4856 }
4857 if (!in_history(histype, new_entry, TRUE))
4858 {
4859 if (++hisidx[histype] == hislen)
4860 hisidx[histype] = 0;
4861 hisptr = &history[histype][hisidx[histype]];
4862 vim_free(hisptr->hisstr);
4863
4864 /* Store the separator after the NUL of the string. */
4865 len = STRLEN(new_entry);
4866 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4867 if (hisptr->hisstr != NULL)
4868 hisptr->hisstr[len + 1] = sep;
4869
4870 hisptr->hisnum = ++hisnum[histype];
4871 if (histype == HIST_SEARCH && in_map)
4872 last_maptick = maptick;
4873 }
4874}
4875
4876#if defined(FEAT_EVAL) || defined(PROTO)
4877
4878/*
4879 * Get identifier of newest history entry.
4880 * "histype" may be one of the HIST_ values.
4881 */
4882 int
4883get_history_idx(histype)
4884 int histype;
4885{
4886 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4887 || hisidx[histype] < 0)
4888 return -1;
4889
4890 return history[histype][hisidx[histype]].hisnum;
4891}
4892
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004893static struct cmdline_info *get_ccline_ptr __ARGS((void));
4894
4895/*
4896 * Get pointer to the command line info to use. cmdline_paste() may clear
4897 * ccline and put the previous value in prev_ccline.
4898 */
4899 static struct cmdline_info *
4900get_ccline_ptr()
4901{
4902 if ((State & CMDLINE) == 0)
4903 return NULL;
4904 if (ccline.cmdbuff != NULL)
4905 return &ccline;
4906 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4907 return &prev_ccline;
4908 return NULL;
4909}
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911/*
4912 * Get the current command line in allocated memory.
4913 * Only works when the command line is being edited.
4914 * Returns NULL when something is wrong.
4915 */
4916 char_u *
4917get_cmdline_str()
4918{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004919 struct cmdline_info *p = get_ccline_ptr();
4920
4921 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004923 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924}
4925
4926/*
4927 * Get the current command line position, counted in bytes.
4928 * Zero is the first position.
4929 * Only works when the command line is being edited.
4930 * Returns -1 when something is wrong.
4931 */
4932 int
4933get_cmdline_pos()
4934{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004935 struct cmdline_info *p = get_ccline_ptr();
4936
4937 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004939 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940}
4941
4942/*
4943 * Set the command line byte position to "pos". Zero is the first position.
4944 * Only works when the command line is being edited.
4945 * Returns 1 when failed, 0 when OK.
4946 */
4947 int
4948set_cmdline_pos(pos)
4949 int pos;
4950{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004951 struct cmdline_info *p = get_ccline_ptr();
4952
4953 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return 1;
4955
4956 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4957 * changed the command line. */
4958 if (pos < 0)
4959 new_cmdpos = 0;
4960 else
4961 new_cmdpos = pos;
4962 return 0;
4963}
4964
4965/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004966 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00004967 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004968 * Only works when the command line is being edited.
4969 * Returns NUL when something is wrong.
4970 */
4971 int
4972get_cmdline_type()
4973{
4974 struct cmdline_info *p = get_ccline_ptr();
4975
4976 if (p == NULL)
4977 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00004978 if (p->cmdfirstc == NUL)
4979 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004980 return p->cmdfirstc;
4981}
4982
4983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 * Calculate history index from a number:
4985 * num > 0: seen as identifying number of a history entry
4986 * num < 0: relative position in history wrt newest entry
4987 * "histype" may be one of the HIST_ values.
4988 */
4989 static int
4990calc_hist_idx(histype, num)
4991 int histype;
4992 int num;
4993{
4994 int i;
4995 histentry_T *hist;
4996 int wrapped = FALSE;
4997
4998 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4999 || (i = hisidx[histype]) < 0 || num == 0)
5000 return -1;
5001
5002 hist = history[histype];
5003 if (num > 0)
5004 {
5005 while (hist[i].hisnum > num)
5006 if (--i < 0)
5007 {
5008 if (wrapped)
5009 break;
5010 i += hislen;
5011 wrapped = TRUE;
5012 }
5013 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5014 return i;
5015 }
5016 else if (-num <= hislen)
5017 {
5018 i += num + 1;
5019 if (i < 0)
5020 i += hislen;
5021 if (hist[i].hisstr != NULL)
5022 return i;
5023 }
5024 return -1;
5025}
5026
5027/*
5028 * Get a history entry by its index.
5029 * "histype" may be one of the HIST_ values.
5030 */
5031 char_u *
5032get_history_entry(histype, idx)
5033 int histype;
5034 int idx;
5035{
5036 idx = calc_hist_idx(histype, idx);
5037 if (idx >= 0)
5038 return history[histype][idx].hisstr;
5039 else
5040 return (char_u *)"";
5041}
5042
5043/*
5044 * Clear all entries of a history.
5045 * "histype" may be one of the HIST_ values.
5046 */
5047 int
5048clr_history(histype)
5049 int histype;
5050{
5051 int i;
5052 histentry_T *hisptr;
5053
5054 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5055 {
5056 hisptr = history[histype];
5057 for (i = hislen; i--;)
5058 {
5059 vim_free(hisptr->hisstr);
5060 hisptr->hisnum = 0;
5061 hisptr++->hisstr = NULL;
5062 }
5063 hisidx[histype] = -1; /* mark history as cleared */
5064 hisnum[histype] = 0; /* reset identifier counter */
5065 return OK;
5066 }
5067 return FAIL;
5068}
5069
5070/*
5071 * Remove all entries matching {str} from a history.
5072 * "histype" may be one of the HIST_ values.
5073 */
5074 int
5075del_history_entry(histype, str)
5076 int histype;
5077 char_u *str;
5078{
5079 regmatch_T regmatch;
5080 histentry_T *hisptr;
5081 int idx;
5082 int i;
5083 int last;
5084 int found = FALSE;
5085
5086 regmatch.regprog = NULL;
5087 regmatch.rm_ic = FALSE; /* always match case */
5088 if (hislen != 0
5089 && histype >= 0
5090 && histype < HIST_COUNT
5091 && *str != NUL
5092 && (idx = hisidx[histype]) >= 0
5093 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5094 != NULL)
5095 {
5096 i = last = idx;
5097 do
5098 {
5099 hisptr = &history[histype][i];
5100 if (hisptr->hisstr == NULL)
5101 break;
5102 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5103 {
5104 found = TRUE;
5105 vim_free(hisptr->hisstr);
5106 hisptr->hisstr = NULL;
5107 hisptr->hisnum = 0;
5108 }
5109 else
5110 {
5111 if (i != last)
5112 {
5113 history[histype][last] = *hisptr;
5114 hisptr->hisstr = NULL;
5115 hisptr->hisnum = 0;
5116 }
5117 if (--last < 0)
5118 last += hislen;
5119 }
5120 if (--i < 0)
5121 i += hislen;
5122 } while (i != idx);
5123 if (history[histype][idx].hisstr == NULL)
5124 hisidx[histype] = -1;
5125 }
5126 vim_free(regmatch.regprog);
5127 return found;
5128}
5129
5130/*
5131 * Remove an indexed entry from a history.
5132 * "histype" may be one of the HIST_ values.
5133 */
5134 int
5135del_history_idx(histype, idx)
5136 int histype;
5137 int idx;
5138{
5139 int i, j;
5140
5141 i = calc_hist_idx(histype, idx);
5142 if (i < 0)
5143 return FALSE;
5144 idx = hisidx[histype];
5145 vim_free(history[histype][i].hisstr);
5146
5147 /* When deleting the last added search string in a mapping, reset
5148 * last_maptick, so that the last added search string isn't deleted again.
5149 */
5150 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5151 last_maptick = -1;
5152
5153 while (i != idx)
5154 {
5155 j = (i + 1) % hislen;
5156 history[histype][i] = history[histype][j];
5157 i = j;
5158 }
5159 history[histype][i].hisstr = NULL;
5160 history[histype][i].hisnum = 0;
5161 if (--i < 0)
5162 i += hislen;
5163 hisidx[histype] = i;
5164 return TRUE;
5165}
5166
5167#endif /* FEAT_EVAL */
5168
5169#if defined(FEAT_CRYPT) || defined(PROTO)
5170/*
5171 * Very specific function to remove the value in ":set key=val" from the
5172 * history.
5173 */
5174 void
5175remove_key_from_history()
5176{
5177 char_u *p;
5178 int i;
5179
5180 i = hisidx[HIST_CMD];
5181 if (i < 0)
5182 return;
5183 p = history[HIST_CMD][i].hisstr;
5184 if (p != NULL)
5185 for ( ; *p; ++p)
5186 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5187 {
5188 p = vim_strchr(p + 3, '=');
5189 if (p == NULL)
5190 break;
5191 ++p;
5192 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5193 if (p[i] == '\\' && p[i + 1])
5194 ++i;
5195 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5196 --p;
5197 }
5198}
5199#endif
5200
5201#endif /* FEAT_CMDHIST */
5202
5203#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5204/*
5205 * Get indices "num1,num2" that specify a range within a list (not a range of
5206 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5207 * Returns OK if parsed successfully, otherwise FAIL.
5208 */
5209 int
5210get_list_range(str, num1, num2)
5211 char_u **str;
5212 int *num1;
5213 int *num2;
5214{
5215 int len;
5216 int first = FALSE;
5217 long num;
5218
5219 *str = skipwhite(*str);
5220 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5221 {
5222 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5223 *str += len;
5224 *num1 = (int)num;
5225 first = TRUE;
5226 }
5227 *str = skipwhite(*str);
5228 if (**str == ',') /* parse "to" part of range */
5229 {
5230 *str = skipwhite(*str + 1);
5231 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5232 if (len > 0)
5233 {
5234 *num2 = (int)num;
5235 *str = skipwhite(*str + len);
5236 }
5237 else if (!first) /* no number given at all */
5238 return FAIL;
5239 }
5240 else if (first) /* only one number given */
5241 *num2 = *num1;
5242 return OK;
5243}
5244#endif
5245
5246#if defined(FEAT_CMDHIST) || defined(PROTO)
5247/*
5248 * :history command - print a history
5249 */
5250 void
5251ex_history(eap)
5252 exarg_T *eap;
5253{
5254 histentry_T *hist;
5255 int histype1 = HIST_CMD;
5256 int histype2 = HIST_CMD;
5257 int hisidx1 = 1;
5258 int hisidx2 = -1;
5259 int idx;
5260 int i, j, k;
5261 char_u *end;
5262 char_u *arg = eap->arg;
5263
5264 if (hislen == 0)
5265 {
5266 MSG(_("'history' option is zero"));
5267 return;
5268 }
5269
5270 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5271 {
5272 end = arg;
5273 while (ASCII_ISALPHA(*end)
5274 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5275 end++;
5276 i = *end;
5277 *end = NUL;
5278 histype1 = get_histtype(arg);
5279 if (histype1 == -1)
5280 {
5281 if (STRICMP(arg, "all") == 0)
5282 {
5283 histype1 = 0;
5284 histype2 = HIST_COUNT-1;
5285 }
5286 else
5287 {
5288 *end = i;
5289 EMSG(_(e_trailing));
5290 return;
5291 }
5292 }
5293 else
5294 histype2 = histype1;
5295 *end = i;
5296 }
5297 else
5298 end = arg;
5299 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5300 {
5301 EMSG(_(e_trailing));
5302 return;
5303 }
5304
5305 for (; !got_int && histype1 <= histype2; ++histype1)
5306 {
5307 STRCPY(IObuff, "\n # ");
5308 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5309 MSG_PUTS_TITLE(IObuff);
5310 idx = hisidx[histype1];
5311 hist = history[histype1];
5312 j = hisidx1;
5313 k = hisidx2;
5314 if (j < 0)
5315 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5316 if (k < 0)
5317 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5318 if (idx >= 0 && j <= k)
5319 for (i = idx + 1; !got_int; ++i)
5320 {
5321 if (i == hislen)
5322 i = 0;
5323 if (hist[i].hisstr != NULL
5324 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5325 {
5326 msg_putchar('\n');
5327 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5328 hist[i].hisnum);
5329 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5330 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5331 (int)Columns - 10);
5332 else
5333 STRCAT(IObuff, hist[i].hisstr);
5334 msg_outtrans(IObuff);
5335 out_flush();
5336 }
5337 if (i == idx)
5338 break;
5339 }
5340 }
5341}
5342#endif
5343
5344#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5345static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5346static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5347static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5348static int viminfo_add_at_front = FALSE;
5349
5350static int hist_type2char __ARGS((int type, int use_question));
5351
5352/*
5353 * Translate a history type number to the associated character.
5354 */
5355 static int
5356hist_type2char(type, use_question)
5357 int type;
5358 int use_question; /* use '?' instead of '/' */
5359{
5360 if (type == HIST_CMD)
5361 return ':';
5362 if (type == HIST_SEARCH)
5363 {
5364 if (use_question)
5365 return '?';
5366 else
5367 return '/';
5368 }
5369 if (type == HIST_EXPR)
5370 return '=';
5371 return '@';
5372}
5373
5374/*
5375 * Prepare for reading the history from the viminfo file.
5376 * This allocates history arrays to store the read history lines.
5377 */
5378 void
5379prepare_viminfo_history(asklen)
5380 int asklen;
5381{
5382 int i;
5383 int num;
5384 int type;
5385 int len;
5386
5387 init_history();
5388 viminfo_add_at_front = (asklen != 0);
5389 if (asklen > hislen)
5390 asklen = hislen;
5391
5392 for (type = 0; type < HIST_COUNT; ++type)
5393 {
5394 /*
5395 * Count the number of empty spaces in the history list. If there are
5396 * more spaces available than we request, then fill them up.
5397 */
5398 for (i = 0, num = 0; i < hislen; i++)
5399 if (history[type][i].hisstr == NULL)
5400 num++;
5401 len = asklen;
5402 if (num > len)
5403 len = num;
5404 if (len <= 0)
5405 viminfo_history[type] = NULL;
5406 else
5407 viminfo_history[type] =
5408 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5409 if (viminfo_history[type] == NULL)
5410 len = 0;
5411 viminfo_hislen[type] = len;
5412 viminfo_hisidx[type] = 0;
5413 }
5414}
5415
5416/*
5417 * Accept a line from the viminfo, store it in the history array when it's
5418 * new.
5419 */
5420 int
5421read_viminfo_history(virp)
5422 vir_T *virp;
5423{
5424 int type;
5425 long_u len;
5426 char_u *val;
5427 char_u *p;
5428
5429 type = hist_char2type(virp->vir_line[0]);
5430 if (viminfo_hisidx[type] < viminfo_hislen[type])
5431 {
5432 val = viminfo_readstring(virp, 1, TRUE);
5433 if (val != NULL && *val != NUL)
5434 {
5435 if (!in_history(type, val + (type == HIST_SEARCH),
5436 viminfo_add_at_front))
5437 {
5438 /* Need to re-allocate to append the separator byte. */
5439 len = STRLEN(val);
5440 p = lalloc(len + 2, TRUE);
5441 if (p != NULL)
5442 {
5443 if (type == HIST_SEARCH)
5444 {
5445 /* Search entry: Move the separator from the first
5446 * column to after the NUL. */
5447 mch_memmove(p, val + 1, (size_t)len);
5448 p[len] = (*val == ' ' ? NUL : *val);
5449 }
5450 else
5451 {
5452 /* Not a search entry: No separator in the viminfo
5453 * file, add a NUL separator. */
5454 mch_memmove(p, val, (size_t)len + 1);
5455 p[len + 1] = NUL;
5456 }
5457 viminfo_history[type][viminfo_hisidx[type]++] = p;
5458 }
5459 }
5460 }
5461 vim_free(val);
5462 }
5463 return viminfo_readline(virp);
5464}
5465
5466 void
5467finish_viminfo_history()
5468{
5469 int idx;
5470 int i;
5471 int type;
5472
5473 for (type = 0; type < HIST_COUNT; ++type)
5474 {
5475 if (history[type] == NULL)
5476 return;
5477 idx = hisidx[type] + viminfo_hisidx[type];
5478 if (idx >= hislen)
5479 idx -= hislen;
5480 else if (idx < 0)
5481 idx = hislen - 1;
5482 if (viminfo_add_at_front)
5483 hisidx[type] = idx;
5484 else
5485 {
5486 if (hisidx[type] == -1)
5487 hisidx[type] = hislen - 1;
5488 do
5489 {
5490 if (history[type][idx].hisstr != NULL)
5491 break;
5492 if (++idx == hislen)
5493 idx = 0;
5494 } while (idx != hisidx[type]);
5495 if (idx != hisidx[type] && --idx < 0)
5496 idx = hislen - 1;
5497 }
5498 for (i = 0; i < viminfo_hisidx[type]; i++)
5499 {
5500 vim_free(history[type][idx].hisstr);
5501 history[type][idx].hisstr = viminfo_history[type][i];
5502 if (--idx < 0)
5503 idx = hislen - 1;
5504 }
5505 idx += 1;
5506 idx %= hislen;
5507 for (i = 0; i < viminfo_hisidx[type]; i++)
5508 {
5509 history[type][idx++].hisnum = ++hisnum[type];
5510 idx %= hislen;
5511 }
5512 vim_free(viminfo_history[type]);
5513 viminfo_history[type] = NULL;
5514 }
5515}
5516
5517 void
5518write_viminfo_history(fp)
5519 FILE *fp;
5520{
5521 int i;
5522 int type;
5523 int num_saved;
5524 char_u *p;
5525 int c;
5526
5527 init_history();
5528 if (hislen == 0)
5529 return;
5530 for (type = 0; type < HIST_COUNT; ++type)
5531 {
5532 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5533 if (num_saved == 0)
5534 continue;
5535 if (num_saved < 0) /* Use default */
5536 num_saved = hislen;
5537 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5538 type == HIST_CMD ? _("Command Line") :
5539 type == HIST_SEARCH ? _("Search String") :
5540 type == HIST_EXPR ? _("Expression") :
5541 _("Input Line"));
5542 if (num_saved > hislen)
5543 num_saved = hislen;
5544 i = hisidx[type];
5545 if (i >= 0)
5546 while (num_saved--)
5547 {
5548 p = history[type][i].hisstr;
5549 if (p != NULL)
5550 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005551 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 /* For the search history: put the separator in the second
5553 * column; use a space if there isn't one. */
5554 if (type == HIST_SEARCH)
5555 {
5556 c = p[STRLEN(p) + 1];
5557 putc(c == NUL ? ' ' : c, fp);
5558 }
5559 viminfo_writestring(fp, p);
5560 }
5561 if (--i < 0)
5562 i = hislen - 1;
5563 }
5564 }
5565}
5566#endif /* FEAT_VIMINFO */
5567
5568#if defined(FEAT_FKMAP) || defined(PROTO)
5569/*
5570 * Write a character at the current cursor+offset position.
5571 * It is directly written into the command buffer block.
5572 */
5573 void
5574cmd_pchar(c, offset)
5575 int c, offset;
5576{
5577 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5578 {
5579 EMSG(_("E198: cmd_pchar beyond the command length"));
5580 return;
5581 }
5582 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5583 ccline.cmdbuff[ccline.cmdlen] = NUL;
5584}
5585
5586 int
5587cmd_gchar(offset)
5588 int offset;
5589{
5590 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5591 {
5592 /* EMSG(_("cmd_gchar beyond the command length")); */
5593 return NUL;
5594 }
5595 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5596}
5597#endif
5598
5599#if defined(FEAT_CMDWIN) || defined(PROTO)
5600/*
5601 * Open a window on the current command line and history. Allow editing in
5602 * the window. Returns when the window is closed.
5603 * Returns:
5604 * CR if the command is to be executed
5605 * Ctrl_C if it is to be abandoned
5606 * K_IGNORE if editing continues
5607 */
5608 static int
5609ex_window()
5610{
5611 struct cmdline_info save_ccline;
5612 buf_T *old_curbuf = curbuf;
5613 win_T *old_curwin = curwin;
5614 buf_T *bp;
5615 win_T *wp;
5616 int i;
5617 linenr_T lnum;
5618 int histtype;
5619 garray_T winsizes;
5620 char_u typestr[2];
5621 int save_restart_edit = restart_edit;
5622 int save_State = State;
5623 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005624#ifdef FEAT_RIGHTLEFT
5625 int save_cmdmsg_rl = cmdmsg_rl;
5626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
5628 /* Can't do this recursively. Can't do it when typing a password. */
5629 if (cmdwin_type != 0
5630# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5631 || cmdline_star > 0
5632# endif
5633 )
5634 {
5635 beep_flush();
5636 return K_IGNORE;
5637 }
5638
5639 /* Save current window sizes. */
5640 win_size_save(&winsizes);
5641
5642# ifdef FEAT_AUTOCMD
5643 /* Don't execute autocommands while creating the window. */
5644 ++autocmd_block;
5645# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00005646 /* don't use a new tab page */
5647 cmdmod.tab = 0;
5648
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 /* Create a window for the command-line buffer. */
5650 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5651 {
5652 beep_flush();
5653 return K_IGNORE;
5654 }
5655 cmdwin_type = ccline.cmdfirstc;
5656 if (cmdwin_type == NUL)
5657 cmdwin_type = '-';
5658
5659 /* Create the command-line buffer empty. */
5660 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5661 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5662 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5663 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5664 curbuf->b_p_ma = TRUE;
5665# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005666 curwin->w_p_rl = cmdmsg_rl;
5667 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668# endif
5669# ifdef FEAT_SCROLLBIND
5670 curwin->w_p_scb = FALSE;
5671# endif
5672
5673# ifdef FEAT_AUTOCMD
5674 /* Do execute autocommands for setting the filetype (load syntax). */
5675 --autocmd_block;
5676# endif
5677
Bram Moolenaar46152342005-09-07 21:18:43 +00005678 /* Showing the prompt may have set need_wait_return, reset it. */
5679 need_wait_return = FALSE;
5680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 histtype = hist_char2type(ccline.cmdfirstc);
5682 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5683 {
5684 if (p_wc == TAB)
5685 {
5686 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5687 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5688 }
5689 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5690 }
5691
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005692 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5693 * sets 'textwidth' to 78). */
5694 curbuf->b_p_tw = 0;
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* Fill the buffer with the history. */
5697 init_history();
5698 if (hislen > 0)
5699 {
5700 i = hisidx[histtype];
5701 if (i >= 0)
5702 {
5703 lnum = 0;
5704 do
5705 {
5706 if (++i == hislen)
5707 i = 0;
5708 if (history[histtype][i].hisstr != NULL)
5709 ml_append(lnum++, history[histtype][i].hisstr,
5710 (colnr_T)0, FALSE);
5711 }
5712 while (i != hisidx[histtype]);
5713 }
5714 }
5715
5716 /* Replace the empty last line with the current command-line and put the
5717 * cursor there. */
5718 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5719 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5720 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005721 changed_line_abv_curs();
5722 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 redraw_later(NOT_VALID);
5724
5725 /* Save the command line info, can be used recursively. */
5726 save_ccline = ccline;
5727 ccline.cmdbuff = NULL;
5728 ccline.cmdprompt = NULL;
5729
5730 /* No Ex mode here! */
5731 exmode_active = 0;
5732
5733 State = NORMAL;
5734# ifdef FEAT_MOUSE
5735 setmouse();
5736# endif
5737
5738# ifdef FEAT_AUTOCMD
5739 /* Trigger CmdwinEnter autocommands. */
5740 typestr[0] = cmdwin_type;
5741 typestr[1] = NUL;
5742 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5743# endif
5744
5745 i = RedrawingDisabled;
5746 RedrawingDisabled = 0;
5747
5748 /*
5749 * Call the main loop until <CR> or CTRL-C is typed.
5750 */
5751 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005752 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753
5754 RedrawingDisabled = i;
5755
5756# ifdef FEAT_AUTOCMD
5757 /* Trigger CmdwinLeave autocommands. */
5758 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5759# endif
5760
5761 /* Restore the comand line info. */
5762 ccline = save_ccline;
5763 cmdwin_type = 0;
5764
5765 exmode_active = save_exmode;
5766
5767 /* Safety check: The old window or buffer was deleted: It's a a bug when
5768 * this happens! */
5769 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5770 {
5771 cmdwin_result = Ctrl_C;
5772 EMSG(_("E199: Active window or buffer deleted"));
5773 }
5774 else
5775 {
5776# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5777 /* autocmds may abort script processing */
5778 if (aborting() && cmdwin_result != K_IGNORE)
5779 cmdwin_result = Ctrl_C;
5780# endif
5781 /* Set the new command line from the cmdline buffer. */
5782 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005783 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005785 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5786
5787 if (histtype == HIST_CMD)
5788 {
5789 /* Execute the command directly. */
5790 ccline.cmdbuff = vim_strsave((char_u *)p);
5791 cmdwin_result = CAR;
5792 }
5793 else
5794 {
5795 /* First need to cancel what we were doing. */
5796 ccline.cmdbuff = NULL;
5797 stuffcharReadbuff(':');
5798 stuffReadbuff((char_u *)p);
5799 stuffcharReadbuff(CAR);
5800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 }
5802 else if (cmdwin_result == K_XF2) /* :qa typed */
5803 {
5804 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5805 cmdwin_result = CAR;
5806 }
5807 else
5808 ccline.cmdbuff = vim_strsave(ml_get_curline());
5809 if (ccline.cmdbuff == NULL)
5810 cmdwin_result = Ctrl_C;
5811 else
5812 {
5813 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5814 ccline.cmdbufflen = ccline.cmdlen + 1;
5815 ccline.cmdpos = curwin->w_cursor.col;
5816 if (ccline.cmdpos > ccline.cmdlen)
5817 ccline.cmdpos = ccline.cmdlen;
5818 if (cmdwin_result == K_IGNORE)
5819 {
5820 set_cmdspos_cursor();
5821 redrawcmd();
5822 }
5823 }
5824
5825# ifdef FEAT_AUTOCMD
5826 /* Don't execute autocommands while deleting the window. */
5827 ++autocmd_block;
5828# endif
5829 wp = curwin;
5830 bp = curbuf;
5831 win_goto(old_curwin);
5832 win_close(wp, TRUE);
5833 close_buffer(NULL, bp, DOBUF_WIPE);
5834
5835 /* Restore window sizes. */
5836 win_size_restore(&winsizes);
5837
5838# ifdef FEAT_AUTOCMD
5839 --autocmd_block;
5840# endif
5841 }
5842
5843 ga_clear(&winsizes);
5844 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005845# ifdef FEAT_RIGHTLEFT
5846 cmdmsg_rl = save_cmdmsg_rl;
5847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 State = save_State;
5850# ifdef FEAT_MOUSE
5851 setmouse();
5852# endif
5853
5854 return cmdwin_result;
5855}
5856#endif /* FEAT_CMDWIN */
5857
5858/*
5859 * Used for commands that either take a simple command string argument, or:
5860 * cmd << endmarker
5861 * {script}
5862 * endmarker
5863 * Returns a pointer to allocated memory with {script} or NULL.
5864 */
5865 char_u *
5866script_get(eap, cmd)
5867 exarg_T *eap;
5868 char_u *cmd;
5869{
5870 char_u *theline;
5871 char *end_pattern = NULL;
5872 char dot[] = ".";
5873 garray_T ga;
5874
5875 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5876 return NULL;
5877
5878 ga_init2(&ga, 1, 0x400);
5879
5880 if (cmd[2] != NUL)
5881 end_pattern = (char *)skipwhite(cmd + 2);
5882 else
5883 end_pattern = dot;
5884
5885 for (;;)
5886 {
5887 theline = eap->getline(
5888#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005889 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
5891 NUL, eap->cookie, 0);
5892
5893 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5894 break;
5895
5896 ga_concat(&ga, theline);
5897 ga_append(&ga, '\n');
5898 vim_free(theline);
5899 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005900 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 return (char_u *)ga.ga_data;
5903}