blob: 6828f671de9531d72f02ffbcad188202cc56547c [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
2942 msg_start();
2943 redrawcmdprompt();
2944
2945 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
2946 msg_no_more = TRUE;
2947 draw_cmdline(0, ccline.cmdlen);
2948 msg_clr_eos();
2949 msg_no_more = FALSE;
2950
2951 set_cmdspos_cursor();
2952
2953 /*
2954 * An emsg() before may have set msg_scroll. This is used in normal mode,
2955 * in cmdline mode we can reset them now.
2956 */
2957 msg_scroll = FALSE; /* next message overwrites cmdline */
2958
2959 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
2960 * in cmdline mode */
2961 skip_redraw = FALSE;
2962}
2963
2964 void
2965compute_cmdrow()
2966{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002967 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 cmdline_row = Rows - 1;
2969 else
2970 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
2971 + W_STATUS_HEIGHT(lastwin);
2972}
2973
2974 static void
2975cursorcmd()
2976{
2977 if (cmd_silent)
2978 return;
2979
2980#ifdef FEAT_RIGHTLEFT
2981 if (cmdmsg_rl)
2982 {
2983 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
2984 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
2985 if (msg_row <= 0)
2986 msg_row = Rows - 1;
2987 }
2988 else
2989#endif
2990 {
2991 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
2992 msg_col = ccline.cmdspos % (int)Columns;
2993 if (msg_row >= Rows)
2994 msg_row = Rows - 1;
2995 }
2996
2997 windgoto(msg_row, msg_col);
2998#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2999 redrawcmd_preedit();
3000#endif
3001#ifdef MCH_CURSOR_SHAPE
3002 mch_update_cursor();
3003#endif
3004}
3005
3006 void
3007gotocmdline(clr)
3008 int clr;
3009{
3010 msg_start();
3011#ifdef FEAT_RIGHTLEFT
3012 if (cmdmsg_rl)
3013 msg_col = Columns - 1;
3014 else
3015#endif
3016 msg_col = 0; /* always start in column 0 */
3017 if (clr) /* clear the bottom line(s) */
3018 msg_clr_eos(); /* will reset clear_cmdline */
3019 windgoto(cmdline_row, 0);
3020}
3021
3022/*
3023 * Check the word in front of the cursor for an abbreviation.
3024 * Called when the non-id character "c" has been entered.
3025 * When an abbreviation is recognized it is removed from the text with
3026 * backspaces and the replacement string is inserted, followed by "c".
3027 */
3028 static int
3029ccheck_abbr(c)
3030 int c;
3031{
3032 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3033 return FALSE;
3034
3035 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3036}
3037
3038/*
3039 * Return FAIL if this is not an appropriate context in which to do
3040 * completion of anything, return OK if it is (even if there are no matches).
3041 * For the caller, this means that the character is just passed through like a
3042 * normal character (instead of being expanded). This allows :s/^I^D etc.
3043 */
3044 static int
3045nextwild(xp, type, options)
3046 expand_T *xp;
3047 int type;
3048 int options; /* extra options for ExpandOne() */
3049{
3050 int i, j;
3051 char_u *p1;
3052 char_u *p2;
3053 int oldlen;
3054 int difflen;
3055 int v;
3056
3057 if (xp->xp_numfiles == -1)
3058 {
3059 set_expand_context(xp);
3060 cmd_showtail = expand_showtail(xp);
3061 }
3062
3063 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3064 {
3065 beep_flush();
3066 return OK; /* Something illegal on command line */
3067 }
3068 if (xp->xp_context == EXPAND_NOTHING)
3069 {
3070 /* Caller can use the character as a normal char instead */
3071 return FAIL;
3072 }
3073
3074 MSG_PUTS("..."); /* show that we are busy */
3075 out_flush();
3076
3077 i = (int)(xp->xp_pattern - ccline.cmdbuff);
3078 oldlen = ccline.cmdpos - i;
3079
3080 if (type == WILD_NEXT || type == WILD_PREV)
3081 {
3082 /*
3083 * Get next/previous match for a previous expanded pattern.
3084 */
3085 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3086 }
3087 else
3088 {
3089 /*
3090 * Translate string into pattern and expand it.
3091 */
3092 if ((p1 = addstar(&ccline.cmdbuff[i], oldlen, xp->xp_context)) == NULL)
3093 p2 = NULL;
3094 else
3095 {
3096 p2 = ExpandOne(xp, p1, vim_strnsave(&ccline.cmdbuff[i], oldlen),
3097 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT|WILD_ESCAPE
3098 |options, type);
3099 vim_free(p1);
3100 /* longest match: make sure it is not shorter (happens with :help */
3101 if (p2 != NULL && type == WILD_LONGEST)
3102 {
3103 for (j = 0; j < oldlen; ++j)
3104 if (ccline.cmdbuff[i + j] == '*'
3105 || ccline.cmdbuff[i + j] == '?')
3106 break;
3107 if ((int)STRLEN(p2) < j)
3108 {
3109 vim_free(p2);
3110 p2 = NULL;
3111 }
3112 }
3113 }
3114 }
3115
3116 if (p2 != NULL && !got_int)
3117 {
3118 difflen = (int)STRLEN(p2) - oldlen;
3119 if (ccline.cmdlen + difflen > ccline.cmdbufflen - 4)
3120 {
3121 v = realloc_cmdbuff(ccline.cmdlen + difflen);
3122 xp->xp_pattern = ccline.cmdbuff + i;
3123 }
3124 else
3125 v = OK;
3126 if (v == OK)
3127 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003128 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3129 &ccline.cmdbuff[ccline.cmdpos],
3130 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3131 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 ccline.cmdlen += difflen;
3133 ccline.cmdpos += difflen;
3134 }
3135 }
3136 vim_free(p2);
3137
3138 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003139 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140
3141 /* When expanding a ":map" command and no matches are found, assume that
3142 * the key is supposed to be inserted literally */
3143 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3144 return FAIL;
3145
3146 if (xp->xp_numfiles <= 0 && p2 == NULL)
3147 beep_flush();
3148 else if (xp->xp_numfiles == 1)
3149 /* free expanded pattern */
3150 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3151
3152 return OK;
3153}
3154
3155/*
3156 * Do wildcard expansion on the string 'str'.
3157 * Chars that should not be expanded must be preceded with a backslash.
3158 * Return a pointer to alloced memory containing the new string.
3159 * Return NULL for failure.
3160 *
3161 * Results are cached in xp->xp_files and xp->xp_numfiles.
3162 *
3163 * mode = WILD_FREE: just free previously expanded matches
3164 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3165 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3166 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3167 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3168 * mode = WILD_ALL: return all matches concatenated
3169 * mode = WILD_LONGEST: return longest matched part
3170 *
3171 * options = WILD_LIST_NOTFOUND: list entries without a match
3172 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3173 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3174 * options = WILD_NO_BEEP: Don't beep for multiple matches
3175 * options = WILD_ADD_SLASH: add a slash after directory names
3176 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3177 * options = WILD_SILENT: don't print warning messages
3178 * options = WILD_ESCAPE: put backslash before special chars
3179 *
3180 * The variables xp->xp_context and xp->xp_backslash must have been set!
3181 */
3182 char_u *
3183ExpandOne(xp, str, orig, options, mode)
3184 expand_T *xp;
3185 char_u *str;
3186 char_u *orig; /* allocated copy of original of expanded string */
3187 int options;
3188 int mode;
3189{
3190 char_u *ss = NULL;
3191 static int findex;
3192 static char_u *orig_save = NULL; /* kept value of orig */
3193 int i;
3194 long_u len;
3195 int non_suf_match; /* number without matching suffix */
3196
3197 /*
3198 * first handle the case of using an old match
3199 */
3200 if (mode == WILD_NEXT || mode == WILD_PREV)
3201 {
3202 if (xp->xp_numfiles > 0)
3203 {
3204 if (mode == WILD_PREV)
3205 {
3206 if (findex == -1)
3207 findex = xp->xp_numfiles;
3208 --findex;
3209 }
3210 else /* mode == WILD_NEXT */
3211 ++findex;
3212
3213 /*
3214 * When wrapping around, return the original string, set findex to
3215 * -1.
3216 */
3217 if (findex < 0)
3218 {
3219 if (orig_save == NULL)
3220 findex = xp->xp_numfiles - 1;
3221 else
3222 findex = -1;
3223 }
3224 if (findex >= xp->xp_numfiles)
3225 {
3226 if (orig_save == NULL)
3227 findex = 0;
3228 else
3229 findex = -1;
3230 }
3231#ifdef FEAT_WILDMENU
3232 if (p_wmnu)
3233 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3234 findex, cmd_showtail);
3235#endif
3236 if (findex == -1)
3237 return vim_strsave(orig_save);
3238 return vim_strsave(xp->xp_files[findex]);
3239 }
3240 else
3241 return NULL;
3242 }
3243
3244/* free old names */
3245 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3246 {
3247 FreeWild(xp->xp_numfiles, xp->xp_files);
3248 xp->xp_numfiles = -1;
3249 vim_free(orig_save);
3250 orig_save = NULL;
3251 }
3252 findex = 0;
3253
3254 if (mode == WILD_FREE) /* only release file name */
3255 return NULL;
3256
3257 if (xp->xp_numfiles == -1)
3258 {
3259 vim_free(orig_save);
3260 orig_save = orig;
3261
3262 /*
3263 * Do the expansion.
3264 */
3265 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3266 options) == FAIL)
3267 {
3268#ifdef FNAME_ILLEGAL
3269 /* Illegal file name has been silently skipped. But when there
3270 * are wildcards, the real problem is that there was no match,
3271 * causing the pattern to be added, which has illegal characters.
3272 */
3273 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3274 EMSG2(_(e_nomatch2), str);
3275#endif
3276 }
3277 else if (xp->xp_numfiles == 0)
3278 {
3279 if (!(options & WILD_SILENT))
3280 EMSG2(_(e_nomatch2), str);
3281 }
3282 else
3283 {
3284 /* Escape the matches for use on the command line. */
3285 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3286
3287 /*
3288 * Check for matching suffixes in file names.
3289 */
3290 if (mode != WILD_ALL && mode != WILD_LONGEST)
3291 {
3292 if (xp->xp_numfiles)
3293 non_suf_match = xp->xp_numfiles;
3294 else
3295 non_suf_match = 1;
3296 if ((xp->xp_context == EXPAND_FILES
3297 || xp->xp_context == EXPAND_DIRECTORIES)
3298 && xp->xp_numfiles > 1)
3299 {
3300 /*
3301 * More than one match; check suffix.
3302 * The files will have been sorted on matching suffix in
3303 * expand_wildcards, only need to check the first two.
3304 */
3305 non_suf_match = 0;
3306 for (i = 0; i < 2; ++i)
3307 if (match_suffix(xp->xp_files[i]))
3308 ++non_suf_match;
3309 }
3310 if (non_suf_match != 1)
3311 {
3312 /* Can we ever get here unless it's while expanding
3313 * interactively? If not, we can get rid of this all
3314 * together. Don't really want to wait for this message
3315 * (and possibly have to hit return to continue!).
3316 */
3317 if (!(options & WILD_SILENT))
3318 EMSG(_(e_toomany));
3319 else if (!(options & WILD_NO_BEEP))
3320 beep_flush();
3321 }
3322 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3323 ss = vim_strsave(xp->xp_files[0]);
3324 }
3325 }
3326 }
3327
3328 /* Find longest common part */
3329 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3330 {
3331 for (len = 0; xp->xp_files[0][len]; ++len)
3332 {
3333 for (i = 0; i < xp->xp_numfiles; ++i)
3334 {
3335#ifdef CASE_INSENSITIVE_FILENAME
3336 if (xp->xp_context == EXPAND_DIRECTORIES
3337 || xp->xp_context == EXPAND_FILES
3338 || xp->xp_context == EXPAND_BUFFERS)
3339 {
3340 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3341 TOLOWER_LOC(xp->xp_files[0][len]))
3342 break;
3343 }
3344 else
3345#endif
3346 if (xp->xp_files[i][len] != xp->xp_files[0][len])
3347 break;
3348 }
3349 if (i < xp->xp_numfiles)
3350 {
3351 if (!(options & WILD_NO_BEEP))
3352 vim_beep();
3353 break;
3354 }
3355 }
3356 ss = alloc((unsigned)len + 1);
3357 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003358 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 findex = -1; /* next p_wc gets first one */
3360 }
3361
3362 /* Concatenate all matching names */
3363 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3364 {
3365 len = 0;
3366 for (i = 0; i < xp->xp_numfiles; ++i)
3367 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3368 ss = lalloc(len, TRUE);
3369 if (ss != NULL)
3370 {
3371 *ss = NUL;
3372 for (i = 0; i < xp->xp_numfiles; ++i)
3373 {
3374 STRCAT(ss, xp->xp_files[i]);
3375 if (i != xp->xp_numfiles - 1)
3376 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3377 }
3378 }
3379 }
3380
3381 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3382 ExpandCleanup(xp);
3383
3384 return ss;
3385}
3386
3387/*
3388 * Prepare an expand structure for use.
3389 */
3390 void
3391ExpandInit(xp)
3392 expand_T *xp;
3393{
3394 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003395#ifndef BACKSLASH_IN_FILENAME
3396 xp->xp_shell = FALSE;
3397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 xp->xp_numfiles = -1;
3399 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003400#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3401 xp->xp_arg = NULL;
3402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403}
3404
3405/*
3406 * Cleanup an expand structure after use.
3407 */
3408 void
3409ExpandCleanup(xp)
3410 expand_T *xp;
3411{
3412 if (xp->xp_numfiles >= 0)
3413 {
3414 FreeWild(xp->xp_numfiles, xp->xp_files);
3415 xp->xp_numfiles = -1;
3416 }
3417}
3418
3419 void
3420ExpandEscape(xp, str, numfiles, files, options)
3421 expand_T *xp;
3422 char_u *str;
3423 int numfiles;
3424 char_u **files;
3425 int options;
3426{
3427 int i;
3428 char_u *p;
3429
3430 /*
3431 * May change home directory back to "~"
3432 */
3433 if (options & WILD_HOME_REPLACE)
3434 tilde_replace(str, numfiles, files);
3435
3436 if (options & WILD_ESCAPE)
3437 {
3438 if (xp->xp_context == EXPAND_FILES
3439 || xp->xp_context == EXPAND_BUFFERS
3440 || xp->xp_context == EXPAND_DIRECTORIES)
3441 {
3442 /*
3443 * Insert a backslash into a file name before a space, \, %, #
3444 * and wildmatch characters, except '~'.
3445 */
3446 for (i = 0; i < numfiles; ++i)
3447 {
3448 /* for ":set path=" we need to escape spaces twice */
3449 if (xp->xp_backslash == XP_BS_THREE)
3450 {
3451 p = vim_strsave_escaped(files[i], (char_u *)" ");
3452 if (p != NULL)
3453 {
3454 vim_free(files[i]);
3455 files[i] = p;
3456#if defined(BACKSLASH_IN_FILENAME) || defined(COLON_AS_PATHSEP)
3457 p = vim_strsave_escaped(files[i], (char_u *)" ");
3458 if (p != NULL)
3459 {
3460 vim_free(files[i]);
3461 files[i] = p;
3462 }
3463#endif
3464 }
3465 }
3466#ifdef BACKSLASH_IN_FILENAME
3467 {
3468 char_u buf[20];
3469 int j = 0;
3470
3471 /* Don't escape '[' and '{' if they are in 'isfname'. */
3472 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3473 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3474 buf[j++] = *p;
3475 buf[j] = NUL;
3476 p = vim_strsave_escaped(files[i], buf);
3477 }
3478#else
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003479 p = vim_strsave_escaped(files[i],
3480 xp->xp_shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
3482 if (p != NULL)
3483 {
3484 vim_free(files[i]);
3485 files[i] = p;
3486 }
3487
3488 /* If 'str' starts with "\~", replace "~" at start of
3489 * files[i] with "\~". */
3490 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003491 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003494
3495 /* If the first file starts with a '+' escape it. Otherwise it
3496 * could be seen as "+cmd". */
3497 if (*files[0] == '+')
3498 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 }
3500 else if (xp->xp_context == EXPAND_TAGS)
3501 {
3502 /*
3503 * Insert a backslash before characters in a tag name that
3504 * would terminate the ":tag" command.
3505 */
3506 for (i = 0; i < numfiles; ++i)
3507 {
3508 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3509 if (p != NULL)
3510 {
3511 vim_free(files[i]);
3512 files[i] = p;
3513 }
3514 }
3515 }
3516 }
3517}
3518
3519/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003520 * Put a backslash before the file name in "pp", which is in allocated memory.
3521 */
3522 static void
3523escape_fname(pp)
3524 char_u **pp;
3525{
3526 char_u *p;
3527
3528 p = alloc((unsigned)(STRLEN(*pp) + 2));
3529 if (p != NULL)
3530 {
3531 p[0] = '\\';
3532 STRCPY(p + 1, *pp);
3533 vim_free(*pp);
3534 *pp = p;
3535 }
3536}
3537
3538/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 * For each file name in files[num_files]:
3540 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3541 */
3542 void
3543tilde_replace(orig_pat, num_files, files)
3544 char_u *orig_pat;
3545 int num_files;
3546 char_u **files;
3547{
3548 int i;
3549 char_u *p;
3550
3551 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3552 {
3553 for (i = 0; i < num_files; ++i)
3554 {
3555 p = home_replace_save(NULL, files[i]);
3556 if (p != NULL)
3557 {
3558 vim_free(files[i]);
3559 files[i] = p;
3560 }
3561 }
3562 }
3563}
3564
3565/*
3566 * Show all matches for completion on the command line.
3567 * Returns EXPAND_NOTHING when the character that triggered expansion should
3568 * be inserted like a normal character.
3569 */
3570/*ARGSUSED*/
3571 static int
3572showmatches(xp, wildmenu)
3573 expand_T *xp;
3574 int wildmenu;
3575{
3576#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3577 int num_files;
3578 char_u **files_found;
3579 int i, j, k;
3580 int maxlen;
3581 int lines;
3582 int columns;
3583 char_u *p;
3584 int lastlen;
3585 int attr;
3586 int showtail;
3587
3588 if (xp->xp_numfiles == -1)
3589 {
3590 set_expand_context(xp);
3591 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3592 &num_files, &files_found);
3593 showtail = expand_showtail(xp);
3594 if (i != EXPAND_OK)
3595 return i;
3596
3597 }
3598 else
3599 {
3600 num_files = xp->xp_numfiles;
3601 files_found = xp->xp_files;
3602 showtail = cmd_showtail;
3603 }
3604
3605#ifdef FEAT_WILDMENU
3606 if (!wildmenu)
3607 {
3608#endif
3609 msg_didany = FALSE; /* lines_left will be set */
3610 msg_start(); /* prepare for paging */
3611 msg_putchar('\n');
3612 out_flush();
3613 cmdline_row = msg_row;
3614 msg_didany = FALSE; /* lines_left will be set again */
3615 msg_start(); /* prepare for paging */
3616#ifdef FEAT_WILDMENU
3617 }
3618#endif
3619
3620 if (got_int)
3621 got_int = FALSE; /* only int. the completion, not the cmd line */
3622#ifdef FEAT_WILDMENU
3623 else if (wildmenu)
3624 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3625#endif
3626 else
3627 {
3628 /* find the length of the longest file name */
3629 maxlen = 0;
3630 for (i = 0; i < num_files; ++i)
3631 {
3632 if (!showtail && (xp->xp_context == EXPAND_FILES
3633 || xp->xp_context == EXPAND_BUFFERS))
3634 {
3635 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3636 j = vim_strsize(NameBuff);
3637 }
3638 else
3639 j = vim_strsize(L_SHOWFILE(i));
3640 if (j > maxlen)
3641 maxlen = j;
3642 }
3643
3644 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3645 lines = num_files;
3646 else
3647 {
3648 /* compute the number of columns and lines for the listing */
3649 maxlen += 2; /* two spaces between file names */
3650 columns = ((int)Columns + 2) / maxlen;
3651 if (columns < 1)
3652 columns = 1;
3653 lines = (num_files + columns - 1) / columns;
3654 }
3655
3656 attr = hl_attr(HLF_D); /* find out highlighting for directories */
3657
3658 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3659 {
3660 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
3661 msg_clr_eos();
3662 msg_advance(maxlen - 3);
3663 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
3664 }
3665
3666 /* list the files line by line */
3667 for (i = 0; i < lines; ++i)
3668 {
3669 lastlen = 999;
3670 for (k = i; k < num_files; k += lines)
3671 {
3672 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
3673 {
3674 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
3675 p = files_found[k] + STRLEN(files_found[k]) + 1;
3676 msg_advance(maxlen + 1);
3677 msg_puts(p);
3678 msg_advance(maxlen + 3);
3679 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
3680 break;
3681 }
3682 for (j = maxlen - lastlen; --j >= 0; )
3683 msg_putchar(' ');
3684 if (xp->xp_context == EXPAND_FILES
3685 || xp->xp_context == EXPAND_BUFFERS)
3686 {
3687 /* highlight directories */
3688 j = (mch_isdir(files_found[k]));
3689 if (showtail)
3690 p = L_SHOWFILE(k);
3691 else
3692 {
3693 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
3694 TRUE);
3695 p = NameBuff;
3696 }
3697 }
3698 else
3699 {
3700 j = FALSE;
3701 p = L_SHOWFILE(k);
3702 }
3703 lastlen = msg_outtrans_attr(p, j ? attr : 0);
3704 }
3705 if (msg_col > 0) /* when not wrapped around */
3706 {
3707 msg_clr_eos();
3708 msg_putchar('\n');
3709 }
3710 out_flush(); /* show one line at a time */
3711 if (got_int)
3712 {
3713 got_int = FALSE;
3714 break;
3715 }
3716 }
3717
3718 /*
3719 * we redraw the command below the lines that we have just listed
3720 * This is a bit tricky, but it saves a lot of screen updating.
3721 */
3722 cmdline_row = msg_row; /* will put it back later */
3723 }
3724
3725 if (xp->xp_numfiles == -1)
3726 FreeWild(num_files, files_found);
3727
3728 return EXPAND_OK;
3729}
3730
3731/*
3732 * Private gettail for showmatches() (and win_redr_status_matches()):
3733 * Find tail of file name path, but ignore trailing "/".
3734 */
3735 char_u *
3736sm_gettail(s)
3737 char_u *s;
3738{
3739 char_u *p;
3740 char_u *t = s;
3741 int had_sep = FALSE;
3742
3743 for (p = s; *p != NUL; )
3744 {
3745 if (vim_ispathsep(*p)
3746#ifdef BACKSLASH_IN_FILENAME
3747 && !rem_backslash(p)
3748#endif
3749 )
3750 had_sep = TRUE;
3751 else if (had_sep)
3752 {
3753 t = p;
3754 had_sep = FALSE;
3755 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003756 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 }
3758 return t;
3759}
3760
3761/*
3762 * Return TRUE if we only need to show the tail of completion matches.
3763 * When not completing file names or there is a wildcard in the path FALSE is
3764 * returned.
3765 */
3766 static int
3767expand_showtail(xp)
3768 expand_T *xp;
3769{
3770 char_u *s;
3771 char_u *end;
3772
3773 /* When not completing file names a "/" may mean something different. */
3774 if (xp->xp_context != EXPAND_FILES && xp->xp_context != EXPAND_DIRECTORIES)
3775 return FALSE;
3776
3777 end = gettail(xp->xp_pattern);
3778 if (end == xp->xp_pattern) /* there is no path separator */
3779 return FALSE;
3780
3781 for (s = xp->xp_pattern; s < end; s++)
3782 {
3783 /* Skip escaped wildcards. Only when the backslash is not a path
3784 * separator, on DOS the '*' "path\*\file" must not be skipped. */
3785 if (rem_backslash(s))
3786 ++s;
3787 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
3788 return FALSE;
3789 }
3790 return TRUE;
3791}
3792
3793/*
3794 * Prepare a string for expansion.
3795 * When expanding file names: The string will be used with expand_wildcards().
3796 * Copy the file name into allocated memory and add a '*' at the end.
3797 * When expanding other names: The string will be used with regcomp(). Copy
3798 * the name into allocated memory and prepend "^".
3799 */
3800 char_u *
3801addstar(fname, len, context)
3802 char_u *fname;
3803 int len;
3804 int context; /* EXPAND_FILES etc. */
3805{
3806 char_u *retval;
3807 int i, j;
3808 int new_len;
3809 char_u *tail;
3810
3811 if (context != EXPAND_FILES && context != EXPAND_DIRECTORIES)
3812 {
3813 /*
3814 * Matching will be done internally (on something other than files).
3815 * So we convert the file-matching-type wildcards into our kind for
3816 * use with vim_regcomp(). First work out how long it will be:
3817 */
3818
3819 /* For help tags the translation is done in find_help_tags().
3820 * For a tag pattern starting with "/" no translation is needed. */
3821 if (context == EXPAND_HELP
3822 || context == EXPAND_COLORS
3823 || context == EXPAND_COMPILER
3824 || (context == EXPAND_TAGS && fname[0] == '/'))
3825 retval = vim_strnsave(fname, len);
3826 else
3827 {
3828 new_len = len + 2; /* +2 for '^' at start, NUL at end */
3829 for (i = 0; i < len; i++)
3830 {
3831 if (fname[i] == '*' || fname[i] == '~')
3832 new_len++; /* '*' needs to be replaced by ".*"
3833 '~' needs to be replaced by "\~" */
3834
3835 /* Buffer names are like file names. "." should be literal */
3836 if (context == EXPAND_BUFFERS && fname[i] == '.')
3837 new_len++; /* "." becomes "\." */
3838
3839 /* Custom expansion takes care of special things, match
3840 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003841 if ((context == EXPAND_USER_DEFINED
3842 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 new_len++; /* '\' becomes "\\" */
3844 }
3845 retval = alloc(new_len);
3846 if (retval != NULL)
3847 {
3848 retval[0] = '^';
3849 j = 1;
3850 for (i = 0; i < len; i++, j++)
3851 {
3852 /* Skip backslash. But why? At least keep it for custom
3853 * expansion. */
3854 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003855 && context != EXPAND_USER_LIST
3856 && fname[i] == '\\'
3857 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 break;
3859
3860 switch (fname[i])
3861 {
3862 case '*': retval[j++] = '.';
3863 break;
3864 case '~': retval[j++] = '\\';
3865 break;
3866 case '?': retval[j] = '.';
3867 continue;
3868 case '.': if (context == EXPAND_BUFFERS)
3869 retval[j++] = '\\';
3870 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00003871 case '\\': if (context == EXPAND_USER_DEFINED
3872 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 retval[j++] = '\\';
3874 break;
3875 }
3876 retval[j] = fname[i];
3877 }
3878 retval[j] = NUL;
3879 }
3880 }
3881 }
3882 else
3883 {
3884 retval = alloc(len + 4);
3885 if (retval != NULL)
3886 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003887 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889 /*
3890 * Don't add a star to ~, ~user, $var or `cmd`.
3891 * ~ would be at the start of the file name, but not the tail.
3892 * $ could be anywhere in the tail.
3893 * ` could be anywhere in the file name.
3894 */
3895 tail = gettail(retval);
3896 if ((*retval != '~' || tail != retval)
3897 && vim_strchr(tail, '$') == NULL
3898 && vim_strchr(retval, '`') == NULL)
3899 retval[len++] = '*';
3900 retval[len] = NUL;
3901 }
3902 }
3903 return retval;
3904}
3905
3906/*
3907 * Must parse the command line so far to work out what context we are in.
3908 * Completion can then be done based on that context.
3909 * This routine sets the variables:
3910 * xp->xp_pattern The start of the pattern to be expanded within
3911 * the command line (ends at the cursor).
3912 * xp->xp_context The type of thing to expand. Will be one of:
3913 *
3914 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
3915 * the command line, like an unknown command. Caller
3916 * should beep.
3917 * EXPAND_NOTHING Unrecognised context for completion, use char like
3918 * a normal char, rather than for completion. eg
3919 * :s/^I/
3920 * EXPAND_COMMANDS Cursor is still touching the command, so complete
3921 * it.
3922 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
3923 * EXPAND_FILES After command with XFILE set, or after setting
3924 * with P_EXPAND set. eg :e ^I, :w>>^I
3925 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
3926 * when we know only directories are of interest. eg
3927 * :set dir=^I
3928 * EXPAND_SETTINGS Complete variable names. eg :set d^I
3929 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
3930 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
3931 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
3932 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
3933 * EXPAND_EVENTS Complete event names
3934 * EXPAND_SYNTAX Complete :syntax command arguments
3935 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
3936 * EXPAND_AUGROUP Complete autocommand group names
3937 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
3938 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
3939 * eg :unmap a^I , :cunab x^I
3940 * EXPAND_FUNCTIONS Complete internal or user defined function names,
3941 * eg :call sub^I
3942 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
3943 * EXPAND_EXPRESSION Complete internal or user defined function/variable
3944 * names in expressions, eg :while s^I
3945 * EXPAND_ENV_VARS Complete environment variable names
3946 */
3947 static void
3948set_expand_context(xp)
3949 expand_T *xp;
3950{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003951 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (ccline.cmdfirstc != ':'
3953#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003954 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003955 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#endif
3957 )
3958 {
3959 xp->xp_context = EXPAND_NOTHING;
3960 return;
3961 }
3962 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
3963}
3964
3965 void
3966set_cmd_context(xp, str, len, col)
3967 expand_T *xp;
3968 char_u *str; /* start of command line */
3969 int len; /* length of command line (excl. NUL) */
3970 int col; /* position of cursor */
3971{
3972 int old_char = NUL;
3973 char_u *nextcomm;
3974
3975 /*
3976 * Avoid a UMR warning from Purify, only save the character if it has been
3977 * written before.
3978 */
3979 if (col < len)
3980 old_char = str[col];
3981 str[col] = NUL;
3982 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003983
3984#ifdef FEAT_EVAL
3985 if (ccline.cmdfirstc == '=')
3986 /* pass CMD_SIZE because there is no real command */
3987 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003988 else if (ccline.input_fn)
3989 {
3990 xp->xp_context = ccline.xp_context;
3991 xp->xp_pattern = ccline.cmdbuff;
3992 xp->xp_arg = ccline.xp_arg;
3993 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003994 else
3995#endif
3996 while (nextcomm != NULL)
3997 nextcomm = set_one_cmd_context(xp, nextcomm);
3998
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 str[col] = old_char;
4000}
4001
4002/*
4003 * Expand the command line "str" from context "xp".
4004 * "xp" must have been set by set_cmd_context().
4005 * xp->xp_pattern points into "str", to where the text that is to be expanded
4006 * starts.
4007 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4008 * cursor.
4009 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4010 * key that triggered expansion literally.
4011 * Returns EXPAND_OK otherwise.
4012 */
4013 int
4014expand_cmdline(xp, str, col, matchcount, matches)
4015 expand_T *xp;
4016 char_u *str; /* start of command line */
4017 int col; /* position of cursor */
4018 int *matchcount; /* return: nr of matches */
4019 char_u ***matches; /* return: array of pointers to matches */
4020{
4021 char_u *file_str = NULL;
4022
4023 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4024 {
4025 beep_flush();
4026 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4027 }
4028 if (xp->xp_context == EXPAND_NOTHING)
4029 {
4030 /* Caller can use the character as a normal char instead */
4031 return EXPAND_NOTHING;
4032 }
4033
4034 /* add star to file name, or convert to regexp if not exp. files. */
4035 file_str = addstar(xp->xp_pattern,
4036 (int)(str + col - xp->xp_pattern), xp->xp_context);
4037 if (file_str == NULL)
4038 return EXPAND_UNSUCCESSFUL;
4039
4040 /* find all files that match the description */
4041 if (ExpandFromContext(xp, file_str, matchcount, matches,
4042 WILD_ADD_SLASH|WILD_SILENT) == FAIL)
4043 {
4044 *matchcount = 0;
4045 *matches = NULL;
4046 }
4047 vim_free(file_str);
4048
4049 return EXPAND_OK;
4050}
4051
4052#ifdef FEAT_MULTI_LANG
4053/*
4054 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4055 */
4056static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4057
4058 static void
4059cleanup_help_tags(num_file, file)
4060 int num_file;
4061 char_u **file;
4062{
4063 int i, j;
4064 int len;
4065
4066 for (i = 0; i < num_file; ++i)
4067 {
4068 len = (int)STRLEN(file[i]) - 3;
4069 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4070 {
4071 /* Sorting on priority means the same item in another language may
4072 * be anywhere. Search all items for a match up to the "@en". */
4073 for (j = 0; j < num_file; ++j)
4074 if (j != i
4075 && (int)STRLEN(file[j]) == len + 3
4076 && STRNCMP(file[i], file[j], len + 1) == 0)
4077 break;
4078 if (j == num_file)
4079 file[i][len] = NUL;
4080 }
4081 }
4082}
4083#endif
4084
4085/*
4086 * Do the expansion based on xp->xp_context and "pat".
4087 */
4088 static int
4089ExpandFromContext(xp, pat, num_file, file, options)
4090 expand_T *xp;
4091 char_u *pat;
4092 int *num_file;
4093 char_u ***file;
4094 int options;
4095{
4096#ifdef FEAT_CMDL_COMPL
4097 regmatch_T regmatch;
4098#endif
4099 int ret;
4100 int flags;
4101
4102 flags = EW_DIR; /* include directories */
4103 if (options & WILD_LIST_NOTFOUND)
4104 flags |= EW_NOTFOUND;
4105 if (options & WILD_ADD_SLASH)
4106 flags |= EW_ADDSLASH;
4107 if (options & WILD_KEEP_ALL)
4108 flags |= EW_KEEPALL;
4109 if (options & WILD_SILENT)
4110 flags |= EW_SILENT;
4111
4112 if (xp->xp_context == EXPAND_FILES || xp->xp_context == EXPAND_DIRECTORIES)
4113 {
4114 /*
4115 * Expand file or directory names.
4116 */
4117 int free_pat = FALSE;
4118 int i;
4119
4120 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4121 * space */
4122 if (xp->xp_backslash != XP_BS_NONE)
4123 {
4124 free_pat = TRUE;
4125 pat = vim_strsave(pat);
4126 for (i = 0; pat[i]; ++i)
4127 if (pat[i] == '\\')
4128 {
4129 if (xp->xp_backslash == XP_BS_THREE
4130 && pat[i + 1] == '\\'
4131 && pat[i + 2] == '\\'
4132 && pat[i + 3] == ' ')
4133 STRCPY(pat + i, pat + i + 3);
4134 if (xp->xp_backslash == XP_BS_ONE
4135 && pat[i + 1] == ' ')
4136 STRCPY(pat + i, pat + i + 1);
4137 }
4138 }
4139
4140 if (xp->xp_context == EXPAND_FILES)
4141 flags |= EW_FILE;
4142 else
4143 flags = (flags | EW_DIR) & ~EW_FILE;
4144 ret = expand_wildcards(1, &pat, num_file, file, flags);
4145 if (free_pat)
4146 vim_free(pat);
4147 return ret;
4148 }
4149
4150 *file = (char_u **)"";
4151 *num_file = 0;
4152 if (xp->xp_context == EXPAND_HELP)
4153 {
4154 if (find_help_tags(pat, num_file, file, FALSE) == OK)
4155 {
4156#ifdef FEAT_MULTI_LANG
4157 cleanup_help_tags(*num_file, *file);
4158#endif
4159 return OK;
4160 }
4161 return FAIL;
4162 }
4163
4164#ifndef FEAT_CMDL_COMPL
4165 return FAIL;
4166#else
4167 if (xp->xp_context == EXPAND_OLD_SETTING)
4168 return ExpandOldSetting(num_file, file);
4169 if (xp->xp_context == EXPAND_BUFFERS)
4170 return ExpandBufnames(pat, num_file, file, options);
4171 if (xp->xp_context == EXPAND_TAGS
4172 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4173 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4174 if (xp->xp_context == EXPAND_COLORS)
4175 return ExpandRTDir(pat, num_file, file, "colors");
4176 if (xp->xp_context == EXPAND_COMPILER)
4177 return ExpandRTDir(pat, num_file, file, "compiler");
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004178# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4179 if (xp->xp_context == EXPAND_USER_LIST)
4180 return ExpandUserList(xp, num_file, file);
4181# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
4183 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4184 if (regmatch.regprog == NULL)
4185 return FAIL;
4186
4187 /* set ignore-case according to p_ic, p_scs and pat */
4188 regmatch.rm_ic = ignorecase(pat);
4189
4190 if (xp->xp_context == EXPAND_SETTINGS
4191 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4192 ret = ExpandSettings(xp, &regmatch, num_file, file);
4193 else if (xp->xp_context == EXPAND_MAPPINGS)
4194 ret = ExpandMappings(&regmatch, num_file, file);
4195# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4196 else if (xp->xp_context == EXPAND_USER_DEFINED)
4197 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4198# endif
4199 else
4200 {
4201 static struct expgen
4202 {
4203 int context;
4204 char_u *((*func)__ARGS((expand_T *, int)));
4205 int ic;
4206 } tab[] =
4207 {
4208 {EXPAND_COMMANDS, get_command_name, FALSE},
4209#ifdef FEAT_USR_CMDS
4210 {EXPAND_USER_COMMANDS, get_user_commands, FALSE},
4211 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE},
4212 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE},
4213 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE},
4214#endif
4215#ifdef FEAT_EVAL
4216 {EXPAND_USER_VARS, get_user_var_name, FALSE},
4217 {EXPAND_FUNCTIONS, get_function_name, FALSE},
4218 {EXPAND_USER_FUNC, get_user_func_name, FALSE},
4219 {EXPAND_EXPRESSION, get_expr_name, FALSE},
4220#endif
4221#ifdef FEAT_MENU
4222 {EXPAND_MENUS, get_menu_name, FALSE},
4223 {EXPAND_MENUNAMES, get_menu_names, FALSE},
4224#endif
4225#ifdef FEAT_SYN_HL
4226 {EXPAND_SYNTAX, get_syntax_name, TRUE},
4227#endif
4228 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE},
4229#ifdef FEAT_AUTOCMD
4230 {EXPAND_EVENTS, get_event_name, TRUE},
4231 {EXPAND_AUGROUP, get_augroup_name, TRUE},
4232#endif
4233#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4234 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
4235 {EXPAND_LANGUAGE, get_lang_arg, TRUE},
4236#endif
4237 {EXPAND_ENV_VARS, get_env_name, TRUE},
4238 };
4239 int i;
4240
4241 /*
4242 * Find a context in the table and call the ExpandGeneric() with the
4243 * right function to do the expansion.
4244 */
4245 ret = FAIL;
4246 for (i = 0; i < sizeof(tab) / sizeof(struct expgen); ++i)
4247 if (xp->xp_context == tab[i].context)
4248 {
4249 if (tab[i].ic)
4250 regmatch.rm_ic = TRUE;
4251 ret = ExpandGeneric(xp, &regmatch, num_file, file, tab[i].func);
4252 break;
4253 }
4254 }
4255
4256 vim_free(regmatch.regprog);
4257
4258 return ret;
4259#endif /* FEAT_CMDL_COMPL */
4260}
4261
4262#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4263/*
4264 * Expand a list of names.
4265 *
4266 * Generic function for command line completion. It calls a function to
4267 * obtain strings, one by one. The strings are matched against a regexp
4268 * program. Matching strings are copied into an array, which is returned.
4269 *
4270 * Returns OK when no problems encountered, FAIL for error (out of memory).
4271 */
4272 int
4273ExpandGeneric(xp, regmatch, num_file, file, func)
4274 expand_T *xp;
4275 regmatch_T *regmatch;
4276 int *num_file;
4277 char_u ***file;
4278 char_u *((*func)__ARGS((expand_T *, int)));
4279 /* returns a string from the list */
4280{
4281 int i;
4282 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004283 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 char_u *str;
4285
4286 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004287 * round == 0: count the number of matching names
4288 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004290 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 {
4292 for (i = 0; ; ++i)
4293 {
4294 str = (*func)(xp, i);
4295 if (str == NULL) /* end of list */
4296 break;
4297 if (*str == NUL) /* skip empty strings */
4298 continue;
4299
4300 if (vim_regexec(regmatch, str, (colnr_T)0))
4301 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004302 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 {
4304 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4305 (*file)[count] = str;
4306#ifdef FEAT_MENU
4307 if (func == get_menu_names && str != NULL)
4308 {
4309 /* test for separator added by get_menu_names() */
4310 str += STRLEN(str) - 1;
4311 if (*str == '\001')
4312 *str = '.';
4313 }
4314#endif
4315 }
4316 ++count;
4317 }
4318 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004319 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 if (count == 0)
4322 return OK;
4323 *num_file = count;
4324 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4325 if (*file == NULL)
4326 {
4327 *file = (char_u **)"";
4328 return FAIL;
4329 }
4330 count = 0;
4331 }
4332 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004333
4334 /* Sort the results. */
4335 sort_strings(*file, *num_file);
4336
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 return OK;
4338}
4339
4340# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004341static 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));
4342
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343/*
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004344 * call "user_expand_func()" to invoke a user defined VimL function and return
4345 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004347 static void *
4348call_user_expand_func(user_expand_func, xp, num_file, file)
4349 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 int *num_file;
4352 char_u ***file;
4353{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004354 char_u keep;
4355 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004358 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004359 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
4361 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004362 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 *num_file = 0;
4364 *file = NULL;
4365
4366 keep = ccline.cmdbuff[ccline.cmdlen];
4367 ccline.cmdbuff[ccline.cmdlen] = 0;
4368 sprintf((char *)num, "%d", ccline.cmdpos);
4369 args[0] = xp->xp_pattern;
4370 args[1] = ccline.cmdbuff;
4371 args[2] = num;
4372
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004373 /* Save the cmdline, we don't know what the function may do. */
4374 save_ccline = ccline;
4375 ccline.cmdbuff = NULL;
4376 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004378
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004379 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004380
4381 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 current_SID = save_current_SID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004383
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004385
4386 return ret;
4387}
4388
4389/*
4390 * Expand names with a function defined by the user.
4391 */
4392 static int
4393ExpandUserDefined(xp, regmatch, num_file, file)
4394 expand_T *xp;
4395 regmatch_T *regmatch;
4396 int *num_file;
4397 char_u ***file;
4398{
4399 char_u *retstr;
4400 char_u *s;
4401 char_u *e;
4402 char_u keep;
4403 garray_T ga;
4404
4405 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
4406 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 return FAIL;
4408
4409 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004410 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
4412 e = vim_strchr(s, '\n');
4413 if (e == NULL)
4414 e = s + STRLEN(s);
4415 keep = *e;
4416 *e = 0;
4417
4418 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
4419 {
4420 *e = keep;
4421 if (*e != NUL)
4422 ++e;
4423 continue;
4424 }
4425
4426 if (ga_grow(&ga, 1) == FAIL)
4427 break;
4428
4429 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
4430 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431
4432 *e = keep;
4433 if (*e != NUL)
4434 ++e;
4435 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004436 vim_free(retstr);
4437 *file = ga.ga_data;
4438 *num_file = ga.ga_len;
4439 return OK;
4440}
4441
4442/*
4443 * Expand names with a list returned by a function defined by the user.
4444 */
4445 static int
4446ExpandUserList(xp, num_file, file)
4447 expand_T *xp;
4448 int *num_file;
4449 char_u ***file;
4450{
4451 list_T *retlist;
4452 listitem_T *li;
4453 garray_T ga;
4454
4455 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
4456 if (retlist == NULL)
4457 return FAIL;
4458
4459 ga_init2(&ga, (int)sizeof(char *), 3);
4460 /* Loop over the items in the list. */
4461 for (li = retlist->lv_first; li != NULL; li = li->li_next)
4462 {
4463 if (li->li_tv.v_type != VAR_STRING)
4464 continue; /* Skip non-string items */
4465
4466 if (ga_grow(&ga, 1) == FAIL)
4467 break;
4468
4469 ((char_u **)ga.ga_data)[ga.ga_len] =
4470 vim_strsave(li->li_tv.vval.v_string);
4471 ++ga.ga_len;
4472 }
4473 list_unref(retlist);
4474
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 *file = ga.ga_data;
4476 *num_file = ga.ga_len;
4477 return OK;
4478}
4479#endif
4480
4481/*
4482 * Expand color scheme names: 'runtimepath'/colors/{pat}.vim
4483 * or compiler names.
4484 */
4485 static int
4486ExpandRTDir(pat, num_file, file, dirname)
4487 char_u *pat;
4488 int *num_file;
4489 char_u ***file;
4490 char *dirname; /* "colors" or "compiler" */
4491{
4492 char_u *all;
4493 char_u *s;
4494 char_u *e;
4495 garray_T ga;
4496
4497 *num_file = 0;
4498 *file = NULL;
4499 s = alloc((unsigned)(STRLEN(pat) + STRLEN(dirname) + 7));
4500 if (s == NULL)
4501 return FAIL;
4502 sprintf((char *)s, "%s/%s*.vim", dirname, pat);
4503 all = globpath(p_rtp, s);
4504 vim_free(s);
4505 if (all == NULL)
4506 return FAIL;
4507
4508 ga_init2(&ga, (int)sizeof(char *), 3);
4509 for (s = all; *s != NUL; s = e)
4510 {
4511 e = vim_strchr(s, '\n');
4512 if (e == NULL)
4513 e = s + STRLEN(s);
4514 if (ga_grow(&ga, 1) == FAIL)
4515 break;
4516 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
4517 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004518 for (s = e - 4; s > all; mb_ptr_back(all, s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 if (*s == '\n' || vim_ispathsep(*s))
4520 break;
4521 ++s;
4522 ((char_u **)ga.ga_data)[ga.ga_len] =
4523 vim_strnsave(s, (int)(e - s - 4));
4524 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 if (*e != NUL)
4527 ++e;
4528 }
4529 vim_free(all);
4530 *file = ga.ga_data;
4531 *num_file = ga.ga_len;
4532 return OK;
4533}
4534
4535#endif
4536
4537#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
4538/*
4539 * Expand "file" for all comma-separated directories in "path".
4540 * Returns an allocated string with all matches concatenated, separated by
4541 * newlines. Returns NULL for an error or no matches.
4542 */
4543 char_u *
4544globpath(path, file)
4545 char_u *path;
4546 char_u *file;
4547{
4548 expand_T xpc;
4549 char_u *buf;
4550 garray_T ga;
4551 int i;
4552 int len;
4553 int num_p;
4554 char_u **p;
4555 char_u *cur = NULL;
4556
4557 buf = alloc(MAXPATHL);
4558 if (buf == NULL)
4559 return NULL;
4560
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004561 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004563
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 ga_init2(&ga, 1, 100);
4565
4566 /* Loop over all entries in {path}. */
4567 while (*path != NUL)
4568 {
4569 /* Copy one item of the path to buf[] and concatenate the file name. */
4570 copy_option_part(&path, buf, MAXPATHL, ",");
4571 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
4572 {
4573 add_pathsep(buf);
4574 STRCAT(buf, file);
4575 if (ExpandFromContext(&xpc, buf, &num_p, &p, WILD_SILENT) != FAIL
4576 && num_p > 0)
4577 {
4578 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT);
4579 for (len = 0, i = 0; i < num_p; ++i)
4580 len += (long_u)STRLEN(p[i]) + 1;
4581
4582 /* Concatenate new results to previous ones. */
4583 if (ga_grow(&ga, len) == OK)
4584 {
4585 cur = (char_u *)ga.ga_data + ga.ga_len;
4586 for (i = 0; i < num_p; ++i)
4587 {
4588 STRCPY(cur, p[i]);
4589 cur += STRLEN(p[i]);
4590 *cur++ = '\n';
4591 }
4592 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 }
4594 FreeWild(num_p, p);
4595 }
4596 }
4597 }
4598 if (cur != NULL)
4599 *--cur = 0; /* Replace trailing newline with NUL */
4600
4601 vim_free(buf);
4602 return (char_u *)ga.ga_data;
4603}
4604
4605#endif
4606
4607#if defined(FEAT_CMDHIST) || defined(PROTO)
4608
4609/*********************************
4610 * Command line history stuff *
4611 *********************************/
4612
4613/*
4614 * Translate a history character to the associated type number.
4615 */
4616 static int
4617hist_char2type(c)
4618 int c;
4619{
4620 if (c == ':')
4621 return HIST_CMD;
4622 if (c == '=')
4623 return HIST_EXPR;
4624 if (c == '@')
4625 return HIST_INPUT;
4626 if (c == '>')
4627 return HIST_DEBUG;
4628 return HIST_SEARCH; /* must be '?' or '/' */
4629}
4630
4631/*
4632 * Table of history names.
4633 * These names are used in :history and various hist...() functions.
4634 * It is sufficient to give the significant prefix of a history name.
4635 */
4636
4637static char *(history_names[]) =
4638{
4639 "cmd",
4640 "search",
4641 "expr",
4642 "input",
4643 "debug",
4644 NULL
4645};
4646
4647/*
4648 * init_history() - Initialize the command line history.
4649 * Also used to re-allocate the history when the size changes.
4650 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00004651 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652init_history()
4653{
4654 int newlen; /* new length of history table */
4655 histentry_T *temp;
4656 int i;
4657 int j;
4658 int type;
4659
4660 /*
4661 * If size of history table changed, reallocate it
4662 */
4663 newlen = (int)p_hi;
4664 if (newlen != hislen) /* history length changed */
4665 {
4666 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
4667 {
4668 if (newlen)
4669 {
4670 temp = (histentry_T *)lalloc(
4671 (long_u)(newlen * sizeof(histentry_T)), TRUE);
4672 if (temp == NULL) /* out of memory! */
4673 {
4674 if (type == 0) /* first one: just keep the old length */
4675 {
4676 newlen = hislen;
4677 break;
4678 }
4679 /* Already changed one table, now we can only have zero
4680 * length for all tables. */
4681 newlen = 0;
4682 type = -1;
4683 continue;
4684 }
4685 }
4686 else
4687 temp = NULL;
4688 if (newlen == 0 || temp != NULL)
4689 {
4690 if (hisidx[type] < 0) /* there are no entries yet */
4691 {
4692 for (i = 0; i < newlen; ++i)
4693 {
4694 temp[i].hisnum = 0;
4695 temp[i].hisstr = NULL;
4696 }
4697 }
4698 else if (newlen > hislen) /* array becomes bigger */
4699 {
4700 for (i = 0; i <= hisidx[type]; ++i)
4701 temp[i] = history[type][i];
4702 j = i;
4703 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
4704 {
4705 temp[i].hisnum = 0;
4706 temp[i].hisstr = NULL;
4707 }
4708 for ( ; j < hislen; ++i, ++j)
4709 temp[i] = history[type][j];
4710 }
4711 else /* array becomes smaller or 0 */
4712 {
4713 j = hisidx[type];
4714 for (i = newlen - 1; ; --i)
4715 {
4716 if (i >= 0) /* copy newest entries */
4717 temp[i] = history[type][j];
4718 else /* remove older entries */
4719 vim_free(history[type][j].hisstr);
4720 if (--j < 0)
4721 j = hislen - 1;
4722 if (j == hisidx[type])
4723 break;
4724 }
4725 hisidx[type] = newlen - 1;
4726 }
4727 vim_free(history[type]);
4728 history[type] = temp;
4729 }
4730 }
4731 hislen = newlen;
4732 }
4733}
4734
4735/*
4736 * Check if command line 'str' is already in history.
4737 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
4738 */
4739 static int
4740in_history(type, str, move_to_front)
4741 int type;
4742 char_u *str;
4743 int move_to_front; /* Move the entry to the front if it exists */
4744{
4745 int i;
4746 int last_i = -1;
4747
4748 if (hisidx[type] < 0)
4749 return FALSE;
4750 i = hisidx[type];
4751 do
4752 {
4753 if (history[type][i].hisstr == NULL)
4754 return FALSE;
4755 if (STRCMP(str, history[type][i].hisstr) == 0)
4756 {
4757 if (!move_to_front)
4758 return TRUE;
4759 last_i = i;
4760 break;
4761 }
4762 if (--i < 0)
4763 i = hislen - 1;
4764 } while (i != hisidx[type]);
4765
4766 if (last_i >= 0)
4767 {
4768 str = history[type][i].hisstr;
4769 while (i != hisidx[type])
4770 {
4771 if (++i >= hislen)
4772 i = 0;
4773 history[type][last_i] = history[type][i];
4774 last_i = i;
4775 }
4776 history[type][i].hisstr = str;
4777 history[type][i].hisnum = ++hisnum[type];
4778 return TRUE;
4779 }
4780 return FALSE;
4781}
4782
4783/*
4784 * Convert history name (from table above) to its HIST_ equivalent.
4785 * When "name" is empty, return "cmd" history.
4786 * Returns -1 for unknown history name.
4787 */
4788 int
4789get_histtype(name)
4790 char_u *name;
4791{
4792 int i;
4793 int len = (int)STRLEN(name);
4794
4795 /* No argument: use current history. */
4796 if (len == 0)
4797 return hist_char2type(ccline.cmdfirstc);
4798
4799 for (i = 0; history_names[i] != NULL; ++i)
4800 if (STRNICMP(name, history_names[i], len) == 0)
4801 return i;
4802
4803 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
4804 return hist_char2type(name[0]);
4805
4806 return -1;
4807}
4808
4809static int last_maptick = -1; /* last seen maptick */
4810
4811/*
4812 * Add the given string to the given history. If the string is already in the
4813 * history then it is moved to the front. "histype" may be one of he HIST_
4814 * values.
4815 */
4816 void
4817add_to_history(histype, new_entry, in_map, sep)
4818 int histype;
4819 char_u *new_entry;
4820 int in_map; /* consider maptick when inside a mapping */
4821 int sep; /* separator character used (search hist) */
4822{
4823 histentry_T *hisptr;
4824 int len;
4825
4826 if (hislen == 0) /* no history */
4827 return;
4828
4829 /*
4830 * Searches inside the same mapping overwrite each other, so that only
4831 * the last line is kept. Be careful not to remove a line that was moved
4832 * down, only lines that were added.
4833 */
4834 if (histype == HIST_SEARCH && in_map)
4835 {
4836 if (maptick == last_maptick)
4837 {
4838 /* Current line is from the same mapping, remove it */
4839 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
4840 vim_free(hisptr->hisstr);
4841 hisptr->hisstr = NULL;
4842 hisptr->hisnum = 0;
4843 --hisnum[histype];
4844 if (--hisidx[HIST_SEARCH] < 0)
4845 hisidx[HIST_SEARCH] = hislen - 1;
4846 }
4847 last_maptick = -1;
4848 }
4849 if (!in_history(histype, new_entry, TRUE))
4850 {
4851 if (++hisidx[histype] == hislen)
4852 hisidx[histype] = 0;
4853 hisptr = &history[histype][hisidx[histype]];
4854 vim_free(hisptr->hisstr);
4855
4856 /* Store the separator after the NUL of the string. */
4857 len = STRLEN(new_entry);
4858 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
4859 if (hisptr->hisstr != NULL)
4860 hisptr->hisstr[len + 1] = sep;
4861
4862 hisptr->hisnum = ++hisnum[histype];
4863 if (histype == HIST_SEARCH && in_map)
4864 last_maptick = maptick;
4865 }
4866}
4867
4868#if defined(FEAT_EVAL) || defined(PROTO)
4869
4870/*
4871 * Get identifier of newest history entry.
4872 * "histype" may be one of the HIST_ values.
4873 */
4874 int
4875get_history_idx(histype)
4876 int histype;
4877{
4878 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4879 || hisidx[histype] < 0)
4880 return -1;
4881
4882 return history[histype][hisidx[histype]].hisnum;
4883}
4884
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004885static struct cmdline_info *get_ccline_ptr __ARGS((void));
4886
4887/*
4888 * Get pointer to the command line info to use. cmdline_paste() may clear
4889 * ccline and put the previous value in prev_ccline.
4890 */
4891 static struct cmdline_info *
4892get_ccline_ptr()
4893{
4894 if ((State & CMDLINE) == 0)
4895 return NULL;
4896 if (ccline.cmdbuff != NULL)
4897 return &ccline;
4898 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
4899 return &prev_ccline;
4900 return NULL;
4901}
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903/*
4904 * Get the current command line in allocated memory.
4905 * Only works when the command line is being edited.
4906 * Returns NULL when something is wrong.
4907 */
4908 char_u *
4909get_cmdline_str()
4910{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004911 struct cmdline_info *p = get_ccline_ptr();
4912
4913 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004915 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916}
4917
4918/*
4919 * Get the current command line position, counted in bytes.
4920 * Zero is the first position.
4921 * Only works when the command line is being edited.
4922 * Returns -1 when something is wrong.
4923 */
4924 int
4925get_cmdline_pos()
4926{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004927 struct cmdline_info *p = get_ccline_ptr();
4928
4929 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004931 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932}
4933
4934/*
4935 * Set the command line byte position to "pos". Zero is the first position.
4936 * Only works when the command line is being edited.
4937 * Returns 1 when failed, 0 when OK.
4938 */
4939 int
4940set_cmdline_pos(pos)
4941 int pos;
4942{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00004943 struct cmdline_info *p = get_ccline_ptr();
4944
4945 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 return 1;
4947
4948 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
4949 * changed the command line. */
4950 if (pos < 0)
4951 new_cmdpos = 0;
4952 else
4953 new_cmdpos = pos;
4954 return 0;
4955}
4956
4957/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004958 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00004959 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004960 * Only works when the command line is being edited.
4961 * Returns NUL when something is wrong.
4962 */
4963 int
4964get_cmdline_type()
4965{
4966 struct cmdline_info *p = get_ccline_ptr();
4967
4968 if (p == NULL)
4969 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00004970 if (p->cmdfirstc == NUL)
4971 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004972 return p->cmdfirstc;
4973}
4974
4975/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 * Calculate history index from a number:
4977 * num > 0: seen as identifying number of a history entry
4978 * num < 0: relative position in history wrt newest entry
4979 * "histype" may be one of the HIST_ values.
4980 */
4981 static int
4982calc_hist_idx(histype, num)
4983 int histype;
4984 int num;
4985{
4986 int i;
4987 histentry_T *hist;
4988 int wrapped = FALSE;
4989
4990 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
4991 || (i = hisidx[histype]) < 0 || num == 0)
4992 return -1;
4993
4994 hist = history[histype];
4995 if (num > 0)
4996 {
4997 while (hist[i].hisnum > num)
4998 if (--i < 0)
4999 {
5000 if (wrapped)
5001 break;
5002 i += hislen;
5003 wrapped = TRUE;
5004 }
5005 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5006 return i;
5007 }
5008 else if (-num <= hislen)
5009 {
5010 i += num + 1;
5011 if (i < 0)
5012 i += hislen;
5013 if (hist[i].hisstr != NULL)
5014 return i;
5015 }
5016 return -1;
5017}
5018
5019/*
5020 * Get a history entry by its index.
5021 * "histype" may be one of the HIST_ values.
5022 */
5023 char_u *
5024get_history_entry(histype, idx)
5025 int histype;
5026 int idx;
5027{
5028 idx = calc_hist_idx(histype, idx);
5029 if (idx >= 0)
5030 return history[histype][idx].hisstr;
5031 else
5032 return (char_u *)"";
5033}
5034
5035/*
5036 * Clear all entries of a history.
5037 * "histype" may be one of the HIST_ values.
5038 */
5039 int
5040clr_history(histype)
5041 int histype;
5042{
5043 int i;
5044 histentry_T *hisptr;
5045
5046 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5047 {
5048 hisptr = history[histype];
5049 for (i = hislen; i--;)
5050 {
5051 vim_free(hisptr->hisstr);
5052 hisptr->hisnum = 0;
5053 hisptr++->hisstr = NULL;
5054 }
5055 hisidx[histype] = -1; /* mark history as cleared */
5056 hisnum[histype] = 0; /* reset identifier counter */
5057 return OK;
5058 }
5059 return FAIL;
5060}
5061
5062/*
5063 * Remove all entries matching {str} from a history.
5064 * "histype" may be one of the HIST_ values.
5065 */
5066 int
5067del_history_entry(histype, str)
5068 int histype;
5069 char_u *str;
5070{
5071 regmatch_T regmatch;
5072 histentry_T *hisptr;
5073 int idx;
5074 int i;
5075 int last;
5076 int found = FALSE;
5077
5078 regmatch.regprog = NULL;
5079 regmatch.rm_ic = FALSE; /* always match case */
5080 if (hislen != 0
5081 && histype >= 0
5082 && histype < HIST_COUNT
5083 && *str != NUL
5084 && (idx = hisidx[histype]) >= 0
5085 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5086 != NULL)
5087 {
5088 i = last = idx;
5089 do
5090 {
5091 hisptr = &history[histype][i];
5092 if (hisptr->hisstr == NULL)
5093 break;
5094 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5095 {
5096 found = TRUE;
5097 vim_free(hisptr->hisstr);
5098 hisptr->hisstr = NULL;
5099 hisptr->hisnum = 0;
5100 }
5101 else
5102 {
5103 if (i != last)
5104 {
5105 history[histype][last] = *hisptr;
5106 hisptr->hisstr = NULL;
5107 hisptr->hisnum = 0;
5108 }
5109 if (--last < 0)
5110 last += hislen;
5111 }
5112 if (--i < 0)
5113 i += hislen;
5114 } while (i != idx);
5115 if (history[histype][idx].hisstr == NULL)
5116 hisidx[histype] = -1;
5117 }
5118 vim_free(regmatch.regprog);
5119 return found;
5120}
5121
5122/*
5123 * Remove an indexed entry from a history.
5124 * "histype" may be one of the HIST_ values.
5125 */
5126 int
5127del_history_idx(histype, idx)
5128 int histype;
5129 int idx;
5130{
5131 int i, j;
5132
5133 i = calc_hist_idx(histype, idx);
5134 if (i < 0)
5135 return FALSE;
5136 idx = hisidx[histype];
5137 vim_free(history[histype][i].hisstr);
5138
5139 /* When deleting the last added search string in a mapping, reset
5140 * last_maptick, so that the last added search string isn't deleted again.
5141 */
5142 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5143 last_maptick = -1;
5144
5145 while (i != idx)
5146 {
5147 j = (i + 1) % hislen;
5148 history[histype][i] = history[histype][j];
5149 i = j;
5150 }
5151 history[histype][i].hisstr = NULL;
5152 history[histype][i].hisnum = 0;
5153 if (--i < 0)
5154 i += hislen;
5155 hisidx[histype] = i;
5156 return TRUE;
5157}
5158
5159#endif /* FEAT_EVAL */
5160
5161#if defined(FEAT_CRYPT) || defined(PROTO)
5162/*
5163 * Very specific function to remove the value in ":set key=val" from the
5164 * history.
5165 */
5166 void
5167remove_key_from_history()
5168{
5169 char_u *p;
5170 int i;
5171
5172 i = hisidx[HIST_CMD];
5173 if (i < 0)
5174 return;
5175 p = history[HIST_CMD][i].hisstr;
5176 if (p != NULL)
5177 for ( ; *p; ++p)
5178 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5179 {
5180 p = vim_strchr(p + 3, '=');
5181 if (p == NULL)
5182 break;
5183 ++p;
5184 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5185 if (p[i] == '\\' && p[i + 1])
5186 ++i;
5187 mch_memmove(p, p + i, STRLEN(p + i) + 1);
5188 --p;
5189 }
5190}
5191#endif
5192
5193#endif /* FEAT_CMDHIST */
5194
5195#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5196/*
5197 * Get indices "num1,num2" that specify a range within a list (not a range of
5198 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5199 * Returns OK if parsed successfully, otherwise FAIL.
5200 */
5201 int
5202get_list_range(str, num1, num2)
5203 char_u **str;
5204 int *num1;
5205 int *num2;
5206{
5207 int len;
5208 int first = FALSE;
5209 long num;
5210
5211 *str = skipwhite(*str);
5212 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5213 {
5214 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5215 *str += len;
5216 *num1 = (int)num;
5217 first = TRUE;
5218 }
5219 *str = skipwhite(*str);
5220 if (**str == ',') /* parse "to" part of range */
5221 {
5222 *str = skipwhite(*str + 1);
5223 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5224 if (len > 0)
5225 {
5226 *num2 = (int)num;
5227 *str = skipwhite(*str + len);
5228 }
5229 else if (!first) /* no number given at all */
5230 return FAIL;
5231 }
5232 else if (first) /* only one number given */
5233 *num2 = *num1;
5234 return OK;
5235}
5236#endif
5237
5238#if defined(FEAT_CMDHIST) || defined(PROTO)
5239/*
5240 * :history command - print a history
5241 */
5242 void
5243ex_history(eap)
5244 exarg_T *eap;
5245{
5246 histentry_T *hist;
5247 int histype1 = HIST_CMD;
5248 int histype2 = HIST_CMD;
5249 int hisidx1 = 1;
5250 int hisidx2 = -1;
5251 int idx;
5252 int i, j, k;
5253 char_u *end;
5254 char_u *arg = eap->arg;
5255
5256 if (hislen == 0)
5257 {
5258 MSG(_("'history' option is zero"));
5259 return;
5260 }
5261
5262 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5263 {
5264 end = arg;
5265 while (ASCII_ISALPHA(*end)
5266 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5267 end++;
5268 i = *end;
5269 *end = NUL;
5270 histype1 = get_histtype(arg);
5271 if (histype1 == -1)
5272 {
5273 if (STRICMP(arg, "all") == 0)
5274 {
5275 histype1 = 0;
5276 histype2 = HIST_COUNT-1;
5277 }
5278 else
5279 {
5280 *end = i;
5281 EMSG(_(e_trailing));
5282 return;
5283 }
5284 }
5285 else
5286 histype2 = histype1;
5287 *end = i;
5288 }
5289 else
5290 end = arg;
5291 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5292 {
5293 EMSG(_(e_trailing));
5294 return;
5295 }
5296
5297 for (; !got_int && histype1 <= histype2; ++histype1)
5298 {
5299 STRCPY(IObuff, "\n # ");
5300 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5301 MSG_PUTS_TITLE(IObuff);
5302 idx = hisidx[histype1];
5303 hist = history[histype1];
5304 j = hisidx1;
5305 k = hisidx2;
5306 if (j < 0)
5307 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5308 if (k < 0)
5309 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5310 if (idx >= 0 && j <= k)
5311 for (i = idx + 1; !got_int; ++i)
5312 {
5313 if (i == hislen)
5314 i = 0;
5315 if (hist[i].hisstr != NULL
5316 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5317 {
5318 msg_putchar('\n');
5319 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5320 hist[i].hisnum);
5321 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5322 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
5323 (int)Columns - 10);
5324 else
5325 STRCAT(IObuff, hist[i].hisstr);
5326 msg_outtrans(IObuff);
5327 out_flush();
5328 }
5329 if (i == idx)
5330 break;
5331 }
5332 }
5333}
5334#endif
5335
5336#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
5337static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
5338static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
5339static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
5340static int viminfo_add_at_front = FALSE;
5341
5342static int hist_type2char __ARGS((int type, int use_question));
5343
5344/*
5345 * Translate a history type number to the associated character.
5346 */
5347 static int
5348hist_type2char(type, use_question)
5349 int type;
5350 int use_question; /* use '?' instead of '/' */
5351{
5352 if (type == HIST_CMD)
5353 return ':';
5354 if (type == HIST_SEARCH)
5355 {
5356 if (use_question)
5357 return '?';
5358 else
5359 return '/';
5360 }
5361 if (type == HIST_EXPR)
5362 return '=';
5363 return '@';
5364}
5365
5366/*
5367 * Prepare for reading the history from the viminfo file.
5368 * This allocates history arrays to store the read history lines.
5369 */
5370 void
5371prepare_viminfo_history(asklen)
5372 int asklen;
5373{
5374 int i;
5375 int num;
5376 int type;
5377 int len;
5378
5379 init_history();
5380 viminfo_add_at_front = (asklen != 0);
5381 if (asklen > hislen)
5382 asklen = hislen;
5383
5384 for (type = 0; type < HIST_COUNT; ++type)
5385 {
5386 /*
5387 * Count the number of empty spaces in the history list. If there are
5388 * more spaces available than we request, then fill them up.
5389 */
5390 for (i = 0, num = 0; i < hislen; i++)
5391 if (history[type][i].hisstr == NULL)
5392 num++;
5393 len = asklen;
5394 if (num > len)
5395 len = num;
5396 if (len <= 0)
5397 viminfo_history[type] = NULL;
5398 else
5399 viminfo_history[type] =
5400 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
5401 if (viminfo_history[type] == NULL)
5402 len = 0;
5403 viminfo_hislen[type] = len;
5404 viminfo_hisidx[type] = 0;
5405 }
5406}
5407
5408/*
5409 * Accept a line from the viminfo, store it in the history array when it's
5410 * new.
5411 */
5412 int
5413read_viminfo_history(virp)
5414 vir_T *virp;
5415{
5416 int type;
5417 long_u len;
5418 char_u *val;
5419 char_u *p;
5420
5421 type = hist_char2type(virp->vir_line[0]);
5422 if (viminfo_hisidx[type] < viminfo_hislen[type])
5423 {
5424 val = viminfo_readstring(virp, 1, TRUE);
5425 if (val != NULL && *val != NUL)
5426 {
5427 if (!in_history(type, val + (type == HIST_SEARCH),
5428 viminfo_add_at_front))
5429 {
5430 /* Need to re-allocate to append the separator byte. */
5431 len = STRLEN(val);
5432 p = lalloc(len + 2, TRUE);
5433 if (p != NULL)
5434 {
5435 if (type == HIST_SEARCH)
5436 {
5437 /* Search entry: Move the separator from the first
5438 * column to after the NUL. */
5439 mch_memmove(p, val + 1, (size_t)len);
5440 p[len] = (*val == ' ' ? NUL : *val);
5441 }
5442 else
5443 {
5444 /* Not a search entry: No separator in the viminfo
5445 * file, add a NUL separator. */
5446 mch_memmove(p, val, (size_t)len + 1);
5447 p[len + 1] = NUL;
5448 }
5449 viminfo_history[type][viminfo_hisidx[type]++] = p;
5450 }
5451 }
5452 }
5453 vim_free(val);
5454 }
5455 return viminfo_readline(virp);
5456}
5457
5458 void
5459finish_viminfo_history()
5460{
5461 int idx;
5462 int i;
5463 int type;
5464
5465 for (type = 0; type < HIST_COUNT; ++type)
5466 {
5467 if (history[type] == NULL)
5468 return;
5469 idx = hisidx[type] + viminfo_hisidx[type];
5470 if (idx >= hislen)
5471 idx -= hislen;
5472 else if (idx < 0)
5473 idx = hislen - 1;
5474 if (viminfo_add_at_front)
5475 hisidx[type] = idx;
5476 else
5477 {
5478 if (hisidx[type] == -1)
5479 hisidx[type] = hislen - 1;
5480 do
5481 {
5482 if (history[type][idx].hisstr != NULL)
5483 break;
5484 if (++idx == hislen)
5485 idx = 0;
5486 } while (idx != hisidx[type]);
5487 if (idx != hisidx[type] && --idx < 0)
5488 idx = hislen - 1;
5489 }
5490 for (i = 0; i < viminfo_hisidx[type]; i++)
5491 {
5492 vim_free(history[type][idx].hisstr);
5493 history[type][idx].hisstr = viminfo_history[type][i];
5494 if (--idx < 0)
5495 idx = hislen - 1;
5496 }
5497 idx += 1;
5498 idx %= hislen;
5499 for (i = 0; i < viminfo_hisidx[type]; i++)
5500 {
5501 history[type][idx++].hisnum = ++hisnum[type];
5502 idx %= hislen;
5503 }
5504 vim_free(viminfo_history[type]);
5505 viminfo_history[type] = NULL;
5506 }
5507}
5508
5509 void
5510write_viminfo_history(fp)
5511 FILE *fp;
5512{
5513 int i;
5514 int type;
5515 int num_saved;
5516 char_u *p;
5517 int c;
5518
5519 init_history();
5520 if (hislen == 0)
5521 return;
5522 for (type = 0; type < HIST_COUNT; ++type)
5523 {
5524 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
5525 if (num_saved == 0)
5526 continue;
5527 if (num_saved < 0) /* Use default */
5528 num_saved = hislen;
5529 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
5530 type == HIST_CMD ? _("Command Line") :
5531 type == HIST_SEARCH ? _("Search String") :
5532 type == HIST_EXPR ? _("Expression") :
5533 _("Input Line"));
5534 if (num_saved > hislen)
5535 num_saved = hislen;
5536 i = hisidx[type];
5537 if (i >= 0)
5538 while (num_saved--)
5539 {
5540 p = history[type][i].hisstr;
5541 if (p != NULL)
5542 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00005543 fputc(hist_type2char(type, TRUE), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 /* For the search history: put the separator in the second
5545 * column; use a space if there isn't one. */
5546 if (type == HIST_SEARCH)
5547 {
5548 c = p[STRLEN(p) + 1];
5549 putc(c == NUL ? ' ' : c, fp);
5550 }
5551 viminfo_writestring(fp, p);
5552 }
5553 if (--i < 0)
5554 i = hislen - 1;
5555 }
5556 }
5557}
5558#endif /* FEAT_VIMINFO */
5559
5560#if defined(FEAT_FKMAP) || defined(PROTO)
5561/*
5562 * Write a character at the current cursor+offset position.
5563 * It is directly written into the command buffer block.
5564 */
5565 void
5566cmd_pchar(c, offset)
5567 int c, offset;
5568{
5569 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5570 {
5571 EMSG(_("E198: cmd_pchar beyond the command length"));
5572 return;
5573 }
5574 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
5575 ccline.cmdbuff[ccline.cmdlen] = NUL;
5576}
5577
5578 int
5579cmd_gchar(offset)
5580 int offset;
5581{
5582 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
5583 {
5584 /* EMSG(_("cmd_gchar beyond the command length")); */
5585 return NUL;
5586 }
5587 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
5588}
5589#endif
5590
5591#if defined(FEAT_CMDWIN) || defined(PROTO)
5592/*
5593 * Open a window on the current command line and history. Allow editing in
5594 * the window. Returns when the window is closed.
5595 * Returns:
5596 * CR if the command is to be executed
5597 * Ctrl_C if it is to be abandoned
5598 * K_IGNORE if editing continues
5599 */
5600 static int
5601ex_window()
5602{
5603 struct cmdline_info save_ccline;
5604 buf_T *old_curbuf = curbuf;
5605 win_T *old_curwin = curwin;
5606 buf_T *bp;
5607 win_T *wp;
5608 int i;
5609 linenr_T lnum;
5610 int histtype;
5611 garray_T winsizes;
5612 char_u typestr[2];
5613 int save_restart_edit = restart_edit;
5614 int save_State = State;
5615 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00005616#ifdef FEAT_RIGHTLEFT
5617 int save_cmdmsg_rl = cmdmsg_rl;
5618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 /* Can't do this recursively. Can't do it when typing a password. */
5621 if (cmdwin_type != 0
5622# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
5623 || cmdline_star > 0
5624# endif
5625 )
5626 {
5627 beep_flush();
5628 return K_IGNORE;
5629 }
5630
5631 /* Save current window sizes. */
5632 win_size_save(&winsizes);
5633
5634# ifdef FEAT_AUTOCMD
5635 /* Don't execute autocommands while creating the window. */
5636 ++autocmd_block;
5637# endif
5638 /* Create a window for the command-line buffer. */
5639 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
5640 {
5641 beep_flush();
5642 return K_IGNORE;
5643 }
5644 cmdwin_type = ccline.cmdfirstc;
5645 if (cmdwin_type == NUL)
5646 cmdwin_type = '-';
5647
5648 /* Create the command-line buffer empty. */
5649 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE);
5650 (void)setfname(curbuf, (char_u *)"command-line", NULL, TRUE);
5651 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
5652 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
5653 curbuf->b_p_ma = TRUE;
5654# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00005655 curwin->w_p_rl = cmdmsg_rl;
5656 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657# endif
5658# ifdef FEAT_SCROLLBIND
5659 curwin->w_p_scb = FALSE;
5660# endif
5661
5662# ifdef FEAT_AUTOCMD
5663 /* Do execute autocommands for setting the filetype (load syntax). */
5664 --autocmd_block;
5665# endif
5666
Bram Moolenaar46152342005-09-07 21:18:43 +00005667 /* Showing the prompt may have set need_wait_return, reset it. */
5668 need_wait_return = FALSE;
5669
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 histtype = hist_char2type(ccline.cmdfirstc);
5671 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
5672 {
5673 if (p_wc == TAB)
5674 {
5675 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
5676 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
5677 }
5678 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
5679 }
5680
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00005681 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
5682 * sets 'textwidth' to 78). */
5683 curbuf->b_p_tw = 0;
5684
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 /* Fill the buffer with the history. */
5686 init_history();
5687 if (hislen > 0)
5688 {
5689 i = hisidx[histtype];
5690 if (i >= 0)
5691 {
5692 lnum = 0;
5693 do
5694 {
5695 if (++i == hislen)
5696 i = 0;
5697 if (history[histtype][i].hisstr != NULL)
5698 ml_append(lnum++, history[histtype][i].hisstr,
5699 (colnr_T)0, FALSE);
5700 }
5701 while (i != hisidx[histtype]);
5702 }
5703 }
5704
5705 /* Replace the empty last line with the current command-line and put the
5706 * cursor there. */
5707 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
5708 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
5709 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00005710 changed_line_abv_curs();
5711 invalidate_botline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 redraw_later(NOT_VALID);
5713
5714 /* Save the command line info, can be used recursively. */
5715 save_ccline = ccline;
5716 ccline.cmdbuff = NULL;
5717 ccline.cmdprompt = NULL;
5718
5719 /* No Ex mode here! */
5720 exmode_active = 0;
5721
5722 State = NORMAL;
5723# ifdef FEAT_MOUSE
5724 setmouse();
5725# endif
5726
5727# ifdef FEAT_AUTOCMD
5728 /* Trigger CmdwinEnter autocommands. */
5729 typestr[0] = cmdwin_type;
5730 typestr[1] = NUL;
5731 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
5732# endif
5733
5734 i = RedrawingDisabled;
5735 RedrawingDisabled = 0;
5736
5737 /*
5738 * Call the main loop until <CR> or CTRL-C is typed.
5739 */
5740 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005741 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742
5743 RedrawingDisabled = i;
5744
5745# ifdef FEAT_AUTOCMD
5746 /* Trigger CmdwinLeave autocommands. */
5747 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
5748# endif
5749
5750 /* Restore the comand line info. */
5751 ccline = save_ccline;
5752 cmdwin_type = 0;
5753
5754 exmode_active = save_exmode;
5755
5756 /* Safety check: The old window or buffer was deleted: It's a a bug when
5757 * this happens! */
5758 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
5759 {
5760 cmdwin_result = Ctrl_C;
5761 EMSG(_("E199: Active window or buffer deleted"));
5762 }
5763 else
5764 {
5765# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5766 /* autocmds may abort script processing */
5767 if (aborting() && cmdwin_result != K_IGNORE)
5768 cmdwin_result = Ctrl_C;
5769# endif
5770 /* Set the new command line from the cmdline buffer. */
5771 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00005772 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 {
Bram Moolenaar46152342005-09-07 21:18:43 +00005774 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
5775
5776 if (histtype == HIST_CMD)
5777 {
5778 /* Execute the command directly. */
5779 ccline.cmdbuff = vim_strsave((char_u *)p);
5780 cmdwin_result = CAR;
5781 }
5782 else
5783 {
5784 /* First need to cancel what we were doing. */
5785 ccline.cmdbuff = NULL;
5786 stuffcharReadbuff(':');
5787 stuffReadbuff((char_u *)p);
5788 stuffcharReadbuff(CAR);
5789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
5791 else if (cmdwin_result == K_XF2) /* :qa typed */
5792 {
5793 ccline.cmdbuff = vim_strsave((char_u *)"qa");
5794 cmdwin_result = CAR;
5795 }
5796 else
5797 ccline.cmdbuff = vim_strsave(ml_get_curline());
5798 if (ccline.cmdbuff == NULL)
5799 cmdwin_result = Ctrl_C;
5800 else
5801 {
5802 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
5803 ccline.cmdbufflen = ccline.cmdlen + 1;
5804 ccline.cmdpos = curwin->w_cursor.col;
5805 if (ccline.cmdpos > ccline.cmdlen)
5806 ccline.cmdpos = ccline.cmdlen;
5807 if (cmdwin_result == K_IGNORE)
5808 {
5809 set_cmdspos_cursor();
5810 redrawcmd();
5811 }
5812 }
5813
5814# ifdef FEAT_AUTOCMD
5815 /* Don't execute autocommands while deleting the window. */
5816 ++autocmd_block;
5817# endif
5818 wp = curwin;
5819 bp = curbuf;
5820 win_goto(old_curwin);
5821 win_close(wp, TRUE);
5822 close_buffer(NULL, bp, DOBUF_WIPE);
5823
5824 /* Restore window sizes. */
5825 win_size_restore(&winsizes);
5826
5827# ifdef FEAT_AUTOCMD
5828 --autocmd_block;
5829# endif
5830 }
5831
5832 ga_clear(&winsizes);
5833 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00005834# ifdef FEAT_RIGHTLEFT
5835 cmdmsg_rl = save_cmdmsg_rl;
5836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838 State = save_State;
5839# ifdef FEAT_MOUSE
5840 setmouse();
5841# endif
5842
5843 return cmdwin_result;
5844}
5845#endif /* FEAT_CMDWIN */
5846
5847/*
5848 * Used for commands that either take a simple command string argument, or:
5849 * cmd << endmarker
5850 * {script}
5851 * endmarker
5852 * Returns a pointer to allocated memory with {script} or NULL.
5853 */
5854 char_u *
5855script_get(eap, cmd)
5856 exarg_T *eap;
5857 char_u *cmd;
5858{
5859 char_u *theline;
5860 char *end_pattern = NULL;
5861 char dot[] = ".";
5862 garray_T ga;
5863
5864 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
5865 return NULL;
5866
5867 ga_init2(&ga, 1, 0x400);
5868
5869 if (cmd[2] != NUL)
5870 end_pattern = (char *)skipwhite(cmd + 2);
5871 else
5872 end_pattern = dot;
5873
5874 for (;;)
5875 {
5876 theline = eap->getline(
5877#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00005878 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879#endif
5880 NUL, eap->cookie, 0);
5881
5882 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
5883 break;
5884
5885 ga_concat(&ga, theline);
5886 ga_append(&ga, '\n');
5887 vim_free(theline);
5888 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00005889 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890
5891 return (char_u *)ga.ga_data;
5892}