blob: 96c3a8ceba56fef014475a0099c5e263a2ce11cf [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);
114static int ExpandRTDir(char_u *pat, 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 {
503# ifdef FEAT_VERTSPLIT
504 win_redraw_last_status(topframe);
505# else
506 lastwin->w_redr_status = TRUE;
507# endif
508 redraw_statuslines();
509 }
510 KeyTyped = skt;
511 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000512 if (ccline.input_fn)
513 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 }
515#endif
516 }
517
518#ifdef FEAT_WILDMENU
519 /* Special translations for 'wildmenu' */
520 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
521 {
522 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000523 if (c == K_DOWN && ccline.cmdpos > 0
524 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000526 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 {
528 /* Hitting <Up>: Remove one submenu name in front of the
529 * cursor */
530 int found = FALSE;
531
532 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
533 i = 0;
534 while (--j > 0)
535 {
536 /* check for start of menu name */
537 if (ccline.cmdbuff[j] == ' '
538 && ccline.cmdbuff[j - 1] != '\\')
539 {
540 i = j + 1;
541 break;
542 }
543 /* check for start of submenu name */
544 if (ccline.cmdbuff[j] == '.'
545 && ccline.cmdbuff[j - 1] != '\\')
546 {
547 if (found)
548 {
549 i = j + 1;
550 break;
551 }
552 else
553 found = TRUE;
554 }
555 }
556 if (i > 0)
557 cmdline_del(i);
558 c = p_wc;
559 xpc.xp_context = EXPAND_NOTHING;
560 }
561 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000562 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000563 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000564 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 {
566 char_u upseg[5];
567
568 upseg[0] = PATHSEP;
569 upseg[1] = '.';
570 upseg[2] = '.';
571 upseg[3] = PATHSEP;
572 upseg[4] = NUL;
573
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000574 if (c == K_DOWN
575 && ccline.cmdpos > 0
576 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
577 && (ccline.cmdpos < 3
578 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
580 {
581 /* go down a directory */
582 c = p_wc;
583 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000584 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 {
586 /* If in a direct ancestor, strip off one ../ to go down */
587 int found = FALSE;
588
589 j = ccline.cmdpos;
590 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
591 while (--j > i)
592 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000593#ifdef FEAT_MBYTE
594 if (has_mbyte)
595 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 if (vim_ispathsep(ccline.cmdbuff[j]))
598 {
599 found = TRUE;
600 break;
601 }
602 }
603 if (found
604 && ccline.cmdbuff[j - 1] == '.'
605 && ccline.cmdbuff[j - 2] == '.'
606 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
607 {
608 cmdline_del(j - 2);
609 c = p_wc;
610 }
611 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000612 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 {
614 /* go up a directory */
615 int found = FALSE;
616
617 j = ccline.cmdpos - 1;
618 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
619 while (--j > i)
620 {
621#ifdef FEAT_MBYTE
622 if (has_mbyte)
623 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
624#endif
625 if (vim_ispathsep(ccline.cmdbuff[j])
626#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100627 && vim_strchr((char_u *)" *?[{`$%#",
628 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#endif
630 )
631 {
632 if (found)
633 {
634 i = j + 1;
635 break;
636 }
637 else
638 found = TRUE;
639 }
640 }
641
642 if (!found)
643 j = i;
644 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
645 j += 4;
646 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
647 && j == i)
648 j += 3;
649 else
650 j = 0;
651 if (j > 0)
652 {
653 /* TODO this is only for DOS/UNIX systems - need to put in
654 * machine-specific stuff here and in upseg init */
655 cmdline_del(j);
656 put_on_cmdline(upseg + 1, 3, FALSE);
657 }
658 else if (ccline.cmdpos > i)
659 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100660
661 /* Now complete in the new directory. Set KeyTyped in case the
662 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100664 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 }
666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668#endif /* FEAT_WILDMENU */
669
670 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
671 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
672 if (c == Ctrl_BSL)
673 {
674 ++no_mapping;
675 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000676 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 --no_mapping;
678 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200679 /* CTRL-\ e doesn't work when obtaining an expression, unless it
680 * is in a mapping. */
681 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
682 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 {
684 vungetc(c);
685 c = Ctrl_BSL;
686 }
687#ifdef FEAT_EVAL
688 else if (c == 'e')
689 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200690 char_u *p = NULL;
691 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692
693 /*
694 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000695 * Need to save and restore the current command line, to be
696 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 */
698 if (ccline.cmdpos == ccline.cmdlen)
699 new_cmdpos = 99999; /* keep it at the end */
700 else
701 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000702
703 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000705 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000706 if (c == '=')
707 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000708 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000709 * to avoid nasty things like going to another buffer when
710 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000711 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000712 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000714 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000715 restore_cmdline(&save_ccline);
716
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200717 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200719 len = (int)STRLEN(p);
720 if (realloc_cmdbuff(len + 1) == OK)
721 {
722 ccline.cmdlen = len;
723 STRCPY(ccline.cmdbuff, p);
724 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200726 /* Restore the cursor or use the position set with
727 * set_cmdline_pos(). */
728 if (new_cmdpos > ccline.cmdlen)
729 ccline.cmdpos = ccline.cmdlen;
730 else
731 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200733 KeyTyped = FALSE; /* Don't do p_wc completion. */
734 redrawcmd();
735 goto cmdline_changed;
736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 }
738 }
739 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100740 got_int = FALSE; /* don't abandon the command line */
741 did_emsg = FALSE;
742 emsg_on_display = FALSE;
743 redrawcmd();
744 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746#endif
747 else
748 {
749 if (c == Ctrl_G && p_im && restart_edit == 0)
750 restart_edit = 'a';
751 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
752 in history */
753 goto returncmd; /* back to Normal mode */
754 }
755 }
756
757#ifdef FEAT_CMDWIN
758 if (c == cedit_key || c == K_CMDWIN)
759 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200760 if (ex_normal_busy == 0 && got_int == FALSE)
761 {
762 /*
763 * Open a window to edit the command line (and history).
764 */
765 c = ex_window();
766 some_key_typed = TRUE;
767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 }
769# ifdef FEAT_DIGRAPHS
770 else
771# endif
772#endif
773#ifdef FEAT_DIGRAPHS
774 c = do_digraph(c);
775#endif
776
777 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
778 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
779 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000780 /* In Ex mode a backslash escapes a newline. */
781 if (exmode_active
782 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000783 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000784 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000785 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000787 if (c == K_KENTER)
788 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000790 else
791 {
792 gotesc = FALSE; /* Might have typed ESC previously, don't
793 truncate the cmdline now. */
794 if (ccheck_abbr(c + ABBR_OFF))
795 goto cmdline_changed;
796 if (!cmd_silent)
797 {
798 windgoto(msg_row, 0);
799 out_flush();
800 }
801 break;
802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 }
804
805 /*
806 * Completion for 'wildchar' or 'wildcharm' key.
807 * - hitting <ESC> twice means: abandon command line.
808 * - wildcard expansion is only done when the 'wildchar' key is really
809 * typed, not when it comes from a macro
810 */
811 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
812 {
813 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
814 {
815 /* if 'wildmode' contains "list" may still need to list */
816 if (xpc.xp_numfiles > 1
817 && !did_wild_list
818 && (wim_flags[wim_index] & WIM_LIST))
819 {
820 (void)showmatches(&xpc, FALSE);
821 redrawcmd();
822 did_wild_list = TRUE;
823 }
824 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100825 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
826 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100828 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
829 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 else
831 res = OK; /* don't insert 'wildchar' now */
832 }
833 else /* typed p_wc first time */
834 {
835 wim_index = 0;
836 j = ccline.cmdpos;
837 /* if 'wildmode' first contains "longest", get longest
838 * common part */
839 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100840 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
841 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100843 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
844 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
846 /* if interrupted while completing, behave like it failed */
847 if (got_int)
848 {
849 (void)vpeekc(); /* remove <C-C> from input stream */
850 got_int = FALSE; /* don't abandon the command line */
851 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
852#ifdef FEAT_WILDMENU
853 xpc.xp_context = EXPAND_NOTHING;
854#endif
855 goto cmdline_changed;
856 }
857
858 /* when more than one match, and 'wildmode' first contains
859 * "list", or no change and 'wildmode' contains "longest,list",
860 * list all matches */
861 if (res == OK && xpc.xp_numfiles > 1)
862 {
863 /* a "longest" that didn't do anything is skipped (but not
864 * "list:longest") */
865 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
866 wim_index = 1;
867 if ((wim_flags[wim_index] & WIM_LIST)
868#ifdef FEAT_WILDMENU
869 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
870#endif
871 )
872 {
873 if (!(wim_flags[0] & WIM_LONGEST))
874 {
875#ifdef FEAT_WILDMENU
876 int p_wmnu_save = p_wmnu;
877 p_wmnu = 0;
878#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100879 /* remove match */
880 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#ifdef FEAT_WILDMENU
882 p_wmnu = p_wmnu_save;
883#endif
884 }
885#ifdef FEAT_WILDMENU
886 (void)showmatches(&xpc, p_wmnu
887 && ((wim_flags[wim_index] & WIM_LIST) == 0));
888#else
889 (void)showmatches(&xpc, FALSE);
890#endif
891 redrawcmd();
892 did_wild_list = TRUE;
893 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100894 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
895 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100897 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
898 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 }
900 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200901 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 }
903#ifdef FEAT_WILDMENU
904 else if (xpc.xp_numfiles == -1)
905 xpc.xp_context = EXPAND_NOTHING;
906#endif
907 }
908 if (wim_index < 3)
909 ++wim_index;
910 if (c == ESC)
911 gotesc = TRUE;
912 if (res == OK)
913 goto cmdline_changed;
914 }
915
916 gotesc = FALSE;
917
918 /* <S-Tab> goes to last match, in a clumsy way */
919 if (c == K_S_TAB && KeyTyped)
920 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100921 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
922 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
923 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 goto cmdline_changed;
925 }
926
927 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
928 c = NL;
929
930 do_abbr = TRUE; /* default: check for abbreviation */
931
932 /*
933 * Big switch for a typed command line character.
934 */
935 switch (c)
936 {
937 case K_BS:
938 case Ctrl_H:
939 case K_DEL:
940 case K_KDEL:
941 case Ctrl_W:
942#ifdef FEAT_FKMAP
943 if (cmd_fkmap && c == K_BS)
944 c = K_DEL;
945#endif
946 if (c == K_KDEL)
947 c = K_DEL;
948
949 /*
950 * delete current character is the same as backspace on next
951 * character, except at end of line
952 */
953 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
954 ++ccline.cmdpos;
955#ifdef FEAT_MBYTE
956 if (has_mbyte && c == K_DEL)
957 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
958 ccline.cmdbuff + ccline.cmdpos);
959#endif
960 if (ccline.cmdpos > 0)
961 {
962 char_u *p;
963
964 j = ccline.cmdpos;
965 p = ccline.cmdbuff + j;
966#ifdef FEAT_MBYTE
967 if (has_mbyte)
968 {
969 p = mb_prevptr(ccline.cmdbuff, p);
970 if (c == Ctrl_W)
971 {
972 while (p > ccline.cmdbuff && vim_isspace(*p))
973 p = mb_prevptr(ccline.cmdbuff, p);
974 i = mb_get_class(p);
975 while (p > ccline.cmdbuff && mb_get_class(p) == i)
976 p = mb_prevptr(ccline.cmdbuff, p);
977 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000978 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 }
981 else
982#endif
983 if (c == Ctrl_W)
984 {
985 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
986 --p;
987 i = vim_iswordc(p[-1]);
988 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
989 && vim_iswordc(p[-1]) == i)
990 --p;
991 }
992 else
993 --p;
994 ccline.cmdpos = (int)(p - ccline.cmdbuff);
995 ccline.cmdlen -= j - ccline.cmdpos;
996 i = ccline.cmdpos;
997 while (i < ccline.cmdlen)
998 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
999
1000 /* Truncate at the end, required for multi-byte chars. */
1001 ccline.cmdbuff[ccline.cmdlen] = NUL;
1002 redrawcmd();
1003 }
1004 else if (ccline.cmdlen == 0 && c != Ctrl_W
1005 && ccline.cmdprompt == NULL && indent == 0)
1006 {
1007 /* In ex and debug mode it doesn't make sense to return. */
1008 if (exmode_active
1009#ifdef FEAT_EVAL
1010 || ccline.cmdfirstc == '>'
1011#endif
1012 )
1013 goto cmdline_not_changed;
1014
1015 vim_free(ccline.cmdbuff); /* no commandline to return */
1016 ccline.cmdbuff = NULL;
1017 if (!cmd_silent)
1018 {
1019#ifdef FEAT_RIGHTLEFT
1020 if (cmdmsg_rl)
1021 msg_col = Columns;
1022 else
1023#endif
1024 msg_col = 0;
1025 msg_putchar(' '); /* delete ':' */
1026 }
1027 redraw_cmdline = TRUE;
1028 goto returncmd; /* back to cmd mode */
1029 }
1030 goto cmdline_changed;
1031
1032 case K_INS:
1033 case K_KINS:
1034#ifdef FEAT_FKMAP
1035 /* if Farsi mode set, we are in reverse insert mode -
1036 Do not change the mode */
1037 if (cmd_fkmap)
1038 beep_flush();
1039 else
1040#endif
1041 ccline.overstrike = !ccline.overstrike;
1042#ifdef CURSOR_SHAPE
1043 ui_cursor_shape(); /* may show different cursor shape */
1044#endif
1045 goto cmdline_not_changed;
1046
1047 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001048 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 {
1050 /* ":lmap" mappings exists, toggle use of mappings. */
1051 State ^= LANGMAP;
1052#ifdef USE_IM_CONTROL
1053 im_set_active(FALSE); /* Disable input method */
1054#endif
1055 if (b_im_ptr != NULL)
1056 {
1057 if (State & LANGMAP)
1058 *b_im_ptr = B_IMODE_LMAP;
1059 else
1060 *b_im_ptr = B_IMODE_NONE;
1061 }
1062 }
1063#ifdef USE_IM_CONTROL
1064 else
1065 {
1066 /* There are no ":lmap" mappings, toggle IM. When
1067 * 'imdisable' is set don't try getting the status, it's
1068 * always off. */
1069 if ((p_imdisable && b_im_ptr != NULL)
1070 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1071 {
1072 im_set_active(FALSE); /* Disable input method */
1073 if (b_im_ptr != NULL)
1074 *b_im_ptr = B_IMODE_NONE;
1075 }
1076 else
1077 {
1078 im_set_active(TRUE); /* Enable input method */
1079 if (b_im_ptr != NULL)
1080 *b_im_ptr = B_IMODE_IM;
1081 }
1082 }
1083#endif
1084 if (b_im_ptr != NULL)
1085 {
1086 if (b_im_ptr == &curbuf->b_p_iminsert)
1087 set_iminsert_global();
1088 else
1089 set_imsearch_global();
1090 }
1091#ifdef CURSOR_SHAPE
1092 ui_cursor_shape(); /* may show different cursor shape */
1093#endif
1094#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1095 /* Show/unshow value of 'keymap' in status lines later. */
1096 status_redraw_curbuf();
1097#endif
1098 goto cmdline_not_changed;
1099
1100/* case '@': only in very old vi */
1101 case Ctrl_U:
1102 /* delete all characters left of the cursor */
1103 j = ccline.cmdpos;
1104 ccline.cmdlen -= j;
1105 i = ccline.cmdpos = 0;
1106 while (i < ccline.cmdlen)
1107 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1108 /* Truncate at the end, required for multi-byte chars. */
1109 ccline.cmdbuff[ccline.cmdlen] = NUL;
1110 redrawcmd();
1111 goto cmdline_changed;
1112
1113#ifdef FEAT_CLIPBOARD
1114 case Ctrl_Y:
1115 /* Copy the modeless selection, if there is one. */
1116 if (clip_star.state != SELECT_CLEARED)
1117 {
1118 if (clip_star.state == SELECT_DONE)
1119 clip_copy_modeless_selection(TRUE);
1120 goto cmdline_not_changed;
1121 }
1122 break;
1123#endif
1124
1125 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1126 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001127 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001128 * ":normal" runs out of characters. */
1129 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001130 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 goto cmdline_not_changed;
1132
1133 gotesc = TRUE; /* will free ccline.cmdbuff after
1134 putting it in history */
1135 goto returncmd; /* back to cmd mode */
1136
1137 case Ctrl_R: /* insert register */
1138#ifdef USE_ON_FLY_SCROLL
1139 dont_scroll = TRUE; /* disallow scrolling here */
1140#endif
1141 putcmdline('"', TRUE);
1142 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001143 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 if (i == Ctrl_O)
1145 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1146 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001147 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 --no_mapping;
1149#ifdef FEAT_EVAL
1150 /*
1151 * Insert the result of an expression.
1152 * Need to save the current command line, to be able to enter
1153 * a new one...
1154 */
1155 new_cmdpos = -1;
1156 if (c == '=')
1157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1159 {
1160 beep_flush();
1161 c = ESC;
1162 }
1163 else
1164 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001165 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001167 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169 }
1170#endif
1171 if (c != ESC) /* use ESC to cancel inserting register */
1172 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001173 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001174
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001175#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001176 /* When there was a serious error abort getting the
1177 * command line. */
1178 if (aborting())
1179 {
1180 gotesc = TRUE; /* will free ccline.cmdbuff after
1181 putting it in history */
1182 goto returncmd; /* back to cmd mode */
1183 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 KeyTyped = FALSE; /* Don't do p_wc completion. */
1186#ifdef FEAT_EVAL
1187 if (new_cmdpos >= 0)
1188 {
1189 /* set_cmdline_pos() was used */
1190 if (new_cmdpos > ccline.cmdlen)
1191 ccline.cmdpos = ccline.cmdlen;
1192 else
1193 ccline.cmdpos = new_cmdpos;
1194 }
1195#endif
1196 }
1197 redrawcmd();
1198 goto cmdline_changed;
1199
1200 case Ctrl_D:
1201 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1202 break; /* Use ^D as normal char instead */
1203
1204 redrawcmd();
1205 continue; /* don't do incremental search now */
1206
1207 case K_RIGHT:
1208 case K_S_RIGHT:
1209 case K_C_RIGHT:
1210 do
1211 {
1212 if (ccline.cmdpos >= ccline.cmdlen)
1213 break;
1214 i = cmdline_charsize(ccline.cmdpos);
1215 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1216 break;
1217 ccline.cmdspos += i;
1218#ifdef FEAT_MBYTE
1219 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001220 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 + ccline.cmdpos);
1222 else
1223#endif
1224 ++ccline.cmdpos;
1225 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001226 while ((c == K_S_RIGHT || c == K_C_RIGHT
1227 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1229#ifdef FEAT_MBYTE
1230 if (has_mbyte)
1231 set_cmdspos_cursor();
1232#endif
1233 goto cmdline_not_changed;
1234
1235 case K_LEFT:
1236 case K_S_LEFT:
1237 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001238 if (ccline.cmdpos == 0)
1239 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 do
1241 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 --ccline.cmdpos;
1243#ifdef FEAT_MBYTE
1244 if (has_mbyte) /* move to first byte of char */
1245 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1246 ccline.cmdbuff + ccline.cmdpos);
1247#endif
1248 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1249 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001250 while (ccline.cmdpos > 0
1251 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001252 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1254#ifdef FEAT_MBYTE
1255 if (has_mbyte)
1256 set_cmdspos_cursor();
1257#endif
1258 goto cmdline_not_changed;
1259
1260 case K_IGNORE:
Bram Moolenaar30405d32008-01-02 20:55:27 +00001261 /* Ignore mouse event or ex_window() result. */
1262 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263
Bram Moolenaar4770d092006-01-12 23:22:24 +00001264#ifdef FEAT_GUI_W32
1265 /* On Win32 ignore <M-F4>, we get it when closing the window was
1266 * cancelled. */
1267 case K_F4:
1268 if (mod_mask == MOD_MASK_ALT)
1269 {
1270 redrawcmd(); /* somehow the cmdline is cleared */
1271 goto cmdline_not_changed;
1272 }
1273 break;
1274#endif
1275
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#ifdef FEAT_MOUSE
1277 case K_MIDDLEDRAG:
1278 case K_MIDDLERELEASE:
1279 goto cmdline_not_changed; /* Ignore mouse */
1280
1281 case K_MIDDLEMOUSE:
1282# ifdef FEAT_GUI
1283 /* When GUI is active, also paste when 'mouse' is empty */
1284 if (!gui.in_use)
1285# endif
1286 if (!mouse_has(MOUSE_COMMAND))
1287 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001288# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001290 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001292# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001293 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 redrawcmd();
1295 goto cmdline_changed;
1296
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001297# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001299 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 redrawcmd();
1301 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304 case K_LEFTDRAG:
1305 case K_LEFTRELEASE:
1306 case K_RIGHTDRAG:
1307 case K_RIGHTRELEASE:
1308 /* Ignore drag and release events when the button-down wasn't
1309 * seen before. */
1310 if (ignore_drag_release)
1311 goto cmdline_not_changed;
1312 /* FALLTHROUGH */
1313 case K_LEFTMOUSE:
1314 case K_RIGHTMOUSE:
1315 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1316 ignore_drag_release = TRUE;
1317 else
1318 ignore_drag_release = FALSE;
1319# ifdef FEAT_GUI
1320 /* When GUI is active, also move when 'mouse' is empty */
1321 if (!gui.in_use)
1322# endif
1323 if (!mouse_has(MOUSE_COMMAND))
1324 goto cmdline_not_changed; /* Ignore mouse */
1325# ifdef FEAT_CLIPBOARD
1326 if (mouse_row < cmdline_row && clip_star.available)
1327 {
1328 int button, is_click, is_drag;
1329
1330 /*
1331 * Handle modeless selection.
1332 */
1333 button = get_mouse_button(KEY2TERMCAP1(c),
1334 &is_click, &is_drag);
1335 if (mouse_model_popup() && button == MOUSE_LEFT
1336 && (mod_mask & MOD_MASK_SHIFT))
1337 {
1338 /* Translate shift-left to right button. */
1339 button = MOUSE_RIGHT;
1340 mod_mask &= ~MOD_MASK_SHIFT;
1341 }
1342 clip_modeless(button, is_click, is_drag);
1343 goto cmdline_not_changed;
1344 }
1345# endif
1346
1347 set_cmdspos();
1348 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1349 ++ccline.cmdpos)
1350 {
1351 i = cmdline_charsize(ccline.cmdpos);
1352 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1353 && mouse_col < ccline.cmdspos % Columns + i)
1354 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001355# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (has_mbyte)
1357 {
1358 /* Count ">" for double-wide char that doesn't fit. */
1359 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001360 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 + ccline.cmdpos) - 1;
1362 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 ccline.cmdspos += i;
1365 }
1366 goto cmdline_not_changed;
1367
1368 /* Mouse scroll wheel: ignored here */
1369 case K_MOUSEDOWN:
1370 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001371 case K_MOUSELEFT:
1372 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 /* Alternate buttons ignored here */
1374 case K_X1MOUSE:
1375 case K_X1DRAG:
1376 case K_X1RELEASE:
1377 case K_X2MOUSE:
1378 case K_X2DRAG:
1379 case K_X2RELEASE:
1380 goto cmdline_not_changed;
1381
1382#endif /* FEAT_MOUSE */
1383
1384#ifdef FEAT_GUI
1385 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1386 case K_LEFTRELEASE_NM:
1387 goto cmdline_not_changed;
1388
1389 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001390 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
1392 gui_do_scroll();
1393 redrawcmd();
1394 }
1395 goto cmdline_not_changed;
1396
1397 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001398 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001400 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 redrawcmd();
1402 }
1403 goto cmdline_not_changed;
1404#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001405#ifdef FEAT_GUI_TABLINE
1406 case K_TABLINE:
1407 case K_TABMENU:
1408 /* Don't want to change any tabs here. Make sure the same tab
1409 * is still selected. */
1410 if (gui_use_tabline())
1411 gui_mch_set_curtab(tabpage_index(curtab));
1412 goto cmdline_not_changed;
1413#endif
1414
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 case K_SELECT: /* end of Select mode mapping - ignore */
1416 goto cmdline_not_changed;
1417
1418 case Ctrl_B: /* begin of command line */
1419 case K_HOME:
1420 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 case K_S_HOME:
1422 case K_C_HOME:
1423 ccline.cmdpos = 0;
1424 set_cmdspos();
1425 goto cmdline_not_changed;
1426
1427 case Ctrl_E: /* end of command line */
1428 case K_END:
1429 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 case K_S_END:
1431 case K_C_END:
1432 ccline.cmdpos = ccline.cmdlen;
1433 set_cmdspos_cursor();
1434 goto cmdline_not_changed;
1435
1436 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001437 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 break;
1439 goto cmdline_changed;
1440
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001441 case Ctrl_L:
1442#ifdef FEAT_SEARCH_EXTRA
1443 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1444 {
1445 /* Add a character from under the cursor for 'incsearch' */
1446 if (did_incsearch
1447 && !equalpos(curwin->w_cursor, old_cursor))
1448 {
1449 c = gchar_cursor();
Bram Moolenaara9dc3752010-07-11 20:46:53 +02001450 /* If 'ignorecase' and 'smartcase' are set and the
1451 * command line has no uppercase characters, convert
1452 * the character to lowercase */
1453 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
1454 c = MB_TOLOWER(c);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001455 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001456 {
1457 if (c == firstc || vim_strchr((char_u *)(
1458 p_magic ? "\\^$.*[" : "\\^$"), c)
1459 != NULL)
1460 {
1461 /* put a backslash before special characters */
1462 stuffcharReadbuff(c);
1463 c = '\\';
1464 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001465 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001466 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001467 }
1468 goto cmdline_not_changed;
1469 }
1470#endif
1471
1472 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001473 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 break;
1475 goto cmdline_changed;
1476
1477 case Ctrl_N: /* next match */
1478 case Ctrl_P: /* previous match */
1479 if (xpc.xp_numfiles > 0)
1480 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001481 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1482 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 break;
1484 goto cmdline_changed;
1485 }
1486
1487#ifdef FEAT_CMDHIST
1488 case K_UP:
1489 case K_DOWN:
1490 case K_S_UP:
1491 case K_S_DOWN:
1492 case K_PAGEUP:
1493 case K_KPAGEUP:
1494 case K_PAGEDOWN:
1495 case K_KPAGEDOWN:
1496 if (hislen == 0 || firstc == NUL) /* no history */
1497 goto cmdline_not_changed;
1498
1499 i = hiscnt;
1500
1501 /* save current command string so it can be restored later */
1502 if (lookfor == NULL)
1503 {
1504 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1505 goto cmdline_not_changed;
1506 lookfor[ccline.cmdpos] = NUL;
1507 }
1508
1509 j = (int)STRLEN(lookfor);
1510 for (;;)
1511 {
1512 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001513 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001514 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 {
1516 if (hiscnt == hislen) /* first time */
1517 hiscnt = hisidx[histype];
1518 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1519 hiscnt = hislen - 1;
1520 else if (hiscnt != hisidx[histype] + 1)
1521 --hiscnt;
1522 else /* at top of list */
1523 {
1524 hiscnt = i;
1525 break;
1526 }
1527 }
1528 else /* one step forwards */
1529 {
1530 /* on last entry, clear the line */
1531 if (hiscnt == hisidx[histype])
1532 {
1533 hiscnt = hislen;
1534 break;
1535 }
1536
1537 /* not on a history line, nothing to do */
1538 if (hiscnt == hislen)
1539 break;
1540 if (hiscnt == hislen - 1) /* wrap around */
1541 hiscnt = 0;
1542 else
1543 ++hiscnt;
1544 }
1545 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1546 {
1547 hiscnt = i;
1548 break;
1549 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001550 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001551 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 || STRNCMP(history[histype][hiscnt].hisstr,
1553 lookfor, (size_t)j) == 0)
1554 break;
1555 }
1556
1557 if (hiscnt != i) /* jumped to other entry */
1558 {
1559 char_u *p;
1560 int len;
1561 int old_firstc;
1562
1563 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001564 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 if (hiscnt == hislen)
1566 p = lookfor; /* back to the old one */
1567 else
1568 p = history[histype][hiscnt].hisstr;
1569
1570 if (histype == HIST_SEARCH
1571 && p != lookfor
1572 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1573 {
1574 /* Correct for the separator character used when
1575 * adding the history entry vs the one used now.
1576 * First loop: count length.
1577 * Second loop: copy the characters. */
1578 for (i = 0; i <= 1; ++i)
1579 {
1580 len = 0;
1581 for (j = 0; p[j] != NUL; ++j)
1582 {
1583 /* Replace old sep with new sep, unless it is
1584 * escaped. */
1585 if (p[j] == old_firstc
1586 && (j == 0 || p[j - 1] != '\\'))
1587 {
1588 if (i > 0)
1589 ccline.cmdbuff[len] = firstc;
1590 }
1591 else
1592 {
1593 /* Escape new sep, unless it is already
1594 * escaped. */
1595 if (p[j] == firstc
1596 && (j == 0 || p[j - 1] != '\\'))
1597 {
1598 if (i > 0)
1599 ccline.cmdbuff[len] = '\\';
1600 ++len;
1601 }
1602 if (i > 0)
1603 ccline.cmdbuff[len] = p[j];
1604 }
1605 ++len;
1606 }
1607 if (i == 0)
1608 {
1609 alloc_cmdbuff(len);
1610 if (ccline.cmdbuff == NULL)
1611 goto returncmd;
1612 }
1613 }
1614 ccline.cmdbuff[len] = NUL;
1615 }
1616 else
1617 {
1618 alloc_cmdbuff((int)STRLEN(p));
1619 if (ccline.cmdbuff == NULL)
1620 goto returncmd;
1621 STRCPY(ccline.cmdbuff, p);
1622 }
1623
1624 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1625 redrawcmd();
1626 goto cmdline_changed;
1627 }
1628 beep_flush();
1629 goto cmdline_not_changed;
1630#endif
1631
1632 case Ctrl_V:
1633 case Ctrl_Q:
1634#ifdef FEAT_MOUSE
1635 ignore_drag_release = TRUE;
1636#endif
1637 putcmdline('^', TRUE);
1638 c = get_literal(); /* get next (two) character(s) */
1639 do_abbr = FALSE; /* don't do abbreviation now */
1640#ifdef FEAT_MBYTE
1641 /* may need to remove ^ when composing char was typed */
1642 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1643 {
1644 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1645 msg_putchar(' ');
1646 cursorcmd();
1647 }
1648#endif
1649 break;
1650
1651#ifdef FEAT_DIGRAPHS
1652 case Ctrl_K:
1653#ifdef FEAT_MOUSE
1654 ignore_drag_release = TRUE;
1655#endif
1656 putcmdline('?', TRUE);
1657#ifdef USE_ON_FLY_SCROLL
1658 dont_scroll = TRUE; /* disallow scrolling here */
1659#endif
1660 c = get_digraph(TRUE);
1661 if (c != NUL)
1662 break;
1663
1664 redrawcmd();
1665 goto cmdline_not_changed;
1666#endif /* FEAT_DIGRAPHS */
1667
1668#ifdef FEAT_RIGHTLEFT
1669 case Ctrl__: /* CTRL-_: switch language mode */
1670 if (!p_ari)
1671 break;
1672#ifdef FEAT_FKMAP
1673 if (p_altkeymap)
1674 {
1675 cmd_fkmap = !cmd_fkmap;
1676 if (cmd_fkmap) /* in Farsi always in Insert mode */
1677 ccline.overstrike = FALSE;
1678 }
1679 else /* Hebrew is default */
1680#endif
1681 cmd_hkmap = !cmd_hkmap;
1682 goto cmdline_not_changed;
1683#endif
1684
1685 default:
1686#ifdef UNIX
1687 if (c == intr_char)
1688 {
1689 gotesc = TRUE; /* will free ccline.cmdbuff after
1690 putting it in history */
1691 goto returncmd; /* back to Normal mode */
1692 }
1693#endif
1694 /*
1695 * Normal character with no special meaning. Just set mod_mask
1696 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1697 * the string <S-Space>. This should only happen after ^V.
1698 */
1699 if (!IS_SPECIAL(c))
1700 mod_mask = 0x0;
1701 break;
1702 }
1703 /*
1704 * End of switch on command line character.
1705 * We come here if we have a normal character.
1706 */
1707
Bram Moolenaarede3e632013-06-23 16:16:19 +02001708 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709#ifdef FEAT_MBYTE
1710 /* Add ABBR_OFF for characters above 0x100, this is
1711 * what check_abbr() expects. */
1712 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1713#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001714 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 goto cmdline_changed;
1716
1717 /*
1718 * put the character in the command line
1719 */
1720 if (IS_SPECIAL(c) || mod_mask != 0)
1721 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1722 else
1723 {
1724#ifdef FEAT_MBYTE
1725 if (has_mbyte)
1726 {
1727 j = (*mb_char2bytes)(c, IObuff);
1728 IObuff[j] = NUL; /* exclude composing chars */
1729 put_on_cmdline(IObuff, j, TRUE);
1730 }
1731 else
1732#endif
1733 {
1734 IObuff[0] = c;
1735 put_on_cmdline(IObuff, 1, TRUE);
1736 }
1737 }
1738 goto cmdline_changed;
1739
1740/*
1741 * This part implements incremental searches for "/" and "?"
1742 * Jump to cmdline_not_changed when a character has been read but the command
1743 * line did not change. Then we only search and redraw if something changed in
1744 * the past.
1745 * Jump to cmdline_changed when the command line did change.
1746 * (Sorry for the goto's, I know it is ugly).
1747 */
1748cmdline_not_changed:
1749#ifdef FEAT_SEARCH_EXTRA
1750 if (!incsearch_postponed)
1751 continue;
1752#endif
1753
1754cmdline_changed:
1755#ifdef FEAT_SEARCH_EXTRA
1756 /*
1757 * 'incsearch' highlighting.
1758 */
1759 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1760 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001761 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001762#ifdef FEAT_RELTIME
1763 proftime_T tm;
1764#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001765
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 /* if there is a character waiting, search and redraw later */
1767 if (char_avail())
1768 {
1769 incsearch_postponed = TRUE;
1770 continue;
1771 }
1772 incsearch_postponed = FALSE;
1773 curwin->w_cursor = old_cursor; /* start at old position */
1774
1775 /* If there is no command line, don't do anything */
1776 if (ccline.cmdlen == 0)
1777 i = 0;
1778 else
1779 {
1780 cursor_off(); /* so the user knows we're busy */
1781 out_flush();
1782 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001783#ifdef FEAT_RELTIME
1784 /* Set the time limit to half a second. */
1785 profile_setlimit(500L, &tm);
1786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001788 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1789#ifdef FEAT_RELTIME
1790 &tm
1791#else
1792 NULL
1793#endif
1794 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 --emsg_off;
1796 /* if interrupted while searching, behave like it failed */
1797 if (got_int)
1798 {
1799 (void)vpeekc(); /* remove <C-C> from input stream */
1800 got_int = FALSE; /* don't abandon the command line */
1801 i = 0;
1802 }
1803 else if (char_avail())
1804 /* cancelled searching because a char was typed */
1805 incsearch_postponed = TRUE;
1806 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001807 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 highlight_match = TRUE; /* highlight position */
1809 else
1810 highlight_match = FALSE; /* remove highlight */
1811
1812 /* first restore the old curwin values, so the screen is
1813 * positioned in the same way as the actual search command */
1814 curwin->w_leftcol = old_leftcol;
1815 curwin->w_topline = old_topline;
1816# ifdef FEAT_DIFF
1817 curwin->w_topfill = old_topfill;
1818# endif
1819 curwin->w_botline = old_botline;
1820 changed_cline_bef_curs();
1821 update_topline();
1822
1823 if (i != 0)
1824 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001825 pos_T save_pos = curwin->w_cursor;
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 /*
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001828 * First move cursor to end of match, then to the start. This
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 * moves the whole match onto the screen when 'nowrap' is set.
1830 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 curwin->w_cursor.lnum += search_match_lines;
1832 curwin->w_cursor.col = search_match_endcol;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001833 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
1834 {
1835 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
1836 coladvance((colnr_T)MAXCOL);
1837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001839 end_pos = curwin->w_cursor;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001840 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001842 else
1843 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001844
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001846# ifdef FEAT_WINDOWS
1847 /* May redraw the status line to show the cursor position. */
1848 if (p_ru && curwin->w_status_height > 0)
1849 curwin->w_redr_status = TRUE;
1850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001852 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001853 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001854 restore_cmdline(&save_ccline);
1855
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001856 /* Leave it at the end to make CTRL-R CTRL-W work. */
1857 if (i != 0)
1858 curwin->w_cursor = end_pos;
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 msg_starthere();
1861 redrawcmdline();
1862 did_incsearch = TRUE;
1863 }
1864#else /* FEAT_SEARCH_EXTRA */
1865 ;
1866#endif
1867
1868#ifdef FEAT_RIGHTLEFT
1869 if (cmdmsg_rl
1870# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001871 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872# endif
1873 )
1874 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001875 * right-left typing. Not efficient, but it works.
1876 * Do it only when there are no characters left to read
1877 * to avoid useless intermediate redraws. */
1878 if (vpeekc() == NUL)
1879 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880#endif
1881 }
1882
1883returncmd:
1884
1885#ifdef FEAT_RIGHTLEFT
1886 cmdmsg_rl = FALSE;
1887#endif
1888
1889#ifdef FEAT_FKMAP
1890 cmd_fkmap = 0;
1891#endif
1892
1893 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001894 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895
1896#ifdef FEAT_SEARCH_EXTRA
1897 if (did_incsearch)
1898 {
1899 curwin->w_cursor = old_cursor;
1900 curwin->w_curswant = old_curswant;
1901 curwin->w_leftcol = old_leftcol;
1902 curwin->w_topline = old_topline;
1903# ifdef FEAT_DIFF
1904 curwin->w_topfill = old_topfill;
1905# endif
1906 curwin->w_botline = old_botline;
1907 highlight_match = FALSE;
1908 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001909 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 }
1911#endif
1912
1913 if (ccline.cmdbuff != NULL)
1914 {
1915 /*
1916 * Put line in history buffer (":" and "=" only when it was typed).
1917 */
1918#ifdef FEAT_CMDHIST
1919 if (ccline.cmdlen && firstc != NUL
1920 && (some_key_typed || histype == HIST_SEARCH))
1921 {
1922 add_to_history(histype, ccline.cmdbuff, TRUE,
1923 histype == HIST_SEARCH ? firstc : NUL);
1924 if (firstc == ':')
1925 {
1926 vim_free(new_last_cmdline);
1927 new_last_cmdline = vim_strsave(ccline.cmdbuff);
1928 }
1929 }
1930#endif
1931
1932 if (gotesc) /* abandon command line */
1933 {
1934 vim_free(ccline.cmdbuff);
1935 ccline.cmdbuff = NULL;
1936 if (msg_scrolled == 0)
1937 compute_cmdrow();
1938 MSG("");
1939 redraw_cmdline = TRUE;
1940 }
1941 }
1942
1943 /*
1944 * If the screen was shifted up, redraw the whole screen (later).
1945 * If the line is too long, clear it, so ruler and shown command do
1946 * not get printed in the middle of it.
1947 */
1948 msg_check();
1949 msg_scroll = save_msg_scroll;
1950 redir_off = FALSE;
1951
1952 /* When the command line was typed, no need for a wait-return prompt. */
1953 if (some_key_typed)
1954 need_wait_return = FALSE;
1955
1956 State = save_State;
1957#ifdef USE_IM_CONTROL
1958 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
1959 im_save_status(b_im_ptr);
1960 im_set_active(FALSE);
1961#endif
1962#ifdef FEAT_MOUSE
1963 setmouse();
1964#endif
1965#ifdef CURSOR_SHAPE
1966 ui_cursor_shape(); /* may show different cursor shape */
1967#endif
1968
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001969 {
1970 char_u *p = ccline.cmdbuff;
1971
1972 /* Make ccline empty, getcmdline() may try to use it. */
1973 ccline.cmdbuff = NULL;
1974 return p;
1975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976}
1977
1978#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
1979/*
1980 * Get a command line with a prompt.
1981 * This is prepared to be called recursively from getcmdline() (e.g. by
1982 * f_input() when evaluating an expression from CTRL-R =).
1983 * Returns the command line in allocated memory, or NULL.
1984 */
1985 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01001986getcmdline_prompt(
1987 int firstc,
1988 char_u *prompt, /* command line prompt */
1989 int attr, /* attributes for prompt */
1990 int xp_context, /* type of expansion */
1991 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992{
1993 char_u *s;
1994 struct cmdline_info save_ccline;
1995 int msg_col_save = msg_col;
1996
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001997 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 ccline.cmdprompt = prompt;
1999 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002000# ifdef FEAT_EVAL
2001 ccline.xp_context = xp_context;
2002 ccline.xp_arg = xp_arg;
2003 ccline.input_fn = (firstc == '@');
2004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002006 restore_cmdline(&save_ccline);
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002007 /* Restore msg_col, the prompt from input() may have changed it.
2008 * But only if called recursively and the commandline is therefore being
2009 * restored to an old one; if not, the input() prompt stays on the screen,
2010 * so we need its modified msg_col left intact. */
2011 if (ccline.cmdbuff != NULL)
2012 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 return s;
2015}
2016#endif
2017
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002018/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002019 * Return TRUE when the text must not be changed and we can't switch to
2020 * another window or buffer. Used when editing the command line, evaluating
2021 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002022 */
2023 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002024text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002025{
2026#ifdef FEAT_CMDWIN
2027 if (cmdwin_type != 0)
2028 return TRUE;
2029#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002030 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002031}
2032
2033/*
2034 * Give an error message for a command that isn't allowed while the cmdline
2035 * window is open or editing the cmdline in another way.
2036 */
2037 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002038text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002039{
2040#ifdef FEAT_CMDWIN
2041 if (cmdwin_type != 0)
2042 EMSG(_(e_cmdwin));
2043 else
2044#endif
2045 EMSG(_(e_secure));
2046}
2047
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002048#if defined(FEAT_AUTOCMD) || defined(PROTO)
2049/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002050 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2051 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002052 */
2053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002054curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002055{
2056 if (curbuf_lock > 0)
2057 {
2058 EMSG(_("E788: Not allowed to edit another buffer now"));
2059 return TRUE;
2060 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002061 return allbuf_locked();
2062}
2063
2064/*
2065 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2066 * message.
2067 */
2068 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002069allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002070{
2071 if (allbuf_lock > 0)
2072 {
2073 EMSG(_("E811: Not allowed to change buffer information now"));
2074 return TRUE;
2075 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002076 return FALSE;
2077}
2078#endif
2079
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002081cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2084 if (cmdline_star > 0) /* showing '*', always 1 position */
2085 return 1;
2086#endif
2087 return ptr2cells(ccline.cmdbuff + idx);
2088}
2089
2090/*
2091 * Compute the offset of the cursor on the command line for the prompt and
2092 * indent.
2093 */
2094 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002095set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002097 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 ccline.cmdspos = 1 + ccline.cmdindent;
2099 else
2100 ccline.cmdspos = 0 + ccline.cmdindent;
2101}
2102
2103/*
2104 * Compute the screen position for the cursor on the command line.
2105 */
2106 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002107set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108{
2109 int i, m, c;
2110
2111 set_cmdspos();
2112 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002113 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002115 if (m < 0) /* overflow, Columns or Rows at weird value */
2116 m = MAXCOL;
2117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 else
2119 m = MAXCOL;
2120 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2121 {
2122 c = cmdline_charsize(i);
2123#ifdef FEAT_MBYTE
2124 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2125 if (has_mbyte)
2126 correct_cmdspos(i, c);
2127#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002128 /* If the cmdline doesn't fit, show cursor on last visible char.
2129 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 if ((ccline.cmdspos += c) >= m)
2131 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 ccline.cmdspos -= c;
2133 break;
2134 }
2135#ifdef FEAT_MBYTE
2136 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002137 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138#endif
2139 }
2140}
2141
2142#ifdef FEAT_MBYTE
2143/*
2144 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2145 * character that doesn't fit, so that a ">" must be displayed.
2146 */
2147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002148correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002150 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2152 && ccline.cmdspos % Columns + cells > Columns)
2153 ccline.cmdspos++;
2154}
2155#endif
2156
2157/*
2158 * Get an Ex command line for the ":" command.
2159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002161getexline(
2162 int c, /* normally ':', NUL for ":append" */
2163 void *cookie UNUSED,
2164 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 /* When executing a register, remove ':' that's in front of each line. */
2167 if (exec_from_reg && vpeekc() == ':')
2168 (void)vgetc();
2169 return getcmdline(c, 1L, indent);
2170}
2171
2172/*
2173 * Get an Ex command line for Ex mode.
2174 * In Ex mode we only use the OS supplied line editing features and no
2175 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002176 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002179getexmodeline(
2180 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002181 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002182 void *cookie UNUSED,
2183 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002185 garray_T line_ga;
2186 char_u *pend;
2187 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002188 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002189 int escaped = FALSE; /* CTRL-V typed */
2190 int vcol = 0;
2191 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002192 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002193 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194
2195 /* Switch cursor on now. This avoids that it happens after the "\n", which
2196 * confuses the system function that computes tabstops. */
2197 cursor_on();
2198
2199 /* always start in column 0; write a newline if necessary */
2200 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002201 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002203 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002205 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002206 if (p_prompt)
2207 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 while (indent-- > 0)
2209 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 }
2212
2213 ga_init2(&line_ga, 1, 30);
2214
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002215 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002216 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002217 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002218 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002219 while (indent >= 8)
2220 {
2221 ga_append(&line_ga, TAB);
2222 msg_puts((char_u *)" ");
2223 indent -= 8;
2224 }
2225 while (indent-- > 0)
2226 {
2227 ga_append(&line_ga, ' ');
2228 msg_putchar(' ');
2229 }
2230 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002231 ++no_mapping;
2232 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 /*
2235 * Get the line, one character at a time.
2236 */
2237 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002238 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002240 long sw;
2241 char_u *s;
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 if (ga_grow(&line_ga, 40) == FAIL)
2244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002246 /* Get one character at a time. Don't use inchar(), it can't handle
2247 * special characters. */
Bram Moolenaar76624232007-07-28 12:21:47 +00002248 prev_char = c1;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002249 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250
2251 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002252 * Handle line editing.
2253 * Previously this was left to the system, putting the terminal in
2254 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002256 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002258 msg_putchar('\n');
2259 break;
2260 }
2261
2262 if (!escaped)
2263 {
2264 /* CR typed means "enter", which is NL */
2265 if (c1 == '\r')
2266 c1 = '\n';
2267
2268 if (c1 == BS || c1 == K_BS
2269 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002271 if (line_ga.ga_len > 0)
2272 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002273#ifdef FEAT_MBYTE
2274 if (has_mbyte)
2275 {
2276 p = (char_u *)line_ga.ga_data;
2277 p[line_ga.ga_len] = NUL;
2278 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2279 line_ga.ga_len -= len;
2280 }
2281 else
2282#endif
2283 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002284 goto redraw;
2285 }
2286 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 }
2288
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002289 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002291 msg_col = startcol;
2292 msg_clr_eos();
2293 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002294 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002295 }
2296
2297 if (c1 == Ctrl_T)
2298 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002299 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002300 p = (char_u *)line_ga.ga_data;
2301 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002302 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002303 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002304add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002305 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002307 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002308 p = (char_u *)line_ga.ga_data;
2309 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002310 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2311 *s = ' ';
2312 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002314redraw:
2315 /* redraw the line */
2316 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002317 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002318 p = (char_u *)line_ga.ga_data;
2319 p[line_ga.ga_len] = NUL;
2320 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002322 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002324 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002326 msg_putchar(' ');
2327 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002328 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002330 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002332 len = MB_PTR2LEN(p);
2333 msg_outtrans_len(p, len);
2334 vcol += ptr2cells(p);
2335 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
2337 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002338 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002339 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002340 continue;
2341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002343 if (c1 == Ctrl_D)
2344 {
2345 /* Delete one shiftwidth. */
2346 p = (char_u *)line_ga.ga_data;
2347 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002349 if (prev_char == '^')
2350 ex_keep_indent = TRUE;
2351 indent = 0;
2352 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
2354 else
2355 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002356 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002357 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002358 if (indent > 0)
2359 {
2360 --indent;
2361 indent -= indent % get_sw_value(curbuf);
2362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002364 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002365 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002366 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002367 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2368 --line_ga.ga_len;
2369 }
2370 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002372
2373 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2374 {
2375 escaped = TRUE;
2376 continue;
2377 }
2378
2379 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2380 if (IS_SPECIAL(c1))
2381 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002383
2384 if (IS_SPECIAL(c1))
2385 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002386#ifdef FEAT_MBYTE
2387 if (has_mbyte)
2388 len = (*mb_char2bytes)(c1,
2389 (char_u *)line_ga.ga_data + line_ga.ga_len);
2390 else
2391#endif
2392 {
2393 len = 1;
2394 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2395 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002396 if (c1 == '\n')
2397 msg_putchar('\n');
2398 else if (c1 == TAB)
2399 {
2400 /* Don't use chartabsize(), 'ts' can be different */
2401 do
2402 {
2403 msg_putchar(' ');
2404 } while (++vcol % 8);
2405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002408 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002409 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002410 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002412 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002413 escaped = FALSE;
2414
2415 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002416 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002417
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002418 /* We are done when a NL is entered, but not when it comes after an
2419 * odd number of backslashes, that results in a NUL. */
2420 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002422 int bcount = 0;
2423
2424 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2425 ++bcount;
2426
2427 if (bcount > 0)
2428 {
2429 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2430 * "\NL", etc. */
2431 line_ga.ga_len -= (bcount + 1) / 2;
2432 pend -= (bcount + 1) / 2;
2433 pend[-1] = '\n';
2434 }
2435
2436 if ((bcount & 1) == 0)
2437 {
2438 --line_ga.ga_len;
2439 --pend;
2440 *pend = NUL;
2441 break;
2442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444 }
2445
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002446 --no_mapping;
2447 --allow_keys;
2448
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 /* make following messages go to the next line */
2450 msg_didout = FALSE;
2451 msg_col = 0;
2452 if (msg_row < Rows - 1)
2453 ++msg_row;
2454 emsg_on_display = FALSE; /* don't want ui_delay() */
2455
2456 if (got_int)
2457 ga_clear(&line_ga);
2458
2459 return (char_u *)line_ga.ga_data;
2460}
2461
Bram Moolenaare344bea2005-09-01 20:46:49 +00002462# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2463 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464/*
2465 * Return TRUE if ccline.overstrike is on.
2466 */
2467 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002468cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 return ccline.overstrike;
2471}
2472
2473/*
2474 * Return TRUE if the cursor is at the end of the cmdline.
2475 */
2476 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002477cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478{
2479 return (ccline.cmdpos >= ccline.cmdlen);
2480}
2481#endif
2482
Bram Moolenaar9372a112005-12-06 19:59:18 +00002483#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484/*
2485 * Return the virtual column number at the current cursor position.
2486 * This is used by the IM code to obtain the start of the preedit string.
2487 */
2488 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002489cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490{
2491 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2492 return MAXCOL;
2493
2494# ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 {
2497 colnr_T col;
2498 int i = 0;
2499
2500 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002501 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502
2503 return col;
2504 }
2505 else
2506# endif
2507 return ccline.cmdpos;
2508}
2509#endif
2510
2511#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2512/*
2513 * If part of the command line is an IM preedit string, redraw it with
2514 * IM feedback attributes. The cursor position is restored after drawing.
2515 */
2516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002517redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 if ((State & CMDLINE)
2520 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002521 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 && !p_imdisable
2523 && im_is_preediting())
2524 {
2525 int cmdpos = 0;
2526 int cmdspos;
2527 int old_row;
2528 int old_col;
2529 colnr_T col;
2530
2531 old_row = msg_row;
2532 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002533 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534
2535# ifdef FEAT_MBYTE
2536 if (has_mbyte)
2537 {
2538 for (col = 0; col < preedit_start_col
2539 && cmdpos < ccline.cmdlen; ++col)
2540 {
2541 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002542 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544 }
2545 else
2546# endif
2547 {
2548 cmdspos += preedit_start_col;
2549 cmdpos += preedit_start_col;
2550 }
2551
2552 msg_row = cmdline_row + (cmdspos / (int)Columns);
2553 msg_col = cmdspos % (int)Columns;
2554 if (msg_row >= Rows)
2555 msg_row = Rows - 1;
2556
2557 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2558 {
2559 int char_len;
2560 int char_attr;
2561
2562 char_attr = im_get_feedback_attr(col);
2563 if (char_attr < 0)
2564 break; /* end of preedit string */
2565
2566# ifdef FEAT_MBYTE
2567 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002568 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 else
2570# endif
2571 char_len = 1;
2572
2573 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2574 cmdpos += char_len;
2575 }
2576
2577 msg_row = old_row;
2578 msg_col = old_col;
2579 }
2580}
2581#endif /* FEAT_XIM && FEAT_GUI_GTK */
2582
2583/*
2584 * Allocate a new command line buffer.
2585 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2586 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2587 */
2588 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002589alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590{
2591 /*
2592 * give some extra space to avoid having to allocate all the time
2593 */
2594 if (len < 80)
2595 len = 100;
2596 else
2597 len += 20;
2598
2599 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2600 ccline.cmdbufflen = len;
2601}
2602
2603/*
2604 * Re-allocate the command line to length len + something extra.
2605 * return FAIL for failure, OK otherwise
2606 */
2607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002608realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 char_u *p;
2611
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002612 if (len < ccline.cmdbufflen)
2613 return OK; /* no need to resize */
2614
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 p = ccline.cmdbuff;
2616 alloc_cmdbuff(len); /* will get some more */
2617 if (ccline.cmdbuff == NULL) /* out of memory */
2618 {
2619 ccline.cmdbuff = p; /* keep the old one */
2620 return FAIL;
2621 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002622 /* There isn't always a NUL after the command, but it may need to be
2623 * there, thus copy up to the NUL and add a NUL. */
2624 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2625 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002627
2628 if (ccline.xpc != NULL
2629 && ccline.xpc->xp_pattern != NULL
2630 && ccline.xpc->xp_context != EXPAND_NOTHING
2631 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2632 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002633 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002634
2635 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2636 * to point into the newly allocated memory. */
2637 if (i >= 0 && i <= ccline.cmdlen)
2638 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2639 }
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 return OK;
2642}
2643
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002644#if defined(FEAT_ARABIC) || defined(PROTO)
2645static char_u *arshape_buf = NULL;
2646
2647# if defined(EXITFREE) || defined(PROTO)
2648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002649free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002650{
2651 vim_free(arshape_buf);
2652}
2653# endif
2654#endif
2655
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656/*
2657 * Draw part of the cmdline at the current cursor position. But draw stars
2658 * when cmdline_star is TRUE.
2659 */
2660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002661draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662{
2663#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2664 int i;
2665
2666 if (cmdline_star > 0)
2667 for (i = 0; i < len; ++i)
2668 {
2669 msg_putchar('*');
2670# ifdef FEAT_MBYTE
2671 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002672 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673# endif
2674 }
2675 else
2676#endif
2677#ifdef FEAT_ARABIC
2678 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 static int buflen = 0;
2681 char_u *p;
2682 int j;
2683 int newlen = 0;
2684 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002685 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 int prev_c = 0;
2687 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002688 int u8c;
2689 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691
2692 /*
2693 * Do arabic shaping into a temporary buffer. This is very
2694 * inefficient!
2695 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002696 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
2698 /* Re-allocate the buffer. We keep it around to avoid a lot of
2699 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002700 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002701 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002702 arshape_buf = alloc(buflen);
2703 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 return; /* out of memory */
2705 }
2706
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002707 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2708 {
2709 /* Prepend a space to draw the leading composing char on. */
2710 arshape_buf[0] = ' ';
2711 newlen = 1;
2712 }
2713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 for (j = start; j < start + len; j += mb_l)
2715 {
2716 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002717 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002718 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 if (ARABIC_CHAR(u8c))
2720 {
2721 /* Do Arabic shaping. */
2722 if (cmdmsg_rl)
2723 {
2724 /* displaying from right to left */
2725 pc = prev_c;
2726 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002727 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (j + mb_l >= start + len)
2729 nc = NUL;
2730 else
2731 nc = utf_ptr2char(p + mb_l);
2732 }
2733 else
2734 {
2735 /* displaying from left to right */
2736 if (j + mb_l >= start + len)
2737 pc = NUL;
2738 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002739 {
2740 int pcc[MAX_MCO];
2741
2742 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002744 pc1 = pcc[0];
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 nc = prev_c;
2747 }
2748 prev_c = u8c;
2749
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002750 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002752 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002753 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002755 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2756 if (u8cc[1] != 0)
2757 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002758 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760 }
2761 else
2762 {
2763 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002764 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 newlen += mb_l;
2766 }
2767 }
2768
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002769 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
2771 else
2772#endif
2773 msg_outtrans_len(ccline.cmdbuff + start, len);
2774}
2775
2776/*
2777 * Put a character on the command line. Shifts the following text to the
2778 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2779 * "c" must be printable (fit in one display cell)!
2780 */
2781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002782putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 if (cmd_silent)
2785 return;
2786 msg_no_more = TRUE;
2787 msg_putchar(c);
2788 if (shift)
2789 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2790 msg_no_more = FALSE;
2791 cursorcmd();
2792}
2793
2794/*
2795 * Undo a putcmdline(c, FALSE).
2796 */
2797 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002798unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 if (cmd_silent)
2801 return;
2802 msg_no_more = TRUE;
2803 if (ccline.cmdlen == ccline.cmdpos)
2804 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002805#ifdef FEAT_MBYTE
2806 else if (has_mbyte)
2807 draw_cmdline(ccline.cmdpos,
2808 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
2811 draw_cmdline(ccline.cmdpos, 1);
2812 msg_no_more = FALSE;
2813 cursorcmd();
2814}
2815
2816/*
2817 * Put the given string, of the given length, onto the command line.
2818 * If len is -1, then STRLEN() is used to calculate the length.
2819 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2820 * part will be redrawn, otherwise it will not. If this function is called
2821 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2822 * called afterwards.
2823 */
2824 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002825put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826{
2827 int retval;
2828 int i;
2829 int m;
2830 int c;
2831
2832 if (len < 0)
2833 len = (int)STRLEN(str);
2834
2835 /* Check if ccline.cmdbuff needs to be longer */
2836 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002837 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 else
2839 retval = OK;
2840 if (retval == OK)
2841 {
2842 if (!ccline.overstrike)
2843 {
2844 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2845 ccline.cmdbuff + ccline.cmdpos,
2846 (size_t)(ccline.cmdlen - ccline.cmdpos));
2847 ccline.cmdlen += len;
2848 }
2849 else
2850 {
2851#ifdef FEAT_MBYTE
2852 if (has_mbyte)
2853 {
2854 /* Count nr of characters in the new string. */
2855 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002856 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 ++m;
2858 /* Count nr of bytes in cmdline that are overwritten by these
2859 * characters. */
2860 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002861 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 --m;
2863 if (i < ccline.cmdlen)
2864 {
2865 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
2866 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
2867 ccline.cmdlen += ccline.cmdpos + len - i;
2868 }
2869 else
2870 ccline.cmdlen = ccline.cmdpos + len;
2871 }
2872 else
2873#endif
2874 if (ccline.cmdpos + len > ccline.cmdlen)
2875 ccline.cmdlen = ccline.cmdpos + len;
2876 }
2877 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
2878 ccline.cmdbuff[ccline.cmdlen] = NUL;
2879
2880#ifdef FEAT_MBYTE
2881 if (enc_utf8)
2882 {
2883 /* When the inserted text starts with a composing character,
2884 * backup to the character before it. There could be two of them.
2885 */
2886 i = 0;
2887 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2888 while (ccline.cmdpos > 0 && utf_iscomposing(c))
2889 {
2890 i = (*mb_head_off)(ccline.cmdbuff,
2891 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2892 ccline.cmdpos -= i;
2893 len += i;
2894 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
2895 }
2896# ifdef FEAT_ARABIC
2897 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
2898 {
2899 /* Check the previous character for Arabic combining pair. */
2900 i = (*mb_head_off)(ccline.cmdbuff,
2901 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
2902 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
2903 + ccline.cmdpos - i), c))
2904 {
2905 ccline.cmdpos -= i;
2906 len += i;
2907 }
2908 else
2909 i = 0;
2910 }
2911# endif
2912 if (i != 0)
2913 {
2914 /* Also backup the cursor position. */
2915 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
2916 ccline.cmdspos -= i;
2917 msg_col -= i;
2918 if (msg_col < 0)
2919 {
2920 msg_col += Columns;
2921 --msg_row;
2922 }
2923 }
2924 }
2925#endif
2926
2927 if (redraw && !cmd_silent)
2928 {
2929 msg_no_more = TRUE;
2930 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02002931 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2933 /* Avoid clearing the rest of the line too often. */
2934 if (cmdline_row != i || ccline.overstrike)
2935 msg_clr_eos();
2936 msg_no_more = FALSE;
2937 }
2938#ifdef FEAT_FKMAP
2939 /*
2940 * If we are in Farsi command mode, the character input must be in
2941 * Insert mode. So do not advance the cmdpos.
2942 */
2943 if (!cmd_fkmap)
2944#endif
2945 {
2946 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002949 if (m < 0) /* overflow, Columns or Rows at weird value */
2950 m = MAXCOL;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 else
2953 m = MAXCOL;
2954 for (i = 0; i < len; ++i)
2955 {
2956 c = cmdline_charsize(ccline.cmdpos);
2957#ifdef FEAT_MBYTE
2958 /* count ">" for a double-wide char that doesn't fit. */
2959 if (has_mbyte)
2960 correct_cmdspos(ccline.cmdpos, c);
2961#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002962 /* Stop cursor at the end of the screen, but do increment the
2963 * insert position, so that entering a very long command
2964 * works, even though you can't see it. */
2965 if (ccline.cmdspos + c < m)
2966 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967#ifdef FEAT_MBYTE
2968 if (has_mbyte)
2969 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002970 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 if (c > len - i - 1)
2972 c = len - i - 1;
2973 ccline.cmdpos += c;
2974 i += c;
2975 }
2976#endif
2977 ++ccline.cmdpos;
2978 }
2979 }
2980 }
2981 if (redraw)
2982 msg_check();
2983 return retval;
2984}
2985
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002986static struct cmdline_info prev_ccline;
2987static int prev_ccline_used = FALSE;
2988
2989/*
2990 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
2991 * and overwrite it. But get_cmdline_str() may need it, thus make it
2992 * available globally in prev_ccline.
2993 */
2994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002996{
2997 if (!prev_ccline_used)
2998 {
2999 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3000 prev_ccline_used = TRUE;
3001 }
3002 *ccp = prev_ccline;
3003 prev_ccline = ccline;
3004 ccline.cmdbuff = NULL;
3005 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003006 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003007}
3008
3009/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003010 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003011 */
3012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003014{
3015 ccline = prev_ccline;
3016 prev_ccline = *ccp;
3017}
3018
Bram Moolenaar5a305422006-04-28 22:38:25 +00003019#if defined(FEAT_EVAL) || defined(PROTO)
3020/*
3021 * Save the command line into allocated memory. Returns a pointer to be
3022 * passed to restore_cmdline_alloc() later.
3023 * Returns NULL when failed.
3024 */
3025 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003026save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003027{
3028 struct cmdline_info *p;
3029
3030 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3031 if (p != NULL)
3032 save_cmdline(p);
3033 return (char_u *)p;
3034}
3035
3036/*
3037 * Restore the command line from the return value of save_cmdline_alloc().
3038 */
3039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003040restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003041{
3042 if (p != NULL)
3043 {
3044 restore_cmdline((struct cmdline_info *)p);
3045 vim_free(p);
3046 }
3047}
3048#endif
3049
Bram Moolenaar8299df92004-07-10 09:47:34 +00003050/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003051 * Paste a yank register into the command line.
3052 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003053 * insert_reg() can't be used here, because special characters from the
3054 * register contents will be interpreted as commands.
3055 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003056 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003057 */
3058 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059cmdline_paste(
3060 int regname,
3061 int literally, /* Insert text literally instead of "as typed" */
3062 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003063{
3064 long i;
3065 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003066 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003067 int allocated;
3068 struct cmdline_info save_ccline;
3069
3070 /* check for valid regname; also accept special characters for CTRL-R in
3071 * the command line */
3072 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3073 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3074 return FAIL;
3075
3076 /* A register containing CTRL-R can cause an endless loop. Allow using
3077 * CTRL-C to break the loop. */
3078 line_breakcheck();
3079 if (got_int)
3080 return FAIL;
3081
3082#ifdef FEAT_CLIPBOARD
3083 regname = may_get_selection(regname);
3084#endif
3085
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003086 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003087 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003088 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003089 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003090 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003091 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003092 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003093
3094 if (i)
3095 {
3096 /* Got the value of a special register in "arg". */
3097 if (arg == NULL)
3098 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003099
3100 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3101 * part of the word. */
3102 p = arg;
3103 if (p_is && regname == Ctrl_W)
3104 {
3105 char_u *w;
3106 int len;
3107
3108 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003109 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003110 {
3111#ifdef FEAT_MBYTE
3112 if (has_mbyte)
3113 {
3114 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3115 if (!vim_iswordc(mb_ptr2char(w - len)))
3116 break;
3117 w -= len;
3118 }
3119 else
3120#endif
3121 {
3122 if (!vim_iswordc(w[-1]))
3123 break;
3124 --w;
3125 }
3126 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003127 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003128 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3129 p += len;
3130 }
3131
3132 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003133 if (allocated)
3134 vim_free(arg);
3135 return OK;
3136 }
3137
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003138 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003139}
3140
3141/*
3142 * Put a string on the command line.
3143 * When "literally" is TRUE, insert literally.
3144 * When "literally" is FALSE, insert as typed, but don't leave the command
3145 * line.
3146 */
3147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003149{
3150 int c, cv;
3151
3152 if (literally)
3153 put_on_cmdline(s, -1, TRUE);
3154 else
3155 while (*s != NUL)
3156 {
3157 cv = *s;
3158 if (cv == Ctrl_V && s[1])
3159 ++s;
3160#ifdef FEAT_MBYTE
3161 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003162 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003163 else
3164#endif
3165 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003166 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3167 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003168#ifdef UNIX
3169 || c == intr_char
3170#endif
3171 || (c == Ctrl_BSL && *s == Ctrl_N))
3172 stuffcharReadbuff(Ctrl_V);
3173 stuffcharReadbuff(c);
3174 }
3175}
3176
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177#ifdef FEAT_WILDMENU
3178/*
3179 * Delete characters on the command line, from "from" to the current
3180 * position.
3181 */
3182 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003183cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184{
3185 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3186 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3187 ccline.cmdlen -= ccline.cmdpos - from;
3188 ccline.cmdpos = from;
3189}
3190#endif
3191
3192/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003193 * this function is called when the screen size changes and with incremental
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * search
3195 */
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/*
4495 * Cleanup matches for help tags: remove "@en" if "en" is the only language.
4496 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004497static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004500cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501{
4502 int i, j;
4503 int len;
4504
4505 for (i = 0; i < num_file; ++i)
4506 {
4507 len = (int)STRLEN(file[i]) - 3;
4508 if (len > 0 && STRCMP(file[i] + len, "@en") == 0)
4509 {
4510 /* Sorting on priority means the same item in another language may
4511 * be anywhere. Search all items for a match up to the "@en". */
4512 for (j = 0; j < num_file; ++j)
4513 if (j != i
4514 && (int)STRLEN(file[j]) == len + 3
4515 && STRNCMP(file[i], file[j], len + 1) == 0)
4516 break;
4517 if (j == num_file)
4518 file[i][len] = NUL;
4519 }
4520 }
4521}
4522#endif
4523
4524/*
4525 * Do the expansion based on xp->xp_context and "pat".
4526 */
4527 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004528ExpandFromContext(
4529 expand_T *xp,
4530 char_u *pat,
4531 int *num_file,
4532 char_u ***file,
4533 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534{
4535#ifdef FEAT_CMDL_COMPL
4536 regmatch_T regmatch;
4537#endif
4538 int ret;
4539 int flags;
4540
4541 flags = EW_DIR; /* include directories */
4542 if (options & WILD_LIST_NOTFOUND)
4543 flags |= EW_NOTFOUND;
4544 if (options & WILD_ADD_SLASH)
4545 flags |= EW_ADDSLASH;
4546 if (options & WILD_KEEP_ALL)
4547 flags |= EW_KEEPALL;
4548 if (options & WILD_SILENT)
4549 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004550 if (options & WILD_ALLLINKS)
4551 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004553 if (xp->xp_context == EXPAND_FILES
4554 || xp->xp_context == EXPAND_DIRECTORIES
4555 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 {
4557 /*
4558 * Expand file or directory names.
4559 */
4560 int free_pat = FALSE;
4561 int i;
4562
4563 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4564 * space */
4565 if (xp->xp_backslash != XP_BS_NONE)
4566 {
4567 free_pat = TRUE;
4568 pat = vim_strsave(pat);
4569 for (i = 0; pat[i]; ++i)
4570 if (pat[i] == '\\')
4571 {
4572 if (xp->xp_backslash == XP_BS_THREE
4573 && pat[i + 1] == '\\'
4574 && pat[i + 2] == '\\'
4575 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004576 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (xp->xp_backslash == XP_BS_ONE
4578 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004579 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 }
4582
4583 if (xp->xp_context == EXPAND_FILES)
4584 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004585 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4586 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 else
4588 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004589 if (options & WILD_ICASE)
4590 flags |= EW_ICASE;
4591
Bram Moolenaard7834d32009-12-02 16:14:36 +00004592 /* Expand wildcards, supporting %:h and the like. */
4593 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 if (free_pat)
4595 vim_free(pat);
4596 return ret;
4597 }
4598
4599 *file = (char_u **)"";
4600 *num_file = 0;
4601 if (xp->xp_context == EXPAND_HELP)
4602 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004603 /* With an empty argument we would get all the help tags, which is
4604 * very slow. Get matches for "help" instead. */
4605 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4606 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 {
4608#ifdef FEAT_MULTI_LANG
4609 cleanup_help_tags(*num_file, *file);
4610#endif
4611 return OK;
4612 }
4613 return FAIL;
4614 }
4615
4616#ifndef FEAT_CMDL_COMPL
4617 return FAIL;
4618#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004619 if (xp->xp_context == EXPAND_SHELLCMD)
4620 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 if (xp->xp_context == EXPAND_OLD_SETTING)
4622 return ExpandOldSetting(num_file, file);
4623 if (xp->xp_context == EXPAND_BUFFERS)
4624 return ExpandBufnames(pat, num_file, file, options);
4625 if (xp->xp_context == EXPAND_TAGS
4626 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4627 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4628 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004629 {
4630 char *directories[] = {"colors", NULL};
4631 return ExpandRTDir(pat, num_file, file, directories);
4632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004634 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004635 char *directories[] = {"compiler", NULL};
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004636 return ExpandRTDir(pat, num_file, file, directories);
4637 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004638 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004639 {
4640 char *directories[] = {"syntax", NULL};
4641 return ExpandRTDir(pat, num_file, file, directories);
4642 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004643 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004644 {
4645 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
4646 return ExpandRTDir(pat, num_file, file, directories);
4647 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004648# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4649 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004650 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004651# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004652 if (xp->xp_context == EXPAND_PACKADD)
4653 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4656 if (regmatch.regprog == NULL)
4657 return FAIL;
4658
4659 /* set ignore-case according to p_ic, p_scs and pat */
4660 regmatch.rm_ic = ignorecase(pat);
4661
4662 if (xp->xp_context == EXPAND_SETTINGS
4663 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4664 ret = ExpandSettings(xp, &regmatch, num_file, file);
4665 else if (xp->xp_context == EXPAND_MAPPINGS)
4666 ret = ExpandMappings(&regmatch, num_file, file);
4667# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4668 else if (xp->xp_context == EXPAND_USER_DEFINED)
4669 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4670# endif
4671 else
4672 {
4673 static struct expgen
4674 {
4675 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004676 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004678 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 } tab[] =
4680 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004681 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4682 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004683#ifdef FEAT_CMDHIST
4684 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004687 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004688 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004689 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4690 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4691 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692#endif
4693#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004694 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4695 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4696 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4697 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698#endif
4699#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004700 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4701 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702#endif
4703#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004704 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004706#ifdef FEAT_PROFILE
4707 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4708#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004709 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004711 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4712 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004714#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004715 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004716#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004717#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004718 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004719#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004720#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004721 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4724 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004725 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4726 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004728 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004729 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 };
4731 int i;
4732
4733 /*
4734 * Find a context in the table and call the ExpandGeneric() with the
4735 * right function to do the expansion.
4736 */
4737 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004738 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 if (xp->xp_context == tab[i].context)
4740 {
4741 if (tab[i].ic)
4742 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004743 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004744 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 break;
4746 }
4747 }
4748
Bram Moolenaar473de612013-06-08 18:19:48 +02004749 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 return ret;
4752#endif /* FEAT_CMDL_COMPL */
4753}
4754
4755#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4756/*
4757 * Expand a list of names.
4758 *
4759 * Generic function for command line completion. It calls a function to
4760 * obtain strings, one by one. The strings are matched against a regexp
4761 * program. Matching strings are copied into an array, which is returned.
4762 *
4763 * Returns OK when no problems encountered, FAIL for error (out of memory).
4764 */
4765 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004766ExpandGeneric(
4767 expand_T *xp,
4768 regmatch_T *regmatch,
4769 int *num_file,
4770 char_u ***file,
4771 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004773 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774{
4775 int i;
4776 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004777 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 char_u *str;
4779
4780 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004781 * round == 0: count the number of matching names
4782 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004784 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 for (i = 0; ; ++i)
4787 {
4788 str = (*func)(xp, i);
4789 if (str == NULL) /* end of list */
4790 break;
4791 if (*str == NUL) /* skip empty strings */
4792 continue;
4793
4794 if (vim_regexec(regmatch, str, (colnr_T)0))
4795 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004796 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004798 if (escaped)
4799 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4800 else
4801 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 (*file)[count] = str;
4803#ifdef FEAT_MENU
4804 if (func == get_menu_names && str != NULL)
4805 {
4806 /* test for separator added by get_menu_names() */
4807 str += STRLEN(str) - 1;
4808 if (*str == '\001')
4809 *str = '.';
4810 }
4811#endif
4812 }
4813 ++count;
4814 }
4815 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004816 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 {
4818 if (count == 0)
4819 return OK;
4820 *num_file = count;
4821 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
4822 if (*file == NULL)
4823 {
4824 *file = (char_u **)"";
4825 return FAIL;
4826 }
4827 count = 0;
4828 }
4829 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004830
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00004831 /* Sort the results. Keep menu's in the specified order. */
4832 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02004833 {
4834 if (xp->xp_context == EXPAND_EXPRESSION
4835 || xp->xp_context == EXPAND_FUNCTIONS
4836 || xp->xp_context == EXPAND_USER_FUNC)
4837 /* <SNR> functions should be sorted to the end. */
4838 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
4839 sort_func_compare);
4840 else
4841 sort_strings(*file, *num_file);
4842 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004843
Bram Moolenaar4f688582007-07-24 12:34:30 +00004844#ifdef FEAT_CMDL_COMPL
4845 /* Reset the variables used for special highlight names expansion, so that
4846 * they don't show up when getting normal highlight names by ID. */
4847 reset_expand_highlight();
4848#endif
4849
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 return OK;
4851}
4852
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004853/*
4854 * Complete a shell command.
4855 * Returns FAIL or OK;
4856 */
4857 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858expand_shellcmd(
4859 char_u *filepat, /* pattern to match with command names */
4860 int *num_file, /* return: number of matches */
4861 char_u ***file, /* return: array with matches */
4862 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004863{
4864 char_u *pat;
4865 int i;
4866 char_u *path;
4867 int mustfree = FALSE;
4868 garray_T ga;
4869 char_u *buf = alloc(MAXPATHL);
4870 size_t l;
4871 char_u *s, *e;
4872 int flags = flagsarg;
4873 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01004874 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004875
4876 if (buf == NULL)
4877 return FAIL;
4878
4879 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4880 * space */
4881 pat = vim_strsave(filepat);
4882 for (i = 0; pat[i]; ++i)
4883 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004884 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004885
Bram Moolenaarb5971142015-03-21 17:32:19 +01004886 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004887
4888 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00004889 if (mch_isFullName(pat))
4890 path = (char_u *)" ";
4891 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004892 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
4893 path = (char_u *)".";
4894 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004895 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004896 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01004897 if (path == NULL)
4898 path = (char_u *)"";
4899 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004900
4901 /*
4902 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01004903 * collect them in "ga". When "." is not in $PATH also expand for the
4904 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004905 */
4906 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01004907 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004908 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01004909 if (*s == NUL)
4910 {
4911 if (did_curdir)
4912 break;
4913 /* Find directories in the current directory, path is empty. */
4914 did_curdir = TRUE;
4915 }
4916 else if (*s == '.')
4917 did_curdir = TRUE;
4918
Bram Moolenaar68c31742006-09-02 15:54:18 +00004919 if (*s == ' ')
4920 ++s; /* Skip space used for absolute path name. */
4921
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004922#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004923 e = vim_strchr(s, ';');
4924#else
4925 e = vim_strchr(s, ':');
4926#endif
4927 if (e == NULL)
4928 e = s + STRLEN(s);
4929
4930 l = e - s;
4931 if (l > MAXPATHL - 5)
4932 break;
4933 vim_strncpy(buf, s, l);
4934 add_pathsep(buf);
4935 l = STRLEN(buf);
4936 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
4937
4938 /* Expand matches in one directory of $PATH. */
4939 ret = expand_wildcards(1, &buf, num_file, file, flags);
4940 if (ret == OK)
4941 {
4942 if (ga_grow(&ga, *num_file) == FAIL)
4943 FreeWild(*num_file, *file);
4944 else
4945 {
4946 for (i = 0; i < *num_file; ++i)
4947 {
4948 s = (*file)[i];
4949 if (STRLEN(s) > l)
4950 {
4951 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00004952 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004953 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
4954 }
4955 else
4956 vim_free(s);
4957 }
4958 vim_free(*file);
4959 }
4960 }
4961 if (*e != NUL)
4962 ++e;
4963 }
4964 *file = ga.ga_data;
4965 *num_file = ga.ga_len;
4966
4967 vim_free(buf);
4968 vim_free(pat);
4969 if (mustfree)
4970 vim_free(path);
4971 return OK;
4972}
4973
4974
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004976static 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 +00004977
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978/*
Bram Moolenaar21669c02008-01-18 12:16:16 +00004979 * Call "user_expand_func()" to invoke a user defined VimL function and return
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004980 * the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004982 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004983call_user_expand_func(
4984 void *(*user_expand_func)(char_u *, int, char_u **, int),
4985 expand_T *xp,
4986 int *num_file,
4987 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02004989 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004990 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004993 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00004994 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
Bram Moolenaarb7515462013-06-29 12:58:33 +02004996 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004997 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 *num_file = 0;
4999 *file = NULL;
5000
Bram Moolenaarb7515462013-06-29 12:58:33 +02005001 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005002 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005003 keep = ccline.cmdbuff[ccline.cmdlen];
5004 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005005 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005006
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005007 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005008 args[1] = xp->xp_line;
5009 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 args[2] = num;
5011
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005012 /* Save the cmdline, we don't know what the function may do. */
5013 save_ccline = ccline;
5014 ccline.cmdbuff = NULL;
5015 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005017
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005018 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005019
5020 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005022 if (ccline.cmdbuff != NULL)
5023 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005024
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005025 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005026 return ret;
5027}
5028
5029/*
5030 * Expand names with a function defined by the user.
5031 */
5032 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005033ExpandUserDefined(
5034 expand_T *xp,
5035 regmatch_T *regmatch,
5036 int *num_file,
5037 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005038{
5039 char_u *retstr;
5040 char_u *s;
5041 char_u *e;
5042 char_u keep;
5043 garray_T ga;
5044
5045 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5046 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 return FAIL;
5048
5049 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005050 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 {
5052 e = vim_strchr(s, '\n');
5053 if (e == NULL)
5054 e = s + STRLEN(s);
5055 keep = *e;
5056 *e = 0;
5057
5058 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5059 {
5060 *e = keep;
5061 if (*e != NUL)
5062 ++e;
5063 continue;
5064 }
5065
5066 if (ga_grow(&ga, 1) == FAIL)
5067 break;
5068
5069 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5070 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 *e = keep;
5073 if (*e != NUL)
5074 ++e;
5075 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005076 vim_free(retstr);
5077 *file = ga.ga_data;
5078 *num_file = ga.ga_len;
5079 return OK;
5080}
5081
5082/*
5083 * Expand names with a list returned by a function defined by the user.
5084 */
5085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005086ExpandUserList(
5087 expand_T *xp,
5088 int *num_file,
5089 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005090{
5091 list_T *retlist;
5092 listitem_T *li;
5093 garray_T ga;
5094
5095 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5096 if (retlist == NULL)
5097 return FAIL;
5098
5099 ga_init2(&ga, (int)sizeof(char *), 3);
5100 /* Loop over the items in the list. */
5101 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5102 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005103 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5104 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005105
5106 if (ga_grow(&ga, 1) == FAIL)
5107 break;
5108
5109 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005110 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005111 ++ga.ga_len;
5112 }
5113 list_unref(retlist);
5114
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 *file = ga.ga_data;
5116 *num_file = ga.ga_len;
5117 return OK;
5118}
5119#endif
5120
5121/*
Bram Moolenaar883f5d02010-06-21 06:24:34 +02005122 * Expand color scheme, compiler or filetype names:
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005123 * 'runtimepath'/{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005124 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 */
5126 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005127ExpandRTDir(
5128 char_u *pat,
5129 int *num_file,
5130 char_u ***file,
5131 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 char_u *s;
5134 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005135 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005137 int i;
5138 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 *num_file = 0;
5141 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005142 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005143 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005145 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005147 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5148 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005150 ga_clear_strings(&ga);
5151 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005153 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005154 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005155 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005157
5158 for (i = 0; i < ga.ga_len; ++i)
5159 {
5160 match = ((char_u **)ga.ga_data)[i];
5161 s = match;
5162 e = s + STRLEN(s);
5163 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5164 {
5165 e -= 4;
5166 for (s = e; s > match; mb_ptr_back(match, s))
5167 if (s < match || vim_ispathsep(*s))
5168 break;
5169 ++s;
5170 *e = NUL;
5171 mch_memmove(match, s, e - s + 1);
5172 }
5173 }
5174
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005175 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005176 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005177
5178 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005179 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005180 remove_duplicates(&ga);
5181
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 *file = ga.ga_data;
5183 *num_file = ga.ga_len;
5184 return OK;
5185}
5186
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005187/*
5188 * Expand loadplugin names:
5189 * 'packpath'/pack/ * /opt/{pat}
5190 */
5191 static int
5192ExpandPackAddDir(
5193 char_u *pat,
5194 int *num_file,
5195 char_u ***file)
5196{
5197 char_u *s;
5198 char_u *e;
5199 char_u *match;
5200 garray_T ga;
5201 int i;
5202 int pat_len;
5203
5204 *num_file = 0;
5205 *file = NULL;
5206 pat_len = (int)STRLEN(pat);
5207 ga_init2(&ga, (int)sizeof(char *), 10);
5208
5209 s = alloc((unsigned)(pat_len + 26));
5210 if (s == NULL)
5211 {
5212 ga_clear_strings(&ga);
5213 return FAIL;
5214 }
5215 sprintf((char *)s, "pack/*/opt/%s*", pat);
5216 globpath(p_pp, s, &ga, 0);
5217 vim_free(s);
5218
5219 for (i = 0; i < ga.ga_len; ++i)
5220 {
5221 match = ((char_u **)ga.ga_data)[i];
5222 s = gettail(match);
5223 e = s + STRLEN(s);
5224 mch_memmove(match, s, e - s + 1);
5225 }
5226
5227 if (ga.ga_len == 0)
5228 return FAIL;
5229
5230 /* Sort and remove duplicates which can happen when specifying multiple
5231 * directories in dirnames. */
5232 remove_duplicates(&ga);
5233
5234 *file = ga.ga_data;
5235 *num_file = ga.ga_len;
5236 return OK;
5237}
5238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#endif
5240
5241#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5242/*
5243 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005244 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005246 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005247globpath(
5248 char_u *path,
5249 char_u *file,
5250 garray_T *ga,
5251 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
5253 expand_T xpc;
5254 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 int num_p;
5257 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258
5259 buf = alloc(MAXPATHL);
5260 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005261 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005263 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005265
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /* Loop over all entries in {path}. */
5267 while (*path != NUL)
5268 {
5269 /* Copy one item of the path to buf[] and concatenate the file name. */
5270 copy_option_part(&path, buf, MAXPATHL, ",");
5271 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5272 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005273# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005274 /* Using the platform's path separator (\) makes vim incorrectly
5275 * treat it as an escape character, use '/' instead. */
5276 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5277 STRCAT(buf, "/");
5278# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005282 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5283 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005285 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005287 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 for (i = 0; i < num_p; ++i)
5290 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005291 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005292 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005293 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 FreeWild(num_p, p);
5298 }
5299 }
5300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
5302 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303}
5304
5305#endif
5306
5307#if defined(FEAT_CMDHIST) || defined(PROTO)
5308
5309/*********************************
5310 * Command line history stuff *
5311 *********************************/
5312
5313/*
5314 * Translate a history character to the associated type number.
5315 */
5316 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005317hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318{
5319 if (c == ':')
5320 return HIST_CMD;
5321 if (c == '=')
5322 return HIST_EXPR;
5323 if (c == '@')
5324 return HIST_INPUT;
5325 if (c == '>')
5326 return HIST_DEBUG;
5327 return HIST_SEARCH; /* must be '?' or '/' */
5328}
5329
5330/*
5331 * Table of history names.
5332 * These names are used in :history and various hist...() functions.
5333 * It is sufficient to give the significant prefix of a history name.
5334 */
5335
5336static char *(history_names[]) =
5337{
5338 "cmd",
5339 "search",
5340 "expr",
5341 "input",
5342 "debug",
5343 NULL
5344};
5345
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5347/*
5348 * Function given to ExpandGeneric() to obtain the possible first
5349 * arguments of the ":history command.
5350 */
5351 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005353{
5354 static char_u compl[2] = { NUL, NUL };
5355 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005356 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005357 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5358
5359 if (idx < short_names_count)
5360 {
5361 compl[0] = (char_u)short_names[idx];
5362 return compl;
5363 }
5364 if (idx < short_names_count + history_name_count)
5365 return (char_u *)history_names[idx - short_names_count];
5366 if (idx == short_names_count + history_name_count)
5367 return (char_u *)"all";
5368 return NULL;
5369}
5370#endif
5371
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372/*
5373 * init_history() - Initialize the command line history.
5374 * Also used to re-allocate the history when the size changes.
5375 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005376 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005377init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
5379 int newlen; /* new length of history table */
5380 histentry_T *temp;
5381 int i;
5382 int j;
5383 int type;
5384
5385 /*
5386 * If size of history table changed, reallocate it
5387 */
5388 newlen = (int)p_hi;
5389 if (newlen != hislen) /* history length changed */
5390 {
5391 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5392 {
5393 if (newlen)
5394 {
5395 temp = (histentry_T *)lalloc(
5396 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5397 if (temp == NULL) /* out of memory! */
5398 {
5399 if (type == 0) /* first one: just keep the old length */
5400 {
5401 newlen = hislen;
5402 break;
5403 }
5404 /* Already changed one table, now we can only have zero
5405 * length for all tables. */
5406 newlen = 0;
5407 type = -1;
5408 continue;
5409 }
5410 }
5411 else
5412 temp = NULL;
5413 if (newlen == 0 || temp != NULL)
5414 {
5415 if (hisidx[type] < 0) /* there are no entries yet */
5416 {
5417 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005418 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420 else if (newlen > hislen) /* array becomes bigger */
5421 {
5422 for (i = 0; i <= hisidx[type]; ++i)
5423 temp[i] = history[type][i];
5424 j = i;
5425 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005426 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 for ( ; j < hislen; ++i, ++j)
5428 temp[i] = history[type][j];
5429 }
5430 else /* array becomes smaller or 0 */
5431 {
5432 j = hisidx[type];
5433 for (i = newlen - 1; ; --i)
5434 {
5435 if (i >= 0) /* copy newest entries */
5436 temp[i] = history[type][j];
5437 else /* remove older entries */
5438 vim_free(history[type][j].hisstr);
5439 if (--j < 0)
5440 j = hislen - 1;
5441 if (j == hisidx[type])
5442 break;
5443 }
5444 hisidx[type] = newlen - 1;
5445 }
5446 vim_free(history[type]);
5447 history[type] = temp;
5448 }
5449 }
5450 hislen = newlen;
5451 }
5452}
5453
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005454 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005455clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005456{
5457 hisptr->hisnum = 0;
5458 hisptr->viminfo = FALSE;
5459 hisptr->hisstr = NULL;
5460}
5461
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462/*
5463 * Check if command line 'str' is already in history.
5464 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5465 */
5466 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005467in_history(
5468 int type,
5469 char_u *str,
5470 int move_to_front, /* Move the entry to the front if it exists */
5471 int sep,
5472 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473{
5474 int i;
5475 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005476 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
5478 if (hisidx[type] < 0)
5479 return FALSE;
5480 i = hisidx[type];
5481 do
5482 {
5483 if (history[type][i].hisstr == NULL)
5484 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005485
5486 /* For search history, check that the separator character matches as
5487 * well. */
5488 p = history[type][i].hisstr;
5489 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005490 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005491 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 {
5493 if (!move_to_front)
5494 return TRUE;
5495 last_i = i;
5496 break;
5497 }
5498 if (--i < 0)
5499 i = hislen - 1;
5500 } while (i != hisidx[type]);
5501
5502 if (last_i >= 0)
5503 {
5504 str = history[type][i].hisstr;
5505 while (i != hisidx[type])
5506 {
5507 if (++i >= hislen)
5508 i = 0;
5509 history[type][last_i] = history[type][i];
5510 last_i = i;
5511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005513 history[type][i].viminfo = FALSE;
5514 history[type][i].hisstr = str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 return TRUE;
5516 }
5517 return FALSE;
5518}
5519
5520/*
5521 * Convert history name (from table above) to its HIST_ equivalent.
5522 * When "name" is empty, return "cmd" history.
5523 * Returns -1 for unknown history name.
5524 */
5525 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005526get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527{
5528 int i;
5529 int len = (int)STRLEN(name);
5530
5531 /* No argument: use current history. */
5532 if (len == 0)
5533 return hist_char2type(ccline.cmdfirstc);
5534
5535 for (i = 0; history_names[i] != NULL; ++i)
5536 if (STRNICMP(name, history_names[i], len) == 0)
5537 return i;
5538
5539 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5540 return hist_char2type(name[0]);
5541
5542 return -1;
5543}
5544
5545static int last_maptick = -1; /* last seen maptick */
5546
5547/*
5548 * Add the given string to the given history. If the string is already in the
5549 * history then it is moved to the front. "histype" may be one of he HIST_
5550 * values.
5551 */
5552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553add_to_history(
5554 int histype,
5555 char_u *new_entry,
5556 int in_map, /* consider maptick when inside a mapping */
5557 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 histentry_T *hisptr;
5560 int len;
5561
5562 if (hislen == 0) /* no history */
5563 return;
5564
Bram Moolenaara939e432013-11-09 05:30:26 +01005565 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5566 return;
5567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 /*
5569 * Searches inside the same mapping overwrite each other, so that only
5570 * the last line is kept. Be careful not to remove a line that was moved
5571 * down, only lines that were added.
5572 */
5573 if (histype == HIST_SEARCH && in_map)
5574 {
5575 if (maptick == last_maptick)
5576 {
5577 /* Current line is from the same mapping, remove it */
5578 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5579 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005580 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 --hisnum[histype];
5582 if (--hisidx[HIST_SEARCH] < 0)
5583 hisidx[HIST_SEARCH] = hislen - 1;
5584 }
5585 last_maptick = -1;
5586 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005587 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
5589 if (++hisidx[histype] == hislen)
5590 hisidx[histype] = 0;
5591 hisptr = &history[histype][hisidx[histype]];
5592 vim_free(hisptr->hisstr);
5593
5594 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005595 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5597 if (hisptr->hisstr != NULL)
5598 hisptr->hisstr[len + 1] = sep;
5599
5600 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005601 hisptr->viminfo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 if (histype == HIST_SEARCH && in_map)
5603 last_maptick = maptick;
5604 }
5605}
5606
5607#if defined(FEAT_EVAL) || defined(PROTO)
5608
5609/*
5610 * Get identifier of newest history entry.
5611 * "histype" may be one of the HIST_ values.
5612 */
5613 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005614get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615{
5616 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5617 || hisidx[histype] < 0)
5618 return -1;
5619
5620 return history[histype][hisidx[histype]].hisnum;
5621}
5622
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005623static struct cmdline_info *get_ccline_ptr(void);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005624
5625/*
5626 * Get pointer to the command line info to use. cmdline_paste() may clear
5627 * ccline and put the previous value in prev_ccline.
5628 */
5629 static struct cmdline_info *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005630get_ccline_ptr(void)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005631{
5632 if ((State & CMDLINE) == 0)
5633 return NULL;
5634 if (ccline.cmdbuff != NULL)
5635 return &ccline;
5636 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
5637 return &prev_ccline;
5638 return NULL;
5639}
5640
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641/*
5642 * Get the current command line in allocated memory.
5643 * Only works when the command line is being edited.
5644 * Returns NULL when something is wrong.
5645 */
5646 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647get_cmdline_str(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005649 struct cmdline_info *p = get_ccline_ptr();
5650
5651 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 return NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005653 return vim_strnsave(p->cmdbuff, p->cmdlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654}
5655
5656/*
5657 * Get the current command line position, counted in bytes.
5658 * Zero is the first position.
5659 * Only works when the command line is being edited.
5660 * Returns -1 when something is wrong.
5661 */
5662 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663get_cmdline_pos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005665 struct cmdline_info *p = get_ccline_ptr();
5666
5667 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 return -1;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005669 return p->cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670}
5671
5672/*
5673 * Set the command line byte position to "pos". Zero is the first position.
5674 * Only works when the command line is being edited.
5675 * Returns 1 when failed, 0 when OK.
5676 */
5677 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678set_cmdline_pos(
5679 int pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00005681 struct cmdline_info *p = get_ccline_ptr();
5682
5683 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 return 1;
5685
5686 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
5687 * changed the command line. */
5688 if (pos < 0)
5689 new_cmdpos = 0;
5690 else
5691 new_cmdpos = pos;
5692 return 0;
5693}
5694
5695/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005696 * Get the current command-line type.
Bram Moolenaar1e015462005-09-25 22:16:38 +00005697 * Returns ':' or '/' or '?' or '@' or '>' or '-'
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005698 * Only works when the command line is being edited.
5699 * Returns NUL when something is wrong.
5700 */
5701 int
Bram Moolenaard14e00e2016-01-31 17:30:51 +01005702get_cmdline_type(void)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005703{
5704 struct cmdline_info *p = get_ccline_ptr();
5705
5706 if (p == NULL)
5707 return NUL;
Bram Moolenaar1e015462005-09-25 22:16:38 +00005708 if (p->cmdfirstc == NUL)
5709 return (p->input_fn) ? '@' : '-';
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005710 return p->cmdfirstc;
5711}
5712
5713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 * Calculate history index from a number:
5715 * num > 0: seen as identifying number of a history entry
5716 * num < 0: relative position in history wrt newest entry
5717 * "histype" may be one of the HIST_ values.
5718 */
5719 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005720calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721{
5722 int i;
5723 histentry_T *hist;
5724 int wrapped = FALSE;
5725
5726 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5727 || (i = hisidx[histype]) < 0 || num == 0)
5728 return -1;
5729
5730 hist = history[histype];
5731 if (num > 0)
5732 {
5733 while (hist[i].hisnum > num)
5734 if (--i < 0)
5735 {
5736 if (wrapped)
5737 break;
5738 i += hislen;
5739 wrapped = TRUE;
5740 }
5741 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5742 return i;
5743 }
5744 else if (-num <= hislen)
5745 {
5746 i += num + 1;
5747 if (i < 0)
5748 i += hislen;
5749 if (hist[i].hisstr != NULL)
5750 return i;
5751 }
5752 return -1;
5753}
5754
5755/*
5756 * Get a history entry by its index.
5757 * "histype" may be one of the HIST_ values.
5758 */
5759 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005760get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
5762 idx = calc_hist_idx(histype, idx);
5763 if (idx >= 0)
5764 return history[histype][idx].hisstr;
5765 else
5766 return (char_u *)"";
5767}
5768
5769/*
5770 * Clear all entries of a history.
5771 * "histype" may be one of the HIST_ values.
5772 */
5773 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 int i;
5777 histentry_T *hisptr;
5778
5779 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5780 {
5781 hisptr = history[histype];
5782 for (i = hislen; i--;)
5783 {
5784 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005785 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 }
5787 hisidx[histype] = -1; /* mark history as cleared */
5788 hisnum[histype] = 0; /* reset identifier counter */
5789 return OK;
5790 }
5791 return FAIL;
5792}
5793
5794/*
5795 * Remove all entries matching {str} from a history.
5796 * "histype" may be one of the HIST_ values.
5797 */
5798 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005799del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800{
5801 regmatch_T regmatch;
5802 histentry_T *hisptr;
5803 int idx;
5804 int i;
5805 int last;
5806 int found = FALSE;
5807
5808 regmatch.regprog = NULL;
5809 regmatch.rm_ic = FALSE; /* always match case */
5810 if (hislen != 0
5811 && histype >= 0
5812 && histype < HIST_COUNT
5813 && *str != NUL
5814 && (idx = hisidx[histype]) >= 0
5815 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5816 != NULL)
5817 {
5818 i = last = idx;
5819 do
5820 {
5821 hisptr = &history[histype][i];
5822 if (hisptr->hisstr == NULL)
5823 break;
5824 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5825 {
5826 found = TRUE;
5827 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005828 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
5830 else
5831 {
5832 if (i != last)
5833 {
5834 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005835 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836 }
5837 if (--last < 0)
5838 last += hislen;
5839 }
5840 if (--i < 0)
5841 i += hislen;
5842 } while (i != idx);
5843 if (history[histype][idx].hisstr == NULL)
5844 hisidx[histype] = -1;
5845 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005846 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 return found;
5848}
5849
5850/*
5851 * Remove an indexed entry from a history.
5852 * "histype" may be one of the HIST_ values.
5853 */
5854 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005855del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 int i, j;
5858
5859 i = calc_hist_idx(histype, idx);
5860 if (i < 0)
5861 return FALSE;
5862 idx = hisidx[histype];
5863 vim_free(history[histype][i].hisstr);
5864
5865 /* When deleting the last added search string in a mapping, reset
5866 * last_maptick, so that the last added search string isn't deleted again.
5867 */
5868 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
5869 last_maptick = -1;
5870
5871 while (i != idx)
5872 {
5873 j = (i + 1) % hislen;
5874 history[histype][i] = history[histype][j];
5875 i = j;
5876 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005877 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 if (--i < 0)
5879 i += hislen;
5880 hisidx[histype] = i;
5881 return TRUE;
5882}
5883
5884#endif /* FEAT_EVAL */
5885
5886#if defined(FEAT_CRYPT) || defined(PROTO)
5887/*
5888 * Very specific function to remove the value in ":set key=val" from the
5889 * history.
5890 */
5891 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005892remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 char_u *p;
5895 int i;
5896
5897 i = hisidx[HIST_CMD];
5898 if (i < 0)
5899 return;
5900 p = history[HIST_CMD][i].hisstr;
5901 if (p != NULL)
5902 for ( ; *p; ++p)
5903 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
5904 {
5905 p = vim_strchr(p + 3, '=');
5906 if (p == NULL)
5907 break;
5908 ++p;
5909 for (i = 0; p[i] && !vim_iswhite(p[i]); ++i)
5910 if (p[i] == '\\' && p[i + 1])
5911 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00005912 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 --p;
5914 }
5915}
5916#endif
5917
5918#endif /* FEAT_CMDHIST */
5919
5920#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
5921/*
5922 * Get indices "num1,num2" that specify a range within a list (not a range of
5923 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
5924 * Returns OK if parsed successfully, otherwise FAIL.
5925 */
5926 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005927get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
5929 int len;
5930 int first = FALSE;
5931 long num;
5932
5933 *str = skipwhite(*str);
5934 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
5935 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005936 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 *str += len;
5938 *num1 = (int)num;
5939 first = TRUE;
5940 }
5941 *str = skipwhite(*str);
5942 if (**str == ',') /* parse "to" part of range */
5943 {
5944 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005945 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 if (len > 0)
5947 {
5948 *num2 = (int)num;
5949 *str = skipwhite(*str + len);
5950 }
5951 else if (!first) /* no number given at all */
5952 return FAIL;
5953 }
5954 else if (first) /* only one number given */
5955 *num2 = *num1;
5956 return OK;
5957}
5958#endif
5959
5960#if defined(FEAT_CMDHIST) || defined(PROTO)
5961/*
5962 * :history command - print a history
5963 */
5964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 histentry_T *hist;
5968 int histype1 = HIST_CMD;
5969 int histype2 = HIST_CMD;
5970 int hisidx1 = 1;
5971 int hisidx2 = -1;
5972 int idx;
5973 int i, j, k;
5974 char_u *end;
5975 char_u *arg = eap->arg;
5976
5977 if (hislen == 0)
5978 {
5979 MSG(_("'history' option is zero"));
5980 return;
5981 }
5982
5983 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
5984 {
5985 end = arg;
5986 while (ASCII_ISALPHA(*end)
5987 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
5988 end++;
5989 i = *end;
5990 *end = NUL;
5991 histype1 = get_histtype(arg);
5992 if (histype1 == -1)
5993 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00005994 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 {
5996 histype1 = 0;
5997 histype2 = HIST_COUNT-1;
5998 }
5999 else
6000 {
6001 *end = i;
6002 EMSG(_(e_trailing));
6003 return;
6004 }
6005 }
6006 else
6007 histype2 = histype1;
6008 *end = i;
6009 }
6010 else
6011 end = arg;
6012 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6013 {
6014 EMSG(_(e_trailing));
6015 return;
6016 }
6017
6018 for (; !got_int && histype1 <= histype2; ++histype1)
6019 {
6020 STRCPY(IObuff, "\n # ");
6021 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6022 MSG_PUTS_TITLE(IObuff);
6023 idx = hisidx[histype1];
6024 hist = history[histype1];
6025 j = hisidx1;
6026 k = hisidx2;
6027 if (j < 0)
6028 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6029 if (k < 0)
6030 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6031 if (idx >= 0 && j <= k)
6032 for (i = idx + 1; !got_int; ++i)
6033 {
6034 if (i == hislen)
6035 i = 0;
6036 if (hist[i].hisstr != NULL
6037 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6038 {
6039 msg_putchar('\n');
6040 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6041 hist[i].hisnum);
6042 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6043 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006044 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 else
6046 STRCAT(IObuff, hist[i].hisstr);
6047 msg_outtrans(IObuff);
6048 out_flush();
6049 }
6050 if (i == idx)
6051 break;
6052 }
6053 }
6054}
6055#endif
6056
6057#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006058/*
6059 * Buffers for history read from a viminfo file. Only valid while reading.
6060 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061static char_u **viminfo_history[HIST_COUNT] = {NULL, NULL, NULL, NULL};
6062static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0};
6063static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0};
6064static int viminfo_add_at_front = FALSE;
6065
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006066static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068/*
6069 * Translate a history type number to the associated character.
6070 */
6071 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072hist_type2char(
6073 int type,
6074 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 if (type == HIST_CMD)
6077 return ':';
6078 if (type == HIST_SEARCH)
6079 {
6080 if (use_question)
6081 return '?';
6082 else
6083 return '/';
6084 }
6085 if (type == HIST_EXPR)
6086 return '=';
6087 return '@';
6088}
6089
6090/*
6091 * Prepare for reading the history from the viminfo file.
6092 * This allocates history arrays to store the read history lines.
6093 */
6094 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006095prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 int i;
6098 int num;
6099 int type;
6100 int len;
6101
6102 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006103 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 if (asklen > hislen)
6105 asklen = hislen;
6106
6107 for (type = 0; type < HIST_COUNT; ++type)
6108 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006109 /* Count the number of empty spaces in the history list. Entries read
6110 * from viminfo previously are also considered empty. If there are
6111 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006113 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 num++;
6115 len = asklen;
6116 if (num > len)
6117 len = num;
6118 if (len <= 0)
6119 viminfo_history[type] = NULL;
6120 else
6121 viminfo_history[type] =
6122 (char_u **)lalloc((long_u)(len * sizeof(char_u *)), FALSE);
6123 if (viminfo_history[type] == NULL)
6124 len = 0;
6125 viminfo_hislen[type] = len;
6126 viminfo_hisidx[type] = 0;
6127 }
6128}
6129
6130/*
6131 * Accept a line from the viminfo, store it in the history array when it's
6132 * new.
6133 */
6134 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006135read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 int type;
6138 long_u len;
6139 char_u *val;
6140 char_u *p;
6141
6142 type = hist_char2type(virp->vir_line[0]);
6143 if (viminfo_hisidx[type] < viminfo_hislen[type])
6144 {
6145 val = viminfo_readstring(virp, 1, TRUE);
6146 if (val != NULL && *val != NUL)
6147 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006148 int sep = (*val == ' ' ? NUL : *val);
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006151 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 {
6153 /* Need to re-allocate to append the separator byte. */
6154 len = STRLEN(val);
6155 p = lalloc(len + 2, TRUE);
6156 if (p != NULL)
6157 {
6158 if (type == HIST_SEARCH)
6159 {
6160 /* Search entry: Move the separator from the first
6161 * column to after the NUL. */
6162 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006163 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 }
6165 else
6166 {
6167 /* Not a search entry: No separator in the viminfo
6168 * file, add a NUL separator. */
6169 mch_memmove(p, val, (size_t)len + 1);
6170 p[len + 1] = NUL;
6171 }
6172 viminfo_history[type][viminfo_hisidx[type]++] = p;
6173 }
6174 }
6175 }
6176 vim_free(val);
6177 }
6178 return viminfo_readline(virp);
6179}
6180
Bram Moolenaar07219f92013-04-14 23:19:36 +02006181/*
6182 * Finish reading history lines from viminfo. Not used when writing viminfo.
6183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006185finish_viminfo_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
6187 int idx;
6188 int i;
6189 int type;
6190
6191 for (type = 0; type < HIST_COUNT; ++type)
6192 {
6193 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006194 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 idx = hisidx[type] + viminfo_hisidx[type];
6196 if (idx >= hislen)
6197 idx -= hislen;
6198 else if (idx < 0)
6199 idx = hislen - 1;
6200 if (viminfo_add_at_front)
6201 hisidx[type] = idx;
6202 else
6203 {
6204 if (hisidx[type] == -1)
6205 hisidx[type] = hislen - 1;
6206 do
6207 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006208 if (history[type][idx].hisstr != NULL
6209 || history[type][idx].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 break;
6211 if (++idx == hislen)
6212 idx = 0;
6213 } while (idx != hisidx[type]);
6214 if (idx != hisidx[type] && --idx < 0)
6215 idx = hislen - 1;
6216 }
6217 for (i = 0; i < viminfo_hisidx[type]; i++)
6218 {
6219 vim_free(history[type][idx].hisstr);
6220 history[type][idx].hisstr = viminfo_history[type][i];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006221 history[type][idx].viminfo = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 if (--idx < 0)
6223 idx = hislen - 1;
6224 }
6225 idx += 1;
6226 idx %= hislen;
6227 for (i = 0; i < viminfo_hisidx[type]; i++)
6228 {
6229 history[type][idx++].hisnum = ++hisnum[type];
6230 idx %= hislen;
6231 }
6232 vim_free(viminfo_history[type]);
6233 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006234 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236}
6237
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006238/*
6239 * Write history to viminfo file in "fp".
6240 * When "merge" is TRUE merge history lines with a previously read viminfo
6241 * file, data is in viminfo_history[].
6242 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6243 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245write_viminfo_history(
6246 FILE *fp,
6247 int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248{
6249 int i;
6250 int type;
6251 int num_saved;
6252 char_u *p;
6253 int c;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006254 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255
6256 init_history();
6257 if (hislen == 0)
6258 return;
6259 for (type = 0; type < HIST_COUNT; ++type)
6260 {
6261 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6262 if (num_saved == 0)
6263 continue;
6264 if (num_saved < 0) /* Use default */
6265 num_saved = hislen;
6266 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6267 type == HIST_CMD ? _("Command Line") :
6268 type == HIST_SEARCH ? _("Search String") :
6269 type == HIST_EXPR ? _("Expression") :
6270 _("Input Line"));
6271 if (num_saved > hislen)
6272 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006273
6274 /*
6275 * Merge typed and viminfo history:
6276 * round 1: history of typed commands.
6277 * round 2: history from recently read viminfo.
6278 */
6279 for (round = 1; round <= 2; ++round)
6280 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006281 if (round == 1)
6282 /* start at newest entry, somewhere in the list */
6283 i = hisidx[type];
6284 else if (viminfo_hisidx[type] > 0)
6285 /* start at newest entry, first in the list */
6286 i = 0;
6287 else
6288 /* empty list */
6289 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006290 if (i >= 0)
6291 while (num_saved > 0
6292 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006294 p = round == 1 ? history[type][i].hisstr
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006295 : viminfo_history[type] == NULL ? NULL
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006296 : viminfo_history[type][i];
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006297 if (p != NULL && (round == 2
6298 || !merge
6299 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006301 --num_saved;
6302 fputc(hist_type2char(type, TRUE), fp);
6303 /* For the search history: put the separator in the
6304 * second column; use a space if there isn't one. */
6305 if (type == HIST_SEARCH)
6306 {
6307 c = p[STRLEN(p) + 1];
6308 putc(c == NUL ? ' ' : c, fp);
6309 }
6310 viminfo_writestring(fp, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006312 if (round == 1)
6313 {
6314 /* Decrement index, loop around and stop when back at
6315 * the start. */
6316 if (--i < 0)
6317 i = hislen - 1;
6318 if (i == hisidx[type])
6319 break;
6320 }
6321 else
6322 {
6323 /* Increment index. Stop at the end in the while. */
6324 ++i;
6325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006327 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006328 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006329 if (viminfo_history[type] != NULL)
6330 vim_free(viminfo_history[type][i]);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006331 vim_free(viminfo_history[type]);
6332 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006333 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
6335}
6336#endif /* FEAT_VIMINFO */
6337
6338#if defined(FEAT_FKMAP) || defined(PROTO)
6339/*
6340 * Write a character at the current cursor+offset position.
6341 * It is directly written into the command buffer block.
6342 */
6343 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006344cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6347 {
6348 EMSG(_("E198: cmd_pchar beyond the command length"));
6349 return;
6350 }
6351 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6352 ccline.cmdbuff[ccline.cmdlen] = NUL;
6353}
6354
6355 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006356cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6359 {
6360 /* EMSG(_("cmd_gchar beyond the command length")); */
6361 return NUL;
6362 }
6363 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6364}
6365#endif
6366
6367#if defined(FEAT_CMDWIN) || defined(PROTO)
6368/*
6369 * Open a window on the current command line and history. Allow editing in
6370 * the window. Returns when the window is closed.
6371 * Returns:
6372 * CR if the command is to be executed
6373 * Ctrl_C if it is to be abandoned
6374 * K_IGNORE if editing continues
6375 */
6376 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006377ex_window(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 struct cmdline_info save_ccline;
6380 buf_T *old_curbuf = curbuf;
6381 win_T *old_curwin = curwin;
6382 buf_T *bp;
6383 win_T *wp;
6384 int i;
6385 linenr_T lnum;
6386 int histtype;
6387 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006388#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006390#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 int save_restart_edit = restart_edit;
6392 int save_State = State;
6393 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006394#ifdef FEAT_RIGHTLEFT
6395 int save_cmdmsg_rl = cmdmsg_rl;
6396#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006397#ifdef FEAT_FOLDING
6398 int save_KeyTyped;
6399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400
6401 /* Can't do this recursively. Can't do it when typing a password. */
6402 if (cmdwin_type != 0
6403# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6404 || cmdline_star > 0
6405# endif
6406 )
6407 {
6408 beep_flush();
6409 return K_IGNORE;
6410 }
6411
6412 /* Save current window sizes. */
6413 win_size_save(&winsizes);
6414
6415# ifdef FEAT_AUTOCMD
6416 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006417 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006419 /* don't use a new tab page */
6420 cmdmod.tab = 0;
6421
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 /* Create a window for the command-line buffer. */
6423 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6424 {
6425 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006426# ifdef FEAT_AUTOCMD
6427 unblock_autocmds();
6428# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 return K_IGNORE;
6430 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006431 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432
6433 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006434 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006435 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
6437 set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL);
6438 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006439#ifdef FEAT_FOLDING
6440 curwin->w_p_fen = FALSE;
6441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006443 curwin->w_p_rl = cmdmsg_rl;
6444 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006446 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447
6448# ifdef FEAT_AUTOCMD
6449 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006450 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451# endif
6452
Bram Moolenaar46152342005-09-07 21:18:43 +00006453 /* Showing the prompt may have set need_wait_return, reset it. */
6454 need_wait_return = FALSE;
6455
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006456 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6458 {
6459 if (p_wc == TAB)
6460 {
6461 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6462 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6463 }
6464 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6465 }
6466
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006467 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6468 * sets 'textwidth' to 78). */
6469 curbuf->b_p_tw = 0;
6470
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 /* Fill the buffer with the history. */
6472 init_history();
6473 if (hislen > 0)
6474 {
6475 i = hisidx[histtype];
6476 if (i >= 0)
6477 {
6478 lnum = 0;
6479 do
6480 {
6481 if (++i == hislen)
6482 i = 0;
6483 if (history[histtype][i].hisstr != NULL)
6484 ml_append(lnum++, history[histtype][i].hisstr,
6485 (colnr_T)0, FALSE);
6486 }
6487 while (i != hisidx[histtype]);
6488 }
6489 }
6490
6491 /* Replace the empty last line with the current command-line and put the
6492 * cursor there. */
6493 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6494 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6495 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006496 changed_line_abv_curs();
6497 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006498 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499
6500 /* Save the command line info, can be used recursively. */
6501 save_ccline = ccline;
6502 ccline.cmdbuff = NULL;
6503 ccline.cmdprompt = NULL;
6504
6505 /* No Ex mode here! */
6506 exmode_active = 0;
6507
6508 State = NORMAL;
6509# ifdef FEAT_MOUSE
6510 setmouse();
6511# endif
6512
6513# ifdef FEAT_AUTOCMD
6514 /* Trigger CmdwinEnter autocommands. */
6515 typestr[0] = cmdwin_type;
6516 typestr[1] = NUL;
6517 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006518 if (restart_edit != 0) /* autocmd with ":startinsert" */
6519 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520# endif
6521
6522 i = RedrawingDisabled;
6523 RedrawingDisabled = 0;
6524
6525 /*
6526 * Call the main loop until <CR> or CTRL-C is typed.
6527 */
6528 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006529 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530
6531 RedrawingDisabled = i;
6532
6533# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006534
6535# ifdef FEAT_FOLDING
6536 save_KeyTyped = KeyTyped;
6537# endif
6538
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 /* Trigger CmdwinLeave autocommands. */
6540 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006541
6542# ifdef FEAT_FOLDING
6543 /* Restore KeyTyped in case it is modified by autocommands */
6544 KeyTyped = save_KeyTyped;
6545# endif
6546
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547# endif
6548
Bram Moolenaarccc18222007-05-10 18:25:20 +00006549 /* Restore the command line info. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006550 ccline = save_ccline;
6551 cmdwin_type = 0;
6552
6553 exmode_active = save_exmode;
6554
Bram Moolenaarf9821062008-06-20 16:31:07 +00006555 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 * this happens! */
6557 if (!win_valid(old_curwin) || !buf_valid(old_curbuf))
6558 {
6559 cmdwin_result = Ctrl_C;
6560 EMSG(_("E199: Active window or buffer deleted"));
6561 }
6562 else
6563 {
6564# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
6565 /* autocmds may abort script processing */
6566 if (aborting() && cmdwin_result != K_IGNORE)
6567 cmdwin_result = Ctrl_C;
6568# endif
6569 /* Set the new command line from the cmdline buffer. */
6570 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00006571 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 {
Bram Moolenaar46152342005-09-07 21:18:43 +00006573 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
6574
6575 if (histtype == HIST_CMD)
6576 {
6577 /* Execute the command directly. */
6578 ccline.cmdbuff = vim_strsave((char_u *)p);
6579 cmdwin_result = CAR;
6580 }
6581 else
6582 {
6583 /* First need to cancel what we were doing. */
6584 ccline.cmdbuff = NULL;
6585 stuffcharReadbuff(':');
6586 stuffReadbuff((char_u *)p);
6587 stuffcharReadbuff(CAR);
6588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 }
6590 else if (cmdwin_result == K_XF2) /* :qa typed */
6591 {
6592 ccline.cmdbuff = vim_strsave((char_u *)"qa");
6593 cmdwin_result = CAR;
6594 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02006595 else if (cmdwin_result == Ctrl_C)
6596 {
6597 /* :q or :close, don't execute any command
6598 * and don't modify the cmd window. */
6599 ccline.cmdbuff = NULL;
6600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 else
6602 ccline.cmdbuff = vim_strsave(ml_get_curline());
6603 if (ccline.cmdbuff == NULL)
6604 cmdwin_result = Ctrl_C;
6605 else
6606 {
6607 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
6608 ccline.cmdbufflen = ccline.cmdlen + 1;
6609 ccline.cmdpos = curwin->w_cursor.col;
6610 if (ccline.cmdpos > ccline.cmdlen)
6611 ccline.cmdpos = ccline.cmdlen;
6612 if (cmdwin_result == K_IGNORE)
6613 {
6614 set_cmdspos_cursor();
6615 redrawcmd();
6616 }
6617 }
6618
6619# ifdef FEAT_AUTOCMD
6620 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006621 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02006623# ifdef FEAT_CONCEAL
6624 /* Avoid command-line window first character being concealed. */
6625 curwin->w_p_cole = 0;
6626# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 wp = curwin;
6628 bp = curbuf;
6629 win_goto(old_curwin);
6630 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01006631
6632 /* win_close() may have already wiped the buffer when 'bh' is
6633 * set to 'wipe' */
6634 if (buf_valid(bp))
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006635 close_buffer(NULL, bp, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636
6637 /* Restore window sizes. */
6638 win_size_restore(&winsizes);
6639
6640# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006641 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642# endif
6643 }
6644
6645 ga_clear(&winsizes);
6646 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00006647# ifdef FEAT_RIGHTLEFT
6648 cmdmsg_rl = save_cmdmsg_rl;
6649# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650
6651 State = save_State;
6652# ifdef FEAT_MOUSE
6653 setmouse();
6654# endif
6655
6656 return cmdwin_result;
6657}
6658#endif /* FEAT_CMDWIN */
6659
6660/*
6661 * Used for commands that either take a simple command string argument, or:
6662 * cmd << endmarker
6663 * {script}
6664 * endmarker
6665 * Returns a pointer to allocated memory with {script} or NULL.
6666 */
6667 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006668script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 char_u *theline;
6671 char *end_pattern = NULL;
6672 char dot[] = ".";
6673 garray_T ga;
6674
6675 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
6676 return NULL;
6677
6678 ga_init2(&ga, 1, 0x400);
6679
6680 if (cmd[2] != NUL)
6681 end_pattern = (char *)skipwhite(cmd + 2);
6682 else
6683 end_pattern = dot;
6684
6685 for (;;)
6686 {
6687 theline = eap->getline(
6688#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00006689 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690#endif
6691 NUL, eap->cookie, 0);
6692
6693 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006694 {
6695 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00006697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 ga_concat(&ga, theline);
6700 ga_append(&ga, '\n');
6701 vim_free(theline);
6702 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00006703 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704
6705 return (char_u *)ga.ga_data;
6706}