blob: 740cca204854daeb5b3cd011b7d784f197e8d058 [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 */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 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 Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
55#ifdef FEAT_CMDHIST
56typedef struct hist_entry
57{
58 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020059 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 char_u *hisstr; /* actual entry, separator char after the NUL */
61} histentry_T;
62
63static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
64static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
65static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
66 /* identifying (unique) number of newest history entry */
67static int hislen = 0; /* actual length of history tables */
68
69static int hist_char2type __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaar07219f92013-04-14 23:19:36 +020071static int in_history __ARGS((int, char_u *, int, int, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_EVAL
73static int calc_hist_idx __ARGS((int histype, int num));
74# endif
75#endif
76
77#ifdef FEAT_RIGHTLEFT
78static int cmd_hkmap = 0; /* Hebrew mapping during command line */
79#endif
80
81#ifdef FEAT_FKMAP
82static int cmd_fkmap = 0; /* Farsi mapping during command line */
83#endif
84
85static int cmdline_charsize __ARGS((int idx));
86static void set_cmdspos __ARGS((void));
87static void set_cmdspos_cursor __ARGS((void));
88#ifdef FEAT_MBYTE
89static void correct_cmdspos __ARGS((int idx, int cells));
90#endif
91static void alloc_cmdbuff __ARGS((int len));
92static int realloc_cmdbuff __ARGS((int len));
93static void draw_cmdline __ARGS((int start, int len));
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +000094static void save_cmdline __ARGS((struct cmdline_info *ccp));
95static void restore_cmdline __ARGS((struct cmdline_info *ccp));
Bram Moolenaar1769d5a2006-10-17 14:25:24 +000096static int cmdline_paste __ARGS((int regname, int literally, int remcr));
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
98static void redrawcmd_preedit __ARGS((void));
99#endif
100#ifdef FEAT_WILDMENU
101static void cmdline_del __ARGS((int from));
102#endif
103static void redrawcmdprompt __ARGS((void));
104static void cursorcmd __ARGS((void));
105static int ccheck_abbr __ARGS((int));
Bram Moolenaarb3479632012-11-28 16:49:58 +0100106static int nextwild __ARGS((expand_T *xp, int type, int options, int escape));
Bram Moolenaar45360022005-07-21 21:08:21 +0000107static void escape_fname __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108static int showmatches __ARGS((expand_T *xp, int wildmenu));
109static void set_expand_context __ARGS((expand_T *xp));
110static int ExpandFromContext __ARGS((expand_T *xp, char_u *, int *, char_u ***, int));
111static int expand_showtail __ARGS((expand_T *xp));
112#ifdef FEAT_CMDL_COMPL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000113static int expand_shellcmd __ARGS((char_u *filepat, int *num_file, char_u ***file, int flagsarg));
Bram Moolenaar0c7437a2011-06-26 19:40:23 +0200114static int ExpandRTDir __ARGS((char_u *pat, int *num_file, char_u ***file, char *dirname[]));
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200115# ifdef FEAT_CMDHIST
116static char_u *get_history_arg __ARGS((expand_T *xp, int idx));
117# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
119static int ExpandUserDefined __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
Bram Moolenaar35fdbb52005-07-09 21:08:57 +0000120static int ExpandUserList __ARGS((expand_T *xp, int *num_file, char_u ***file));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121# endif
122#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200123#ifdef FEAT_CMDHIST
124static void clear_hist_entry __ARGS((histentry_T *hisptr));
125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127#ifdef FEAT_CMDWIN
128static int ex_window __ARGS((void));
129#endif
130
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200131#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
132static int
133#ifdef __BORLANDC__
134_RTLENTRYF
135#endif
136sort_func_compare __ARGS((const void *s1, const void *s2));
137#endif
138
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139/*
140 * getcmdline() - accept a command line starting with firstc.
141 *
142 * firstc == ':' get ":" command line.
143 * firstc == '/' or '?' get search pattern
144 * firstc == '=' get expression
145 * firstc == '@' get text for input() function
146 * firstc == '>' get text for debug mode
147 * firstc == NUL get text for :insert command
148 * firstc == -1 like NUL, and break on CTRL-C
149 *
150 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
151 * command line.
152 *
153 * Careful: getcmdline() can be called recursively!
154 *
155 * Return pointer to allocated string if there is a commandline, NULL
156 * otherwise.
157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 char_u *
159getcmdline(firstc, count, indent)
160 int firstc;
Bram Moolenaar78a15312009-05-15 19:33:18 +0000161 long count UNUSED; /* only used for incremental search */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162 int indent; /* indent for inside conditionals */
163{
164 int c;
165 int i;
166 int j;
167 int gotesc = FALSE; /* TRUE when <ESC> just typed */
168 int do_abbr; /* when TRUE check for abbr. */
169#ifdef FEAT_CMDHIST
170 char_u *lookfor = NULL; /* string to match */
171 int hiscnt; /* current history line in use */
172 int histype; /* history type to be used */
173#endif
174#ifdef FEAT_SEARCH_EXTRA
175 pos_T old_cursor;
176 colnr_T old_curswant;
177 colnr_T old_leftcol;
178 linenr_T old_topline;
179# ifdef FEAT_DIFF
180 int old_topfill;
181# endif
182 linenr_T old_botline;
183 int did_incsearch = FALSE;
184 int incsearch_postponed = FALSE;
185#endif
186 int did_wild_list = FALSE; /* did wild_list() recently */
187 int wim_index = 0; /* index in wim_flags[] */
188 int res;
189 int save_msg_scroll = msg_scroll;
190 int save_State = State; /* remember State when called */
191 int some_key_typed = FALSE; /* one of the keys was typed */
192#ifdef FEAT_MOUSE
193 /* mouse drag and release events are ignored, unless they are
194 * preceded with a mouse down event */
195 int ignore_drag_release = TRUE;
196#endif
197#ifdef FEAT_EVAL
198 int break_ctrl_c = FALSE;
199#endif
200 expand_T xpc;
201 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000202#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
203 /* Everything that may work recursively should save and restore the
204 * current command line in save_ccline. That includes update_screen(), a
205 * custom status line may invoke ":normal". */
206 struct cmdline_info save_ccline;
207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
209#ifdef FEAT_SNIFF
210 want_sniff_request = 0;
211#endif
212#ifdef FEAT_EVAL
213 if (firstc == -1)
214 {
215 firstc = NUL;
216 break_ctrl_c = TRUE;
217 }
218#endif
219#ifdef FEAT_RIGHTLEFT
220 /* start without Hebrew mapping for a command line */
221 if (firstc == ':' || firstc == '=' || firstc == '>')
222 cmd_hkmap = 0;
223#endif
224
225 ccline.overstrike = FALSE; /* always start in insert mode */
226#ifdef FEAT_SEARCH_EXTRA
227 old_cursor = curwin->w_cursor; /* needs to be restored later */
228 old_curswant = curwin->w_curswant;
229 old_leftcol = curwin->w_leftcol;
230 old_topline = curwin->w_topline;
231# ifdef FEAT_DIFF
232 old_topfill = curwin->w_topfill;
233# endif
234 old_botline = curwin->w_botline;
235#endif
236
237 /*
238 * set some variables for redrawcmd()
239 */
240 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000241 ccline.cmdindent = (firstc > 0 ? indent : 0);
242
243 /* alloc initial ccline.cmdbuff */
244 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 if (ccline.cmdbuff == NULL)
246 return NULL; /* out of memory */
247 ccline.cmdlen = ccline.cmdpos = 0;
248 ccline.cmdbuff[0] = NUL;
249
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000250 /* autoindent for :insert and :append */
251 if (firstc <= 0)
252 {
253 copy_spaces(ccline.cmdbuff, indent);
254 ccline.cmdbuff[indent] = NUL;
255 ccline.cmdpos = indent;
256 ccline.cmdspos = indent;
257 ccline.cmdlen = indent;
258 }
259
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000261 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263#ifdef FEAT_RIGHTLEFT
264 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
265 && (firstc == '/' || firstc == '?'))
266 cmdmsg_rl = TRUE;
267 else
268 cmdmsg_rl = FALSE;
269#endif
270
271 redir_off = TRUE; /* don't redirect the typed command */
272 if (!cmd_silent)
273 {
274 i = msg_scrolled;
275 msg_scrolled = 0; /* avoid wait_return message */
276 gotocmdline(TRUE);
277 msg_scrolled += i;
278 redrawcmdprompt(); /* draw prompt or indent */
279 set_cmdspos();
280 }
281 xpc.xp_context = EXPAND_NOTHING;
282 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000283#ifndef BACKSLASH_IN_FILENAME
284 xpc.xp_shell = FALSE;
285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000287#if defined(FEAT_EVAL)
288 if (ccline.input_fn)
289 {
290 xpc.xp_context = ccline.xp_context;
291 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000292# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000293 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000294# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000295 }
296#endif
297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 /*
299 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
300 * doing ":@0" when register 0 doesn't contain a CR.
301 */
302 msg_scroll = FALSE;
303
304 State = CMDLINE;
305
306 if (firstc == '/' || firstc == '?' || firstc == '@')
307 {
308 /* Use ":lmap" mappings for search pattern and input(). */
309 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
310 b_im_ptr = &curbuf->b_p_iminsert;
311 else
312 b_im_ptr = &curbuf->b_p_imsearch;
313 if (*b_im_ptr == B_IMODE_LMAP)
314 State |= LANGMAP;
315#ifdef USE_IM_CONTROL
316 im_set_active(*b_im_ptr == B_IMODE_IM);
317#endif
318 }
319#ifdef USE_IM_CONTROL
320 else if (p_imcmdline)
321 im_set_active(TRUE);
322#endif
323
324#ifdef FEAT_MOUSE
325 setmouse();
326#endif
327#ifdef CURSOR_SHAPE
328 ui_cursor_shape(); /* may show different cursor shape */
329#endif
330
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000331 /* When inside an autocommand for writing "exiting" may be set and
332 * terminal mode set to cooked. Need to set raw mode here then. */
333 settmode(TMODE_RAW);
334
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335#ifdef FEAT_CMDHIST
336 init_history();
337 hiscnt = hislen; /* set hiscnt to impossible history value */
338 histype = hist_char2type(firstc);
339#endif
340
341#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000342 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343#endif
344
345 /*
346 * Collect the command string, handling editing keys.
347 */
348 for (;;)
349 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000350 redir_off = TRUE; /* Don't redirect the typed command.
351 Repeated, because a ":redir" inside
352 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef USE_ON_FLY_SCROLL
354 dont_scroll = FALSE; /* allow scrolling here */
355#endif
356 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
357
358 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000359
360 /* Get a character. Ignore K_IGNORE, it should not do anything, such
361 * as stop completion. */
362 do
363 {
364 c = safe_vgetc();
365 } while (c == K_IGNORE);
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 if (KeyTyped)
368 {
369 some_key_typed = TRUE;
370#ifdef FEAT_RIGHTLEFT
371 if (cmd_hkmap)
372 c = hkmap(c);
373# ifdef FEAT_FKMAP
374 if (cmd_fkmap)
375 c = cmdl_fkmap(c);
376# endif
377 if (cmdmsg_rl && !KeyStuffed)
378 {
379 /* Invert horizontal movements and operations. Only when
380 * typed by the user directly, not when the result of a
381 * mapping. */
382 switch (c)
383 {
384 case K_RIGHT: c = K_LEFT; break;
385 case K_S_RIGHT: c = K_S_LEFT; break;
386 case K_C_RIGHT: c = K_C_LEFT; break;
387 case K_LEFT: c = K_RIGHT; break;
388 case K_S_LEFT: c = K_S_RIGHT; break;
389 case K_C_LEFT: c = K_C_RIGHT; break;
390 }
391 }
392#endif
393 }
394
395 /*
396 * Ignore got_int when CTRL-C was typed here.
397 * Don't ignore it in :global, we really need to break then, e.g., for
398 * ":g/pat/normal /pat" (without the <CR>).
399 * Don't ignore it for the input() function.
400 */
401 if ((c == Ctrl_C
402#ifdef UNIX
403 || c == intr_char
404#endif
405 )
406#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
407 && firstc != '@'
408#endif
409#ifdef FEAT_EVAL
410 && !break_ctrl_c
411#endif
412 && !global_busy)
413 got_int = FALSE;
414
415#ifdef FEAT_CMDHIST
416 /* free old command line when finished moving around in the history
417 * list */
418 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000419 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000420 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 && c != K_PAGEDOWN && c != K_PAGEUP
422 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000423 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
425 {
426 vim_free(lookfor);
427 lookfor = NULL;
428 }
429#endif
430
431 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000432 * When there are matching completions to select <S-Tab> works like
433 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000435 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 c = Ctrl_P;
437
438#ifdef FEAT_WILDMENU
439 /* Special translations for 'wildmenu' */
440 if (did_wild_list && p_wmnu)
441 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000442 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000444 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445 c = Ctrl_N;
446 }
447 /* Hitting CR after "emenu Name.": complete submenu */
448 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
449 && ccline.cmdpos > 1
450 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
451 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
452 && (c == '\n' || c == '\r' || c == K_KENTER))
453 c = K_DOWN;
454#endif
455
456 /* free expanded names when finished walking through matches */
457 if (xpc.xp_numfiles != -1
458 && !(c == p_wc && KeyTyped) && c != p_wcm
459 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
460 && c != Ctrl_L)
461 {
462 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
463 did_wild_list = FALSE;
464#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000465 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467 xpc.xp_context = EXPAND_NOTHING;
468 wim_index = 0;
469#ifdef FEAT_WILDMENU
470 if (p_wmnu && wild_menu_showing != 0)
471 {
472 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000473 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000474
475 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000476 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
478 if (wild_menu_showing == WM_SCROLLED)
479 {
480 /* Entered command line, move it up */
481 cmdline_row--;
482 redrawcmd();
483 }
484 else if (save_p_ls != -1)
485 {
486 /* restore 'laststatus' and 'winminheight' */
487 p_ls = save_p_ls;
488 p_wmh = save_p_wmh;
489 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000490 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000492 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000493 redrawcmd();
494 save_p_ls = -1;
495 }
496 else
497 {
498# ifdef FEAT_VERTSPLIT
499 win_redraw_last_status(topframe);
500# else
501 lastwin->w_redr_status = TRUE;
502# endif
503 redraw_statuslines();
504 }
505 KeyTyped = skt;
506 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000507 if (ccline.input_fn)
508 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 }
510#endif
511 }
512
513#ifdef FEAT_WILDMENU
514 /* Special translations for 'wildmenu' */
515 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
516 {
517 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000518 if (c == K_DOWN && ccline.cmdpos > 0
519 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000521 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 {
523 /* Hitting <Up>: Remove one submenu name in front of the
524 * cursor */
525 int found = FALSE;
526
527 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
528 i = 0;
529 while (--j > 0)
530 {
531 /* check for start of menu name */
532 if (ccline.cmdbuff[j] == ' '
533 && ccline.cmdbuff[j - 1] != '\\')
534 {
535 i = j + 1;
536 break;
537 }
538 /* check for start of submenu name */
539 if (ccline.cmdbuff[j] == '.'
540 && ccline.cmdbuff[j - 1] != '\\')
541 {
542 if (found)
543 {
544 i = j + 1;
545 break;
546 }
547 else
548 found = TRUE;
549 }
550 }
551 if (i > 0)
552 cmdline_del(i);
553 c = p_wc;
554 xpc.xp_context = EXPAND_NOTHING;
555 }
556 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000557 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000558 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000559 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 {
561 char_u upseg[5];
562
563 upseg[0] = PATHSEP;
564 upseg[1] = '.';
565 upseg[2] = '.';
566 upseg[3] = PATHSEP;
567 upseg[4] = NUL;
568
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000569 if (c == K_DOWN
570 && ccline.cmdpos > 0
571 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
572 && (ccline.cmdpos < 3
573 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
575 {
576 /* go down a directory */
577 c = p_wc;
578 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000579 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 {
581 /* If in a direct ancestor, strip off one ../ to go down */
582 int found = FALSE;
583
584 j = ccline.cmdpos;
585 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
586 while (--j > i)
587 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000588#ifdef FEAT_MBYTE
589 if (has_mbyte)
590 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (vim_ispathsep(ccline.cmdbuff[j]))
593 {
594 found = TRUE;
595 break;
596 }
597 }
598 if (found
599 && ccline.cmdbuff[j - 1] == '.'
600 && ccline.cmdbuff[j - 2] == '.'
601 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
602 {
603 cmdline_del(j - 2);
604 c = p_wc;
605 }
606 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000607 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 {
609 /* go up a directory */
610 int found = FALSE;
611
612 j = ccline.cmdpos - 1;
613 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
614 while (--j > i)
615 {
616#ifdef FEAT_MBYTE
617 if (has_mbyte)
618 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
619#endif
620 if (vim_ispathsep(ccline.cmdbuff[j])
621#ifdef BACKSLASH_IN_FILENAME
622 && vim_strchr(" *?[{`$%#", ccline.cmdbuff[j + 1])
623 == NULL
624#endif
625 )
626 {
627 if (found)
628 {
629 i = j + 1;
630 break;
631 }
632 else
633 found = TRUE;
634 }
635 }
636
637 if (!found)
638 j = i;
639 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
640 j += 4;
641 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
642 && j == i)
643 j += 3;
644 else
645 j = 0;
646 if (j > 0)
647 {
648 /* TODO this is only for DOS/UNIX systems - need to put in
649 * machine-specific stuff here and in upseg init */
650 cmdline_del(j);
651 put_on_cmdline(upseg + 1, 3, FALSE);
652 }
653 else if (ccline.cmdpos > i)
654 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100655
656 /* Now complete in the new directory. Set KeyTyped in case the
657 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100659 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 }
661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662
663#endif /* FEAT_WILDMENU */
664
665 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
666 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
667 if (c == Ctrl_BSL)
668 {
669 ++no_mapping;
670 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000671 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 --no_mapping;
673 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200674 /* CTRL-\ e doesn't work when obtaining an expression, unless it
675 * is in a mapping. */
676 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
677 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 vungetc(c);
680 c = Ctrl_BSL;
681 }
682#ifdef FEAT_EVAL
683 else if (c == 'e')
684 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200685 char_u *p = NULL;
686 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687
688 /*
689 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000690 * Need to save and restore the current command line, to be
691 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692 */
693 if (ccline.cmdpos == ccline.cmdlen)
694 new_cmdpos = 99999; /* keep it at the end */
695 else
696 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000697
698 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000700 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 if (c == '=')
702 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000703 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000704 * to avoid nasty things like going to another buffer when
705 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000706 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000707 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000709 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000710 restore_cmdline(&save_ccline);
711
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200712 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200714 len = (int)STRLEN(p);
715 if (realloc_cmdbuff(len + 1) == OK)
716 {
717 ccline.cmdlen = len;
718 STRCPY(ccline.cmdbuff, p);
719 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200721 /* Restore the cursor or use the position set with
722 * set_cmdline_pos(). */
723 if (new_cmdpos > ccline.cmdlen)
724 ccline.cmdpos = ccline.cmdlen;
725 else
726 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200728 KeyTyped = FALSE; /* Don't do p_wc completion. */
729 redrawcmd();
730 goto cmdline_changed;
731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 }
734 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100735 got_int = FALSE; /* don't abandon the command line */
736 did_emsg = FALSE;
737 emsg_on_display = FALSE;
738 redrawcmd();
739 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 }
741#endif
742 else
743 {
744 if (c == Ctrl_G && p_im && restart_edit == 0)
745 restart_edit = 'a';
746 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
747 in history */
748 goto returncmd; /* back to Normal mode */
749 }
750 }
751
752#ifdef FEAT_CMDWIN
753 if (c == cedit_key || c == K_CMDWIN)
754 {
755 /*
756 * Open a window to edit the command line (and history).
757 */
758 c = ex_window();
759 some_key_typed = TRUE;
760 }
761# ifdef FEAT_DIGRAPHS
762 else
763# endif
764#endif
765#ifdef FEAT_DIGRAPHS
766 c = do_digraph(c);
767#endif
768
769 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
770 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
771 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000772 /* In Ex mode a backslash escapes a newline. */
773 if (exmode_active
774 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000775 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000776 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000777 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000779 if (c == K_KENTER)
780 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000782 else
783 {
784 gotesc = FALSE; /* Might have typed ESC previously, don't
785 truncate the cmdline now. */
786 if (ccheck_abbr(c + ABBR_OFF))
787 goto cmdline_changed;
788 if (!cmd_silent)
789 {
790 windgoto(msg_row, 0);
791 out_flush();
792 }
793 break;
794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 }
796
797 /*
798 * Completion for 'wildchar' or 'wildcharm' key.
799 * - hitting <ESC> twice means: abandon command line.
800 * - wildcard expansion is only done when the 'wildchar' key is really
801 * typed, not when it comes from a macro
802 */
803 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
804 {
805 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
806 {
807 /* if 'wildmode' contains "list" may still need to list */
808 if (xpc.xp_numfiles > 1
809 && !did_wild_list
810 && (wim_flags[wim_index] & WIM_LIST))
811 {
812 (void)showmatches(&xpc, FALSE);
813 redrawcmd();
814 did_wild_list = TRUE;
815 }
816 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100817 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
818 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100820 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
821 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 else
823 res = OK; /* don't insert 'wildchar' now */
824 }
825 else /* typed p_wc first time */
826 {
827 wim_index = 0;
828 j = ccline.cmdpos;
829 /* if 'wildmode' first contains "longest", get longest
830 * common part */
831 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100832 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
833 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100835 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
836 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837
838 /* if interrupted while completing, behave like it failed */
839 if (got_int)
840 {
841 (void)vpeekc(); /* remove <C-C> from input stream */
842 got_int = FALSE; /* don't abandon the command line */
843 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
844#ifdef FEAT_WILDMENU
845 xpc.xp_context = EXPAND_NOTHING;
846#endif
847 goto cmdline_changed;
848 }
849
850 /* when more than one match, and 'wildmode' first contains
851 * "list", or no change and 'wildmode' contains "longest,list",
852 * list all matches */
853 if (res == OK && xpc.xp_numfiles > 1)
854 {
855 /* a "longest" that didn't do anything is skipped (but not
856 * "list:longest") */
857 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
858 wim_index = 1;
859 if ((wim_flags[wim_index] & WIM_LIST)
860#ifdef FEAT_WILDMENU
861 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
862#endif
863 )
864 {
865 if (!(wim_flags[0] & WIM_LONGEST))
866 {
867#ifdef FEAT_WILDMENU
868 int p_wmnu_save = p_wmnu;
869 p_wmnu = 0;
870#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100871 /* remove match */
872 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873#ifdef FEAT_WILDMENU
874 p_wmnu = p_wmnu_save;
875#endif
876 }
877#ifdef FEAT_WILDMENU
878 (void)showmatches(&xpc, p_wmnu
879 && ((wim_flags[wim_index] & WIM_LIST) == 0));
880#else
881 (void)showmatches(&xpc, FALSE);
882#endif
883 redrawcmd();
884 did_wild_list = TRUE;
885 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100886 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
887 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100889 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
890 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 }
892 else
893 vim_beep();
894 }
895#ifdef FEAT_WILDMENU
896 else if (xpc.xp_numfiles == -1)
897 xpc.xp_context = EXPAND_NOTHING;
898#endif
899 }
900 if (wim_index < 3)
901 ++wim_index;
902 if (c == ESC)
903 gotesc = TRUE;
904 if (res == OK)
905 goto cmdline_changed;
906 }
907
908 gotesc = FALSE;
909
910 /* <S-Tab> goes to last match, in a clumsy way */
911 if (c == K_S_TAB && KeyTyped)
912 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100913 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
914 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
915 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 goto cmdline_changed;
917 }
918
919 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
920 c = NL;
921
922 do_abbr = TRUE; /* default: check for abbreviation */
923
924 /*
925 * Big switch for a typed command line character.
926 */
927 switch (c)
928 {
929 case K_BS:
930 case Ctrl_H:
931 case K_DEL:
932 case K_KDEL:
933 case Ctrl_W:
934#ifdef FEAT_FKMAP
935 if (cmd_fkmap && c == K_BS)
936 c = K_DEL;
937#endif
938 if (c == K_KDEL)
939 c = K_DEL;
940
941 /*
942 * delete current character is the same as backspace on next
943 * character, except at end of line
944 */
945 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
946 ++ccline.cmdpos;
947#ifdef FEAT_MBYTE
948 if (has_mbyte && c == K_DEL)
949 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
950 ccline.cmdbuff + ccline.cmdpos);
951#endif
952 if (ccline.cmdpos > 0)
953 {
954 char_u *p;
955
956 j = ccline.cmdpos;
957 p = ccline.cmdbuff + j;
958#ifdef FEAT_MBYTE
959 if (has_mbyte)
960 {
961 p = mb_prevptr(ccline.cmdbuff, p);
962 if (c == Ctrl_W)
963 {
964 while (p > ccline.cmdbuff && vim_isspace(*p))
965 p = mb_prevptr(ccline.cmdbuff, p);
966 i = mb_get_class(p);
967 while (p > ccline.cmdbuff && mb_get_class(p) == i)
968 p = mb_prevptr(ccline.cmdbuff, p);
969 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000970 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 }
973 else
974#endif
975 if (c == Ctrl_W)
976 {
977 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
978 --p;
979 i = vim_iswordc(p[-1]);
980 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
981 && vim_iswordc(p[-1]) == i)
982 --p;
983 }
984 else
985 --p;
986 ccline.cmdpos = (int)(p - ccline.cmdbuff);
987 ccline.cmdlen -= j - ccline.cmdpos;
988 i = ccline.cmdpos;
989 while (i < ccline.cmdlen)
990 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
991
992 /* Truncate at the end, required for multi-byte chars. */
993 ccline.cmdbuff[ccline.cmdlen] = NUL;
994 redrawcmd();
995 }
996 else if (ccline.cmdlen == 0 && c != Ctrl_W
997 && ccline.cmdprompt == NULL && indent == 0)
998 {
999 /* In ex and debug mode it doesn't make sense to return. */
1000 if (exmode_active
1001#ifdef FEAT_EVAL
1002 || ccline.cmdfirstc == '>'
1003#endif
1004 )
1005 goto cmdline_not_changed;
1006
1007 vim_free(ccline.cmdbuff); /* no commandline to return */
1008 ccline.cmdbuff = NULL;
1009 if (!cmd_silent)
1010 {
1011#ifdef FEAT_RIGHTLEFT
1012 if (cmdmsg_rl)
1013 msg_col = Columns;
1014 else
1015#endif
1016 msg_col = 0;
1017 msg_putchar(' '); /* delete ':' */
1018 }
1019 redraw_cmdline = TRUE;
1020 goto returncmd; /* back to cmd mode */
1021 }
1022 goto cmdline_changed;
1023
1024 case K_INS:
1025 case K_KINS:
1026#ifdef FEAT_FKMAP
1027 /* if Farsi mode set, we are in reverse insert mode -
1028 Do not change the mode */
1029 if (cmd_fkmap)
1030 beep_flush();
1031 else
1032#endif
1033 ccline.overstrike = !ccline.overstrike;
1034#ifdef CURSOR_SHAPE
1035 ui_cursor_shape(); /* may show different cursor shape */
1036#endif
1037 goto cmdline_not_changed;
1038
1039 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001040 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 {
1042 /* ":lmap" mappings exists, toggle use of mappings. */
1043 State ^= LANGMAP;
1044#ifdef USE_IM_CONTROL
1045 im_set_active(FALSE); /* Disable input method */
1046#endif
1047 if (b_im_ptr != NULL)
1048 {
1049 if (State & LANGMAP)
1050 *b_im_ptr = B_IMODE_LMAP;
1051 else
1052 *b_im_ptr = B_IMODE_NONE;
1053 }
1054 }
1055#ifdef USE_IM_CONTROL
1056 else
1057 {
1058 /* There are no ":lmap" mappings, toggle IM. When
1059 * 'imdisable' is set don't try getting the status, it's
1060 * always off. */
1061 if ((p_imdisable && b_im_ptr != NULL)
1062 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1063 {
1064 im_set_active(FALSE); /* Disable input method */
1065 if (b_im_ptr != NULL)
1066 *b_im_ptr = B_IMODE_NONE;
1067 }
1068 else
1069 {
1070 im_set_active(TRUE); /* Enable input method */
1071 if (b_im_ptr != NULL)
1072 *b_im_ptr = B_IMODE_IM;
1073 }
1074 }
1075#endif
1076 if (b_im_ptr != NULL)
1077 {
1078 if (b_im_ptr == &curbuf->b_p_iminsert)
1079 set_iminsert_global();
1080 else
1081 set_imsearch_global();
1082 }
1083#ifdef CURSOR_SHAPE
1084 ui_cursor_shape(); /* may show different cursor shape */
1085#endif
1086#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1087 /* Show/unshow value of 'keymap' in status lines later. */
1088 status_redraw_curbuf();
1089#endif
1090 goto cmdline_not_changed;
1091
1092/* case '@': only in very old vi */
1093 case Ctrl_U:
1094 /* delete all characters left of the cursor */
1095 j = ccline.cmdpos;
1096 ccline.cmdlen -= j;
1097 i = ccline.cmdpos = 0;
1098 while (i < ccline.cmdlen)
1099 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1100 /* Truncate at the end, required for multi-byte chars. */
1101 ccline.cmdbuff[ccline.cmdlen] = NUL;
1102 redrawcmd();
1103 goto cmdline_changed;
1104
1105#ifdef FEAT_CLIPBOARD
1106 case Ctrl_Y:
1107 /* Copy the modeless selection, if there is one. */
1108 if (clip_star.state != SELECT_CLEARED)
1109 {
1110 if (clip_star.state == SELECT_DONE)
1111 clip_copy_modeless_selection(TRUE);
1112 goto cmdline_not_changed;
1113 }
1114 break;
1115#endif
1116
1117 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1118 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001119 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001120 * ":normal" runs out of characters. */
1121 if (exmode_active
1122#ifdef FEAT_EX_EXTRA
1123 && (ex_normal_busy == 0 || typebuf.tb_len > 0)
1124#endif
1125 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 goto cmdline_not_changed;
1127
1128 gotesc = TRUE; /* will free ccline.cmdbuff after
1129 putting it in history */
1130 goto returncmd; /* back to cmd mode */
1131
1132 case Ctrl_R: /* insert register */
1133#ifdef USE_ON_FLY_SCROLL
1134 dont_scroll = TRUE; /* disallow scrolling here */
1135#endif
1136 putcmdline('"', TRUE);
1137 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001138 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 if (i == Ctrl_O)
1140 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1141 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001142 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 --no_mapping;
1144#ifdef FEAT_EVAL
1145 /*
1146 * Insert the result of an expression.
1147 * Need to save the current command line, to be able to enter
1148 * a new one...
1149 */
1150 new_cmdpos = -1;
1151 if (c == '=')
1152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1154 {
1155 beep_flush();
1156 c = ESC;
1157 }
1158 else
1159 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001160 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001162 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 }
1164 }
1165#endif
1166 if (c != ESC) /* use ESC to cancel inserting register */
1167 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001168 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001169
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001170#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001171 /* When there was a serious error abort getting the
1172 * command line. */
1173 if (aborting())
1174 {
1175 gotesc = TRUE; /* will free ccline.cmdbuff after
1176 putting it in history */
1177 goto returncmd; /* back to cmd mode */
1178 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 KeyTyped = FALSE; /* Don't do p_wc completion. */
1181#ifdef FEAT_EVAL
1182 if (new_cmdpos >= 0)
1183 {
1184 /* set_cmdline_pos() was used */
1185 if (new_cmdpos > ccline.cmdlen)
1186 ccline.cmdpos = ccline.cmdlen;
1187 else
1188 ccline.cmdpos = new_cmdpos;
1189 }
1190#endif
1191 }
1192 redrawcmd();
1193 goto cmdline_changed;
1194
1195 case Ctrl_D:
1196 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1197 break; /* Use ^D as normal char instead */
1198
1199 redrawcmd();
1200 continue; /* don't do incremental search now */
1201
1202 case K_RIGHT:
1203 case K_S_RIGHT:
1204 case K_C_RIGHT:
1205 do
1206 {
1207 if (ccline.cmdpos >= ccline.cmdlen)
1208 break;
1209 i = cmdline_charsize(ccline.cmdpos);
1210 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1211 break;
1212 ccline.cmdspos += i;
1213#ifdef FEAT_MBYTE
1214 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001215 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 + ccline.cmdpos);
1217 else
1218#endif
1219 ++ccline.cmdpos;
1220 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001221 while ((c == K_S_RIGHT || c == K_C_RIGHT
1222 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1224#ifdef FEAT_MBYTE
1225 if (has_mbyte)
1226 set_cmdspos_cursor();
1227#endif
1228 goto cmdline_not_changed;
1229
1230 case K_LEFT:
1231 case K_S_LEFT:
1232 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001233 if (ccline.cmdpos == 0)
1234 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 do
1236 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 --ccline.cmdpos;
1238#ifdef FEAT_MBYTE
1239 if (has_mbyte) /* move to first byte of char */
1240 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1241 ccline.cmdbuff + ccline.cmdpos);
1242#endif
1243 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1244 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001245 while (ccline.cmdpos > 0
1246 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001247 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1249#ifdef FEAT_MBYTE
1250 if (has_mbyte)
1251 set_cmdspos_cursor();
1252#endif
1253 goto cmdline_not_changed;
1254
1255 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001256 /* Ignore mouse event or ex_window() result. */
1257 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaar4770d092006-01-12 23:22:24 +00001259#ifdef FEAT_GUI_W32
1260 /* On Win32 ignore <M-F4>, we get it when closing the window was
1261 * cancelled. */
1262 case K_F4:
1263 if (mod_mask == MOD_MASK_ALT)
1264 {
1265 redrawcmd(); /* somehow the cmdline is cleared */
1266 goto cmdline_not_changed;
1267 }
1268 break;
1269#endif
1270
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271#ifdef FEAT_MOUSE
1272 case K_MIDDLEDRAG:
1273 case K_MIDDLERELEASE:
1274 goto cmdline_not_changed; /* Ignore mouse */
1275
1276 case K_MIDDLEMOUSE:
1277# ifdef FEAT_GUI
1278 /* When GUI is active, also paste when 'mouse' is empty */
1279 if (!gui.in_use)
1280# endif
1281 if (!mouse_has(MOUSE_COMMAND))
1282 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001283# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001285 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001287# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001288 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 redrawcmd();
1290 goto cmdline_changed;
1291
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001292# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001294 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 redrawcmd();
1296 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001297# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299 case K_LEFTDRAG:
1300 case K_LEFTRELEASE:
1301 case K_RIGHTDRAG:
1302 case K_RIGHTRELEASE:
1303 /* Ignore drag and release events when the button-down wasn't
1304 * seen before. */
1305 if (ignore_drag_release)
1306 goto cmdline_not_changed;
1307 /* FALLTHROUGH */
1308 case K_LEFTMOUSE:
1309 case K_RIGHTMOUSE:
1310 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1311 ignore_drag_release = TRUE;
1312 else
1313 ignore_drag_release = FALSE;
1314# ifdef FEAT_GUI
1315 /* When GUI is active, also move when 'mouse' is empty */
1316 if (!gui.in_use)
1317# endif
1318 if (!mouse_has(MOUSE_COMMAND))
1319 goto cmdline_not_changed; /* Ignore mouse */
1320# ifdef FEAT_CLIPBOARD
1321 if (mouse_row < cmdline_row && clip_star.available)
1322 {
1323 int button, is_click, is_drag;
1324
1325 /*
1326 * Handle modeless selection.
1327 */
1328 button = get_mouse_button(KEY2TERMCAP1(c),
1329 &is_click, &is_drag);
1330 if (mouse_model_popup() && button == MOUSE_LEFT
1331 && (mod_mask & MOD_MASK_SHIFT))
1332 {
1333 /* Translate shift-left to right button. */
1334 button = MOUSE_RIGHT;
1335 mod_mask &= ~MOD_MASK_SHIFT;
1336 }
1337 clip_modeless(button, is_click, is_drag);
1338 goto cmdline_not_changed;
1339 }
1340# endif
1341
1342 set_cmdspos();
1343 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1344 ++ccline.cmdpos)
1345 {
1346 i = cmdline_charsize(ccline.cmdpos);
1347 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1348 && mouse_col < ccline.cmdspos % Columns + i)
1349 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001350# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 if (has_mbyte)
1352 {
1353 /* Count ">" for double-wide char that doesn't fit. */
1354 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001355 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 + ccline.cmdpos) - 1;
1357 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 ccline.cmdspos += i;
1360 }
1361 goto cmdline_not_changed;
1362
1363 /* Mouse scroll wheel: ignored here */
1364 case K_MOUSEDOWN:
1365 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001366 case K_MOUSELEFT:
1367 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 /* Alternate buttons ignored here */
1369 case K_X1MOUSE:
1370 case K_X1DRAG:
1371 case K_X1RELEASE:
1372 case K_X2MOUSE:
1373 case K_X2DRAG:
1374 case K_X2RELEASE:
1375 goto cmdline_not_changed;
1376
1377#endif /* FEAT_MOUSE */
1378
1379#ifdef FEAT_GUI
1380 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1381 case K_LEFTRELEASE_NM:
1382 goto cmdline_not_changed;
1383
1384 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001385 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 {
1387 gui_do_scroll();
1388 redrawcmd();
1389 }
1390 goto cmdline_not_changed;
1391
1392 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001393 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001395 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 redrawcmd();
1397 }
1398 goto cmdline_not_changed;
1399#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001400#ifdef FEAT_GUI_TABLINE
1401 case K_TABLINE:
1402 case K_TABMENU:
1403 /* Don't want to change any tabs here. Make sure the same tab
1404 * is still selected. */
1405 if (gui_use_tabline())
1406 gui_mch_set_curtab(tabpage_index(curtab));
1407 goto cmdline_not_changed;
1408#endif
1409
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 case K_SELECT: /* end of Select mode mapping - ignore */
1411 goto cmdline_not_changed;
1412
1413 case Ctrl_B: /* begin of command line */
1414 case K_HOME:
1415 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 case K_S_HOME:
1417 case K_C_HOME:
1418 ccline.cmdpos = 0;
1419 set_cmdspos();
1420 goto cmdline_not_changed;
1421
1422 case Ctrl_E: /* end of command line */
1423 case K_END:
1424 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 case K_S_END:
1426 case K_C_END:
1427 ccline.cmdpos = ccline.cmdlen;
1428 set_cmdspos_cursor();
1429 goto cmdline_not_changed;
1430
1431 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001432 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 break;
1434 goto cmdline_changed;
1435
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001436 case Ctrl_L:
1437#ifdef FEAT_SEARCH_EXTRA
1438 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1439 {
1440 /* Add a character from under the cursor for 'incsearch' */
1441 if (did_incsearch
1442 && !equalpos(curwin->w_cursor, old_cursor))
1443 {
1444 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001445 /* If 'ignorecase' and 'smartcase' are set and the
1446 * command line has no uppercase characters, convert
1447 * the character to lowercase */
1448 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1449 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001450 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001451 {
1452 if (c == firstc || vim_strchr((char_u *)(
1453 p_magic ? "\\^$.*[" : "\\^$"), c)
1454 != NULL)
1455 {
1456 /* put a backslash before special characters */
1457 stuffcharReadbuff(c);
1458 c = '\\';
1459 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001460 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001461 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001462 }
1463 goto cmdline_not_changed;
1464 }
1465#endif
1466
1467 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001468 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 break;
1470 goto cmdline_changed;
1471
1472 case Ctrl_N: /* next match */
1473 case Ctrl_P: /* previous match */
1474 if (xpc.xp_numfiles > 0)
1475 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001476 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1477 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 break;
1479 goto cmdline_changed;
1480 }
1481
1482#ifdef FEAT_CMDHIST
1483 case K_UP:
1484 case K_DOWN:
1485 case K_S_UP:
1486 case K_S_DOWN:
1487 case K_PAGEUP:
1488 case K_KPAGEUP:
1489 case K_PAGEDOWN:
1490 case K_KPAGEDOWN:
1491 if (hislen == 0 || firstc == NUL) /* no history */
1492 goto cmdline_not_changed;
1493
1494 i = hiscnt;
1495
1496 /* save current command string so it can be restored later */
1497 if (lookfor == NULL)
1498 {
1499 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1500 goto cmdline_not_changed;
1501 lookfor[ccline.cmdpos] = NUL;
1502 }
1503
1504 j = (int)STRLEN(lookfor);
1505 for (;;)
1506 {
1507 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001508 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001509 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 {
1511 if (hiscnt == hislen) /* first time */
1512 hiscnt = hisidx[histype];
1513 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1514 hiscnt = hislen - 1;
1515 else if (hiscnt != hisidx[histype] + 1)
1516 --hiscnt;
1517 else /* at top of list */
1518 {
1519 hiscnt = i;
1520 break;
1521 }
1522 }
1523 else /* one step forwards */
1524 {
1525 /* on last entry, clear the line */
1526 if (hiscnt == hisidx[histype])
1527 {
1528 hiscnt = hislen;
1529 break;
1530 }
1531
1532 /* not on a history line, nothing to do */
1533 if (hiscnt == hislen)
1534 break;
1535 if (hiscnt == hislen - 1) /* wrap around */
1536 hiscnt = 0;
1537 else
1538 ++hiscnt;
1539 }
1540 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1541 {
1542 hiscnt = i;
1543 break;
1544 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001545 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001546 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 || STRNCMP(history[histype][hiscnt].hisstr,
1548 lookfor, (size_t)j) == 0)
1549 break;
1550 }
1551
1552 if (hiscnt != i) /* jumped to other entry */
1553 {
1554 char_u *p;
1555 int len;
1556 int old_firstc;
1557
1558 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001559 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 if (hiscnt == hislen)
1561 p = lookfor; /* back to the old one */
1562 else
1563 p = history[histype][hiscnt].hisstr;
1564
1565 if (histype == HIST_SEARCH
1566 && p != lookfor
1567 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1568 {
1569 /* Correct for the separator character used when
1570 * adding the history entry vs the one used now.
1571 * First loop: count length.
1572 * Second loop: copy the characters. */
1573 for (i = 0; i <= 1; ++i)
1574 {
1575 len = 0;
1576 for (j = 0; p[j] != NUL; ++j)
1577 {
1578 /* Replace old sep with new sep, unless it is
1579 * escaped. */
1580 if (p[j] == old_firstc
1581 && (j == 0 || p[j - 1] != '\\'))
1582 {
1583 if (i > 0)
1584 ccline.cmdbuff[len] = firstc;
1585 }
1586 else
1587 {
1588 /* Escape new sep, unless it is already
1589 * escaped. */
1590 if (p[j] == firstc
1591 && (j == 0 || p[j - 1] != '\\'))
1592 {
1593 if (i > 0)
1594 ccline.cmdbuff[len] = '\\';
1595 ++len;
1596 }
1597 if (i > 0)
1598 ccline.cmdbuff[len] = p[j];
1599 }
1600 ++len;
1601 }
1602 if (i == 0)
1603 {
1604 alloc_cmdbuff(len);
1605 if (ccline.cmdbuff == NULL)
1606 goto returncmd;
1607 }
1608 }
1609 ccline.cmdbuff[len] = NUL;
1610 }
1611 else
1612 {
1613 alloc_cmdbuff((int)STRLEN(p));
1614 if (ccline.cmdbuff == NULL)
1615 goto returncmd;
1616 STRCPY(ccline.cmdbuff, p);
1617 }
1618
1619 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1620 redrawcmd();
1621 goto cmdline_changed;
1622 }
1623 beep_flush();
1624 goto cmdline_not_changed;
1625#endif
1626
1627 case Ctrl_V:
1628 case Ctrl_Q:
1629#ifdef FEAT_MOUSE
1630 ignore_drag_release = TRUE;
1631#endif
1632 putcmdline('^', TRUE);
1633 c = get_literal(); /* get next (two) character(s) */
1634 do_abbr = FALSE; /* don't do abbreviation now */
1635#ifdef FEAT_MBYTE
1636 /* may need to remove ^ when composing char was typed */
1637 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1638 {
1639 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1640 msg_putchar(' ');
1641 cursorcmd();
1642 }
1643#endif
1644 break;
1645
1646#ifdef FEAT_DIGRAPHS
1647 case Ctrl_K:
1648#ifdef FEAT_MOUSE
1649 ignore_drag_release = TRUE;
1650#endif
1651 putcmdline('?', TRUE);
1652#ifdef USE_ON_FLY_SCROLL
1653 dont_scroll = TRUE; /* disallow scrolling here */
1654#endif
1655 c = get_digraph(TRUE);
1656 if (c != NUL)
1657 break;
1658
1659 redrawcmd();
1660 goto cmdline_not_changed;
1661#endif /* FEAT_DIGRAPHS */
1662
1663#ifdef FEAT_RIGHTLEFT
1664 case Ctrl__: /* CTRL-_: switch language mode */
1665 if (!p_ari)
1666 break;
1667#ifdef FEAT_FKMAP
1668 if (p_altkeymap)
1669 {
1670 cmd_fkmap = !cmd_fkmap;
1671 if (cmd_fkmap) /* in Farsi always in Insert mode */
1672 ccline.overstrike = FALSE;
1673 }
1674 else /* Hebrew is default */
1675#endif
1676 cmd_hkmap = !cmd_hkmap;
1677 goto cmdline_not_changed;
1678#endif
1679
1680 default:
1681#ifdef UNIX
1682 if (c == intr_char)
1683 {
1684 gotesc = TRUE; /* will free ccline.cmdbuff after
1685 putting it in history */
1686 goto returncmd; /* back to Normal mode */
1687 }
1688#endif
1689 /*
1690 * Normal character with no special meaning. Just set mod_mask
1691 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1692 * the string <S-Space>. This should only happen after ^V.
1693 */
1694 if (!IS_SPECIAL(c))
1695 mod_mask = 0x0;
1696 break;
1697 }
1698 /*
1699 * End of switch on command line character.
1700 * We come here if we have a normal character.
1701 */
1702
1703 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && ccheck_abbr(
1704#ifdef FEAT_MBYTE
1705 /* Add ABBR_OFF for characters above 0x100, this is
1706 * what check_abbr() expects. */
1707 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1708#endif
1709 c))
1710 goto cmdline_changed;
1711
1712 /*
1713 * put the character in the command line
1714 */
1715 if (IS_SPECIAL(c) || mod_mask != 0)
1716 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1717 else
1718 {
1719#ifdef FEAT_MBYTE
1720 if (has_mbyte)
1721 {
1722 j = (*mb_char2bytes)(c, IObuff);
1723 IObuff[j] = NUL; /* exclude composing chars */
1724 put_on_cmdline(IObuff, j, TRUE);
1725 }
1726 else
1727#endif
1728 {
1729 IObuff[0] = c;
1730 put_on_cmdline(IObuff, 1, TRUE);
1731 }
1732 }
1733 goto cmdline_changed;
1734
1735/*
1736 * This part implements incremental searches for "/" and "?"
1737 * Jump to cmdline_not_changed when a character has been read but the command
1738 * line did not change. Then we only search and redraw if something changed in
1739 * the past.
1740 * Jump to cmdline_changed when the command line did change.
1741 * (Sorry for the goto's, I know it is ugly).
1742 */
1743cmdline_not_changed:
1744#ifdef FEAT_SEARCH_EXTRA
1745 if (!incsearch_postponed)
1746 continue;
1747#endif
1748
1749cmdline_changed:
1750#ifdef FEAT_SEARCH_EXTRA
1751 /*
1752 * 'incsearch' highlighting.
1753 */
1754 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1755 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001756 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001757#ifdef FEAT_RELTIME
1758 proftime_T tm;
1759#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001760
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 /* if there is a character waiting, search and redraw later */
1762 if (char_avail())
1763 {
1764 incsearch_postponed = TRUE;
1765 continue;
1766 }
1767 incsearch_postponed = FALSE;
1768 curwin->w_cursor = old_cursor; /* start at old position */
1769
1770 /* If there is no command line, don't do anything */
1771 if (ccline.cmdlen == 0)
1772 i = 0;
1773 else
1774 {
1775 cursor_off(); /* so the user knows we're busy */
1776 out_flush();
1777 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001778#ifdef FEAT_RELTIME
1779 /* Set the time limit to half a second. */
1780 profile_setlimit(500L, &tm);
1781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001783 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1784#ifdef FEAT_RELTIME
1785 &tm
1786#else
1787 NULL
1788#endif
1789 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 --emsg_off;
1791 /* if interrupted while searching, behave like it failed */
1792 if (got_int)
1793 {
1794 (void)vpeekc(); /* remove <C-C> from input stream */
1795 got_int = FALSE; /* don't abandon the command line */
1796 i = 0;
1797 }
1798 else if (char_avail())
1799 /* cancelled searching because a char was typed */
1800 incsearch_postponed = TRUE;
1801 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001802 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 highlight_match = TRUE; /* highlight position */
1804 else
1805 highlight_match = FALSE; /* remove highlight */
1806
1807 /* first restore the old curwin values, so the screen is
1808 * positioned in the same way as the actual search command */
1809 curwin->w_leftcol = old_leftcol;
1810 curwin->w_topline = old_topline;
1811# ifdef FEAT_DIFF
1812 curwin->w_topfill = old_topfill;
1813# endif
1814 curwin->w_botline = old_botline;
1815 changed_cline_bef_curs();
1816 update_topline();
1817
1818 if (i != 0)
1819 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001820 pos_T save_pos = curwin->w_cursor;
1821
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001823 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 * moves the whole match onto the screen when 'nowrap' is set.
1825 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 curwin->w_cursor.lnum += search_match_lines;
1827 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001828 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1829 {
1830 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1831 coladvance((colnr_T)MAXCOL);
1832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001834 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001835 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001837 else
1838 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001841# ifdef FEAT_WINDOWS
1842 /* May redraw the status line to show the cursor position. */
1843 if (p_ru && curwin->w_status_height > 0)
1844 curwin->w_redr_status = TRUE;
1845# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001847 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001848 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001849 restore_cmdline(&save_ccline);
1850
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001851 /* Leave it at the end to make CTRL-R CTRL-W work. */
1852 if (i != 0)
1853 curwin->w_cursor = end_pos;
1854
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 msg_starthere();
1856 redrawcmdline();
1857 did_incsearch = TRUE;
1858 }
1859#else /* FEAT_SEARCH_EXTRA */
1860 ;
1861#endif
1862
1863#ifdef FEAT_RIGHTLEFT
1864 if (cmdmsg_rl
1865# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001866 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867# endif
1868 )
1869 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001870 * right-left typing. Not efficient, but it works.
1871 * Do it only when there are no characters left to read
1872 * to avoid useless intermediate redraws. */
1873 if (vpeekc() == NUL)
1874 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875#endif
1876 }
1877
1878returncmd:
1879
1880#ifdef FEAT_RIGHTLEFT
1881 cmdmsg_rl = FALSE;
1882#endif
1883
1884#ifdef FEAT_FKMAP
1885 cmd_fkmap = 0;
1886#endif
1887
1888 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001889 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891#ifdef FEAT_SEARCH_EXTRA
1892 if (did_incsearch)
1893 {
1894 curwin->w_cursor = old_cursor;
1895 curwin->w_curswant = old_curswant;
1896 curwin->w_leftcol = old_leftcol;
1897 curwin->w_topline = old_topline;
1898# ifdef FEAT_DIFF
1899 curwin->w_topfill = old_topfill;
1900# endif
1901 curwin->w_botline = old_botline;
1902 highlight_match = FALSE;
1903 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001904 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906#endif
1907
1908 if (ccline.cmdbuff != NULL)
1909 {
1910 /*
1911 * Put line in history buffer (":" and "=" only when it was typed).
1912 */
1913#ifdef FEAT_CMDHIST
1914 if (ccline.cmdlen && firstc != NUL
1915 && (some_key_typed || histype == HIST_SEARCH))
1916 {
1917 add_to_history(histype, ccline.cmdbuff, TRUE,
1918 histype == HIST_SEARCH ? firstc : NUL);
1919 if (firstc == ':')
1920 {
1921 vim_free(new_last_cmdline);
1922 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1923 }
1924 }
1925#endif
1926
1927 if (gotesc) /* abandon command line */
1928 {
1929 vim_free(ccline.cmdbuff);
1930 ccline.cmdbuff = NULL;
1931 if (msg_scrolled == 0)
1932 compute_cmdrow();
1933 MSG("");
1934 redraw_cmdline = TRUE;
1935 }
1936 }
1937
1938 /*
1939 * If the screen was shifted up, redraw the whole screen (later).
1940 * If the line is too long, clear it, so ruler and shown command do
1941 * not get printed in the middle of it.
1942 */
1943 msg_check();
1944 msg_scroll = save_msg_scroll;
1945 redir_off = FALSE;
1946
1947 /* When the command line was typed, no need for a wait-return prompt. */
1948 if (some_key_typed)
1949 need_wait_return = FALSE;
1950
1951 State = save_State;
1952#ifdef USE_IM_CONTROL
1953 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1954 im_save_status(b_im_ptr);
1955 im_set_active(FALSE);
1956#endif
1957#ifdef FEAT_MOUSE
1958 setmouse();
1959#endif
1960#ifdef CURSOR_SHAPE
1961 ui_cursor_shape(); /* may show different cursor shape */
1962#endif
1963
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001964 {
1965 char_u *p = ccline.cmdbuff;
1966
1967 /* Make ccline empty, getcmdline() may try to use it. */
1968 ccline.cmdbuff = NULL;
1969 return p;
1970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971}
1972
1973#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1974/*
1975 * Get a command line with a prompt.
1976 * This is prepared to be called recursively from getcmdline() (e.g. by
1977 * f_input() when evaluating an expression from CTRL-R =).
1978 * Returns the command line in allocated memory, or NULL.
1979 */
1980 char_u *
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001981getcmdline_prompt(firstc, prompt, attr, xp_context, xp_arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 int firstc;
1983 char_u *prompt; /* command line prompt */
1984 int attr; /* attributes for prompt */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001985 int xp_context; /* type of expansion */
1986 char_u *xp_arg; /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987{
1988 char_u *s;
1989 struct cmdline_info save_ccline;
1990 int msg_col_save = msg_col;
1991
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001992 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 ccline.cmdprompt = prompt;
1994 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001995# ifdef FEAT_EVAL
1996 ccline.xp_context = xp_context;
1997 ccline.xp_arg = xp_arg;
1998 ccline.input_fn = (firstc == '@');
1999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002001 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002002 /* Restore msg_col, the prompt from input() may have changed it.
2003 * But only if called recursively and the commandline is therefore being
2004 * restored to an old one; if not, the input() prompt stays on the screen,
2005 * so we need its modified msg_col left intact. */
2006 if (ccline.cmdbuff != NULL)
2007 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008
2009 return s;
2010}
2011#endif
2012
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002013/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002014 * Return TRUE when the text must not be changed and we can't switch to
2015 * another window or buffer. Used when editing the command line, evaluating
2016 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002017 */
2018 int
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002019text_locked()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002020{
2021#ifdef FEAT_CMDWIN
2022 if (cmdwin_type != 0)
2023 return TRUE;
2024#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002025 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002026}
2027
2028/*
2029 * Give an error message for a command that isn't allowed while the cmdline
2030 * window is open or editing the cmdline in another way.
2031 */
2032 void
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002033text_locked_msg()
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002034{
2035#ifdef FEAT_CMDWIN
2036 if (cmdwin_type != 0)
2037 EMSG(_(e_cmdwin));
2038 else
2039#endif
2040 EMSG(_(e_secure));
2041}
2042
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002043#if defined(FEAT_AUTOCMD) || defined(PROTO)
2044/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002045 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2046 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002047 */
2048 int
2049curbuf_locked()
2050{
2051 if (curbuf_lock > 0)
2052 {
2053 EMSG(_("E788: Not allowed to edit another buffer now"));
2054 return TRUE;
2055 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002056 return allbuf_locked();
2057}
2058
2059/*
2060 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2061 * message.
2062 */
2063 int
2064allbuf_locked()
2065{
2066 if (allbuf_lock > 0)
2067 {
2068 EMSG(_("E811: Not allowed to change buffer information now"));
2069 return TRUE;
2070 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002071 return FALSE;
2072}
2073#endif
2074
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 static int
2076cmdline_charsize(idx)
2077 int idx;
2078{
2079#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2080 if (cmdline_star > 0) /* showing '*', always 1 position */
2081 return 1;
2082#endif
2083 return ptr2cells(ccline.cmdbuff + idx);
2084}
2085
2086/*
2087 * Compute the offset of the cursor on the command line for the prompt and
2088 * indent.
2089 */
2090 static void
2091set_cmdspos()
2092{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002093 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 ccline.cmdspos = 1 + ccline.cmdindent;
2095 else
2096 ccline.cmdspos = 0 + ccline.cmdindent;
2097}
2098
2099/*
2100 * Compute the screen position for the cursor on the command line.
2101 */
2102 static void
2103set_cmdspos_cursor()
2104{
2105 int i, m, c;
2106
2107 set_cmdspos();
2108 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002109 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002111 if (m < 0) /* overflow, Columns or Rows at weird value */
2112 m = MAXCOL;
2113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 else
2115 m = MAXCOL;
2116 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2117 {
2118 c = cmdline_charsize(i);
2119#ifdef FEAT_MBYTE
2120 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2121 if (has_mbyte)
2122 correct_cmdspos(i, c);
2123#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002124 /* If the cmdline doesn't fit, show cursor on last visible char.
2125 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if ((ccline.cmdspos += c) >= m)
2127 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 ccline.cmdspos -= c;
2129 break;
2130 }
2131#ifdef FEAT_MBYTE
2132 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002133 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134#endif
2135 }
2136}
2137
2138#ifdef FEAT_MBYTE
2139/*
2140 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2141 * character that doesn't fit, so that a ">" must be displayed.
2142 */
2143 static void
2144correct_cmdspos(idx, cells)
2145 int idx;
2146 int cells;
2147{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002148 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2150 && ccline.cmdspos % Columns + cells > Columns)
2151 ccline.cmdspos++;
2152}
2153#endif
2154
2155/*
2156 * Get an Ex command line for the ":" command.
2157 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002159getexline(c, cookie, indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 int c; /* normally ':', NUL for ":append" */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002161 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 int indent; /* indent for inside conditionals */
2163{
2164 /* When executing a register, remove ':' that's in front of each line. */
2165 if (exec_from_reg && vpeekc() == ':')
2166 (void)vgetc();
2167 return getcmdline(c, 1L, indent);
2168}
2169
2170/*
2171 * Get an Ex command line for Ex mode.
2172 * In Ex mode we only use the OS supplied line editing features and no
2173 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002174 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 char_u *
Bram Moolenaar78a15312009-05-15 19:33:18 +00002177getexmodeline(promptc, cookie, indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002178 int promptc; /* normally ':', NUL for ":append" and '?' for
2179 :s prompt */
Bram Moolenaar78a15312009-05-15 19:33:18 +00002180 void *cookie UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 int indent; /* indent for inside conditionals */
2182{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002183 garray_T line_ga;
2184 char_u *pend;
2185 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002186 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002187 int escaped = FALSE; /* CTRL-V typed */
2188 int vcol = 0;
2189 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002190 int prev_char;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 /* Switch cursor on now. This avoids that it happens after the "\n", which
2193 * confuses the system function that computes tabstops. */
2194 cursor_on();
2195
2196 /* always start in column 0; write a newline if necessary */
2197 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002198 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002202 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 if (p_prompt)
2204 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 while (indent-- > 0)
2206 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 }
2209
2210 ga_init2(&line_ga, 1, 30);
2211
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002212 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002213 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002214 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002215 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002216 while (indent >= 8)
2217 {
2218 ga_append(&line_ga, TAB);
2219 msg_puts((char_u *)" ");
2220 indent -= 8;
2221 }
2222 while (indent-- > 0)
2223 {
2224 ga_append(&line_ga, ' ');
2225 msg_putchar(' ');
2226 }
2227 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002228 ++no_mapping;
2229 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002230
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 /*
2232 * Get the line, one character at a time.
2233 */
2234 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002235 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 {
2237 if (ga_grow(&line_ga, 40) == FAIL)
2238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002240 /* Get one character at a time. Don't use inchar(), it can't handle
2241 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002242 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002243 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
2245 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 * Handle line editing.
2247 * Previously this was left to the system, putting the terminal in
2248 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002250 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 msg_putchar('\n');
2253 break;
2254 }
2255
2256 if (!escaped)
2257 {
2258 /* CR typed means "enter", which is NL */
2259 if (c1 == '\r')
2260 c1 = '\n';
2261
2262 if (c1 == BS || c1 == K_BS
2263 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002265 if (line_ga.ga_len > 0)
2266 {
2267 --line_ga.ga_len;
2268 goto redraw;
2269 }
2270 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 }
2272
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002273 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002275 msg_col = startcol;
2276 msg_clr_eos();
2277 line_ga.ga_len = 0;
2278 continue;
2279 }
2280
2281 if (c1 == Ctrl_T)
2282 {
Bram Moolenaar14f24742012-08-08 18:01:05 +02002283 long sw = get_sw_value();
2284
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002285 p = (char_u *)line_ga.ga_data;
2286 p[line_ga.ga_len] = NUL;
2287 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002288 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289add_indent:
2290 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002292 char_u *s = skipwhite(p);
2293
2294 ga_grow(&line_ga, 1);
2295 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2296 *s = ' ';
2297 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299redraw:
2300 /* redraw the line */
2301 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002302 vcol = 0;
2303 for (p = (char_u *)line_ga.ga_data;
2304 p < (char_u *)line_ga.ga_data + line_ga.ga_len; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002306 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002308 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 msg_putchar(' ');
2311 } while (++vcol % 8);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002315 msg_outtrans_len(p, 1);
2316 vcol += char2cells(*p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
2318 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002319 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002320 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002321 continue;
2322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 if (c1 == Ctrl_D)
2325 {
2326 /* Delete one shiftwidth. */
2327 p = (char_u *)line_ga.ga_data;
2328 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 if (prev_char == '^')
2331 ex_keep_indent = TRUE;
2332 indent = 0;
2333 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 else
2336 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337 p[line_ga.ga_len] = NUL;
2338 indent = get_indent_str(p, 8);
2339 --indent;
Bram Moolenaar14f24742012-08-08 18:01:05 +02002340 indent -= indent % get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 while (get_indent_str(p, 8) > indent)
2343 {
2344 char_u *s = skipwhite(p);
2345
2346 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2347 --line_ga.ga_len;
2348 }
2349 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002351
2352 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2353 {
2354 escaped = TRUE;
2355 continue;
2356 }
2357
2358 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2359 if (IS_SPECIAL(c1))
2360 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002362
2363 if (IS_SPECIAL(c1))
2364 c1 = '?';
2365 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002366 if (c1 == '\n')
2367 msg_putchar('\n');
2368 else if (c1 == TAB)
2369 {
2370 /* Don't use chartabsize(), 'ts' can be different */
2371 do
2372 {
2373 msg_putchar(' ');
2374 } while (++vcol % 8);
2375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002378 msg_outtrans_len(
2379 ((char_u *)line_ga.ga_data) + line_ga.ga_len, 1);
2380 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002382 ++line_ga.ga_len;
2383 escaped = FALSE;
2384
2385 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002386 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002387
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002388 /* We are done when a NL is entered, but not when it comes after an
2389 * odd number of backslashes, that results in a NUL. */
2390 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002392 int bcount = 0;
2393
2394 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2395 ++bcount;
2396
2397 if (bcount > 0)
2398 {
2399 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2400 * "\NL", etc. */
2401 line_ga.ga_len -= (bcount + 1) / 2;
2402 pend -= (bcount + 1) / 2;
2403 pend[-1] = '\n';
2404 }
2405
2406 if ((bcount & 1) == 0)
2407 {
2408 --line_ga.ga_len;
2409 --pend;
2410 *pend = NUL;
2411 break;
2412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 }
2414 }
2415
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002416 --no_mapping;
2417 --allow_keys;
2418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 /* make following messages go to the next line */
2420 msg_didout = FALSE;
2421 msg_col = 0;
2422 if (msg_row < Rows - 1)
2423 ++msg_row;
2424 emsg_on_display = FALSE; /* don't want ui_delay() */
2425
2426 if (got_int)
2427 ga_clear(&line_ga);
2428
2429 return (char_u *)line_ga.ga_data;
2430}
2431
Bram Moolenaare344bea2005-09-01 20:46:49 +00002432# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2433 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434/*
2435 * Return TRUE if ccline.overstrike is on.
2436 */
2437 int
2438cmdline_overstrike()
2439{
2440 return ccline.overstrike;
2441}
2442
2443/*
2444 * Return TRUE if the cursor is at the end of the cmdline.
2445 */
2446 int
2447cmdline_at_end()
2448{
2449 return (ccline.cmdpos >= ccline.cmdlen);
2450}
2451#endif
2452
Bram Moolenaar9372a112005-12-06 19:59:18 +00002453#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454/*
2455 * Return the virtual column number at the current cursor position.
2456 * This is used by the IM code to obtain the start of the preedit string.
2457 */
2458 colnr_T
2459cmdline_getvcol_cursor()
2460{
2461 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2462 return MAXCOL;
2463
2464# ifdef FEAT_MBYTE
2465 if (has_mbyte)
2466 {
2467 colnr_T col;
2468 int i = 0;
2469
2470 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002471 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472
2473 return col;
2474 }
2475 else
2476# endif
2477 return ccline.cmdpos;
2478}
2479#endif
2480
2481#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2482/*
2483 * If part of the command line is an IM preedit string, redraw it with
2484 * IM feedback attributes. The cursor position is restored after drawing.
2485 */
2486 static void
2487redrawcmd_preedit()
2488{
2489 if ((State & CMDLINE)
2490 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002491 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 && !p_imdisable
2493 && im_is_preediting())
2494 {
2495 int cmdpos = 0;
2496 int cmdspos;
2497 int old_row;
2498 int old_col;
2499 colnr_T col;
2500
2501 old_row = msg_row;
2502 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002503 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504
2505# ifdef FEAT_MBYTE
2506 if (has_mbyte)
2507 {
2508 for (col = 0; col < preedit_start_col
2509 && cmdpos < ccline.cmdlen; ++col)
2510 {
2511 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002512 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514 }
2515 else
2516# endif
2517 {
2518 cmdspos += preedit_start_col;
2519 cmdpos += preedit_start_col;
2520 }
2521
2522 msg_row = cmdline_row + (cmdspos / (int)Columns);
2523 msg_col = cmdspos % (int)Columns;
2524 if (msg_row >= Rows)
2525 msg_row = Rows - 1;
2526
2527 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2528 {
2529 int char_len;
2530 int char_attr;
2531
2532 char_attr = im_get_feedback_attr(col);
2533 if (char_attr < 0)
2534 break; /* end of preedit string */
2535
2536# ifdef FEAT_MBYTE
2537 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002538 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 else
2540# endif
2541 char_len = 1;
2542
2543 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2544 cmdpos += char_len;
2545 }
2546
2547 msg_row = old_row;
2548 msg_col = old_col;
2549 }
2550}
2551#endif /* FEAT_XIM && FEAT_GUI_GTK */
2552
2553/*
2554 * Allocate a new command line buffer.
2555 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2556 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2557 */
2558 static void
2559alloc_cmdbuff(len)
2560 int len;
2561{
2562 /*
2563 * give some extra space to avoid having to allocate all the time
2564 */
2565 if (len < 80)
2566 len = 100;
2567 else
2568 len += 20;
2569
2570 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2571 ccline.cmdbufflen = len;
2572}
2573
2574/*
2575 * Re-allocate the command line to length len + something extra.
2576 * return FAIL for failure, OK otherwise
2577 */
2578 static int
2579realloc_cmdbuff(len)
2580 int len;
2581{
2582 char_u *p;
2583
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002584 if (len < ccline.cmdbufflen)
2585 return OK; /* no need to resize */
2586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 p = ccline.cmdbuff;
2588 alloc_cmdbuff(len); /* will get some more */
2589 if (ccline.cmdbuff == NULL) /* out of memory */
2590 {
2591 ccline.cmdbuff = p; /* keep the old one */
2592 return FAIL;
2593 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002594 /* There isn't always a NUL after the command, but it may need to be
2595 * there, thus copy up to the NUL and add a NUL. */
2596 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2597 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002599
2600 if (ccline.xpc != NULL
2601 && ccline.xpc->xp_pattern != NULL
2602 && ccline.xpc->xp_context != EXPAND_NOTHING
2603 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2604 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002605 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002606
2607 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2608 * to point into the newly allocated memory. */
2609 if (i >= 0 && i <= ccline.cmdlen)
2610 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2611 }
2612
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 return OK;
2614}
2615
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002616#if defined(FEAT_ARABIC) || defined(PROTO)
2617static char_u *arshape_buf = NULL;
2618
2619# if defined(EXITFREE) || defined(PROTO)
2620 void
2621free_cmdline_buf()
2622{
2623 vim_free(arshape_buf);
2624}
2625# endif
2626#endif
2627
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628/*
2629 * Draw part of the cmdline at the current cursor position. But draw stars
2630 * when cmdline_star is TRUE.
2631 */
2632 static void
2633draw_cmdline(start, len)
2634 int start;
2635 int len;
2636{
2637#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2638 int i;
2639
2640 if (cmdline_star > 0)
2641 for (i = 0; i < len; ++i)
2642 {
2643 msg_putchar('*');
2644# ifdef FEAT_MBYTE
2645 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002646 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647# endif
2648 }
2649 else
2650#endif
2651#ifdef FEAT_ARABIC
2652 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2653 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 static int buflen = 0;
2655 char_u *p;
2656 int j;
2657 int newlen = 0;
2658 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002659 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 int prev_c = 0;
2661 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002662 int u8c;
2663 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665
2666 /*
2667 * Do arabic shaping into a temporary buffer. This is very
2668 * inefficient!
2669 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002670 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 {
2672 /* Re-allocate the buffer. We keep it around to avoid a lot of
2673 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002674 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002675 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002676 arshape_buf = alloc(buflen);
2677 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return; /* out of memory */
2679 }
2680
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002681 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2682 {
2683 /* Prepend a space to draw the leading composing char on. */
2684 arshape_buf[0] = ' ';
2685 newlen = 1;
2686 }
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 for (j = start; j < start + len; j += mb_l)
2689 {
2690 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002691 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002692 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (ARABIC_CHAR(u8c))
2694 {
2695 /* Do Arabic shaping. */
2696 if (cmdmsg_rl)
2697 {
2698 /* displaying from right to left */
2699 pc = prev_c;
2700 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002701 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 if (j + mb_l >= start + len)
2703 nc = NUL;
2704 else
2705 nc = utf_ptr2char(p + mb_l);
2706 }
2707 else
2708 {
2709 /* displaying from left to right */
2710 if (j + mb_l >= start + len)
2711 pc = NUL;
2712 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002713 {
2714 int pcc[MAX_MCO];
2715
2716 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002718 pc1 = pcc[0];
2719 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 nc = prev_c;
2721 }
2722 prev_c = u8c;
2723
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002724 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002726 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002727 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002729 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2730 if (u8cc[1] != 0)
2731 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002732 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
2734 }
2735 else
2736 {
2737 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002738 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 newlen += mb_l;
2740 }
2741 }
2742
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002743 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 }
2745 else
2746#endif
2747 msg_outtrans_len(ccline.cmdbuff + start, len);
2748}
2749
2750/*
2751 * Put a character on the command line. Shifts the following text to the
2752 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2753 * "c" must be printable (fit in one display cell)!
2754 */
2755 void
2756putcmdline(c, shift)
2757 int c;
2758 int shift;
2759{
2760 if (cmd_silent)
2761 return;
2762 msg_no_more = TRUE;
2763 msg_putchar(c);
2764 if (shift)
2765 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2766 msg_no_more = FALSE;
2767 cursorcmd();
2768}
2769
2770/*
2771 * Undo a putcmdline(c, FALSE).
2772 */
2773 void
2774unputcmdline()
2775{
2776 if (cmd_silent)
2777 return;
2778 msg_no_more = TRUE;
2779 if (ccline.cmdlen == ccline.cmdpos)
2780 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002781#ifdef FEAT_MBYTE
2782 else if (has_mbyte)
2783 draw_cmdline(ccline.cmdpos,
2784 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
2787 draw_cmdline(ccline.cmdpos, 1);
2788 msg_no_more = FALSE;
2789 cursorcmd();
2790}
2791
2792/*
2793 * Put the given string, of the given length, onto the command line.
2794 * If len is -1, then STRLEN() is used to calculate the length.
2795 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2796 * part will be redrawn, otherwise it will not. If this function is called
2797 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2798 * called afterwards.
2799 */
2800 int
2801put_on_cmdline(str, len, redraw)
2802 char_u *str;
2803 int len;
2804 int redraw;
2805{
2806 int retval;
2807 int i;
2808 int m;
2809 int c;
2810
2811 if (len < 0)
2812 len = (int)STRLEN(str);
2813
2814 /* Check if ccline.cmdbuff needs to be longer */
2815 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002816 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 else
2818 retval = OK;
2819 if (retval == OK)
2820 {
2821 if (!ccline.overstrike)
2822 {
2823 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2824 ccline.cmdbuff + ccline.cmdpos,
2825 (size_t)(ccline.cmdlen - ccline.cmdpos));
2826 ccline.cmdlen += len;
2827 }
2828 else
2829 {
2830#ifdef FEAT_MBYTE
2831 if (has_mbyte)
2832 {
2833 /* Count nr of characters in the new string. */
2834 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002835 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 ++m;
2837 /* Count nr of bytes in cmdline that are overwritten by these
2838 * characters. */
2839 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002840 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 --m;
2842 if (i < ccline.cmdlen)
2843 {
2844 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2845 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2846 ccline.cmdlen += ccline.cmdpos + len - i;
2847 }
2848 else
2849 ccline.cmdlen = ccline.cmdpos + len;
2850 }
2851 else
2852#endif
2853 if (ccline.cmdpos + len > ccline.cmdlen)
2854 ccline.cmdlen = ccline.cmdpos + len;
2855 }
2856 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2857 ccline.cmdbuff[ccline.cmdlen] = NUL;
2858
2859#ifdef FEAT_MBYTE
2860 if (enc_utf8)
2861 {
2862 /* When the inserted text starts with a composing character,
2863 * backup to the character before it. There could be two of them.
2864 */
2865 i = 0;
2866 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2867 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2868 {
2869 i = (*mb_head_off)(ccline.cmdbuff,
2870 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2871 ccline.cmdpos -= i;
2872 len += i;
2873 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2874 }
2875# ifdef FEAT_ARABIC
2876 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2877 {
2878 /* Check the previous character for Arabic combining pair. */
2879 i = (*mb_head_off)(ccline.cmdbuff,
2880 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2881 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2882 + ccline.cmdpos - i), c))
2883 {
2884 ccline.cmdpos -= i;
2885 len += i;
2886 }
2887 else
2888 i = 0;
2889 }
2890# endif
2891 if (i != 0)
2892 {
2893 /* Also backup the cursor position. */
2894 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2895 ccline.cmdspos -= i;
2896 msg_col -= i;
2897 if (msg_col < 0)
2898 {
2899 msg_col += Columns;
2900 --msg_row;
2901 }
2902 }
2903 }
2904#endif
2905
2906 if (redraw && !cmd_silent)
2907 {
2908 msg_no_more = TRUE;
2909 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002910 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2912 /* Avoid clearing the rest of the line too often. */
2913 if (cmdline_row != i || ccline.overstrike)
2914 msg_clr_eos();
2915 msg_no_more = FALSE;
2916 }
2917#ifdef FEAT_FKMAP
2918 /*
2919 * If we are in Farsi command mode, the character input must be in
2920 * Insert mode. So do not advance the cmdpos.
2921 */
2922 if (!cmd_fkmap)
2923#endif
2924 {
2925 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002926 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002928 if (m < 0) /* overflow, Columns or Rows at weird value */
2929 m = MAXCOL;
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 else
2932 m = MAXCOL;
2933 for (i = 0; i < len; ++i)
2934 {
2935 c = cmdline_charsize(ccline.cmdpos);
2936#ifdef FEAT_MBYTE
2937 /* count ">" for a double-wide char that doesn't fit. */
2938 if (has_mbyte)
2939 correct_cmdspos(ccline.cmdpos, c);
2940#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002941 /* Stop cursor at the end of the screen, but do increment the
2942 * insert position, so that entering a very long command
2943 * works, even though you can't see it. */
2944 if (ccline.cmdspos + c < m)
2945 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946#ifdef FEAT_MBYTE
2947 if (has_mbyte)
2948 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002949 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (c > len - i - 1)
2951 c = len - i - 1;
2952 ccline.cmdpos += c;
2953 i += c;
2954 }
2955#endif
2956 ++ccline.cmdpos;
2957 }
2958 }
2959 }
2960 if (redraw)
2961 msg_check();
2962 return retval;
2963}
2964
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002965static struct cmdline_info prev_ccline;
2966static int prev_ccline_used = FALSE;
2967
2968/*
2969 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2970 * and overwrite it. But get_cmdline_str() may need it, thus make it
2971 * available globally in prev_ccline.
2972 */
2973 static void
2974save_cmdline(ccp)
2975 struct cmdline_info *ccp;
2976{
2977 if (!prev_ccline_used)
2978 {
2979 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2980 prev_ccline_used = TRUE;
2981 }
2982 *ccp = prev_ccline;
2983 prev_ccline = ccline;
2984 ccline.cmdbuff = NULL;
2985 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002986 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002987}
2988
2989/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00002990 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002991 */
2992 static void
2993restore_cmdline(ccp)
2994 struct cmdline_info *ccp;
2995{
2996 ccline = prev_ccline;
2997 prev_ccline = *ccp;
2998}
2999
Bram Moolenaar5a305422006-04-28 22:38:25 +00003000#if defined(FEAT_EVAL) || defined(PROTO)
3001/*
3002 * Save the command line into allocated memory. Returns a pointer to be
3003 * passed to restore_cmdline_alloc() later.
3004 * Returns NULL when failed.
3005 */
3006 char_u *
3007save_cmdline_alloc()
3008{
3009 struct cmdline_info *p;
3010
3011 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3012 if (p != NULL)
3013 save_cmdline(p);
3014 return (char_u *)p;
3015}
3016
3017/*
3018 * Restore the command line from the return value of save_cmdline_alloc().
3019 */
3020 void
3021restore_cmdline_alloc(p)
3022 char_u *p;
3023{
3024 if (p != NULL)
3025 {
3026 restore_cmdline((struct cmdline_info *)p);
3027 vim_free(p);
3028 }
3029}
3030#endif
3031
Bram Moolenaar8299df92004-07-10 09:47:34 +00003032/*
3033 * paste a yank register into the command line.
3034 * used by CTRL-R command in command-line mode
3035 * insert_reg() can't be used here, because special characters from the
3036 * register contents will be interpreted as commands.
3037 *
3038 * return FAIL for failure, OK otherwise
3039 */
3040 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003041cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003042 int regname;
3043 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003044 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003045{
3046 long i;
3047 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003048 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003049 int allocated;
3050 struct cmdline_info save_ccline;
3051
3052 /* check for valid regname; also accept special characters for CTRL-R in
3053 * the command line */
3054 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3055 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3056 return FAIL;
3057
3058 /* A register containing CTRL-R can cause an endless loop. Allow using
3059 * CTRL-C to break the loop. */
3060 line_breakcheck();
3061 if (got_int)
3062 return FAIL;
3063
3064#ifdef FEAT_CLIPBOARD
3065 regname = may_get_selection(regname);
3066#endif
3067
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003068 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003069 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003070 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003071 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003072 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003073 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003074 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003075
3076 if (i)
3077 {
3078 /* Got the value of a special register in "arg". */
3079 if (arg == NULL)
3080 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003081
3082 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3083 * part of the word. */
3084 p = arg;
3085 if (p_is && regname == Ctrl_W)
3086 {
3087 char_u *w;
3088 int len;
3089
3090 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003091 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003092 {
3093#ifdef FEAT_MBYTE
3094 if (has_mbyte)
3095 {
3096 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3097 if (!vim_iswordc(mb_ptr2char(w - len)))
3098 break;
3099 w -= len;
3100 }
3101 else
3102#endif
3103 {
3104 if (!vim_iswordc(w[-1]))
3105 break;
3106 --w;
3107 }
3108 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003109 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003110 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3111 p += len;
3112 }
3113
3114 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003115 if (allocated)
3116 vim_free(arg);
3117 return OK;
3118 }
3119
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003120 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003121}
3122
3123/*
3124 * Put a string on the command line.
3125 * When "literally" is TRUE, insert literally.
3126 * When "literally" is FALSE, insert as typed, but don't leave the command
3127 * line.
3128 */
3129 void
3130cmdline_paste_str(s, literally)
3131 char_u *s;
3132 int literally;
3133{
3134 int c, cv;
3135
3136 if (literally)
3137 put_on_cmdline(s, -1, TRUE);
3138 else
3139 while (*s != NUL)
3140 {
3141 cv = *s;
3142 if (cv == Ctrl_V && s[1])
3143 ++s;
3144#ifdef FEAT_MBYTE
3145 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003146 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003147 else
3148#endif
3149 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003150 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3151 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003152#ifdef UNIX
3153 || c == intr_char
3154#endif
3155 || (c == Ctrl_BSL && *s == Ctrl_N))
3156 stuffcharReadbuff(Ctrl_V);
3157 stuffcharReadbuff(c);
3158 }
3159}
3160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#ifdef FEAT_WILDMENU
3162/*
3163 * Delete characters on the command line, from "from" to the current
3164 * position.
3165 */
3166 static void
3167cmdline_del(from)
3168 int from;
3169{
3170 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3171 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3172 ccline.cmdlen -= ccline.cmdpos - from;
3173 ccline.cmdpos = from;
3174}
3175#endif
3176
3177/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003178 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 * search
3180 */
3181 void
3182redrawcmdline()
3183{
3184 if (cmd_silent)
3185 return;
3186 need_wait_return = FALSE;
3187 compute_cmdrow();
3188 redrawcmd();
3189 cursorcmd();
3190}
3191
3192 static void
3193redrawcmdprompt()
3194{
3195 int i;
3196
3197 if (cmd_silent)
3198 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003199 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 msg_putchar(ccline.cmdfirstc);
3201 if (ccline.cmdprompt != NULL)
3202 {
3203 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3204 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3205 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003206 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 --ccline.cmdindent;
3208 }
3209 else
3210 for (i = ccline.cmdindent; i > 0; --i)
3211 msg_putchar(' ');
3212}
3213
3214/*
3215 * Redraw what is currently on the command line.
3216 */
3217 void
3218redrawcmd()
3219{
3220 if (cmd_silent)
3221 return;
3222
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003223 /* when 'incsearch' is set there may be no command line while redrawing */
3224 if (ccline.cmdbuff == NULL)
3225 {
3226 windgoto(cmdline_row, 0);
3227 msg_clr_eos();
3228 return;
3229 }
3230
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 msg_start();
3232 redrawcmdprompt();
3233
3234 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3235 msg_no_more = TRUE;
3236 draw_cmdline(0, ccline.cmdlen);
3237 msg_clr_eos();
3238 msg_no_more = FALSE;
3239
3240 set_cmdspos_cursor();
3241
3242 /*
3243 * An emsg() before may have set msg_scroll. This is used in normal mode,
3244 * in cmdline mode we can reset them now.
3245 */
3246 msg_scroll = FALSE; /* next message overwrites cmdline */
3247
3248 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3249 * in cmdline mode */
3250 skip_redraw = FALSE;
3251}
3252
3253 void
3254compute_cmdrow()
3255{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003256 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 cmdline_row = Rows - 1;
3258 else
3259 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3260 + W_STATUS_HEIGHT(lastwin);
3261}
3262
3263 static void
3264cursorcmd()
3265{
3266 if (cmd_silent)
3267 return;
3268
3269#ifdef FEAT_RIGHTLEFT
3270 if (cmdmsg_rl)
3271 {
3272 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3273 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3274 if (msg_row <= 0)
3275 msg_row = Rows - 1;
3276 }
3277 else
3278#endif
3279 {
3280 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3281 msg_col = ccline.cmdspos % (int)Columns;
3282 if (msg_row >= Rows)
3283 msg_row = Rows - 1;
3284 }
3285
3286 windgoto(msg_row, msg_col);
3287#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3288 redrawcmd_preedit();
3289#endif
3290#ifdef MCH_CURSOR_SHAPE
3291 mch_update_cursor();
3292#endif
3293}
3294
3295 void
3296gotocmdline(clr)
3297 int clr;
3298{
3299 msg_start();
3300#ifdef FEAT_RIGHTLEFT
3301 if (cmdmsg_rl)
3302 msg_col = Columns - 1;
3303 else
3304#endif
3305 msg_col = 0; /* always start in column 0 */
3306 if (clr) /* clear the bottom line(s) */
3307 msg_clr_eos(); /* will reset clear_cmdline */
3308 windgoto(cmdline_row, 0);
3309}
3310
3311/*
3312 * Check the word in front of the cursor for an abbreviation.
3313 * Called when the non-id character "c" has been entered.
3314 * When an abbreviation is recognized it is removed from the text with
3315 * backspaces and the replacement string is inserted, followed by "c".
3316 */
3317 static int
3318ccheck_abbr(c)
3319 int c;
3320{
3321 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3322 return FALSE;
3323
3324 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3325}
3326
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003327#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3328 static int
3329#ifdef __BORLANDC__
3330_RTLENTRYF
3331#endif
3332sort_func_compare(s1, s2)
3333 const void *s1;
3334 const void *s2;
3335{
3336 char_u *p1 = *(char_u **)s1;
3337 char_u *p2 = *(char_u **)s2;
3338
3339 if (*p1 != '<' && *p2 == '<') return -1;
3340 if (*p1 == '<' && *p2 != '<') return 1;
3341 return STRCMP(p1, p2);
3342}
3343#endif
3344
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345/*
3346 * Return FAIL if this is not an appropriate context in which to do
3347 * completion of anything, return OK if it is (even if there are no matches).
3348 * For the caller, this means that the character is just passed through like a
3349 * normal character (instead of being expanded). This allows :s/^I^D etc.
3350 */
3351 static int
Bram Moolenaarb3479632012-11-28 16:49:58 +01003352nextwild(xp, type, options, escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 expand_T *xp;
3354 int type;
3355 int options; /* extra options for ExpandOne() */
Bram Moolenaarb3479632012-11-28 16:49:58 +01003356 int escape; /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357{
3358 int i, j;
3359 char_u *p1;
3360 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 int difflen;
3362 int v;
3363
3364 if (xp->xp_numfiles == -1)
3365 {
3366 set_expand_context(xp);
3367 cmd_showtail = expand_showtail(xp);
3368 }
3369
3370 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3371 {
3372 beep_flush();
3373 return OK; /* Something illegal on command line */
3374 }
3375 if (xp->xp_context == EXPAND_NOTHING)
3376 {
3377 /* Caller can use the character as a normal char instead */
3378 return FAIL;
3379 }
3380
3381 MSG_PUTS("..."); /* show that we are busy */
3382 out_flush();
3383
3384 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003385 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386
3387 if (type == WILD_NEXT || type == WILD_PREV)
3388 {
3389 /*
3390 * Get next/previous match for a previous expanded pattern.
3391 */
3392 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3393 }
3394 else
3395 {
3396 /*
3397 * Translate string into pattern and expand it.
3398 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003399 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3400 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 p2 = NULL;
3402 else
3403 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003404 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003405 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3406 if (escape)
3407 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003408
3409 if (p_wic)
3410 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003411 p2 = ExpandOne(xp, p1,
3412 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003413 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003415 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416 if (p2 != NULL && type == WILD_LONGEST)
3417 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003418 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 if (ccline.cmdbuff[i + j] == '*'
3420 || ccline.cmdbuff[i + j] == '?')
3421 break;
3422 if ((int)STRLEN(p2) < j)
3423 {
3424 vim_free(p2);
3425 p2 = NULL;
3426 }
3427 }
3428 }
3429 }
3430
3431 if (p2 != NULL && !got_int)
3432 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003433 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003434 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003436 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 xp->xp_pattern = ccline.cmdbuff + i;
3438 }
3439 else
3440 v = OK;
3441 if (v == OK)
3442 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003443 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3444 &ccline.cmdbuff[ccline.cmdpos],
3445 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3446 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 ccline.cmdlen += difflen;
3448 ccline.cmdpos += difflen;
3449 }
3450 }
3451 vim_free(p2);
3452
3453 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003454 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455
3456 /* When expanding a ":map" command and no matches are found, assume that
3457 * the key is supposed to be inserted literally */
3458 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3459 return FAIL;
3460
3461 if (xp->xp_numfiles <= 0 && p2 == NULL)
3462 beep_flush();
3463 else if (xp->xp_numfiles == 1)
3464 /* free expanded pattern */
3465 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3466
3467 return OK;
3468}
3469
3470/*
3471 * Do wildcard expansion on the string 'str'.
3472 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003473 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 * Return NULL for failure.
3475 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003476 * "orig" is the originally expanded string, copied to allocated memory. It
3477 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3478 * WILD_PREV "orig" should be NULL.
3479 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003480 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3481 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 *
3483 * mode = WILD_FREE: just free previously expanded matches
3484 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3485 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3486 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3487 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3488 * mode = WILD_ALL: return all matches concatenated
3489 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003490 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 *
3492 * options = WILD_LIST_NOTFOUND: list entries without a match
3493 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3494 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3495 * options = WILD_NO_BEEP: Don't beep for multiple matches
3496 * options = WILD_ADD_SLASH: add a slash after directory names
3497 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3498 * options = WILD_SILENT: don't print warning messages
3499 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003500 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 *
3502 * The variables xp->xp_context and xp->xp_backslash must have been set!
3503 */
3504 char_u *
3505ExpandOne(xp, str, orig, options, mode)
3506 expand_T *xp;
3507 char_u *str;
3508 char_u *orig; /* allocated copy of original of expanded string */
3509 int options;
3510 int mode;
3511{
3512 char_u *ss = NULL;
3513 static int findex;
3514 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003515 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 int i;
3517 long_u len;
3518 int non_suf_match; /* number without matching suffix */
3519
3520 /*
3521 * first handle the case of using an old match
3522 */
3523 if (mode == WILD_NEXT || mode == WILD_PREV)
3524 {
3525 if (xp->xp_numfiles > 0)
3526 {
3527 if (mode == WILD_PREV)
3528 {
3529 if (findex == -1)
3530 findex = xp->xp_numfiles;
3531 --findex;
3532 }
3533 else /* mode == WILD_NEXT */
3534 ++findex;
3535
3536 /*
3537 * When wrapping around, return the original string, set findex to
3538 * -1.
3539 */
3540 if (findex < 0)
3541 {
3542 if (orig_save == NULL)
3543 findex = xp->xp_numfiles - 1;
3544 else
3545 findex = -1;
3546 }
3547 if (findex >= xp->xp_numfiles)
3548 {
3549 if (orig_save == NULL)
3550 findex = 0;
3551 else
3552 findex = -1;
3553 }
3554#ifdef FEAT_WILDMENU
3555 if (p_wmnu)
3556 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3557 findex, cmd_showtail);
3558#endif
3559 if (findex == -1)
3560 return vim_strsave(orig_save);
3561 return vim_strsave(xp->xp_files[findex]);
3562 }
3563 else
3564 return NULL;
3565 }
3566
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003567 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3569 {
3570 FreeWild(xp->xp_numfiles, xp->xp_files);
3571 xp->xp_numfiles = -1;
3572 vim_free(orig_save);
3573 orig_save = NULL;
3574 }
3575 findex = 0;
3576
3577 if (mode == WILD_FREE) /* only release file name */
3578 return NULL;
3579
3580 if (xp->xp_numfiles == -1)
3581 {
3582 vim_free(orig_save);
3583 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003584 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 /*
3587 * Do the expansion.
3588 */
3589 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3590 options) == FAIL)
3591 {
3592#ifdef FNAME_ILLEGAL
3593 /* Illegal file name has been silently skipped. But when there
3594 * are wildcards, the real problem is that there was no match,
3595 * causing the pattern to be added, which has illegal characters.
3596 */
3597 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3598 EMSG2(_(e_nomatch2), str);
3599#endif
3600 }
3601 else if (xp->xp_numfiles == 0)
3602 {
3603 if (!(options & WILD_SILENT))
3604 EMSG2(_(e_nomatch2), str);
3605 }
3606 else
3607 {
3608 /* Escape the matches for use on the command line. */
3609 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3610
3611 /*
3612 * Check for matching suffixes in file names.
3613 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003614 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3615 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 {
3617 if (xp->xp_numfiles)
3618 non_suf_match = xp->xp_numfiles;
3619 else
3620 non_suf_match = 1;
3621 if ((xp->xp_context == EXPAND_FILES
3622 || xp->xp_context == EXPAND_DIRECTORIES)
3623 && xp->xp_numfiles > 1)
3624 {
3625 /*
3626 * More than one match; check suffix.
3627 * The files will have been sorted on matching suffix in
3628 * expand_wildcards, only need to check the first two.
3629 */
3630 non_suf_match = 0;
3631 for (i = 0; i < 2; ++i)
3632 if (match_suffix(xp->xp_files[i]))
3633 ++non_suf_match;
3634 }
3635 if (non_suf_match != 1)
3636 {
3637 /* Can we ever get here unless it's while expanding
3638 * interactively? If not, we can get rid of this all
3639 * together. Don't really want to wait for this message
3640 * (and possibly have to hit return to continue!).
3641 */
3642 if (!(options & WILD_SILENT))
3643 EMSG(_(e_toomany));
3644 else if (!(options & WILD_NO_BEEP))
3645 beep_flush();
3646 }
3647 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3648 ss = vim_strsave(xp->xp_files[0]);
3649 }
3650 }
3651 }
3652
3653 /* Find longest common part */
3654 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3655 {
3656 for (len = 0; xp->xp_files[0][len]; ++len)
3657 {
3658 for (i = 0; i < xp->xp_numfiles; ++i)
3659 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003660 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003662 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003663 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
3665 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3666 TOLOWER_LOC(xp->xp_files[0][len]))
3667 break;
3668 }
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003669 else if (xp->xp_files[i][len] != xp->xp_files[0][len])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 break;
3671 }
3672 if (i < xp->xp_numfiles)
3673 {
3674 if (!(options & WILD_NO_BEEP))
3675 vim_beep();
3676 break;
3677 }
3678 }
3679 ss = alloc((unsigned)len + 1);
3680 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003681 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 findex = -1; /* next p_wc gets first one */
3683 }
3684
3685 /* Concatenate all matching names */
3686 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3687 {
3688 len = 0;
3689 for (i = 0; i < xp->xp_numfiles; ++i)
3690 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3691 ss = lalloc(len, TRUE);
3692 if (ss != NULL)
3693 {
3694 *ss = NUL;
3695 for (i = 0; i < xp->xp_numfiles; ++i)
3696 {
3697 STRCAT(ss, xp->xp_files[i]);
3698 if (i != xp->xp_numfiles - 1)
3699 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3700 }
3701 }
3702 }
3703
3704 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3705 ExpandCleanup(xp);
3706
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003707 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003708 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003709 vim_free(orig);
3710
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 return ss;
3712}
3713
3714/*
3715 * Prepare an expand structure for use.
3716 */
3717 void
3718ExpandInit(xp)
3719 expand_T *xp;
3720{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003721 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003722 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003724#ifndef BACKSLASH_IN_FILENAME
3725 xp->xp_shell = FALSE;
3726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 xp->xp_numfiles = -1;
3728 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003729#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3730 xp->xp_arg = NULL;
3731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732}
3733
3734/*
3735 * Cleanup an expand structure after use.
3736 */
3737 void
3738ExpandCleanup(xp)
3739 expand_T *xp;
3740{
3741 if (xp->xp_numfiles >= 0)
3742 {
3743 FreeWild(xp->xp_numfiles, xp->xp_files);
3744 xp->xp_numfiles = -1;
3745 }
3746}
3747
3748 void
3749ExpandEscape(xp, str, numfiles, files, options)
3750 expand_T *xp;
3751 char_u *str;
3752 int numfiles;
3753 char_u **files;
3754 int options;
3755{
3756 int i;
3757 char_u *p;
3758
3759 /*
3760 * May change home directory back to "~"
3761 */
3762 if (options & WILD_HOME_REPLACE)
3763 tilde_replace(str, numfiles, files);
3764
3765 if (options & WILD_ESCAPE)
3766 {
3767 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003768 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003769 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 || xp->xp_context == EXPAND_BUFFERS
3771 || xp->xp_context == EXPAND_DIRECTORIES)
3772 {
3773 /*
3774 * Insert a backslash into a file name before a space, \, %, #
3775 * and wildmatch characters, except '~'.
3776 */
3777 for (i = 0; i < numfiles; ++i)
3778 {
3779 /* for ":set path=" we need to escape spaces twice */
3780 if (xp->xp_backslash == XP_BS_THREE)
3781 {
3782 p = vim_strsave_escaped(files[i], (char_u *)" ");
3783 if (p != NULL)
3784 {
3785 vim_free(files[i]);
3786 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003787#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 p = vim_strsave_escaped(files[i], (char_u *)" ");
3789 if (p != NULL)
3790 {
3791 vim_free(files[i]);
3792 files[i] = p;
3793 }
3794#endif
3795 }
3796 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003797#ifdef BACKSLASH_IN_FILENAME
3798 p = vim_strsave_fnameescape(files[i], FALSE);
3799#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003800 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 if (p != NULL)
3803 {
3804 vim_free(files[i]);
3805 files[i] = p;
3806 }
3807
3808 /* If 'str' starts with "\~", replace "~" at start of
3809 * files[i] with "\~". */
3810 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003811 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 }
3813 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003814
3815 /* If the first file starts with a '+' escape it. Otherwise it
3816 * could be seen as "+cmd". */
3817 if (*files[0] == '+')
3818 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 else if (xp->xp_context == EXPAND_TAGS)
3821 {
3822 /*
3823 * Insert a backslash before characters in a tag name that
3824 * would terminate the ":tag" command.
3825 */
3826 for (i = 0; i < numfiles; ++i)
3827 {
3828 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3829 if (p != NULL)
3830 {
3831 vim_free(files[i]);
3832 files[i] = p;
3833 }
3834 }
3835 }
3836 }
3837}
3838
3839/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003840 * Escape special characters in "fname" for when used as a file name argument
3841 * after a Vim command, or, when "shell" is non-zero, a shell command.
3842 * Returns the result in allocated memory.
3843 */
3844 char_u *
3845vim_strsave_fnameescape(fname, shell)
3846 char_u *fname;
3847 int shell;
3848{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003849 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003850#ifdef BACKSLASH_IN_FILENAME
3851 char_u buf[20];
3852 int j = 0;
3853
3854 /* Don't escape '[' and '{' if they are in 'isfname'. */
3855 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
3856 if ((*p != '[' && *p != '{') || !vim_isfilec(*p))
3857 buf[j++] = *p;
3858 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003859 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003860#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003861 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3862 if (shell && csh_like_shell() && p != NULL)
3863 {
3864 char_u *s;
3865
3866 /* For csh and similar shells need to put two backslashes before '!'.
3867 * One is taken by Vim, one by the shell. */
3868 s = vim_strsave_escaped(p, (char_u *)"!");
3869 vim_free(p);
3870 p = s;
3871 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003872#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003873
3874 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3875 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003876 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003877 escape_fname(&p);
3878
3879 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003880}
3881
3882/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003883 * Put a backslash before the file name in "pp", which is in allocated memory.
3884 */
3885 static void
3886escape_fname(pp)
3887 char_u **pp;
3888{
3889 char_u *p;
3890
3891 p = alloc((unsigned)(STRLEN(*pp) + 2));
3892 if (p != NULL)
3893 {
3894 p[0] = '\\';
3895 STRCPY(p + 1, *pp);
3896 vim_free(*pp);
3897 *pp = p;
3898 }
3899}
3900
3901/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 * For each file name in files[num_files]:
3903 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3904 */
3905 void
3906tilde_replace(orig_pat, num_files, files)
3907 char_u *orig_pat;
3908 int num_files;
3909 char_u **files;
3910{
3911 int i;
3912 char_u *p;
3913
3914 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3915 {
3916 for (i = 0; i < num_files; ++i)
3917 {
3918 p = home_replace_save(NULL, files[i]);
3919 if (p != NULL)
3920 {
3921 vim_free(files[i]);
3922 files[i] = p;
3923 }
3924 }
3925 }
3926}
3927
3928/*
3929 * Show all matches for completion on the command line.
3930 * Returns EXPAND_NOTHING when the character that triggered expansion should
3931 * be inserted like a normal character.
3932 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 static int
3934showmatches(xp, wildmenu)
3935 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003936 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937{
3938#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3939 int num_files;
3940 char_u **files_found;
3941 int i, j, k;
3942 int maxlen;
3943 int lines;
3944 int columns;
3945 char_u *p;
3946 int lastlen;
3947 int attr;
3948 int showtail;
3949
3950 if (xp->xp_numfiles == -1)
3951 {
3952 set_expand_context(xp);
3953 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3954 &num_files, &files_found);
3955 showtail = expand_showtail(xp);
3956 if (i != EXPAND_OK)
3957 return i;
3958
3959 }
3960 else
3961 {
3962 num_files = xp->xp_numfiles;
3963 files_found = xp->xp_files;
3964 showtail = cmd_showtail;
3965 }
3966
3967#ifdef FEAT_WILDMENU
3968 if (!wildmenu)
3969 {
3970#endif
3971 msg_didany = FALSE; /* lines_left will be set */
3972 msg_start(); /* prepare for paging */
3973 msg_putchar('\n');
3974 out_flush();
3975 cmdline_row = msg_row;
3976 msg_didany = FALSE; /* lines_left will be set again */
3977 msg_start(); /* prepare for paging */
3978#ifdef FEAT_WILDMENU
3979 }
3980#endif
3981
3982 if (got_int)
3983 got_int = FALSE; /* only int. the completion, not the cmd line */
3984#ifdef FEAT_WILDMENU
3985 else if (wildmenu)
3986 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
3987#endif
3988 else
3989 {
3990 /* find the length of the longest file name */
3991 maxlen = 0;
3992 for (i = 0; i < num_files; ++i)
3993 {
3994 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003995 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 || xp->xp_context == EXPAND_BUFFERS))
3997 {
3998 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
3999 j = vim_strsize(NameBuff);
4000 }
4001 else
4002 j = vim_strsize(L_SHOWFILE(i));
4003 if (j > maxlen)
4004 maxlen = j;
4005 }
4006
4007 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4008 lines = num_files;
4009 else
4010 {
4011 /* compute the number of columns and lines for the listing */
4012 maxlen += 2; /* two spaces between file names */
4013 columns = ((int)Columns + 2) / maxlen;
4014 if (columns < 1)
4015 columns = 1;
4016 lines = (num_files + columns - 1) / columns;
4017 }
4018
4019 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4020
4021 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4022 {
4023 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4024 msg_clr_eos();
4025 msg_advance(maxlen - 3);
4026 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4027 }
4028
4029 /* list the files line by line */
4030 for (i = 0; i < lines; ++i)
4031 {
4032 lastlen = 999;
4033 for (k = i; k < num_files; k += lines)
4034 {
4035 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4036 {
4037 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4038 p = files_found[k] + STRLEN(files_found[k]) + 1;
4039 msg_advance(maxlen + 1);
4040 msg_puts(p);
4041 msg_advance(maxlen + 3);
4042 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4043 break;
4044 }
4045 for (j = maxlen - lastlen; --j >= 0; )
4046 msg_putchar(' ');
4047 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004048 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 || xp->xp_context == EXPAND_BUFFERS)
4050 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004051 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004052 if (xp->xp_numfiles != -1)
4053 {
4054 char_u *halved_slash;
4055 char_u *exp_path;
4056
4057 /* Expansion was done before and special characters
4058 * were escaped, need to halve backslashes. Also
4059 * $HOME has been replaced with ~/. */
4060 exp_path = expand_env_save_opt(files_found[k], TRUE);
4061 halved_slash = backslash_halve_save(
4062 exp_path != NULL ? exp_path : files_found[k]);
4063 j = mch_isdir(halved_slash != NULL ? halved_slash
4064 : files_found[k]);
4065 vim_free(exp_path);
4066 vim_free(halved_slash);
4067 }
4068 else
4069 /* Expansion was done here, file names are literal. */
4070 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (showtail)
4072 p = L_SHOWFILE(k);
4073 else
4074 {
4075 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4076 TRUE);
4077 p = NameBuff;
4078 }
4079 }
4080 else
4081 {
4082 j = FALSE;
4083 p = L_SHOWFILE(k);
4084 }
4085 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4086 }
4087 if (msg_col > 0) /* when not wrapped around */
4088 {
4089 msg_clr_eos();
4090 msg_putchar('\n');
4091 }
4092 out_flush(); /* show one line at a time */
4093 if (got_int)
4094 {
4095 got_int = FALSE;
4096 break;
4097 }
4098 }
4099
4100 /*
4101 * we redraw the command below the lines that we have just listed
4102 * This is a bit tricky, but it saves a lot of screen updating.
4103 */
4104 cmdline_row = msg_row; /* will put it back later */
4105 }
4106
4107 if (xp->xp_numfiles == -1)
4108 FreeWild(num_files, files_found);
4109
4110 return EXPAND_OK;
4111}
4112
4113/*
4114 * Private gettail for showmatches() (and win_redr_status_matches()):
4115 * Find tail of file name path, but ignore trailing "/".
4116 */
4117 char_u *
4118sm_gettail(s)
4119 char_u *s;
4120{
4121 char_u *p;
4122 char_u *t = s;
4123 int had_sep = FALSE;
4124
4125 for (p = s; *p != NUL; )
4126 {
4127 if (vim_ispathsep(*p)
4128#ifdef BACKSLASH_IN_FILENAME
4129 && !rem_backslash(p)
4130#endif
4131 )
4132 had_sep = TRUE;
4133 else if (had_sep)
4134 {
4135 t = p;
4136 had_sep = FALSE;
4137 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004138 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 return t;
4141}
4142
4143/*
4144 * Return TRUE if we only need to show the tail of completion matches.
4145 * When not completing file names or there is a wildcard in the path FALSE is
4146 * returned.
4147 */
4148 static int
4149expand_showtail(xp)
4150 expand_T *xp;
4151{
4152 char_u *s;
4153 char_u *end;
4154
4155 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004156 if (xp->xp_context != EXPAND_FILES
4157 && xp->xp_context != EXPAND_SHELLCMD
4158 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return FALSE;
4160
4161 end = gettail(xp->xp_pattern);
4162 if (end == xp->xp_pattern) /* there is no path separator */
4163 return FALSE;
4164
4165 for (s = xp->xp_pattern; s < end; s++)
4166 {
4167 /* Skip escaped wildcards. Only when the backslash is not a path
4168 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4169 if (rem_backslash(s))
4170 ++s;
4171 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4172 return FALSE;
4173 }
4174 return TRUE;
4175}
4176
4177/*
4178 * Prepare a string for expansion.
4179 * When expanding file names: The string will be used with expand_wildcards().
4180 * Copy the file name into allocated memory and add a '*' at the end.
4181 * When expanding other names: The string will be used with regcomp(). Copy
4182 * the name into allocated memory and prepend "^".
4183 */
4184 char_u *
4185addstar(fname, len, context)
4186 char_u *fname;
4187 int len;
4188 int context; /* EXPAND_FILES etc. */
4189{
4190 char_u *retval;
4191 int i, j;
4192 int new_len;
4193 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004194 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004196 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004197 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004198 && context != EXPAND_SHELLCMD
4199 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
4201 /*
4202 * Matching will be done internally (on something other than files).
4203 * So we convert the file-matching-type wildcards into our kind for
4204 * use with vim_regcomp(). First work out how long it will be:
4205 */
4206
4207 /* For help tags the translation is done in find_help_tags().
4208 * For a tag pattern starting with "/" no translation is needed. */
4209 if (context == EXPAND_HELP
4210 || context == EXPAND_COLORS
4211 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004212 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004213 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 || (context == EXPAND_TAGS && fname[0] == '/'))
4215 retval = vim_strnsave(fname, len);
4216 else
4217 {
4218 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4219 for (i = 0; i < len; i++)
4220 {
4221 if (fname[i] == '*' || fname[i] == '~')
4222 new_len++; /* '*' needs to be replaced by ".*"
4223 '~' needs to be replaced by "\~" */
4224
4225 /* Buffer names are like file names. "." should be literal */
4226 if (context == EXPAND_BUFFERS && fname[i] == '.')
4227 new_len++; /* "." becomes "\." */
4228
4229 /* Custom expansion takes care of special things, match
4230 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004231 if ((context == EXPAND_USER_DEFINED
4232 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 new_len++; /* '\' becomes "\\" */
4234 }
4235 retval = alloc(new_len);
4236 if (retval != NULL)
4237 {
4238 retval[0] = '^';
4239 j = 1;
4240 for (i = 0; i < len; i++, j++)
4241 {
4242 /* Skip backslash. But why? At least keep it for custom
4243 * expansion. */
4244 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004245 && context != EXPAND_USER_LIST
4246 && fname[i] == '\\'
4247 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249
4250 switch (fname[i])
4251 {
4252 case '*': retval[j++] = '.';
4253 break;
4254 case '~': retval[j++] = '\\';
4255 break;
4256 case '?': retval[j] = '.';
4257 continue;
4258 case '.': if (context == EXPAND_BUFFERS)
4259 retval[j++] = '\\';
4260 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004261 case '\\': if (context == EXPAND_USER_DEFINED
4262 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 retval[j++] = '\\';
4264 break;
4265 }
4266 retval[j] = fname[i];
4267 }
4268 retval[j] = NUL;
4269 }
4270 }
4271 }
4272 else
4273 {
4274 retval = alloc(len + 4);
4275 if (retval != NULL)
4276 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004277 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004280 * Don't add a star to *, ~, ~user, $var or `cmd`.
4281 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 * ~ would be at the start of the file name, but not the tail.
4283 * $ could be anywhere in the tail.
4284 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004285 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 */
4287 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004288 ends_in_star = (len > 0 && retval[len - 1] == '*');
4289#ifndef BACKSLASH_IN_FILENAME
4290 for (i = len - 2; i >= 0; --i)
4291 {
4292 if (retval[i] != '\\')
4293 break;
4294 ends_in_star = !ends_in_star;
4295 }
4296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004298 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 && vim_strchr(tail, '$') == NULL
4300 && vim_strchr(retval, '`') == NULL)
4301 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004302 else if (len > 0 && retval[len - 1] == '$')
4303 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 retval[len] = NUL;
4305 }
4306 }
4307 return retval;
4308}
4309
4310/*
4311 * Must parse the command line so far to work out what context we are in.
4312 * Completion can then be done based on that context.
4313 * This routine sets the variables:
4314 * xp->xp_pattern The start of the pattern to be expanded within
4315 * the command line (ends at the cursor).
4316 * xp->xp_context The type of thing to expand. Will be one of:
4317 *
4318 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4319 * the command line, like an unknown command. Caller
4320 * should beep.
4321 * EXPAND_NOTHING Unrecognised context for completion, use char like
4322 * a normal char, rather than for completion. eg
4323 * :s/^I/
4324 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4325 * it.
4326 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4327 * EXPAND_FILES After command with XFILE set, or after setting
4328 * with P_EXPAND set. eg :e ^I, :w>>^I
4329 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4330 * when we know only directories are of interest. eg
4331 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004332 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4334 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4335 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4336 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4337 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4338 * EXPAND_EVENTS Complete event names
4339 * EXPAND_SYNTAX Complete :syntax command arguments
4340 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4341 * EXPAND_AUGROUP Complete autocommand group names
4342 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4343 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4344 * eg :unmap a^I , :cunab x^I
4345 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4346 * eg :call sub^I
4347 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4348 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4349 * names in expressions, eg :while s^I
4350 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004351 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 */
4353 static void
4354set_expand_context(xp)
4355 expand_T *xp;
4356{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004357 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 if (ccline.cmdfirstc != ':'
4359#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004360 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004361 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362#endif
4363 )
4364 {
4365 xp->xp_context = EXPAND_NOTHING;
4366 return;
4367 }
4368 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4369}
4370
4371 void
4372set_cmd_context(xp, str, len, col)
4373 expand_T *xp;
4374 char_u *str; /* start of command line */
4375 int len; /* length of command line (excl. NUL) */
4376 int col; /* position of cursor */
4377{
4378 int old_char = NUL;
4379 char_u *nextcomm;
4380
4381 /*
4382 * Avoid a UMR warning from Purify, only save the character if it has been
4383 * written before.
4384 */
4385 if (col < len)
4386 old_char = str[col];
4387 str[col] = NUL;
4388 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004389
4390#ifdef FEAT_EVAL
4391 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004392 {
4393# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004394 /* pass CMD_SIZE because there is no real command */
4395 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004396# endif
4397 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004398 else if (ccline.input_fn)
4399 {
4400 xp->xp_context = ccline.xp_context;
4401 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004402# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004403 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004404# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004405 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004406 else
4407#endif
4408 while (nextcomm != NULL)
4409 nextcomm = set_one_cmd_context(xp, nextcomm);
4410
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 str[col] = old_char;
4412}
4413
4414/*
4415 * Expand the command line "str" from context "xp".
4416 * "xp" must have been set by set_cmd_context().
4417 * xp->xp_pattern points into "str", to where the text that is to be expanded
4418 * starts.
4419 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4420 * cursor.
4421 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4422 * key that triggered expansion literally.
4423 * Returns EXPAND_OK otherwise.
4424 */
4425 int
4426expand_cmdline(xp, str, col, matchcount, matches)
4427 expand_T *xp;
4428 char_u *str; /* start of command line */
4429 int col; /* position of cursor */
4430 int *matchcount; /* return: nr of matches */
4431 char_u ***matches; /* return: array of pointers to matches */
4432{
4433 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004434 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
4436 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4437 {
4438 beep_flush();
4439 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4440 }
4441 if (xp->xp_context == EXPAND_NOTHING)
4442 {
4443 /* Caller can use the character as a normal char instead */
4444 return EXPAND_NOTHING;
4445 }
4446
4447 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004448 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4449 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 if (file_str == NULL)
4451 return EXPAND_UNSUCCESSFUL;
4452
Bram Moolenaar94950a92010-12-02 16:01:29 +01004453 if (p_wic)
4454 options += WILD_ICASE;
4455
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004457 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 *matchcount = 0;
4460 *matches = NULL;
4461 }
4462 vim_free(file_str);
4463
4464 return EXPAND_OK;
4465}
4466
4467#ifdef FEAT_MULTI_LANG
4468/*
4469 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4470 */
4471static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4472
4473 static void
4474cleanup_help_tags(num_file, file)
4475 int num_file;
4476 char_u **file;
4477{
4478 int i, j;
4479 int len;
4480
4481 for (i = 0; i < num_file; ++i)
4482 {
4483 len = (int)STRLEN(file[i]) - 3;
4484 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4485 {
4486 /* Sorting on priority means the same item in another language may
4487 * be anywhere. Search all items for a match up to the "@en". */
4488 for (j = 0; j < num_file; ++j)
4489 if (j != i
4490 && (int)STRLEN(file[j]) == len + 3
4491 && STRNCMP(file[i], file[j], len + 1) == 0)
4492 break;
4493 if (j == num_file)
4494 file[i][len] = NUL;
4495 }
4496 }
4497}
4498#endif
4499
4500/*
4501 * Do the expansion based on xp->xp_context and "pat".
4502 */
4503 static int
4504ExpandFromContext(xp, pat, num_file, file, options)
4505 expand_T *xp;
4506 char_u *pat;
4507 int *num_file;
4508 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004509 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511#ifdef FEAT_CMDL_COMPL
4512 regmatch_T regmatch;
4513#endif
4514 int ret;
4515 int flags;
4516
4517 flags = EW_DIR; /* include directories */
4518 if (options & WILD_LIST_NOTFOUND)
4519 flags |= EW_NOTFOUND;
4520 if (options & WILD_ADD_SLASH)
4521 flags |= EW_ADDSLASH;
4522 if (options & WILD_KEEP_ALL)
4523 flags |= EW_KEEPALL;
4524 if (options & WILD_SILENT)
4525 flags |= EW_SILENT;
4526
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004527 if (xp->xp_context == EXPAND_FILES
4528 || xp->xp_context == EXPAND_DIRECTORIES
4529 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
4531 /*
4532 * Expand file or directory names.
4533 */
4534 int free_pat = FALSE;
4535 int i;
4536
4537 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4538 * space */
4539 if (xp->xp_backslash != XP_BS_NONE)
4540 {
4541 free_pat = TRUE;
4542 pat = vim_strsave(pat);
4543 for (i = 0; pat[i]; ++i)
4544 if (pat[i] == '\\')
4545 {
4546 if (xp->xp_backslash == XP_BS_THREE
4547 && pat[i + 1] == '\\'
4548 && pat[i + 2] == '\\'
4549 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004550 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 if (xp->xp_backslash == XP_BS_ONE
4552 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004553 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 }
4556
4557 if (xp->xp_context == EXPAND_FILES)
4558 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004559 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4560 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 else
4562 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004563 if (options & WILD_ICASE)
4564 flags |= EW_ICASE;
4565
Bram Moolenaard7834d32009-12-02 16:14:36 +00004566 /* Expand wildcards, supporting %:h and the like. */
4567 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 if (free_pat)
4569 vim_free(pat);
4570 return ret;
4571 }
4572
4573 *file = (char_u **)"";
4574 *num_file = 0;
4575 if (xp->xp_context == EXPAND_HELP)
4576 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004577 /* With an empty argument we would get all the help tags, which is
4578 * very slow. Get matches for "help" instead. */
4579 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4580 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582#ifdef FEAT_MULTI_LANG
4583 cleanup_help_tags(*num_file, *file);
4584#endif
4585 return OK;
4586 }
4587 return FAIL;
4588 }
4589
4590#ifndef FEAT_CMDL_COMPL
4591 return FAIL;
4592#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004593 if (xp->xp_context == EXPAND_SHELLCMD)
4594 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 if (xp->xp_context == EXPAND_OLD_SETTING)
4596 return ExpandOldSetting(num_file, file);
4597 if (xp->xp_context == EXPAND_BUFFERS)
4598 return ExpandBufnames(pat, num_file, file, options);
4599 if (xp->xp_context == EXPAND_TAGS
4600 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4601 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4602 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004603 {
4604 char *directories[] = {"colors", NULL};
4605 return ExpandRTDir(pat, num_file, file, directories);
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004608 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004609 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004610 return ExpandRTDir(pat, num_file, file, directories);
4611 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004612 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004613 {
4614 char *directories[] = {"syntax", NULL};
4615 return ExpandRTDir(pat, num_file, file, directories);
4616 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004617 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004618 {
4619 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4620 return ExpandRTDir(pat, num_file, file, directories);
4621 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004622# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4623 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004624 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004625# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
4627 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4628 if (regmatch.regprog == NULL)
4629 return FAIL;
4630
4631 /* set ignore-case according to p_ic, p_scs and pat */
4632 regmatch.rm_ic = ignorecase(pat);
4633
4634 if (xp->xp_context == EXPAND_SETTINGS
4635 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4636 ret = ExpandSettings(xp, &regmatch, num_file, file);
4637 else if (xp->xp_context == EXPAND_MAPPINGS)
4638 ret = ExpandMappings(&regmatch, num_file, file);
4639# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4640 else if (xp->xp_context == EXPAND_USER_DEFINED)
4641 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4642# endif
4643 else
4644 {
4645 static struct expgen
4646 {
4647 int context;
4648 char_u *((*func)__ARGS((expand_T *, int)));
4649 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004650 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 } tab[] =
4652 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004653 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4654 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004655#ifdef FEAT_CMDHIST
4656 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004659 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4660 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4661 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4662 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#endif
4664#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004665 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4666 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4667 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4668 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669#endif
4670#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004671 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4672 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673#endif
4674#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004675 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004677 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004679 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4680 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004682#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004683 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004684#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004685#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004686 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004687#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004688#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004689 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004690#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4692 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004693 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4694 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004696 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004697 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 };
4699 int i;
4700
4701 /*
4702 * Find a context in the table and call the ExpandGeneric() with the
4703 * right function to do the expansion.
4704 */
4705 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004706 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 if (xp->xp_context == tab[i].context)
4708 {
4709 if (tab[i].ic)
4710 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004711 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004712 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 break;
4714 }
4715 }
4716
4717 vim_free(regmatch.regprog);
4718
4719 return ret;
4720#endif /* FEAT_CMDL_COMPL */
4721}
4722
4723#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4724/*
4725 * Expand a list of names.
4726 *
4727 * Generic function for command line completion. It calls a function to
4728 * obtain strings, one by one. The strings are matched against a regexp
4729 * program. Matching strings are copied into an array, which is returned.
4730 *
4731 * Returns OK when no problems encountered, FAIL for error (out of memory).
4732 */
4733 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004734ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 expand_T *xp;
4736 regmatch_T *regmatch;
4737 int *num_file;
4738 char_u ***file;
4739 char_u *((*func)__ARGS((expand_T *, int)));
4740 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004741 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 int i;
4744 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004745 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 char_u *str;
4747
4748 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004749 * round == 0: count the number of matching names
4750 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004752 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
4754 for (i = 0; ; ++i)
4755 {
4756 str = (*func)(xp, i);
4757 if (str == NULL) /* end of list */
4758 break;
4759 if (*str == NUL) /* skip empty strings */
4760 continue;
4761
4762 if (vim_regexec(regmatch, str, (colnr_T)0))
4763 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004764 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004766 if (escaped)
4767 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4768 else
4769 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 (*file)[count] = str;
4771#ifdef FEAT_MENU
4772 if (func == get_menu_names && str != NULL)
4773 {
4774 /* test for separator added by get_menu_names() */
4775 str += STRLEN(str) - 1;
4776 if (*str == '\001')
4777 *str = '.';
4778 }
4779#endif
4780 }
4781 ++count;
4782 }
4783 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004784 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 if (count == 0)
4787 return OK;
4788 *num_file = count;
4789 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4790 if (*file == NULL)
4791 {
4792 *file = (char_u **)"";
4793 return FAIL;
4794 }
4795 count = 0;
4796 }
4797 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004798
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004799 /* Sort the results. Keep menu's in the specified order. */
4800 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004801 {
4802 if (xp->xp_context == EXPAND_EXPRESSION
4803 || xp->xp_context == EXPAND_FUNCTIONS
4804 || xp->xp_context == EXPAND_USER_FUNC)
4805 /* <SNR> functions should be sorted to the end. */
4806 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4807 sort_func_compare);
4808 else
4809 sort_strings(*file, *num_file);
4810 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004811
Bram Moolenaar4f688582007-07-24 12:34:30 +00004812#ifdef FEAT_CMDL_COMPL
4813 /* Reset the variables used for special highlight names expansion, so that
4814 * they don't show up when getting normal highlight names by ID. */
4815 reset_expand_highlight();
4816#endif
4817
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return OK;
4819}
4820
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004821/*
4822 * Complete a shell command.
4823 * Returns FAIL or OK;
4824 */
4825 static int
4826expand_shellcmd(filepat, num_file, file, flagsarg)
4827 char_u *filepat; /* pattern to match with command names */
4828 int *num_file; /* return: number of matches */
4829 char_u ***file; /* return: array with matches */
4830 int flagsarg; /* EW_ flags */
4831{
4832 char_u *pat;
4833 int i;
4834 char_u *path;
4835 int mustfree = FALSE;
4836 garray_T ga;
4837 char_u *buf = alloc(MAXPATHL);
4838 size_t l;
4839 char_u *s, *e;
4840 int flags = flagsarg;
4841 int ret;
4842
4843 if (buf == NULL)
4844 return FAIL;
4845
4846 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4847 * space */
4848 pat = vim_strsave(filepat);
4849 for (i = 0; pat[i]; ++i)
4850 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004851 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004852
4853 flags |= EW_FILE | EW_EXEC;
4854
4855 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004856 if (mch_isFullName(pat))
4857 path = (char_u *)" ";
4858 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004859 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4860 path = (char_u *)".";
4861 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004862 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004863 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004864 if (path == NULL)
4865 path = (char_u *)"";
4866 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004867
4868 /*
4869 * Go over all directories in $PATH. Expand matches in that directory and
4870 * collect them in "ga".
4871 */
4872 ga_init2(&ga, (int)sizeof(char *), 10);
4873 for (s = path; *s != NUL; s = e)
4874 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004875 if (*s == ' ')
4876 ++s; /* Skip space used for absolute path name. */
4877
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004878#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4879 e = vim_strchr(s, ';');
4880#else
4881 e = vim_strchr(s, ':');
4882#endif
4883 if (e == NULL)
4884 e = s + STRLEN(s);
4885
4886 l = e - s;
4887 if (l > MAXPATHL - 5)
4888 break;
4889 vim_strncpy(buf, s, l);
4890 add_pathsep(buf);
4891 l = STRLEN(buf);
4892 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4893
4894 /* Expand matches in one directory of $PATH. */
4895 ret = expand_wildcards(1, &buf, num_file, file, flags);
4896 if (ret == OK)
4897 {
4898 if (ga_grow(&ga, *num_file) == FAIL)
4899 FreeWild(*num_file, *file);
4900 else
4901 {
4902 for (i = 0; i < *num_file; ++i)
4903 {
4904 s = (*file)[i];
4905 if (STRLEN(s) > l)
4906 {
4907 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004908 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004909 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4910 }
4911 else
4912 vim_free(s);
4913 }
4914 vim_free(*file);
4915 }
4916 }
4917 if (*e != NUL)
4918 ++e;
4919 }
4920 *file = ga.ga_data;
4921 *num_file = ga.ga_len;
4922
4923 vim_free(buf);
4924 vim_free(pat);
4925 if (mustfree)
4926 vim_free(path);
4927 return OK;
4928}
4929
4930
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004932static 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));
4933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004935 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004936 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004938 static void *
4939call_user_expand_func(user_expand_func, xp, num_file, file)
4940 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 int *num_file;
4943 char_u ***file;
4944{
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004945 char_u keep;
4946 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004949 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004950 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0')
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 *num_file = 0;
4955 *file = NULL;
4956
Bram Moolenaar21669c02008-01-18 12:16:16 +00004957 if (ccline.cmdbuff == NULL)
4958 {
4959 /* Completion from Insert mode, pass fake arguments. */
4960 keep = 0;
Bram Moolenaar9a31f882008-01-22 11:44:47 +00004961 sprintf((char *)num, "%d", (int)STRLEN(xp->xp_pattern));
Bram Moolenaar21669c02008-01-18 12:16:16 +00004962 args[1] = xp->xp_pattern;
4963 }
4964 else
4965 {
4966 /* Completion on the command line, pass real arguments. */
4967 keep = ccline.cmdbuff[ccline.cmdlen];
4968 ccline.cmdbuff[ccline.cmdlen] = 0;
4969 sprintf((char *)num, "%d", ccline.cmdpos);
4970 args[1] = ccline.cmdbuff;
4971 }
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004972 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 args[2] = num;
4974
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004975 /* Save the cmdline, we don't know what the function may do. */
4976 save_ccline = ccline;
4977 ccline.cmdbuff = NULL;
4978 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004980
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004981 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004982
4983 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004985 if (ccline.cmdbuff != NULL)
4986 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004987
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004988 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004989 return ret;
4990}
4991
4992/*
4993 * Expand names with a function defined by the user.
4994 */
4995 static int
4996ExpandUserDefined(xp, regmatch, num_file, file)
4997 expand_T *xp;
4998 regmatch_T *regmatch;
4999 int *num_file;
5000 char_u ***file;
5001{
5002 char_u *retstr;
5003 char_u *s;
5004 char_u *e;
5005 char_u keep;
5006 garray_T ga;
5007
5008 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5009 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 return FAIL;
5011
5012 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005013 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
5015 e = vim_strchr(s, '\n');
5016 if (e == NULL)
5017 e = s + STRLEN(s);
5018 keep = *e;
5019 *e = 0;
5020
5021 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5022 {
5023 *e = keep;
5024 if (*e != NUL)
5025 ++e;
5026 continue;
5027 }
5028
5029 if (ga_grow(&ga, 1) == FAIL)
5030 break;
5031
5032 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5033 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 *e = keep;
5036 if (*e != NUL)
5037 ++e;
5038 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005039 vim_free(retstr);
5040 *file = ga.ga_data;
5041 *num_file = ga.ga_len;
5042 return OK;
5043}
5044
5045/*
5046 * Expand names with a list returned by a function defined by the user.
5047 */
5048 static int
5049ExpandUserList(xp, num_file, file)
5050 expand_T *xp;
5051 int *num_file;
5052 char_u ***file;
5053{
5054 list_T *retlist;
5055 listitem_T *li;
5056 garray_T ga;
5057
5058 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5059 if (retlist == NULL)
5060 return FAIL;
5061
5062 ga_init2(&ga, (int)sizeof(char *), 3);
5063 /* Loop over the items in the list. */
5064 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5065 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005066 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5067 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005068
5069 if (ga_grow(&ga, 1) == FAIL)
5070 break;
5071
5072 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005073 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005074 ++ga.ga_len;
5075 }
5076 list_unref(retlist);
5077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 *file = ga.ga_data;
5079 *num_file = ga.ga_len;
5080 return OK;
5081}
5082#endif
5083
5084/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005085 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005086 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005087 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 */
5089 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005090ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 char_u *pat;
5092 int *num_file;
5093 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095{
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005096 char_u *matches;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 char_u *s;
5098 char_u *e;
5099 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005100 int i;
5101 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 *num_file = 0;
5104 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005105 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005106 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005108 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005110 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5111 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005113 ga_clear_strings(&ga);
5114 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005116 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
5117 matches = globpath(p_rtp, s, 0);
5118 vim_free(s);
5119 if (matches == NULL)
5120 continue;
5121
5122 for (s = matches; *s != NUL; s = e)
5123 {
5124 e = vim_strchr(s, '\n');
5125 if (e == NULL)
5126 e = s + STRLEN(s);
5127 if (ga_grow(&ga, 1) == FAIL)
5128 break;
5129 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5130 {
5131 for (s = e - 4; s > matches; mb_ptr_back(matches, s))
5132 if (*s == '\n' || vim_ispathsep(*s))
5133 break;
5134 ++s;
5135 ((char_u **)ga.ga_data)[ga.ga_len] =
5136 vim_strnsave(s, (int)(e - s - 4));
5137 ++ga.ga_len;
5138 }
5139 if (*e != NUL)
5140 ++e;
5141 }
5142 vim_free(matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005145 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005146
5147 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005148 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005149 remove_duplicates(&ga);
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 *file = ga.ga_data;
5152 *num_file = ga.ga_len;
5153 return OK;
5154}
5155
5156#endif
5157
5158#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5159/*
5160 * Expand "file" for all comma-separated directories in "path".
5161 * Returns an allocated string with all matches concatenated, separated by
5162 * newlines. Returns NULL for an error or no matches.
5163 */
5164 char_u *
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005165globpath(path, file, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 char_u *path;
5167 char_u *file;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005168 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169{
5170 expand_T xpc;
5171 char_u *buf;
5172 garray_T ga;
5173 int i;
5174 int len;
5175 int num_p;
5176 char_u **p;
5177 char_u *cur = NULL;
5178
5179 buf = alloc(MAXPATHL);
5180 if (buf == NULL)
5181 return NULL;
5182
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005183 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005185
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 ga_init2(&ga, 1, 100);
5187
5188 /* Loop over all entries in {path}. */
5189 while (*path != NUL)
5190 {
5191 /* Copy one item of the path to buf[] and concatenate the file name. */
5192 copy_option_part(&path, buf, MAXPATHL, ",");
5193 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5194 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005195# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005196 /* Using the platform's path separator (\) makes vim incorrectly
5197 * treat it as an escape character, use '/' instead. */
5198 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5199 STRCAT(buf, "/");
5200# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005204 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5205 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005207 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 for (len = 0, i = 0; i < num_p; ++i)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005209 len += (int)STRLEN(p[i]) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210
5211 /* Concatenate new results to previous ones. */
5212 if (ga_grow(&ga, len) == OK)
5213 {
5214 cur = (char_u *)ga.ga_data + ga.ga_len;
5215 for (i = 0; i < num_p; ++i)
5216 {
5217 STRCPY(cur, p[i]);
5218 cur += STRLEN(p[i]);
5219 *cur++ = '\n';
5220 }
5221 ga.ga_len += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223 FreeWild(num_p, p);
5224 }
5225 }
5226 }
5227 if (cur != NULL)
5228 *--cur = 0; /* Replace trailing newline with NUL */
5229
5230 vim_free(buf);
5231 return (char_u *)ga.ga_data;
5232}
5233
5234#endif
5235
5236#if defined(FEAT_CMDHIST) || defined(PROTO)
5237
5238/*********************************
5239 * Command line history stuff *
5240 *********************************/
5241
5242/*
5243 * Translate a history character to the associated type number.
5244 */
5245 static int
5246hist_char2type(c)
5247 int c;
5248{
5249 if (c == ':')
5250 return HIST_CMD;
5251 if (c == '=')
5252 return HIST_EXPR;
5253 if (c == '@')
5254 return HIST_INPUT;
5255 if (c == '>')
5256 return HIST_DEBUG;
5257 return HIST_SEARCH; /* must be '?' or '/' */
5258}
5259
5260/*
5261 * Table of history names.
5262 * These names are used in :history and various hist...() functions.
5263 * It is sufficient to give the significant prefix of a history name.
5264 */
5265
5266static char *(history_names[]) =
5267{
5268 "cmd",
5269 "search",
5270 "expr",
5271 "input",
5272 "debug",
5273 NULL
5274};
5275
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005276#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5277/*
5278 * Function given to ExpandGeneric() to obtain the possible first
5279 * arguments of the ":history command.
5280 */
5281 static char_u *
5282get_history_arg(xp, idx)
5283 expand_T *xp UNUSED;
5284 int idx;
5285{
5286 static char_u compl[2] = { NUL, NUL };
5287 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005288 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005289 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5290
5291 if (idx < short_names_count)
5292 {
5293 compl[0] = (char_u)short_names[idx];
5294 return compl;
5295 }
5296 if (idx < short_names_count + history_name_count)
5297 return (char_u *)history_names[idx - short_names_count];
5298 if (idx == short_names_count + history_name_count)
5299 return (char_u *)"all";
5300 return NULL;
5301}
5302#endif
5303
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304/*
5305 * init_history() - Initialize the command line history.
5306 * Also used to re-allocate the history when the size changes.
5307 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005308 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309init_history()
5310{
5311 int newlen; /* new length of history table */
5312 histentry_T *temp;
5313 int i;
5314 int j;
5315 int type;
5316
5317 /*
5318 * If size of history table changed, reallocate it
5319 */
5320 newlen = (int)p_hi;
5321 if (newlen != hislen) /* history length changed */
5322 {
5323 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5324 {
5325 if (newlen)
5326 {
5327 temp = (histentry_T *)lalloc(
5328 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5329 if (temp == NULL) /* out of memory! */
5330 {
5331 if (type == 0) /* first one: just keep the old length */
5332 {
5333 newlen = hislen;
5334 break;
5335 }
5336 /* Already changed one table, now we can only have zero
5337 * length for all tables. */
5338 newlen = 0;
5339 type = -1;
5340 continue;
5341 }
5342 }
5343 else
5344 temp = NULL;
5345 if (newlen == 0 || temp != NULL)
5346 {
5347 if (hisidx[type] < 0) /* there are no entries yet */
5348 {
5349 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005350 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 }
5352 else if (newlen > hislen) /* array becomes bigger */
5353 {
5354 for (i = 0; i <= hisidx[type]; ++i)
5355 temp[i] = history[type][i];
5356 j = i;
5357 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005358 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 for ( ; j < hislen; ++i, ++j)
5360 temp[i] = history[type][j];
5361 }
5362 else /* array becomes smaller or 0 */
5363 {
5364 j = hisidx[type];
5365 for (i = newlen - 1; ; --i)
5366 {
5367 if (i >= 0) /* copy newest entries */
5368 temp[i] = history[type][j];
5369 else /* remove older entries */
5370 vim_free(history[type][j].hisstr);
5371 if (--j < 0)
5372 j = hislen - 1;
5373 if (j == hisidx[type])
5374 break;
5375 }
5376 hisidx[type] = newlen - 1;
5377 }
5378 vim_free(history[type]);
5379 history[type] = temp;
5380 }
5381 }
5382 hislen = newlen;
5383 }
5384}
5385
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005386 static void
5387clear_hist_entry(hisptr)
5388 histentry_T *hisptr;
5389{
5390 hisptr->hisnum = 0;
5391 hisptr->viminfo = FALSE;
5392 hisptr->hisstr = NULL;
5393}
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395/*
5396 * Check if command line 'str' is already in history.
5397 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5398 */
5399 static int
Bram Moolenaar07219f92013-04-14 23:19:36 +02005400in_history(type, str, move_to_front, sep, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 int type;
5402 char_u *str;
5403 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005404 int sep;
Bram Moolenaar07219f92013-04-14 23:19:36 +02005405 int writing; /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 int i;
5408 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005409 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 if (hisidx[type] < 0)
5412 return FALSE;
5413 i = hisidx[type];
5414 do
5415 {
5416 if (history[type][i].hisstr == NULL)
5417 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005418
5419 /* For search history, check that the separator character matches as
5420 * well. */
5421 p = history[type][i].hisstr;
5422 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005423 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005424 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
5426 if (!move_to_front)
5427 return TRUE;
5428 last_i = i;
5429 break;
5430 }
5431 if (--i < 0)
5432 i = hislen - 1;
5433 } while (i != hisidx[type]);
5434
5435 if (last_i >= 0)
5436 {
5437 str = history[type][i].hisstr;
5438 while (i != hisidx[type])
5439 {
5440 if (++i >= hislen)
5441 i = 0;
5442 history[type][last_i] = history[type][i];
5443 last_i = i;
5444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005446 history[type][i].viminfo = FALSE;
5447 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 return TRUE;
5449 }
5450 return FALSE;
5451}
5452
5453/*
5454 * Convert history name (from table above) to its HIST_ equivalent.
5455 * When "name" is empty, return "cmd" history.
5456 * Returns -1 for unknown history name.
5457 */
5458 int
5459get_histtype(name)
5460 char_u *name;
5461{
5462 int i;
5463 int len = (int)STRLEN(name);
5464
5465 /* No argument: use current history. */
5466 if (len == 0)
5467 return hist_char2type(ccline.cmdfirstc);
5468
5469 for (i = 0; history_names[i] != NULL; ++i)
5470 if (STRNICMP(name, history_names[i], len) == 0)
5471 return i;
5472
5473 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5474 return hist_char2type(name[0]);
5475
5476 return -1;
5477}
5478
5479static int last_maptick = -1; /* last seen maptick */
5480
5481/*
5482 * Add the given string to the given history. If the string is already in the
5483 * history then it is moved to the front. "histype" may be one of he HIST_
5484 * values.
5485 */
5486 void
5487add_to_history(histype, new_entry, in_map, sep)
5488 int histype;
5489 char_u *new_entry;
5490 int in_map; /* consider maptick when inside a mapping */
5491 int sep; /* separator character used (search hist) */
5492{
5493 histentry_T *hisptr;
5494 int len;
5495
5496 if (hislen == 0) /* no history */
5497 return;
5498
5499 /*
5500 * Searches inside the same mapping overwrite each other, so that only
5501 * the last line is kept. Be careful not to remove a line that was moved
5502 * down, only lines that were added.
5503 */
5504 if (histype == HIST_SEARCH && in_map)
5505 {
5506 if (maptick == last_maptick)
5507 {
5508 /* Current line is from the same mapping, remove it */
5509 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5510 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005511 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 --hisnum[histype];
5513 if (--hisidx[HIST_SEARCH] < 0)
5514 hisidx[HIST_SEARCH] = hislen - 1;
5515 }
5516 last_maptick = -1;
5517 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005518 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005519 {
5520 if (++hisidx[histype] == hislen)
5521 hisidx[histype] = 0;
5522 hisptr = &history[histype][hisidx[histype]];
5523 vim_free(hisptr->hisstr);
5524
5525 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005526 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5528 if (hisptr->hisstr != NULL)
5529 hisptr->hisstr[len + 1] = sep;
5530
5531 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005532 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 if (histype == HIST_SEARCH && in_map)
5534 last_maptick = maptick;
5535 }
5536}
5537
5538#if defined(FEAT_EVAL) || defined(PROTO)
5539
5540/*
5541 * Get identifier of newest history entry.
5542 * "histype" may be one of the HIST_ values.
5543 */
5544 int
5545get_history_idx(histype)
5546 int histype;
5547{
5548 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5549 || hisidx[histype] < 0)
5550 return -1;
5551
5552 return history[histype][hisidx[histype]].hisnum;
5553}
5554
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005555static struct cmdline_info *get_ccline_ptr __ARGS((void));
5556
5557/*
5558 * Get pointer to the command line info to use. cmdline_paste() may clear
5559 * ccline and put the previous value in prev_ccline.
5560 */
5561 static struct cmdline_info *
5562get_ccline_ptr()
5563{
5564 if ((State & CMDLINE) == 0)
5565 return NULL;
5566 if (ccline.cmdbuff != NULL)
5567 return &ccline;
5568 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5569 return &prev_ccline;
5570 return NULL;
5571}
5572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573/*
5574 * Get the current command line in allocated memory.
5575 * Only works when the command line is being edited.
5576 * Returns NULL when something is wrong.
5577 */
5578 char_u *
5579get_cmdline_str()
5580{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005581 struct cmdline_info *p = get_ccline_ptr();
5582
5583 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005585 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586}
5587
5588/*
5589 * Get the current command line position, counted in bytes.
5590 * Zero is the first position.
5591 * Only works when the command line is being edited.
5592 * Returns -1 when something is wrong.
5593 */
5594 int
5595get_cmdline_pos()
5596{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005597 struct cmdline_info *p = get_ccline_ptr();
5598
5599 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005601 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602}
5603
5604/*
5605 * Set the command line byte position to "pos". Zero is the first position.
5606 * Only works when the command line is being edited.
5607 * Returns 1 when failed, 0 when OK.
5608 */
5609 int
5610set_cmdline_pos(pos)
5611 int pos;
5612{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005613 struct cmdline_info *p = get_ccline_ptr();
5614
5615 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 return 1;
5617
5618 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5619 * changed the command line. */
5620 if (pos < 0)
5621 new_cmdpos = 0;
5622 else
5623 new_cmdpos = pos;
5624 return 0;
5625}
5626
5627/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005628 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005629 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005630 * Only works when the command line is being edited.
5631 * Returns NUL when something is wrong.
5632 */
5633 int
5634get_cmdline_type()
5635{
5636 struct cmdline_info *p = get_ccline_ptr();
5637
5638 if (p == NULL)
5639 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005640 if (p->cmdfirstc == NUL)
5641 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005642 return p->cmdfirstc;
5643}
5644
5645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 * Calculate history index from a number:
5647 * num > 0: seen as identifying number of a history entry
5648 * num < 0: relative position in history wrt newest entry
5649 * "histype" may be one of the HIST_ values.
5650 */
5651 static int
5652calc_hist_idx(histype, num)
5653 int histype;
5654 int num;
5655{
5656 int i;
5657 histentry_T *hist;
5658 int wrapped = FALSE;
5659
5660 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5661 || (i = hisidx[histype]) < 0 || num == 0)
5662 return -1;
5663
5664 hist = history[histype];
5665 if (num > 0)
5666 {
5667 while (hist[i].hisnum > num)
5668 if (--i < 0)
5669 {
5670 if (wrapped)
5671 break;
5672 i += hislen;
5673 wrapped = TRUE;
5674 }
5675 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5676 return i;
5677 }
5678 else if (-num <= hislen)
5679 {
5680 i += num + 1;
5681 if (i < 0)
5682 i += hislen;
5683 if (hist[i].hisstr != NULL)
5684 return i;
5685 }
5686 return -1;
5687}
5688
5689/*
5690 * Get a history entry by its index.
5691 * "histype" may be one of the HIST_ values.
5692 */
5693 char_u *
5694get_history_entry(histype, idx)
5695 int histype;
5696 int idx;
5697{
5698 idx = calc_hist_idx(histype, idx);
5699 if (idx >= 0)
5700 return history[histype][idx].hisstr;
5701 else
5702 return (char_u *)"";
5703}
5704
5705/*
5706 * Clear all entries of a history.
5707 * "histype" may be one of the HIST_ values.
5708 */
5709 int
5710clr_history(histype)
5711 int histype;
5712{
5713 int i;
5714 histentry_T *hisptr;
5715
5716 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5717 {
5718 hisptr = history[histype];
5719 for (i = hislen; i--;)
5720 {
5721 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005722 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
5724 hisidx[histype] = -1; /* mark history as cleared */
5725 hisnum[histype] = 0; /* reset identifier counter */
5726 return OK;
5727 }
5728 return FAIL;
5729}
5730
5731/*
5732 * Remove all entries matching {str} from a history.
5733 * "histype" may be one of the HIST_ values.
5734 */
5735 int
5736del_history_entry(histype, str)
5737 int histype;
5738 char_u *str;
5739{
5740 regmatch_T regmatch;
5741 histentry_T *hisptr;
5742 int idx;
5743 int i;
5744 int last;
5745 int found = FALSE;
5746
5747 regmatch.regprog = NULL;
5748 regmatch.rm_ic = FALSE; /* always match case */
5749 if (hislen != 0
5750 && histype >= 0
5751 && histype < HIST_COUNT
5752 && *str != NUL
5753 && (idx = hisidx[histype]) >= 0
5754 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5755 != NULL)
5756 {
5757 i = last = idx;
5758 do
5759 {
5760 hisptr = &history[histype][i];
5761 if (hisptr->hisstr == NULL)
5762 break;
5763 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5764 {
5765 found = TRUE;
5766 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005767 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 }
5769 else
5770 {
5771 if (i != last)
5772 {
5773 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005774 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 }
5776 if (--last < 0)
5777 last += hislen;
5778 }
5779 if (--i < 0)
5780 i += hislen;
5781 } while (i != idx);
5782 if (history[histype][idx].hisstr == NULL)
5783 hisidx[histype] = -1;
5784 }
5785 vim_free(regmatch.regprog);
5786 return found;
5787}
5788
5789/*
5790 * Remove an indexed entry from a history.
5791 * "histype" may be one of the HIST_ values.
5792 */
5793 int
5794del_history_idx(histype, idx)
5795 int histype;
5796 int idx;
5797{
5798 int i, j;
5799
5800 i = calc_hist_idx(histype, idx);
5801 if (i < 0)
5802 return FALSE;
5803 idx = hisidx[histype];
5804 vim_free(history[histype][i].hisstr);
5805
5806 /* When deleting the last added search string in a mapping, reset
5807 * last_maptick, so that the last added search string isn't deleted again.
5808 */
5809 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5810 last_maptick = -1;
5811
5812 while (i != idx)
5813 {
5814 j = (i + 1) % hislen;
5815 history[histype][i] = history[histype][j];
5816 i = j;
5817 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005818 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 if (--i < 0)
5820 i += hislen;
5821 hisidx[histype] = i;
5822 return TRUE;
5823}
5824
5825#endif /* FEAT_EVAL */
5826
5827#if defined(FEAT_CRYPT) || defined(PROTO)
5828/*
5829 * Very specific function to remove the value in ":set key=val" from the
5830 * history.
5831 */
5832 void
5833remove_key_from_history()
5834{
5835 char_u *p;
5836 int i;
5837
5838 i = hisidx[HIST_CMD];
5839 if (i < 0)
5840 return;
5841 p = history[HIST_CMD][i].hisstr;
5842 if (p != NULL)
5843 for ( ; *p; ++p)
5844 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5845 {
5846 p = vim_strchr(p + 3, '=');
5847 if (p == NULL)
5848 break;
5849 ++p;
5850 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5851 if (p[i] == '\\' && p[i + 1])
5852 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005853 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 --p;
5855 }
5856}
5857#endif
5858
5859#endif /* FEAT_CMDHIST */
5860
5861#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5862/*
5863 * Get indices "num1,num2" that specify a range within a list (not a range of
5864 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5865 * Returns OK if parsed successfully, otherwise FAIL.
5866 */
5867 int
5868get_list_range(str, num1, num2)
5869 char_u **str;
5870 int *num1;
5871 int *num2;
5872{
5873 int len;
5874 int first = FALSE;
5875 long num;
5876
5877 *str = skipwhite(*str);
5878 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5879 {
5880 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5881 *str += len;
5882 *num1 = (int)num;
5883 first = TRUE;
5884 }
5885 *str = skipwhite(*str);
5886 if (**str == ',') /* parse "to" part of range */
5887 {
5888 *str = skipwhite(*str + 1);
5889 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5890 if (len > 0)
5891 {
5892 *num2 = (int)num;
5893 *str = skipwhite(*str + len);
5894 }
5895 else if (!first) /* no number given at all */
5896 return FAIL;
5897 }
5898 else if (first) /* only one number given */
5899 *num2 = *num1;
5900 return OK;
5901}
5902#endif
5903
5904#if defined(FEAT_CMDHIST) || defined(PROTO)
5905/*
5906 * :history command - print a history
5907 */
5908 void
5909ex_history(eap)
5910 exarg_T *eap;
5911{
5912 histentry_T *hist;
5913 int histype1 = HIST_CMD;
5914 int histype2 = HIST_CMD;
5915 int hisidx1 = 1;
5916 int hisidx2 = -1;
5917 int idx;
5918 int i, j, k;
5919 char_u *end;
5920 char_u *arg = eap->arg;
5921
5922 if (hislen == 0)
5923 {
5924 MSG(_("'history' option is zero"));
5925 return;
5926 }
5927
5928 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5929 {
5930 end = arg;
5931 while (ASCII_ISALPHA(*end)
5932 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5933 end++;
5934 i = *end;
5935 *end = NUL;
5936 histype1 = get_histtype(arg);
5937 if (histype1 == -1)
5938 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005939 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 {
5941 histype1 = 0;
5942 histype2 = HIST_COUNT-1;
5943 }
5944 else
5945 {
5946 *end = i;
5947 EMSG(_(e_trailing));
5948 return;
5949 }
5950 }
5951 else
5952 histype2 = histype1;
5953 *end = i;
5954 }
5955 else
5956 end = arg;
5957 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5958 {
5959 EMSG(_(e_trailing));
5960 return;
5961 }
5962
5963 for (; !got_int && histype1 <= histype2; ++histype1)
5964 {
5965 STRCPY(IObuff, "\n # ");
5966 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5967 MSG_PUTS_TITLE(IObuff);
5968 idx = hisidx[histype1];
5969 hist = history[histype1];
5970 j = hisidx1;
5971 k = hisidx2;
5972 if (j < 0)
5973 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5974 if (k < 0)
5975 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5976 if (idx >= 0 && j <= k)
5977 for (i = idx + 1; !got_int; ++i)
5978 {
5979 if (i == hislen)
5980 i = 0;
5981 if (hist[i].hisstr != NULL
5982 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5983 {
5984 msg_putchar('\n');
5985 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5986 hist[i].hisnum);
5987 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5988 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01005989 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 else
5991 STRCAT(IObuff, hist[i].hisstr);
5992 msg_outtrans(IObuff);
5993 out_flush();
5994 }
5995 if (i == idx)
5996 break;
5997 }
5998 }
5999}
6000#endif
6001
6002#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
6003static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6004static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6005static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6006static int viminfo_add_at_front = FALSE;
6007
6008static int hist_type2char __ARGS((int type, int use_question));
6009
6010/*
6011 * Translate a history type number to the associated character.
6012 */
6013 static int
6014hist_type2char(type, use_question)
6015 int type;
6016 int use_question; /* use '?' instead of '/' */
6017{
6018 if (type == HIST_CMD)
6019 return ':';
6020 if (type == HIST_SEARCH)
6021 {
6022 if (use_question)
6023 return '?';
6024 else
6025 return '/';
6026 }
6027 if (type == HIST_EXPR)
6028 return '=';
6029 return '@';
6030}
6031
6032/*
6033 * Prepare for reading the history from the viminfo file.
6034 * This allocates history arrays to store the read history lines.
6035 */
6036 void
Bram Moolenaar07219f92013-04-14 23:19:36 +02006037prepare_viminfo_history(asklen, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 int asklen;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006039 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040{
6041 int i;
6042 int num;
6043 int type;
6044 int len;
6045
6046 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006047 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 if (asklen > hislen)
6049 asklen = hislen;
6050
6051 for (type = 0; type < HIST_COUNT; ++type)
6052 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006053 /* Count the number of empty spaces in the history list. Entries read
6054 * from viminfo previously are also considered empty. If there are
6055 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006057 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 num++;
6059 len = asklen;
6060 if (num > len)
6061 len = num;
6062 if (len <= 0)
6063 viminfo_history[type] = NULL;
6064 else
6065 viminfo_history[type] =
6066 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6067 if (viminfo_history[type] == NULL)
6068 len = 0;
6069 viminfo_hislen[type] = len;
6070 viminfo_hisidx[type] = 0;
6071 }
6072}
6073
6074/*
6075 * Accept a line from the viminfo, store it in the history array when it's
6076 * new.
6077 */
6078 int
Bram Moolenaar07219f92013-04-14 23:19:36 +02006079read_viminfo_history(virp, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 vir_T *virp;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006081 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 int type;
6084 long_u len;
6085 char_u *val;
6086 char_u *p;
6087
6088 type = hist_char2type(virp->vir_line[0]);
6089 if (viminfo_hisidx[type] < viminfo_hislen[type])
6090 {
6091 val = viminfo_readstring(virp, 1, TRUE);
6092 if (val != NULL && *val != NUL)
6093 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006094 int sep = (*val == ' ' ? NUL : *val);
6095
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006097 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 {
6099 /* Need to re-allocate to append the separator byte. */
6100 len = STRLEN(val);
6101 p = lalloc(len + 2, TRUE);
6102 if (p != NULL)
6103 {
6104 if (type == HIST_SEARCH)
6105 {
6106 /* Search entry: Move the separator from the first
6107 * column to after the NUL. */
6108 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006109 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 }
6111 else
6112 {
6113 /* Not a search entry: No separator in the viminfo
6114 * file, add a NUL separator. */
6115 mch_memmove(p, val, (size_t)len + 1);
6116 p[len + 1] = NUL;
6117 }
6118 viminfo_history[type][viminfo_hisidx[type]++] = p;
6119 }
6120 }
6121 }
6122 vim_free(val);
6123 }
6124 return viminfo_readline(virp);
6125}
6126
Bram Moolenaar07219f92013-04-14 23:19:36 +02006127/*
6128 * Finish reading history lines from viminfo. Not used when writing viminfo.
6129 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 void
6131finish_viminfo_history()
6132{
6133 int idx;
6134 int i;
6135 int type;
6136
6137 for (type = 0; type < HIST_COUNT; ++type)
6138 {
6139 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006140 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 idx = hisidx[type] + viminfo_hisidx[type];
6142 if (idx >= hislen)
6143 idx -= hislen;
6144 else if (idx < 0)
6145 idx = hislen - 1;
6146 if (viminfo_add_at_front)
6147 hisidx[type] = idx;
6148 else
6149 {
6150 if (hisidx[type] == -1)
6151 hisidx[type] = hislen - 1;
6152 do
6153 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006154 if (history[type][idx].hisstr != NULL
6155 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 break;
6157 if (++idx == hislen)
6158 idx = 0;
6159 } while (idx != hisidx[type]);
6160 if (idx != hisidx[type] && --idx < 0)
6161 idx = hislen - 1;
6162 }
6163 for (i = 0; i < viminfo_hisidx[type]; i++)
6164 {
6165 vim_free(history[type][idx].hisstr);
6166 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006167 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 if (--idx < 0)
6169 idx = hislen - 1;
6170 }
6171 idx += 1;
6172 idx %= hislen;
6173 for (i = 0; i < viminfo_hisidx[type]; i++)
6174 {
6175 history[type][idx++].hisnum = ++hisnum[type];
6176 idx %= hislen;
6177 }
6178 vim_free(viminfo_history[type]);
6179 viminfo_history[type] = NULL;
6180 }
6181}
6182
6183 void
6184write_viminfo_history(fp)
6185 FILE *fp;
6186{
6187 int i;
6188 int type;
6189 int num_saved;
6190 char_u *p;
6191 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006192 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193
6194 init_history();
6195 if (hislen == 0)
6196 return;
6197 for (type = 0; type < HIST_COUNT; ++type)
6198 {
6199 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6200 if (num_saved == 0)
6201 continue;
6202 if (num_saved < 0) /* Use default */
6203 num_saved = hislen;
6204 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6205 type == HIST_CMD ? _("Command Line") :
6206 type == HIST_SEARCH ? _("Search String") :
6207 type == HIST_EXPR ? _("Expression") :
6208 _("Input Line"));
6209 if (num_saved > hislen)
6210 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006211
6212 /*
6213 * Merge typed and viminfo history:
6214 * round 1: history of typed commands.
6215 * round 2: history from recently read viminfo.
6216 */
6217 for (round = 1; round <= 2; ++round)
6218 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006219 if (round == 1)
6220 /* start at newest entry, somewhere in the list */
6221 i = hisidx[type];
6222 else if (viminfo_hisidx[type] > 0)
6223 /* start at newest entry, first in the list */
6224 i = 0;
6225 else
6226 /* empty list */
6227 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006228 if (i >= 0)
6229 while (num_saved > 0
6230 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006232 p = round == 1 ? history[type][i].hisstr
6233 : viminfo_history[type][i];
Bram Moolenaar07219f92013-04-14 23:19:36 +02006234 if (p != NULL && (round == 2 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006236 --num_saved;
6237 fputc(hist_type2char(type, TRUE), fp);
6238 /* For the search history: put the separator in the
6239 * second column; use a space if there isn't one. */
6240 if (type == HIST_SEARCH)
6241 {
6242 c = p[STRLEN(p) + 1];
6243 putc(c == NUL ? ' ' : c, fp);
6244 }
6245 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006247 if (round == 1)
6248 {
6249 /* Decrement index, loop around and stop when back at
6250 * the start. */
6251 if (--i < 0)
6252 i = hislen - 1;
6253 if (i == hisidx[type])
6254 break;
6255 }
6256 else
6257 {
6258 /* Increment index. Stop at the end in the while. */
6259 ++i;
6260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006262 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006263 for (i = 0; i < viminfo_hisidx[type]; ++i)
6264 vim_free(viminfo_history[type][i]);
6265 vim_free(viminfo_history[type]);
6266 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006267 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 }
6269}
6270#endif /* FEAT_VIMINFO */
6271
6272#if defined(FEAT_FKMAP) || defined(PROTO)
6273/*
6274 * Write a character at the current cursor+offset position.
6275 * It is directly written into the command buffer block.
6276 */
6277 void
6278cmd_pchar(c, offset)
6279 int c, offset;
6280{
6281 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6282 {
6283 EMSG(_("E198: cmd_pchar beyond the command length"));
6284 return;
6285 }
6286 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6287 ccline.cmdbuff[ccline.cmdlen] = NUL;
6288}
6289
6290 int
6291cmd_gchar(offset)
6292 int offset;
6293{
6294 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6295 {
6296 /* EMSG(_("cmd_gchar beyond the command length")); */
6297 return NUL;
6298 }
6299 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6300}
6301#endif
6302
6303#if defined(FEAT_CMDWIN) || defined(PROTO)
6304/*
6305 * Open a window on the current command line and history. Allow editing in
6306 * the window. Returns when the window is closed.
6307 * Returns:
6308 * CR if the command is to be executed
6309 * Ctrl_C if it is to be abandoned
6310 * K_IGNORE if editing continues
6311 */
6312 static int
6313ex_window()
6314{
6315 struct cmdline_info save_ccline;
6316 buf_T *old_curbuf = curbuf;
6317 win_T *old_curwin = curwin;
6318 buf_T *bp;
6319 win_T *wp;
6320 int i;
6321 linenr_T lnum;
6322 int histtype;
6323 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006324#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 int save_restart_edit = restart_edit;
6328 int save_State = State;
6329 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006330#ifdef FEAT_RIGHTLEFT
6331 int save_cmdmsg_rl = cmdmsg_rl;
6332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333
6334 /* Can't do this recursively. Can't do it when typing a password. */
6335 if (cmdwin_type != 0
6336# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6337 || cmdline_star > 0
6338# endif
6339 )
6340 {
6341 beep_flush();
6342 return K_IGNORE;
6343 }
6344
6345 /* Save current window sizes. */
6346 win_size_save(&winsizes);
6347
6348# ifdef FEAT_AUTOCMD
6349 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006350 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006352 /* don't use a new tab page */
6353 cmdmod.tab = 0;
6354
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355 /* Create a window for the command-line buffer. */
6356 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6357 {
6358 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006359# ifdef FEAT_AUTOCMD
6360 unblock_autocmds();
6361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 return K_IGNORE;
6363 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006364 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365
6366 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006367 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006368 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6370 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6371 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006372#ifdef FEAT_FOLDING
6373 curwin->w_p_fen = FALSE;
6374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006376 curwin->w_p_rl = cmdmsg_rl;
6377 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006379 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381# ifdef FEAT_AUTOCMD
6382 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006383 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384# endif
6385
Bram Moolenaar46152342005-09-07 21:18:43 +00006386 /* Showing the prompt may have set need_wait_return, reset it. */
6387 need_wait_return = FALSE;
6388
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006389 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6391 {
6392 if (p_wc == TAB)
6393 {
6394 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6395 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6396 }
6397 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6398 }
6399
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006400 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6401 * sets 'textwidth' to 78). */
6402 curbuf->b_p_tw = 0;
6403
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 /* Fill the buffer with the history. */
6405 init_history();
6406 if (hislen > 0)
6407 {
6408 i = hisidx[histtype];
6409 if (i >= 0)
6410 {
6411 lnum = 0;
6412 do
6413 {
6414 if (++i == hislen)
6415 i = 0;
6416 if (history[histtype][i].hisstr != NULL)
6417 ml_append(lnum++, history[histtype][i].hisstr,
6418 (colnr_T)0, FALSE);
6419 }
6420 while (i != hisidx[histtype]);
6421 }
6422 }
6423
6424 /* Replace the empty last line with the current command-line and put the
6425 * cursor there. */
6426 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6427 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6428 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006429 changed_line_abv_curs();
6430 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006431 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 /* Save the command line info, can be used recursively. */
6434 save_ccline = ccline;
6435 ccline.cmdbuff = NULL;
6436 ccline.cmdprompt = NULL;
6437
6438 /* No Ex mode here! */
6439 exmode_active = 0;
6440
6441 State = NORMAL;
6442# ifdef FEAT_MOUSE
6443 setmouse();
6444# endif
6445
6446# ifdef FEAT_AUTOCMD
6447 /* Trigger CmdwinEnter autocommands. */
6448 typestr[0] = cmdwin_type;
6449 typestr[1] = NUL;
6450 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006451 if (restart_edit != 0) /* autocmd with ":startinsert" */
6452 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453# endif
6454
6455 i = RedrawingDisabled;
6456 RedrawingDisabled = 0;
6457
6458 /*
6459 * Call the main loop until <CR> or CTRL-C is typed.
6460 */
6461 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006462 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 RedrawingDisabled = i;
6465
6466# ifdef FEAT_AUTOCMD
6467 /* Trigger CmdwinLeave autocommands. */
6468 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6469# endif
6470
Bram Moolenaarccc18222007-05-10 18:25:20 +00006471 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 ccline = save_ccline;
6473 cmdwin_type = 0;
6474
6475 exmode_active = save_exmode;
6476
Bram Moolenaarf9821062008-06-20 16:31:07 +00006477 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 * this happens! */
6479 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6480 {
6481 cmdwin_result = Ctrl_C;
6482 EMSG(_("E199: Active window or buffer deleted"));
6483 }
6484 else
6485 {
6486# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6487 /* autocmds may abort script processing */
6488 if (aborting() && cmdwin_result != K_IGNORE)
6489 cmdwin_result = Ctrl_C;
6490# endif
6491 /* Set the new command line from the cmdline buffer. */
6492 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006493 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006495 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6496
6497 if (histtype == HIST_CMD)
6498 {
6499 /* Execute the command directly. */
6500 ccline.cmdbuff = vim_strsave((char_u *)p);
6501 cmdwin_result = CAR;
6502 }
6503 else
6504 {
6505 /* First need to cancel what we were doing. */
6506 ccline.cmdbuff = NULL;
6507 stuffcharReadbuff(':');
6508 stuffReadbuff((char_u *)p);
6509 stuffcharReadbuff(CAR);
6510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 }
6512 else if (cmdwin_result == K_XF2) /* :qa typed */
6513 {
6514 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6515 cmdwin_result = CAR;
6516 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006517 else if (cmdwin_result == Ctrl_C)
6518 {
6519 /* :q or :close, don't execute any command
6520 * and don't modify the cmd window. */
6521 ccline.cmdbuff = NULL;
6522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 else
6524 ccline.cmdbuff = vim_strsave(ml_get_curline());
6525 if (ccline.cmdbuff == NULL)
6526 cmdwin_result = Ctrl_C;
6527 else
6528 {
6529 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6530 ccline.cmdbufflen = ccline.cmdlen + 1;
6531 ccline.cmdpos = curwin->w_cursor.col;
6532 if (ccline.cmdpos > ccline.cmdlen)
6533 ccline.cmdpos = ccline.cmdlen;
6534 if (cmdwin_result == K_IGNORE)
6535 {
6536 set_cmdspos_cursor();
6537 redrawcmd();
6538 }
6539 }
6540
6541# ifdef FEAT_AUTOCMD
6542 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006543 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544# endif
6545 wp = curwin;
6546 bp = curbuf;
6547 win_goto(old_curwin);
6548 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006549
6550 /* win_close() may have already wiped the buffer when 'bh' is
6551 * set to 'wipe' */
6552 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006553 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554
6555 /* Restore window sizes. */
6556 win_size_restore(&winsizes);
6557
6558# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006559 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560# endif
6561 }
6562
6563 ga_clear(&winsizes);
6564 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006565# ifdef FEAT_RIGHTLEFT
6566 cmdmsg_rl = save_cmdmsg_rl;
6567# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 State = save_State;
6570# ifdef FEAT_MOUSE
6571 setmouse();
6572# endif
6573
6574 return cmdwin_result;
6575}
6576#endif /* FEAT_CMDWIN */
6577
6578/*
6579 * Used for commands that either take a simple command string argument, or:
6580 * cmd << endmarker
6581 * {script}
6582 * endmarker
6583 * Returns a pointer to allocated memory with {script} or NULL.
6584 */
6585 char_u *
6586script_get(eap, cmd)
6587 exarg_T *eap;
6588 char_u *cmd;
6589{
6590 char_u *theline;
6591 char *end_pattern = NULL;
6592 char dot[] = ".";
6593 garray_T ga;
6594
6595 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6596 return NULL;
6597
6598 ga_init2(&ga, 1, 0x400);
6599
6600 if (cmd[2] != NUL)
6601 end_pattern = (char *)skipwhite(cmd + 2);
6602 else
6603 end_pattern = dot;
6604
6605 for (;;)
6606 {
6607 theline = eap->getline(
6608#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006609 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610#endif
6611 NUL, eap->cookie, 0);
6612
6613 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006614 {
6615 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618
6619 ga_concat(&ga, theline);
6620 ga_append(&ga, '\n');
6621 vim_free(theline);
6622 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006623 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624
6625 return (char_u *)ga.ga_data;
6626}