blob: ed9707a26253eb8d66bb784856cf6d3c8df3ec69 [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
Bram Moolenaard25c16e2016-01-29 22:13:30 +010069static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000070
Bram Moolenaard25c16e2016-01-29 22:13:30 +010071static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000072# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010073static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000074# 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
Bram Moolenaard25c16e2016-01-29 22:13:30 +010085static int cmdline_charsize(int idx);
86static void set_cmdspos(void);
87static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000088#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000090#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010091static void alloc_cmdbuff(int len);
92static int realloc_cmdbuff(int len);
93static void draw_cmdline(int start, int len);
94static void save_cmdline(struct cmdline_info *ccp);
95static void restore_cmdline(struct cmdline_info *ccp);
96static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
100#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100101static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmdprompt(void);
104static void cursorcmd(void);
105static int ccheck_abbr(int);
106static int nextwild(expand_T *xp, int type, int options, int escape);
107static void escape_fname(char_u **pp);
108static int showmatches(expand_T *xp, int wildmenu);
109static void set_expand_context(expand_T *xp);
110static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
111static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100113static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100114static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100115static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200116# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100117static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200118# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100120static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
121static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122# endif
123#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200124#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128#ifdef FEAT_CMDWIN
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100129static int ex_window(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130#endif
131
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200132#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
133static int
134#ifdef __BORLANDC__
135_RTLENTRYF
136#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100137sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200138#endif
139
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140/*
141 * getcmdline() - accept a command line starting with firstc.
142 *
143 * firstc == ':' get ":" command line.
144 * firstc == '/' or '?' get search pattern
145 * firstc == '=' get expression
146 * firstc == '@' get text for input() function
147 * firstc == '>' get text for debug mode
148 * firstc == NUL get text for :insert command
149 * firstc == -1 like NUL, and break on CTRL-C
150 *
151 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
152 * command line.
153 *
154 * Careful: getcmdline() can be called recursively!
155 *
156 * Return pointer to allocated string if there is a commandline, NULL
157 * otherwise.
158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100160getcmdline(
161 int firstc,
162 long count UNUSED, /* only used for incremental search */
163 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164{
165 int c;
166 int i;
167 int j;
168 int gotesc = FALSE; /* TRUE when <ESC> just typed */
169 int do_abbr; /* when TRUE check for abbr. */
170#ifdef FEAT_CMDHIST
171 char_u *lookfor = NULL; /* string to match */
172 int hiscnt; /* current history line in use */
173 int histype; /* history type to be used */
174#endif
175#ifdef FEAT_SEARCH_EXTRA
176 pos_T old_cursor;
177 colnr_T old_curswant;
178 colnr_T old_leftcol;
179 linenr_T old_topline;
180# ifdef FEAT_DIFF
181 int old_topfill;
182# endif
183 linenr_T old_botline;
184 int did_incsearch = FALSE;
185 int incsearch_postponed = FALSE;
186#endif
187 int did_wild_list = FALSE; /* did wild_list() recently */
188 int wim_index = 0; /* index in wim_flags[] */
189 int res;
190 int save_msg_scroll = msg_scroll;
191 int save_State = State; /* remember State when called */
192 int some_key_typed = FALSE; /* one of the keys was typed */
193#ifdef FEAT_MOUSE
194 /* mouse drag and release events are ignored, unless they are
195 * preceded with a mouse down event */
196 int ignore_drag_release = TRUE;
197#endif
198#ifdef FEAT_EVAL
199 int break_ctrl_c = FALSE;
200#endif
201 expand_T xpc;
202 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000203#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
204 /* Everything that may work recursively should save and restore the
205 * current command line in save_ccline. That includes update_screen(), a
206 * custom status line may invoke ":normal". */
207 struct cmdline_info save_ccline;
208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#ifdef FEAT_EVAL
211 if (firstc == -1)
212 {
213 firstc = NUL;
214 break_ctrl_c = TRUE;
215 }
216#endif
217#ifdef FEAT_RIGHTLEFT
218 /* start without Hebrew mapping for a command line */
219 if (firstc == ':' || firstc == '=' || firstc == '>')
220 cmd_hkmap = 0;
221#endif
222
223 ccline.overstrike = FALSE; /* always start in insert mode */
224#ifdef FEAT_SEARCH_EXTRA
225 old_cursor = curwin->w_cursor; /* needs to be restored later */
226 old_curswant = curwin->w_curswant;
227 old_leftcol = curwin->w_leftcol;
228 old_topline = curwin->w_topline;
229# ifdef FEAT_DIFF
230 old_topfill = curwin->w_topfill;
231# endif
232 old_botline = curwin->w_botline;
233#endif
234
235 /*
236 * set some variables for redrawcmd()
237 */
238 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000239 ccline.cmdindent = (firstc > 0 ? indent : 0);
240
241 /* alloc initial ccline.cmdbuff */
242 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 if (ccline.cmdbuff == NULL)
244 return NULL; /* out of memory */
245 ccline.cmdlen = ccline.cmdpos = 0;
246 ccline.cmdbuff[0] = NUL;
247
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000248 /* autoindent for :insert and :append */
249 if (firstc <= 0)
250 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200251 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000252 ccline.cmdbuff[indent] = NUL;
253 ccline.cmdpos = indent;
254 ccline.cmdspos = indent;
255 ccline.cmdlen = indent;
256 }
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000259 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260
261#ifdef FEAT_RIGHTLEFT
262 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
263 && (firstc == '/' || firstc == '?'))
264 cmdmsg_rl = TRUE;
265 else
266 cmdmsg_rl = FALSE;
267#endif
268
269 redir_off = TRUE; /* don't redirect the typed command */
270 if (!cmd_silent)
271 {
272 i = msg_scrolled;
273 msg_scrolled = 0; /* avoid wait_return message */
274 gotocmdline(TRUE);
275 msg_scrolled += i;
276 redrawcmdprompt(); /* draw prompt or indent */
277 set_cmdspos();
278 }
279 xpc.xp_context = EXPAND_NOTHING;
280 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000281#ifndef BACKSLASH_IN_FILENAME
282 xpc.xp_shell = FALSE;
283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000285#if defined(FEAT_EVAL)
286 if (ccline.input_fn)
287 {
288 xpc.xp_context = ccline.xp_context;
289 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000290# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000291 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000292# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000293 }
294#endif
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 /*
297 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
298 * doing ":@0" when register 0 doesn't contain a CR.
299 */
300 msg_scroll = FALSE;
301
302 State = CMDLINE;
303
304 if (firstc == '/' || firstc == '?' || firstc == '@')
305 {
306 /* Use ":lmap" mappings for search pattern and input(). */
307 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
308 b_im_ptr = &curbuf->b_p_iminsert;
309 else
310 b_im_ptr = &curbuf->b_p_imsearch;
311 if (*b_im_ptr == B_IMODE_LMAP)
312 State |= LANGMAP;
313#ifdef USE_IM_CONTROL
314 im_set_active(*b_im_ptr == B_IMODE_IM);
315#endif
316 }
317#ifdef USE_IM_CONTROL
318 else if (p_imcmdline)
319 im_set_active(TRUE);
320#endif
321
322#ifdef FEAT_MOUSE
323 setmouse();
324#endif
325#ifdef CURSOR_SHAPE
326 ui_cursor_shape(); /* may show different cursor shape */
327#endif
328
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000329 /* When inside an autocommand for writing "exiting" may be set and
330 * terminal mode set to cooked. Need to set raw mode here then. */
331 settmode(TMODE_RAW);
332
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333#ifdef FEAT_CMDHIST
334 init_history();
335 hiscnt = hislen; /* set hiscnt to impossible history value */
336 histype = hist_char2type(firstc);
337#endif
338
339#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000340 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341#endif
342
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200343 /* If something above caused an error, reset the flags, we do want to type
344 * and execute commands. Display may be messed up a bit. */
345 if (did_emsg)
346 redrawcmd();
347 did_emsg = FALSE;
348 got_int = FALSE;
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 /*
351 * Collect the command string, handling editing keys.
352 */
353 for (;;)
354 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000355 redir_off = TRUE; /* Don't redirect the typed command.
356 Repeated, because a ":redir" inside
357 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef USE_ON_FLY_SCROLL
359 dont_scroll = FALSE; /* allow scrolling here */
360#endif
361 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
362
363 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000364
365 /* Get a character. Ignore K_IGNORE, it should not do anything, such
366 * as stop completion. */
367 do
368 {
369 c = safe_vgetc();
370 } while (c == K_IGNORE);
371
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 if (KeyTyped)
373 {
374 some_key_typed = TRUE;
375#ifdef FEAT_RIGHTLEFT
376 if (cmd_hkmap)
377 c = hkmap(c);
378# ifdef FEAT_FKMAP
379 if (cmd_fkmap)
380 c = cmdl_fkmap(c);
381# endif
382 if (cmdmsg_rl && !KeyStuffed)
383 {
384 /* Invert horizontal movements and operations. Only when
385 * typed by the user directly, not when the result of a
386 * mapping. */
387 switch (c)
388 {
389 case K_RIGHT: c = K_LEFT; break;
390 case K_S_RIGHT: c = K_S_LEFT; break;
391 case K_C_RIGHT: c = K_C_LEFT; break;
392 case K_LEFT: c = K_RIGHT; break;
393 case K_S_LEFT: c = K_S_RIGHT; break;
394 case K_C_LEFT: c = K_C_RIGHT; break;
395 }
396 }
397#endif
398 }
399
400 /*
401 * Ignore got_int when CTRL-C was typed here.
402 * Don't ignore it in :global, we really need to break then, e.g., for
403 * ":g/pat/normal /pat" (without the <CR>).
404 * Don't ignore it for the input() function.
405 */
406 if ((c == Ctrl_C
407#ifdef UNIX
408 || c == intr_char
409#endif
410 )
411#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
412 && firstc != '@'
413#endif
414#ifdef FEAT_EVAL
415 && !break_ctrl_c
416#endif
417 && !global_busy)
418 got_int = FALSE;
419
420#ifdef FEAT_CMDHIST
421 /* free old command line when finished moving around in the history
422 * list */
423 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000424 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000425 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 && c != K_PAGEDOWN && c != K_PAGEUP
427 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000428 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
430 {
431 vim_free(lookfor);
432 lookfor = NULL;
433 }
434#endif
435
436 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000437 * When there are matching completions to select <S-Tab> works like
438 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000440 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 c = Ctrl_P;
442
443#ifdef FEAT_WILDMENU
444 /* Special translations for 'wildmenu' */
445 if (did_wild_list && p_wmnu)
446 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000447 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000449 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450 c = Ctrl_N;
451 }
452 /* Hitting CR after "emenu Name.": complete submenu */
453 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
454 && ccline.cmdpos > 1
455 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
456 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
457 && (c == '\n' || c == '\r' || c == K_KENTER))
458 c = K_DOWN;
459#endif
460
461 /* free expanded names when finished walking through matches */
462 if (xpc.xp_numfiles != -1
463 && !(c == p_wc && KeyTyped) && c != p_wcm
464 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
465 && c != Ctrl_L)
466 {
467 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
468 did_wild_list = FALSE;
469#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000470 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471#endif
472 xpc.xp_context = EXPAND_NOTHING;
473 wim_index = 0;
474#ifdef FEAT_WILDMENU
475 if (p_wmnu && wild_menu_showing != 0)
476 {
477 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000478 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000479
480 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000481 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482
483 if (wild_menu_showing == WM_SCROLLED)
484 {
485 /* Entered command line, move it up */
486 cmdline_row--;
487 redrawcmd();
488 }
489 else if (save_p_ls != -1)
490 {
491 /* restore 'laststatus' and 'winminheight' */
492 p_ls = save_p_ls;
493 p_wmh = save_p_wmh;
494 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000495 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000497 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 redrawcmd();
499 save_p_ls = -1;
500 }
501 else
502 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 redraw_statuslines();
505 }
506 KeyTyped = skt;
507 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000508 if (ccline.input_fn)
509 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 }
511#endif
512 }
513
514#ifdef FEAT_WILDMENU
515 /* Special translations for 'wildmenu' */
516 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
517 {
518 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000519 if (c == K_DOWN && ccline.cmdpos > 0
520 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000522 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 {
524 /* Hitting <Up>: Remove one submenu name in front of the
525 * cursor */
526 int found = FALSE;
527
528 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
529 i = 0;
530 while (--j > 0)
531 {
532 /* check for start of menu name */
533 if (ccline.cmdbuff[j] == ' '
534 && ccline.cmdbuff[j - 1] != '\\')
535 {
536 i = j + 1;
537 break;
538 }
539 /* check for start of submenu name */
540 if (ccline.cmdbuff[j] == '.'
541 && ccline.cmdbuff[j - 1] != '\\')
542 {
543 if (found)
544 {
545 i = j + 1;
546 break;
547 }
548 else
549 found = TRUE;
550 }
551 }
552 if (i > 0)
553 cmdline_del(i);
554 c = p_wc;
555 xpc.xp_context = EXPAND_NOTHING;
556 }
557 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000558 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000559 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000560 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
562 char_u upseg[5];
563
564 upseg[0] = PATHSEP;
565 upseg[1] = '.';
566 upseg[2] = '.';
567 upseg[3] = PATHSEP;
568 upseg[4] = NUL;
569
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000570 if (c == K_DOWN
571 && ccline.cmdpos > 0
572 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
573 && (ccline.cmdpos < 3
574 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
576 {
577 /* go down a directory */
578 c = p_wc;
579 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000580 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 {
582 /* If in a direct ancestor, strip off one ../ to go down */
583 int found = FALSE;
584
585 j = ccline.cmdpos;
586 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
587 while (--j > i)
588 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000589#ifdef FEAT_MBYTE
590 if (has_mbyte)
591 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 if (vim_ispathsep(ccline.cmdbuff[j]))
594 {
595 found = TRUE;
596 break;
597 }
598 }
599 if (found
600 && ccline.cmdbuff[j - 1] == '.'
601 && ccline.cmdbuff[j - 2] == '.'
602 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
603 {
604 cmdline_del(j - 2);
605 c = p_wc;
606 }
607 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000608 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 {
610 /* go up a directory */
611 int found = FALSE;
612
613 j = ccline.cmdpos - 1;
614 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
615 while (--j > i)
616 {
617#ifdef FEAT_MBYTE
618 if (has_mbyte)
619 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
620#endif
621 if (vim_ispathsep(ccline.cmdbuff[j])
622#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100623 && vim_strchr((char_u *)" *?[{`$%#",
624 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#endif
626 )
627 {
628 if (found)
629 {
630 i = j + 1;
631 break;
632 }
633 else
634 found = TRUE;
635 }
636 }
637
638 if (!found)
639 j = i;
640 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
641 j += 4;
642 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
643 && j == i)
644 j += 3;
645 else
646 j = 0;
647 if (j > 0)
648 {
649 /* TODO this is only for DOS/UNIX systems - need to put in
650 * machine-specific stuff here and in upseg init */
651 cmdline_del(j);
652 put_on_cmdline(upseg + 1, 3, FALSE);
653 }
654 else if (ccline.cmdpos > i)
655 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100656
657 /* Now complete in the new directory. Set KeyTyped in case the
658 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100660 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 }
662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663
664#endif /* FEAT_WILDMENU */
665
666 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
667 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
668 if (c == Ctrl_BSL)
669 {
670 ++no_mapping;
671 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000672 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 --no_mapping;
674 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200675 /* CTRL-\ e doesn't work when obtaining an expression, unless it
676 * is in a mapping. */
677 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
678 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 {
680 vungetc(c);
681 c = Ctrl_BSL;
682 }
683#ifdef FEAT_EVAL
684 else if (c == 'e')
685 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200686 char_u *p = NULL;
687 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688
689 /*
690 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000691 * Need to save and restore the current command line, to be
692 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 */
694 if (ccline.cmdpos == ccline.cmdlen)
695 new_cmdpos = 99999; /* keep it at the end */
696 else
697 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000698
699 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000701 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 if (c == '=')
703 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000704 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000705 * to avoid nasty things like going to another buffer when
706 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000707 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000708 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000710 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000711 restore_cmdline(&save_ccline);
712
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200713 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200715 len = (int)STRLEN(p);
716 if (realloc_cmdbuff(len + 1) == OK)
717 {
718 ccline.cmdlen = len;
719 STRCPY(ccline.cmdbuff, p);
720 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200722 /* Restore the cursor or use the position set with
723 * set_cmdline_pos(). */
724 if (new_cmdpos > ccline.cmdlen)
725 ccline.cmdpos = ccline.cmdlen;
726 else
727 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200729 KeyTyped = FALSE; /* Don't do p_wc completion. */
730 redrawcmd();
731 goto cmdline_changed;
732 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 }
734 }
735 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100736 got_int = FALSE; /* don't abandon the command line */
737 did_emsg = FALSE;
738 emsg_on_display = FALSE;
739 redrawcmd();
740 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 }
742#endif
743 else
744 {
745 if (c == Ctrl_G && p_im && restart_edit == 0)
746 restart_edit = 'a';
747 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
748 in history */
749 goto returncmd; /* back to Normal mode */
750 }
751 }
752
753#ifdef FEAT_CMDWIN
754 if (c == cedit_key || c == K_CMDWIN)
755 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200756 if (ex_normal_busy == 0 && got_int == FALSE)
757 {
758 /*
759 * Open a window to edit the command line (and history).
760 */
761 c = ex_window();
762 some_key_typed = TRUE;
763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 }
765# ifdef FEAT_DIGRAPHS
766 else
767# endif
768#endif
769#ifdef FEAT_DIGRAPHS
770 c = do_digraph(c);
771#endif
772
773 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
774 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
775 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000776 /* In Ex mode a backslash escapes a newline. */
777 if (exmode_active
778 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000779 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000780 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000781 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000783 if (c == K_KENTER)
784 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000786 else
787 {
788 gotesc = FALSE; /* Might have typed ESC previously, don't
789 truncate the cmdline now. */
790 if (ccheck_abbr(c + ABBR_OFF))
791 goto cmdline_changed;
792 if (!cmd_silent)
793 {
794 windgoto(msg_row, 0);
795 out_flush();
796 }
797 break;
798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 }
800
801 /*
802 * Completion for 'wildchar' or 'wildcharm' key.
803 * - hitting <ESC> twice means: abandon command line.
804 * - wildcard expansion is only done when the 'wildchar' key is really
805 * typed, not when it comes from a macro
806 */
807 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
808 {
809 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
810 {
811 /* if 'wildmode' contains "list" may still need to list */
812 if (xpc.xp_numfiles > 1
813 && !did_wild_list
814 && (wim_flags[wim_index] & WIM_LIST))
815 {
816 (void)showmatches(&xpc, FALSE);
817 redrawcmd();
818 did_wild_list = TRUE;
819 }
820 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100821 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
822 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100824 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
825 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 else
827 res = OK; /* don't insert 'wildchar' now */
828 }
829 else /* typed p_wc first time */
830 {
831 wim_index = 0;
832 j = ccline.cmdpos;
833 /* if 'wildmode' first contains "longest", get longest
834 * common part */
835 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100836 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
837 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100839 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
840 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841
842 /* if interrupted while completing, behave like it failed */
843 if (got_int)
844 {
845 (void)vpeekc(); /* remove <C-C> from input stream */
846 got_int = FALSE; /* don't abandon the command line */
847 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
848#ifdef FEAT_WILDMENU
849 xpc.xp_context = EXPAND_NOTHING;
850#endif
851 goto cmdline_changed;
852 }
853
854 /* when more than one match, and 'wildmode' first contains
855 * "list", or no change and 'wildmode' contains "longest,list",
856 * list all matches */
857 if (res == OK && xpc.xp_numfiles > 1)
858 {
859 /* a "longest" that didn't do anything is skipped (but not
860 * "list:longest") */
861 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
862 wim_index = 1;
863 if ((wim_flags[wim_index] & WIM_LIST)
864#ifdef FEAT_WILDMENU
865 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
866#endif
867 )
868 {
869 if (!(wim_flags[0] & WIM_LONGEST))
870 {
871#ifdef FEAT_WILDMENU
872 int p_wmnu_save = p_wmnu;
873 p_wmnu = 0;
874#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100875 /* remove match */
876 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877#ifdef FEAT_WILDMENU
878 p_wmnu = p_wmnu_save;
879#endif
880 }
881#ifdef FEAT_WILDMENU
882 (void)showmatches(&xpc, p_wmnu
883 && ((wim_flags[wim_index] & WIM_LIST) == 0));
884#else
885 (void)showmatches(&xpc, FALSE);
886#endif
887 redrawcmd();
888 did_wild_list = TRUE;
889 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100890 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
891 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100893 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
894 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 }
896 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200897 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899#ifdef FEAT_WILDMENU
900 else if (xpc.xp_numfiles == -1)
901 xpc.xp_context = EXPAND_NOTHING;
902#endif
903 }
904 if (wim_index < 3)
905 ++wim_index;
906 if (c == ESC)
907 gotesc = TRUE;
908 if (res == OK)
909 goto cmdline_changed;
910 }
911
912 gotesc = FALSE;
913
914 /* <S-Tab> goes to last match, in a clumsy way */
915 if (c == K_S_TAB && KeyTyped)
916 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100917 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
918 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
919 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920 goto cmdline_changed;
921 }
922
923 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
924 c = NL;
925
926 do_abbr = TRUE; /* default: check for abbreviation */
927
928 /*
929 * Big switch for a typed command line character.
930 */
931 switch (c)
932 {
933 case K_BS:
934 case Ctrl_H:
935 case K_DEL:
936 case K_KDEL:
937 case Ctrl_W:
938#ifdef FEAT_FKMAP
939 if (cmd_fkmap && c == K_BS)
940 c = K_DEL;
941#endif
942 if (c == K_KDEL)
943 c = K_DEL;
944
945 /*
946 * delete current character is the same as backspace on next
947 * character, except at end of line
948 */
949 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
950 ++ccline.cmdpos;
951#ifdef FEAT_MBYTE
952 if (has_mbyte && c == K_DEL)
953 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
954 ccline.cmdbuff + ccline.cmdpos);
955#endif
956 if (ccline.cmdpos > 0)
957 {
958 char_u *p;
959
960 j = ccline.cmdpos;
961 p = ccline.cmdbuff + j;
962#ifdef FEAT_MBYTE
963 if (has_mbyte)
964 {
965 p = mb_prevptr(ccline.cmdbuff, p);
966 if (c == Ctrl_W)
967 {
968 while (p > ccline.cmdbuff && vim_isspace(*p))
969 p = mb_prevptr(ccline.cmdbuff, p);
970 i = mb_get_class(p);
971 while (p > ccline.cmdbuff && mb_get_class(p) == i)
972 p = mb_prevptr(ccline.cmdbuff, p);
973 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000974 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 }
976 }
977 else
978#endif
979 if (c == Ctrl_W)
980 {
981 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
982 --p;
983 i = vim_iswordc(p[-1]);
984 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
985 && vim_iswordc(p[-1]) == i)
986 --p;
987 }
988 else
989 --p;
990 ccline.cmdpos = (int)(p - ccline.cmdbuff);
991 ccline.cmdlen -= j - ccline.cmdpos;
992 i = ccline.cmdpos;
993 while (i < ccline.cmdlen)
994 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
995
996 /* Truncate at the end, required for multi-byte chars. */
997 ccline.cmdbuff[ccline.cmdlen] = NUL;
998 redrawcmd();
999 }
1000 else if (ccline.cmdlen == 0 && c != Ctrl_W
1001 && ccline.cmdprompt == NULL && indent == 0)
1002 {
1003 /* In ex and debug mode it doesn't make sense to return. */
1004 if (exmode_active
1005#ifdef FEAT_EVAL
1006 || ccline.cmdfirstc == '>'
1007#endif
1008 )
1009 goto cmdline_not_changed;
1010
1011 vim_free(ccline.cmdbuff); /* no commandline to return */
1012 ccline.cmdbuff = NULL;
1013 if (!cmd_silent)
1014 {
1015#ifdef FEAT_RIGHTLEFT
1016 if (cmdmsg_rl)
1017 msg_col = Columns;
1018 else
1019#endif
1020 msg_col = 0;
1021 msg_putchar(' '); /* delete ':' */
1022 }
1023 redraw_cmdline = TRUE;
1024 goto returncmd; /* back to cmd mode */
1025 }
1026 goto cmdline_changed;
1027
1028 case K_INS:
1029 case K_KINS:
1030#ifdef FEAT_FKMAP
1031 /* if Farsi mode set, we are in reverse insert mode -
1032 Do not change the mode */
1033 if (cmd_fkmap)
1034 beep_flush();
1035 else
1036#endif
1037 ccline.overstrike = !ccline.overstrike;
1038#ifdef CURSOR_SHAPE
1039 ui_cursor_shape(); /* may show different cursor shape */
1040#endif
1041 goto cmdline_not_changed;
1042
1043 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001044 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 {
1046 /* ":lmap" mappings exists, toggle use of mappings. */
1047 State ^= LANGMAP;
1048#ifdef USE_IM_CONTROL
1049 im_set_active(FALSE); /* Disable input method */
1050#endif
1051 if (b_im_ptr != NULL)
1052 {
1053 if (State & LANGMAP)
1054 *b_im_ptr = B_IMODE_LMAP;
1055 else
1056 *b_im_ptr = B_IMODE_NONE;
1057 }
1058 }
1059#ifdef USE_IM_CONTROL
1060 else
1061 {
1062 /* There are no ":lmap" mappings, toggle IM. When
1063 * 'imdisable' is set don't try getting the status, it's
1064 * always off. */
1065 if ((p_imdisable && b_im_ptr != NULL)
1066 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1067 {
1068 im_set_active(FALSE); /* Disable input method */
1069 if (b_im_ptr != NULL)
1070 *b_im_ptr = B_IMODE_NONE;
1071 }
1072 else
1073 {
1074 im_set_active(TRUE); /* Enable input method */
1075 if (b_im_ptr != NULL)
1076 *b_im_ptr = B_IMODE_IM;
1077 }
1078 }
1079#endif
1080 if (b_im_ptr != NULL)
1081 {
1082 if (b_im_ptr == &curbuf->b_p_iminsert)
1083 set_iminsert_global();
1084 else
1085 set_imsearch_global();
1086 }
1087#ifdef CURSOR_SHAPE
1088 ui_cursor_shape(); /* may show different cursor shape */
1089#endif
1090#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1091 /* Show/unshow value of 'keymap' in status lines later. */
1092 status_redraw_curbuf();
1093#endif
1094 goto cmdline_not_changed;
1095
1096/* case '@': only in very old vi */
1097 case Ctrl_U:
1098 /* delete all characters left of the cursor */
1099 j = ccline.cmdpos;
1100 ccline.cmdlen -= j;
1101 i = ccline.cmdpos = 0;
1102 while (i < ccline.cmdlen)
1103 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1104 /* Truncate at the end, required for multi-byte chars. */
1105 ccline.cmdbuff[ccline.cmdlen] = NUL;
1106 redrawcmd();
1107 goto cmdline_changed;
1108
1109#ifdef FEAT_CLIPBOARD
1110 case Ctrl_Y:
1111 /* Copy the modeless selection, if there is one. */
1112 if (clip_star.state != SELECT_CLEARED)
1113 {
1114 if (clip_star.state == SELECT_DONE)
1115 clip_copy_modeless_selection(TRUE);
1116 goto cmdline_not_changed;
1117 }
1118 break;
1119#endif
1120
1121 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1122 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001123 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001124 * ":normal" runs out of characters. */
1125 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001126 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 goto cmdline_not_changed;
1128
1129 gotesc = TRUE; /* will free ccline.cmdbuff after
1130 putting it in history */
1131 goto returncmd; /* back to cmd mode */
1132
1133 case Ctrl_R: /* insert register */
1134#ifdef USE_ON_FLY_SCROLL
1135 dont_scroll = TRUE; /* disallow scrolling here */
1136#endif
1137 putcmdline('"', TRUE);
1138 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001139 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 if (i == Ctrl_O)
1141 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1142 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001143 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 --no_mapping;
1145#ifdef FEAT_EVAL
1146 /*
1147 * Insert the result of an expression.
1148 * Need to save the current command line, to be able to enter
1149 * a new one...
1150 */
1151 new_cmdpos = -1;
1152 if (c == '=')
1153 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1155 {
1156 beep_flush();
1157 c = ESC;
1158 }
1159 else
1160 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001161 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001163 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165 }
1166#endif
1167 if (c != ESC) /* use ESC to cancel inserting register */
1168 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001169 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001170
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001171#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001172 /* When there was a serious error abort getting the
1173 * command line. */
1174 if (aborting())
1175 {
1176 gotesc = TRUE; /* will free ccline.cmdbuff after
1177 putting it in history */
1178 goto returncmd; /* back to cmd mode */
1179 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 KeyTyped = FALSE; /* Don't do p_wc completion. */
1182#ifdef FEAT_EVAL
1183 if (new_cmdpos >= 0)
1184 {
1185 /* set_cmdline_pos() was used */
1186 if (new_cmdpos > ccline.cmdlen)
1187 ccline.cmdpos = ccline.cmdlen;
1188 else
1189 ccline.cmdpos = new_cmdpos;
1190 }
1191#endif
1192 }
1193 redrawcmd();
1194 goto cmdline_changed;
1195
1196 case Ctrl_D:
1197 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1198 break; /* Use ^D as normal char instead */
1199
1200 redrawcmd();
1201 continue; /* don't do incremental search now */
1202
1203 case K_RIGHT:
1204 case K_S_RIGHT:
1205 case K_C_RIGHT:
1206 do
1207 {
1208 if (ccline.cmdpos >= ccline.cmdlen)
1209 break;
1210 i = cmdline_charsize(ccline.cmdpos);
1211 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1212 break;
1213 ccline.cmdspos += i;
1214#ifdef FEAT_MBYTE
1215 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001216 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 + ccline.cmdpos);
1218 else
1219#endif
1220 ++ccline.cmdpos;
1221 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001222 while ((c == K_S_RIGHT || c == K_C_RIGHT
1223 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1225#ifdef FEAT_MBYTE
1226 if (has_mbyte)
1227 set_cmdspos_cursor();
1228#endif
1229 goto cmdline_not_changed;
1230
1231 case K_LEFT:
1232 case K_S_LEFT:
1233 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001234 if (ccline.cmdpos == 0)
1235 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 do
1237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 --ccline.cmdpos;
1239#ifdef FEAT_MBYTE
1240 if (has_mbyte) /* move to first byte of char */
1241 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1242 ccline.cmdbuff + ccline.cmdpos);
1243#endif
1244 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1245 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001246 while (ccline.cmdpos > 0
1247 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001248 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1250#ifdef FEAT_MBYTE
1251 if (has_mbyte)
1252 set_cmdspos_cursor();
1253#endif
1254 goto cmdline_not_changed;
1255
1256 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001257 /* Ignore mouse event or ex_window() result. */
1258 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259
Bram Moolenaar4770d092006-01-12 23:22:24 +00001260#ifdef FEAT_GUI_W32
1261 /* On Win32 ignore <M-F4>, we get it when closing the window was
1262 * cancelled. */
1263 case K_F4:
1264 if (mod_mask == MOD_MASK_ALT)
1265 {
1266 redrawcmd(); /* somehow the cmdline is cleared */
1267 goto cmdline_not_changed;
1268 }
1269 break;
1270#endif
1271
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272#ifdef FEAT_MOUSE
1273 case K_MIDDLEDRAG:
1274 case K_MIDDLERELEASE:
1275 goto cmdline_not_changed; /* Ignore mouse */
1276
1277 case K_MIDDLEMOUSE:
1278# ifdef FEAT_GUI
1279 /* When GUI is active, also paste when 'mouse' is empty */
1280 if (!gui.in_use)
1281# endif
1282 if (!mouse_has(MOUSE_COMMAND))
1283 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001284# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001286 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001288# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001289 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 redrawcmd();
1291 goto cmdline_changed;
1292
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001293# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001295 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 redrawcmd();
1297 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001298# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
1300 case K_LEFTDRAG:
1301 case K_LEFTRELEASE:
1302 case K_RIGHTDRAG:
1303 case K_RIGHTRELEASE:
1304 /* Ignore drag and release events when the button-down wasn't
1305 * seen before. */
1306 if (ignore_drag_release)
1307 goto cmdline_not_changed;
1308 /* FALLTHROUGH */
1309 case K_LEFTMOUSE:
1310 case K_RIGHTMOUSE:
1311 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1312 ignore_drag_release = TRUE;
1313 else
1314 ignore_drag_release = FALSE;
1315# ifdef FEAT_GUI
1316 /* When GUI is active, also move when 'mouse' is empty */
1317 if (!gui.in_use)
1318# endif
1319 if (!mouse_has(MOUSE_COMMAND))
1320 goto cmdline_not_changed; /* Ignore mouse */
1321# ifdef FEAT_CLIPBOARD
1322 if (mouse_row < cmdline_row && clip_star.available)
1323 {
1324 int button, is_click, is_drag;
1325
1326 /*
1327 * Handle modeless selection.
1328 */
1329 button = get_mouse_button(KEY2TERMCAP1(c),
1330 &is_click, &is_drag);
1331 if (mouse_model_popup() && button == MOUSE_LEFT
1332 && (mod_mask & MOD_MASK_SHIFT))
1333 {
1334 /* Translate shift-left to right button. */
1335 button = MOUSE_RIGHT;
1336 mod_mask &= ~MOD_MASK_SHIFT;
1337 }
1338 clip_modeless(button, is_click, is_drag);
1339 goto cmdline_not_changed;
1340 }
1341# endif
1342
1343 set_cmdspos();
1344 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1345 ++ccline.cmdpos)
1346 {
1347 i = cmdline_charsize(ccline.cmdpos);
1348 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1349 && mouse_col < ccline.cmdspos % Columns + i)
1350 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001351# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (has_mbyte)
1353 {
1354 /* Count ">" for double-wide char that doesn't fit. */
1355 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001356 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 + ccline.cmdpos) - 1;
1358 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001359# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 ccline.cmdspos += i;
1361 }
1362 goto cmdline_not_changed;
1363
1364 /* Mouse scroll wheel: ignored here */
1365 case K_MOUSEDOWN:
1366 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001367 case K_MOUSELEFT:
1368 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 /* Alternate buttons ignored here */
1370 case K_X1MOUSE:
1371 case K_X1DRAG:
1372 case K_X1RELEASE:
1373 case K_X2MOUSE:
1374 case K_X2DRAG:
1375 case K_X2RELEASE:
1376 goto cmdline_not_changed;
1377
1378#endif /* FEAT_MOUSE */
1379
1380#ifdef FEAT_GUI
1381 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1382 case K_LEFTRELEASE_NM:
1383 goto cmdline_not_changed;
1384
1385 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001386 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 {
1388 gui_do_scroll();
1389 redrawcmd();
1390 }
1391 goto cmdline_not_changed;
1392
1393 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001394 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001396 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 redrawcmd();
1398 }
1399 goto cmdline_not_changed;
1400#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001401#ifdef FEAT_GUI_TABLINE
1402 case K_TABLINE:
1403 case K_TABMENU:
1404 /* Don't want to change any tabs here. Make sure the same tab
1405 * is still selected. */
1406 if (gui_use_tabline())
1407 gui_mch_set_curtab(tabpage_index(curtab));
1408 goto cmdline_not_changed;
1409#endif
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 case K_SELECT: /* end of Select mode mapping - ignore */
1412 goto cmdline_not_changed;
1413
1414 case Ctrl_B: /* begin of command line */
1415 case K_HOME:
1416 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 case K_S_HOME:
1418 case K_C_HOME:
1419 ccline.cmdpos = 0;
1420 set_cmdspos();
1421 goto cmdline_not_changed;
1422
1423 case Ctrl_E: /* end of command line */
1424 case K_END:
1425 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 case K_S_END:
1427 case K_C_END:
1428 ccline.cmdpos = ccline.cmdlen;
1429 set_cmdspos_cursor();
1430 goto cmdline_not_changed;
1431
1432 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001433 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 break;
1435 goto cmdline_changed;
1436
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001437 case Ctrl_L:
1438#ifdef FEAT_SEARCH_EXTRA
1439 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1440 {
1441 /* Add a character from under the cursor for 'incsearch' */
1442 if (did_incsearch
1443 && !equalpos(curwin->w_cursor, old_cursor))
1444 {
1445 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001446 /* If 'ignorecase' and 'smartcase' are set and the
1447 * command line has no uppercase characters, convert
1448 * the character to lowercase */
1449 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1450 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001451 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001452 {
1453 if (c == firstc || vim_strchr((char_u *)(
1454 p_magic ? "\\^$.*[" : "\\^$"), c)
1455 != NULL)
1456 {
1457 /* put a backslash before special characters */
1458 stuffcharReadbuff(c);
1459 c = '\\';
1460 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001461 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001462 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001463 }
1464 goto cmdline_not_changed;
1465 }
1466#endif
1467
1468 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001469 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 break;
1471 goto cmdline_changed;
1472
1473 case Ctrl_N: /* next match */
1474 case Ctrl_P: /* previous match */
1475 if (xpc.xp_numfiles > 0)
1476 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001477 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1478 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 break;
1480 goto cmdline_changed;
1481 }
1482
1483#ifdef FEAT_CMDHIST
1484 case K_UP:
1485 case K_DOWN:
1486 case K_S_UP:
1487 case K_S_DOWN:
1488 case K_PAGEUP:
1489 case K_KPAGEUP:
1490 case K_PAGEDOWN:
1491 case K_KPAGEDOWN:
1492 if (hislen == 0 || firstc == NUL) /* no history */
1493 goto cmdline_not_changed;
1494
1495 i = hiscnt;
1496
1497 /* save current command string so it can be restored later */
1498 if (lookfor == NULL)
1499 {
1500 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1501 goto cmdline_not_changed;
1502 lookfor[ccline.cmdpos] = NUL;
1503 }
1504
1505 j = (int)STRLEN(lookfor);
1506 for (;;)
1507 {
1508 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001509 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001510 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 {
1512 if (hiscnt == hislen) /* first time */
1513 hiscnt = hisidx[histype];
1514 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1515 hiscnt = hislen - 1;
1516 else if (hiscnt != hisidx[histype] + 1)
1517 --hiscnt;
1518 else /* at top of list */
1519 {
1520 hiscnt = i;
1521 break;
1522 }
1523 }
1524 else /* one step forwards */
1525 {
1526 /* on last entry, clear the line */
1527 if (hiscnt == hisidx[histype])
1528 {
1529 hiscnt = hislen;
1530 break;
1531 }
1532
1533 /* not on a history line, nothing to do */
1534 if (hiscnt == hislen)
1535 break;
1536 if (hiscnt == hislen - 1) /* wrap around */
1537 hiscnt = 0;
1538 else
1539 ++hiscnt;
1540 }
1541 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1542 {
1543 hiscnt = i;
1544 break;
1545 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001546 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001547 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 || STRNCMP(history[histype][hiscnt].hisstr,
1549 lookfor, (size_t)j) == 0)
1550 break;
1551 }
1552
1553 if (hiscnt != i) /* jumped to other entry */
1554 {
1555 char_u *p;
1556 int len;
1557 int old_firstc;
1558
1559 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001560 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (hiscnt == hislen)
1562 p = lookfor; /* back to the old one */
1563 else
1564 p = history[histype][hiscnt].hisstr;
1565
1566 if (histype == HIST_SEARCH
1567 && p != lookfor
1568 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1569 {
1570 /* Correct for the separator character used when
1571 * adding the history entry vs the one used now.
1572 * First loop: count length.
1573 * Second loop: copy the characters. */
1574 for (i = 0; i <= 1; ++i)
1575 {
1576 len = 0;
1577 for (j = 0; p[j] != NUL; ++j)
1578 {
1579 /* Replace old sep with new sep, unless it is
1580 * escaped. */
1581 if (p[j] == old_firstc
1582 && (j == 0 || p[j - 1] != '\\'))
1583 {
1584 if (i > 0)
1585 ccline.cmdbuff[len] = firstc;
1586 }
1587 else
1588 {
1589 /* Escape new sep, unless it is already
1590 * escaped. */
1591 if (p[j] == firstc
1592 && (j == 0 || p[j - 1] != '\\'))
1593 {
1594 if (i > 0)
1595 ccline.cmdbuff[len] = '\\';
1596 ++len;
1597 }
1598 if (i > 0)
1599 ccline.cmdbuff[len] = p[j];
1600 }
1601 ++len;
1602 }
1603 if (i == 0)
1604 {
1605 alloc_cmdbuff(len);
1606 if (ccline.cmdbuff == NULL)
1607 goto returncmd;
1608 }
1609 }
1610 ccline.cmdbuff[len] = NUL;
1611 }
1612 else
1613 {
1614 alloc_cmdbuff((int)STRLEN(p));
1615 if (ccline.cmdbuff == NULL)
1616 goto returncmd;
1617 STRCPY(ccline.cmdbuff, p);
1618 }
1619
1620 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1621 redrawcmd();
1622 goto cmdline_changed;
1623 }
1624 beep_flush();
1625 goto cmdline_not_changed;
1626#endif
1627
1628 case Ctrl_V:
1629 case Ctrl_Q:
1630#ifdef FEAT_MOUSE
1631 ignore_drag_release = TRUE;
1632#endif
1633 putcmdline('^', TRUE);
1634 c = get_literal(); /* get next (two) character(s) */
1635 do_abbr = FALSE; /* don't do abbreviation now */
1636#ifdef FEAT_MBYTE
1637 /* may need to remove ^ when composing char was typed */
1638 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1639 {
1640 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1641 msg_putchar(' ');
1642 cursorcmd();
1643 }
1644#endif
1645 break;
1646
1647#ifdef FEAT_DIGRAPHS
1648 case Ctrl_K:
1649#ifdef FEAT_MOUSE
1650 ignore_drag_release = TRUE;
1651#endif
1652 putcmdline('?', TRUE);
1653#ifdef USE_ON_FLY_SCROLL
1654 dont_scroll = TRUE; /* disallow scrolling here */
1655#endif
1656 c = get_digraph(TRUE);
1657 if (c != NUL)
1658 break;
1659
1660 redrawcmd();
1661 goto cmdline_not_changed;
1662#endif /* FEAT_DIGRAPHS */
1663
1664#ifdef FEAT_RIGHTLEFT
1665 case Ctrl__: /* CTRL-_: switch language mode */
1666 if (!p_ari)
1667 break;
1668#ifdef FEAT_FKMAP
1669 if (p_altkeymap)
1670 {
1671 cmd_fkmap = !cmd_fkmap;
1672 if (cmd_fkmap) /* in Farsi always in Insert mode */
1673 ccline.overstrike = FALSE;
1674 }
1675 else /* Hebrew is default */
1676#endif
1677 cmd_hkmap = !cmd_hkmap;
1678 goto cmdline_not_changed;
1679#endif
1680
1681 default:
1682#ifdef UNIX
1683 if (c == intr_char)
1684 {
1685 gotesc = TRUE; /* will free ccline.cmdbuff after
1686 putting it in history */
1687 goto returncmd; /* back to Normal mode */
1688 }
1689#endif
1690 /*
1691 * Normal character with no special meaning. Just set mod_mask
1692 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1693 * the string <S-Space>. This should only happen after ^V.
1694 */
1695 if (!IS_SPECIAL(c))
1696 mod_mask = 0x0;
1697 break;
1698 }
1699 /*
1700 * End of switch on command line character.
1701 * We come here if we have a normal character.
1702 */
1703
Bram Moolenaarede3e632013-06-23 16:16:19 +02001704 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705#ifdef FEAT_MBYTE
1706 /* Add ABBR_OFF for characters above 0x100, this is
1707 * what check_abbr() expects. */
1708 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1709#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001710 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 goto cmdline_changed;
1712
1713 /*
1714 * put the character in the command line
1715 */
1716 if (IS_SPECIAL(c) || mod_mask != 0)
1717 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1718 else
1719 {
1720#ifdef FEAT_MBYTE
1721 if (has_mbyte)
1722 {
1723 j = (*mb_char2bytes)(c, IObuff);
1724 IObuff[j] = NUL; /* exclude composing chars */
1725 put_on_cmdline(IObuff, j, TRUE);
1726 }
1727 else
1728#endif
1729 {
1730 IObuff[0] = c;
1731 put_on_cmdline(IObuff, 1, TRUE);
1732 }
1733 }
1734 goto cmdline_changed;
1735
1736/*
1737 * This part implements incremental searches for "/" and "?"
1738 * Jump to cmdline_not_changed when a character has been read but the command
1739 * line did not change. Then we only search and redraw if something changed in
1740 * the past.
1741 * Jump to cmdline_changed when the command line did change.
1742 * (Sorry for the goto's, I know it is ugly).
1743 */
1744cmdline_not_changed:
1745#ifdef FEAT_SEARCH_EXTRA
1746 if (!incsearch_postponed)
1747 continue;
1748#endif
1749
1750cmdline_changed:
1751#ifdef FEAT_SEARCH_EXTRA
1752 /*
1753 * 'incsearch' highlighting.
1754 */
1755 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1756 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001757 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001758#ifdef FEAT_RELTIME
1759 proftime_T tm;
1760#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 /* if there is a character waiting, search and redraw later */
1763 if (char_avail())
1764 {
1765 incsearch_postponed = TRUE;
1766 continue;
1767 }
1768 incsearch_postponed = FALSE;
1769 curwin->w_cursor = old_cursor; /* start at old position */
1770
1771 /* If there is no command line, don't do anything */
1772 if (ccline.cmdlen == 0)
1773 i = 0;
1774 else
1775 {
1776 cursor_off(); /* so the user knows we're busy */
1777 out_flush();
1778 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001779#ifdef FEAT_RELTIME
1780 /* Set the time limit to half a second. */
1781 profile_setlimit(500L, &tm);
1782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001784 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1785#ifdef FEAT_RELTIME
1786 &tm
1787#else
1788 NULL
1789#endif
1790 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 --emsg_off;
1792 /* if interrupted while searching, behave like it failed */
1793 if (got_int)
1794 {
1795 (void)vpeekc(); /* remove <C-C> from input stream */
1796 got_int = FALSE; /* don't abandon the command line */
1797 i = 0;
1798 }
1799 else if (char_avail())
1800 /* cancelled searching because a char was typed */
1801 incsearch_postponed = TRUE;
1802 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001803 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 highlight_match = TRUE; /* highlight position */
1805 else
1806 highlight_match = FALSE; /* remove highlight */
1807
1808 /* first restore the old curwin values, so the screen is
1809 * positioned in the same way as the actual search command */
1810 curwin->w_leftcol = old_leftcol;
1811 curwin->w_topline = old_topline;
1812# ifdef FEAT_DIFF
1813 curwin->w_topfill = old_topfill;
1814# endif
1815 curwin->w_botline = old_botline;
1816 changed_cline_bef_curs();
1817 update_topline();
1818
1819 if (i != 0)
1820 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001821 pos_T save_pos = curwin->w_cursor;
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001824 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 * moves the whole match onto the screen when 'nowrap' is set.
1826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 curwin->w_cursor.lnum += search_match_lines;
1828 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001829 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1830 {
1831 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1832 coladvance((colnr_T)MAXCOL);
1833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001835 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001836 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001838 else
1839 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001842# ifdef FEAT_WINDOWS
1843 /* May redraw the status line to show the cursor position. */
1844 if (p_ru && curwin->w_status_height > 0)
1845 curwin->w_redr_status = TRUE;
1846# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001848 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001849 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001850 restore_cmdline(&save_ccline);
1851
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001852 /* Leave it at the end to make CTRL-R CTRL-W work. */
1853 if (i != 0)
1854 curwin->w_cursor = end_pos;
1855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 msg_starthere();
1857 redrawcmdline();
1858 did_incsearch = TRUE;
1859 }
1860#else /* FEAT_SEARCH_EXTRA */
1861 ;
1862#endif
1863
1864#ifdef FEAT_RIGHTLEFT
1865 if (cmdmsg_rl
1866# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001867 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868# endif
1869 )
1870 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001871 * right-left typing. Not efficient, but it works.
1872 * Do it only when there are no characters left to read
1873 * to avoid useless intermediate redraws. */
1874 if (vpeekc() == NUL)
1875 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#endif
1877 }
1878
1879returncmd:
1880
1881#ifdef FEAT_RIGHTLEFT
1882 cmdmsg_rl = FALSE;
1883#endif
1884
1885#ifdef FEAT_FKMAP
1886 cmd_fkmap = 0;
1887#endif
1888
1889 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001890 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891
1892#ifdef FEAT_SEARCH_EXTRA
1893 if (did_incsearch)
1894 {
1895 curwin->w_cursor = old_cursor;
1896 curwin->w_curswant = old_curswant;
1897 curwin->w_leftcol = old_leftcol;
1898 curwin->w_topline = old_topline;
1899# ifdef FEAT_DIFF
1900 curwin->w_topfill = old_topfill;
1901# endif
1902 curwin->w_botline = old_botline;
1903 highlight_match = FALSE;
1904 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001905 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 }
1907#endif
1908
1909 if (ccline.cmdbuff != NULL)
1910 {
1911 /*
1912 * Put line in history buffer (":" and "=" only when it was typed).
1913 */
1914#ifdef FEAT_CMDHIST
1915 if (ccline.cmdlen && firstc != NUL
1916 && (some_key_typed || histype == HIST_SEARCH))
1917 {
1918 add_to_history(histype, ccline.cmdbuff, TRUE,
1919 histype == HIST_SEARCH ? firstc : NUL);
1920 if (firstc == ':')
1921 {
1922 vim_free(new_last_cmdline);
1923 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1924 }
1925 }
1926#endif
1927
1928 if (gotesc) /* abandon command line */
1929 {
1930 vim_free(ccline.cmdbuff);
1931 ccline.cmdbuff = NULL;
1932 if (msg_scrolled == 0)
1933 compute_cmdrow();
1934 MSG("");
1935 redraw_cmdline = TRUE;
1936 }
1937 }
1938
1939 /*
1940 * If the screen was shifted up, redraw the whole screen (later).
1941 * If the line is too long, clear it, so ruler and shown command do
1942 * not get printed in the middle of it.
1943 */
1944 msg_check();
1945 msg_scroll = save_msg_scroll;
1946 redir_off = FALSE;
1947
1948 /* When the command line was typed, no need for a wait-return prompt. */
1949 if (some_key_typed)
1950 need_wait_return = FALSE;
1951
1952 State = save_State;
1953#ifdef USE_IM_CONTROL
1954 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1955 im_save_status(b_im_ptr);
1956 im_set_active(FALSE);
1957#endif
1958#ifdef FEAT_MOUSE
1959 setmouse();
1960#endif
1961#ifdef CURSOR_SHAPE
1962 ui_cursor_shape(); /* may show different cursor shape */
1963#endif
1964
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001965 {
1966 char_u *p = ccline.cmdbuff;
1967
1968 /* Make ccline empty, getcmdline() may try to use it. */
1969 ccline.cmdbuff = NULL;
1970 return p;
1971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972}
1973
1974#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1975/*
1976 * Get a command line with a prompt.
1977 * This is prepared to be called recursively from getcmdline() (e.g. by
1978 * f_input() when evaluating an expression from CTRL-R =).
1979 * Returns the command line in allocated memory, or NULL.
1980 */
1981 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001982getcmdline_prompt(
1983 int firstc,
1984 char_u *prompt, /* command line prompt */
1985 int attr, /* attributes for prompt */
1986 int xp_context, /* type of expansion */
1987 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988{
1989 char_u *s;
1990 struct cmdline_info save_ccline;
1991 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01001992 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001994 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 ccline.cmdprompt = prompt;
1996 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001997# ifdef FEAT_EVAL
1998 ccline.xp_context = xp_context;
1999 ccline.xp_arg = xp_arg;
2000 ccline.input_fn = (firstc == '@');
2001# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002002 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002004 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002005 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002006 /* Restore msg_col, the prompt from input() may have changed it.
2007 * But only if called recursively and the commandline is therefore being
2008 * restored to an old one; if not, the input() prompt stays on the screen,
2009 * so we need its modified msg_col left intact. */
2010 if (ccline.cmdbuff != NULL)
2011 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012
2013 return s;
2014}
2015#endif
2016
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002017/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002018 * Return TRUE when the text must not be changed and we can't switch to
2019 * another window or buffer. Used when editing the command line, evaluating
2020 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002021 */
2022 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002023text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002024{
2025#ifdef FEAT_CMDWIN
2026 if (cmdwin_type != 0)
2027 return TRUE;
2028#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002029 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002030}
2031
2032/*
2033 * Give an error message for a command that isn't allowed while the cmdline
2034 * window is open or editing the cmdline in another way.
2035 */
2036 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002037text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002038{
2039#ifdef FEAT_CMDWIN
2040 if (cmdwin_type != 0)
2041 EMSG(_(e_cmdwin));
2042 else
2043#endif
2044 EMSG(_(e_secure));
2045}
2046
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002047#if defined(FEAT_AUTOCMD) || defined(PROTO)
2048/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002049 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2050 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002051 */
2052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002053curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054{
2055 if (curbuf_lock > 0)
2056 {
2057 EMSG(_("E788: Not allowed to edit another buffer now"));
2058 return TRUE;
2059 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002060 return allbuf_locked();
2061}
2062
2063/*
2064 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2065 * message.
2066 */
2067 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002068allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002069{
2070 if (allbuf_lock > 0)
2071 {
2072 EMSG(_("E811: Not allowed to change buffer information now"));
2073 return TRUE;
2074 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002075 return FALSE;
2076}
2077#endif
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002080cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081{
2082#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2083 if (cmdline_star > 0) /* showing '*', always 1 position */
2084 return 1;
2085#endif
2086 return ptr2cells(ccline.cmdbuff + idx);
2087}
2088
2089/*
2090 * Compute the offset of the cursor on the command line for the prompt and
2091 * indent.
2092 */
2093 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002094set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002096 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 ccline.cmdspos = 1 + ccline.cmdindent;
2098 else
2099 ccline.cmdspos = 0 + ccline.cmdindent;
2100}
2101
2102/*
2103 * Compute the screen position for the cursor on the command line.
2104 */
2105 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002106set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107{
2108 int i, m, c;
2109
2110 set_cmdspos();
2111 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002112 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002114 if (m < 0) /* overflow, Columns or Rows at weird value */
2115 m = MAXCOL;
2116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 else
2118 m = MAXCOL;
2119 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2120 {
2121 c = cmdline_charsize(i);
2122#ifdef FEAT_MBYTE
2123 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2124 if (has_mbyte)
2125 correct_cmdspos(i, c);
2126#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002127 /* If the cmdline doesn't fit, show cursor on last visible char.
2128 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 if ((ccline.cmdspos += c) >= m)
2130 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 ccline.cmdspos -= c;
2132 break;
2133 }
2134#ifdef FEAT_MBYTE
2135 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002136 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
2138 }
2139}
2140
2141#ifdef FEAT_MBYTE
2142/*
2143 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2144 * character that doesn't fit, so that a ">" must be displayed.
2145 */
2146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002147correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002149 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2151 && ccline.cmdspos % Columns + cells > Columns)
2152 ccline.cmdspos++;
2153}
2154#endif
2155
2156/*
2157 * Get an Ex command line for the ":" command.
2158 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002160getexline(
2161 int c, /* normally ':', NUL for ":append" */
2162 void *cookie UNUSED,
2163 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 /* When executing a register, remove ':' that's in front of each line. */
2166 if (exec_from_reg && vpeekc() == ':')
2167 (void)vgetc();
2168 return getcmdline(c, 1L, indent);
2169}
2170
2171/*
2172 * Get an Ex command line for Ex mode.
2173 * In Ex mode we only use the OS supplied line editing features and no
2174 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002175 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002178getexmodeline(
2179 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002180 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002181 void *cookie UNUSED,
2182 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002184 garray_T line_ga;
2185 char_u *pend;
2186 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002187 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002188 int escaped = FALSE; /* CTRL-V typed */
2189 int vcol = 0;
2190 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002191 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002192 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193
2194 /* Switch cursor on now. This avoids that it happens after the "\n", which
2195 * confuses the system function that computes tabstops. */
2196 cursor_on();
2197
2198 /* always start in column 0; write a newline if necessary */
2199 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002200 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002202 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002204 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002205 if (p_prompt)
2206 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 while (indent-- > 0)
2208 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 }
2211
2212 ga_init2(&line_ga, 1, 30);
2213
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002214 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002215 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002216 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002217 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002218 while (indent >= 8)
2219 {
2220 ga_append(&line_ga, TAB);
2221 msg_puts((char_u *)" ");
2222 indent -= 8;
2223 }
2224 while (indent-- > 0)
2225 {
2226 ga_append(&line_ga, ' ');
2227 msg_putchar(' ');
2228 }
2229 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002230 ++no_mapping;
2231 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002232
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 /*
2234 * Get the line, one character at a time.
2235 */
2236 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002237 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002239 long sw;
2240 char_u *s;
2241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 if (ga_grow(&line_ga, 40) == FAIL)
2243 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002245 /* Get one character at a time. Don't use inchar(), it can't handle
2246 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002247 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002248 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
2250 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002251 * Handle line editing.
2252 * Previously this was left to the system, putting the terminal in
2253 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002255 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002257 msg_putchar('\n');
2258 break;
2259 }
2260
2261 if (!escaped)
2262 {
2263 /* CR typed means "enter", which is NL */
2264 if (c1 == '\r')
2265 c1 = '\n';
2266
2267 if (c1 == BS || c1 == K_BS
2268 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002270 if (line_ga.ga_len > 0)
2271 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002272#ifdef FEAT_MBYTE
2273 if (has_mbyte)
2274 {
2275 p = (char_u *)line_ga.ga_data;
2276 p[line_ga.ga_len] = NUL;
2277 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2278 line_ga.ga_len -= len;
2279 }
2280 else
2281#endif
2282 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002283 goto redraw;
2284 }
2285 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002288 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002290 msg_col = startcol;
2291 msg_clr_eos();
2292 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002293 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002294 }
2295
2296 if (c1 == Ctrl_T)
2297 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002298 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002299 p = (char_u *)line_ga.ga_data;
2300 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002301 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002302 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002303add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002304 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002306 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002307 p = (char_u *)line_ga.ga_data;
2308 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002309 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2310 *s = ' ';
2311 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002313redraw:
2314 /* redraw the line */
2315 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002316 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002317 p = (char_u *)line_ga.ga_data;
2318 p[line_ga.ga_len] = NUL;
2319 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002321 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002323 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002325 msg_putchar(' ');
2326 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002327 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002329 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002331 len = MB_PTR2LEN(p);
2332 msg_outtrans_len(p, len);
2333 vcol += ptr2cells(p);
2334 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002337 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002338 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002339 continue;
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002342 if (c1 == Ctrl_D)
2343 {
2344 /* Delete one shiftwidth. */
2345 p = (char_u *)line_ga.ga_data;
2346 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002348 if (prev_char == '^')
2349 ex_keep_indent = TRUE;
2350 indent = 0;
2351 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 }
2353 else
2354 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002355 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002356 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002357 if (indent > 0)
2358 {
2359 --indent;
2360 indent -= indent % get_sw_value(curbuf);
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002363 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002364 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002365 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002366 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2367 --line_ga.ga_len;
2368 }
2369 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002371
2372 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2373 {
2374 escaped = TRUE;
2375 continue;
2376 }
2377
2378 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2379 if (IS_SPECIAL(c1))
2380 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002382
2383 if (IS_SPECIAL(c1))
2384 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002385#ifdef FEAT_MBYTE
2386 if (has_mbyte)
2387 len = (*mb_char2bytes)(c1,
2388 (char_u *)line_ga.ga_data + line_ga.ga_len);
2389 else
2390#endif
2391 {
2392 len = 1;
2393 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2394 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002395 if (c1 == '\n')
2396 msg_putchar('\n');
2397 else if (c1 == TAB)
2398 {
2399 /* Don't use chartabsize(), 'ts' can be different */
2400 do
2401 {
2402 msg_putchar(' ');
2403 } while (++vcol % 8);
2404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002407 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002408 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002409 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002411 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002412 escaped = FALSE;
2413
2414 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002415 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002416
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002417 /* We are done when a NL is entered, but not when it comes after an
2418 * odd number of backslashes, that results in a NUL. */
2419 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002421 int bcount = 0;
2422
2423 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2424 ++bcount;
2425
2426 if (bcount > 0)
2427 {
2428 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2429 * "\NL", etc. */
2430 line_ga.ga_len -= (bcount + 1) / 2;
2431 pend -= (bcount + 1) / 2;
2432 pend[-1] = '\n';
2433 }
2434
2435 if ((bcount & 1) == 0)
2436 {
2437 --line_ga.ga_len;
2438 --pend;
2439 *pend = NUL;
2440 break;
2441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 }
2443 }
2444
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002445 --no_mapping;
2446 --allow_keys;
2447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 /* make following messages go to the next line */
2449 msg_didout = FALSE;
2450 msg_col = 0;
2451 if (msg_row < Rows - 1)
2452 ++msg_row;
2453 emsg_on_display = FALSE; /* don't want ui_delay() */
2454
2455 if (got_int)
2456 ga_clear(&line_ga);
2457
2458 return (char_u *)line_ga.ga_data;
2459}
2460
Bram Moolenaare344bea2005-09-01 20:46:49 +00002461# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2462 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463/*
2464 * Return TRUE if ccline.overstrike is on.
2465 */
2466 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002467cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 return ccline.overstrike;
2470}
2471
2472/*
2473 * Return TRUE if the cursor is at the end of the cmdline.
2474 */
2475 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002476cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 return (ccline.cmdpos >= ccline.cmdlen);
2479}
2480#endif
2481
Bram Moolenaar9372a112005-12-06 19:59:18 +00002482#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483/*
2484 * Return the virtual column number at the current cursor position.
2485 * This is used by the IM code to obtain the start of the preedit string.
2486 */
2487 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002488cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
2490 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2491 return MAXCOL;
2492
2493# ifdef FEAT_MBYTE
2494 if (has_mbyte)
2495 {
2496 colnr_T col;
2497 int i = 0;
2498
2499 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002500 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 return col;
2503 }
2504 else
2505# endif
2506 return ccline.cmdpos;
2507}
2508#endif
2509
2510#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2511/*
2512 * If part of the command line is an IM preedit string, redraw it with
2513 * IM feedback attributes. The cursor position is restored after drawing.
2514 */
2515 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002516redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517{
2518 if ((State & CMDLINE)
2519 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002520 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 && !p_imdisable
2522 && im_is_preediting())
2523 {
2524 int cmdpos = 0;
2525 int cmdspos;
2526 int old_row;
2527 int old_col;
2528 colnr_T col;
2529
2530 old_row = msg_row;
2531 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002532 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
2534# ifdef FEAT_MBYTE
2535 if (has_mbyte)
2536 {
2537 for (col = 0; col < preedit_start_col
2538 && cmdpos < ccline.cmdlen; ++col)
2539 {
2540 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002541 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
2543 }
2544 else
2545# endif
2546 {
2547 cmdspos += preedit_start_col;
2548 cmdpos += preedit_start_col;
2549 }
2550
2551 msg_row = cmdline_row + (cmdspos / (int)Columns);
2552 msg_col = cmdspos % (int)Columns;
2553 if (msg_row >= Rows)
2554 msg_row = Rows - 1;
2555
2556 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2557 {
2558 int char_len;
2559 int char_attr;
2560
2561 char_attr = im_get_feedback_attr(col);
2562 if (char_attr < 0)
2563 break; /* end of preedit string */
2564
2565# ifdef FEAT_MBYTE
2566 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002567 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 else
2569# endif
2570 char_len = 1;
2571
2572 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2573 cmdpos += char_len;
2574 }
2575
2576 msg_row = old_row;
2577 msg_col = old_col;
2578 }
2579}
2580#endif /* FEAT_XIM && FEAT_GUI_GTK */
2581
2582/*
2583 * Allocate a new command line buffer.
2584 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2585 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2586 */
2587 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002588alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590 /*
2591 * give some extra space to avoid having to allocate all the time
2592 */
2593 if (len < 80)
2594 len = 100;
2595 else
2596 len += 20;
2597
2598 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2599 ccline.cmdbufflen = len;
2600}
2601
2602/*
2603 * Re-allocate the command line to length len + something extra.
2604 * return FAIL for failure, OK otherwise
2605 */
2606 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002607realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 char_u *p;
2610
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002611 if (len < ccline.cmdbufflen)
2612 return OK; /* no need to resize */
2613
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 p = ccline.cmdbuff;
2615 alloc_cmdbuff(len); /* will get some more */
2616 if (ccline.cmdbuff == NULL) /* out of memory */
2617 {
2618 ccline.cmdbuff = p; /* keep the old one */
2619 return FAIL;
2620 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002621 /* There isn't always a NUL after the command, but it may need to be
2622 * there, thus copy up to the NUL and add a NUL. */
2623 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2624 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002626
2627 if (ccline.xpc != NULL
2628 && ccline.xpc->xp_pattern != NULL
2629 && ccline.xpc->xp_context != EXPAND_NOTHING
2630 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2631 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002632 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002633
2634 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2635 * to point into the newly allocated memory. */
2636 if (i >= 0 && i <= ccline.cmdlen)
2637 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2638 }
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 return OK;
2641}
2642
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002643#if defined(FEAT_ARABIC) || defined(PROTO)
2644static char_u *arshape_buf = NULL;
2645
2646# if defined(EXITFREE) || defined(PROTO)
2647 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002648free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002649{
2650 vim_free(arshape_buf);
2651}
2652# endif
2653#endif
2654
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655/*
2656 * Draw part of the cmdline at the current cursor position. But draw stars
2657 * when cmdline_star is TRUE.
2658 */
2659 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002660draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2663 int i;
2664
2665 if (cmdline_star > 0)
2666 for (i = 0; i < len; ++i)
2667 {
2668 msg_putchar('*');
2669# ifdef FEAT_MBYTE
2670 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002671 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672# endif
2673 }
2674 else
2675#endif
2676#ifdef FEAT_ARABIC
2677 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 static int buflen = 0;
2680 char_u *p;
2681 int j;
2682 int newlen = 0;
2683 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002684 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 int prev_c = 0;
2686 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002687 int u8c;
2688 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 /*
2692 * Do arabic shaping into a temporary buffer. This is very
2693 * inefficient!
2694 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002695 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
2697 /* Re-allocate the buffer. We keep it around to avoid a lot of
2698 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002699 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002700 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002701 arshape_buf = alloc(buflen);
2702 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return; /* out of memory */
2704 }
2705
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002706 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2707 {
2708 /* Prepend a space to draw the leading composing char on. */
2709 arshape_buf[0] = ' ';
2710 newlen = 1;
2711 }
2712
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 for (j = start; j < start + len; j += mb_l)
2714 {
2715 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002716 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002717 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (ARABIC_CHAR(u8c))
2719 {
2720 /* Do Arabic shaping. */
2721 if (cmdmsg_rl)
2722 {
2723 /* displaying from right to left */
2724 pc = prev_c;
2725 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002726 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 if (j + mb_l >= start + len)
2728 nc = NUL;
2729 else
2730 nc = utf_ptr2char(p + mb_l);
2731 }
2732 else
2733 {
2734 /* displaying from left to right */
2735 if (j + mb_l >= start + len)
2736 pc = NUL;
2737 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002738 {
2739 int pcc[MAX_MCO];
2740
2741 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002743 pc1 = pcc[0];
2744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 nc = prev_c;
2746 }
2747 prev_c = u8c;
2748
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002749 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002751 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002752 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002754 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2755 if (u8cc[1] != 0)
2756 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002757 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 }
2759 }
2760 else
2761 {
2762 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002763 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 newlen += mb_l;
2765 }
2766 }
2767
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002768 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 else
2771#endif
2772 msg_outtrans_len(ccline.cmdbuff + start, len);
2773}
2774
2775/*
2776 * Put a character on the command line. Shifts the following text to the
2777 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2778 * "c" must be printable (fit in one display cell)!
2779 */
2780 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002781putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
2783 if (cmd_silent)
2784 return;
2785 msg_no_more = TRUE;
2786 msg_putchar(c);
2787 if (shift)
2788 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2789 msg_no_more = FALSE;
2790 cursorcmd();
2791}
2792
2793/*
2794 * Undo a putcmdline(c, FALSE).
2795 */
2796 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002797unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798{
2799 if (cmd_silent)
2800 return;
2801 msg_no_more = TRUE;
2802 if (ccline.cmdlen == ccline.cmdpos)
2803 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002804#ifdef FEAT_MBYTE
2805 else if (has_mbyte)
2806 draw_cmdline(ccline.cmdpos,
2807 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2808#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 else
2810 draw_cmdline(ccline.cmdpos, 1);
2811 msg_no_more = FALSE;
2812 cursorcmd();
2813}
2814
2815/*
2816 * Put the given string, of the given length, onto the command line.
2817 * If len is -1, then STRLEN() is used to calculate the length.
2818 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2819 * part will be redrawn, otherwise it will not. If this function is called
2820 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2821 * called afterwards.
2822 */
2823 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002824put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825{
2826 int retval;
2827 int i;
2828 int m;
2829 int c;
2830
2831 if (len < 0)
2832 len = (int)STRLEN(str);
2833
2834 /* Check if ccline.cmdbuff needs to be longer */
2835 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002836 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 else
2838 retval = OK;
2839 if (retval == OK)
2840 {
2841 if (!ccline.overstrike)
2842 {
2843 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2844 ccline.cmdbuff + ccline.cmdpos,
2845 (size_t)(ccline.cmdlen - ccline.cmdpos));
2846 ccline.cmdlen += len;
2847 }
2848 else
2849 {
2850#ifdef FEAT_MBYTE
2851 if (has_mbyte)
2852 {
2853 /* Count nr of characters in the new string. */
2854 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002855 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 ++m;
2857 /* Count nr of bytes in cmdline that are overwritten by these
2858 * characters. */
2859 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002860 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 --m;
2862 if (i < ccline.cmdlen)
2863 {
2864 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2865 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2866 ccline.cmdlen += ccline.cmdpos + len - i;
2867 }
2868 else
2869 ccline.cmdlen = ccline.cmdpos + len;
2870 }
2871 else
2872#endif
2873 if (ccline.cmdpos + len > ccline.cmdlen)
2874 ccline.cmdlen = ccline.cmdpos + len;
2875 }
2876 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2877 ccline.cmdbuff[ccline.cmdlen] = NUL;
2878
2879#ifdef FEAT_MBYTE
2880 if (enc_utf8)
2881 {
2882 /* When the inserted text starts with a composing character,
2883 * backup to the character before it. There could be two of them.
2884 */
2885 i = 0;
2886 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2887 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2888 {
2889 i = (*mb_head_off)(ccline.cmdbuff,
2890 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2891 ccline.cmdpos -= i;
2892 len += i;
2893 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2894 }
2895# ifdef FEAT_ARABIC
2896 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2897 {
2898 /* Check the previous character for Arabic combining pair. */
2899 i = (*mb_head_off)(ccline.cmdbuff,
2900 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2901 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2902 + ccline.cmdpos - i), c))
2903 {
2904 ccline.cmdpos -= i;
2905 len += i;
2906 }
2907 else
2908 i = 0;
2909 }
2910# endif
2911 if (i != 0)
2912 {
2913 /* Also backup the cursor position. */
2914 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2915 ccline.cmdspos -= i;
2916 msg_col -= i;
2917 if (msg_col < 0)
2918 {
2919 msg_col += Columns;
2920 --msg_row;
2921 }
2922 }
2923 }
2924#endif
2925
2926 if (redraw && !cmd_silent)
2927 {
2928 msg_no_more = TRUE;
2929 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002930 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2932 /* Avoid clearing the rest of the line too often. */
2933 if (cmdline_row != i || ccline.overstrike)
2934 msg_clr_eos();
2935 msg_no_more = FALSE;
2936 }
2937#ifdef FEAT_FKMAP
2938 /*
2939 * If we are in Farsi command mode, the character input must be in
2940 * Insert mode. So do not advance the cmdpos.
2941 */
2942 if (!cmd_fkmap)
2943#endif
2944 {
2945 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002946 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002948 if (m < 0) /* overflow, Columns or Rows at weird value */
2949 m = MAXCOL;
2950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 else
2952 m = MAXCOL;
2953 for (i = 0; i < len; ++i)
2954 {
2955 c = cmdline_charsize(ccline.cmdpos);
2956#ifdef FEAT_MBYTE
2957 /* count ">" for a double-wide char that doesn't fit. */
2958 if (has_mbyte)
2959 correct_cmdspos(ccline.cmdpos, c);
2960#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002961 /* Stop cursor at the end of the screen, but do increment the
2962 * insert position, so that entering a very long command
2963 * works, even though you can't see it. */
2964 if (ccline.cmdspos + c < m)
2965 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966#ifdef FEAT_MBYTE
2967 if (has_mbyte)
2968 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002969 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 if (c > len - i - 1)
2971 c = len - i - 1;
2972 ccline.cmdpos += c;
2973 i += c;
2974 }
2975#endif
2976 ++ccline.cmdpos;
2977 }
2978 }
2979 }
2980 if (redraw)
2981 msg_check();
2982 return retval;
2983}
2984
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002985static struct cmdline_info prev_ccline;
2986static int prev_ccline_used = FALSE;
2987
2988/*
2989 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2990 * and overwrite it. But get_cmdline_str() may need it, thus make it
2991 * available globally in prev_ccline.
2992 */
2993 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002994save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002995{
2996 if (!prev_ccline_used)
2997 {
2998 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
2999 prev_ccline_used = TRUE;
3000 }
3001 *ccp = prev_ccline;
3002 prev_ccline = ccline;
3003 ccline.cmdbuff = NULL;
3004 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003005 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003006}
3007
3008/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003009 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003010 */
3011 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003012restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003013{
3014 ccline = prev_ccline;
3015 prev_ccline = *ccp;
3016}
3017
Bram Moolenaar5a305422006-04-28 22:38:25 +00003018#if defined(FEAT_EVAL) || defined(PROTO)
3019/*
3020 * Save the command line into allocated memory. Returns a pointer to be
3021 * passed to restore_cmdline_alloc() later.
3022 * Returns NULL when failed.
3023 */
3024 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003026{
3027 struct cmdline_info *p;
3028
3029 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3030 if (p != NULL)
3031 save_cmdline(p);
3032 return (char_u *)p;
3033}
3034
3035/*
3036 * Restore the command line from the return value of save_cmdline_alloc().
3037 */
3038 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003039restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003040{
3041 if (p != NULL)
3042 {
3043 restore_cmdline((struct cmdline_info *)p);
3044 vim_free(p);
3045 }
3046}
3047#endif
3048
Bram Moolenaar8299df92004-07-10 09:47:34 +00003049/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003050 * Paste a yank register into the command line.
3051 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003052 * insert_reg() can't be used here, because special characters from the
3053 * register contents will be interpreted as commands.
3054 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003055 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003056 */
3057 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003058cmdline_paste(
3059 int regname,
3060 int literally, /* Insert text literally instead of "as typed" */
3061 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003062{
3063 long i;
3064 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003065 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003066 int allocated;
3067 struct cmdline_info save_ccline;
3068
3069 /* check for valid regname; also accept special characters for CTRL-R in
3070 * the command line */
3071 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3072 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3073 return FAIL;
3074
3075 /* A register containing CTRL-R can cause an endless loop. Allow using
3076 * CTRL-C to break the loop. */
3077 line_breakcheck();
3078 if (got_int)
3079 return FAIL;
3080
3081#ifdef FEAT_CLIPBOARD
3082 regname = may_get_selection(regname);
3083#endif
3084
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003085 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003086 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003087 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003088 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003089 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003090 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003091 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003092
3093 if (i)
3094 {
3095 /* Got the value of a special register in "arg". */
3096 if (arg == NULL)
3097 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003098
3099 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3100 * part of the word. */
3101 p = arg;
3102 if (p_is && regname == Ctrl_W)
3103 {
3104 char_u *w;
3105 int len;
3106
3107 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003108 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003109 {
3110#ifdef FEAT_MBYTE
3111 if (has_mbyte)
3112 {
3113 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3114 if (!vim_iswordc(mb_ptr2char(w - len)))
3115 break;
3116 w -= len;
3117 }
3118 else
3119#endif
3120 {
3121 if (!vim_iswordc(w[-1]))
3122 break;
3123 --w;
3124 }
3125 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003126 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003127 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3128 p += len;
3129 }
3130
3131 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003132 if (allocated)
3133 vim_free(arg);
3134 return OK;
3135 }
3136
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003137 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003138}
3139
3140/*
3141 * Put a string on the command line.
3142 * When "literally" is TRUE, insert literally.
3143 * When "literally" is FALSE, insert as typed, but don't leave the command
3144 * line.
3145 */
3146 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003147cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003148{
3149 int c, cv;
3150
3151 if (literally)
3152 put_on_cmdline(s, -1, TRUE);
3153 else
3154 while (*s != NUL)
3155 {
3156 cv = *s;
3157 if (cv == Ctrl_V && s[1])
3158 ++s;
3159#ifdef FEAT_MBYTE
3160 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003161 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003162 else
3163#endif
3164 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003165 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3166 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003167#ifdef UNIX
3168 || c == intr_char
3169#endif
3170 || (c == Ctrl_BSL && *s == Ctrl_N))
3171 stuffcharReadbuff(Ctrl_V);
3172 stuffcharReadbuff(c);
3173 }
3174}
3175
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176#ifdef FEAT_WILDMENU
3177/*
3178 * Delete characters on the command line, from "from" to the current
3179 * position.
3180 */
3181 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003182cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3185 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3186 ccline.cmdlen -= ccline.cmdpos - from;
3187 ccline.cmdpos = from;
3188}
3189#endif
3190
3191/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003192 * This function is called when the screen size changes and with incremental
3193 * search and in other situations where the command line may have been
3194 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 */
3196 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003197redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 if (cmd_silent)
3200 return;
3201 need_wait_return = FALSE;
3202 compute_cmdrow();
3203 redrawcmd();
3204 cursorcmd();
3205}
3206
3207 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003208redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int i;
3211
3212 if (cmd_silent)
3213 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003214 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 msg_putchar(ccline.cmdfirstc);
3216 if (ccline.cmdprompt != NULL)
3217 {
3218 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3219 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3220 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003221 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 --ccline.cmdindent;
3223 }
3224 else
3225 for (i = ccline.cmdindent; i > 0; --i)
3226 msg_putchar(' ');
3227}
3228
3229/*
3230 * Redraw what is currently on the command line.
3231 */
3232 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003233redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 if (cmd_silent)
3236 return;
3237
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003238 /* when 'incsearch' is set there may be no command line while redrawing */
3239 if (ccline.cmdbuff == NULL)
3240 {
3241 windgoto(cmdline_row, 0);
3242 msg_clr_eos();
3243 return;
3244 }
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 msg_start();
3247 redrawcmdprompt();
3248
3249 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3250 msg_no_more = TRUE;
3251 draw_cmdline(0, ccline.cmdlen);
3252 msg_clr_eos();
3253 msg_no_more = FALSE;
3254
3255 set_cmdspos_cursor();
3256
3257 /*
3258 * An emsg() before may have set msg_scroll. This is used in normal mode,
3259 * in cmdline mode we can reset them now.
3260 */
3261 msg_scroll = FALSE; /* next message overwrites cmdline */
3262
3263 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3264 * in cmdline mode */
3265 skip_redraw = FALSE;
3266}
3267
3268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003269compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003271 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 cmdline_row = Rows - 1;
3273 else
3274 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3275 + W_STATUS_HEIGHT(lastwin);
3276}
3277
3278 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003279cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 if (cmd_silent)
3282 return;
3283
3284#ifdef FEAT_RIGHTLEFT
3285 if (cmdmsg_rl)
3286 {
3287 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3288 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3289 if (msg_row <= 0)
3290 msg_row = Rows - 1;
3291 }
3292 else
3293#endif
3294 {
3295 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3296 msg_col = ccline.cmdspos % (int)Columns;
3297 if (msg_row >= Rows)
3298 msg_row = Rows - 1;
3299 }
3300
3301 windgoto(msg_row, msg_col);
3302#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3303 redrawcmd_preedit();
3304#endif
3305#ifdef MCH_CURSOR_SHAPE
3306 mch_update_cursor();
3307#endif
3308}
3309
3310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003311gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312{
3313 msg_start();
3314#ifdef FEAT_RIGHTLEFT
3315 if (cmdmsg_rl)
3316 msg_col = Columns - 1;
3317 else
3318#endif
3319 msg_col = 0; /* always start in column 0 */
3320 if (clr) /* clear the bottom line(s) */
3321 msg_clr_eos(); /* will reset clear_cmdline */
3322 windgoto(cmdline_row, 0);
3323}
3324
3325/*
3326 * Check the word in front of the cursor for an abbreviation.
3327 * Called when the non-id character "c" has been entered.
3328 * When an abbreviation is recognized it is removed from the text with
3329 * backspaces and the replacement string is inserted, followed by "c".
3330 */
3331 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003332ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3335 return FALSE;
3336
3337 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3338}
3339
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003340#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3341 static int
3342#ifdef __BORLANDC__
3343_RTLENTRYF
3344#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003345sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003346{
3347 char_u *p1 = *(char_u **)s1;
3348 char_u *p2 = *(char_u **)s2;
3349
3350 if (*p1 != '<' && *p2 == '<') return -1;
3351 if (*p1 == '<' && *p2 != '<') return 1;
3352 return STRCMP(p1, p2);
3353}
3354#endif
3355
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356/*
3357 * Return FAIL if this is not an appropriate context in which to do
3358 * completion of anything, return OK if it is (even if there are no matches).
3359 * For the caller, this means that the character is just passed through like a
3360 * normal character (instead of being expanded). This allows :s/^I^D etc.
3361 */
3362 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003363nextwild(
3364 expand_T *xp,
3365 int type,
3366 int options, /* extra options for ExpandOne() */
3367 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
3369 int i, j;
3370 char_u *p1;
3371 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 int difflen;
3373 int v;
3374
3375 if (xp->xp_numfiles == -1)
3376 {
3377 set_expand_context(xp);
3378 cmd_showtail = expand_showtail(xp);
3379 }
3380
3381 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3382 {
3383 beep_flush();
3384 return OK; /* Something illegal on command line */
3385 }
3386 if (xp->xp_context == EXPAND_NOTHING)
3387 {
3388 /* Caller can use the character as a normal char instead */
3389 return FAIL;
3390 }
3391
3392 MSG_PUTS("..."); /* show that we are busy */
3393 out_flush();
3394
3395 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003396 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 if (type == WILD_NEXT || type == WILD_PREV)
3399 {
3400 /*
3401 * Get next/previous match for a previous expanded pattern.
3402 */
3403 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3404 }
3405 else
3406 {
3407 /*
3408 * Translate string into pattern and expand it.
3409 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003410 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3411 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 p2 = NULL;
3413 else
3414 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003415 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003416 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3417 if (escape)
3418 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003419
3420 if (p_wic)
3421 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003422 p2 = ExpandOne(xp, p1,
3423 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003424 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003426 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (p2 != NULL && type == WILD_LONGEST)
3428 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003429 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 if (ccline.cmdbuff[i + j] == '*'
3431 || ccline.cmdbuff[i + j] == '?')
3432 break;
3433 if ((int)STRLEN(p2) < j)
3434 {
3435 vim_free(p2);
3436 p2 = NULL;
3437 }
3438 }
3439 }
3440 }
3441
3442 if (p2 != NULL && !got_int)
3443 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003444 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003445 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003447 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 xp->xp_pattern = ccline.cmdbuff + i;
3449 }
3450 else
3451 v = OK;
3452 if (v == OK)
3453 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003454 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3455 &ccline.cmdbuff[ccline.cmdpos],
3456 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3457 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 ccline.cmdlen += difflen;
3459 ccline.cmdpos += difflen;
3460 }
3461 }
3462 vim_free(p2);
3463
3464 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003465 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 /* When expanding a ":map" command and no matches are found, assume that
3468 * the key is supposed to be inserted literally */
3469 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3470 return FAIL;
3471
3472 if (xp->xp_numfiles <= 0 && p2 == NULL)
3473 beep_flush();
3474 else if (xp->xp_numfiles == 1)
3475 /* free expanded pattern */
3476 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3477
3478 return OK;
3479}
3480
3481/*
3482 * Do wildcard expansion on the string 'str'.
3483 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003484 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 * Return NULL for failure.
3486 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003487 * "orig" is the originally expanded string, copied to allocated memory. It
3488 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3489 * WILD_PREV "orig" should be NULL.
3490 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003491 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3492 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 *
3494 * mode = WILD_FREE: just free previously expanded matches
3495 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3496 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3497 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3498 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3499 * mode = WILD_ALL: return all matches concatenated
3500 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003501 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 *
3503 * options = WILD_LIST_NOTFOUND: list entries without a match
3504 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3505 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3506 * options = WILD_NO_BEEP: Don't beep for multiple matches
3507 * options = WILD_ADD_SLASH: add a slash after directory names
3508 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3509 * options = WILD_SILENT: don't print warning messages
3510 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003511 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 *
3513 * The variables xp->xp_context and xp->xp_backslash must have been set!
3514 */
3515 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003516ExpandOne(
3517 expand_T *xp,
3518 char_u *str,
3519 char_u *orig, /* allocated copy of original of expanded string */
3520 int options,
3521 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522{
3523 char_u *ss = NULL;
3524 static int findex;
3525 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003526 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 int i;
3528 long_u len;
3529 int non_suf_match; /* number without matching suffix */
3530
3531 /*
3532 * first handle the case of using an old match
3533 */
3534 if (mode == WILD_NEXT || mode == WILD_PREV)
3535 {
3536 if (xp->xp_numfiles > 0)
3537 {
3538 if (mode == WILD_PREV)
3539 {
3540 if (findex == -1)
3541 findex = xp->xp_numfiles;
3542 --findex;
3543 }
3544 else /* mode == WILD_NEXT */
3545 ++findex;
3546
3547 /*
3548 * When wrapping around, return the original string, set findex to
3549 * -1.
3550 */
3551 if (findex < 0)
3552 {
3553 if (orig_save == NULL)
3554 findex = xp->xp_numfiles - 1;
3555 else
3556 findex = -1;
3557 }
3558 if (findex >= xp->xp_numfiles)
3559 {
3560 if (orig_save == NULL)
3561 findex = 0;
3562 else
3563 findex = -1;
3564 }
3565#ifdef FEAT_WILDMENU
3566 if (p_wmnu)
3567 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3568 findex, cmd_showtail);
3569#endif
3570 if (findex == -1)
3571 return vim_strsave(orig_save);
3572 return vim_strsave(xp->xp_files[findex]);
3573 }
3574 else
3575 return NULL;
3576 }
3577
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003578 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3580 {
3581 FreeWild(xp->xp_numfiles, xp->xp_files);
3582 xp->xp_numfiles = -1;
3583 vim_free(orig_save);
3584 orig_save = NULL;
3585 }
3586 findex = 0;
3587
3588 if (mode == WILD_FREE) /* only release file name */
3589 return NULL;
3590
3591 if (xp->xp_numfiles == -1)
3592 {
3593 vim_free(orig_save);
3594 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003595 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
3597 /*
3598 * Do the expansion.
3599 */
3600 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3601 options) == FAIL)
3602 {
3603#ifdef FNAME_ILLEGAL
3604 /* Illegal file name has been silently skipped. But when there
3605 * are wildcards, the real problem is that there was no match,
3606 * causing the pattern to be added, which has illegal characters.
3607 */
3608 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3609 EMSG2(_(e_nomatch2), str);
3610#endif
3611 }
3612 else if (xp->xp_numfiles == 0)
3613 {
3614 if (!(options & WILD_SILENT))
3615 EMSG2(_(e_nomatch2), str);
3616 }
3617 else
3618 {
3619 /* Escape the matches for use on the command line. */
3620 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3621
3622 /*
3623 * Check for matching suffixes in file names.
3624 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003625 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3626 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 {
3628 if (xp->xp_numfiles)
3629 non_suf_match = xp->xp_numfiles;
3630 else
3631 non_suf_match = 1;
3632 if ((xp->xp_context == EXPAND_FILES
3633 || xp->xp_context == EXPAND_DIRECTORIES)
3634 && xp->xp_numfiles > 1)
3635 {
3636 /*
3637 * More than one match; check suffix.
3638 * The files will have been sorted on matching suffix in
3639 * expand_wildcards, only need to check the first two.
3640 */
3641 non_suf_match = 0;
3642 for (i = 0; i < 2; ++i)
3643 if (match_suffix(xp->xp_files[i]))
3644 ++non_suf_match;
3645 }
3646 if (non_suf_match != 1)
3647 {
3648 /* Can we ever get here unless it's while expanding
3649 * interactively? If not, we can get rid of this all
3650 * together. Don't really want to wait for this message
3651 * (and possibly have to hit return to continue!).
3652 */
3653 if (!(options & WILD_SILENT))
3654 EMSG(_(e_toomany));
3655 else if (!(options & WILD_NO_BEEP))
3656 beep_flush();
3657 }
3658 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3659 ss = vim_strsave(xp->xp_files[0]);
3660 }
3661 }
3662 }
3663
3664 /* Find longest common part */
3665 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3666 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003667 int mb_len = 1;
3668 int c0, ci;
3669
3670 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003672#ifdef FEAT_MBYTE
3673 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003675 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3676 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3677 }
3678 else
3679#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003680 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003681 for (i = 1; i < xp->xp_numfiles; ++i)
3682 {
3683#ifdef FEAT_MBYTE
3684 if (has_mbyte)
3685 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3686 else
3687#endif
3688 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003689 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003691 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003692 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003694 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 break;
3696 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003697 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 break;
3699 }
3700 if (i < xp->xp_numfiles)
3701 {
3702 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003703 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 break;
3705 }
3706 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 ss = alloc((unsigned)len + 1);
3709 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003710 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 findex = -1; /* next p_wc gets first one */
3712 }
3713
3714 /* Concatenate all matching names */
3715 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3716 {
3717 len = 0;
3718 for (i = 0; i < xp->xp_numfiles; ++i)
3719 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3720 ss = lalloc(len, TRUE);
3721 if (ss != NULL)
3722 {
3723 *ss = NUL;
3724 for (i = 0; i < xp->xp_numfiles; ++i)
3725 {
3726 STRCAT(ss, xp->xp_files[i]);
3727 if (i != xp->xp_numfiles - 1)
3728 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3729 }
3730 }
3731 }
3732
3733 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3734 ExpandCleanup(xp);
3735
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003736 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003737 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003738 vim_free(orig);
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 return ss;
3741}
3742
3743/*
3744 * Prepare an expand structure for use.
3745 */
3746 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003747ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003749 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003750 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003752#ifndef BACKSLASH_IN_FILENAME
3753 xp->xp_shell = FALSE;
3754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 xp->xp_numfiles = -1;
3756 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003757#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3758 xp->xp_arg = NULL;
3759#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003760 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761}
3762
3763/*
3764 * Cleanup an expand structure after use.
3765 */
3766 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003767ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 if (xp->xp_numfiles >= 0)
3770 {
3771 FreeWild(xp->xp_numfiles, xp->xp_files);
3772 xp->xp_numfiles = -1;
3773 }
3774}
3775
3776 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003777ExpandEscape(
3778 expand_T *xp,
3779 char_u *str,
3780 int numfiles,
3781 char_u **files,
3782 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783{
3784 int i;
3785 char_u *p;
3786
3787 /*
3788 * May change home directory back to "~"
3789 */
3790 if (options & WILD_HOME_REPLACE)
3791 tilde_replace(str, numfiles, files);
3792
3793 if (options & WILD_ESCAPE)
3794 {
3795 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003796 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003797 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 || xp->xp_context == EXPAND_BUFFERS
3799 || xp->xp_context == EXPAND_DIRECTORIES)
3800 {
3801 /*
3802 * Insert a backslash into a file name before a space, \, %, #
3803 * and wildmatch characters, except '~'.
3804 */
3805 for (i = 0; i < numfiles; ++i)
3806 {
3807 /* for ":set path=" we need to escape spaces twice */
3808 if (xp->xp_backslash == XP_BS_THREE)
3809 {
3810 p = vim_strsave_escaped(files[i], (char_u *)" ");
3811 if (p != NULL)
3812 {
3813 vim_free(files[i]);
3814 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003815#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 p = vim_strsave_escaped(files[i], (char_u *)" ");
3817 if (p != NULL)
3818 {
3819 vim_free(files[i]);
3820 files[i] = p;
3821 }
3822#endif
3823 }
3824 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003825#ifdef BACKSLASH_IN_FILENAME
3826 p = vim_strsave_fnameescape(files[i], FALSE);
3827#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003828 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (p != NULL)
3831 {
3832 vim_free(files[i]);
3833 files[i] = p;
3834 }
3835
3836 /* If 'str' starts with "\~", replace "~" at start of
3837 * files[i] with "\~". */
3838 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00003839 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 }
3841 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00003842
3843 /* If the first file starts with a '+' escape it. Otherwise it
3844 * could be seen as "+cmd". */
3845 if (*files[0] == '+')
3846 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848 else if (xp->xp_context == EXPAND_TAGS)
3849 {
3850 /*
3851 * Insert a backslash before characters in a tag name that
3852 * would terminate the ":tag" command.
3853 */
3854 for (i = 0; i < numfiles; ++i)
3855 {
3856 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
3857 if (p != NULL)
3858 {
3859 vim_free(files[i]);
3860 files[i] = p;
3861 }
3862 }
3863 }
3864 }
3865}
3866
3867/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003868 * Escape special characters in "fname" for when used as a file name argument
3869 * after a Vim command, or, when "shell" is non-zero, a shell command.
3870 * Returns the result in allocated memory.
3871 */
3872 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003873vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003874{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003875 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003876#ifdef BACKSLASH_IN_FILENAME
3877 char_u buf[20];
3878 int j = 0;
3879
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003880 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003881 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01003882 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003883 buf[j++] = *p;
3884 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003885 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003886#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00003887 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
3888 if (shell && csh_like_shell() && p != NULL)
3889 {
3890 char_u *s;
3891
3892 /* For csh and similar shells need to put two backslashes before '!'.
3893 * One is taken by Vim, one by the shell. */
3894 s = vim_strsave_escaped(p, (char_u *)"!");
3895 vim_free(p);
3896 p = s;
3897 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003898#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003899
3900 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
3901 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02003902 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00003903 escape_fname(&p);
3904
3905 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003906}
3907
3908/*
Bram Moolenaar45360022005-07-21 21:08:21 +00003909 * Put a backslash before the file name in "pp", which is in allocated memory.
3910 */
3911 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003912escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00003913{
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
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003931tilde_replace(
3932 char_u *orig_pat,
3933 int num_files,
3934 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935{
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
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003959showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960{
3961#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
3962 int num_files;
3963 char_u **files_found;
3964 int i, j, k;
3965 int maxlen;
3966 int lines;
3967 int columns;
3968 char_u *p;
3969 int lastlen;
3970 int attr;
3971 int showtail;
3972
3973 if (xp->xp_numfiles == -1)
3974 {
3975 set_expand_context(xp);
3976 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
3977 &num_files, &files_found);
3978 showtail = expand_showtail(xp);
3979 if (i != EXPAND_OK)
3980 return i;
3981
3982 }
3983 else
3984 {
3985 num_files = xp->xp_numfiles;
3986 files_found = xp->xp_files;
3987 showtail = cmd_showtail;
3988 }
3989
3990#ifdef FEAT_WILDMENU
3991 if (!wildmenu)
3992 {
3993#endif
3994 msg_didany = FALSE; /* lines_left will be set */
3995 msg_start(); /* prepare for paging */
3996 msg_putchar('\n');
3997 out_flush();
3998 cmdline_row = msg_row;
3999 msg_didany = FALSE; /* lines_left will be set again */
4000 msg_start(); /* prepare for paging */
4001#ifdef FEAT_WILDMENU
4002 }
4003#endif
4004
4005 if (got_int)
4006 got_int = FALSE; /* only int. the completion, not the cmd line */
4007#ifdef FEAT_WILDMENU
4008 else if (wildmenu)
4009 win_redr_status_matches(xp, num_files, files_found, 0, showtail);
4010#endif
4011 else
4012 {
4013 /* find the length of the longest file name */
4014 maxlen = 0;
4015 for (i = 0; i < num_files; ++i)
4016 {
4017 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004018 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 || xp->xp_context == EXPAND_BUFFERS))
4020 {
4021 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4022 j = vim_strsize(NameBuff);
4023 }
4024 else
4025 j = vim_strsize(L_SHOWFILE(i));
4026 if (j > maxlen)
4027 maxlen = j;
4028 }
4029
4030 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4031 lines = num_files;
4032 else
4033 {
4034 /* compute the number of columns and lines for the listing */
4035 maxlen += 2; /* two spaces between file names */
4036 columns = ((int)Columns + 2) / maxlen;
4037 if (columns < 1)
4038 columns = 1;
4039 lines = (num_files + columns - 1) / columns;
4040 }
4041
4042 attr = hl_attr(HLF_D); /* find out highlighting for directories */
4043
4044 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4045 {
4046 MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T));
4047 msg_clr_eos();
4048 msg_advance(maxlen - 3);
4049 MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T));
4050 }
4051
4052 /* list the files line by line */
4053 for (i = 0; i < lines; ++i)
4054 {
4055 lastlen = 999;
4056 for (k = i; k < num_files; k += lines)
4057 {
4058 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4059 {
4060 msg_outtrans_attr(files_found[k], hl_attr(HLF_D));
4061 p = files_found[k] + STRLEN(files_found[k]) + 1;
4062 msg_advance(maxlen + 1);
4063 msg_puts(p);
4064 msg_advance(maxlen + 3);
4065 msg_puts_long_attr(p + 2, hl_attr(HLF_D));
4066 break;
4067 }
4068 for (j = maxlen - lastlen; --j >= 0; )
4069 msg_putchar(' ');
4070 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004071 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 || xp->xp_context == EXPAND_BUFFERS)
4073 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004074 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004075 if (xp->xp_numfiles != -1)
4076 {
4077 char_u *halved_slash;
4078 char_u *exp_path;
4079
4080 /* Expansion was done before and special characters
4081 * were escaped, need to halve backslashes. Also
4082 * $HOME has been replaced with ~/. */
4083 exp_path = expand_env_save_opt(files_found[k], TRUE);
4084 halved_slash = backslash_halve_save(
4085 exp_path != NULL ? exp_path : files_found[k]);
4086 j = mch_isdir(halved_slash != NULL ? halved_slash
4087 : files_found[k]);
4088 vim_free(exp_path);
4089 vim_free(halved_slash);
4090 }
4091 else
4092 /* Expansion was done here, file names are literal. */
4093 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 if (showtail)
4095 p = L_SHOWFILE(k);
4096 else
4097 {
4098 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4099 TRUE);
4100 p = NameBuff;
4101 }
4102 }
4103 else
4104 {
4105 j = FALSE;
4106 p = L_SHOWFILE(k);
4107 }
4108 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4109 }
4110 if (msg_col > 0) /* when not wrapped around */
4111 {
4112 msg_clr_eos();
4113 msg_putchar('\n');
4114 }
4115 out_flush(); /* show one line at a time */
4116 if (got_int)
4117 {
4118 got_int = FALSE;
4119 break;
4120 }
4121 }
4122
4123 /*
4124 * we redraw the command below the lines that we have just listed
4125 * This is a bit tricky, but it saves a lot of screen updating.
4126 */
4127 cmdline_row = msg_row; /* will put it back later */
4128 }
4129
4130 if (xp->xp_numfiles == -1)
4131 FreeWild(num_files, files_found);
4132
4133 return EXPAND_OK;
4134}
4135
4136/*
4137 * Private gettail for showmatches() (and win_redr_status_matches()):
4138 * Find tail of file name path, but ignore trailing "/".
4139 */
4140 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004141sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142{
4143 char_u *p;
4144 char_u *t = s;
4145 int had_sep = FALSE;
4146
4147 for (p = s; *p != NUL; )
4148 {
4149 if (vim_ispathsep(*p)
4150#ifdef BACKSLASH_IN_FILENAME
4151 && !rem_backslash(p)
4152#endif
4153 )
4154 had_sep = TRUE;
4155 else if (had_sep)
4156 {
4157 t = p;
4158 had_sep = FALSE;
4159 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004160 mb_ptr_adv(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 }
4162 return t;
4163}
4164
4165/*
4166 * Return TRUE if we only need to show the tail of completion matches.
4167 * When not completing file names or there is a wildcard in the path FALSE is
4168 * returned.
4169 */
4170 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004171expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 char_u *s;
4174 char_u *end;
4175
4176 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004177 if (xp->xp_context != EXPAND_FILES
4178 && xp->xp_context != EXPAND_SHELLCMD
4179 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 return FALSE;
4181
4182 end = gettail(xp->xp_pattern);
4183 if (end == xp->xp_pattern) /* there is no path separator */
4184 return FALSE;
4185
4186 for (s = xp->xp_pattern; s < end; s++)
4187 {
4188 /* Skip escaped wildcards. Only when the backslash is not a path
4189 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4190 if (rem_backslash(s))
4191 ++s;
4192 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4193 return FALSE;
4194 }
4195 return TRUE;
4196}
4197
4198/*
4199 * Prepare a string for expansion.
4200 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004201 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * When expanding other names: The string will be used with regcomp(). Copy
4203 * the name into allocated memory and prepend "^".
4204 */
4205 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004206addstar(
4207 char_u *fname,
4208 int len,
4209 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
4211 char_u *retval;
4212 int i, j;
4213 int new_len;
4214 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004215 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004217 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004218 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004219 && context != EXPAND_SHELLCMD
4220 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
4222 /*
4223 * Matching will be done internally (on something other than files).
4224 * So we convert the file-matching-type wildcards into our kind for
4225 * use with vim_regcomp(). First work out how long it will be:
4226 */
4227
4228 /* For help tags the translation is done in find_help_tags().
4229 * For a tag pattern starting with "/" no translation is needed. */
4230 if (context == EXPAND_HELP
4231 || context == EXPAND_COLORS
4232 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004233 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004234 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004235 || context == EXPAND_PACKADD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 || (context == EXPAND_TAGS && fname[0] == '/'))
4237 retval = vim_strnsave(fname, len);
4238 else
4239 {
4240 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4241 for (i = 0; i < len; i++)
4242 {
4243 if (fname[i] == '*' || fname[i] == '~')
4244 new_len++; /* '*' needs to be replaced by ".*"
4245 '~' needs to be replaced by "\~" */
4246
4247 /* Buffer names are like file names. "." should be literal */
4248 if (context == EXPAND_BUFFERS && fname[i] == '.')
4249 new_len++; /* "." becomes "\." */
4250
4251 /* Custom expansion takes care of special things, match
4252 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004253 if ((context == EXPAND_USER_DEFINED
4254 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 new_len++; /* '\' becomes "\\" */
4256 }
4257 retval = alloc(new_len);
4258 if (retval != NULL)
4259 {
4260 retval[0] = '^';
4261 j = 1;
4262 for (i = 0; i < len; i++, j++)
4263 {
4264 /* Skip backslash. But why? At least keep it for custom
4265 * expansion. */
4266 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004267 && context != EXPAND_USER_LIST
4268 && fname[i] == '\\'
4269 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 break;
4271
4272 switch (fname[i])
4273 {
4274 case '*': retval[j++] = '.';
4275 break;
4276 case '~': retval[j++] = '\\';
4277 break;
4278 case '?': retval[j] = '.';
4279 continue;
4280 case '.': if (context == EXPAND_BUFFERS)
4281 retval[j++] = '\\';
4282 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004283 case '\\': if (context == EXPAND_USER_DEFINED
4284 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 retval[j++] = '\\';
4286 break;
4287 }
4288 retval[j] = fname[i];
4289 }
4290 retval[j] = NUL;
4291 }
4292 }
4293 }
4294 else
4295 {
4296 retval = alloc(len + 4);
4297 if (retval != NULL)
4298 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004299 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300
4301 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004302 * Don't add a star to *, ~, ~user, $var or `cmd`.
4303 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 * ~ would be at the start of the file name, but not the tail.
4305 * $ could be anywhere in the tail.
4306 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004307 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 */
4309 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004310 ends_in_star = (len > 0 && retval[len - 1] == '*');
4311#ifndef BACKSLASH_IN_FILENAME
4312 for (i = len - 2; i >= 0; --i)
4313 {
4314 if (retval[i] != '\\')
4315 break;
4316 ends_in_star = !ends_in_star;
4317 }
4318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004320 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 && vim_strchr(tail, '$') == NULL
4322 && vim_strchr(retval, '`') == NULL)
4323 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004324 else if (len > 0 && retval[len - 1] == '$')
4325 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 retval[len] = NUL;
4327 }
4328 }
4329 return retval;
4330}
4331
4332/*
4333 * Must parse the command line so far to work out what context we are in.
4334 * Completion can then be done based on that context.
4335 * This routine sets the variables:
4336 * xp->xp_pattern The start of the pattern to be expanded within
4337 * the command line (ends at the cursor).
4338 * xp->xp_context The type of thing to expand. Will be one of:
4339 *
4340 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4341 * the command line, like an unknown command. Caller
4342 * should beep.
4343 * EXPAND_NOTHING Unrecognised context for completion, use char like
4344 * a normal char, rather than for completion. eg
4345 * :s/^I/
4346 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4347 * it.
4348 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4349 * EXPAND_FILES After command with XFILE set, or after setting
4350 * with P_EXPAND set. eg :e ^I, :w>>^I
4351 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4352 * when we know only directories are of interest. eg
4353 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004354 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4356 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4357 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4358 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4359 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4360 * EXPAND_EVENTS Complete event names
4361 * EXPAND_SYNTAX Complete :syntax command arguments
4362 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4363 * EXPAND_AUGROUP Complete autocommand group names
4364 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4365 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4366 * eg :unmap a^I , :cunab x^I
4367 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4368 * eg :call sub^I
4369 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4370 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4371 * names in expressions, eg :while s^I
4372 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004373 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 */
4375 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004376set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004378 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 if (ccline.cmdfirstc != ':'
4380#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004381 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004382 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383#endif
4384 )
4385 {
4386 xp->xp_context = EXPAND_NOTHING;
4387 return;
4388 }
4389 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos);
4390}
4391
4392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004393set_cmd_context(
4394 expand_T *xp,
4395 char_u *str, /* start of command line */
4396 int len, /* length of command line (excl. NUL) */
4397 int col) /* position of cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 int old_char = NUL;
4400 char_u *nextcomm;
4401
4402 /*
4403 * Avoid a UMR warning from Purify, only save the character if it has been
4404 * written before.
4405 */
4406 if (col < len)
4407 old_char = str[col];
4408 str[col] = NUL;
4409 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004410
4411#ifdef FEAT_EVAL
4412 if (ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004413 {
4414# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004415 /* pass CMD_SIZE because there is no real command */
4416 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004417# endif
4418 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004419 else if (ccline.input_fn)
4420 {
4421 xp->xp_context = ccline.xp_context;
4422 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004423# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004424 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004425# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004426 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004427 else
4428#endif
4429 while (nextcomm != NULL)
4430 nextcomm = set_one_cmd_context(xp, nextcomm);
4431
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004432 /* Store the string here so that call_user_expand_func() can get to them
4433 * easily. */
4434 xp->xp_line = str;
4435 xp->xp_col = col;
4436
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 str[col] = old_char;
4438}
4439
4440/*
4441 * Expand the command line "str" from context "xp".
4442 * "xp" must have been set by set_cmd_context().
4443 * xp->xp_pattern points into "str", to where the text that is to be expanded
4444 * starts.
4445 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4446 * cursor.
4447 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4448 * key that triggered expansion literally.
4449 * Returns EXPAND_OK otherwise.
4450 */
4451 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004452expand_cmdline(
4453 expand_T *xp,
4454 char_u *str, /* start of command line */
4455 int col, /* position of cursor */
4456 int *matchcount, /* return: nr of matches */
4457 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458{
4459 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004460 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461
4462 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4463 {
4464 beep_flush();
4465 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4466 }
4467 if (xp->xp_context == EXPAND_NOTHING)
4468 {
4469 /* Caller can use the character as a normal char instead */
4470 return EXPAND_NOTHING;
4471 }
4472
4473 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004474 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4475 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 if (file_str == NULL)
4477 return EXPAND_UNSUCCESSFUL;
4478
Bram Moolenaar94950a92010-12-02 16:01:29 +01004479 if (p_wic)
4480 options += WILD_ICASE;
4481
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004483 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
4485 *matchcount = 0;
4486 *matches = NULL;
4487 }
4488 vim_free(file_str);
4489
4490 return EXPAND_OK;
4491}
4492
4493#ifdef FEAT_MULTI_LANG
4494/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004495 * Cleanup matches for help tags:
4496 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4497 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004499static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500
4501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004502cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
4504 int i, j;
4505 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004506 char_u buf[4];
4507 char_u *p = buf;
4508
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004509 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004510 {
4511 *p++ = '@';
4512 *p++ = p_hlg[0];
4513 *p++ = p_hlg[1];
4514 }
4515 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516
4517 for (i = 0; i < num_file; ++i)
4518 {
4519 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004520 if (len <= 0)
4521 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004522 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 /* Sorting on priority means the same item in another language may
4525 * be anywhere. Search all items for a match up to the "@en". */
4526 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004527 if (j != i && (int)STRLEN(file[j]) == len + 3
4528 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004531 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 file[i][len] = NUL;
4533 }
4534 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004535
4536 if (*buf != NUL)
4537 for (i = 0; i < num_file; ++i)
4538 {
4539 len = (int)STRLEN(file[i]) - 3;
4540 if (len <= 0)
4541 continue;
4542 if (STRCMP(file[i] + len, buf) == 0)
4543 {
4544 /* remove the default language */
4545 file[i][len] = NUL;
4546 }
4547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548}
4549#endif
4550
4551/*
4552 * Do the expansion based on xp->xp_context and "pat".
4553 */
4554 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004555ExpandFromContext(
4556 expand_T *xp,
4557 char_u *pat,
4558 int *num_file,
4559 char_u ***file,
4560 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562#ifdef FEAT_CMDL_COMPL
4563 regmatch_T regmatch;
4564#endif
4565 int ret;
4566 int flags;
4567
4568 flags = EW_DIR; /* include directories */
4569 if (options & WILD_LIST_NOTFOUND)
4570 flags |= EW_NOTFOUND;
4571 if (options & WILD_ADD_SLASH)
4572 flags |= EW_ADDSLASH;
4573 if (options & WILD_KEEP_ALL)
4574 flags |= EW_KEEPALL;
4575 if (options & WILD_SILENT)
4576 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004577 if (options & WILD_ALLLINKS)
4578 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004580 if (xp->xp_context == EXPAND_FILES
4581 || xp->xp_context == EXPAND_DIRECTORIES
4582 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
4584 /*
4585 * Expand file or directory names.
4586 */
4587 int free_pat = FALSE;
4588 int i;
4589
4590 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4591 * space */
4592 if (xp->xp_backslash != XP_BS_NONE)
4593 {
4594 free_pat = TRUE;
4595 pat = vim_strsave(pat);
4596 for (i = 0; pat[i]; ++i)
4597 if (pat[i] == '\\')
4598 {
4599 if (xp->xp_backslash == XP_BS_THREE
4600 && pat[i + 1] == '\\'
4601 && pat[i + 2] == '\\'
4602 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004603 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (xp->xp_backslash == XP_BS_ONE
4605 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004606 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 }
4609
4610 if (xp->xp_context == EXPAND_FILES)
4611 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004612 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4613 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 else
4615 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004616 if (options & WILD_ICASE)
4617 flags |= EW_ICASE;
4618
Bram Moolenaard7834d32009-12-02 16:14:36 +00004619 /* Expand wildcards, supporting %:h and the like. */
4620 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (free_pat)
4622 vim_free(pat);
4623 return ret;
4624 }
4625
4626 *file = (char_u **)"";
4627 *num_file = 0;
4628 if (xp->xp_context == EXPAND_HELP)
4629 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004630 /* With an empty argument we would get all the help tags, which is
4631 * very slow. Get matches for "help" instead. */
4632 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4633 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
4635#ifdef FEAT_MULTI_LANG
4636 cleanup_help_tags(*num_file, *file);
4637#endif
4638 return OK;
4639 }
4640 return FAIL;
4641 }
4642
4643#ifndef FEAT_CMDL_COMPL
4644 return FAIL;
4645#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004646 if (xp->xp_context == EXPAND_SHELLCMD)
4647 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 if (xp->xp_context == EXPAND_OLD_SETTING)
4649 return ExpandOldSetting(num_file, file);
4650 if (xp->xp_context == EXPAND_BUFFERS)
4651 return ExpandBufnames(pat, num_file, file, options);
4652 if (xp->xp_context == EXPAND_TAGS
4653 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4654 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4655 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004656 {
4657 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004658 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4659 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004662 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004663 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004664 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004665 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004666 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004667 {
4668 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004669 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004670 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004671 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004672 {
4673 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004674 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004675 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004676# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4677 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004678 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004679# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004680 if (xp->xp_context == EXPAND_PACKADD)
4681 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
4683 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4684 if (regmatch.regprog == NULL)
4685 return FAIL;
4686
4687 /* set ignore-case according to p_ic, p_scs and pat */
4688 regmatch.rm_ic = ignorecase(pat);
4689
4690 if (xp->xp_context == EXPAND_SETTINGS
4691 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4692 ret = ExpandSettings(xp, &regmatch, num_file, file);
4693 else if (xp->xp_context == EXPAND_MAPPINGS)
4694 ret = ExpandMappings(&regmatch, num_file, file);
4695# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4696 else if (xp->xp_context == EXPAND_USER_DEFINED)
4697 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4698# endif
4699 else
4700 {
4701 static struct expgen
4702 {
4703 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004704 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004706 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 } tab[] =
4708 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004709 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4710 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004711#ifdef FEAT_CMDHIST
4712 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004715 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004716 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004717 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4718 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4719 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720#endif
4721#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004722 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4723 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4724 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4725 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726#endif
4727#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004728 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4729 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730#endif
4731#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004732 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004734#ifdef FEAT_PROFILE
4735 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4736#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004737 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004739 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4740 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004742#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004743 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004744#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004745#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004746 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004747#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004748#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004749 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4752 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004753 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4754 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004756 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004757 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 };
4759 int i;
4760
4761 /*
4762 * Find a context in the table and call the ExpandGeneric() with the
4763 * right function to do the expansion.
4764 */
4765 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004766 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (xp->xp_context == tab[i].context)
4768 {
4769 if (tab[i].ic)
4770 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004771 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004772 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 break;
4774 }
4775 }
4776
Bram Moolenaar473de612013-06-08 18:19:48 +02004777 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778
4779 return ret;
4780#endif /* FEAT_CMDL_COMPL */
4781}
4782
4783#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4784/*
4785 * Expand a list of names.
4786 *
4787 * Generic function for command line completion. It calls a function to
4788 * obtain strings, one by one. The strings are matched against a regexp
4789 * program. Matching strings are copied into an array, which is returned.
4790 *
4791 * Returns OK when no problems encountered, FAIL for error (out of memory).
4792 */
4793 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004794ExpandGeneric(
4795 expand_T *xp,
4796 regmatch_T *regmatch,
4797 int *num_file,
4798 char_u ***file,
4799 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004801 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802{
4803 int i;
4804 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004805 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 char_u *str;
4807
4808 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004809 * round == 0: count the number of matching names
4810 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004812 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 {
4814 for (i = 0; ; ++i)
4815 {
4816 str = (*func)(xp, i);
4817 if (str == NULL) /* end of list */
4818 break;
4819 if (*str == NUL) /* skip empty strings */
4820 continue;
4821
4822 if (vim_regexec(regmatch, str, (colnr_T)0))
4823 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004824 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004826 if (escaped)
4827 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4828 else
4829 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 (*file)[count] = str;
4831#ifdef FEAT_MENU
4832 if (func == get_menu_names && str != NULL)
4833 {
4834 /* test for separator added by get_menu_names() */
4835 str += STRLEN(str) - 1;
4836 if (*str == '\001')
4837 *str = '.';
4838 }
4839#endif
4840 }
4841 ++count;
4842 }
4843 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004844 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 if (count == 0)
4847 return OK;
4848 *num_file = count;
4849 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4850 if (*file == NULL)
4851 {
4852 *file = (char_u **)"";
4853 return FAIL;
4854 }
4855 count = 0;
4856 }
4857 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004858
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004859 /* Sort the results. Keep menu's in the specified order. */
4860 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004861 {
4862 if (xp->xp_context == EXPAND_EXPRESSION
4863 || xp->xp_context == EXPAND_FUNCTIONS
4864 || xp->xp_context == EXPAND_USER_FUNC)
4865 /* <SNR> functions should be sorted to the end. */
4866 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4867 sort_func_compare);
4868 else
4869 sort_strings(*file, *num_file);
4870 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004871
Bram Moolenaar4f688582007-07-24 12:34:30 +00004872#ifdef FEAT_CMDL_COMPL
4873 /* Reset the variables used for special highlight names expansion, so that
4874 * they don't show up when getting normal highlight names by ID. */
4875 reset_expand_highlight();
4876#endif
4877
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 return OK;
4879}
4880
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004881/*
4882 * Complete a shell command.
4883 * Returns FAIL or OK;
4884 */
4885 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004886expand_shellcmd(
4887 char_u *filepat, /* pattern to match with command names */
4888 int *num_file, /* return: number of matches */
4889 char_u ***file, /* return: array with matches */
4890 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004891{
4892 char_u *pat;
4893 int i;
4894 char_u *path;
4895 int mustfree = FALSE;
4896 garray_T ga;
4897 char_u *buf = alloc(MAXPATHL);
4898 size_t l;
4899 char_u *s, *e;
4900 int flags = flagsarg;
4901 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01004902 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004903
4904 if (buf == NULL)
4905 return FAIL;
4906
4907 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4908 * space */
4909 pat = vim_strsave(filepat);
4910 for (i = 0; pat[i]; ++i)
4911 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004912 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004913
Bram Moolenaarb5971142015-03-21 17:32:19 +01004914 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004915
4916 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004917 if (mch_isFullName(pat))
4918 path = (char_u *)" ";
4919 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004920 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4921 path = (char_u *)".";
4922 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004923 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004924 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004925 if (path == NULL)
4926 path = (char_u *)"";
4927 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004928
4929 /*
4930 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01004931 * collect them in "ga". When "." is not in $PATH also expand for the
4932 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004933 */
4934 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01004935 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004936 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01004937 if (*s == NUL)
4938 {
4939 if (did_curdir)
4940 break;
4941 /* Find directories in the current directory, path is empty. */
4942 did_curdir = TRUE;
4943 }
4944 else if (*s == '.')
4945 did_curdir = TRUE;
4946
Bram Moolenaar68c31742006-09-02 15:54:18 +00004947 if (*s == ' ')
4948 ++s; /* Skip space used for absolute path name. */
4949
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004950#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004951 e = vim_strchr(s, ';');
4952#else
4953 e = vim_strchr(s, ':');
4954#endif
4955 if (e == NULL)
4956 e = s + STRLEN(s);
4957
4958 l = e - s;
4959 if (l > MAXPATHL - 5)
4960 break;
4961 vim_strncpy(buf, s, l);
4962 add_pathsep(buf);
4963 l = STRLEN(buf);
4964 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4965
4966 /* Expand matches in one directory of $PATH. */
4967 ret = expand_wildcards(1, &buf, num_file, file, flags);
4968 if (ret == OK)
4969 {
4970 if (ga_grow(&ga, *num_file) == FAIL)
4971 FreeWild(*num_file, *file);
4972 else
4973 {
4974 for (i = 0; i < *num_file; ++i)
4975 {
4976 s = (*file)[i];
4977 if (STRLEN(s) > l)
4978 {
4979 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004980 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004981 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4982 }
4983 else
4984 vim_free(s);
4985 }
4986 vim_free(*file);
4987 }
4988 }
4989 if (*e != NUL)
4990 ++e;
4991 }
4992 *file = ga.ga_data;
4993 *num_file = ga.ga_len;
4994
4995 vim_free(buf);
4996 vim_free(pat);
4997 if (mustfree)
4998 vim_free(path);
4999 return OK;
5000}
5001
5002
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005004static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, char_u **, int), expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005005
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00005007 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005008 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005010 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011call_user_expand_func(
5012 void *(*user_expand_func)(char_u *, int, char_u **, int),
5013 expand_T *xp,
5014 int *num_file,
5015 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005017 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005018 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005021 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005022 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
Bram Moolenaarb7515462013-06-29 12:58:33 +02005024 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005025 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 *num_file = 0;
5027 *file = NULL;
5028
Bram Moolenaarb7515462013-06-29 12:58:33 +02005029 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005030 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005031 keep = ccline.cmdbuff[ccline.cmdlen];
5032 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005033 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005034
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005035 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005036 args[1] = xp->xp_line;
5037 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 args[2] = num;
5039
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005040 /* Save the cmdline, we don't know what the function may do. */
5041 save_ccline = ccline;
5042 ccline.cmdbuff = NULL;
5043 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005045
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005046 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005047
5048 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005050 if (ccline.cmdbuff != NULL)
5051 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005052
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005053 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005054 return ret;
5055}
5056
5057/*
5058 * Expand names with a function defined by the user.
5059 */
5060 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005061ExpandUserDefined(
5062 expand_T *xp,
5063 regmatch_T *regmatch,
5064 int *num_file,
5065 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005066{
5067 char_u *retstr;
5068 char_u *s;
5069 char_u *e;
5070 char_u keep;
5071 garray_T ga;
5072
5073 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5074 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 return FAIL;
5076
5077 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005078 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 e = vim_strchr(s, '\n');
5081 if (e == NULL)
5082 e = s + STRLEN(s);
5083 keep = *e;
5084 *e = 0;
5085
5086 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5087 {
5088 *e = keep;
5089 if (*e != NUL)
5090 ++e;
5091 continue;
5092 }
5093
5094 if (ga_grow(&ga, 1) == FAIL)
5095 break;
5096
5097 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5098 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099
5100 *e = keep;
5101 if (*e != NUL)
5102 ++e;
5103 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005104 vim_free(retstr);
5105 *file = ga.ga_data;
5106 *num_file = ga.ga_len;
5107 return OK;
5108}
5109
5110/*
5111 * Expand names with a list returned by a function defined by the user.
5112 */
5113 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005114ExpandUserList(
5115 expand_T *xp,
5116 int *num_file,
5117 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005118{
5119 list_T *retlist;
5120 listitem_T *li;
5121 garray_T ga;
5122
5123 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5124 if (retlist == NULL)
5125 return FAIL;
5126
5127 ga_init2(&ga, (int)sizeof(char *), 3);
5128 /* Loop over the items in the list. */
5129 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5130 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005131 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5132 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005133
5134 if (ga_grow(&ga, 1) == FAIL)
5135 break;
5136
5137 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005138 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005139 ++ga.ga_len;
5140 }
5141 list_unref(retlist);
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 *file = ga.ga_data;
5144 *num_file = ga.ga_len;
5145 return OK;
5146}
5147#endif
5148
5149/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005150 * Expand color scheme, compiler or filetype names.
5151 * Search from 'runtimepath':
5152 * 'runtimepath'/{dirnames}/{pat}.vim
5153 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5154 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5155 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5156 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005157 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 */
5159 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005160ExpandRTDir(
5161 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005162 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005163 int *num_file,
5164 char_u ***file,
5165 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 char_u *s;
5168 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005169 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005171 int i;
5172 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 *num_file = 0;
5175 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005176 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005177 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005179 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005181 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5182 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005184 ga_clear_strings(&ga);
5185 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005187 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005188 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005189 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005191
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005192 if (flags & DIP_START) {
5193 for (i = 0; dirnames[i] != NULL; ++i)
5194 {
5195 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5196 if (s == NULL)
5197 {
5198 ga_clear_strings(&ga);
5199 return FAIL;
5200 }
5201 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5202 globpath(p_pp, s, &ga, 0);
5203 vim_free(s);
5204 }
5205 }
5206
5207 if (flags & DIP_OPT) {
5208 for (i = 0; dirnames[i] != NULL; ++i)
5209 {
5210 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5211 if (s == NULL)
5212 {
5213 ga_clear_strings(&ga);
5214 return FAIL;
5215 }
5216 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5217 globpath(p_pp, s, &ga, 0);
5218 vim_free(s);
5219 }
5220 }
5221
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005222 for (i = 0; i < ga.ga_len; ++i)
5223 {
5224 match = ((char_u **)ga.ga_data)[i];
5225 s = match;
5226 e = s + STRLEN(s);
5227 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5228 {
5229 e -= 4;
5230 for (s = e; s > match; mb_ptr_back(match, s))
5231 if (s < match || vim_ispathsep(*s))
5232 break;
5233 ++s;
5234 *e = NUL;
5235 mch_memmove(match, s, e - s + 1);
5236 }
5237 }
5238
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005239 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005240 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005241
5242 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005243 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005244 remove_duplicates(&ga);
5245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 *file = ga.ga_data;
5247 *num_file = ga.ga_len;
5248 return OK;
5249}
5250
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005251/*
5252 * Expand loadplugin names:
5253 * 'packpath'/pack/ * /opt/{pat}
5254 */
5255 static int
5256ExpandPackAddDir(
5257 char_u *pat,
5258 int *num_file,
5259 char_u ***file)
5260{
5261 char_u *s;
5262 char_u *e;
5263 char_u *match;
5264 garray_T ga;
5265 int i;
5266 int pat_len;
5267
5268 *num_file = 0;
5269 *file = NULL;
5270 pat_len = (int)STRLEN(pat);
5271 ga_init2(&ga, (int)sizeof(char *), 10);
5272
5273 s = alloc((unsigned)(pat_len + 26));
5274 if (s == NULL)
5275 {
5276 ga_clear_strings(&ga);
5277 return FAIL;
5278 }
5279 sprintf((char *)s, "pack/*/opt/%s*", pat);
5280 globpath(p_pp, s, &ga, 0);
5281 vim_free(s);
5282
5283 for (i = 0; i < ga.ga_len; ++i)
5284 {
5285 match = ((char_u **)ga.ga_data)[i];
5286 s = gettail(match);
5287 e = s + STRLEN(s);
5288 mch_memmove(match, s, e - s + 1);
5289 }
5290
5291 if (ga.ga_len == 0)
5292 return FAIL;
5293
5294 /* Sort and remove duplicates which can happen when specifying multiple
5295 * directories in dirnames. */
5296 remove_duplicates(&ga);
5297
5298 *file = ga.ga_data;
5299 *num_file = ga.ga_len;
5300 return OK;
5301}
5302
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303#endif
5304
5305#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5306/*
5307 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005308 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005310 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311globpath(
5312 char_u *path,
5313 char_u *file,
5314 garray_T *ga,
5315 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316{
5317 expand_T xpc;
5318 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 int num_p;
5321 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322
5323 buf = alloc(MAXPATHL);
5324 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005325 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005327 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005329
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /* Loop over all entries in {path}. */
5331 while (*path != NUL)
5332 {
5333 /* Copy one item of the path to buf[] and concatenate the file name. */
5334 copy_option_part(&path, buf, MAXPATHL, ",");
5335 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5336 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005337# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005338 /* Using the platform's path separator (\) makes vim incorrectly
5339 * treat it as an escape character, use '/' instead. */
5340 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5341 STRCAT(buf, "/");
5342# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005346 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5347 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005349 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005351 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 for (i = 0; i < num_p; ++i)
5354 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005355 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005356 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005357 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 FreeWild(num_p, p);
5362 }
5363 }
5364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365
5366 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367}
5368
5369#endif
5370
5371#if defined(FEAT_CMDHIST) || defined(PROTO)
5372
5373/*********************************
5374 * Command line history stuff *
5375 *********************************/
5376
5377/*
5378 * Translate a history character to the associated type number.
5379 */
5380 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
5383 if (c == ':')
5384 return HIST_CMD;
5385 if (c == '=')
5386 return HIST_EXPR;
5387 if (c == '@')
5388 return HIST_INPUT;
5389 if (c == '>')
5390 return HIST_DEBUG;
5391 return HIST_SEARCH; /* must be '?' or '/' */
5392}
5393
5394/*
5395 * Table of history names.
5396 * These names are used in :history and various hist...() functions.
5397 * It is sufficient to give the significant prefix of a history name.
5398 */
5399
5400static char *(history_names[]) =
5401{
5402 "cmd",
5403 "search",
5404 "expr",
5405 "input",
5406 "debug",
5407 NULL
5408};
5409
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005410#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5411/*
5412 * Function given to ExpandGeneric() to obtain the possible first
5413 * arguments of the ":history command.
5414 */
5415 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005416get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005417{
5418 static char_u compl[2] = { NUL, NUL };
5419 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005420 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005421 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5422
5423 if (idx < short_names_count)
5424 {
5425 compl[0] = (char_u)short_names[idx];
5426 return compl;
5427 }
5428 if (idx < short_names_count + history_name_count)
5429 return (char_u *)history_names[idx - short_names_count];
5430 if (idx == short_names_count + history_name_count)
5431 return (char_u *)"all";
5432 return NULL;
5433}
5434#endif
5435
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436/*
5437 * init_history() - Initialize the command line history.
5438 * Also used to re-allocate the history when the size changes.
5439 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005440 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442{
5443 int newlen; /* new length of history table */
5444 histentry_T *temp;
5445 int i;
5446 int j;
5447 int type;
5448
5449 /*
5450 * If size of history table changed, reallocate it
5451 */
5452 newlen = (int)p_hi;
5453 if (newlen != hislen) /* history length changed */
5454 {
5455 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5456 {
5457 if (newlen)
5458 {
5459 temp = (histentry_T *)lalloc(
5460 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5461 if (temp == NULL) /* out of memory! */
5462 {
5463 if (type == 0) /* first one: just keep the old length */
5464 {
5465 newlen = hislen;
5466 break;
5467 }
5468 /* Already changed one table, now we can only have zero
5469 * length for all tables. */
5470 newlen = 0;
5471 type = -1;
5472 continue;
5473 }
5474 }
5475 else
5476 temp = NULL;
5477 if (newlen == 0 || temp != NULL)
5478 {
5479 if (hisidx[type] < 0) /* there are no entries yet */
5480 {
5481 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005482 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 }
5484 else if (newlen > hislen) /* array becomes bigger */
5485 {
5486 for (i = 0; i <= hisidx[type]; ++i)
5487 temp[i] = history[type][i];
5488 j = i;
5489 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005490 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 for ( ; j < hislen; ++i, ++j)
5492 temp[i] = history[type][j];
5493 }
5494 else /* array becomes smaller or 0 */
5495 {
5496 j = hisidx[type];
5497 for (i = newlen - 1; ; --i)
5498 {
5499 if (i >= 0) /* copy newest entries */
5500 temp[i] = history[type][j];
5501 else /* remove older entries */
5502 vim_free(history[type][j].hisstr);
5503 if (--j < 0)
5504 j = hislen - 1;
5505 if (j == hisidx[type])
5506 break;
5507 }
5508 hisidx[type] = newlen - 1;
5509 }
5510 vim_free(history[type]);
5511 history[type] = temp;
5512 }
5513 }
5514 hislen = newlen;
5515 }
5516}
5517
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005520{
5521 hisptr->hisnum = 0;
5522 hisptr->viminfo = FALSE;
5523 hisptr->hisstr = NULL;
5524}
5525
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526/*
5527 * Check if command line 'str' is already in history.
5528 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5529 */
5530 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005531in_history(
5532 int type,
5533 char_u *str,
5534 int move_to_front, /* Move the entry to the front if it exists */
5535 int sep,
5536 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537{
5538 int i;
5539 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005540 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541
5542 if (hisidx[type] < 0)
5543 return FALSE;
5544 i = hisidx[type];
5545 do
5546 {
5547 if (history[type][i].hisstr == NULL)
5548 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005549
5550 /* For search history, check that the separator character matches as
5551 * well. */
5552 p = history[type][i].hisstr;
5553 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005554 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005555 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
5557 if (!move_to_front)
5558 return TRUE;
5559 last_i = i;
5560 break;
5561 }
5562 if (--i < 0)
5563 i = hislen - 1;
5564 } while (i != hisidx[type]);
5565
5566 if (last_i >= 0)
5567 {
5568 str = history[type][i].hisstr;
5569 while (i != hisidx[type])
5570 {
5571 if (++i >= hislen)
5572 i = 0;
5573 history[type][last_i] = history[type][i];
5574 last_i = i;
5575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005577 history[type][i].viminfo = FALSE;
5578 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 return TRUE;
5580 }
5581 return FALSE;
5582}
5583
5584/*
5585 * Convert history name (from table above) to its HIST_ equivalent.
5586 * When "name" is empty, return "cmd" history.
5587 * Returns -1 for unknown history name.
5588 */
5589 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
5592 int i;
5593 int len = (int)STRLEN(name);
5594
5595 /* No argument: use current history. */
5596 if (len == 0)
5597 return hist_char2type(ccline.cmdfirstc);
5598
5599 for (i = 0; history_names[i] != NULL; ++i)
5600 if (STRNICMP(name, history_names[i], len) == 0)
5601 return i;
5602
5603 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5604 return hist_char2type(name[0]);
5605
5606 return -1;
5607}
5608
5609static int last_maptick = -1; /* last seen maptick */
5610
5611/*
5612 * Add the given string to the given history. If the string is already in the
5613 * history then it is moved to the front. "histype" may be one of he HIST_
5614 * values.
5615 */
5616 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617add_to_history(
5618 int histype,
5619 char_u *new_entry,
5620 int in_map, /* consider maptick when inside a mapping */
5621 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622{
5623 histentry_T *hisptr;
5624 int len;
5625
5626 if (hislen == 0) /* no history */
5627 return;
5628
Bram Moolenaara939e432013-11-09 05:30:26 +01005629 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5630 return;
5631
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 /*
5633 * Searches inside the same mapping overwrite each other, so that only
5634 * the last line is kept. Be careful not to remove a line that was moved
5635 * down, only lines that were added.
5636 */
5637 if (histype == HIST_SEARCH && in_map)
5638 {
5639 if (maptick == last_maptick)
5640 {
5641 /* Current line is from the same mapping, remove it */
5642 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5643 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005644 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 --hisnum[histype];
5646 if (--hisidx[HIST_SEARCH] < 0)
5647 hisidx[HIST_SEARCH] = hislen - 1;
5648 }
5649 last_maptick = -1;
5650 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005651 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 {
5653 if (++hisidx[histype] == hislen)
5654 hisidx[histype] = 0;
5655 hisptr = &history[histype][hisidx[histype]];
5656 vim_free(hisptr->hisstr);
5657
5658 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005659 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5661 if (hisptr->hisstr != NULL)
5662 hisptr->hisstr[len + 1] = sep;
5663
5664 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005665 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 if (histype == HIST_SEARCH && in_map)
5667 last_maptick = maptick;
5668 }
5669}
5670
5671#if defined(FEAT_EVAL) || defined(PROTO)
5672
5673/*
5674 * Get identifier of newest history entry.
5675 * "histype" may be one of the HIST_ values.
5676 */
5677 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5681 || hisidx[histype] < 0)
5682 return -1;
5683
5684 return history[histype][hisidx[histype]].hisnum;
5685}
5686
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 * Calculate history index from a number:
5689 * num > 0: seen as identifying number of a history entry
5690 * num < 0: relative position in history wrt newest entry
5691 * "histype" may be one of the HIST_ values.
5692 */
5693 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005694calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695{
5696 int i;
5697 histentry_T *hist;
5698 int wrapped = FALSE;
5699
5700 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5701 || (i = hisidx[histype]) < 0 || num == 0)
5702 return -1;
5703
5704 hist = history[histype];
5705 if (num > 0)
5706 {
5707 while (hist[i].hisnum > num)
5708 if (--i < 0)
5709 {
5710 if (wrapped)
5711 break;
5712 i += hislen;
5713 wrapped = TRUE;
5714 }
5715 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5716 return i;
5717 }
5718 else if (-num <= hislen)
5719 {
5720 i += num + 1;
5721 if (i < 0)
5722 i += hislen;
5723 if (hist[i].hisstr != NULL)
5724 return i;
5725 }
5726 return -1;
5727}
5728
5729/*
5730 * Get a history entry by its index.
5731 * "histype" may be one of the HIST_ values.
5732 */
5733 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005734get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 idx = calc_hist_idx(histype, idx);
5737 if (idx >= 0)
5738 return history[histype][idx].hisstr;
5739 else
5740 return (char_u *)"";
5741}
5742
5743/*
5744 * Clear all entries of a history.
5745 * "histype" may be one of the HIST_ values.
5746 */
5747 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005748clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 int i;
5751 histentry_T *hisptr;
5752
5753 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5754 {
5755 hisptr = history[histype];
5756 for (i = hislen; i--;)
5757 {
5758 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005759 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005760 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761 }
5762 hisidx[histype] = -1; /* mark history as cleared */
5763 hisnum[histype] = 0; /* reset identifier counter */
5764 return OK;
5765 }
5766 return FAIL;
5767}
5768
5769/*
5770 * Remove all entries matching {str} from a history.
5771 * "histype" may be one of the HIST_ values.
5772 */
5773 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 regmatch_T regmatch;
5777 histentry_T *hisptr;
5778 int idx;
5779 int i;
5780 int last;
5781 int found = FALSE;
5782
5783 regmatch.regprog = NULL;
5784 regmatch.rm_ic = FALSE; /* always match case */
5785 if (hislen != 0
5786 && histype >= 0
5787 && histype < HIST_COUNT
5788 && *str != NUL
5789 && (idx = hisidx[histype]) >= 0
5790 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5791 != NULL)
5792 {
5793 i = last = idx;
5794 do
5795 {
5796 hisptr = &history[histype][i];
5797 if (hisptr->hisstr == NULL)
5798 break;
5799 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5800 {
5801 found = TRUE;
5802 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005803 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
5805 else
5806 {
5807 if (i != last)
5808 {
5809 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005810 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
5812 if (--last < 0)
5813 last += hislen;
5814 }
5815 if (--i < 0)
5816 i += hislen;
5817 } while (i != idx);
5818 if (history[histype][idx].hisstr == NULL)
5819 hisidx[histype] = -1;
5820 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005821 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 return found;
5823}
5824
5825/*
5826 * Remove an indexed entry from a history.
5827 * "histype" may be one of the HIST_ values.
5828 */
5829 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 int i, j;
5833
5834 i = calc_hist_idx(histype, idx);
5835 if (i < 0)
5836 return FALSE;
5837 idx = hisidx[histype];
5838 vim_free(history[histype][i].hisstr);
5839
5840 /* When deleting the last added search string in a mapping, reset
5841 * last_maptick, so that the last added search string isn't deleted again.
5842 */
5843 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5844 last_maptick = -1;
5845
5846 while (i != idx)
5847 {
5848 j = (i + 1) % hislen;
5849 history[histype][i] = history[histype][j];
5850 i = j;
5851 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005852 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 if (--i < 0)
5854 i += hislen;
5855 hisidx[histype] = i;
5856 return TRUE;
5857}
5858
5859#endif /* FEAT_EVAL */
5860
5861#if defined(FEAT_CRYPT) || defined(PROTO)
5862/*
5863 * Very specific function to remove the value in ":set key=val" from the
5864 * history.
5865 */
5866 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 char_u *p;
5870 int i;
5871
5872 i = hisidx[HIST_CMD];
5873 if (i < 0)
5874 return;
5875 p = history[HIST_CMD][i].hisstr;
5876 if (p != NULL)
5877 for ( ; *p; ++p)
5878 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5879 {
5880 p = vim_strchr(p + 3, '=');
5881 if (p == NULL)
5882 break;
5883 ++p;
5884 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5885 if (p[i] == '\\' && p[i + 1])
5886 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005887 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 --p;
5889 }
5890}
5891#endif
5892
5893#endif /* FEAT_CMDHIST */
5894
Bram Moolenaar064154c2016-03-19 22:50:43 +01005895#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005896/*
5897 * Get pointer to the command line info to use. cmdline_paste() may clear
5898 * ccline and put the previous value in prev_ccline.
5899 */
5900 static struct cmdline_info *
5901get_ccline_ptr(void)
5902{
5903 if ((State & CMDLINE) == 0)
5904 return NULL;
5905 if (ccline.cmdbuff != NULL)
5906 return &ccline;
5907 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5908 return &prev_ccline;
5909 return NULL;
5910}
Bram Moolenaar064154c2016-03-19 22:50:43 +01005911#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005912
Bram Moolenaar064154c2016-03-19 22:50:43 +01005913#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005914/*
5915 * Get the current command line in allocated memory.
5916 * Only works when the command line is being edited.
5917 * Returns NULL when something is wrong.
5918 */
5919 char_u *
5920get_cmdline_str(void)
5921{
5922 struct cmdline_info *p = get_ccline_ptr();
5923
5924 if (p == NULL)
5925 return NULL;
5926 return vim_strnsave(p->cmdbuff, p->cmdlen);
5927}
5928
5929/*
5930 * Get the current command line position, counted in bytes.
5931 * Zero is the first position.
5932 * Only works when the command line is being edited.
5933 * Returns -1 when something is wrong.
5934 */
5935 int
5936get_cmdline_pos(void)
5937{
5938 struct cmdline_info *p = get_ccline_ptr();
5939
5940 if (p == NULL)
5941 return -1;
5942 return p->cmdpos;
5943}
5944
5945/*
5946 * Set the command line byte position to "pos". Zero is the first position.
5947 * Only works when the command line is being edited.
5948 * Returns 1 when failed, 0 when OK.
5949 */
5950 int
5951set_cmdline_pos(
5952 int pos)
5953{
5954 struct cmdline_info *p = get_ccline_ptr();
5955
5956 if (p == NULL)
5957 return 1;
5958
5959 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5960 * changed the command line. */
5961 if (pos < 0)
5962 new_cmdpos = 0;
5963 else
5964 new_cmdpos = pos;
5965 return 0;
5966}
5967#endif
5968
5969#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
5970/*
5971 * Get the current command-line type.
5972 * Returns ':' or '/' or '?' or '@' or '>' or '-'
5973 * Only works when the command line is being edited.
5974 * Returns NUL when something is wrong.
5975 */
5976 int
5977get_cmdline_type(void)
5978{
5979 struct cmdline_info *p = get_ccline_ptr();
5980
5981 if (p == NULL)
5982 return NUL;
5983 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01005984 return
5985# ifdef FEAT_EVAL
5986 (p->input_fn) ? '@' :
5987# endif
5988 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01005989 return p->cmdfirstc;
5990}
5991#endif
5992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5994/*
5995 * Get indices "num1,num2" that specify a range within a list (not a range of
5996 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5997 * Returns OK if parsed successfully, otherwise FAIL.
5998 */
5999 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006000get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001{
6002 int len;
6003 int first = FALSE;
6004 long num;
6005
6006 *str = skipwhite(*str);
6007 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6008 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006009 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 *str += len;
6011 *num1 = (int)num;
6012 first = TRUE;
6013 }
6014 *str = skipwhite(*str);
6015 if (**str == ',') /* parse "to" part of range */
6016 {
6017 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006018 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (len > 0)
6020 {
6021 *num2 = (int)num;
6022 *str = skipwhite(*str + len);
6023 }
6024 else if (!first) /* no number given at all */
6025 return FAIL;
6026 }
6027 else if (first) /* only one number given */
6028 *num2 = *num1;
6029 return OK;
6030}
6031#endif
6032
6033#if defined(FEAT_CMDHIST) || defined(PROTO)
6034/*
6035 * :history command - print a history
6036 */
6037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 histentry_T *hist;
6041 int histype1 = HIST_CMD;
6042 int histype2 = HIST_CMD;
6043 int hisidx1 = 1;
6044 int hisidx2 = -1;
6045 int idx;
6046 int i, j, k;
6047 char_u *end;
6048 char_u *arg = eap->arg;
6049
6050 if (hislen == 0)
6051 {
6052 MSG(_("'history' option is zero"));
6053 return;
6054 }
6055
6056 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6057 {
6058 end = arg;
6059 while (ASCII_ISALPHA(*end)
6060 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6061 end++;
6062 i = *end;
6063 *end = NUL;
6064 histype1 = get_histtype(arg);
6065 if (histype1 == -1)
6066 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006067 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 {
6069 histype1 = 0;
6070 histype2 = HIST_COUNT-1;
6071 }
6072 else
6073 {
6074 *end = i;
6075 EMSG(_(e_trailing));
6076 return;
6077 }
6078 }
6079 else
6080 histype2 = histype1;
6081 *end = i;
6082 }
6083 else
6084 end = arg;
6085 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6086 {
6087 EMSG(_(e_trailing));
6088 return;
6089 }
6090
6091 for (; !got_int && histype1 <= histype2; ++histype1)
6092 {
6093 STRCPY(IObuff, "\n # ");
6094 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6095 MSG_PUTS_TITLE(IObuff);
6096 idx = hisidx[histype1];
6097 hist = history[histype1];
6098 j = hisidx1;
6099 k = hisidx2;
6100 if (j < 0)
6101 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6102 if (k < 0)
6103 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6104 if (idx >= 0 && j <= k)
6105 for (i = idx + 1; !got_int; ++i)
6106 {
6107 if (i == hislen)
6108 i = 0;
6109 if (hist[i].hisstr != NULL
6110 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6111 {
6112 msg_putchar('\n');
6113 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6114 hist[i].hisnum);
6115 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6116 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006117 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 else
6119 STRCAT(IObuff, hist[i].hisstr);
6120 msg_outtrans(IObuff);
6121 out_flush();
6122 }
6123 if (i == idx)
6124 break;
6125 }
6126 }
6127}
6128#endif
6129
6130#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006131/*
6132 * Buffers for history read from a viminfo file. Only valid while reading.
6133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6135static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6136static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6137static int viminfo_add_at_front = FALSE;
6138
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006139static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140
6141/*
6142 * Translate a history type number to the associated character.
6143 */
6144 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006145hist_type2char(
6146 int type,
6147 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 if (type == HIST_CMD)
6150 return ':';
6151 if (type == HIST_SEARCH)
6152 {
6153 if (use_question)
6154 return '?';
6155 else
6156 return '/';
6157 }
6158 if (type == HIST_EXPR)
6159 return '=';
6160 return '@';
6161}
6162
6163/*
6164 * Prepare for reading the history from the viminfo file.
6165 * This allocates history arrays to store the read history lines.
6166 */
6167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 int i;
6171 int num;
6172 int type;
6173 int len;
6174
6175 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006176 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 if (asklen > hislen)
6178 asklen = hislen;
6179
6180 for (type = 0; type < HIST_COUNT; ++type)
6181 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006182 /* Count the number of empty spaces in the history list. Entries read
6183 * from viminfo previously are also considered empty. If there are
6184 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006186 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 num++;
6188 len = asklen;
6189 if (num > len)
6190 len = num;
6191 if (len <= 0)
6192 viminfo_history[type] = NULL;
6193 else
6194 viminfo_history[type] =
6195 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6196 if (viminfo_history[type] == NULL)
6197 len = 0;
6198 viminfo_hislen[type] = len;
6199 viminfo_hisidx[type] = 0;
6200 }
6201}
6202
6203/*
6204 * Accept a line from the viminfo, store it in the history array when it's
6205 * new.
6206 */
6207 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006208read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 int type;
6211 long_u len;
6212 char_u *val;
6213 char_u *p;
6214
6215 type = hist_char2type(virp->vir_line[0]);
6216 if (viminfo_hisidx[type] < viminfo_hislen[type])
6217 {
6218 val = viminfo_readstring(virp, 1, TRUE);
6219 if (val != NULL && *val != NUL)
6220 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006221 int sep = (*val == ' ' ? NUL : *val);
6222
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006224 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
6226 /* Need to re-allocate to append the separator byte. */
6227 len = STRLEN(val);
6228 p = lalloc(len + 2, TRUE);
6229 if (p != NULL)
6230 {
6231 if (type == HIST_SEARCH)
6232 {
6233 /* Search entry: Move the separator from the first
6234 * column to after the NUL. */
6235 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006236 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
6238 else
6239 {
6240 /* Not a search entry: No separator in the viminfo
6241 * file, add a NUL separator. */
6242 mch_memmove(p, val, (size_t)len + 1);
6243 p[len + 1] = NUL;
6244 }
6245 viminfo_history[type][viminfo_hisidx[type]++] = p;
6246 }
6247 }
6248 }
6249 vim_free(val);
6250 }
6251 return viminfo_readline(virp);
6252}
6253
Bram Moolenaar07219f92013-04-14 23:19:36 +02006254/*
6255 * Finish reading history lines from viminfo. Not used when writing viminfo.
6256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006258finish_viminfo_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 int idx;
6261 int i;
6262 int type;
6263
6264 for (type = 0; type < HIST_COUNT; ++type)
6265 {
6266 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006267 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 idx = hisidx[type] + viminfo_hisidx[type];
6269 if (idx >= hislen)
6270 idx -= hislen;
6271 else if (idx < 0)
6272 idx = hislen - 1;
6273 if (viminfo_add_at_front)
6274 hisidx[type] = idx;
6275 else
6276 {
6277 if (hisidx[type] == -1)
6278 hisidx[type] = hislen - 1;
6279 do
6280 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006281 if (history[type][idx].hisstr != NULL
6282 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 break;
6284 if (++idx == hislen)
6285 idx = 0;
6286 } while (idx != hisidx[type]);
6287 if (idx != hisidx[type] && --idx < 0)
6288 idx = hislen - 1;
6289 }
6290 for (i = 0; i < viminfo_hisidx[type]; i++)
6291 {
6292 vim_free(history[type][idx].hisstr);
6293 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006294 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 if (--idx < 0)
6296 idx = hislen - 1;
6297 }
6298 idx += 1;
6299 idx %= hislen;
6300 for (i = 0; i < viminfo_hisidx[type]; i++)
6301 {
6302 history[type][idx++].hisnum = ++hisnum[type];
6303 idx %= hislen;
6304 }
6305 vim_free(viminfo_history[type]);
6306 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006307 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 }
6309}
6310
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006311/*
6312 * Write history to viminfo file in "fp".
6313 * When "merge" is TRUE merge history lines with a previously read viminfo
6314 * file, data is in viminfo_history[].
6315 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6316 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006318write_viminfo_history(
6319 FILE *fp,
6320 int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321{
6322 int i;
6323 int type;
6324 int num_saved;
6325 char_u *p;
6326 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006327 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006328
6329 init_history();
6330 if (hislen == 0)
6331 return;
6332 for (type = 0; type < HIST_COUNT; ++type)
6333 {
6334 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6335 if (num_saved == 0)
6336 continue;
6337 if (num_saved < 0) /* Use default */
6338 num_saved = hislen;
6339 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6340 type == HIST_CMD ? _("Command Line") :
6341 type == HIST_SEARCH ? _("Search String") :
6342 type == HIST_EXPR ? _("Expression") :
6343 _("Input Line"));
6344 if (num_saved > hislen)
6345 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006346
6347 /*
6348 * Merge typed and viminfo history:
6349 * round 1: history of typed commands.
6350 * round 2: history from recently read viminfo.
6351 */
6352 for (round = 1; round <= 2; ++round)
6353 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006354 if (round == 1)
6355 /* start at newest entry, somewhere in the list */
6356 i = hisidx[type];
6357 else if (viminfo_hisidx[type] > 0)
6358 /* start at newest entry, first in the list */
6359 i = 0;
6360 else
6361 /* empty list */
6362 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006363 if (i >= 0)
6364 while (num_saved > 0
6365 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006367 p = round == 1 ? history[type][i].hisstr
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006368 : viminfo_history[type] == NULL ? NULL
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006369 : viminfo_history[type][i];
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006370 if (p != NULL && (round == 2
6371 || !merge
6372 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006374 --num_saved;
6375 fputc(hist_type2char(type, TRUE), fp);
6376 /* For the search history: put the separator in the
6377 * second column; use a space if there isn't one. */
6378 if (type == HIST_SEARCH)
6379 {
6380 c = p[STRLEN(p) + 1];
6381 putc(c == NUL ? ' ' : c, fp);
6382 }
6383 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006385 if (round == 1)
6386 {
6387 /* Decrement index, loop around and stop when back at
6388 * the start. */
6389 if (--i < 0)
6390 i = hislen - 1;
6391 if (i == hisidx[type])
6392 break;
6393 }
6394 else
6395 {
6396 /* Increment index. Stop at the end in the while. */
6397 ++i;
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006400 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006401 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006402 if (viminfo_history[type] != NULL)
6403 vim_free(viminfo_history[type][i]);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006404 vim_free(viminfo_history[type]);
6405 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006406 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006407 }
6408}
6409#endif /* FEAT_VIMINFO */
6410
6411#if defined(FEAT_FKMAP) || defined(PROTO)
6412/*
6413 * Write a character at the current cursor+offset position.
6414 * It is directly written into the command buffer block.
6415 */
6416 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006417cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
6419 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6420 {
6421 EMSG(_("E198: cmd_pchar beyond the command length"));
6422 return;
6423 }
6424 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6425 ccline.cmdbuff[ccline.cmdlen] = NUL;
6426}
6427
6428 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006429cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006430{
6431 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6432 {
6433 /* EMSG(_("cmd_gchar beyond the command length")); */
6434 return NUL;
6435 }
6436 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6437}
6438#endif
6439
6440#if defined(FEAT_CMDWIN) || defined(PROTO)
6441/*
6442 * Open a window on the current command line and history. Allow editing in
6443 * the window. Returns when the window is closed.
6444 * Returns:
6445 * CR if the command is to be executed
6446 * Ctrl_C if it is to be abandoned
6447 * K_IGNORE if editing continues
6448 */
6449 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006450ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 struct cmdline_info save_ccline;
6453 buf_T *old_curbuf = curbuf;
6454 win_T *old_curwin = curwin;
6455 buf_T *bp;
6456 win_T *wp;
6457 int i;
6458 linenr_T lnum;
6459 int histtype;
6460 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006461#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 int save_restart_edit = restart_edit;
6465 int save_State = State;
6466 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006467#ifdef FEAT_RIGHTLEFT
6468 int save_cmdmsg_rl = cmdmsg_rl;
6469#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006470#ifdef FEAT_FOLDING
6471 int save_KeyTyped;
6472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473
6474 /* Can't do this recursively. Can't do it when typing a password. */
6475 if (cmdwin_type != 0
6476# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6477 || cmdline_star > 0
6478# endif
6479 )
6480 {
6481 beep_flush();
6482 return K_IGNORE;
6483 }
6484
6485 /* Save current window sizes. */
6486 win_size_save(&winsizes);
6487
6488# ifdef FEAT_AUTOCMD
6489 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006490 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006492 /* don't use a new tab page */
6493 cmdmod.tab = 0;
6494
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 /* Create a window for the command-line buffer. */
6496 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6497 {
6498 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006499# ifdef FEAT_AUTOCMD
6500 unblock_autocmds();
6501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 return K_IGNORE;
6503 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006504 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006507 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006508 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6510 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6511 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006512#ifdef FEAT_FOLDING
6513 curwin->w_p_fen = FALSE;
6514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006516 curwin->w_p_rl = cmdmsg_rl;
6517 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006519 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520
6521# ifdef FEAT_AUTOCMD
6522 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006523 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524# endif
6525
Bram Moolenaar46152342005-09-07 21:18:43 +00006526 /* Showing the prompt may have set need_wait_return, reset it. */
6527 need_wait_return = FALSE;
6528
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006529 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6531 {
6532 if (p_wc == TAB)
6533 {
6534 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6535 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6536 }
6537 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6538 }
6539
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006540 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6541 * sets 'textwidth' to 78). */
6542 curbuf->b_p_tw = 0;
6543
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 /* Fill the buffer with the history. */
6545 init_history();
6546 if (hislen > 0)
6547 {
6548 i = hisidx[histtype];
6549 if (i >= 0)
6550 {
6551 lnum = 0;
6552 do
6553 {
6554 if (++i == hislen)
6555 i = 0;
6556 if (history[histtype][i].hisstr != NULL)
6557 ml_append(lnum++, history[histtype][i].hisstr,
6558 (colnr_T)0, FALSE);
6559 }
6560 while (i != hisidx[histtype]);
6561 }
6562 }
6563
6564 /* Replace the empty last line with the current command-line and put the
6565 * cursor there. */
6566 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6567 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6568 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006569 changed_line_abv_curs();
6570 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006571 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
6573 /* Save the command line info, can be used recursively. */
6574 save_ccline = ccline;
6575 ccline.cmdbuff = NULL;
6576 ccline.cmdprompt = NULL;
6577
6578 /* No Ex mode here! */
6579 exmode_active = 0;
6580
6581 State = NORMAL;
6582# ifdef FEAT_MOUSE
6583 setmouse();
6584# endif
6585
6586# ifdef FEAT_AUTOCMD
6587 /* Trigger CmdwinEnter autocommands. */
6588 typestr[0] = cmdwin_type;
6589 typestr[1] = NUL;
6590 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006591 if (restart_edit != 0) /* autocmd with ":startinsert" */
6592 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593# endif
6594
6595 i = RedrawingDisabled;
6596 RedrawingDisabled = 0;
6597
6598 /*
6599 * Call the main loop until <CR> or CTRL-C is typed.
6600 */
6601 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006602 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603
6604 RedrawingDisabled = i;
6605
6606# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006607
6608# ifdef FEAT_FOLDING
6609 save_KeyTyped = KeyTyped;
6610# endif
6611
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 /* Trigger CmdwinLeave autocommands. */
6613 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006614
6615# ifdef FEAT_FOLDING
6616 /* Restore KeyTyped in case it is modified by autocommands */
6617 KeyTyped = save_KeyTyped;
6618# endif
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620# endif
6621
Bram Moolenaarccc18222007-05-10 18:25:20 +00006622 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 ccline = save_ccline;
6624 cmdwin_type = 0;
6625
6626 exmode_active = save_exmode;
6627
Bram Moolenaarf9821062008-06-20 16:31:07 +00006628 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 * this happens! */
6630 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6631 {
6632 cmdwin_result = Ctrl_C;
6633 EMSG(_("E199: Active window or buffer deleted"));
6634 }
6635 else
6636 {
6637# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6638 /* autocmds may abort script processing */
6639 if (aborting() && cmdwin_result != K_IGNORE)
6640 cmdwin_result = Ctrl_C;
6641# endif
6642 /* Set the new command line from the cmdline buffer. */
6643 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006644 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006646 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6647
6648 if (histtype == HIST_CMD)
6649 {
6650 /* Execute the command directly. */
6651 ccline.cmdbuff = vim_strsave((char_u *)p);
6652 cmdwin_result = CAR;
6653 }
6654 else
6655 {
6656 /* First need to cancel what we were doing. */
6657 ccline.cmdbuff = NULL;
6658 stuffcharReadbuff(':');
6659 stuffReadbuff((char_u *)p);
6660 stuffcharReadbuff(CAR);
6661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 }
6663 else if (cmdwin_result == K_XF2) /* :qa typed */
6664 {
6665 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6666 cmdwin_result = CAR;
6667 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006668 else if (cmdwin_result == Ctrl_C)
6669 {
6670 /* :q or :close, don't execute any command
6671 * and don't modify the cmd window. */
6672 ccline.cmdbuff = NULL;
6673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 else
6675 ccline.cmdbuff = vim_strsave(ml_get_curline());
6676 if (ccline.cmdbuff == NULL)
6677 cmdwin_result = Ctrl_C;
6678 else
6679 {
6680 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6681 ccline.cmdbufflen = ccline.cmdlen + 1;
6682 ccline.cmdpos = curwin->w_cursor.col;
6683 if (ccline.cmdpos > ccline.cmdlen)
6684 ccline.cmdpos = ccline.cmdlen;
6685 if (cmdwin_result == K_IGNORE)
6686 {
6687 set_cmdspos_cursor();
6688 redrawcmd();
6689 }
6690 }
6691
6692# ifdef FEAT_AUTOCMD
6693 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006694 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02006696# ifdef FEAT_CONCEAL
6697 /* Avoid command-line window first character being concealed. */
6698 curwin->w_p_cole = 0;
6699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 wp = curwin;
6701 bp = curbuf;
6702 win_goto(old_curwin);
6703 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006704
6705 /* win_close() may have already wiped the buffer when 'bh' is
6706 * set to 'wipe' */
6707 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006708 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 /* Restore window sizes. */
6711 win_size_restore(&winsizes);
6712
6713# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006714 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715# endif
6716 }
6717
6718 ga_clear(&winsizes);
6719 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006720# ifdef FEAT_RIGHTLEFT
6721 cmdmsg_rl = save_cmdmsg_rl;
6722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724 State = save_State;
6725# ifdef FEAT_MOUSE
6726 setmouse();
6727# endif
6728
6729 return cmdwin_result;
6730}
6731#endif /* FEAT_CMDWIN */
6732
6733/*
6734 * Used for commands that either take a simple command string argument, or:
6735 * cmd << endmarker
6736 * {script}
6737 * endmarker
6738 * Returns a pointer to allocated memory with {script} or NULL.
6739 */
6740 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006741script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742{
6743 char_u *theline;
6744 char *end_pattern = NULL;
6745 char dot[] = ".";
6746 garray_T ga;
6747
6748 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6749 return NULL;
6750
6751 ga_init2(&ga, 1, 0x400);
6752
6753 if (cmd[2] != NUL)
6754 end_pattern = (char *)skipwhite(cmd + 2);
6755 else
6756 end_pattern = dot;
6757
6758 for (;;)
6759 {
6760 theline = eap->getline(
6761#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006762 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763#endif
6764 NUL, eap->cookie, 0);
6765
6766 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006767 {
6768 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771
6772 ga_concat(&ga, theline);
6773 ga_append(&ga, '\n');
6774 vim_free(theline);
6775 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006776 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777
6778 return (char_u *)ga.ga_data;
6779}