blob: 2678184bcabeb42500c6658c60b07f0a95bf1842 [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
Bram Moolenaarede3e632013-06-23 16:16:19 +02001703 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#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
Bram Moolenaarede3e632013-06-23 16:16:19 +02001709 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 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 Moolenaar2d54ec92014-06-12 19:44:48 +02002191 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192
2193 /* Switch cursor on now. This avoids that it happens after the "\n", which
2194 * confuses the system function that computes tabstops. */
2195 cursor_on();
2196
2197 /* always start in column 0; write a newline if necessary */
2198 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002199 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002203 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002204 if (p_prompt)
2205 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 while (indent-- > 0)
2207 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 }
2210
2211 ga_init2(&line_ga, 1, 30);
2212
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002213 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002214 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002215 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002216 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002217 while (indent >= 8)
2218 {
2219 ga_append(&line_ga, TAB);
2220 msg_puts((char_u *)" ");
2221 indent -= 8;
2222 }
2223 while (indent-- > 0)
2224 {
2225 ga_append(&line_ga, ' ');
2226 msg_putchar(' ');
2227 }
2228 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002229 ++no_mapping;
2230 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002231
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 /*
2233 * Get the line, one character at a time.
2234 */
2235 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002236 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
2238 if (ga_grow(&line_ga, 40) == FAIL)
2239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002241 /* Get one character at a time. Don't use inchar(), it can't handle
2242 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002243 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002244 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002247 * Handle line editing.
2248 * Previously this was left to the system, putting the terminal in
2249 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002251 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002253 msg_putchar('\n');
2254 break;
2255 }
2256
2257 if (!escaped)
2258 {
2259 /* CR typed means "enter", which is NL */
2260 if (c1 == '\r')
2261 c1 = '\n';
2262
2263 if (c1 == BS || c1 == K_BS
2264 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002266 if (line_ga.ga_len > 0)
2267 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002268#ifdef FEAT_MBYTE
2269 if (has_mbyte)
2270 {
2271 p = (char_u *)line_ga.ga_data;
2272 p[line_ga.ga_len] = NUL;
2273 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2274 line_ga.ga_len -= len;
2275 }
2276 else
2277#endif
2278 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002279 goto redraw;
2280 }
2281 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 }
2283
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002286 msg_col = startcol;
2287 msg_clr_eos();
2288 line_ga.ga_len = 0;
2289 continue;
2290 }
2291
2292 if (c1 == Ctrl_T)
2293 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002294 long sw = get_sw_value(curbuf);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002295
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002296 p = (char_u *)line_ga.ga_data;
2297 p[line_ga.ga_len] = NUL;
2298 indent = get_indent_str(p, 8);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002299 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002300add_indent:
2301 while (get_indent_str(p, 8) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002303 char_u *s = skipwhite(p);
2304
2305 ga_grow(&line_ga, 1);
2306 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2307 *s = ' ';
2308 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310redraw:
2311 /* redraw the line */
2312 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002314 p = (char_u *)line_ga.ga_data;
2315 p[line_ga.ga_len] = NUL;
2316 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002318 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002320 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 msg_putchar(' ');
2323 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002324 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002328 len = MB_PTR2LEN(p);
2329 msg_outtrans_len(p, len);
2330 vcol += ptr2cells(p);
2331 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 }
2333 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002334 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002335 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002336 continue;
2337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002339 if (c1 == Ctrl_D)
2340 {
2341 /* Delete one shiftwidth. */
2342 p = (char_u *)line_ga.ga_data;
2343 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002345 if (prev_char == '^')
2346 ex_keep_indent = TRUE;
2347 indent = 0;
2348 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 }
2350 else
2351 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002352 p[line_ga.ga_len] = NUL;
2353 indent = get_indent_str(p, 8);
2354 --indent;
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01002355 indent -= indent % get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002357 while (get_indent_str(p, 8) > indent)
2358 {
2359 char_u *s = skipwhite(p);
2360
2361 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2362 --line_ga.ga_len;
2363 }
2364 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002366
2367 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2368 {
2369 escaped = TRUE;
2370 continue;
2371 }
2372
2373 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2374 if (IS_SPECIAL(c1))
2375 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002377
2378 if (IS_SPECIAL(c1))
2379 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002380#ifdef FEAT_MBYTE
2381 if (has_mbyte)
2382 len = (*mb_char2bytes)(c1,
2383 (char_u *)line_ga.ga_data + line_ga.ga_len);
2384 else
2385#endif
2386 {
2387 len = 1;
2388 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2389 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002390 if (c1 == '\n')
2391 msg_putchar('\n');
2392 else if (c1 == TAB)
2393 {
2394 /* Don't use chartabsize(), 'ts' can be different */
2395 do
2396 {
2397 msg_putchar(' ');
2398 } while (++vcol % 8);
2399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002403 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002404 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002406 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 escaped = FALSE;
2408
2409 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002410 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002411
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002412 /* We are done when a NL is entered, but not when it comes after an
2413 * odd number of backslashes, that results in a NUL. */
2414 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002416 int bcount = 0;
2417
2418 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2419 ++bcount;
2420
2421 if (bcount > 0)
2422 {
2423 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2424 * "\NL", etc. */
2425 line_ga.ga_len -= (bcount + 1) / 2;
2426 pend -= (bcount + 1) / 2;
2427 pend[-1] = '\n';
2428 }
2429
2430 if ((bcount & 1) == 0)
2431 {
2432 --line_ga.ga_len;
2433 --pend;
2434 *pend = NUL;
2435 break;
2436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
2438 }
2439
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002440 --no_mapping;
2441 --allow_keys;
2442
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 /* make following messages go to the next line */
2444 msg_didout = FALSE;
2445 msg_col = 0;
2446 if (msg_row < Rows - 1)
2447 ++msg_row;
2448 emsg_on_display = FALSE; /* don't want ui_delay() */
2449
2450 if (got_int)
2451 ga_clear(&line_ga);
2452
2453 return (char_u *)line_ga.ga_data;
2454}
2455
Bram Moolenaare344bea2005-09-01 20:46:49 +00002456# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2457 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458/*
2459 * Return TRUE if ccline.overstrike is on.
2460 */
2461 int
2462cmdline_overstrike()
2463{
2464 return ccline.overstrike;
2465}
2466
2467/*
2468 * Return TRUE if the cursor is at the end of the cmdline.
2469 */
2470 int
2471cmdline_at_end()
2472{
2473 return (ccline.cmdpos >= ccline.cmdlen);
2474}
2475#endif
2476
Bram Moolenaar9372a112005-12-06 19:59:18 +00002477#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478/*
2479 * Return the virtual column number at the current cursor position.
2480 * This is used by the IM code to obtain the start of the preedit string.
2481 */
2482 colnr_T
2483cmdline_getvcol_cursor()
2484{
2485 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2486 return MAXCOL;
2487
2488# ifdef FEAT_MBYTE
2489 if (has_mbyte)
2490 {
2491 colnr_T col;
2492 int i = 0;
2493
2494 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002495 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
2497 return col;
2498 }
2499 else
2500# endif
2501 return ccline.cmdpos;
2502}
2503#endif
2504
2505#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2506/*
2507 * If part of the command line is an IM preedit string, redraw it with
2508 * IM feedback attributes. The cursor position is restored after drawing.
2509 */
2510 static void
2511redrawcmd_preedit()
2512{
2513 if ((State & CMDLINE)
2514 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002515 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 && !p_imdisable
2517 && im_is_preediting())
2518 {
2519 int cmdpos = 0;
2520 int cmdspos;
2521 int old_row;
2522 int old_col;
2523 colnr_T col;
2524
2525 old_row = msg_row;
2526 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002527 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529# ifdef FEAT_MBYTE
2530 if (has_mbyte)
2531 {
2532 for (col = 0; col < preedit_start_col
2533 && cmdpos < ccline.cmdlen; ++col)
2534 {
2535 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002536 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538 }
2539 else
2540# endif
2541 {
2542 cmdspos += preedit_start_col;
2543 cmdpos += preedit_start_col;
2544 }
2545
2546 msg_row = cmdline_row + (cmdspos / (int)Columns);
2547 msg_col = cmdspos % (int)Columns;
2548 if (msg_row >= Rows)
2549 msg_row = Rows - 1;
2550
2551 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2552 {
2553 int char_len;
2554 int char_attr;
2555
2556 char_attr = im_get_feedback_attr(col);
2557 if (char_attr < 0)
2558 break; /* end of preedit string */
2559
2560# ifdef FEAT_MBYTE
2561 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002562 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 else
2564# endif
2565 char_len = 1;
2566
2567 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2568 cmdpos += char_len;
2569 }
2570
2571 msg_row = old_row;
2572 msg_col = old_col;
2573 }
2574}
2575#endif /* FEAT_XIM && FEAT_GUI_GTK */
2576
2577/*
2578 * Allocate a new command line buffer.
2579 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2580 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2581 */
2582 static void
2583alloc_cmdbuff(len)
2584 int len;
2585{
2586 /*
2587 * give some extra space to avoid having to allocate all the time
2588 */
2589 if (len < 80)
2590 len = 100;
2591 else
2592 len += 20;
2593
2594 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2595 ccline.cmdbufflen = len;
2596}
2597
2598/*
2599 * Re-allocate the command line to length len + something extra.
2600 * return FAIL for failure, OK otherwise
2601 */
2602 static int
2603realloc_cmdbuff(len)
2604 int len;
2605{
2606 char_u *p;
2607
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002608 if (len < ccline.cmdbufflen)
2609 return OK; /* no need to resize */
2610
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 p = ccline.cmdbuff;
2612 alloc_cmdbuff(len); /* will get some more */
2613 if (ccline.cmdbuff == NULL) /* out of memory */
2614 {
2615 ccline.cmdbuff = p; /* keep the old one */
2616 return FAIL;
2617 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002618 /* There isn't always a NUL after the command, but it may need to be
2619 * there, thus copy up to the NUL and add a NUL. */
2620 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2621 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002623
2624 if (ccline.xpc != NULL
2625 && ccline.xpc->xp_pattern != NULL
2626 && ccline.xpc->xp_context != EXPAND_NOTHING
2627 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2628 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002629 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002630
2631 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2632 * to point into the newly allocated memory. */
2633 if (i >= 0 && i <= ccline.cmdlen)
2634 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2635 }
2636
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return OK;
2638}
2639
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002640#if defined(FEAT_ARABIC) || defined(PROTO)
2641static char_u *arshape_buf = NULL;
2642
2643# if defined(EXITFREE) || defined(PROTO)
2644 void
2645free_cmdline_buf()
2646{
2647 vim_free(arshape_buf);
2648}
2649# endif
2650#endif
2651
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652/*
2653 * Draw part of the cmdline at the current cursor position. But draw stars
2654 * when cmdline_star is TRUE.
2655 */
2656 static void
2657draw_cmdline(start, len)
2658 int start;
2659 int len;
2660{
2661#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2662 int i;
2663
2664 if (cmdline_star > 0)
2665 for (i = 0; i < len; ++i)
2666 {
2667 msg_putchar('*');
2668# ifdef FEAT_MBYTE
2669 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002670 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671# endif
2672 }
2673 else
2674#endif
2675#ifdef FEAT_ARABIC
2676 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 static int buflen = 0;
2679 char_u *p;
2680 int j;
2681 int newlen = 0;
2682 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002683 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int prev_c = 0;
2685 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002686 int u8c;
2687 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 /*
2691 * Do arabic shaping into a temporary buffer. This is very
2692 * inefficient!
2693 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002694 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
2696 /* Re-allocate the buffer. We keep it around to avoid a lot of
2697 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002698 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002699 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002700 arshape_buf = alloc(buflen);
2701 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 return; /* out of memory */
2703 }
2704
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002705 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2706 {
2707 /* Prepend a space to draw the leading composing char on. */
2708 arshape_buf[0] = ' ';
2709 newlen = 1;
2710 }
2711
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 for (j = start; j < start + len; j += mb_l)
2713 {
2714 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002715 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002716 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 if (ARABIC_CHAR(u8c))
2718 {
2719 /* Do Arabic shaping. */
2720 if (cmdmsg_rl)
2721 {
2722 /* displaying from right to left */
2723 pc = prev_c;
2724 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002725 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 if (j + mb_l >= start + len)
2727 nc = NUL;
2728 else
2729 nc = utf_ptr2char(p + mb_l);
2730 }
2731 else
2732 {
2733 /* displaying from left to right */
2734 if (j + mb_l >= start + len)
2735 pc = NUL;
2736 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002737 {
2738 int pcc[MAX_MCO];
2739
2740 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002742 pc1 = pcc[0];
2743 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 nc = prev_c;
2745 }
2746 prev_c = u8c;
2747
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002748 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002750 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002751 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002753 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2754 if (u8cc[1] != 0)
2755 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002756 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 }
2759 else
2760 {
2761 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002762 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 newlen += mb_l;
2764 }
2765 }
2766
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002767 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 else
2770#endif
2771 msg_outtrans_len(ccline.cmdbuff + start, len);
2772}
2773
2774/*
2775 * Put a character on the command line. Shifts the following text to the
2776 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2777 * "c" must be printable (fit in one display cell)!
2778 */
2779 void
2780putcmdline(c, shift)
2781 int c;
2782 int shift;
2783{
2784 if (cmd_silent)
2785 return;
2786 msg_no_more = TRUE;
2787 msg_putchar(c);
2788 if (shift)
2789 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2790 msg_no_more = FALSE;
2791 cursorcmd();
2792}
2793
2794/*
2795 * Undo a putcmdline(c, FALSE).
2796 */
2797 void
2798unputcmdline()
2799{
2800 if (cmd_silent)
2801 return;
2802 msg_no_more = TRUE;
2803 if (ccline.cmdlen == ccline.cmdpos)
2804 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002805#ifdef FEAT_MBYTE
2806 else if (has_mbyte)
2807 draw_cmdline(ccline.cmdpos,
2808 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
2811 draw_cmdline(ccline.cmdpos, 1);
2812 msg_no_more = FALSE;
2813 cursorcmd();
2814}
2815
2816/*
2817 * Put the given string, of the given length, onto the command line.
2818 * If len is -1, then STRLEN() is used to calculate the length.
2819 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2820 * part will be redrawn, otherwise it will not. If this function is called
2821 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2822 * called afterwards.
2823 */
2824 int
2825put_on_cmdline(str, len, redraw)
2826 char_u *str;
2827 int len;
2828 int redraw;
2829{
2830 int retval;
2831 int i;
2832 int m;
2833 int c;
2834
2835 if (len < 0)
2836 len = (int)STRLEN(str);
2837
2838 /* Check if ccline.cmdbuff needs to be longer */
2839 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002840 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 else
2842 retval = OK;
2843 if (retval == OK)
2844 {
2845 if (!ccline.overstrike)
2846 {
2847 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2848 ccline.cmdbuff + ccline.cmdpos,
2849 (size_t)(ccline.cmdlen - ccline.cmdpos));
2850 ccline.cmdlen += len;
2851 }
2852 else
2853 {
2854#ifdef FEAT_MBYTE
2855 if (has_mbyte)
2856 {
2857 /* Count nr of characters in the new string. */
2858 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002859 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 ++m;
2861 /* Count nr of bytes in cmdline that are overwritten by these
2862 * characters. */
2863 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002864 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 --m;
2866 if (i < ccline.cmdlen)
2867 {
2868 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2869 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2870 ccline.cmdlen += ccline.cmdpos + len - i;
2871 }
2872 else
2873 ccline.cmdlen = ccline.cmdpos + len;
2874 }
2875 else
2876#endif
2877 if (ccline.cmdpos + len > ccline.cmdlen)
2878 ccline.cmdlen = ccline.cmdpos + len;
2879 }
2880 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2881 ccline.cmdbuff[ccline.cmdlen] = NUL;
2882
2883#ifdef FEAT_MBYTE
2884 if (enc_utf8)
2885 {
2886 /* When the inserted text starts with a composing character,
2887 * backup to the character before it. There could be two of them.
2888 */
2889 i = 0;
2890 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2891 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2892 {
2893 i = (*mb_head_off)(ccline.cmdbuff,
2894 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2895 ccline.cmdpos -= i;
2896 len += i;
2897 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2898 }
2899# ifdef FEAT_ARABIC
2900 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2901 {
2902 /* Check the previous character for Arabic combining pair. */
2903 i = (*mb_head_off)(ccline.cmdbuff,
2904 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2905 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2906 + ccline.cmdpos - i), c))
2907 {
2908 ccline.cmdpos -= i;
2909 len += i;
2910 }
2911 else
2912 i = 0;
2913 }
2914# endif
2915 if (i != 0)
2916 {
2917 /* Also backup the cursor position. */
2918 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2919 ccline.cmdspos -= i;
2920 msg_col -= i;
2921 if (msg_col < 0)
2922 {
2923 msg_col += Columns;
2924 --msg_row;
2925 }
2926 }
2927 }
2928#endif
2929
2930 if (redraw && !cmd_silent)
2931 {
2932 msg_no_more = TRUE;
2933 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002934 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2936 /* Avoid clearing the rest of the line too often. */
2937 if (cmdline_row != i || ccline.overstrike)
2938 msg_clr_eos();
2939 msg_no_more = FALSE;
2940 }
2941#ifdef FEAT_FKMAP
2942 /*
2943 * If we are in Farsi command mode, the character input must be in
2944 * Insert mode. So do not advance the cmdpos.
2945 */
2946 if (!cmd_fkmap)
2947#endif
2948 {
2949 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002950 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002952 if (m < 0) /* overflow, Columns or Rows at weird value */
2953 m = MAXCOL;
2954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 else
2956 m = MAXCOL;
2957 for (i = 0; i < len; ++i)
2958 {
2959 c = cmdline_charsize(ccline.cmdpos);
2960#ifdef FEAT_MBYTE
2961 /* count ">" for a double-wide char that doesn't fit. */
2962 if (has_mbyte)
2963 correct_cmdspos(ccline.cmdpos, c);
2964#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002965 /* Stop cursor at the end of the screen, but do increment the
2966 * insert position, so that entering a very long command
2967 * works, even though you can't see it. */
2968 if (ccline.cmdspos + c < m)
2969 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970#ifdef FEAT_MBYTE
2971 if (has_mbyte)
2972 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002973 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 if (c > len - i - 1)
2975 c = len - i - 1;
2976 ccline.cmdpos += c;
2977 i += c;
2978 }
2979#endif
2980 ++ccline.cmdpos;
2981 }
2982 }
2983 }
2984 if (redraw)
2985 msg_check();
2986 return retval;
2987}
2988
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002989static struct cmdline_info prev_ccline;
2990static int prev_ccline_used = FALSE;
2991
2992/*
2993 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2994 * and overwrite it. But get_cmdline_str() may need it, thus make it
2995 * available globally in prev_ccline.
2996 */
2997 static void
2998save_cmdline(ccp)
2999 struct cmdline_info *ccp;
3000{
3001 if (!prev_ccline_used)
3002 {
3003 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3004 prev_ccline_used = TRUE;
3005 }
3006 *ccp = prev_ccline;
3007 prev_ccline = ccline;
3008 ccline.cmdbuff = NULL;
3009 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003010 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003011}
3012
3013/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003014 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003015 */
3016 static void
3017restore_cmdline(ccp)
3018 struct cmdline_info *ccp;
3019{
3020 ccline = prev_ccline;
3021 prev_ccline = *ccp;
3022}
3023
Bram Moolenaar5a305422006-04-28 22:38:25 +00003024#if defined(FEAT_EVAL) || defined(PROTO)
3025/*
3026 * Save the command line into allocated memory. Returns a pointer to be
3027 * passed to restore_cmdline_alloc() later.
3028 * Returns NULL when failed.
3029 */
3030 char_u *
3031save_cmdline_alloc()
3032{
3033 struct cmdline_info *p;
3034
3035 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3036 if (p != NULL)
3037 save_cmdline(p);
3038 return (char_u *)p;
3039}
3040
3041/*
3042 * Restore the command line from the return value of save_cmdline_alloc().
3043 */
3044 void
3045restore_cmdline_alloc(p)
3046 char_u *p;
3047{
3048 if (p != NULL)
3049 {
3050 restore_cmdline((struct cmdline_info *)p);
3051 vim_free(p);
3052 }
3053}
3054#endif
3055
Bram Moolenaar8299df92004-07-10 09:47:34 +00003056/*
3057 * paste a yank register into the command line.
3058 * used by CTRL-R command in command-line mode
3059 * insert_reg() can't be used here, because special characters from the
3060 * register contents will be interpreted as commands.
3061 *
3062 * return FAIL for failure, OK otherwise
3063 */
3064 static int
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003065cmdline_paste(regname, literally, remcr)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003066 int regname;
3067 int literally; /* Insert text literally instead of "as typed" */
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003068 int remcr; /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003069{
3070 long i;
3071 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003072 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003073 int allocated;
3074 struct cmdline_info save_ccline;
3075
3076 /* check for valid regname; also accept special characters for CTRL-R in
3077 * the command line */
3078 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3079 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3080 return FAIL;
3081
3082 /* A register containing CTRL-R can cause an endless loop. Allow using
3083 * CTRL-C to break the loop. */
3084 line_breakcheck();
3085 if (got_int)
3086 return FAIL;
3087
3088#ifdef FEAT_CLIPBOARD
3089 regname = may_get_selection(regname);
3090#endif
3091
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003092 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003093 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003094 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003095 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003096 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003097 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003098 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003099
3100 if (i)
3101 {
3102 /* Got the value of a special register in "arg". */
3103 if (arg == NULL)
3104 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003105
3106 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3107 * part of the word. */
3108 p = arg;
3109 if (p_is && regname == Ctrl_W)
3110 {
3111 char_u *w;
3112 int len;
3113
3114 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003115 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003116 {
3117#ifdef FEAT_MBYTE
3118 if (has_mbyte)
3119 {
3120 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3121 if (!vim_iswordc(mb_ptr2char(w - len)))
3122 break;
3123 w -= len;
3124 }
3125 else
3126#endif
3127 {
3128 if (!vim_iswordc(w[-1]))
3129 break;
3130 --w;
3131 }
3132 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003133 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003134 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3135 p += len;
3136 }
3137
3138 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003139 if (allocated)
3140 vim_free(arg);
3141 return OK;
3142 }
3143
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003144 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003145}
3146
3147/*
3148 * Put a string on the command line.
3149 * When "literally" is TRUE, insert literally.
3150 * When "literally" is FALSE, insert as typed, but don't leave the command
3151 * line.
3152 */
3153 void
3154cmdline_paste_str(s, literally)
3155 char_u *s;
3156 int literally;
3157{
3158 int c, cv;
3159
3160 if (literally)
3161 put_on_cmdline(s, -1, TRUE);
3162 else
3163 while (*s != NUL)
3164 {
3165 cv = *s;
3166 if (cv == Ctrl_V && s[1])
3167 ++s;
3168#ifdef FEAT_MBYTE
3169 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003170 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003171 else
3172#endif
3173 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003174 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3175 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003176#ifdef UNIX
3177 || c == intr_char
3178#endif
3179 || (c == Ctrl_BSL && *s == Ctrl_N))
3180 stuffcharReadbuff(Ctrl_V);
3181 stuffcharReadbuff(c);
3182 }
3183}
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185#ifdef FEAT_WILDMENU
3186/*
3187 * Delete characters on the command line, from "from" to the current
3188 * position.
3189 */
3190 static void
3191cmdline_del(from)
3192 int from;
3193{
3194 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3195 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3196 ccline.cmdlen -= ccline.cmdpos - from;
3197 ccline.cmdpos = from;
3198}
3199#endif
3200
3201/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003202 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 * search
3204 */
3205 void
3206redrawcmdline()
3207{
3208 if (cmd_silent)
3209 return;
3210 need_wait_return = FALSE;
3211 compute_cmdrow();
3212 redrawcmd();
3213 cursorcmd();
3214}
3215
3216 static void
3217redrawcmdprompt()
3218{
3219 int i;
3220
3221 if (cmd_silent)
3222 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003223 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 msg_putchar(ccline.cmdfirstc);
3225 if (ccline.cmdprompt != NULL)
3226 {
3227 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3228 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3229 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003230 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 --ccline.cmdindent;
3232 }
3233 else
3234 for (i = ccline.cmdindent; i > 0; --i)
3235 msg_putchar(' ');
3236}
3237
3238/*
3239 * Redraw what is currently on the command line.
3240 */
3241 void
3242redrawcmd()
3243{
3244 if (cmd_silent)
3245 return;
3246
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003247 /* when 'incsearch' is set there may be no command line while redrawing */
3248 if (ccline.cmdbuff == NULL)
3249 {
3250 windgoto(cmdline_row, 0);
3251 msg_clr_eos();
3252 return;
3253 }
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 msg_start();
3256 redrawcmdprompt();
3257
3258 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3259 msg_no_more = TRUE;
3260 draw_cmdline(0, ccline.cmdlen);
3261 msg_clr_eos();
3262 msg_no_more = FALSE;
3263
3264 set_cmdspos_cursor();
3265
3266 /*
3267 * An emsg() before may have set msg_scroll. This is used in normal mode,
3268 * in cmdline mode we can reset them now.
3269 */
3270 msg_scroll = FALSE; /* next message overwrites cmdline */
3271
3272 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3273 * in cmdline mode */
3274 skip_redraw = FALSE;
3275}
3276
3277 void
3278compute_cmdrow()
3279{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003280 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 cmdline_row = Rows - 1;
3282 else
3283 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3284 + W_STATUS_HEIGHT(lastwin);
3285}
3286
3287 static void
3288cursorcmd()
3289{
3290 if (cmd_silent)
3291 return;
3292
3293#ifdef FEAT_RIGHTLEFT
3294 if (cmdmsg_rl)
3295 {
3296 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3297 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3298 if (msg_row <= 0)
3299 msg_row = Rows - 1;
3300 }
3301 else
3302#endif
3303 {
3304 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3305 msg_col = ccline.cmdspos % (int)Columns;
3306 if (msg_row >= Rows)
3307 msg_row = Rows - 1;
3308 }
3309
3310 windgoto(msg_row, msg_col);
3311#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3312 redrawcmd_preedit();
3313#endif
3314#ifdef MCH_CURSOR_SHAPE
3315 mch_update_cursor();
3316#endif
3317}
3318
3319 void
3320gotocmdline(clr)
3321 int clr;
3322{
3323 msg_start();
3324#ifdef FEAT_RIGHTLEFT
3325 if (cmdmsg_rl)
3326 msg_col = Columns - 1;
3327 else
3328#endif
3329 msg_col = 0; /* always start in column 0 */
3330 if (clr) /* clear the bottom line(s) */
3331 msg_clr_eos(); /* will reset clear_cmdline */
3332 windgoto(cmdline_row, 0);
3333}
3334
3335/*
3336 * Check the word in front of the cursor for an abbreviation.
3337 * Called when the non-id character "c" has been entered.
3338 * When an abbreviation is recognized it is removed from the text with
3339 * backspaces and the replacement string is inserted, followed by "c".
3340 */
3341 static int
3342ccheck_abbr(c)
3343 int c;
3344{
3345 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3346 return FALSE;
3347
3348 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3349}
3350
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003351#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3352 static int
3353#ifdef __BORLANDC__
3354_RTLENTRYF
3355#endif
3356sort_func_compare(s1, s2)
3357 const void *s1;
3358 const void *s2;
3359{
3360 char_u *p1 = *(char_u **)s1;
3361 char_u *p2 = *(char_u **)s2;
3362
3363 if (*p1 != '<' && *p2 == '<') return -1;
3364 if (*p1 == '<' && *p2 != '<') return 1;
3365 return STRCMP(p1, p2);
3366}
3367#endif
3368
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369/*
3370 * Return FAIL if this is not an appropriate context in which to do
3371 * completion of anything, return OK if it is (even if there are no matches).
3372 * For the caller, this means that the character is just passed through like a
3373 * normal character (instead of being expanded). This allows :s/^I^D etc.
3374 */
3375 static int
Bram Moolenaarb3479632012-11-28 16:49:58 +01003376nextwild(xp, type, options, escape)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 expand_T *xp;
3378 int type;
3379 int options; /* extra options for ExpandOne() */
Bram Moolenaarb3479632012-11-28 16:49:58 +01003380 int escape; /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 int i, j;
3383 char_u *p1;
3384 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 int difflen;
3386 int v;
3387
3388 if (xp->xp_numfiles == -1)
3389 {
3390 set_expand_context(xp);
3391 cmd_showtail = expand_showtail(xp);
3392 }
3393
3394 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3395 {
3396 beep_flush();
3397 return OK; /* Something illegal on command line */
3398 }
3399 if (xp->xp_context == EXPAND_NOTHING)
3400 {
3401 /* Caller can use the character as a normal char instead */
3402 return FAIL;
3403 }
3404
3405 MSG_PUTS("..."); /* show that we are busy */
3406 out_flush();
3407
3408 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003409 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410
3411 if (type == WILD_NEXT || type == WILD_PREV)
3412 {
3413 /*
3414 * Get next/previous match for a previous expanded pattern.
3415 */
3416 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3417 }
3418 else
3419 {
3420 /*
3421 * Translate string into pattern and expand it.
3422 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003423 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3424 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 p2 = NULL;
3426 else
3427 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003428 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003429 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3430 if (escape)
3431 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003432
3433 if (p_wic)
3434 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003435 p2 = ExpandOne(xp, p1,
3436 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003437 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003439 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (p2 != NULL && type == WILD_LONGEST)
3441 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003442 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 if (ccline.cmdbuff[i + j] == '*'
3444 || ccline.cmdbuff[i + j] == '?')
3445 break;
3446 if ((int)STRLEN(p2) < j)
3447 {
3448 vim_free(p2);
3449 p2 = NULL;
3450 }
3451 }
3452 }
3453 }
3454
3455 if (p2 != NULL && !got_int)
3456 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003457 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003458 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003460 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 xp->xp_pattern = ccline.cmdbuff + i;
3462 }
3463 else
3464 v = OK;
3465 if (v == OK)
3466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003467 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3468 &ccline.cmdbuff[ccline.cmdpos],
3469 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3470 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 ccline.cmdlen += difflen;
3472 ccline.cmdpos += difflen;
3473 }
3474 }
3475 vim_free(p2);
3476
3477 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003478 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479
3480 /* When expanding a ":map" command and no matches are found, assume that
3481 * the key is supposed to be inserted literally */
3482 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3483 return FAIL;
3484
3485 if (xp->xp_numfiles <= 0 && p2 == NULL)
3486 beep_flush();
3487 else if (xp->xp_numfiles == 1)
3488 /* free expanded pattern */
3489 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3490
3491 return OK;
3492}
3493
3494/*
3495 * Do wildcard expansion on the string 'str'.
3496 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003497 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 * Return NULL for failure.
3499 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003500 * "orig" is the originally expanded string, copied to allocated memory. It
3501 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3502 * WILD_PREV "orig" should be NULL.
3503 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003504 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3505 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 *
3507 * mode = WILD_FREE: just free previously expanded matches
3508 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3509 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3510 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3511 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3512 * mode = WILD_ALL: return all matches concatenated
3513 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003514 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 *
3516 * options = WILD_LIST_NOTFOUND: list entries without a match
3517 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3518 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3519 * options = WILD_NO_BEEP: Don't beep for multiple matches
3520 * options = WILD_ADD_SLASH: add a slash after directory names
3521 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3522 * options = WILD_SILENT: don't print warning messages
3523 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003524 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 *
3526 * The variables xp->xp_context and xp->xp_backslash must have been set!
3527 */
3528 char_u *
3529ExpandOne(xp, str, orig, options, mode)
3530 expand_T *xp;
3531 char_u *str;
3532 char_u *orig; /* allocated copy of original of expanded string */
3533 int options;
3534 int mode;
3535{
3536 char_u *ss = NULL;
3537 static int findex;
3538 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003539 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 int i;
3541 long_u len;
3542 int non_suf_match; /* number without matching suffix */
3543
3544 /*
3545 * first handle the case of using an old match
3546 */
3547 if (mode == WILD_NEXT || mode == WILD_PREV)
3548 {
3549 if (xp->xp_numfiles > 0)
3550 {
3551 if (mode == WILD_PREV)
3552 {
3553 if (findex == -1)
3554 findex = xp->xp_numfiles;
3555 --findex;
3556 }
3557 else /* mode == WILD_NEXT */
3558 ++findex;
3559
3560 /*
3561 * When wrapping around, return the original string, set findex to
3562 * -1.
3563 */
3564 if (findex < 0)
3565 {
3566 if (orig_save == NULL)
3567 findex = xp->xp_numfiles - 1;
3568 else
3569 findex = -1;
3570 }
3571 if (findex >= xp->xp_numfiles)
3572 {
3573 if (orig_save == NULL)
3574 findex = 0;
3575 else
3576 findex = -1;
3577 }
3578#ifdef FEAT_WILDMENU
3579 if (p_wmnu)
3580 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3581 findex, cmd_showtail);
3582#endif
3583 if (findex == -1)
3584 return vim_strsave(orig_save);
3585 return vim_strsave(xp->xp_files[findex]);
3586 }
3587 else
3588 return NULL;
3589 }
3590
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003591 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3593 {
3594 FreeWild(xp->xp_numfiles, xp->xp_files);
3595 xp->xp_numfiles = -1;
3596 vim_free(orig_save);
3597 orig_save = NULL;
3598 }
3599 findex = 0;
3600
3601 if (mode == WILD_FREE) /* only release file name */
3602 return NULL;
3603
3604 if (xp->xp_numfiles == -1)
3605 {
3606 vim_free(orig_save);
3607 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003608 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609
3610 /*
3611 * Do the expansion.
3612 */
3613 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3614 options) == FAIL)
3615 {
3616#ifdef FNAME_ILLEGAL
3617 /* Illegal file name has been silently skipped. But when there
3618 * are wildcards, the real problem is that there was no match,
3619 * causing the pattern to be added, which has illegal characters.
3620 */
3621 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3622 EMSG2(_(e_nomatch2), str);
3623#endif
3624 }
3625 else if (xp->xp_numfiles == 0)
3626 {
3627 if (!(options & WILD_SILENT))
3628 EMSG2(_(e_nomatch2), str);
3629 }
3630 else
3631 {
3632 /* Escape the matches for use on the command line. */
3633 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3634
3635 /*
3636 * Check for matching suffixes in file names.
3637 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003638 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3639 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 {
3641 if (xp->xp_numfiles)
3642 non_suf_match = xp->xp_numfiles;
3643 else
3644 non_suf_match = 1;
3645 if ((xp->xp_context == EXPAND_FILES
3646 || xp->xp_context == EXPAND_DIRECTORIES)
3647 && xp->xp_numfiles > 1)
3648 {
3649 /*
3650 * More than one match; check suffix.
3651 * The files will have been sorted on matching suffix in
3652 * expand_wildcards, only need to check the first two.
3653 */
3654 non_suf_match = 0;
3655 for (i = 0; i < 2; ++i)
3656 if (match_suffix(xp->xp_files[i]))
3657 ++non_suf_match;
3658 }
3659 if (non_suf_match != 1)
3660 {
3661 /* Can we ever get here unless it's while expanding
3662 * interactively? If not, we can get rid of this all
3663 * together. Don't really want to wait for this message
3664 * (and possibly have to hit return to continue!).
3665 */
3666 if (!(options & WILD_SILENT))
3667 EMSG(_(e_toomany));
3668 else if (!(options & WILD_NO_BEEP))
3669 beep_flush();
3670 }
3671 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3672 ss = vim_strsave(xp->xp_files[0]);
3673 }
3674 }
3675 }
3676
3677 /* Find longest common part */
3678 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3679 {
3680 for (len = 0; xp->xp_files[0][len]; ++len)
3681 {
3682 for (i = 0; i < xp->xp_numfiles; ++i)
3683 {
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003684 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003686 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003687 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
3689 if (TOLOWER_LOC(xp->xp_files[i][len]) !=
3690 TOLOWER_LOC(xp->xp_files[0][len]))
3691 break;
3692 }
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003693 else if (xp->xp_files[i][len] != xp->xp_files[0][len])
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 break;
3695 }
3696 if (i < xp->xp_numfiles)
3697 {
3698 if (!(options & WILD_NO_BEEP))
3699 vim_beep();
3700 break;
3701 }
3702 }
3703 ss = alloc((unsigned)len + 1);
3704 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003705 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 findex = -1; /* next p_wc gets first one */
3707 }
3708
3709 /* Concatenate all matching names */
3710 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3711 {
3712 len = 0;
3713 for (i = 0; i < xp->xp_numfiles; ++i)
3714 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3715 ss = lalloc(len, TRUE);
3716 if (ss != NULL)
3717 {
3718 *ss = NUL;
3719 for (i = 0; i < xp->xp_numfiles; ++i)
3720 {
3721 STRCAT(ss, xp->xp_files[i]);
3722 if (i != xp->xp_numfiles - 1)
3723 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3724 }
3725 }
3726 }
3727
3728 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3729 ExpandCleanup(xp);
3730
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003731 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003732 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003733 vim_free(orig);
3734
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 return ss;
3736}
3737
3738/*
3739 * Prepare an expand structure for use.
3740 */
3741 void
3742ExpandInit(xp)
3743 expand_T *xp;
3744{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003745 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003746 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003748#ifndef BACKSLASH_IN_FILENAME
3749 xp->xp_shell = FALSE;
3750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 xp->xp_numfiles = -1;
3752 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003753#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3754 xp->xp_arg = NULL;
3755#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003756 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757}
3758
3759/*
3760 * Cleanup an expand structure after use.
3761 */
3762 void
3763ExpandCleanup(xp)
3764 expand_T *xp;
3765{
3766 if (xp->xp_numfiles >= 0)
3767 {
3768 FreeWild(xp->xp_numfiles, xp->xp_files);
3769 xp->xp_numfiles = -1;
3770 }
3771}
3772
3773 void
3774ExpandEscape(xp, str, numfiles, files, options)
3775 expand_T *xp;
3776 char_u *str;
3777 int numfiles;
3778 char_u **files;
3779 int options;
3780{
3781 int i;
3782 char_u *p;
3783
3784 /*
3785 * May change home directory back to "~"
3786 */
3787 if (options & WILD_HOME_REPLACE)
3788 tilde_replace(str, numfiles, files);
3789
3790 if (options & WILD_ESCAPE)
3791 {
3792 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003793 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003794 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 || xp->xp_context == EXPAND_BUFFERS
3796 || xp->xp_context == EXPAND_DIRECTORIES)
3797 {
3798 /*
3799 * Insert a backslash into a file name before a space, \, %, #
3800 * and wildmatch characters, except '~'.
3801 */
3802 for (i = 0; i < numfiles; ++i)
3803 {
3804 /* for ":set path=" we need to escape spaces twice */
3805 if (xp->xp_backslash == XP_BS_THREE)
3806 {
3807 p = vim_strsave_escaped(files[i], (char_u *)" ");
3808 if (p != NULL)
3809 {
3810 vim_free(files[i]);
3811 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003812#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 p = vim_strsave_escaped(files[i], (char_u *)" ");
3814 if (p != NULL)
3815 {
3816 vim_free(files[i]);
3817 files[i] = p;
3818 }
3819#endif
3820 }
3821 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003822#ifdef BACKSLASH_IN_FILENAME
3823 p = vim_strsave_fnameescape(files[i], FALSE);
3824#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003825 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (p != NULL)
3828 {
3829 vim_free(files[i]);
3830 files[i] = p;
3831 }
3832
3833 /* If 'str' starts with "\~", replace "~" at start of
3834 * files[i] with "\~". */
3835 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003836 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003839
3840 /* If the first file starts with a '+' escape it. Otherwise it
3841 * could be seen as "+cmd". */
3842 if (*files[0] == '+')
3843 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 }
3845 else if (xp->xp_context == EXPAND_TAGS)
3846 {
3847 /*
3848 * Insert a backslash before characters in a tag name that
3849 * would terminate the ":tag" command.
3850 */
3851 for (i = 0; i < numfiles; ++i)
3852 {
3853 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3854 if (p != NULL)
3855 {
3856 vim_free(files[i]);
3857 files[i] = p;
3858 }
3859 }
3860 }
3861 }
3862}
3863
3864/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003865 * Escape special characters in "fname" for when used as a file name argument
3866 * after a Vim command, or, when "shell" is non-zero, a shell command.
3867 * Returns the result in allocated memory.
3868 */
3869 char_u *
3870vim_strsave_fnameescape(fname, shell)
3871 char_u *fname;
3872 int shell;
3873{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003874 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003875#ifdef BACKSLASH_IN_FILENAME
3876 char_u buf[20];
3877 int j = 0;
3878
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003879 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003880 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003881 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003882 buf[j++] = *p;
3883 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003884 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003885#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003886 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3887 if (shell && csh_like_shell() && p != NULL)
3888 {
3889 char_u *s;
3890
3891 /* For csh and similar shells need to put two backslashes before '!'.
3892 * One is taken by Vim, one by the shell. */
3893 s = vim_strsave_escaped(p, (char_u *)"!");
3894 vim_free(p);
3895 p = s;
3896 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003897#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003898
3899 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3900 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003901 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003902 escape_fname(&p);
3903
3904 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003905}
3906
3907/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003908 * Put a backslash before the file name in "pp", which is in allocated memory.
3909 */
3910 static void
3911escape_fname(pp)
3912 char_u **pp;
3913{
3914 char_u *p;
3915
3916 p = alloc((unsigned)(STRLEN(*pp) + 2));
3917 if (p != NULL)
3918 {
3919 p[0] = '\\';
3920 STRCPY(p + 1, *pp);
3921 vim_free(*pp);
3922 *pp = p;
3923 }
3924}
3925
3926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 * For each file name in files[num_files]:
3928 * If 'orig_pat' starts with "~/", replace the home directory with "~".
3929 */
3930 void
3931tilde_replace(orig_pat, num_files, files)
3932 char_u *orig_pat;
3933 int num_files;
3934 char_u **files;
3935{
3936 int i;
3937 char_u *p;
3938
3939 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
3940 {
3941 for (i = 0; i < num_files; ++i)
3942 {
3943 p = home_replace_save(NULL, files[i]);
3944 if (p != NULL)
3945 {
3946 vim_free(files[i]);
3947 files[i] = p;
3948 }
3949 }
3950 }
3951}
3952
3953/*
3954 * Show all matches for completion on the command line.
3955 * Returns EXPAND_NOTHING when the character that triggered expansion should
3956 * be inserted like a normal character.
3957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 static int
3959showmatches(xp, wildmenu)
3960 expand_T *xp;
Bram Moolenaar78a15312009-05-15 19:33:18 +00003961 int wildmenu UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962{
3963#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3964 int num_files;
3965 char_u **files_found;
3966 int i, j, k;
3967 int maxlen;
3968 int lines;
3969 int columns;
3970 char_u *p;
3971 int lastlen;
3972 int attr;
3973 int showtail;
3974
3975 if (xp->xp_numfiles == -1)
3976 {
3977 set_expand_context(xp);
3978 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3979 &num_files, &files_found);
3980 showtail = expand_showtail(xp);
3981 if (i != EXPAND_OK)
3982 return i;
3983
3984 }
3985 else
3986 {
3987 num_files = xp->xp_numfiles;
3988 files_found = xp->xp_files;
3989 showtail = cmd_showtail;
3990 }
3991
3992#ifdef FEAT_WILDMENU
3993 if (!wildmenu)
3994 {
3995#endif
3996 msg_didany = FALSE; /* lines_left will be set */
3997 msg_start(); /* prepare for paging */
3998 msg_putchar('\n');
3999 out_flush();
4000 cmdline_row = msg_row;
4001 msg_didany = FALSE; /* lines_left will be set again */
4002 msg_start(); /* prepare for paging */
4003#ifdef FEAT_WILDMENU
4004 }
4005#endif
4006
4007 if (got_int)
4008 got_int = FALSE; /* only int. the completion, not the cmd line */
4009#ifdef FEAT_WILDMENU
4010 else if (wildmenu)
4011 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4012#endif
4013 else
4014 {
4015 /* find the length of the longest file name */
4016 maxlen = 0;
4017 for (i = 0; i < num_files; ++i)
4018 {
4019 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004020 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 || xp->xp_context == EXPAND_BUFFERS))
4022 {
4023 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4024 j = vim_strsize(NameBuff);
4025 }
4026 else
4027 j = vim_strsize(L_SHOWFILE(i));
4028 if (j > maxlen)
4029 maxlen = j;
4030 }
4031
4032 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4033 lines = num_files;
4034 else
4035 {
4036 /* compute the number of columns and lines for the listing */
4037 maxlen += 2; /* two spaces between file names */
4038 columns = ((int)Columns + 2) / maxlen;
4039 if (columns < 1)
4040 columns = 1;
4041 lines = (num_files + columns - 1) / columns;
4042 }
4043
4044 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4045
4046 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4047 {
4048 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4049 msg_clr_eos();
4050 msg_advance(maxlen - 3);
4051 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4052 }
4053
4054 /* list the files line by line */
4055 for (i = 0; i < lines; ++i)
4056 {
4057 lastlen = 999;
4058 for (k = i; k < num_files; k += lines)
4059 {
4060 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4061 {
4062 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4063 p = files_found[k] + STRLEN(files_found[k]) + 1;
4064 msg_advance(maxlen + 1);
4065 msg_puts(p);
4066 msg_advance(maxlen + 3);
4067 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4068 break;
4069 }
4070 for (j = maxlen - lastlen; --j >= 0; )
4071 msg_putchar(' ');
4072 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004073 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 || xp->xp_context == EXPAND_BUFFERS)
4075 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004076 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004077 if (xp->xp_numfiles != -1)
4078 {
4079 char_u *halved_slash;
4080 char_u *exp_path;
4081
4082 /* Expansion was done before and special characters
4083 * were escaped, need to halve backslashes. Also
4084 * $HOME has been replaced with ~/. */
4085 exp_path = expand_env_save_opt(files_found[k], TRUE);
4086 halved_slash = backslash_halve_save(
4087 exp_path != NULL ? exp_path : files_found[k]);
4088 j = mch_isdir(halved_slash != NULL ? halved_slash
4089 : files_found[k]);
4090 vim_free(exp_path);
4091 vim_free(halved_slash);
4092 }
4093 else
4094 /* Expansion was done here, file names are literal. */
4095 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 if (showtail)
4097 p = L_SHOWFILE(k);
4098 else
4099 {
4100 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4101 TRUE);
4102 p = NameBuff;
4103 }
4104 }
4105 else
4106 {
4107 j = FALSE;
4108 p = L_SHOWFILE(k);
4109 }
4110 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4111 }
4112 if (msg_col > 0) /* when not wrapped around */
4113 {
4114 msg_clr_eos();
4115 msg_putchar('\n');
4116 }
4117 out_flush(); /* show one line at a time */
4118 if (got_int)
4119 {
4120 got_int = FALSE;
4121 break;
4122 }
4123 }
4124
4125 /*
4126 * we redraw the command below the lines that we have just listed
4127 * This is a bit tricky, but it saves a lot of screen updating.
4128 */
4129 cmdline_row = msg_row; /* will put it back later */
4130 }
4131
4132 if (xp->xp_numfiles == -1)
4133 FreeWild(num_files, files_found);
4134
4135 return EXPAND_OK;
4136}
4137
4138/*
4139 * Private gettail for showmatches() (and win_redr_status_matches()):
4140 * Find tail of file name path, but ignore trailing "/".
4141 */
4142 char_u *
4143sm_gettail(s)
4144 char_u *s;
4145{
4146 char_u *p;
4147 char_u *t = s;
4148 int had_sep = FALSE;
4149
4150 for (p = s; *p != NUL; )
4151 {
4152 if (vim_ispathsep(*p)
4153#ifdef BACKSLASH_IN_FILENAME
4154 && !rem_backslash(p)
4155#endif
4156 )
4157 had_sep = TRUE;
4158 else if (had_sep)
4159 {
4160 t = p;
4161 had_sep = FALSE;
4162 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004163 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165 return t;
4166}
4167
4168/*
4169 * Return TRUE if we only need to show the tail of completion matches.
4170 * When not completing file names or there is a wildcard in the path FALSE is
4171 * returned.
4172 */
4173 static int
4174expand_showtail(xp)
4175 expand_T *xp;
4176{
4177 char_u *s;
4178 char_u *end;
4179
4180 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004181 if (xp->xp_context != EXPAND_FILES
4182 && xp->xp_context != EXPAND_SHELLCMD
4183 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 return FALSE;
4185
4186 end = gettail(xp->xp_pattern);
4187 if (end == xp->xp_pattern) /* there is no path separator */
4188 return FALSE;
4189
4190 for (s = xp->xp_pattern; s < end; s++)
4191 {
4192 /* Skip escaped wildcards. Only when the backslash is not a path
4193 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4194 if (rem_backslash(s))
4195 ++s;
4196 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4197 return FALSE;
4198 }
4199 return TRUE;
4200}
4201
4202/*
4203 * Prepare a string for expansion.
4204 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004205 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 * When expanding other names: The string will be used with regcomp(). Copy
4207 * the name into allocated memory and prepend "^".
4208 */
4209 char_u *
4210addstar(fname, len, context)
4211 char_u *fname;
4212 int len;
4213 int context; /* EXPAND_FILES etc. */
4214{
4215 char_u *retval;
4216 int i, j;
4217 int new_len;
4218 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004219 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004221 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004222 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004223 && context != EXPAND_SHELLCMD
4224 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 {
4226 /*
4227 * Matching will be done internally (on something other than files).
4228 * So we convert the file-matching-type wildcards into our kind for
4229 * use with vim_regcomp(). First work out how long it will be:
4230 */
4231
4232 /* For help tags the translation is done in find_help_tags().
4233 * For a tag pattern starting with "/" no translation is needed. */
4234 if (context == EXPAND_HELP
4235 || context == EXPAND_COLORS
4236 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004237 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004238 || context == EXPAND_FILETYPE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 || (context == EXPAND_TAGS && fname[0] == '/'))
4240 retval = vim_strnsave(fname, len);
4241 else
4242 {
4243 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4244 for (i = 0; i < len; i++)
4245 {
4246 if (fname[i] == '*' || fname[i] == '~')
4247 new_len++; /* '*' needs to be replaced by ".*"
4248 '~' needs to be replaced by "\~" */
4249
4250 /* Buffer names are like file names. "." should be literal */
4251 if (context == EXPAND_BUFFERS && fname[i] == '.')
4252 new_len++; /* "." becomes "\." */
4253
4254 /* Custom expansion takes care of special things, match
4255 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004256 if ((context == EXPAND_USER_DEFINED
4257 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 new_len++; /* '\' becomes "\\" */
4259 }
4260 retval = alloc(new_len);
4261 if (retval != NULL)
4262 {
4263 retval[0] = '^';
4264 j = 1;
4265 for (i = 0; i < len; i++, j++)
4266 {
4267 /* Skip backslash. But why? At least keep it for custom
4268 * expansion. */
4269 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004270 && context != EXPAND_USER_LIST
4271 && fname[i] == '\\'
4272 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 break;
4274
4275 switch (fname[i])
4276 {
4277 case '*': retval[j++] = '.';
4278 break;
4279 case '~': retval[j++] = '\\';
4280 break;
4281 case '?': retval[j] = '.';
4282 continue;
4283 case '.': if (context == EXPAND_BUFFERS)
4284 retval[j++] = '\\';
4285 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004286 case '\\': if (context == EXPAND_USER_DEFINED
4287 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 retval[j++] = '\\';
4289 break;
4290 }
4291 retval[j] = fname[i];
4292 }
4293 retval[j] = NUL;
4294 }
4295 }
4296 }
4297 else
4298 {
4299 retval = alloc(len + 4);
4300 if (retval != NULL)
4301 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004302 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303
4304 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004305 * Don't add a star to *, ~, ~user, $var or `cmd`.
4306 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 * ~ would be at the start of the file name, but not the tail.
4308 * $ could be anywhere in the tail.
4309 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004310 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 */
4312 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004313 ends_in_star = (len > 0 && retval[len - 1] == '*');
4314#ifndef BACKSLASH_IN_FILENAME
4315 for (i = len - 2; i >= 0; --i)
4316 {
4317 if (retval[i] != '\\')
4318 break;
4319 ends_in_star = !ends_in_star;
4320 }
4321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004323 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 && vim_strchr(tail, '$') == NULL
4325 && vim_strchr(retval, '`') == NULL)
4326 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004327 else if (len > 0 && retval[len - 1] == '$')
4328 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 retval[len] = NUL;
4330 }
4331 }
4332 return retval;
4333}
4334
4335/*
4336 * Must parse the command line so far to work out what context we are in.
4337 * Completion can then be done based on that context.
4338 * This routine sets the variables:
4339 * xp->xp_pattern The start of the pattern to be expanded within
4340 * the command line (ends at the cursor).
4341 * xp->xp_context The type of thing to expand. Will be one of:
4342 *
4343 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4344 * the command line, like an unknown command. Caller
4345 * should beep.
4346 * EXPAND_NOTHING Unrecognised context for completion, use char like
4347 * a normal char, rather than for completion. eg
4348 * :s/^I/
4349 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4350 * it.
4351 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4352 * EXPAND_FILES After command with XFILE set, or after setting
4353 * with P_EXPAND set. eg :e ^I, :w>>^I
4354 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4355 * when we know only directories are of interest. eg
4356 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004357 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4359 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4360 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4361 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4362 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4363 * EXPAND_EVENTS Complete event names
4364 * EXPAND_SYNTAX Complete :syntax command arguments
4365 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4366 * EXPAND_AUGROUP Complete autocommand group names
4367 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4368 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4369 * eg :unmap a^I , :cunab x^I
4370 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4371 * eg :call sub^I
4372 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4373 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4374 * names in expressions, eg :while s^I
4375 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004376 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 */
4378 static void
4379set_expand_context(xp)
4380 expand_T *xp;
4381{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004382 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (ccline.cmdfirstc != ':'
4384#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004385 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004386 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387#endif
4388 )
4389 {
4390 xp->xp_context = EXPAND_NOTHING;
4391 return;
4392 }
4393 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4394}
4395
4396 void
4397set_cmd_context(xp, str, len, col)
4398 expand_T *xp;
4399 char_u *str; /* start of command line */
4400 int len; /* length of command line (excl. NUL) */
4401 int col; /* position of cursor */
4402{
4403 int old_char = NUL;
4404 char_u *nextcomm;
4405
4406 /*
4407 * Avoid a UMR warning from Purify, only save the character if it has been
4408 * written before.
4409 */
4410 if (col < len)
4411 old_char = str[col];
4412 str[col] = NUL;
4413 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004414
4415#ifdef FEAT_EVAL
4416 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004417 {
4418# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004419 /* pass CMD_SIZE because there is no real command */
4420 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004421# endif
4422 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004423 else if (ccline.input_fn)
4424 {
4425 xp->xp_context = ccline.xp_context;
4426 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004427# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004428 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004429# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004430 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004431 else
4432#endif
4433 while (nextcomm != NULL)
4434 nextcomm = set_one_cmd_context(xp, nextcomm);
4435
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004436 /* Store the string here so that call_user_expand_func() can get to them
4437 * easily. */
4438 xp->xp_line = str;
4439 xp->xp_col = col;
4440
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 str[col] = old_char;
4442}
4443
4444/*
4445 * Expand the command line "str" from context "xp".
4446 * "xp" must have been set by set_cmd_context().
4447 * xp->xp_pattern points into "str", to where the text that is to be expanded
4448 * starts.
4449 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4450 * cursor.
4451 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4452 * key that triggered expansion literally.
4453 * Returns EXPAND_OK otherwise.
4454 */
4455 int
4456expand_cmdline(xp, str, col, matchcount, matches)
4457 expand_T *xp;
4458 char_u *str; /* start of command line */
4459 int col; /* position of cursor */
4460 int *matchcount; /* return: nr of matches */
4461 char_u ***matches; /* return: array of pointers to matches */
4462{
4463 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004464 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4467 {
4468 beep_flush();
4469 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4470 }
4471 if (xp->xp_context == EXPAND_NOTHING)
4472 {
4473 /* Caller can use the character as a normal char instead */
4474 return EXPAND_NOTHING;
4475 }
4476
4477 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004478 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4479 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 if (file_str == NULL)
4481 return EXPAND_UNSUCCESSFUL;
4482
Bram Moolenaar94950a92010-12-02 16:01:29 +01004483 if (p_wic)
4484 options += WILD_ICASE;
4485
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004487 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
4489 *matchcount = 0;
4490 *matches = NULL;
4491 }
4492 vim_free(file_str);
4493
4494 return EXPAND_OK;
4495}
4496
4497#ifdef FEAT_MULTI_LANG
4498/*
4499 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4500 */
4501static void cleanup_help_tags __ARGS((int num_file, char_u **file));
4502
4503 static void
4504cleanup_help_tags(num_file, file)
4505 int num_file;
4506 char_u **file;
4507{
4508 int i, j;
4509 int len;
4510
4511 for (i = 0; i < num_file; ++i)
4512 {
4513 len = (int)STRLEN(file[i]) - 3;
4514 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4515 {
4516 /* Sorting on priority means the same item in another language may
4517 * be anywhere. Search all items for a match up to the "@en". */
4518 for (j = 0; j < num_file; ++j)
4519 if (j != i
4520 && (int)STRLEN(file[j]) == len + 3
4521 && STRNCMP(file[i], file[j], len + 1) == 0)
4522 break;
4523 if (j == num_file)
4524 file[i][len] = NUL;
4525 }
4526 }
4527}
4528#endif
4529
4530/*
4531 * Do the expansion based on xp->xp_context and "pat".
4532 */
4533 static int
4534ExpandFromContext(xp, pat, num_file, file, options)
4535 expand_T *xp;
4536 char_u *pat;
4537 int *num_file;
4538 char_u ***file;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004539 int options; /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540{
4541#ifdef FEAT_CMDL_COMPL
4542 regmatch_T regmatch;
4543#endif
4544 int ret;
4545 int flags;
4546
4547 flags = EW_DIR; /* include directories */
4548 if (options & WILD_LIST_NOTFOUND)
4549 flags |= EW_NOTFOUND;
4550 if (options & WILD_ADD_SLASH)
4551 flags |= EW_ADDSLASH;
4552 if (options & WILD_KEEP_ALL)
4553 flags |= EW_KEEPALL;
4554 if (options & WILD_SILENT)
4555 flags |= EW_SILENT;
4556
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004557 if (xp->xp_context == EXPAND_FILES
4558 || xp->xp_context == EXPAND_DIRECTORIES
4559 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 {
4561 /*
4562 * Expand file or directory names.
4563 */
4564 int free_pat = FALSE;
4565 int i;
4566
4567 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4568 * space */
4569 if (xp->xp_backslash != XP_BS_NONE)
4570 {
4571 free_pat = TRUE;
4572 pat = vim_strsave(pat);
4573 for (i = 0; pat[i]; ++i)
4574 if (pat[i] == '\\')
4575 {
4576 if (xp->xp_backslash == XP_BS_THREE
4577 && pat[i + 1] == '\\'
4578 && pat[i + 2] == '\\'
4579 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004580 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (xp->xp_backslash == XP_BS_ONE
4582 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004583 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 }
4586
4587 if (xp->xp_context == EXPAND_FILES)
4588 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004589 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4590 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 else
4592 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004593 if (options & WILD_ICASE)
4594 flags |= EW_ICASE;
4595
Bram Moolenaard7834d32009-12-02 16:14:36 +00004596 /* Expand wildcards, supporting %:h and the like. */
4597 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (free_pat)
4599 vim_free(pat);
4600 return ret;
4601 }
4602
4603 *file = (char_u **)"";
4604 *num_file = 0;
4605 if (xp->xp_context == EXPAND_HELP)
4606 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004607 /* With an empty argument we would get all the help tags, which is
4608 * very slow. Get matches for "help" instead. */
4609 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4610 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 {
4612#ifdef FEAT_MULTI_LANG
4613 cleanup_help_tags(*num_file, *file);
4614#endif
4615 return OK;
4616 }
4617 return FAIL;
4618 }
4619
4620#ifndef FEAT_CMDL_COMPL
4621 return FAIL;
4622#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004623 if (xp->xp_context == EXPAND_SHELLCMD)
4624 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 if (xp->xp_context == EXPAND_OLD_SETTING)
4626 return ExpandOldSetting(num_file, file);
4627 if (xp->xp_context == EXPAND_BUFFERS)
4628 return ExpandBufnames(pat, num_file, file, options);
4629 if (xp->xp_context == EXPAND_TAGS
4630 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4631 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4632 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004633 {
4634 char *directories[] = {"colors", NULL};
4635 return ExpandRTDir(pat, num_file, file, directories);
4636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004638 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004639 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004640 return ExpandRTDir(pat, num_file, file, directories);
4641 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004642 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004643 {
4644 char *directories[] = {"syntax", NULL};
4645 return ExpandRTDir(pat, num_file, file, directories);
4646 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004647 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004648 {
4649 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4650 return ExpandRTDir(pat, num_file, file, directories);
4651 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004652# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4653 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004654 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004655# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
4657 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4658 if (regmatch.regprog == NULL)
4659 return FAIL;
4660
4661 /* set ignore-case according to p_ic, p_scs and pat */
4662 regmatch.rm_ic = ignorecase(pat);
4663
4664 if (xp->xp_context == EXPAND_SETTINGS
4665 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4666 ret = ExpandSettings(xp, &regmatch, num_file, file);
4667 else if (xp->xp_context == EXPAND_MAPPINGS)
4668 ret = ExpandMappings(&regmatch, num_file, file);
4669# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4670 else if (xp->xp_context == EXPAND_USER_DEFINED)
4671 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4672# endif
4673 else
4674 {
4675 static struct expgen
4676 {
4677 int context;
4678 char_u *((*func)__ARGS((expand_T *, int)));
4679 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004680 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 } tab[] =
4682 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004683 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4684 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004685#ifdef FEAT_CMDHIST
4686 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004689 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
4690 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4691 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4692 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693#endif
4694#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004695 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4696 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4697 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4698 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#endif
4700#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004701 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4702 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#endif
4704#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004705 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004707#ifdef FEAT_PROFILE
4708 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4709#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004710 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004712 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4713 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004715#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004716 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004717#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004718#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004719 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004720#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004721#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004722 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4725 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004726 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4727 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004729 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004730 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 };
4732 int i;
4733
4734 /*
4735 * Find a context in the table and call the ExpandGeneric() with the
4736 * right function to do the expansion.
4737 */
4738 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004739 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 if (xp->xp_context == tab[i].context)
4741 {
4742 if (tab[i].ic)
4743 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004744 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004745 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 break;
4747 }
4748 }
4749
Bram Moolenaar473de612013-06-08 18:19:48 +02004750 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 return ret;
4753#endif /* FEAT_CMDL_COMPL */
4754}
4755
4756#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4757/*
4758 * Expand a list of names.
4759 *
4760 * Generic function for command line completion. It calls a function to
4761 * obtain strings, one by one. The strings are matched against a regexp
4762 * program. Matching strings are copied into an array, which is returned.
4763 *
4764 * Returns OK when no problems encountered, FAIL for error (out of memory).
4765 */
4766 int
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004767ExpandGeneric(xp, regmatch, num_file, file, func, escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 expand_T *xp;
4769 regmatch_T *regmatch;
4770 int *num_file;
4771 char_u ***file;
4772 char_u *((*func)__ARGS((expand_T *, int)));
4773 /* returns a string from the list */
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004774 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 int i;
4777 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004778 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 char_u *str;
4780
4781 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004782 * round == 0: count the number of matching names
4783 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004785 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 {
4787 for (i = 0; ; ++i)
4788 {
4789 str = (*func)(xp, i);
4790 if (str == NULL) /* end of list */
4791 break;
4792 if (*str == NUL) /* skip empty strings */
4793 continue;
4794
4795 if (vim_regexec(regmatch, str, (colnr_T)0))
4796 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004797 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004799 if (escaped)
4800 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4801 else
4802 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 (*file)[count] = str;
4804#ifdef FEAT_MENU
4805 if (func == get_menu_names && str != NULL)
4806 {
4807 /* test for separator added by get_menu_names() */
4808 str += STRLEN(str) - 1;
4809 if (*str == '\001')
4810 *str = '.';
4811 }
4812#endif
4813 }
4814 ++count;
4815 }
4816 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004817 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 if (count == 0)
4820 return OK;
4821 *num_file = count;
4822 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4823 if (*file == NULL)
4824 {
4825 *file = (char_u **)"";
4826 return FAIL;
4827 }
4828 count = 0;
4829 }
4830 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004831
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004832 /* Sort the results. Keep menu's in the specified order. */
4833 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004834 {
4835 if (xp->xp_context == EXPAND_EXPRESSION
4836 || xp->xp_context == EXPAND_FUNCTIONS
4837 || xp->xp_context == EXPAND_USER_FUNC)
4838 /* <SNR> functions should be sorted to the end. */
4839 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4840 sort_func_compare);
4841 else
4842 sort_strings(*file, *num_file);
4843 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004844
Bram Moolenaar4f688582007-07-24 12:34:30 +00004845#ifdef FEAT_CMDL_COMPL
4846 /* Reset the variables used for special highlight names expansion, so that
4847 * they don't show up when getting normal highlight names by ID. */
4848 reset_expand_highlight();
4849#endif
4850
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 return OK;
4852}
4853
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004854/*
4855 * Complete a shell command.
4856 * Returns FAIL or OK;
4857 */
4858 static int
4859expand_shellcmd(filepat, num_file, file, flagsarg)
4860 char_u *filepat; /* pattern to match with command names */
4861 int *num_file; /* return: number of matches */
4862 char_u ***file; /* return: array with matches */
4863 int flagsarg; /* EW_ flags */
4864{
4865 char_u *pat;
4866 int i;
4867 char_u *path;
4868 int mustfree = FALSE;
4869 garray_T ga;
4870 char_u *buf = alloc(MAXPATHL);
4871 size_t l;
4872 char_u *s, *e;
4873 int flags = flagsarg;
4874 int ret;
4875
4876 if (buf == NULL)
4877 return FAIL;
4878
4879 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4880 * space */
4881 pat = vim_strsave(filepat);
4882 for (i = 0; pat[i]; ++i)
4883 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004884 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004885
4886 flags |= EW_FILE | EW_EXEC;
4887
4888 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004889 if (mch_isFullName(pat))
4890 path = (char_u *)" ";
4891 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004892 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4893 path = (char_u *)".";
4894 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004895 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004896 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004897 if (path == NULL)
4898 path = (char_u *)"";
4899 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004900
4901 /*
4902 * Go over all directories in $PATH. Expand matches in that directory and
4903 * collect them in "ga".
4904 */
4905 ga_init2(&ga, (int)sizeof(char *), 10);
4906 for (s = path; *s != NUL; s = e)
4907 {
Bram Moolenaar68c31742006-09-02 15:54:18 +00004908 if (*s == ' ')
4909 ++s; /* Skip space used for absolute path name. */
4910
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004911#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
4912 e = vim_strchr(s, ';');
4913#else
4914 e = vim_strchr(s, ':');
4915#endif
4916 if (e == NULL)
4917 e = s + STRLEN(s);
4918
4919 l = e - s;
4920 if (l > MAXPATHL - 5)
4921 break;
4922 vim_strncpy(buf, s, l);
4923 add_pathsep(buf);
4924 l = STRLEN(buf);
4925 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4926
4927 /* Expand matches in one directory of $PATH. */
4928 ret = expand_wildcards(1, &buf, num_file, file, flags);
4929 if (ret == OK)
4930 {
4931 if (ga_grow(&ga, *num_file) == FAIL)
4932 FreeWild(*num_file, *file);
4933 else
4934 {
4935 for (i = 0; i < *num_file; ++i)
4936 {
4937 s = (*file)[i];
4938 if (STRLEN(s) > l)
4939 {
4940 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004941 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004942 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4943 }
4944 else
4945 vim_free(s);
4946 }
4947 vim_free(*file);
4948 }
4949 }
4950 if (*e != NUL)
4951 ++e;
4952 }
4953 *file = ga.ga_data;
4954 *num_file = ga.ga_len;
4955
4956 vim_free(buf);
4957 vim_free(pat);
4958 if (mustfree)
4959 vim_free(path);
4960 return OK;
4961}
4962
4963
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00004965static 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));
4966
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004968 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004969 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004971 static void *
4972call_user_expand_func(user_expand_func, xp, num_file, file)
4973 void *(*user_expand_func) __ARGS((char_u *, int, char_u **, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 expand_T *xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 int *num_file;
4976 char_u ***file;
4977{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02004978 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004979 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004982 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004983 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984
Bram Moolenaarb7515462013-06-29 12:58:33 +02004985 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004986 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 *num_file = 0;
4988 *file = NULL;
4989
Bram Moolenaarb7515462013-06-29 12:58:33 +02004990 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00004991 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00004992 keep = ccline.cmdbuff[ccline.cmdlen];
4993 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00004994 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02004995
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004996 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02004997 args[1] = xp->xp_line;
4998 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 args[2] = num;
5000
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005001 /* Save the cmdline, we don't know what the function may do. */
5002 save_ccline = ccline;
5003 ccline.cmdbuff = NULL;
5004 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005006
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005007 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005008
5009 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005011 if (ccline.cmdbuff != NULL)
5012 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005013
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005014 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005015 return ret;
5016}
5017
5018/*
5019 * Expand names with a function defined by the user.
5020 */
5021 static int
5022ExpandUserDefined(xp, regmatch, num_file, file)
5023 expand_T *xp;
5024 regmatch_T *regmatch;
5025 int *num_file;
5026 char_u ***file;
5027{
5028 char_u *retstr;
5029 char_u *s;
5030 char_u *e;
5031 char_u keep;
5032 garray_T ga;
5033
5034 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5035 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 return FAIL;
5037
5038 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005039 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 e = vim_strchr(s, '\n');
5042 if (e == NULL)
5043 e = s + STRLEN(s);
5044 keep = *e;
5045 *e = 0;
5046
5047 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5048 {
5049 *e = keep;
5050 if (*e != NUL)
5051 ++e;
5052 continue;
5053 }
5054
5055 if (ga_grow(&ga, 1) == FAIL)
5056 break;
5057
5058 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5059 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 *e = keep;
5062 if (*e != NUL)
5063 ++e;
5064 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005065 vim_free(retstr);
5066 *file = ga.ga_data;
5067 *num_file = ga.ga_len;
5068 return OK;
5069}
5070
5071/*
5072 * Expand names with a list returned by a function defined by the user.
5073 */
5074 static int
5075ExpandUserList(xp, num_file, file)
5076 expand_T *xp;
5077 int *num_file;
5078 char_u ***file;
5079{
5080 list_T *retlist;
5081 listitem_T *li;
5082 garray_T ga;
5083
5084 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5085 if (retlist == NULL)
5086 return FAIL;
5087
5088 ga_init2(&ga, (int)sizeof(char *), 3);
5089 /* Loop over the items in the list. */
5090 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5091 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005092 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5093 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005094
5095 if (ga_grow(&ga, 1) == FAIL)
5096 break;
5097
5098 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005099 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005100 ++ga.ga_len;
5101 }
5102 list_unref(retlist);
5103
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 *file = ga.ga_data;
5105 *num_file = ga.ga_len;
5106 return OK;
5107}
5108#endif
5109
5110/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005111 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005112 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005113 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 */
5115 static int
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005116ExpandRTDir(pat, num_file, file, dirnames)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 char_u *pat;
5118 int *num_file;
5119 char_u ***file;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005120 char *dirnames[];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 char_u *s;
5123 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005124 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005126 int i;
5127 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
5129 *num_file = 0;
5130 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005131 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005132 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005134 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005136 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5137 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005139 ga_clear_strings(&ga);
5140 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005142 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005143 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005146
5147 for (i = 0; i < ga.ga_len; ++i)
5148 {
5149 match = ((char_u **)ga.ga_data)[i];
5150 s = match;
5151 e = s + STRLEN(s);
5152 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5153 {
5154 e -= 4;
5155 for (s = e; s > match; mb_ptr_back(match, s))
5156 if (s < match || vim_ispathsep(*s))
5157 break;
5158 ++s;
5159 *e = NUL;
5160 mch_memmove(match, s, e - s + 1);
5161 }
5162 }
5163
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005164 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005165 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005166
5167 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005168 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005169 remove_duplicates(&ga);
5170
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 *file = ga.ga_data;
5172 *num_file = ga.ga_len;
5173 return OK;
5174}
5175
5176#endif
5177
5178#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5179/*
5180 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005181 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005183 void
5184globpath(path, file, ga, expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 char_u *path;
5186 char_u *file;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005187 garray_T *ga;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005188 int expand_options;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189{
5190 expand_T xpc;
5191 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 int num_p;
5194 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195
5196 buf = alloc(MAXPATHL);
5197 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005198 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005200 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005202
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 /* Loop over all entries in {path}. */
5204 while (*path != NUL)
5205 {
5206 /* Copy one item of the path to buf[] and concatenate the file name. */
5207 copy_option_part(&path, buf, MAXPATHL, ",");
5208 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5209 {
Bram Moolenaar2d7c47d2010-08-10 19:50:26 +02005210# if defined(MSWIN) || defined(MSDOS)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005211 /* Using the platform's path separator (\) makes vim incorrectly
5212 * treat it as an escape character, use '/' instead. */
5213 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5214 STRCAT(buf, "/");
5215# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005217# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005219 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5220 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005222 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005224 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 for (i = 0; i < num_p; ++i)
5227 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005228 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005229 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005230 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005233
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 FreeWild(num_p, p);
5235 }
5236 }
5237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
5239 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240}
5241
5242#endif
5243
5244#if defined(FEAT_CMDHIST) || defined(PROTO)
5245
5246/*********************************
5247 * Command line history stuff *
5248 *********************************/
5249
5250/*
5251 * Translate a history character to the associated type number.
5252 */
5253 static int
5254hist_char2type(c)
5255 int c;
5256{
5257 if (c == ':')
5258 return HIST_CMD;
5259 if (c == '=')
5260 return HIST_EXPR;
5261 if (c == '@')
5262 return HIST_INPUT;
5263 if (c == '>')
5264 return HIST_DEBUG;
5265 return HIST_SEARCH; /* must be '?' or '/' */
5266}
5267
5268/*
5269 * Table of history names.
5270 * These names are used in :history and various hist...() functions.
5271 * It is sufficient to give the significant prefix of a history name.
5272 */
5273
5274static char *(history_names[]) =
5275{
5276 "cmd",
5277 "search",
5278 "expr",
5279 "input",
5280 "debug",
5281 NULL
5282};
5283
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005284#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5285/*
5286 * Function given to ExpandGeneric() to obtain the possible first
5287 * arguments of the ":history command.
5288 */
5289 static char_u *
5290get_history_arg(xp, idx)
5291 expand_T *xp UNUSED;
5292 int idx;
5293{
5294 static char_u compl[2] = { NUL, NUL };
5295 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005296 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005297 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5298
5299 if (idx < short_names_count)
5300 {
5301 compl[0] = (char_u)short_names[idx];
5302 return compl;
5303 }
5304 if (idx < short_names_count + history_name_count)
5305 return (char_u *)history_names[idx - short_names_count];
5306 if (idx == short_names_count + history_name_count)
5307 return (char_u *)"all";
5308 return NULL;
5309}
5310#endif
5311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312/*
5313 * init_history() - Initialize the command line history.
5314 * Also used to re-allocate the history when the size changes.
5315 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005316 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317init_history()
5318{
5319 int newlen; /* new length of history table */
5320 histentry_T *temp;
5321 int i;
5322 int j;
5323 int type;
5324
5325 /*
5326 * If size of history table changed, reallocate it
5327 */
5328 newlen = (int)p_hi;
5329 if (newlen != hislen) /* history length changed */
5330 {
5331 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5332 {
5333 if (newlen)
5334 {
5335 temp = (histentry_T *)lalloc(
5336 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5337 if (temp == NULL) /* out of memory! */
5338 {
5339 if (type == 0) /* first one: just keep the old length */
5340 {
5341 newlen = hislen;
5342 break;
5343 }
5344 /* Already changed one table, now we can only have zero
5345 * length for all tables. */
5346 newlen = 0;
5347 type = -1;
5348 continue;
5349 }
5350 }
5351 else
5352 temp = NULL;
5353 if (newlen == 0 || temp != NULL)
5354 {
5355 if (hisidx[type] < 0) /* there are no entries yet */
5356 {
5357 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005358 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360 else if (newlen > hislen) /* array becomes bigger */
5361 {
5362 for (i = 0; i <= hisidx[type]; ++i)
5363 temp[i] = history[type][i];
5364 j = i;
5365 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005366 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 for ( ; j < hislen; ++i, ++j)
5368 temp[i] = history[type][j];
5369 }
5370 else /* array becomes smaller or 0 */
5371 {
5372 j = hisidx[type];
5373 for (i = newlen - 1; ; --i)
5374 {
5375 if (i >= 0) /* copy newest entries */
5376 temp[i] = history[type][j];
5377 else /* remove older entries */
5378 vim_free(history[type][j].hisstr);
5379 if (--j < 0)
5380 j = hislen - 1;
5381 if (j == hisidx[type])
5382 break;
5383 }
5384 hisidx[type] = newlen - 1;
5385 }
5386 vim_free(history[type]);
5387 history[type] = temp;
5388 }
5389 }
5390 hislen = newlen;
5391 }
5392}
5393
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005394 static void
5395clear_hist_entry(hisptr)
5396 histentry_T *hisptr;
5397{
5398 hisptr->hisnum = 0;
5399 hisptr->viminfo = FALSE;
5400 hisptr->hisstr = NULL;
5401}
5402
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403/*
5404 * Check if command line 'str' is already in history.
5405 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5406 */
5407 static int
Bram Moolenaar07219f92013-04-14 23:19:36 +02005408in_history(type, str, move_to_front, sep, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 int type;
5410 char_u *str;
5411 int move_to_front; /* Move the entry to the front if it exists */
Bram Moolenaar4c402232011-07-27 17:58:46 +02005412 int sep;
Bram Moolenaar07219f92013-04-14 23:19:36 +02005413 int writing; /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 int i;
5416 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005417 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
5419 if (hisidx[type] < 0)
5420 return FALSE;
5421 i = hisidx[type];
5422 do
5423 {
5424 if (history[type][i].hisstr == NULL)
5425 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005426
5427 /* For search history, check that the separator character matches as
5428 * well. */
5429 p = history[type][i].hisstr;
5430 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005431 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005432 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 {
5434 if (!move_to_front)
5435 return TRUE;
5436 last_i = i;
5437 break;
5438 }
5439 if (--i < 0)
5440 i = hislen - 1;
5441 } while (i != hisidx[type]);
5442
5443 if (last_i >= 0)
5444 {
5445 str = history[type][i].hisstr;
5446 while (i != hisidx[type])
5447 {
5448 if (++i >= hislen)
5449 i = 0;
5450 history[type][last_i] = history[type][i];
5451 last_i = i;
5452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005454 history[type][i].viminfo = FALSE;
5455 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 return TRUE;
5457 }
5458 return FALSE;
5459}
5460
5461/*
5462 * Convert history name (from table above) to its HIST_ equivalent.
5463 * When "name" is empty, return "cmd" history.
5464 * Returns -1 for unknown history name.
5465 */
5466 int
5467get_histtype(name)
5468 char_u *name;
5469{
5470 int i;
5471 int len = (int)STRLEN(name);
5472
5473 /* No argument: use current history. */
5474 if (len == 0)
5475 return hist_char2type(ccline.cmdfirstc);
5476
5477 for (i = 0; history_names[i] != NULL; ++i)
5478 if (STRNICMP(name, history_names[i], len) == 0)
5479 return i;
5480
5481 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5482 return hist_char2type(name[0]);
5483
5484 return -1;
5485}
5486
5487static int last_maptick = -1; /* last seen maptick */
5488
5489/*
5490 * Add the given string to the given history. If the string is already in the
5491 * history then it is moved to the front. "histype" may be one of he HIST_
5492 * values.
5493 */
5494 void
5495add_to_history(histype, new_entry, in_map, sep)
5496 int histype;
5497 char_u *new_entry;
5498 int in_map; /* consider maptick when inside a mapping */
5499 int sep; /* separator character used (search hist) */
5500{
5501 histentry_T *hisptr;
5502 int len;
5503
5504 if (hislen == 0) /* no history */
5505 return;
5506
Bram Moolenaara939e432013-11-09 05:30:26 +01005507 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5508 return;
5509
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 /*
5511 * Searches inside the same mapping overwrite each other, so that only
5512 * the last line is kept. Be careful not to remove a line that was moved
5513 * down, only lines that were added.
5514 */
5515 if (histype == HIST_SEARCH && in_map)
5516 {
5517 if (maptick == last_maptick)
5518 {
5519 /* Current line is from the same mapping, remove it */
5520 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5521 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005522 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 --hisnum[histype];
5524 if (--hisidx[HIST_SEARCH] < 0)
5525 hisidx[HIST_SEARCH] = hislen - 1;
5526 }
5527 last_maptick = -1;
5528 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005529 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 {
5531 if (++hisidx[histype] == hislen)
5532 hisidx[histype] = 0;
5533 hisptr = &history[histype][hisidx[histype]];
5534 vim_free(hisptr->hisstr);
5535
5536 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005537 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5539 if (hisptr->hisstr != NULL)
5540 hisptr->hisstr[len + 1] = sep;
5541
5542 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005543 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 if (histype == HIST_SEARCH && in_map)
5545 last_maptick = maptick;
5546 }
5547}
5548
5549#if defined(FEAT_EVAL) || defined(PROTO)
5550
5551/*
5552 * Get identifier of newest history entry.
5553 * "histype" may be one of the HIST_ values.
5554 */
5555 int
5556get_history_idx(histype)
5557 int histype;
5558{
5559 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5560 || hisidx[histype] < 0)
5561 return -1;
5562
5563 return history[histype][hisidx[histype]].hisnum;
5564}
5565
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005566static struct cmdline_info *get_ccline_ptr __ARGS((void));
5567
5568/*
5569 * Get pointer to the command line info to use. cmdline_paste() may clear
5570 * ccline and put the previous value in prev_ccline.
5571 */
5572 static struct cmdline_info *
5573get_ccline_ptr()
5574{
5575 if ((State & CMDLINE) == 0)
5576 return NULL;
5577 if (ccline.cmdbuff != NULL)
5578 return &ccline;
5579 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5580 return &prev_ccline;
5581 return NULL;
5582}
5583
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584/*
5585 * Get the current command line in allocated memory.
5586 * Only works when the command line is being edited.
5587 * Returns NULL when something is wrong.
5588 */
5589 char_u *
5590get_cmdline_str()
5591{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005592 struct cmdline_info *p = get_ccline_ptr();
5593
5594 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005596 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597}
5598
5599/*
5600 * Get the current command line position, counted in bytes.
5601 * Zero is the first position.
5602 * Only works when the command line is being edited.
5603 * Returns -1 when something is wrong.
5604 */
5605 int
5606get_cmdline_pos()
5607{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005608 struct cmdline_info *p = get_ccline_ptr();
5609
5610 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005612 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613}
5614
5615/*
5616 * Set the command line byte position to "pos". Zero is the first position.
5617 * Only works when the command line is being edited.
5618 * Returns 1 when failed, 0 when OK.
5619 */
5620 int
5621set_cmdline_pos(pos)
5622 int pos;
5623{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005624 struct cmdline_info *p = get_ccline_ptr();
5625
5626 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 return 1;
5628
5629 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5630 * changed the command line. */
5631 if (pos < 0)
5632 new_cmdpos = 0;
5633 else
5634 new_cmdpos = pos;
5635 return 0;
5636}
5637
5638/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005639 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005640 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005641 * Only works when the command line is being edited.
5642 * Returns NUL when something is wrong.
5643 */
5644 int
5645get_cmdline_type()
5646{
5647 struct cmdline_info *p = get_ccline_ptr();
5648
5649 if (p == NULL)
5650 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005651 if (p->cmdfirstc == NUL)
5652 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005653 return p->cmdfirstc;
5654}
5655
5656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 * Calculate history index from a number:
5658 * num > 0: seen as identifying number of a history entry
5659 * num < 0: relative position in history wrt newest entry
5660 * "histype" may be one of the HIST_ values.
5661 */
5662 static int
5663calc_hist_idx(histype, num)
5664 int histype;
5665 int num;
5666{
5667 int i;
5668 histentry_T *hist;
5669 int wrapped = FALSE;
5670
5671 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5672 || (i = hisidx[histype]) < 0 || num == 0)
5673 return -1;
5674
5675 hist = history[histype];
5676 if (num > 0)
5677 {
5678 while (hist[i].hisnum > num)
5679 if (--i < 0)
5680 {
5681 if (wrapped)
5682 break;
5683 i += hislen;
5684 wrapped = TRUE;
5685 }
5686 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5687 return i;
5688 }
5689 else if (-num <= hislen)
5690 {
5691 i += num + 1;
5692 if (i < 0)
5693 i += hislen;
5694 if (hist[i].hisstr != NULL)
5695 return i;
5696 }
5697 return -1;
5698}
5699
5700/*
5701 * Get a history entry by its index.
5702 * "histype" may be one of the HIST_ values.
5703 */
5704 char_u *
5705get_history_entry(histype, idx)
5706 int histype;
5707 int idx;
5708{
5709 idx = calc_hist_idx(histype, idx);
5710 if (idx >= 0)
5711 return history[histype][idx].hisstr;
5712 else
5713 return (char_u *)"";
5714}
5715
5716/*
5717 * Clear all entries of a history.
5718 * "histype" may be one of the HIST_ values.
5719 */
5720 int
5721clr_history(histype)
5722 int histype;
5723{
5724 int i;
5725 histentry_T *hisptr;
5726
5727 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5728 {
5729 hisptr = history[histype];
5730 for (i = hislen; i--;)
5731 {
5732 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005733 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735 hisidx[histype] = -1; /* mark history as cleared */
5736 hisnum[histype] = 0; /* reset identifier counter */
5737 return OK;
5738 }
5739 return FAIL;
5740}
5741
5742/*
5743 * Remove all entries matching {str} from a history.
5744 * "histype" may be one of the HIST_ values.
5745 */
5746 int
5747del_history_entry(histype, str)
5748 int histype;
5749 char_u *str;
5750{
5751 regmatch_T regmatch;
5752 histentry_T *hisptr;
5753 int idx;
5754 int i;
5755 int last;
5756 int found = FALSE;
5757
5758 regmatch.regprog = NULL;
5759 regmatch.rm_ic = FALSE; /* always match case */
5760 if (hislen != 0
5761 && histype >= 0
5762 && histype < HIST_COUNT
5763 && *str != NUL
5764 && (idx = hisidx[histype]) >= 0
5765 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5766 != NULL)
5767 {
5768 i = last = idx;
5769 do
5770 {
5771 hisptr = &history[histype][i];
5772 if (hisptr->hisstr == NULL)
5773 break;
5774 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5775 {
5776 found = TRUE;
5777 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005778 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 }
5780 else
5781 {
5782 if (i != last)
5783 {
5784 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005785 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787 if (--last < 0)
5788 last += hislen;
5789 }
5790 if (--i < 0)
5791 i += hislen;
5792 } while (i != idx);
5793 if (history[histype][idx].hisstr == NULL)
5794 hisidx[histype] = -1;
5795 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005796 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 return found;
5798}
5799
5800/*
5801 * Remove an indexed entry from a history.
5802 * "histype" may be one of the HIST_ values.
5803 */
5804 int
5805del_history_idx(histype, idx)
5806 int histype;
5807 int idx;
5808{
5809 int i, j;
5810
5811 i = calc_hist_idx(histype, idx);
5812 if (i < 0)
5813 return FALSE;
5814 idx = hisidx[histype];
5815 vim_free(history[histype][i].hisstr);
5816
5817 /* When deleting the last added search string in a mapping, reset
5818 * last_maptick, so that the last added search string isn't deleted again.
5819 */
5820 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5821 last_maptick = -1;
5822
5823 while (i != idx)
5824 {
5825 j = (i + 1) % hislen;
5826 history[histype][i] = history[histype][j];
5827 i = j;
5828 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005829 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (--i < 0)
5831 i += hislen;
5832 hisidx[histype] = i;
5833 return TRUE;
5834}
5835
5836#endif /* FEAT_EVAL */
5837
5838#if defined(FEAT_CRYPT) || defined(PROTO)
5839/*
5840 * Very specific function to remove the value in ":set key=val" from the
5841 * history.
5842 */
5843 void
5844remove_key_from_history()
5845{
5846 char_u *p;
5847 int i;
5848
5849 i = hisidx[HIST_CMD];
5850 if (i < 0)
5851 return;
5852 p = history[HIST_CMD][i].hisstr;
5853 if (p != NULL)
5854 for ( ; *p; ++p)
5855 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5856 {
5857 p = vim_strchr(p + 3, '=');
5858 if (p == NULL)
5859 break;
5860 ++p;
5861 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5862 if (p[i] == '\\' && p[i + 1])
5863 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005864 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 --p;
5866 }
5867}
5868#endif
5869
5870#endif /* FEAT_CMDHIST */
5871
5872#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5873/*
5874 * Get indices "num1,num2" that specify a range within a list (not a range of
5875 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5876 * Returns OK if parsed successfully, otherwise FAIL.
5877 */
5878 int
5879get_list_range(str, num1, num2)
5880 char_u **str;
5881 int *num1;
5882 int *num2;
5883{
5884 int len;
5885 int first = FALSE;
5886 long num;
5887
5888 *str = skipwhite(*str);
5889 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5890 {
5891 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5892 *str += len;
5893 *num1 = (int)num;
5894 first = TRUE;
5895 }
5896 *str = skipwhite(*str);
5897 if (**str == ',') /* parse "to" part of range */
5898 {
5899 *str = skipwhite(*str + 1);
5900 vim_str2nr(*str, NULL, &len, FALSE, FALSE, &num, NULL);
5901 if (len > 0)
5902 {
5903 *num2 = (int)num;
5904 *str = skipwhite(*str + len);
5905 }
5906 else if (!first) /* no number given at all */
5907 return FAIL;
5908 }
5909 else if (first) /* only one number given */
5910 *num2 = *num1;
5911 return OK;
5912}
5913#endif
5914
5915#if defined(FEAT_CMDHIST) || defined(PROTO)
5916/*
5917 * :history command - print a history
5918 */
5919 void
5920ex_history(eap)
5921 exarg_T *eap;
5922{
5923 histentry_T *hist;
5924 int histype1 = HIST_CMD;
5925 int histype2 = HIST_CMD;
5926 int hisidx1 = 1;
5927 int hisidx2 = -1;
5928 int idx;
5929 int i, j, k;
5930 char_u *end;
5931 char_u *arg = eap->arg;
5932
5933 if (hislen == 0)
5934 {
5935 MSG(_("'history' option is zero"));
5936 return;
5937 }
5938
5939 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5940 {
5941 end = arg;
5942 while (ASCII_ISALPHA(*end)
5943 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5944 end++;
5945 i = *end;
5946 *end = NUL;
5947 histype1 = get_histtype(arg);
5948 if (histype1 == -1)
5949 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005950 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
5952 histype1 = 0;
5953 histype2 = HIST_COUNT-1;
5954 }
5955 else
5956 {
5957 *end = i;
5958 EMSG(_(e_trailing));
5959 return;
5960 }
5961 }
5962 else
5963 histype2 = histype1;
5964 *end = i;
5965 }
5966 else
5967 end = arg;
5968 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
5969 {
5970 EMSG(_(e_trailing));
5971 return;
5972 }
5973
5974 for (; !got_int && histype1 <= histype2; ++histype1)
5975 {
5976 STRCPY(IObuff, "\n # ");
5977 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
5978 MSG_PUTS_TITLE(IObuff);
5979 idx = hisidx[histype1];
5980 hist = history[histype1];
5981 j = hisidx1;
5982 k = hisidx2;
5983 if (j < 0)
5984 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
5985 if (k < 0)
5986 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
5987 if (idx >= 0 && j <= k)
5988 for (i = idx + 1; !got_int; ++i)
5989 {
5990 if (i == hislen)
5991 i = 0;
5992 if (hist[i].hisstr != NULL
5993 && hist[i].hisnum >= j && hist[i].hisnum <= k)
5994 {
5995 msg_putchar('\n');
5996 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
5997 hist[i].hisnum);
5998 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
5999 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006000 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 else
6002 STRCAT(IObuff, hist[i].hisstr);
6003 msg_outtrans(IObuff);
6004 out_flush();
6005 }
6006 if (i == idx)
6007 break;
6008 }
6009 }
6010}
6011#endif
6012
6013#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006014/*
6015 * Buffers for history read from a viminfo file. Only valid while reading.
6016 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6018static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6019static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6020static int viminfo_add_at_front = FALSE;
6021
6022static int hist_type2char __ARGS((int type, int use_question));
6023
6024/*
6025 * Translate a history type number to the associated character.
6026 */
6027 static int
6028hist_type2char(type, use_question)
6029 int type;
6030 int use_question; /* use '?' instead of '/' */
6031{
6032 if (type == HIST_CMD)
6033 return ':';
6034 if (type == HIST_SEARCH)
6035 {
6036 if (use_question)
6037 return '?';
6038 else
6039 return '/';
6040 }
6041 if (type == HIST_EXPR)
6042 return '=';
6043 return '@';
6044}
6045
6046/*
6047 * Prepare for reading the history from the viminfo file.
6048 * This allocates history arrays to store the read history lines.
6049 */
6050 void
Bram Moolenaar07219f92013-04-14 23:19:36 +02006051prepare_viminfo_history(asklen, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 int asklen;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006053 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 int i;
6056 int num;
6057 int type;
6058 int len;
6059
6060 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006061 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 if (asklen > hislen)
6063 asklen = hislen;
6064
6065 for (type = 0; type < HIST_COUNT; ++type)
6066 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006067 /* Count the number of empty spaces in the history list. Entries read
6068 * from viminfo previously are also considered empty. If there are
6069 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006071 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 num++;
6073 len = asklen;
6074 if (num > len)
6075 len = num;
6076 if (len <= 0)
6077 viminfo_history[type] = NULL;
6078 else
6079 viminfo_history[type] =
6080 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6081 if (viminfo_history[type] == NULL)
6082 len = 0;
6083 viminfo_hislen[type] = len;
6084 viminfo_hisidx[type] = 0;
6085 }
6086}
6087
6088/*
6089 * Accept a line from the viminfo, store it in the history array when it's
6090 * new.
6091 */
6092 int
Bram Moolenaar07219f92013-04-14 23:19:36 +02006093read_viminfo_history(virp, writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 vir_T *virp;
Bram Moolenaar07219f92013-04-14 23:19:36 +02006095 int writing;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 int type;
6098 long_u len;
6099 char_u *val;
6100 char_u *p;
6101
6102 type = hist_char2type(virp->vir_line[0]);
6103 if (viminfo_hisidx[type] < viminfo_hislen[type])
6104 {
6105 val = viminfo_readstring(virp, 1, TRUE);
6106 if (val != NULL && *val != NUL)
6107 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006108 int sep = (*val == ' ' ? NUL : *val);
6109
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006111 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 /* Need to re-allocate to append the separator byte. */
6114 len = STRLEN(val);
6115 p = lalloc(len + 2, TRUE);
6116 if (p != NULL)
6117 {
6118 if (type == HIST_SEARCH)
6119 {
6120 /* Search entry: Move the separator from the first
6121 * column to after the NUL. */
6122 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006123 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 }
6125 else
6126 {
6127 /* Not a search entry: No separator in the viminfo
6128 * file, add a NUL separator. */
6129 mch_memmove(p, val, (size_t)len + 1);
6130 p[len + 1] = NUL;
6131 }
6132 viminfo_history[type][viminfo_hisidx[type]++] = p;
6133 }
6134 }
6135 }
6136 vim_free(val);
6137 }
6138 return viminfo_readline(virp);
6139}
6140
Bram Moolenaar07219f92013-04-14 23:19:36 +02006141/*
6142 * Finish reading history lines from viminfo. Not used when writing viminfo.
6143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 void
6145finish_viminfo_history()
6146{
6147 int idx;
6148 int i;
6149 int type;
6150
6151 for (type = 0; type < HIST_COUNT; ++type)
6152 {
6153 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006154 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 idx = hisidx[type] + viminfo_hisidx[type];
6156 if (idx >= hislen)
6157 idx -= hislen;
6158 else if (idx < 0)
6159 idx = hislen - 1;
6160 if (viminfo_add_at_front)
6161 hisidx[type] = idx;
6162 else
6163 {
6164 if (hisidx[type] == -1)
6165 hisidx[type] = hislen - 1;
6166 do
6167 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006168 if (history[type][idx].hisstr != NULL
6169 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 break;
6171 if (++idx == hislen)
6172 idx = 0;
6173 } while (idx != hisidx[type]);
6174 if (idx != hisidx[type] && --idx < 0)
6175 idx = hislen - 1;
6176 }
6177 for (i = 0; i < viminfo_hisidx[type]; i++)
6178 {
6179 vim_free(history[type][idx].hisstr);
6180 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006181 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 if (--idx < 0)
6183 idx = hislen - 1;
6184 }
6185 idx += 1;
6186 idx %= hislen;
6187 for (i = 0; i < viminfo_hisidx[type]; i++)
6188 {
6189 history[type][idx++].hisnum = ++hisnum[type];
6190 idx %= hislen;
6191 }
6192 vim_free(viminfo_history[type]);
6193 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006194 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 }
6196}
6197
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006198/*
6199 * Write history to viminfo file in "fp".
6200 * When "merge" is TRUE merge history lines with a previously read viminfo
6201 * file, data is in viminfo_history[].
6202 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6203 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 void
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006205write_viminfo_history(fp, merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 FILE *fp;
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006207 int merge;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208{
6209 int i;
6210 int type;
6211 int num_saved;
6212 char_u *p;
6213 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006214 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215
6216 init_history();
6217 if (hislen == 0)
6218 return;
6219 for (type = 0; type < HIST_COUNT; ++type)
6220 {
6221 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6222 if (num_saved == 0)
6223 continue;
6224 if (num_saved < 0) /* Use default */
6225 num_saved = hislen;
6226 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6227 type == HIST_CMD ? _("Command Line") :
6228 type == HIST_SEARCH ? _("Search String") :
6229 type == HIST_EXPR ? _("Expression") :
6230 _("Input Line"));
6231 if (num_saved > hislen)
6232 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006233
6234 /*
6235 * Merge typed and viminfo history:
6236 * round 1: history of typed commands.
6237 * round 2: history from recently read viminfo.
6238 */
6239 for (round = 1; round <= 2; ++round)
6240 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006241 if (round == 1)
6242 /* start at newest entry, somewhere in the list */
6243 i = hisidx[type];
6244 else if (viminfo_hisidx[type] > 0)
6245 /* start at newest entry, first in the list */
6246 i = 0;
6247 else
6248 /* empty list */
6249 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006250 if (i >= 0)
6251 while (num_saved > 0
6252 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006254 p = round == 1 ? history[type][i].hisstr
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006255 : viminfo_history[type] == NULL ? NULL
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006256 : viminfo_history[type][i];
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006257 if (p != NULL && (round == 2
6258 || !merge
6259 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006261 --num_saved;
6262 fputc(hist_type2char(type, TRUE), fp);
6263 /* For the search history: put the separator in the
6264 * second column; use a space if there isn't one. */
6265 if (type == HIST_SEARCH)
6266 {
6267 c = p[STRLEN(p) + 1];
6268 putc(c == NUL ? ' ' : c, fp);
6269 }
6270 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006272 if (round == 1)
6273 {
6274 /* Decrement index, loop around and stop when back at
6275 * the start. */
6276 if (--i < 0)
6277 i = hislen - 1;
6278 if (i == hisidx[type])
6279 break;
6280 }
6281 else
6282 {
6283 /* Increment index. Stop at the end in the while. */
6284 ++i;
6285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006287 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006288 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006289 if (viminfo_history[type] != NULL)
6290 vim_free(viminfo_history[type][i]);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006291 vim_free(viminfo_history[type]);
6292 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006293 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 }
6295}
6296#endif /* FEAT_VIMINFO */
6297
6298#if defined(FEAT_FKMAP) || defined(PROTO)
6299/*
6300 * Write a character at the current cursor+offset position.
6301 * It is directly written into the command buffer block.
6302 */
6303 void
6304cmd_pchar(c, offset)
6305 int c, offset;
6306{
6307 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6308 {
6309 EMSG(_("E198: cmd_pchar beyond the command length"));
6310 return;
6311 }
6312 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6313 ccline.cmdbuff[ccline.cmdlen] = NUL;
6314}
6315
6316 int
6317cmd_gchar(offset)
6318 int offset;
6319{
6320 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6321 {
6322 /* EMSG(_("cmd_gchar beyond the command length")); */
6323 return NUL;
6324 }
6325 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6326}
6327#endif
6328
6329#if defined(FEAT_CMDWIN) || defined(PROTO)
6330/*
6331 * Open a window on the current command line and history. Allow editing in
6332 * the window. Returns when the window is closed.
6333 * Returns:
6334 * CR if the command is to be executed
6335 * Ctrl_C if it is to be abandoned
6336 * K_IGNORE if editing continues
6337 */
6338 static int
6339ex_window()
6340{
6341 struct cmdline_info save_ccline;
6342 buf_T *old_curbuf = curbuf;
6343 win_T *old_curwin = curwin;
6344 buf_T *bp;
6345 win_T *wp;
6346 int i;
6347 linenr_T lnum;
6348 int histtype;
6349 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006350#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006352#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 int save_restart_edit = restart_edit;
6354 int save_State = State;
6355 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006356#ifdef FEAT_RIGHTLEFT
6357 int save_cmdmsg_rl = cmdmsg_rl;
6358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 /* Can't do this recursively. Can't do it when typing a password. */
6361 if (cmdwin_type != 0
6362# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6363 || cmdline_star > 0
6364# endif
6365 )
6366 {
6367 beep_flush();
6368 return K_IGNORE;
6369 }
6370
6371 /* Save current window sizes. */
6372 win_size_save(&winsizes);
6373
6374# ifdef FEAT_AUTOCMD
6375 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006376 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006378 /* don't use a new tab page */
6379 cmdmod.tab = 0;
6380
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381 /* Create a window for the command-line buffer. */
6382 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6383 {
6384 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006385# ifdef FEAT_AUTOCMD
6386 unblock_autocmds();
6387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 return K_IGNORE;
6389 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006390 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391
6392 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006393 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006394 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6396 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6397 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006398#ifdef FEAT_FOLDING
6399 curwin->w_p_fen = FALSE;
6400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006402 curwin->w_p_rl = cmdmsg_rl;
6403 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006405 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406
6407# ifdef FEAT_AUTOCMD
6408 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006409 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410# endif
6411
Bram Moolenaar46152342005-09-07 21:18:43 +00006412 /* Showing the prompt may have set need_wait_return, reset it. */
6413 need_wait_return = FALSE;
6414
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006415 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6417 {
6418 if (p_wc == TAB)
6419 {
6420 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6421 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6422 }
6423 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6424 }
6425
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006426 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6427 * sets 'textwidth' to 78). */
6428 curbuf->b_p_tw = 0;
6429
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430 /* Fill the buffer with the history. */
6431 init_history();
6432 if (hislen > 0)
6433 {
6434 i = hisidx[histtype];
6435 if (i >= 0)
6436 {
6437 lnum = 0;
6438 do
6439 {
6440 if (++i == hislen)
6441 i = 0;
6442 if (history[histtype][i].hisstr != NULL)
6443 ml_append(lnum++, history[histtype][i].hisstr,
6444 (colnr_T)0, FALSE);
6445 }
6446 while (i != hisidx[histtype]);
6447 }
6448 }
6449
6450 /* Replace the empty last line with the current command-line and put the
6451 * cursor there. */
6452 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6453 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6454 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006455 changed_line_abv_curs();
6456 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006457 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 /* Save the command line info, can be used recursively. */
6460 save_ccline = ccline;
6461 ccline.cmdbuff = NULL;
6462 ccline.cmdprompt = NULL;
6463
6464 /* No Ex mode here! */
6465 exmode_active = 0;
6466
6467 State = NORMAL;
6468# ifdef FEAT_MOUSE
6469 setmouse();
6470# endif
6471
6472# ifdef FEAT_AUTOCMD
6473 /* Trigger CmdwinEnter autocommands. */
6474 typestr[0] = cmdwin_type;
6475 typestr[1] = NUL;
6476 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006477 if (restart_edit != 0) /* autocmd with ":startinsert" */
6478 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479# endif
6480
6481 i = RedrawingDisabled;
6482 RedrawingDisabled = 0;
6483
6484 /*
6485 * Call the main loop until <CR> or CTRL-C is typed.
6486 */
6487 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006488 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006489
6490 RedrawingDisabled = i;
6491
6492# ifdef FEAT_AUTOCMD
6493 /* Trigger CmdwinLeave autocommands. */
6494 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
6495# endif
6496
Bram Moolenaarccc18222007-05-10 18:25:20 +00006497 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 ccline = save_ccline;
6499 cmdwin_type = 0;
6500
6501 exmode_active = save_exmode;
6502
Bram Moolenaarf9821062008-06-20 16:31:07 +00006503 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 * this happens! */
6505 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6506 {
6507 cmdwin_result = Ctrl_C;
6508 EMSG(_("E199: Active window or buffer deleted"));
6509 }
6510 else
6511 {
6512# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6513 /* autocmds may abort script processing */
6514 if (aborting() && cmdwin_result != K_IGNORE)
6515 cmdwin_result = Ctrl_C;
6516# endif
6517 /* Set the new command line from the cmdline buffer. */
6518 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006519 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006521 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6522
6523 if (histtype == HIST_CMD)
6524 {
6525 /* Execute the command directly. */
6526 ccline.cmdbuff = vim_strsave((char_u *)p);
6527 cmdwin_result = CAR;
6528 }
6529 else
6530 {
6531 /* First need to cancel what we were doing. */
6532 ccline.cmdbuff = NULL;
6533 stuffcharReadbuff(':');
6534 stuffReadbuff((char_u *)p);
6535 stuffcharReadbuff(CAR);
6536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 }
6538 else if (cmdwin_result == K_XF2) /* :qa typed */
6539 {
6540 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6541 cmdwin_result = CAR;
6542 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006543 else if (cmdwin_result == Ctrl_C)
6544 {
6545 /* :q or :close, don't execute any command
6546 * and don't modify the cmd window. */
6547 ccline.cmdbuff = NULL;
6548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 else
6550 ccline.cmdbuff = vim_strsave(ml_get_curline());
6551 if (ccline.cmdbuff == NULL)
6552 cmdwin_result = Ctrl_C;
6553 else
6554 {
6555 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6556 ccline.cmdbufflen = ccline.cmdlen + 1;
6557 ccline.cmdpos = curwin->w_cursor.col;
6558 if (ccline.cmdpos > ccline.cmdlen)
6559 ccline.cmdpos = ccline.cmdlen;
6560 if (cmdwin_result == K_IGNORE)
6561 {
6562 set_cmdspos_cursor();
6563 redrawcmd();
6564 }
6565 }
6566
6567# ifdef FEAT_AUTOCMD
6568 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006569 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570# endif
6571 wp = curwin;
6572 bp = curbuf;
6573 win_goto(old_curwin);
6574 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006575
6576 /* win_close() may have already wiped the buffer when 'bh' is
6577 * set to 'wipe' */
6578 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006579 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
6581 /* Restore window sizes. */
6582 win_size_restore(&winsizes);
6583
6584# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006585 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586# endif
6587 }
6588
6589 ga_clear(&winsizes);
6590 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006591# ifdef FEAT_RIGHTLEFT
6592 cmdmsg_rl = save_cmdmsg_rl;
6593# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595 State = save_State;
6596# ifdef FEAT_MOUSE
6597 setmouse();
6598# endif
6599
6600 return cmdwin_result;
6601}
6602#endif /* FEAT_CMDWIN */
6603
6604/*
6605 * Used for commands that either take a simple command string argument, or:
6606 * cmd << endmarker
6607 * {script}
6608 * endmarker
6609 * Returns a pointer to allocated memory with {script} or NULL.
6610 */
6611 char_u *
6612script_get(eap, cmd)
6613 exarg_T *eap;
6614 char_u *cmd;
6615{
6616 char_u *theline;
6617 char *end_pattern = NULL;
6618 char dot[] = ".";
6619 garray_T ga;
6620
6621 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6622 return NULL;
6623
6624 ga_init2(&ga, 1, 0x400);
6625
6626 if (cmd[2] != NUL)
6627 end_pattern = (char *)skipwhite(cmd + 2);
6628 else
6629 end_pattern = dot;
6630
6631 for (;;)
6632 {
6633 theline = eap->getline(
6634#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006635 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#endif
6637 NUL, eap->cookie, 0);
6638
6639 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006640 {
6641 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006643 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644
6645 ga_concat(&ga, theline);
6646 ga_append(&ga, '\n');
6647 vim_free(theline);
6648 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006649 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651 return (char_u *)ga.ga_data;
6652}