blob: afdf0b4c95b1c0b1de0c596b079cd7903643f6c2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000057#ifdef FEAT_CMDHIST
58typedef struct hist_entry
59{
60 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020061 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020063 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064} histentry_T;
65
66static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
67static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
68static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
69 /* identifying (unique) number of newest history entry */
70static int hislen = 0; /* actual length of history tables */
71
Bram Moolenaard25c16e2016-01-29 22:13:30 +010072static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000073
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# endif
78#endif
79
80#ifdef FEAT_RIGHTLEFT
81static int cmd_hkmap = 0; /* Hebrew mapping during command line */
82#endif
83
84#ifdef FEAT_FKMAP
85static int cmd_fkmap = 0; /* Farsi mapping during command line */
86#endif
87
Bram Moolenaard25c16e2016-01-29 22:13:30 +010088static int cmdline_charsize(int idx);
89static void set_cmdspos(void);
90static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010092static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void alloc_cmdbuff(int len);
95static int realloc_cmdbuff(int len);
96static void draw_cmdline(int start, int len);
97static void save_cmdline(struct cmdline_info *ccp);
98static void restore_cmdline(struct cmdline_info *ccp);
99static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100101static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#endif
103#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void redrawcmdprompt(void);
107static void cursorcmd(void);
108static int ccheck_abbr(int);
109static int nextwild(expand_T *xp, int type, int options, int escape);
110static void escape_fname(char_u **pp);
111static int showmatches(expand_T *xp, int wildmenu);
112static void set_expand_context(expand_T *xp);
113static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
114static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100116static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100117static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100118static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100120static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100123static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
124static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125# endif
126#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100128static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000130
131#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200132static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133#endif
134
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200135#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
136static int
137#ifdef __BORLANDC__
138_RTLENTRYF
139#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100140sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200141#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200142#ifdef FEAT_SEARCH_EXTRA
143static void set_search_match(pos_T *t);
144#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/*
147 * getcmdline() - accept a command line starting with firstc.
148 *
149 * firstc == ':' get ":" command line.
150 * firstc == '/' or '?' get search pattern
151 * firstc == '=' get expression
152 * firstc == '@' get text for input() function
153 * firstc == '>' get text for debug mode
154 * firstc == NUL get text for :insert command
155 * firstc == -1 like NUL, and break on CTRL-C
156 *
157 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
158 * command line.
159 *
160 * Careful: getcmdline() can be called recursively!
161 *
162 * Return pointer to allocated string if there is a commandline, NULL
163 * otherwise.
164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100166getcmdline(
167 int firstc,
168 long count UNUSED, /* only used for incremental search */
169 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
171 int c;
172 int i;
173 int j;
174 int gotesc = FALSE; /* TRUE when <ESC> just typed */
175 int do_abbr; /* when TRUE check for abbr. */
176#ifdef FEAT_CMDHIST
177 char_u *lookfor = NULL; /* string to match */
178 int hiscnt; /* current history line in use */
179 int histype; /* history type to be used */
180#endif
181#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200182 pos_T search_start; /* where 'incsearch' starts searching */
183 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000184 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200185 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000186 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200187 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200189 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200190 pos_T match_start = curwin->w_cursor;
191 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192# ifdef FEAT_DIFF
193 int old_topfill;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200194 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195# endif
196 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200197 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 int did_incsearch = FALSE;
199 int incsearch_postponed = FALSE;
200#endif
201 int did_wild_list = FALSE; /* did wild_list() recently */
202 int wim_index = 0; /* index in wim_flags[] */
203 int res;
204 int save_msg_scroll = msg_scroll;
205 int save_State = State; /* remember State when called */
206 int some_key_typed = FALSE; /* one of the keys was typed */
207#ifdef FEAT_MOUSE
208 /* mouse drag and release events are ignored, unless they are
209 * preceded with a mouse down event */
210 int ignore_drag_release = TRUE;
211#endif
212#ifdef FEAT_EVAL
213 int break_ctrl_c = FALSE;
214#endif
215 expand_T xpc;
216 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100217#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000218 /* Everything that may work recursively should save and restore the
219 * current command line in save_ccline. That includes update_screen(), a
220 * custom status line may invoke ":normal". */
221 struct cmdline_info save_ccline;
222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_EVAL
225 if (firstc == -1)
226 {
227 firstc = NUL;
228 break_ctrl_c = TRUE;
229 }
230#endif
231#ifdef FEAT_RIGHTLEFT
232 /* start without Hebrew mapping for a command line */
233 if (firstc == ':' || firstc == '=' || firstc == '>')
234 cmd_hkmap = 0;
235#endif
236
237 ccline.overstrike = FALSE; /* always start in insert mode */
238#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100239 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200240 save_cursor = curwin->w_cursor; /* may be restored later */
241 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 old_curswant = curwin->w_curswant;
243 old_leftcol = curwin->w_leftcol;
244 old_topline = curwin->w_topline;
245# ifdef FEAT_DIFF
246 old_topfill = curwin->w_topfill;
247# endif
248 old_botline = curwin->w_botline;
249#endif
250
251 /*
252 * set some variables for redrawcmd()
253 */
254 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000255 ccline.cmdindent = (firstc > 0 ? indent : 0);
256
257 /* alloc initial ccline.cmdbuff */
258 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (ccline.cmdbuff == NULL)
260 return NULL; /* out of memory */
261 ccline.cmdlen = ccline.cmdpos = 0;
262 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100263 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000265 /* autoindent for :insert and :append */
266 if (firstc <= 0)
267 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200268 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000269 ccline.cmdbuff[indent] = NUL;
270 ccline.cmdpos = indent;
271 ccline.cmdspos = indent;
272 ccline.cmdlen = indent;
273 }
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000276 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278#ifdef FEAT_RIGHTLEFT
279 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
280 && (firstc == '/' || firstc == '?'))
281 cmdmsg_rl = TRUE;
282 else
283 cmdmsg_rl = FALSE;
284#endif
285
286 redir_off = TRUE; /* don't redirect the typed command */
287 if (!cmd_silent)
288 {
289 i = msg_scrolled;
290 msg_scrolled = 0; /* avoid wait_return message */
291 gotocmdline(TRUE);
292 msg_scrolled += i;
293 redrawcmdprompt(); /* draw prompt or indent */
294 set_cmdspos();
295 }
296 xpc.xp_context = EXPAND_NOTHING;
297 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000298#ifndef BACKSLASH_IN_FILENAME
299 xpc.xp_shell = FALSE;
300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000302#if defined(FEAT_EVAL)
303 if (ccline.input_fn)
304 {
305 xpc.xp_context = ccline.xp_context;
306 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000307# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000308 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000309# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000310 }
311#endif
312
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 /*
314 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
315 * doing ":@0" when register 0 doesn't contain a CR.
316 */
317 msg_scroll = FALSE;
318
319 State = CMDLINE;
320
321 if (firstc == '/' || firstc == '?' || firstc == '@')
322 {
323 /* Use ":lmap" mappings for search pattern and input(). */
324 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
325 b_im_ptr = &curbuf->b_p_iminsert;
326 else
327 b_im_ptr = &curbuf->b_p_imsearch;
328 if (*b_im_ptr == B_IMODE_LMAP)
329 State |= LANGMAP;
330#ifdef USE_IM_CONTROL
331 im_set_active(*b_im_ptr == B_IMODE_IM);
332#endif
333 }
334#ifdef USE_IM_CONTROL
335 else if (p_imcmdline)
336 im_set_active(TRUE);
337#endif
338
339#ifdef FEAT_MOUSE
340 setmouse();
341#endif
342#ifdef CURSOR_SHAPE
343 ui_cursor_shape(); /* may show different cursor shape */
344#endif
345
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000346 /* When inside an autocommand for writing "exiting" may be set and
347 * terminal mode set to cooked. Need to set raw mode here then. */
348 settmode(TMODE_RAW);
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350#ifdef FEAT_CMDHIST
351 init_history();
352 hiscnt = hislen; /* set hiscnt to impossible history value */
353 histype = hist_char2type(firstc);
354#endif
355
356#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000357 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#endif
359
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200360 /* If something above caused an error, reset the flags, we do want to type
361 * and execute commands. Display may be messed up a bit. */
362 if (did_emsg)
363 redrawcmd();
364 did_emsg = FALSE;
365 got_int = FALSE;
366
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 /*
368 * Collect the command string, handling editing keys.
369 */
370 for (;;)
371 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000372 redir_off = TRUE; /* Don't redirect the typed command.
373 Repeated, because a ":redir" inside
374 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375#ifdef USE_ON_FLY_SCROLL
376 dont_scroll = FALSE; /* allow scrolling here */
377#endif
378 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
379
380 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000381
382 /* Get a character. Ignore K_IGNORE, it should not do anything, such
383 * as stop completion. */
384 do
385 {
386 c = safe_vgetc();
387 } while (c == K_IGNORE);
388
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 if (KeyTyped)
390 {
391 some_key_typed = TRUE;
392#ifdef FEAT_RIGHTLEFT
393 if (cmd_hkmap)
394 c = hkmap(c);
395# ifdef FEAT_FKMAP
396 if (cmd_fkmap)
397 c = cmdl_fkmap(c);
398# endif
399 if (cmdmsg_rl && !KeyStuffed)
400 {
401 /* Invert horizontal movements and operations. Only when
402 * typed by the user directly, not when the result of a
403 * mapping. */
404 switch (c)
405 {
406 case K_RIGHT: c = K_LEFT; break;
407 case K_S_RIGHT: c = K_S_LEFT; break;
408 case K_C_RIGHT: c = K_C_LEFT; break;
409 case K_LEFT: c = K_RIGHT; break;
410 case K_S_LEFT: c = K_S_RIGHT; break;
411 case K_C_LEFT: c = K_C_RIGHT; break;
412 }
413 }
414#endif
415 }
416
417 /*
418 * Ignore got_int when CTRL-C was typed here.
419 * Don't ignore it in :global, we really need to break then, e.g., for
420 * ":g/pat/normal /pat" (without the <CR>).
421 * Don't ignore it for the input() function.
422 */
423 if ((c == Ctrl_C
424#ifdef UNIX
425 || c == intr_char
426#endif
427 )
428#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
429 && firstc != '@'
430#endif
431#ifdef FEAT_EVAL
432 && !break_ctrl_c
433#endif
434 && !global_busy)
435 got_int = FALSE;
436
437#ifdef FEAT_CMDHIST
438 /* free old command line when finished moving around in the history
439 * list */
440 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000441 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000442 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 && c != K_PAGEDOWN && c != K_PAGEUP
444 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000445 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
447 {
448 vim_free(lookfor);
449 lookfor = NULL;
450 }
451#endif
452
453 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000454 * When there are matching completions to select <S-Tab> works like
455 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000457 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 c = Ctrl_P;
459
460#ifdef FEAT_WILDMENU
461 /* Special translations for 'wildmenu' */
462 if (did_wild_list && p_wmnu)
463 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000464 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000465 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000466 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 c = Ctrl_N;
468 }
469 /* Hitting CR after "emenu Name.": complete submenu */
470 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
471 && ccline.cmdpos > 1
472 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
473 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
474 && (c == '\n' || c == '\r' || c == K_KENTER))
475 c = K_DOWN;
476#endif
477
478 /* free expanded names when finished walking through matches */
479 if (xpc.xp_numfiles != -1
480 && !(c == p_wc && KeyTyped) && c != p_wcm
481 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
482 && c != Ctrl_L)
483 {
484 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
485 did_wild_list = FALSE;
486#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000487 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000488#endif
489 xpc.xp_context = EXPAND_NOTHING;
490 wim_index = 0;
491#ifdef FEAT_WILDMENU
492 if (p_wmnu && wild_menu_showing != 0)
493 {
494 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000495 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000496
497 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000498 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499
500 if (wild_menu_showing == WM_SCROLLED)
501 {
502 /* Entered command line, move it up */
503 cmdline_row--;
504 redrawcmd();
505 }
506 else if (save_p_ls != -1)
507 {
508 /* restore 'laststatus' and 'winminheight' */
509 p_ls = save_p_ls;
510 p_wmh = save_p_wmh;
511 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000512 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000514 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 redrawcmd();
516 save_p_ls = -1;
517 }
518 else
519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 redraw_statuslines();
522 }
523 KeyTyped = skt;
524 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000525 if (ccline.input_fn)
526 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 }
528#endif
529 }
530
531#ifdef FEAT_WILDMENU
532 /* Special translations for 'wildmenu' */
533 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
534 {
535 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000536 if (c == K_DOWN && ccline.cmdpos > 0
537 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000539 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 /* Hitting <Up>: Remove one submenu name in front of the
542 * cursor */
543 int found = FALSE;
544
545 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
546 i = 0;
547 while (--j > 0)
548 {
549 /* check for start of menu name */
550 if (ccline.cmdbuff[j] == ' '
551 && ccline.cmdbuff[j - 1] != '\\')
552 {
553 i = j + 1;
554 break;
555 }
556 /* check for start of submenu name */
557 if (ccline.cmdbuff[j] == '.'
558 && ccline.cmdbuff[j - 1] != '\\')
559 {
560 if (found)
561 {
562 i = j + 1;
563 break;
564 }
565 else
566 found = TRUE;
567 }
568 }
569 if (i > 0)
570 cmdline_del(i);
571 c = p_wc;
572 xpc.xp_context = EXPAND_NOTHING;
573 }
574 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000575 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000576 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000577 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 char_u upseg[5];
580
581 upseg[0] = PATHSEP;
582 upseg[1] = '.';
583 upseg[2] = '.';
584 upseg[3] = PATHSEP;
585 upseg[4] = NUL;
586
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000587 if (c == K_DOWN
588 && ccline.cmdpos > 0
589 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
590 && (ccline.cmdpos < 3
591 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
593 {
594 /* go down a directory */
595 c = p_wc;
596 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000597 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 {
599 /* If in a direct ancestor, strip off one ../ to go down */
600 int found = FALSE;
601
602 j = ccline.cmdpos;
603 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
604 while (--j > i)
605 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000606#ifdef FEAT_MBYTE
607 if (has_mbyte)
608 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 if (vim_ispathsep(ccline.cmdbuff[j]))
611 {
612 found = TRUE;
613 break;
614 }
615 }
616 if (found
617 && ccline.cmdbuff[j - 1] == '.'
618 && ccline.cmdbuff[j - 2] == '.'
619 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
620 {
621 cmdline_del(j - 2);
622 c = p_wc;
623 }
624 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000625 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 {
627 /* go up a directory */
628 int found = FALSE;
629
630 j = ccline.cmdpos - 1;
631 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
632 while (--j > i)
633 {
634#ifdef FEAT_MBYTE
635 if (has_mbyte)
636 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
637#endif
638 if (vim_ispathsep(ccline.cmdbuff[j])
639#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100640 && vim_strchr((char_u *)" *?[{`$%#",
641 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642#endif
643 )
644 {
645 if (found)
646 {
647 i = j + 1;
648 break;
649 }
650 else
651 found = TRUE;
652 }
653 }
654
655 if (!found)
656 j = i;
657 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
658 j += 4;
659 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
660 && j == i)
661 j += 3;
662 else
663 j = 0;
664 if (j > 0)
665 {
666 /* TODO this is only for DOS/UNIX systems - need to put in
667 * machine-specific stuff here and in upseg init */
668 cmdline_del(j);
669 put_on_cmdline(upseg + 1, 3, FALSE);
670 }
671 else if (ccline.cmdpos > i)
672 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100673
674 /* Now complete in the new directory. Set KeyTyped in case the
675 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100677 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 }
679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680
681#endif /* FEAT_WILDMENU */
682
683 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
684 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
685 if (c == Ctrl_BSL)
686 {
687 ++no_mapping;
688 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000689 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 --no_mapping;
691 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200692 /* CTRL-\ e doesn't work when obtaining an expression, unless it
693 * is in a mapping. */
694 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
695 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 {
697 vungetc(c);
698 c = Ctrl_BSL;
699 }
700#ifdef FEAT_EVAL
701 else if (c == 'e')
702 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200703 char_u *p = NULL;
704 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705
706 /*
707 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000708 * Need to save and restore the current command line, to be
709 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 */
711 if (ccline.cmdpos == ccline.cmdlen)
712 new_cmdpos = 99999; /* keep it at the end */
713 else
714 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000715
716 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000718 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 if (c == '=')
720 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000721 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000722 * to avoid nasty things like going to another buffer when
723 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000724 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000725 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000727 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000728 restore_cmdline(&save_ccline);
729
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200730 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200732 len = (int)STRLEN(p);
733 if (realloc_cmdbuff(len + 1) == OK)
734 {
735 ccline.cmdlen = len;
736 STRCPY(ccline.cmdbuff, p);
737 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200739 /* Restore the cursor or use the position set with
740 * set_cmdline_pos(). */
741 if (new_cmdpos > ccline.cmdlen)
742 ccline.cmdpos = ccline.cmdlen;
743 else
744 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200746 KeyTyped = FALSE; /* Don't do p_wc completion. */
747 redrawcmd();
748 goto cmdline_changed;
749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 }
751 }
752 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100753 got_int = FALSE; /* don't abandon the command line */
754 did_emsg = FALSE;
755 emsg_on_display = FALSE;
756 redrawcmd();
757 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 }
759#endif
760 else
761 {
762 if (c == Ctrl_G && p_im && restart_edit == 0)
763 restart_edit = 'a';
764 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
765 in history */
766 goto returncmd; /* back to Normal mode */
767 }
768 }
769
770#ifdef FEAT_CMDWIN
771 if (c == cedit_key || c == K_CMDWIN)
772 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200773 if (ex_normal_busy == 0 && got_int == FALSE)
774 {
775 /*
776 * Open a window to edit the command line (and history).
777 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200778 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200779 some_key_typed = TRUE;
780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 }
782# ifdef FEAT_DIGRAPHS
783 else
784# endif
785#endif
786#ifdef FEAT_DIGRAPHS
787 c = do_digraph(c);
788#endif
789
790 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
791 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000793 /* In Ex mode a backslash escapes a newline. */
794 if (exmode_active
795 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000796 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000797 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000798 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000800 if (c == K_KENTER)
801 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000803 else
804 {
805 gotesc = FALSE; /* Might have typed ESC previously, don't
806 truncate the cmdline now. */
807 if (ccheck_abbr(c + ABBR_OFF))
808 goto cmdline_changed;
809 if (!cmd_silent)
810 {
811 windgoto(msg_row, 0);
812 out_flush();
813 }
814 break;
815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817
818 /*
819 * Completion for 'wildchar' or 'wildcharm' key.
820 * - hitting <ESC> twice means: abandon command line.
821 * - wildcard expansion is only done when the 'wildchar' key is really
822 * typed, not when it comes from a macro
823 */
824 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
825 {
826 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
827 {
828 /* if 'wildmode' contains "list" may still need to list */
829 if (xpc.xp_numfiles > 1
830 && !did_wild_list
831 && (wim_flags[wim_index] & WIM_LIST))
832 {
833 (void)showmatches(&xpc, FALSE);
834 redrawcmd();
835 did_wild_list = TRUE;
836 }
837 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100838 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
839 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100841 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
842 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 else
844 res = OK; /* don't insert 'wildchar' now */
845 }
846 else /* typed p_wc first time */
847 {
848 wim_index = 0;
849 j = ccline.cmdpos;
850 /* if 'wildmode' first contains "longest", get longest
851 * common part */
852 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100853 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
854 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100856 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
857 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859 /* if interrupted while completing, behave like it failed */
860 if (got_int)
861 {
862 (void)vpeekc(); /* remove <C-C> from input stream */
863 got_int = FALSE; /* don't abandon the command line */
864 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
865#ifdef FEAT_WILDMENU
866 xpc.xp_context = EXPAND_NOTHING;
867#endif
868 goto cmdline_changed;
869 }
870
871 /* when more than one match, and 'wildmode' first contains
872 * "list", or no change and 'wildmode' contains "longest,list",
873 * list all matches */
874 if (res == OK && xpc.xp_numfiles > 1)
875 {
876 /* a "longest" that didn't do anything is skipped (but not
877 * "list:longest") */
878 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
879 wim_index = 1;
880 if ((wim_flags[wim_index] & WIM_LIST)
881#ifdef FEAT_WILDMENU
882 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
883#endif
884 )
885 {
886 if (!(wim_flags[0] & WIM_LONGEST))
887 {
888#ifdef FEAT_WILDMENU
889 int p_wmnu_save = p_wmnu;
890 p_wmnu = 0;
891#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100892 /* remove match */
893 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894#ifdef FEAT_WILDMENU
895 p_wmnu = p_wmnu_save;
896#endif
897 }
898#ifdef FEAT_WILDMENU
899 (void)showmatches(&xpc, p_wmnu
900 && ((wim_flags[wim_index] & WIM_LIST) == 0));
901#else
902 (void)showmatches(&xpc, FALSE);
903#endif
904 redrawcmd();
905 did_wild_list = TRUE;
906 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100907 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
908 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100910 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
911 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200914 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916#ifdef FEAT_WILDMENU
917 else if (xpc.xp_numfiles == -1)
918 xpc.xp_context = EXPAND_NOTHING;
919#endif
920 }
921 if (wim_index < 3)
922 ++wim_index;
923 if (c == ESC)
924 gotesc = TRUE;
925 if (res == OK)
926 goto cmdline_changed;
927 }
928
929 gotesc = FALSE;
930
931 /* <S-Tab> goes to last match, in a clumsy way */
932 if (c == K_S_TAB && KeyTyped)
933 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100934 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
935 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
936 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 goto cmdline_changed;
938 }
939
940 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
941 c = NL;
942
943 do_abbr = TRUE; /* default: check for abbreviation */
944
945 /*
946 * Big switch for a typed command line character.
947 */
948 switch (c)
949 {
950 case K_BS:
951 case Ctrl_H:
952 case K_DEL:
953 case K_KDEL:
954 case Ctrl_W:
955#ifdef FEAT_FKMAP
956 if (cmd_fkmap && c == K_BS)
957 c = K_DEL;
958#endif
959 if (c == K_KDEL)
960 c = K_DEL;
961
962 /*
963 * delete current character is the same as backspace on next
964 * character, except at end of line
965 */
966 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
967 ++ccline.cmdpos;
968#ifdef FEAT_MBYTE
969 if (has_mbyte && c == K_DEL)
970 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
971 ccline.cmdbuff + ccline.cmdpos);
972#endif
973 if (ccline.cmdpos > 0)
974 {
975 char_u *p;
976
977 j = ccline.cmdpos;
978 p = ccline.cmdbuff + j;
979#ifdef FEAT_MBYTE
980 if (has_mbyte)
981 {
982 p = mb_prevptr(ccline.cmdbuff, p);
983 if (c == Ctrl_W)
984 {
985 while (p > ccline.cmdbuff && vim_isspace(*p))
986 p = mb_prevptr(ccline.cmdbuff, p);
987 i = mb_get_class(p);
988 while (p > ccline.cmdbuff && mb_get_class(p) == i)
989 p = mb_prevptr(ccline.cmdbuff, p);
990 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000991 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 }
993 }
994 else
995#endif
996 if (c == Ctrl_W)
997 {
998 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
999 --p;
1000 i = vim_iswordc(p[-1]);
1001 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1002 && vim_iswordc(p[-1]) == i)
1003 --p;
1004 }
1005 else
1006 --p;
1007 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1008 ccline.cmdlen -= j - ccline.cmdpos;
1009 i = ccline.cmdpos;
1010 while (i < ccline.cmdlen)
1011 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1012
1013 /* Truncate at the end, required for multi-byte chars. */
1014 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001015#ifdef FEAT_SEARCH_EXTRA
1016 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001017 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001018 search_start = save_cursor;
1019 /* save view settings, so that the screen
1020 * won't be restored at the wrong position */
1021 old_curswant = init_curswant;
1022 old_leftcol = init_leftcol;
1023 old_topline = init_topline;
1024# ifdef FEAT_DIFF
1025 old_topfill = init_topfill;
1026# endif
1027 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001028 }
1029#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 redrawcmd();
1031 }
1032 else if (ccline.cmdlen == 0 && c != Ctrl_W
1033 && ccline.cmdprompt == NULL && indent == 0)
1034 {
1035 /* In ex and debug mode it doesn't make sense to return. */
1036 if (exmode_active
1037#ifdef FEAT_EVAL
1038 || ccline.cmdfirstc == '>'
1039#endif
1040 )
1041 goto cmdline_not_changed;
1042
1043 vim_free(ccline.cmdbuff); /* no commandline to return */
1044 ccline.cmdbuff = NULL;
1045 if (!cmd_silent)
1046 {
1047#ifdef FEAT_RIGHTLEFT
1048 if (cmdmsg_rl)
1049 msg_col = Columns;
1050 else
1051#endif
1052 msg_col = 0;
1053 msg_putchar(' '); /* delete ':' */
1054 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001055#ifdef FEAT_SEARCH_EXTRA
1056 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001057 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 redraw_cmdline = TRUE;
1060 goto returncmd; /* back to cmd mode */
1061 }
1062 goto cmdline_changed;
1063
1064 case K_INS:
1065 case K_KINS:
1066#ifdef FEAT_FKMAP
1067 /* if Farsi mode set, we are in reverse insert mode -
1068 Do not change the mode */
1069 if (cmd_fkmap)
1070 beep_flush();
1071 else
1072#endif
1073 ccline.overstrike = !ccline.overstrike;
1074#ifdef CURSOR_SHAPE
1075 ui_cursor_shape(); /* may show different cursor shape */
1076#endif
1077 goto cmdline_not_changed;
1078
1079 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001080 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 /* ":lmap" mappings exists, toggle use of mappings. */
1083 State ^= LANGMAP;
1084#ifdef USE_IM_CONTROL
1085 im_set_active(FALSE); /* Disable input method */
1086#endif
1087 if (b_im_ptr != NULL)
1088 {
1089 if (State & LANGMAP)
1090 *b_im_ptr = B_IMODE_LMAP;
1091 else
1092 *b_im_ptr = B_IMODE_NONE;
1093 }
1094 }
1095#ifdef USE_IM_CONTROL
1096 else
1097 {
1098 /* There are no ":lmap" mappings, toggle IM. When
1099 * 'imdisable' is set don't try getting the status, it's
1100 * always off. */
1101 if ((p_imdisable && b_im_ptr != NULL)
1102 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1103 {
1104 im_set_active(FALSE); /* Disable input method */
1105 if (b_im_ptr != NULL)
1106 *b_im_ptr = B_IMODE_NONE;
1107 }
1108 else
1109 {
1110 im_set_active(TRUE); /* Enable input method */
1111 if (b_im_ptr != NULL)
1112 *b_im_ptr = B_IMODE_IM;
1113 }
1114 }
1115#endif
1116 if (b_im_ptr != NULL)
1117 {
1118 if (b_im_ptr == &curbuf->b_p_iminsert)
1119 set_iminsert_global();
1120 else
1121 set_imsearch_global();
1122 }
1123#ifdef CURSOR_SHAPE
1124 ui_cursor_shape(); /* may show different cursor shape */
1125#endif
1126#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
1127 /* Show/unshow value of 'keymap' in status lines later. */
1128 status_redraw_curbuf();
1129#endif
1130 goto cmdline_not_changed;
1131
1132/* case '@': only in very old vi */
1133 case Ctrl_U:
1134 /* delete all characters left of the cursor */
1135 j = ccline.cmdpos;
1136 ccline.cmdlen -= j;
1137 i = ccline.cmdpos = 0;
1138 while (i < ccline.cmdlen)
1139 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1140 /* Truncate at the end, required for multi-byte chars. */
1141 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001142#ifdef FEAT_SEARCH_EXTRA
1143 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001144 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 redrawcmd();
1147 goto cmdline_changed;
1148
1149#ifdef FEAT_CLIPBOARD
1150 case Ctrl_Y:
1151 /* Copy the modeless selection, if there is one. */
1152 if (clip_star.state != SELECT_CLEARED)
1153 {
1154 if (clip_star.state == SELECT_DONE)
1155 clip_copy_modeless_selection(TRUE);
1156 goto cmdline_not_changed;
1157 }
1158 break;
1159#endif
1160
1161 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1162 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001163 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001164 * ":normal" runs out of characters. */
1165 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001166 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 goto cmdline_not_changed;
1168
1169 gotesc = TRUE; /* will free ccline.cmdbuff after
1170 putting it in history */
1171 goto returncmd; /* back to cmd mode */
1172
1173 case Ctrl_R: /* insert register */
1174#ifdef USE_ON_FLY_SCROLL
1175 dont_scroll = TRUE; /* disallow scrolling here */
1176#endif
1177 putcmdline('"', TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001178 extra_char = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001180 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181 if (i == Ctrl_O)
1182 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1183 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001184 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001185 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 --no_mapping;
1187#ifdef FEAT_EVAL
1188 /*
1189 * Insert the result of an expression.
1190 * Need to save the current command line, to be able to enter
1191 * a new one...
1192 */
1193 new_cmdpos = -1;
1194 if (c == '=')
1195 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1197 {
1198 beep_flush();
1199 c = ESC;
1200 }
1201 else
1202 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001203 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001205 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 }
1207 }
1208#endif
1209 if (c != ESC) /* use ESC to cancel inserting register */
1210 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001211 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001212
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001213#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001214 /* When there was a serious error abort getting the
1215 * command line. */
1216 if (aborting())
1217 {
1218 gotesc = TRUE; /* will free ccline.cmdbuff after
1219 putting it in history */
1220 goto returncmd; /* back to cmd mode */
1221 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 KeyTyped = FALSE; /* Don't do p_wc completion. */
1224#ifdef FEAT_EVAL
1225 if (new_cmdpos >= 0)
1226 {
1227 /* set_cmdline_pos() was used */
1228 if (new_cmdpos > ccline.cmdlen)
1229 ccline.cmdpos = ccline.cmdlen;
1230 else
1231 ccline.cmdpos = new_cmdpos;
1232 }
1233#endif
1234 }
1235 redrawcmd();
1236 goto cmdline_changed;
1237
1238 case Ctrl_D:
1239 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1240 break; /* Use ^D as normal char instead */
1241
1242 redrawcmd();
1243 continue; /* don't do incremental search now */
1244
1245 case K_RIGHT:
1246 case K_S_RIGHT:
1247 case K_C_RIGHT:
1248 do
1249 {
1250 if (ccline.cmdpos >= ccline.cmdlen)
1251 break;
1252 i = cmdline_charsize(ccline.cmdpos);
1253 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1254 break;
1255 ccline.cmdspos += i;
1256#ifdef FEAT_MBYTE
1257 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001258 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 + ccline.cmdpos);
1260 else
1261#endif
1262 ++ccline.cmdpos;
1263 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001264 while ((c == K_S_RIGHT || c == K_C_RIGHT
1265 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1267#ifdef FEAT_MBYTE
1268 if (has_mbyte)
1269 set_cmdspos_cursor();
1270#endif
1271 goto cmdline_not_changed;
1272
1273 case K_LEFT:
1274 case K_S_LEFT:
1275 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001276 if (ccline.cmdpos == 0)
1277 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 do
1279 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 --ccline.cmdpos;
1281#ifdef FEAT_MBYTE
1282 if (has_mbyte) /* move to first byte of char */
1283 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1284 ccline.cmdbuff + ccline.cmdpos);
1285#endif
1286 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1287 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001288 while (ccline.cmdpos > 0
1289 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001290 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1292#ifdef FEAT_MBYTE
1293 if (has_mbyte)
1294 set_cmdspos_cursor();
1295#endif
1296 goto cmdline_not_changed;
1297
1298 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001299 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001300 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
Bram Moolenaar4770d092006-01-12 23:22:24 +00001302#ifdef FEAT_GUI_W32
1303 /* On Win32 ignore <M-F4>, we get it when closing the window was
1304 * cancelled. */
1305 case K_F4:
1306 if (mod_mask == MOD_MASK_ALT)
1307 {
1308 redrawcmd(); /* somehow the cmdline is cleared */
1309 goto cmdline_not_changed;
1310 }
1311 break;
1312#endif
1313
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#ifdef FEAT_MOUSE
1315 case K_MIDDLEDRAG:
1316 case K_MIDDLERELEASE:
1317 goto cmdline_not_changed; /* Ignore mouse */
1318
1319 case K_MIDDLEMOUSE:
1320# ifdef FEAT_GUI
1321 /* When GUI is active, also paste when 'mouse' is empty */
1322 if (!gui.in_use)
1323# endif
1324 if (!mouse_has(MOUSE_COMMAND))
1325 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001326# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001328 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001330# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001331 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 redrawcmd();
1333 goto cmdline_changed;
1334
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001335# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001337 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 redrawcmd();
1339 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001340# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 case K_LEFTDRAG:
1343 case K_LEFTRELEASE:
1344 case K_RIGHTDRAG:
1345 case K_RIGHTRELEASE:
1346 /* Ignore drag and release events when the button-down wasn't
1347 * seen before. */
1348 if (ignore_drag_release)
1349 goto cmdline_not_changed;
1350 /* FALLTHROUGH */
1351 case K_LEFTMOUSE:
1352 case K_RIGHTMOUSE:
1353 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1354 ignore_drag_release = TRUE;
1355 else
1356 ignore_drag_release = FALSE;
1357# ifdef FEAT_GUI
1358 /* When GUI is active, also move when 'mouse' is empty */
1359 if (!gui.in_use)
1360# endif
1361 if (!mouse_has(MOUSE_COMMAND))
1362 goto cmdline_not_changed; /* Ignore mouse */
1363# ifdef FEAT_CLIPBOARD
1364 if (mouse_row < cmdline_row && clip_star.available)
1365 {
1366 int button, is_click, is_drag;
1367
1368 /*
1369 * Handle modeless selection.
1370 */
1371 button = get_mouse_button(KEY2TERMCAP1(c),
1372 &is_click, &is_drag);
1373 if (mouse_model_popup() && button == MOUSE_LEFT
1374 && (mod_mask & MOD_MASK_SHIFT))
1375 {
1376 /* Translate shift-left to right button. */
1377 button = MOUSE_RIGHT;
1378 mod_mask &= ~MOD_MASK_SHIFT;
1379 }
1380 clip_modeless(button, is_click, is_drag);
1381 goto cmdline_not_changed;
1382 }
1383# endif
1384
1385 set_cmdspos();
1386 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1387 ++ccline.cmdpos)
1388 {
1389 i = cmdline_charsize(ccline.cmdpos);
1390 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1391 && mouse_col < ccline.cmdspos % Columns + i)
1392 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001393# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 if (has_mbyte)
1395 {
1396 /* Count ">" for double-wide char that doesn't fit. */
1397 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001398 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 + ccline.cmdpos) - 1;
1400 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001401# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 ccline.cmdspos += i;
1403 }
1404 goto cmdline_not_changed;
1405
1406 /* Mouse scroll wheel: ignored here */
1407 case K_MOUSEDOWN:
1408 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001409 case K_MOUSELEFT:
1410 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /* Alternate buttons ignored here */
1412 case K_X1MOUSE:
1413 case K_X1DRAG:
1414 case K_X1RELEASE:
1415 case K_X2MOUSE:
1416 case K_X2DRAG:
1417 case K_X2RELEASE:
1418 goto cmdline_not_changed;
1419
1420#endif /* FEAT_MOUSE */
1421
1422#ifdef FEAT_GUI
1423 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1424 case K_LEFTRELEASE_NM:
1425 goto cmdline_not_changed;
1426
1427 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001428 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
1430 gui_do_scroll();
1431 redrawcmd();
1432 }
1433 goto cmdline_not_changed;
1434
1435 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001436 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001438 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 redrawcmd();
1440 }
1441 goto cmdline_not_changed;
1442#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001443#ifdef FEAT_GUI_TABLINE
1444 case K_TABLINE:
1445 case K_TABMENU:
1446 /* Don't want to change any tabs here. Make sure the same tab
1447 * is still selected. */
1448 if (gui_use_tabline())
1449 gui_mch_set_curtab(tabpage_index(curtab));
1450 goto cmdline_not_changed;
1451#endif
1452
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 case K_SELECT: /* end of Select mode mapping - ignore */
1454 goto cmdline_not_changed;
1455
1456 case Ctrl_B: /* begin of command line */
1457 case K_HOME:
1458 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 case K_S_HOME:
1460 case K_C_HOME:
1461 ccline.cmdpos = 0;
1462 set_cmdspos();
1463 goto cmdline_not_changed;
1464
1465 case Ctrl_E: /* end of command line */
1466 case K_END:
1467 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 case K_S_END:
1469 case K_C_END:
1470 ccline.cmdpos = ccline.cmdlen;
1471 set_cmdspos_cursor();
1472 goto cmdline_not_changed;
1473
1474 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001475 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 break;
1477 goto cmdline_changed;
1478
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001479 case Ctrl_L:
1480#ifdef FEAT_SEARCH_EXTRA
1481 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1482 {
1483 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001484 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001485 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001486 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001487 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001488 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001489 c = gchar_cursor();
1490 /* If 'ignorecase' and 'smartcase' are set and the
1491 * command line has no uppercase characters, convert
1492 * the character to lowercase */
1493 if (p_ic && p_scs
1494 && !pat_has_uppercase(ccline.cmdbuff))
1495 c = MB_TOLOWER(c);
1496 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001497 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001498 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001499 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001500 != NULL)
1501 {
1502 /* put a backslash before special
1503 * characters */
1504 stuffcharReadbuff(c);
1505 c = '\\';
1506 }
1507 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001508 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001509 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001510 }
1511 goto cmdline_not_changed;
1512 }
1513#endif
1514
1515 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001516 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 break;
1518 goto cmdline_changed;
1519
1520 case Ctrl_N: /* next match */
1521 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001522 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001524 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1525 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001527 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
Bram Moolenaar11956692016-08-27 16:26:56 +02001529 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530
1531#ifdef FEAT_CMDHIST
1532 case K_UP:
1533 case K_DOWN:
1534 case K_S_UP:
1535 case K_S_DOWN:
1536 case K_PAGEUP:
1537 case K_KPAGEUP:
1538 case K_PAGEDOWN:
1539 case K_KPAGEDOWN:
1540 if (hislen == 0 || firstc == NUL) /* no history */
1541 goto cmdline_not_changed;
1542
1543 i = hiscnt;
1544
1545 /* save current command string so it can be restored later */
1546 if (lookfor == NULL)
1547 {
1548 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1549 goto cmdline_not_changed;
1550 lookfor[ccline.cmdpos] = NUL;
1551 }
1552
1553 j = (int)STRLEN(lookfor);
1554 for (;;)
1555 {
1556 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001557 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001558 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 {
1560 if (hiscnt == hislen) /* first time */
1561 hiscnt = hisidx[histype];
1562 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1563 hiscnt = hislen - 1;
1564 else if (hiscnt != hisidx[histype] + 1)
1565 --hiscnt;
1566 else /* at top of list */
1567 {
1568 hiscnt = i;
1569 break;
1570 }
1571 }
1572 else /* one step forwards */
1573 {
1574 /* on last entry, clear the line */
1575 if (hiscnt == hisidx[histype])
1576 {
1577 hiscnt = hislen;
1578 break;
1579 }
1580
1581 /* not on a history line, nothing to do */
1582 if (hiscnt == hislen)
1583 break;
1584 if (hiscnt == hislen - 1) /* wrap around */
1585 hiscnt = 0;
1586 else
1587 ++hiscnt;
1588 }
1589 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1590 {
1591 hiscnt = i;
1592 break;
1593 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001594 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001595 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 || STRNCMP(history[histype][hiscnt].hisstr,
1597 lookfor, (size_t)j) == 0)
1598 break;
1599 }
1600
1601 if (hiscnt != i) /* jumped to other entry */
1602 {
1603 char_u *p;
1604 int len;
1605 int old_firstc;
1606
1607 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001608 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (hiscnt == hislen)
1610 p = lookfor; /* back to the old one */
1611 else
1612 p = history[histype][hiscnt].hisstr;
1613
1614 if (histype == HIST_SEARCH
1615 && p != lookfor
1616 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1617 {
1618 /* Correct for the separator character used when
1619 * adding the history entry vs the one used now.
1620 * First loop: count length.
1621 * Second loop: copy the characters. */
1622 for (i = 0; i <= 1; ++i)
1623 {
1624 len = 0;
1625 for (j = 0; p[j] != NUL; ++j)
1626 {
1627 /* Replace old sep with new sep, unless it is
1628 * escaped. */
1629 if (p[j] == old_firstc
1630 && (j == 0 || p[j - 1] != '\\'))
1631 {
1632 if (i > 0)
1633 ccline.cmdbuff[len] = firstc;
1634 }
1635 else
1636 {
1637 /* Escape new sep, unless it is already
1638 * escaped. */
1639 if (p[j] == firstc
1640 && (j == 0 || p[j - 1] != '\\'))
1641 {
1642 if (i > 0)
1643 ccline.cmdbuff[len] = '\\';
1644 ++len;
1645 }
1646 if (i > 0)
1647 ccline.cmdbuff[len] = p[j];
1648 }
1649 ++len;
1650 }
1651 if (i == 0)
1652 {
1653 alloc_cmdbuff(len);
1654 if (ccline.cmdbuff == NULL)
1655 goto returncmd;
1656 }
1657 }
1658 ccline.cmdbuff[len] = NUL;
1659 }
1660 else
1661 {
1662 alloc_cmdbuff((int)STRLEN(p));
1663 if (ccline.cmdbuff == NULL)
1664 goto returncmd;
1665 STRCPY(ccline.cmdbuff, p);
1666 }
1667
1668 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1669 redrawcmd();
1670 goto cmdline_changed;
1671 }
1672 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001673#endif
1674 goto cmdline_not_changed;
1675
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001676#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001677 case Ctrl_G: /* next match */
1678 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001679 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1680 {
1681 pos_T t;
1682 int search_flags = SEARCH_KEEP + SEARCH_NOOF
1683 + SEARCH_PEEK;
1684
1685 if (char_avail())
1686 continue;
1687 cursor_off();
1688 out_flush();
1689 if (c == Ctrl_G)
1690 {
1691 t = match_end;
1692 search_flags += SEARCH_COL;
1693 }
1694 else
1695 t = match_start;
1696 ++emsg_off;
1697 i = searchit(curwin, curbuf, &t,
1698 c == Ctrl_G ? FORWARD : BACKWARD,
1699 ccline.cmdbuff, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001700 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001701 --emsg_off;
1702 if (i)
1703 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001704 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001705 match_end = t;
1706 match_start = t;
1707 if (c == Ctrl_T && firstc == '/')
1708 {
1709 /* move just before the current match, so that
1710 * when nv_search finishes the cursor will be
1711 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001712 search_start = t;
1713 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001714 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001715 else if (c == Ctrl_G && firstc == '?')
1716 {
1717 /* move just after the current match, so that
1718 * when nv_search finishes the cursor will be
1719 * put back on the match */
1720 search_start = t;
1721 (void)incl(&search_start);
1722 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001723 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001724 {
1725 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001726 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001727 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001728 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001729 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001730 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001731 }
1732
1733 set_search_match(&match_end);
1734 curwin->w_cursor = match_start;
1735 changed_cline_bef_curs();
1736 update_topline();
1737 validate_cursor();
1738 highlight_match = TRUE;
1739 old_curswant = curwin->w_curswant;
1740 old_leftcol = curwin->w_leftcol;
1741 old_topline = curwin->w_topline;
1742# ifdef FEAT_DIFF
1743 old_topfill = curwin->w_topfill;
1744# endif
1745 old_botline = curwin->w_botline;
1746 update_screen(NOT_VALID);
1747 redrawcmdline();
1748 }
1749 else
1750 vim_beep(BO_ERROR);
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001751 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001752 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001753 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754#endif
1755
1756 case Ctrl_V:
1757 case Ctrl_Q:
1758#ifdef FEAT_MOUSE
1759 ignore_drag_release = TRUE;
1760#endif
1761 putcmdline('^', TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001762 extra_char = '^';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 c = get_literal(); /* get next (two) character(s) */
1764 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001765 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#ifdef FEAT_MBYTE
1767 /* may need to remove ^ when composing char was typed */
1768 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1769 {
1770 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1771 msg_putchar(' ');
1772 cursorcmd();
1773 }
1774#endif
1775 break;
1776
1777#ifdef FEAT_DIGRAPHS
1778 case Ctrl_K:
1779#ifdef FEAT_MOUSE
1780 ignore_drag_release = TRUE;
1781#endif
1782 putcmdline('?', TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001783 extra_char = '?';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#ifdef USE_ON_FLY_SCROLL
1785 dont_scroll = TRUE; /* disallow scrolling here */
1786#endif
1787 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001788 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (c != NUL)
1790 break;
1791
1792 redrawcmd();
1793 goto cmdline_not_changed;
1794#endif /* FEAT_DIGRAPHS */
1795
1796#ifdef FEAT_RIGHTLEFT
1797 case Ctrl__: /* CTRL-_: switch language mode */
1798 if (!p_ari)
1799 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001800# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 if (p_altkeymap)
1802 {
1803 cmd_fkmap = !cmd_fkmap;
1804 if (cmd_fkmap) /* in Farsi always in Insert mode */
1805 ccline.overstrike = FALSE;
1806 }
1807 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 cmd_hkmap = !cmd_hkmap;
1810 goto cmdline_not_changed;
1811#endif
1812
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001813 case K_PS:
1814 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1815 goto cmdline_changed;
1816
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 default:
1818#ifdef UNIX
1819 if (c == intr_char)
1820 {
1821 gotesc = TRUE; /* will free ccline.cmdbuff after
1822 putting it in history */
1823 goto returncmd; /* back to Normal mode */
1824 }
1825#endif
1826 /*
1827 * Normal character with no special meaning. Just set mod_mask
1828 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1829 * the string <S-Space>. This should only happen after ^V.
1830 */
1831 if (!IS_SPECIAL(c))
1832 mod_mask = 0x0;
1833 break;
1834 }
1835 /*
1836 * End of switch on command line character.
1837 * We come here if we have a normal character.
1838 */
1839
Bram Moolenaarede3e632013-06-23 16:16:19 +02001840 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#ifdef FEAT_MBYTE
1842 /* Add ABBR_OFF for characters above 0x100, this is
1843 * what check_abbr() expects. */
1844 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1845#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001846 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 goto cmdline_changed;
1848
1849 /*
1850 * put the character in the command line
1851 */
1852 if (IS_SPECIAL(c) || mod_mask != 0)
1853 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1854 else
1855 {
1856#ifdef FEAT_MBYTE
1857 if (has_mbyte)
1858 {
1859 j = (*mb_char2bytes)(c, IObuff);
1860 IObuff[j] = NUL; /* exclude composing chars */
1861 put_on_cmdline(IObuff, j, TRUE);
1862 }
1863 else
1864#endif
1865 {
1866 IObuff[0] = c;
1867 put_on_cmdline(IObuff, 1, TRUE);
1868 }
1869 }
1870 goto cmdline_changed;
1871
1872/*
1873 * This part implements incremental searches for "/" and "?"
1874 * Jump to cmdline_not_changed when a character has been read but the command
1875 * line did not change. Then we only search and redraw if something changed in
1876 * the past.
1877 * Jump to cmdline_changed when the command line did change.
1878 * (Sorry for the goto's, I know it is ugly).
1879 */
1880cmdline_not_changed:
1881#ifdef FEAT_SEARCH_EXTRA
1882 if (!incsearch_postponed)
1883 continue;
1884#endif
1885
1886cmdline_changed:
1887#ifdef FEAT_SEARCH_EXTRA
1888 /*
1889 * 'incsearch' highlighting.
1890 */
1891 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1892 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001893 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001894#ifdef FEAT_RELTIME
1895 proftime_T tm;
1896#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001897
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 /* if there is a character waiting, search and redraw later */
1899 if (char_avail())
1900 {
1901 incsearch_postponed = TRUE;
1902 continue;
1903 }
1904 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001905 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
1907 /* If there is no command line, don't do anything */
1908 if (ccline.cmdlen == 0)
1909 i = 0;
1910 else
1911 {
1912 cursor_off(); /* so the user knows we're busy */
1913 out_flush();
1914 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001915#ifdef FEAT_RELTIME
1916 /* Set the time limit to half a second. */
1917 profile_setlimit(500L, &tm);
1918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001920 SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK,
1921#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001922 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001923#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001924 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001925#endif
1926 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 --emsg_off;
1928 /* if interrupted while searching, behave like it failed */
1929 if (got_int)
1930 {
1931 (void)vpeekc(); /* remove <C-C> from input stream */
1932 got_int = FALSE; /* don't abandon the command line */
1933 i = 0;
1934 }
1935 else if (char_avail())
1936 /* cancelled searching because a char was typed */
1937 incsearch_postponed = TRUE;
1938 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001939 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 highlight_match = TRUE; /* highlight position */
1941 else
1942 highlight_match = FALSE; /* remove highlight */
1943
1944 /* first restore the old curwin values, so the screen is
1945 * positioned in the same way as the actual search command */
1946 curwin->w_leftcol = old_leftcol;
1947 curwin->w_topline = old_topline;
1948# ifdef FEAT_DIFF
1949 curwin->w_topfill = old_topfill;
1950# endif
1951 curwin->w_botline = old_botline;
1952 changed_cline_bef_curs();
1953 update_topline();
1954
1955 if (i != 0)
1956 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001957 pos_T save_pos = curwin->w_cursor;
1958
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001959 match_start = curwin->w_cursor;
1960 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001962 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001963 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001964 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00001966 else
1967 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001968
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00001970# ifdef FEAT_WINDOWS
1971 /* May redraw the status line to show the cursor position. */
1972 if (p_ru && curwin->w_status_height > 0)
1973 curwin->w_redr_status = TRUE;
1974# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001976 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00001977 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001978 restore_cmdline(&save_ccline);
1979
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001980 /* Leave it at the end to make CTRL-R CTRL-W work. */
1981 if (i != 0)
1982 curwin->w_cursor = end_pos;
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 msg_starthere();
1985 redrawcmdline();
1986 did_incsearch = TRUE;
1987 }
1988#else /* FEAT_SEARCH_EXTRA */
1989 ;
1990#endif
1991
1992#ifdef FEAT_RIGHTLEFT
1993 if (cmdmsg_rl
1994# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00001995 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996# endif
1997 )
1998 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01001999 * right-left typing. Not efficient, but it works.
2000 * Do it only when there are no characters left to read
2001 * to avoid useless intermediate redraws. */
2002 if (vpeekc() == NUL)
2003 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#endif
2005 }
2006
2007returncmd:
2008
2009#ifdef FEAT_RIGHTLEFT
2010 cmdmsg_rl = FALSE;
2011#endif
2012
2013#ifdef FEAT_FKMAP
2014 cmd_fkmap = 0;
2015#endif
2016
2017 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002018 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020#ifdef FEAT_SEARCH_EXTRA
2021 if (did_incsearch)
2022 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002023 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002024 curwin->w_cursor = save_cursor;
2025 else
2026 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002027 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002028 {
2029 /* put the '" mark at the original position */
2030 curwin->w_cursor = save_cursor;
2031 setpcmark();
2032 }
2033 curwin->w_cursor = search_start;
2034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 curwin->w_curswant = old_curswant;
2036 curwin->w_leftcol = old_leftcol;
2037 curwin->w_topline = old_topline;
2038# ifdef FEAT_DIFF
2039 curwin->w_topfill = old_topfill;
2040# endif
2041 curwin->w_botline = old_botline;
2042 highlight_match = FALSE;
2043 validate_cursor(); /* needed for TAB */
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002044 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 }
2046#endif
2047
2048 if (ccline.cmdbuff != NULL)
2049 {
2050 /*
2051 * Put line in history buffer (":" and "=" only when it was typed).
2052 */
2053#ifdef FEAT_CMDHIST
2054 if (ccline.cmdlen && firstc != NUL
2055 && (some_key_typed || histype == HIST_SEARCH))
2056 {
2057 add_to_history(histype, ccline.cmdbuff, TRUE,
2058 histype == HIST_SEARCH ? firstc : NUL);
2059 if (firstc == ':')
2060 {
2061 vim_free(new_last_cmdline);
2062 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2063 }
2064 }
2065#endif
2066
2067 if (gotesc) /* abandon command line */
2068 {
2069 vim_free(ccline.cmdbuff);
2070 ccline.cmdbuff = NULL;
2071 if (msg_scrolled == 0)
2072 compute_cmdrow();
2073 MSG("");
2074 redraw_cmdline = TRUE;
2075 }
2076 }
2077
2078 /*
2079 * If the screen was shifted up, redraw the whole screen (later).
2080 * If the line is too long, clear it, so ruler and shown command do
2081 * not get printed in the middle of it.
2082 */
2083 msg_check();
2084 msg_scroll = save_msg_scroll;
2085 redir_off = FALSE;
2086
2087 /* When the command line was typed, no need for a wait-return prompt. */
2088 if (some_key_typed)
2089 need_wait_return = FALSE;
2090
2091 State = save_State;
2092#ifdef USE_IM_CONTROL
2093 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2094 im_save_status(b_im_ptr);
2095 im_set_active(FALSE);
2096#endif
2097#ifdef FEAT_MOUSE
2098 setmouse();
2099#endif
2100#ifdef CURSOR_SHAPE
2101 ui_cursor_shape(); /* may show different cursor shape */
2102#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002103 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002105 {
2106 char_u *p = ccline.cmdbuff;
2107
2108 /* Make ccline empty, getcmdline() may try to use it. */
2109 ccline.cmdbuff = NULL;
2110 return p;
2111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112}
2113
2114#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2115/*
2116 * Get a command line with a prompt.
2117 * This is prepared to be called recursively from getcmdline() (e.g. by
2118 * f_input() when evaluating an expression from CTRL-R =).
2119 * Returns the command line in allocated memory, or NULL.
2120 */
2121 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002122getcmdline_prompt(
2123 int firstc,
2124 char_u *prompt, /* command line prompt */
2125 int attr, /* attributes for prompt */
2126 int xp_context, /* type of expansion */
2127 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128{
2129 char_u *s;
2130 struct cmdline_info save_ccline;
2131 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002132 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002134 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 ccline.cmdprompt = prompt;
2136 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002137# ifdef FEAT_EVAL
2138 ccline.xp_context = xp_context;
2139 ccline.xp_arg = xp_arg;
2140 ccline.input_fn = (firstc == '@');
2141# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002142 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002144 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002145 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002146 /* Restore msg_col, the prompt from input() may have changed it.
2147 * But only if called recursively and the commandline is therefore being
2148 * restored to an old one; if not, the input() prompt stays on the screen,
2149 * so we need its modified msg_col left intact. */
2150 if (ccline.cmdbuff != NULL)
2151 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152
2153 return s;
2154}
2155#endif
2156
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002157/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002158 * Return TRUE when the text must not be changed and we can't switch to
2159 * another window or buffer. Used when editing the command line, evaluating
2160 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002161 */
2162 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002163text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002164{
2165#ifdef FEAT_CMDWIN
2166 if (cmdwin_type != 0)
2167 return TRUE;
2168#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002169 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002170}
2171
2172/*
2173 * Give an error message for a command that isn't allowed while the cmdline
2174 * window is open or editing the cmdline in another way.
2175 */
2176 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002177text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002178{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002179 EMSG(_(get_text_locked_msg()));
2180}
2181
2182 char_u *
2183get_text_locked_msg(void)
2184{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002185#ifdef FEAT_CMDWIN
2186 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002187 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002188#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002189 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002190}
2191
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002192#if defined(FEAT_AUTOCMD) || defined(PROTO)
2193/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002194 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2195 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002196 */
2197 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002198curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002199{
2200 if (curbuf_lock > 0)
2201 {
2202 EMSG(_("E788: Not allowed to edit another buffer now"));
2203 return TRUE;
2204 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002205 return allbuf_locked();
2206}
2207
2208/*
2209 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2210 * message.
2211 */
2212 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002213allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002214{
2215 if (allbuf_lock > 0)
2216 {
2217 EMSG(_("E811: Not allowed to change buffer information now"));
2218 return TRUE;
2219 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002220 return FALSE;
2221}
2222#endif
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002225cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226{
2227#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2228 if (cmdline_star > 0) /* showing '*', always 1 position */
2229 return 1;
2230#endif
2231 return ptr2cells(ccline.cmdbuff + idx);
2232}
2233
2234/*
2235 * Compute the offset of the cursor on the command line for the prompt and
2236 * indent.
2237 */
2238 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002239set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002241 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 ccline.cmdspos = 1 + ccline.cmdindent;
2243 else
2244 ccline.cmdspos = 0 + ccline.cmdindent;
2245}
2246
2247/*
2248 * Compute the screen position for the cursor on the command line.
2249 */
2250 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002251set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252{
2253 int i, m, c;
2254
2255 set_cmdspos();
2256 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002257 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002259 if (m < 0) /* overflow, Columns or Rows at weird value */
2260 m = MAXCOL;
2261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 else
2263 m = MAXCOL;
2264 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2265 {
2266 c = cmdline_charsize(i);
2267#ifdef FEAT_MBYTE
2268 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2269 if (has_mbyte)
2270 correct_cmdspos(i, c);
2271#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002272 /* If the cmdline doesn't fit, show cursor on last visible char.
2273 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 if ((ccline.cmdspos += c) >= m)
2275 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 ccline.cmdspos -= c;
2277 break;
2278 }
2279#ifdef FEAT_MBYTE
2280 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002281 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#endif
2283 }
2284}
2285
2286#ifdef FEAT_MBYTE
2287/*
2288 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2289 * character that doesn't fit, so that a ">" must be displayed.
2290 */
2291 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002292correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002294 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2296 && ccline.cmdspos % Columns + cells > Columns)
2297 ccline.cmdspos++;
2298}
2299#endif
2300
2301/*
2302 * Get an Ex command line for the ":" command.
2303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002305getexline(
2306 int c, /* normally ':', NUL for ":append" */
2307 void *cookie UNUSED,
2308 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 /* When executing a register, remove ':' that's in front of each line. */
2311 if (exec_from_reg && vpeekc() == ':')
2312 (void)vgetc();
2313 return getcmdline(c, 1L, indent);
2314}
2315
2316/*
2317 * Get an Ex command line for Ex mode.
2318 * In Ex mode we only use the OS supplied line editing features and no
2319 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002320 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002323getexmodeline(
2324 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002325 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002326 void *cookie UNUSED,
2327 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002329 garray_T line_ga;
2330 char_u *pend;
2331 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002332 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002333 int escaped = FALSE; /* CTRL-V typed */
2334 int vcol = 0;
2335 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002336 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002337 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338
2339 /* Switch cursor on now. This avoids that it happens after the "\n", which
2340 * confuses the system function that computes tabstops. */
2341 cursor_on();
2342
2343 /* always start in column 0; write a newline if necessary */
2344 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002345 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002347 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002349 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002350 if (p_prompt)
2351 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 while (indent-- > 0)
2353 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356
2357 ga_init2(&line_ga, 1, 30);
2358
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002359 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002360 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002361 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002362 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002363 while (indent >= 8)
2364 {
2365 ga_append(&line_ga, TAB);
2366 msg_puts((char_u *)" ");
2367 indent -= 8;
2368 }
2369 while (indent-- > 0)
2370 {
2371 ga_append(&line_ga, ' ');
2372 msg_putchar(' ');
2373 }
2374 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002375 ++no_mapping;
2376 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002377
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 /*
2379 * Get the line, one character at a time.
2380 */
2381 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002382 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002384 long sw;
2385 char_u *s;
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 if (ga_grow(&line_ga, 40) == FAIL)
2388 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
Bram Moolenaarba748c82017-02-26 14:00:07 +01002390 /*
2391 * Get one character at a time.
2392 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002393 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002394
2395 /* Check for a ":normal" command and no more characters left. */
2396 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2397 c1 = '\n';
2398 else
2399 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
2401 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 * Handle line editing.
2403 * Previously this was left to the system, putting the terminal in
2404 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002406 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002408 msg_putchar('\n');
2409 break;
2410 }
2411
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002412 if (c1 == K_PS)
2413 {
2414 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2415 goto redraw;
2416 }
2417
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002418 if (!escaped)
2419 {
2420 /* CR typed means "enter", which is NL */
2421 if (c1 == '\r')
2422 c1 = '\n';
2423
2424 if (c1 == BS || c1 == K_BS
2425 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002427 if (line_ga.ga_len > 0)
2428 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002429#ifdef FEAT_MBYTE
2430 if (has_mbyte)
2431 {
2432 p = (char_u *)line_ga.ga_data;
2433 p[line_ga.ga_len] = NUL;
2434 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2435 line_ga.ga_len -= len;
2436 }
2437 else
2438#endif
2439 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002440 goto redraw;
2441 }
2442 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 }
2444
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002445 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002447 msg_col = startcol;
2448 msg_clr_eos();
2449 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002450 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002451 }
2452
2453 if (c1 == Ctrl_T)
2454 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002455 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002456 p = (char_u *)line_ga.ga_data;
2457 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002458 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002459 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002460add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002461 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002463 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002464 p = (char_u *)line_ga.ga_data;
2465 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002466 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2467 *s = ' ';
2468 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002470redraw:
2471 /* redraw the line */
2472 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002473 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002474 p = (char_u *)line_ga.ga_data;
2475 p[line_ga.ga_len] = NUL;
2476 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002478 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002480 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002482 msg_putchar(' ');
2483 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002484 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002486 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002488 len = MB_PTR2LEN(p);
2489 msg_outtrans_len(p, len);
2490 vcol += ptr2cells(p);
2491 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 }
2493 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002494 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002495 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002496 continue;
2497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002499 if (c1 == Ctrl_D)
2500 {
2501 /* Delete one shiftwidth. */
2502 p = (char_u *)line_ga.ga_data;
2503 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002505 if (prev_char == '^')
2506 ex_keep_indent = TRUE;
2507 indent = 0;
2508 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
2510 else
2511 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002512 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002513 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002514 if (indent > 0)
2515 {
2516 --indent;
2517 indent -= indent % get_sw_value(curbuf);
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002520 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002521 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002522 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002523 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2524 --line_ga.ga_len;
2525 }
2526 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002528
2529 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2530 {
2531 escaped = TRUE;
2532 continue;
2533 }
2534
2535 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2536 if (IS_SPECIAL(c1))
2537 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002539
2540 if (IS_SPECIAL(c1))
2541 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002542#ifdef FEAT_MBYTE
2543 if (has_mbyte)
2544 len = (*mb_char2bytes)(c1,
2545 (char_u *)line_ga.ga_data + line_ga.ga_len);
2546 else
2547#endif
2548 {
2549 len = 1;
2550 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2551 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002552 if (c1 == '\n')
2553 msg_putchar('\n');
2554 else if (c1 == TAB)
2555 {
2556 /* Don't use chartabsize(), 'ts' can be different */
2557 do
2558 {
2559 msg_putchar(' ');
2560 } while (++vcol % 8);
2561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002564 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002565 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002566 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002568 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002569 escaped = FALSE;
2570
2571 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002572 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002574 /* We are done when a NL is entered, but not when it comes after an
2575 * odd number of backslashes, that results in a NUL. */
2576 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002578 int bcount = 0;
2579
2580 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2581 ++bcount;
2582
2583 if (bcount > 0)
2584 {
2585 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2586 * "\NL", etc. */
2587 line_ga.ga_len -= (bcount + 1) / 2;
2588 pend -= (bcount + 1) / 2;
2589 pend[-1] = '\n';
2590 }
2591
2592 if ((bcount & 1) == 0)
2593 {
2594 --line_ga.ga_len;
2595 --pend;
2596 *pend = NUL;
2597 break;
2598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600 }
2601
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002602 --no_mapping;
2603 --allow_keys;
2604
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 /* make following messages go to the next line */
2606 msg_didout = FALSE;
2607 msg_col = 0;
2608 if (msg_row < Rows - 1)
2609 ++msg_row;
2610 emsg_on_display = FALSE; /* don't want ui_delay() */
2611
2612 if (got_int)
2613 ga_clear(&line_ga);
2614
2615 return (char_u *)line_ga.ga_data;
2616}
2617
Bram Moolenaare344bea2005-09-01 20:46:49 +00002618# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2619 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620/*
2621 * Return TRUE if ccline.overstrike is on.
2622 */
2623 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002624cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 return ccline.overstrike;
2627}
2628
2629/*
2630 * Return TRUE if the cursor is at the end of the cmdline.
2631 */
2632 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002633cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635 return (ccline.cmdpos >= ccline.cmdlen);
2636}
2637#endif
2638
Bram Moolenaar9372a112005-12-06 19:59:18 +00002639#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640/*
2641 * Return the virtual column number at the current cursor position.
2642 * This is used by the IM code to obtain the start of the preedit string.
2643 */
2644 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002645cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2648 return MAXCOL;
2649
2650# ifdef FEAT_MBYTE
2651 if (has_mbyte)
2652 {
2653 colnr_T col;
2654 int i = 0;
2655
2656 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002657 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658
2659 return col;
2660 }
2661 else
2662# endif
2663 return ccline.cmdpos;
2664}
2665#endif
2666
2667#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2668/*
2669 * If part of the command line is an IM preedit string, redraw it with
2670 * IM feedback attributes. The cursor position is restored after drawing.
2671 */
2672 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002673redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674{
2675 if ((State & CMDLINE)
2676 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002677 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 && !p_imdisable
2679 && im_is_preediting())
2680 {
2681 int cmdpos = 0;
2682 int cmdspos;
2683 int old_row;
2684 int old_col;
2685 colnr_T col;
2686
2687 old_row = msg_row;
2688 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002689 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691# ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 {
2694 for (col = 0; col < preedit_start_col
2695 && cmdpos < ccline.cmdlen; ++col)
2696 {
2697 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002698 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 }
2700 }
2701 else
2702# endif
2703 {
2704 cmdspos += preedit_start_col;
2705 cmdpos += preedit_start_col;
2706 }
2707
2708 msg_row = cmdline_row + (cmdspos / (int)Columns);
2709 msg_col = cmdspos % (int)Columns;
2710 if (msg_row >= Rows)
2711 msg_row = Rows - 1;
2712
2713 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2714 {
2715 int char_len;
2716 int char_attr;
2717
2718 char_attr = im_get_feedback_attr(col);
2719 if (char_attr < 0)
2720 break; /* end of preedit string */
2721
2722# ifdef FEAT_MBYTE
2723 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002724 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 else
2726# endif
2727 char_len = 1;
2728
2729 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2730 cmdpos += char_len;
2731 }
2732
2733 msg_row = old_row;
2734 msg_col = old_col;
2735 }
2736}
2737#endif /* FEAT_XIM && FEAT_GUI_GTK */
2738
2739/*
2740 * Allocate a new command line buffer.
2741 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2742 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2743 */
2744 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002745alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
2747 /*
2748 * give some extra space to avoid having to allocate all the time
2749 */
2750 if (len < 80)
2751 len = 100;
2752 else
2753 len += 20;
2754
2755 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2756 ccline.cmdbufflen = len;
2757}
2758
2759/*
2760 * Re-allocate the command line to length len + something extra.
2761 * return FAIL for failure, OK otherwise
2762 */
2763 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002764realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765{
2766 char_u *p;
2767
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002768 if (len < ccline.cmdbufflen)
2769 return OK; /* no need to resize */
2770
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 p = ccline.cmdbuff;
2772 alloc_cmdbuff(len); /* will get some more */
2773 if (ccline.cmdbuff == NULL) /* out of memory */
2774 {
2775 ccline.cmdbuff = p; /* keep the old one */
2776 return FAIL;
2777 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002778 /* There isn't always a NUL after the command, but it may need to be
2779 * there, thus copy up to the NUL and add a NUL. */
2780 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2781 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002783
2784 if (ccline.xpc != NULL
2785 && ccline.xpc->xp_pattern != NULL
2786 && ccline.xpc->xp_context != EXPAND_NOTHING
2787 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2788 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002789 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002790
2791 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2792 * to point into the newly allocated memory. */
2793 if (i >= 0 && i <= ccline.cmdlen)
2794 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2795 }
2796
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 return OK;
2798}
2799
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002800#if defined(FEAT_ARABIC) || defined(PROTO)
2801static char_u *arshape_buf = NULL;
2802
2803# if defined(EXITFREE) || defined(PROTO)
2804 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002805free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002806{
2807 vim_free(arshape_buf);
2808}
2809# endif
2810#endif
2811
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812/*
2813 * Draw part of the cmdline at the current cursor position. But draw stars
2814 * when cmdline_star is TRUE.
2815 */
2816 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002817draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818{
2819#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2820 int i;
2821
2822 if (cmdline_star > 0)
2823 for (i = 0; i < len; ++i)
2824 {
2825 msg_putchar('*');
2826# ifdef FEAT_MBYTE
2827 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002828 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829# endif
2830 }
2831 else
2832#endif
2833#ifdef FEAT_ARABIC
2834 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2835 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 static int buflen = 0;
2837 char_u *p;
2838 int j;
2839 int newlen = 0;
2840 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002841 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 int prev_c = 0;
2843 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002844 int u8c;
2845 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
2848 /*
2849 * Do arabic shaping into a temporary buffer. This is very
2850 * inefficient!
2851 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002852 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
2854 /* Re-allocate the buffer. We keep it around to avoid a lot of
2855 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002856 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002857 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002858 arshape_buf = alloc(buflen);
2859 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 return; /* out of memory */
2861 }
2862
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002863 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2864 {
2865 /* Prepend a space to draw the leading composing char on. */
2866 arshape_buf[0] = ' ';
2867 newlen = 1;
2868 }
2869
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 for (j = start; j < start + len; j += mb_l)
2871 {
2872 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002873 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002874 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 if (ARABIC_CHAR(u8c))
2876 {
2877 /* Do Arabic shaping. */
2878 if (cmdmsg_rl)
2879 {
2880 /* displaying from right to left */
2881 pc = prev_c;
2882 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002883 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (j + mb_l >= start + len)
2885 nc = NUL;
2886 else
2887 nc = utf_ptr2char(p + mb_l);
2888 }
2889 else
2890 {
2891 /* displaying from left to right */
2892 if (j + mb_l >= start + len)
2893 pc = NUL;
2894 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002895 {
2896 int pcc[MAX_MCO];
2897
2898 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002900 pc1 = pcc[0];
2901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 nc = prev_c;
2903 }
2904 prev_c = u8c;
2905
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002906 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002908 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002909 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002911 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2912 if (u8cc[1] != 0)
2913 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002914 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
2916 }
2917 else
2918 {
2919 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002920 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 newlen += mb_l;
2922 }
2923 }
2924
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002925 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 }
2927 else
2928#endif
2929 msg_outtrans_len(ccline.cmdbuff + start, len);
2930}
2931
2932/*
2933 * Put a character on the command line. Shifts the following text to the
2934 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
2935 * "c" must be printable (fit in one display cell)!
2936 */
2937 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002938putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 if (cmd_silent)
2941 return;
2942 msg_no_more = TRUE;
2943 msg_putchar(c);
2944 if (shift)
2945 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2946 msg_no_more = FALSE;
2947 cursorcmd();
2948}
2949
2950/*
2951 * Undo a putcmdline(c, FALSE).
2952 */
2953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002954unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 if (cmd_silent)
2957 return;
2958 msg_no_more = TRUE;
2959 if (ccline.cmdlen == ccline.cmdpos)
2960 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02002961#ifdef FEAT_MBYTE
2962 else if (has_mbyte)
2963 draw_cmdline(ccline.cmdpos,
2964 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
2965#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else
2967 draw_cmdline(ccline.cmdpos, 1);
2968 msg_no_more = FALSE;
2969 cursorcmd();
2970}
2971
2972/*
2973 * Put the given string, of the given length, onto the command line.
2974 * If len is -1, then STRLEN() is used to calculate the length.
2975 * If 'redraw' is TRUE then the new part of the command line, and the remaining
2976 * part will be redrawn, otherwise it will not. If this function is called
2977 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
2978 * called afterwards.
2979 */
2980 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002981put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 int retval;
2984 int i;
2985 int m;
2986 int c;
2987
2988 if (len < 0)
2989 len = (int)STRLEN(str);
2990
2991 /* Check if ccline.cmdbuff needs to be longer */
2992 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002993 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 else
2995 retval = OK;
2996 if (retval == OK)
2997 {
2998 if (!ccline.overstrike)
2999 {
3000 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3001 ccline.cmdbuff + ccline.cmdpos,
3002 (size_t)(ccline.cmdlen - ccline.cmdpos));
3003 ccline.cmdlen += len;
3004 }
3005 else
3006 {
3007#ifdef FEAT_MBYTE
3008 if (has_mbyte)
3009 {
3010 /* Count nr of characters in the new string. */
3011 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003012 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 ++m;
3014 /* Count nr of bytes in cmdline that are overwritten by these
3015 * characters. */
3016 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003017 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 --m;
3019 if (i < ccline.cmdlen)
3020 {
3021 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3022 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3023 ccline.cmdlen += ccline.cmdpos + len - i;
3024 }
3025 else
3026 ccline.cmdlen = ccline.cmdpos + len;
3027 }
3028 else
3029#endif
3030 if (ccline.cmdpos + len > ccline.cmdlen)
3031 ccline.cmdlen = ccline.cmdpos + len;
3032 }
3033 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3034 ccline.cmdbuff[ccline.cmdlen] = NUL;
3035
3036#ifdef FEAT_MBYTE
3037 if (enc_utf8)
3038 {
3039 /* When the inserted text starts with a composing character,
3040 * backup to the character before it. There could be two of them.
3041 */
3042 i = 0;
3043 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3044 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3045 {
3046 i = (*mb_head_off)(ccline.cmdbuff,
3047 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3048 ccline.cmdpos -= i;
3049 len += i;
3050 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3051 }
3052# ifdef FEAT_ARABIC
3053 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3054 {
3055 /* Check the previous character for Arabic combining pair. */
3056 i = (*mb_head_off)(ccline.cmdbuff,
3057 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3058 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3059 + ccline.cmdpos - i), c))
3060 {
3061 ccline.cmdpos -= i;
3062 len += i;
3063 }
3064 else
3065 i = 0;
3066 }
3067# endif
3068 if (i != 0)
3069 {
3070 /* Also backup the cursor position. */
3071 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3072 ccline.cmdspos -= i;
3073 msg_col -= i;
3074 if (msg_col < 0)
3075 {
3076 msg_col += Columns;
3077 --msg_row;
3078 }
3079 }
3080 }
3081#endif
3082
3083 if (redraw && !cmd_silent)
3084 {
3085 msg_no_more = TRUE;
3086 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003087 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3089 /* Avoid clearing the rest of the line too often. */
3090 if (cmdline_row != i || ccline.overstrike)
3091 msg_clr_eos();
3092 msg_no_more = FALSE;
3093 }
3094#ifdef FEAT_FKMAP
3095 /*
3096 * If we are in Farsi command mode, the character input must be in
3097 * Insert mode. So do not advance the cmdpos.
3098 */
3099 if (!cmd_fkmap)
3100#endif
3101 {
3102 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003103 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003105 if (m < 0) /* overflow, Columns or Rows at weird value */
3106 m = MAXCOL;
3107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else
3109 m = MAXCOL;
3110 for (i = 0; i < len; ++i)
3111 {
3112 c = cmdline_charsize(ccline.cmdpos);
3113#ifdef FEAT_MBYTE
3114 /* count ">" for a double-wide char that doesn't fit. */
3115 if (has_mbyte)
3116 correct_cmdspos(ccline.cmdpos, c);
3117#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003118 /* Stop cursor at the end of the screen, but do increment the
3119 * insert position, so that entering a very long command
3120 * works, even though you can't see it. */
3121 if (ccline.cmdspos + c < m)
3122 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123#ifdef FEAT_MBYTE
3124 if (has_mbyte)
3125 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003126 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 if (c > len - i - 1)
3128 c = len - i - 1;
3129 ccline.cmdpos += c;
3130 i += c;
3131 }
3132#endif
3133 ++ccline.cmdpos;
3134 }
3135 }
3136 }
3137 if (redraw)
3138 msg_check();
3139 return retval;
3140}
3141
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003142static struct cmdline_info prev_ccline;
3143static int prev_ccline_used = FALSE;
3144
3145/*
3146 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3147 * and overwrite it. But get_cmdline_str() may need it, thus make it
3148 * available globally in prev_ccline.
3149 */
3150 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003152{
3153 if (!prev_ccline_used)
3154 {
3155 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3156 prev_ccline_used = TRUE;
3157 }
3158 *ccp = prev_ccline;
3159 prev_ccline = ccline;
3160 ccline.cmdbuff = NULL;
3161 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003162 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003163}
3164
3165/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003166 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003167 */
3168 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003169restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003170{
3171 ccline = prev_ccline;
3172 prev_ccline = *ccp;
3173}
3174
Bram Moolenaar5a305422006-04-28 22:38:25 +00003175#if defined(FEAT_EVAL) || defined(PROTO)
3176/*
3177 * Save the command line into allocated memory. Returns a pointer to be
3178 * passed to restore_cmdline_alloc() later.
3179 * Returns NULL when failed.
3180 */
3181 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003182save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003183{
3184 struct cmdline_info *p;
3185
3186 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3187 if (p != NULL)
3188 save_cmdline(p);
3189 return (char_u *)p;
3190}
3191
3192/*
3193 * Restore the command line from the return value of save_cmdline_alloc().
3194 */
3195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003196restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003197{
3198 if (p != NULL)
3199 {
3200 restore_cmdline((struct cmdline_info *)p);
3201 vim_free(p);
3202 }
3203}
3204#endif
3205
Bram Moolenaar8299df92004-07-10 09:47:34 +00003206/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003207 * Paste a yank register into the command line.
3208 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003209 * insert_reg() can't be used here, because special characters from the
3210 * register contents will be interpreted as commands.
3211 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003212 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003213 */
3214 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003215cmdline_paste(
3216 int regname,
3217 int literally, /* Insert text literally instead of "as typed" */
3218 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003219{
3220 long i;
3221 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003222 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003223 int allocated;
3224 struct cmdline_info save_ccline;
3225
3226 /* check for valid regname; also accept special characters for CTRL-R in
3227 * the command line */
3228 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3229 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3230 return FAIL;
3231
3232 /* A register containing CTRL-R can cause an endless loop. Allow using
3233 * CTRL-C to break the loop. */
3234 line_breakcheck();
3235 if (got_int)
3236 return FAIL;
3237
3238#ifdef FEAT_CLIPBOARD
3239 regname = may_get_selection(regname);
3240#endif
3241
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003242 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003243 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003244 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003245 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003246 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003247 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003248 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003249
3250 if (i)
3251 {
3252 /* Got the value of a special register in "arg". */
3253 if (arg == NULL)
3254 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003255
3256 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3257 * part of the word. */
3258 p = arg;
3259 if (p_is && regname == Ctrl_W)
3260 {
3261 char_u *w;
3262 int len;
3263
3264 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003265 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003266 {
3267#ifdef FEAT_MBYTE
3268 if (has_mbyte)
3269 {
3270 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3271 if (!vim_iswordc(mb_ptr2char(w - len)))
3272 break;
3273 w -= len;
3274 }
3275 else
3276#endif
3277 {
3278 if (!vim_iswordc(w[-1]))
3279 break;
3280 --w;
3281 }
3282 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003283 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003284 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3285 p += len;
3286 }
3287
3288 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003289 if (allocated)
3290 vim_free(arg);
3291 return OK;
3292 }
3293
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003294 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003295}
3296
3297/*
3298 * Put a string on the command line.
3299 * When "literally" is TRUE, insert literally.
3300 * When "literally" is FALSE, insert as typed, but don't leave the command
3301 * line.
3302 */
3303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003304cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003305{
3306 int c, cv;
3307
3308 if (literally)
3309 put_on_cmdline(s, -1, TRUE);
3310 else
3311 while (*s != NUL)
3312 {
3313 cv = *s;
3314 if (cv == Ctrl_V && s[1])
3315 ++s;
3316#ifdef FEAT_MBYTE
3317 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003318 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003319 else
3320#endif
3321 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003322 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3323 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003324#ifdef UNIX
3325 || c == intr_char
3326#endif
3327 || (c == Ctrl_BSL && *s == Ctrl_N))
3328 stuffcharReadbuff(Ctrl_V);
3329 stuffcharReadbuff(c);
3330 }
3331}
3332
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333#ifdef FEAT_WILDMENU
3334/*
3335 * Delete characters on the command line, from "from" to the current
3336 * position.
3337 */
3338 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003339cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340{
3341 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3342 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3343 ccline.cmdlen -= ccline.cmdpos - from;
3344 ccline.cmdpos = from;
3345}
3346#endif
3347
3348/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003349 * This function is called when the screen size changes and with incremental
3350 * search and in other situations where the command line may have been
3351 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 */
3353 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003354redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003356 redrawcmdline_ex(TRUE);
3357}
3358
3359 void
3360redrawcmdline_ex(int do_compute_cmdrow)
3361{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (cmd_silent)
3363 return;
3364 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003365 if (do_compute_cmdrow)
3366 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 redrawcmd();
3368 cursorcmd();
3369}
3370
3371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003372redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 int i;
3375
3376 if (cmd_silent)
3377 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003378 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 msg_putchar(ccline.cmdfirstc);
3380 if (ccline.cmdprompt != NULL)
3381 {
3382 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3383 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3384 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003385 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 --ccline.cmdindent;
3387 }
3388 else
3389 for (i = ccline.cmdindent; i > 0; --i)
3390 msg_putchar(' ');
3391}
3392
3393/*
3394 * Redraw what is currently on the command line.
3395 */
3396 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003397redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
3399 if (cmd_silent)
3400 return;
3401
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003402 /* when 'incsearch' is set there may be no command line while redrawing */
3403 if (ccline.cmdbuff == NULL)
3404 {
3405 windgoto(cmdline_row, 0);
3406 msg_clr_eos();
3407 return;
3408 }
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 msg_start();
3411 redrawcmdprompt();
3412
3413 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3414 msg_no_more = TRUE;
3415 draw_cmdline(0, ccline.cmdlen);
3416 msg_clr_eos();
3417 msg_no_more = FALSE;
3418
3419 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003420 if (extra_char != NUL)
3421 putcmdline(extra_char, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 /*
3424 * An emsg() before may have set msg_scroll. This is used in normal mode,
3425 * in cmdline mode we can reset them now.
3426 */
3427 msg_scroll = FALSE; /* next message overwrites cmdline */
3428
3429 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3430 * in cmdline mode */
3431 skip_redraw = FALSE;
3432}
3433
3434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003435compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003437 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 cmdline_row = Rows - 1;
3439 else
3440 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
3441 + W_STATUS_HEIGHT(lastwin);
3442}
3443
3444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003445cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 if (cmd_silent)
3448 return;
3449
3450#ifdef FEAT_RIGHTLEFT
3451 if (cmdmsg_rl)
3452 {
3453 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3454 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3455 if (msg_row <= 0)
3456 msg_row = Rows - 1;
3457 }
3458 else
3459#endif
3460 {
3461 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3462 msg_col = ccline.cmdspos % (int)Columns;
3463 if (msg_row >= Rows)
3464 msg_row = Rows - 1;
3465 }
3466
3467 windgoto(msg_row, msg_col);
3468#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3469 redrawcmd_preedit();
3470#endif
3471#ifdef MCH_CURSOR_SHAPE
3472 mch_update_cursor();
3473#endif
3474}
3475
3476 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003477gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 msg_start();
3480#ifdef FEAT_RIGHTLEFT
3481 if (cmdmsg_rl)
3482 msg_col = Columns - 1;
3483 else
3484#endif
3485 msg_col = 0; /* always start in column 0 */
3486 if (clr) /* clear the bottom line(s) */
3487 msg_clr_eos(); /* will reset clear_cmdline */
3488 windgoto(cmdline_row, 0);
3489}
3490
3491/*
3492 * Check the word in front of the cursor for an abbreviation.
3493 * Called when the non-id character "c" has been entered.
3494 * When an abbreviation is recognized it is removed from the text with
3495 * backspaces and the replacement string is inserted, followed by "c".
3496 */
3497 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003498ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499{
3500 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3501 return FALSE;
3502
3503 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3504}
3505
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003506#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3507 static int
3508#ifdef __BORLANDC__
3509_RTLENTRYF
3510#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003511sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003512{
3513 char_u *p1 = *(char_u **)s1;
3514 char_u *p2 = *(char_u **)s2;
3515
3516 if (*p1 != '<' && *p2 == '<') return -1;
3517 if (*p1 == '<' && *p2 != '<') return 1;
3518 return STRCMP(p1, p2);
3519}
3520#endif
3521
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522/*
3523 * Return FAIL if this is not an appropriate context in which to do
3524 * completion of anything, return OK if it is (even if there are no matches).
3525 * For the caller, this means that the character is just passed through like a
3526 * normal character (instead of being expanded). This allows :s/^I^D etc.
3527 */
3528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003529nextwild(
3530 expand_T *xp,
3531 int type,
3532 int options, /* extra options for ExpandOne() */
3533 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003534{
3535 int i, j;
3536 char_u *p1;
3537 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 int difflen;
3539 int v;
3540
3541 if (xp->xp_numfiles == -1)
3542 {
3543 set_expand_context(xp);
3544 cmd_showtail = expand_showtail(xp);
3545 }
3546
3547 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3548 {
3549 beep_flush();
3550 return OK; /* Something illegal on command line */
3551 }
3552 if (xp->xp_context == EXPAND_NOTHING)
3553 {
3554 /* Caller can use the character as a normal char instead */
3555 return FAIL;
3556 }
3557
3558 MSG_PUTS("..."); /* show that we are busy */
3559 out_flush();
3560
3561 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003562 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563
3564 if (type == WILD_NEXT || type == WILD_PREV)
3565 {
3566 /*
3567 * Get next/previous match for a previous expanded pattern.
3568 */
3569 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3570 }
3571 else
3572 {
3573 /*
3574 * Translate string into pattern and expand it.
3575 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003576 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3577 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 p2 = NULL;
3579 else
3580 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003581 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003582 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3583 if (escape)
3584 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003585
3586 if (p_wic)
3587 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003588 p2 = ExpandOne(xp, p1,
3589 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003590 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003592 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 if (p2 != NULL && type == WILD_LONGEST)
3594 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003595 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 if (ccline.cmdbuff[i + j] == '*'
3597 || ccline.cmdbuff[i + j] == '?')
3598 break;
3599 if ((int)STRLEN(p2) < j)
3600 {
3601 vim_free(p2);
3602 p2 = NULL;
3603 }
3604 }
3605 }
3606 }
3607
3608 if (p2 != NULL && !got_int)
3609 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003610 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003611 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003613 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 xp->xp_pattern = ccline.cmdbuff + i;
3615 }
3616 else
3617 v = OK;
3618 if (v == OK)
3619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003620 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3621 &ccline.cmdbuff[ccline.cmdpos],
3622 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3623 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 ccline.cmdlen += difflen;
3625 ccline.cmdpos += difflen;
3626 }
3627 }
3628 vim_free(p2);
3629
3630 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003631 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632
3633 /* When expanding a ":map" command and no matches are found, assume that
3634 * the key is supposed to be inserted literally */
3635 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3636 return FAIL;
3637
3638 if (xp->xp_numfiles <= 0 && p2 == NULL)
3639 beep_flush();
3640 else if (xp->xp_numfiles == 1)
3641 /* free expanded pattern */
3642 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3643
3644 return OK;
3645}
3646
3647/*
3648 * Do wildcard expansion on the string 'str'.
3649 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003650 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 * Return NULL for failure.
3652 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003653 * "orig" is the originally expanded string, copied to allocated memory. It
3654 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3655 * WILD_PREV "orig" should be NULL.
3656 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003657 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3658 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 *
3660 * mode = WILD_FREE: just free previously expanded matches
3661 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3662 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3663 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3664 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3665 * mode = WILD_ALL: return all matches concatenated
3666 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003667 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 *
3669 * options = WILD_LIST_NOTFOUND: list entries without a match
3670 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3671 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3672 * options = WILD_NO_BEEP: Don't beep for multiple matches
3673 * options = WILD_ADD_SLASH: add a slash after directory names
3674 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3675 * options = WILD_SILENT: don't print warning messages
3676 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003677 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 *
3679 * The variables xp->xp_context and xp->xp_backslash must have been set!
3680 */
3681 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003682ExpandOne(
3683 expand_T *xp,
3684 char_u *str,
3685 char_u *orig, /* allocated copy of original of expanded string */
3686 int options,
3687 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
3689 char_u *ss = NULL;
3690 static int findex;
3691 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003692 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 int i;
3694 long_u len;
3695 int non_suf_match; /* number without matching suffix */
3696
3697 /*
3698 * first handle the case of using an old match
3699 */
3700 if (mode == WILD_NEXT || mode == WILD_PREV)
3701 {
3702 if (xp->xp_numfiles > 0)
3703 {
3704 if (mode == WILD_PREV)
3705 {
3706 if (findex == -1)
3707 findex = xp->xp_numfiles;
3708 --findex;
3709 }
3710 else /* mode == WILD_NEXT */
3711 ++findex;
3712
3713 /*
3714 * When wrapping around, return the original string, set findex to
3715 * -1.
3716 */
3717 if (findex < 0)
3718 {
3719 if (orig_save == NULL)
3720 findex = xp->xp_numfiles - 1;
3721 else
3722 findex = -1;
3723 }
3724 if (findex >= xp->xp_numfiles)
3725 {
3726 if (orig_save == NULL)
3727 findex = 0;
3728 else
3729 findex = -1;
3730 }
3731#ifdef FEAT_WILDMENU
3732 if (p_wmnu)
3733 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3734 findex, cmd_showtail);
3735#endif
3736 if (findex == -1)
3737 return vim_strsave(orig_save);
3738 return vim_strsave(xp->xp_files[findex]);
3739 }
3740 else
3741 return NULL;
3742 }
3743
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003744 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3746 {
3747 FreeWild(xp->xp_numfiles, xp->xp_files);
3748 xp->xp_numfiles = -1;
3749 vim_free(orig_save);
3750 orig_save = NULL;
3751 }
3752 findex = 0;
3753
3754 if (mode == WILD_FREE) /* only release file name */
3755 return NULL;
3756
3757 if (xp->xp_numfiles == -1)
3758 {
3759 vim_free(orig_save);
3760 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003761 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
3764 * Do the expansion.
3765 */
3766 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3767 options) == FAIL)
3768 {
3769#ifdef FNAME_ILLEGAL
3770 /* Illegal file name has been silently skipped. But when there
3771 * are wildcards, the real problem is that there was no match,
3772 * causing the pattern to be added, which has illegal characters.
3773 */
3774 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3775 EMSG2(_(e_nomatch2), str);
3776#endif
3777 }
3778 else if (xp->xp_numfiles == 0)
3779 {
3780 if (!(options & WILD_SILENT))
3781 EMSG2(_(e_nomatch2), str);
3782 }
3783 else
3784 {
3785 /* Escape the matches for use on the command line. */
3786 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3787
3788 /*
3789 * Check for matching suffixes in file names.
3790 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003791 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3792 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 {
3794 if (xp->xp_numfiles)
3795 non_suf_match = xp->xp_numfiles;
3796 else
3797 non_suf_match = 1;
3798 if ((xp->xp_context == EXPAND_FILES
3799 || xp->xp_context == EXPAND_DIRECTORIES)
3800 && xp->xp_numfiles > 1)
3801 {
3802 /*
3803 * More than one match; check suffix.
3804 * The files will have been sorted on matching suffix in
3805 * expand_wildcards, only need to check the first two.
3806 */
3807 non_suf_match = 0;
3808 for (i = 0; i < 2; ++i)
3809 if (match_suffix(xp->xp_files[i]))
3810 ++non_suf_match;
3811 }
3812 if (non_suf_match != 1)
3813 {
3814 /* Can we ever get here unless it's while expanding
3815 * interactively? If not, we can get rid of this all
3816 * together. Don't really want to wait for this message
3817 * (and possibly have to hit return to continue!).
3818 */
3819 if (!(options & WILD_SILENT))
3820 EMSG(_(e_toomany));
3821 else if (!(options & WILD_NO_BEEP))
3822 beep_flush();
3823 }
3824 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3825 ss = vim_strsave(xp->xp_files[0]);
3826 }
3827 }
3828 }
3829
3830 /* Find longest common part */
3831 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3832 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003833 int mb_len = 1;
3834 int c0, ci;
3835
3836 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003838#ifdef FEAT_MBYTE
3839 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003841 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3842 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3843 }
3844 else
3845#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003846 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003847 for (i = 1; i < xp->xp_numfiles; ++i)
3848 {
3849#ifdef FEAT_MBYTE
3850 if (has_mbyte)
3851 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3852 else
3853#endif
3854 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003855 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003857 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003858 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003860 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 break;
3862 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003863 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 break;
3865 }
3866 if (i < xp->xp_numfiles)
3867 {
3868 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003869 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 break;
3871 }
3872 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003873
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 ss = alloc((unsigned)len + 1);
3875 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003876 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 findex = -1; /* next p_wc gets first one */
3878 }
3879
3880 /* Concatenate all matching names */
3881 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3882 {
3883 len = 0;
3884 for (i = 0; i < xp->xp_numfiles; ++i)
3885 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3886 ss = lalloc(len, TRUE);
3887 if (ss != NULL)
3888 {
3889 *ss = NUL;
3890 for (i = 0; i < xp->xp_numfiles; ++i)
3891 {
3892 STRCAT(ss, xp->xp_files[i]);
3893 if (i != xp->xp_numfiles - 1)
3894 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3895 }
3896 }
3897 }
3898
3899 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3900 ExpandCleanup(xp);
3901
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003902 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003903 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003904 vim_free(orig);
3905
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 return ss;
3907}
3908
3909/*
3910 * Prepare an expand structure for use.
3911 */
3912 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003913ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003915 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003916 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003918#ifndef BACKSLASH_IN_FILENAME
3919 xp->xp_shell = FALSE;
3920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 xp->xp_numfiles = -1;
3922 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003923#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3924 xp->xp_arg = NULL;
3925#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003926 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927}
3928
3929/*
3930 * Cleanup an expand structure after use.
3931 */
3932 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003933ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934{
3935 if (xp->xp_numfiles >= 0)
3936 {
3937 FreeWild(xp->xp_numfiles, xp->xp_files);
3938 xp->xp_numfiles = -1;
3939 }
3940}
3941
3942 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003943ExpandEscape(
3944 expand_T *xp,
3945 char_u *str,
3946 int numfiles,
3947 char_u **files,
3948 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
3950 int i;
3951 char_u *p;
3952
3953 /*
3954 * May change home directory back to "~"
3955 */
3956 if (options & WILD_HOME_REPLACE)
3957 tilde_replace(str, numfiles, files);
3958
3959 if (options & WILD_ESCAPE)
3960 {
3961 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02003962 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003963 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 || xp->xp_context == EXPAND_BUFFERS
3965 || xp->xp_context == EXPAND_DIRECTORIES)
3966 {
3967 /*
3968 * Insert a backslash into a file name before a space, \, %, #
3969 * and wildmatch characters, except '~'.
3970 */
3971 for (i = 0; i < numfiles; ++i)
3972 {
3973 /* for ":set path=" we need to escape spaces twice */
3974 if (xp->xp_backslash == XP_BS_THREE)
3975 {
3976 p = vim_strsave_escaped(files[i], (char_u *)" ");
3977 if (p != NULL)
3978 {
3979 vim_free(files[i]);
3980 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003981#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 p = vim_strsave_escaped(files[i], (char_u *)" ");
3983 if (p != NULL)
3984 {
3985 vim_free(files[i]);
3986 files[i] = p;
3987 }
3988#endif
3989 }
3990 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003991#ifdef BACKSLASH_IN_FILENAME
3992 p = vim_strsave_fnameescape(files[i], FALSE);
3993#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00003994 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00003995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 if (p != NULL)
3997 {
3998 vim_free(files[i]);
3999 files[i] = p;
4000 }
4001
4002 /* If 'str' starts with "\~", replace "~" at start of
4003 * files[i] with "\~". */
4004 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004005 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004008
4009 /* If the first file starts with a '+' escape it. Otherwise it
4010 * could be seen as "+cmd". */
4011 if (*files[0] == '+')
4012 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 }
4014 else if (xp->xp_context == EXPAND_TAGS)
4015 {
4016 /*
4017 * Insert a backslash before characters in a tag name that
4018 * would terminate the ":tag" command.
4019 */
4020 for (i = 0; i < numfiles; ++i)
4021 {
4022 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4023 if (p != NULL)
4024 {
4025 vim_free(files[i]);
4026 files[i] = p;
4027 }
4028 }
4029 }
4030 }
4031}
4032
4033/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004034 * Escape special characters in "fname" for when used as a file name argument
4035 * after a Vim command, or, when "shell" is non-zero, a shell command.
4036 * Returns the result in allocated memory.
4037 */
4038 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004039vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004040{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004041 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004042#ifdef BACKSLASH_IN_FILENAME
4043 char_u buf[20];
4044 int j = 0;
4045
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004046 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004047 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004048 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004049 buf[j++] = *p;
4050 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004051 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004052#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004053 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4054 if (shell && csh_like_shell() && p != NULL)
4055 {
4056 char_u *s;
4057
4058 /* For csh and similar shells need to put two backslashes before '!'.
4059 * One is taken by Vim, one by the shell. */
4060 s = vim_strsave_escaped(p, (char_u *)"!");
4061 vim_free(p);
4062 p = s;
4063 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004064#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004065
4066 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4067 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004068 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004069 escape_fname(&p);
4070
4071 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004072}
4073
4074/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004075 * Put a backslash before the file name in "pp", which is in allocated memory.
4076 */
4077 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004078escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004079{
4080 char_u *p;
4081
4082 p = alloc((unsigned)(STRLEN(*pp) + 2));
4083 if (p != NULL)
4084 {
4085 p[0] = '\\';
4086 STRCPY(p + 1, *pp);
4087 vim_free(*pp);
4088 *pp = p;
4089 }
4090}
4091
4092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 * For each file name in files[num_files]:
4094 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4095 */
4096 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004097tilde_replace(
4098 char_u *orig_pat,
4099 int num_files,
4100 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 int i;
4103 char_u *p;
4104
4105 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4106 {
4107 for (i = 0; i < num_files; ++i)
4108 {
4109 p = home_replace_save(NULL, files[i]);
4110 if (p != NULL)
4111 {
4112 vim_free(files[i]);
4113 files[i] = p;
4114 }
4115 }
4116 }
4117}
4118
4119/*
4120 * Show all matches for completion on the command line.
4121 * Returns EXPAND_NOTHING when the character that triggered expansion should
4122 * be inserted like a normal character.
4123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004125showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4128 int num_files;
4129 char_u **files_found;
4130 int i, j, k;
4131 int maxlen;
4132 int lines;
4133 int columns;
4134 char_u *p;
4135 int lastlen;
4136 int attr;
4137 int showtail;
4138
4139 if (xp->xp_numfiles == -1)
4140 {
4141 set_expand_context(xp);
4142 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4143 &num_files, &files_found);
4144 showtail = expand_showtail(xp);
4145 if (i != EXPAND_OK)
4146 return i;
4147
4148 }
4149 else
4150 {
4151 num_files = xp->xp_numfiles;
4152 files_found = xp->xp_files;
4153 showtail = cmd_showtail;
4154 }
4155
4156#ifdef FEAT_WILDMENU
4157 if (!wildmenu)
4158 {
4159#endif
4160 msg_didany = FALSE; /* lines_left will be set */
4161 msg_start(); /* prepare for paging */
4162 msg_putchar('\n');
4163 out_flush();
4164 cmdline_row = msg_row;
4165 msg_didany = FALSE; /* lines_left will be set again */
4166 msg_start(); /* prepare for paging */
4167#ifdef FEAT_WILDMENU
4168 }
4169#endif
4170
4171 if (got_int)
4172 got_int = FALSE; /* only int. the completion, not the cmd line */
4173#ifdef FEAT_WILDMENU
4174 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004175 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176#endif
4177 else
4178 {
4179 /* find the length of the longest file name */
4180 maxlen = 0;
4181 for (i = 0; i < num_files; ++i)
4182 {
4183 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004184 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 || xp->xp_context == EXPAND_BUFFERS))
4186 {
4187 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4188 j = vim_strsize(NameBuff);
4189 }
4190 else
4191 j = vim_strsize(L_SHOWFILE(i));
4192 if (j > maxlen)
4193 maxlen = j;
4194 }
4195
4196 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4197 lines = num_files;
4198 else
4199 {
4200 /* compute the number of columns and lines for the listing */
4201 maxlen += 2; /* two spaces between file names */
4202 columns = ((int)Columns + 2) / maxlen;
4203 if (columns < 1)
4204 columns = 1;
4205 lines = (num_files + columns - 1) / columns;
4206 }
4207
Bram Moolenaar8820b482017-03-16 17:23:31 +01004208 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209
4210 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4211 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004212 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 msg_clr_eos();
4214 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004215 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 }
4217
4218 /* list the files line by line */
4219 for (i = 0; i < lines; ++i)
4220 {
4221 lastlen = 999;
4222 for (k = i; k < num_files; k += lines)
4223 {
4224 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4225 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004226 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 p = files_found[k] + STRLEN(files_found[k]) + 1;
4228 msg_advance(maxlen + 1);
4229 msg_puts(p);
4230 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004231 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 break;
4233 }
4234 for (j = maxlen - lastlen; --j >= 0; )
4235 msg_putchar(' ');
4236 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004237 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 || xp->xp_context == EXPAND_BUFFERS)
4239 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004240 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004241 if (xp->xp_numfiles != -1)
4242 {
4243 char_u *halved_slash;
4244 char_u *exp_path;
4245
4246 /* Expansion was done before and special characters
4247 * were escaped, need to halve backslashes. Also
4248 * $HOME has been replaced with ~/. */
4249 exp_path = expand_env_save_opt(files_found[k], TRUE);
4250 halved_slash = backslash_halve_save(
4251 exp_path != NULL ? exp_path : files_found[k]);
4252 j = mch_isdir(halved_slash != NULL ? halved_slash
4253 : files_found[k]);
4254 vim_free(exp_path);
4255 vim_free(halved_slash);
4256 }
4257 else
4258 /* Expansion was done here, file names are literal. */
4259 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (showtail)
4261 p = L_SHOWFILE(k);
4262 else
4263 {
4264 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4265 TRUE);
4266 p = NameBuff;
4267 }
4268 }
4269 else
4270 {
4271 j = FALSE;
4272 p = L_SHOWFILE(k);
4273 }
4274 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4275 }
4276 if (msg_col > 0) /* when not wrapped around */
4277 {
4278 msg_clr_eos();
4279 msg_putchar('\n');
4280 }
4281 out_flush(); /* show one line at a time */
4282 if (got_int)
4283 {
4284 got_int = FALSE;
4285 break;
4286 }
4287 }
4288
4289 /*
4290 * we redraw the command below the lines that we have just listed
4291 * This is a bit tricky, but it saves a lot of screen updating.
4292 */
4293 cmdline_row = msg_row; /* will put it back later */
4294 }
4295
4296 if (xp->xp_numfiles == -1)
4297 FreeWild(num_files, files_found);
4298
4299 return EXPAND_OK;
4300}
4301
4302/*
4303 * Private gettail for showmatches() (and win_redr_status_matches()):
4304 * Find tail of file name path, but ignore trailing "/".
4305 */
4306 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004307sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308{
4309 char_u *p;
4310 char_u *t = s;
4311 int had_sep = FALSE;
4312
4313 for (p = s; *p != NUL; )
4314 {
4315 if (vim_ispathsep(*p)
4316#ifdef BACKSLASH_IN_FILENAME
4317 && !rem_backslash(p)
4318#endif
4319 )
4320 had_sep = TRUE;
4321 else if (had_sep)
4322 {
4323 t = p;
4324 had_sep = FALSE;
4325 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004326 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 return t;
4329}
4330
4331/*
4332 * Return TRUE if we only need to show the tail of completion matches.
4333 * When not completing file names or there is a wildcard in the path FALSE is
4334 * returned.
4335 */
4336 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004337expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338{
4339 char_u *s;
4340 char_u *end;
4341
4342 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004343 if (xp->xp_context != EXPAND_FILES
4344 && xp->xp_context != EXPAND_SHELLCMD
4345 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 return FALSE;
4347
4348 end = gettail(xp->xp_pattern);
4349 if (end == xp->xp_pattern) /* there is no path separator */
4350 return FALSE;
4351
4352 for (s = xp->xp_pattern; s < end; s++)
4353 {
4354 /* Skip escaped wildcards. Only when the backslash is not a path
4355 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4356 if (rem_backslash(s))
4357 ++s;
4358 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4359 return FALSE;
4360 }
4361 return TRUE;
4362}
4363
4364/*
4365 * Prepare a string for expansion.
4366 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004367 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 * When expanding other names: The string will be used with regcomp(). Copy
4369 * the name into allocated memory and prepend "^".
4370 */
4371 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004372addstar(
4373 char_u *fname,
4374 int len,
4375 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376{
4377 char_u *retval;
4378 int i, j;
4379 int new_len;
4380 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004381 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004383 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004384 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004385 && context != EXPAND_SHELLCMD
4386 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
4388 /*
4389 * Matching will be done internally (on something other than files).
4390 * So we convert the file-matching-type wildcards into our kind for
4391 * use with vim_regcomp(). First work out how long it will be:
4392 */
4393
4394 /* For help tags the translation is done in find_help_tags().
4395 * For a tag pattern starting with "/" no translation is needed. */
4396 if (context == EXPAND_HELP
4397 || context == EXPAND_COLORS
4398 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004399 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004400 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004401 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004402 || ((context == EXPAND_TAGS_LISTFILES
4403 || context == EXPAND_TAGS)
4404 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 retval = vim_strnsave(fname, len);
4406 else
4407 {
4408 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4409 for (i = 0; i < len; i++)
4410 {
4411 if (fname[i] == '*' || fname[i] == '~')
4412 new_len++; /* '*' needs to be replaced by ".*"
4413 '~' needs to be replaced by "\~" */
4414
4415 /* Buffer names are like file names. "." should be literal */
4416 if (context == EXPAND_BUFFERS && fname[i] == '.')
4417 new_len++; /* "." becomes "\." */
4418
4419 /* Custom expansion takes care of special things, match
4420 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004421 if ((context == EXPAND_USER_DEFINED
4422 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 new_len++; /* '\' becomes "\\" */
4424 }
4425 retval = alloc(new_len);
4426 if (retval != NULL)
4427 {
4428 retval[0] = '^';
4429 j = 1;
4430 for (i = 0; i < len; i++, j++)
4431 {
4432 /* Skip backslash. But why? At least keep it for custom
4433 * expansion. */
4434 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004435 && context != EXPAND_USER_LIST
4436 && fname[i] == '\\'
4437 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 break;
4439
4440 switch (fname[i])
4441 {
4442 case '*': retval[j++] = '.';
4443 break;
4444 case '~': retval[j++] = '\\';
4445 break;
4446 case '?': retval[j] = '.';
4447 continue;
4448 case '.': if (context == EXPAND_BUFFERS)
4449 retval[j++] = '\\';
4450 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004451 case '\\': if (context == EXPAND_USER_DEFINED
4452 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 retval[j++] = '\\';
4454 break;
4455 }
4456 retval[j] = fname[i];
4457 }
4458 retval[j] = NUL;
4459 }
4460 }
4461 }
4462 else
4463 {
4464 retval = alloc(len + 4);
4465 if (retval != NULL)
4466 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004467 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
4469 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004470 * Don't add a star to *, ~, ~user, $var or `cmd`.
4471 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 * ~ would be at the start of the file name, but not the tail.
4473 * $ could be anywhere in the tail.
4474 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004475 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 */
4477 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004478 ends_in_star = (len > 0 && retval[len - 1] == '*');
4479#ifndef BACKSLASH_IN_FILENAME
4480 for (i = len - 2; i >= 0; --i)
4481 {
4482 if (retval[i] != '\\')
4483 break;
4484 ends_in_star = !ends_in_star;
4485 }
4486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004488 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 && vim_strchr(tail, '$') == NULL
4490 && vim_strchr(retval, '`') == NULL)
4491 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004492 else if (len > 0 && retval[len - 1] == '$')
4493 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 retval[len] = NUL;
4495 }
4496 }
4497 return retval;
4498}
4499
4500/*
4501 * Must parse the command line so far to work out what context we are in.
4502 * Completion can then be done based on that context.
4503 * This routine sets the variables:
4504 * xp->xp_pattern The start of the pattern to be expanded within
4505 * the command line (ends at the cursor).
4506 * xp->xp_context The type of thing to expand. Will be one of:
4507 *
4508 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4509 * the command line, like an unknown command. Caller
4510 * should beep.
4511 * EXPAND_NOTHING Unrecognised context for completion, use char like
4512 * a normal char, rather than for completion. eg
4513 * :s/^I/
4514 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4515 * it.
4516 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4517 * EXPAND_FILES After command with XFILE set, or after setting
4518 * with P_EXPAND set. eg :e ^I, :w>>^I
4519 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4520 * when we know only directories are of interest. eg
4521 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4524 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4525 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4526 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4527 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4528 * EXPAND_EVENTS Complete event names
4529 * EXPAND_SYNTAX Complete :syntax command arguments
4530 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4531 * EXPAND_AUGROUP Complete autocommand group names
4532 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4533 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4534 * eg :unmap a^I , :cunab x^I
4535 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4536 * eg :call sub^I
4537 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4538 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4539 * names in expressions, eg :while s^I
4540 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004541 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
4543 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004544set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004546 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (ccline.cmdfirstc != ':'
4548#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004549 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004550 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551#endif
4552 )
4553 {
4554 xp->xp_context = EXPAND_NOTHING;
4555 return;
4556 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004557 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558}
4559
4560 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004561set_cmd_context(
4562 expand_T *xp,
4563 char_u *str, /* start of command line */
4564 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004565 int col, /* position of cursor */
4566 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567{
4568 int old_char = NUL;
4569 char_u *nextcomm;
4570
4571 /*
4572 * Avoid a UMR warning from Purify, only save the character if it has been
4573 * written before.
4574 */
4575 if (col < len)
4576 old_char = str[col];
4577 str[col] = NUL;
4578 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004579
4580#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004581 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004582 {
4583# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004584 /* pass CMD_SIZE because there is no real command */
4585 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004586# endif
4587 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004588 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004589 {
4590 xp->xp_context = ccline.xp_context;
4591 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004592# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004593 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004594# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004595 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004596 else
4597#endif
4598 while (nextcomm != NULL)
4599 nextcomm = set_one_cmd_context(xp, nextcomm);
4600
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004601 /* Store the string here so that call_user_expand_func() can get to them
4602 * easily. */
4603 xp->xp_line = str;
4604 xp->xp_col = col;
4605
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 str[col] = old_char;
4607}
4608
4609/*
4610 * Expand the command line "str" from context "xp".
4611 * "xp" must have been set by set_cmd_context().
4612 * xp->xp_pattern points into "str", to where the text that is to be expanded
4613 * starts.
4614 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4615 * cursor.
4616 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4617 * key that triggered expansion literally.
4618 * Returns EXPAND_OK otherwise.
4619 */
4620 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004621expand_cmdline(
4622 expand_T *xp,
4623 char_u *str, /* start of command line */
4624 int col, /* position of cursor */
4625 int *matchcount, /* return: nr of matches */
4626 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
4628 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004629 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630
4631 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4632 {
4633 beep_flush();
4634 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4635 }
4636 if (xp->xp_context == EXPAND_NOTHING)
4637 {
4638 /* Caller can use the character as a normal char instead */
4639 return EXPAND_NOTHING;
4640 }
4641
4642 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004643 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4644 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 if (file_str == NULL)
4646 return EXPAND_UNSUCCESSFUL;
4647
Bram Moolenaar94950a92010-12-02 16:01:29 +01004648 if (p_wic)
4649 options += WILD_ICASE;
4650
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004652 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
4654 *matchcount = 0;
4655 *matches = NULL;
4656 }
4657 vim_free(file_str);
4658
4659 return EXPAND_OK;
4660}
4661
4662#ifdef FEAT_MULTI_LANG
4663/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004664 * Cleanup matches for help tags:
4665 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4666 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004668static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004671cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 int i, j;
4674 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004675 char_u buf[4];
4676 char_u *p = buf;
4677
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004678 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004679 {
4680 *p++ = '@';
4681 *p++ = p_hlg[0];
4682 *p++ = p_hlg[1];
4683 }
4684 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685
4686 for (i = 0; i < num_file; ++i)
4687 {
4688 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004689 if (len <= 0)
4690 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004691 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
4693 /* Sorting on priority means the same item in another language may
4694 * be anywhere. Search all items for a match up to the "@en". */
4695 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004696 if (j != i && (int)STRLEN(file[j]) == len + 3
4697 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 break;
4699 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004700 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 file[i][len] = NUL;
4702 }
4703 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004704
4705 if (*buf != NUL)
4706 for (i = 0; i < num_file; ++i)
4707 {
4708 len = (int)STRLEN(file[i]) - 3;
4709 if (len <= 0)
4710 continue;
4711 if (STRCMP(file[i] + len, buf) == 0)
4712 {
4713 /* remove the default language */
4714 file[i][len] = NUL;
4715 }
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717}
4718#endif
4719
4720/*
4721 * Do the expansion based on xp->xp_context and "pat".
4722 */
4723 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004724ExpandFromContext(
4725 expand_T *xp,
4726 char_u *pat,
4727 int *num_file,
4728 char_u ***file,
4729 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
4731#ifdef FEAT_CMDL_COMPL
4732 regmatch_T regmatch;
4733#endif
4734 int ret;
4735 int flags;
4736
4737 flags = EW_DIR; /* include directories */
4738 if (options & WILD_LIST_NOTFOUND)
4739 flags |= EW_NOTFOUND;
4740 if (options & WILD_ADD_SLASH)
4741 flags |= EW_ADDSLASH;
4742 if (options & WILD_KEEP_ALL)
4743 flags |= EW_KEEPALL;
4744 if (options & WILD_SILENT)
4745 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004746 if (options & WILD_ALLLINKS)
4747 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004749 if (xp->xp_context == EXPAND_FILES
4750 || xp->xp_context == EXPAND_DIRECTORIES
4751 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 {
4753 /*
4754 * Expand file or directory names.
4755 */
4756 int free_pat = FALSE;
4757 int i;
4758
4759 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4760 * space */
4761 if (xp->xp_backslash != XP_BS_NONE)
4762 {
4763 free_pat = TRUE;
4764 pat = vim_strsave(pat);
4765 for (i = 0; pat[i]; ++i)
4766 if (pat[i] == '\\')
4767 {
4768 if (xp->xp_backslash == XP_BS_THREE
4769 && pat[i + 1] == '\\'
4770 && pat[i + 2] == '\\'
4771 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004772 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (xp->xp_backslash == XP_BS_ONE
4774 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004775 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 }
4777 }
4778
4779 if (xp->xp_context == EXPAND_FILES)
4780 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004781 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4782 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 else
4784 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004785 if (options & WILD_ICASE)
4786 flags |= EW_ICASE;
4787
Bram Moolenaard7834d32009-12-02 16:14:36 +00004788 /* Expand wildcards, supporting %:h and the like. */
4789 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 if (free_pat)
4791 vim_free(pat);
4792 return ret;
4793 }
4794
4795 *file = (char_u **)"";
4796 *num_file = 0;
4797 if (xp->xp_context == EXPAND_HELP)
4798 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004799 /* With an empty argument we would get all the help tags, which is
4800 * very slow. Get matches for "help" instead. */
4801 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4802 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804#ifdef FEAT_MULTI_LANG
4805 cleanup_help_tags(*num_file, *file);
4806#endif
4807 return OK;
4808 }
4809 return FAIL;
4810 }
4811
4812#ifndef FEAT_CMDL_COMPL
4813 return FAIL;
4814#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004815 if (xp->xp_context == EXPAND_SHELLCMD)
4816 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 if (xp->xp_context == EXPAND_OLD_SETTING)
4818 return ExpandOldSetting(num_file, file);
4819 if (xp->xp_context == EXPAND_BUFFERS)
4820 return ExpandBufnames(pat, num_file, file, options);
4821 if (xp->xp_context == EXPAND_TAGS
4822 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4823 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4824 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004825 {
4826 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004827 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4828 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004831 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004832 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004833 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004834 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004835 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004836 {
4837 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004838 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004839 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004840 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004841 {
4842 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004843 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004844 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004845# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4846 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004847 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004848# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004849 if (xp->xp_context == EXPAND_PACKADD)
4850 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4853 if (regmatch.regprog == NULL)
4854 return FAIL;
4855
4856 /* set ignore-case according to p_ic, p_scs and pat */
4857 regmatch.rm_ic = ignorecase(pat);
4858
4859 if (xp->xp_context == EXPAND_SETTINGS
4860 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4861 ret = ExpandSettings(xp, &regmatch, num_file, file);
4862 else if (xp->xp_context == EXPAND_MAPPINGS)
4863 ret = ExpandMappings(&regmatch, num_file, file);
4864# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4865 else if (xp->xp_context == EXPAND_USER_DEFINED)
4866 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4867# endif
4868 else
4869 {
4870 static struct expgen
4871 {
4872 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004873 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004875 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 } tab[] =
4877 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004878 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4879 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004880 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004881#ifdef FEAT_CMDHIST
4882 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004885 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004886 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004887 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4888 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4889 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890#endif
4891#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004892 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4893 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4894 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4895 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896#endif
4897#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004898 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4899 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900#endif
4901#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004902 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004904#ifdef FEAT_PROFILE
4905 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4906#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004907 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#ifdef FEAT_AUTOCMD
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004909 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4910 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911#endif
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004912#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004913 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004914#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004915#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004916 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004917#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004918#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004919 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4922 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004923 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4924 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004926 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004927 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 };
4929 int i;
4930
4931 /*
4932 * Find a context in the table and call the ExpandGeneric() with the
4933 * right function to do the expansion.
4934 */
4935 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00004936 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 if (xp->xp_context == tab[i].context)
4938 {
4939 if (tab[i].ic)
4940 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004941 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02004942 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 break;
4944 }
4945 }
4946
Bram Moolenaar473de612013-06-08 18:19:48 +02004947 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 return ret;
4950#endif /* FEAT_CMDL_COMPL */
4951}
4952
4953#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
4954/*
4955 * Expand a list of names.
4956 *
4957 * Generic function for command line completion. It calls a function to
4958 * obtain strings, one by one. The strings are matched against a regexp
4959 * program. Matching strings are copied into an array, which is returned.
4960 *
4961 * Returns OK when no problems encountered, FAIL for error (out of memory).
4962 */
4963 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004964ExpandGeneric(
4965 expand_T *xp,
4966 regmatch_T *regmatch,
4967 int *num_file,
4968 char_u ***file,
4969 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004971 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
4973 int i;
4974 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004975 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 char_u *str;
4977
4978 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004979 * round == 0: count the number of matching names
4980 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004982 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 for (i = 0; ; ++i)
4985 {
4986 str = (*func)(xp, i);
4987 if (str == NULL) /* end of list */
4988 break;
4989 if (*str == NUL) /* skip empty strings */
4990 continue;
4991
4992 if (vim_regexec(regmatch, str, (colnr_T)0))
4993 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00004994 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004996 if (escaped)
4997 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
4998 else
4999 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 (*file)[count] = str;
5001#ifdef FEAT_MENU
5002 if (func == get_menu_names && str != NULL)
5003 {
5004 /* test for separator added by get_menu_names() */
5005 str += STRLEN(str) - 1;
5006 if (*str == '\001')
5007 *str = '.';
5008 }
5009#endif
5010 }
5011 ++count;
5012 }
5013 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005014 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
5016 if (count == 0)
5017 return OK;
5018 *num_file = count;
5019 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5020 if (*file == NULL)
5021 {
5022 *file = (char_u **)"";
5023 return FAIL;
5024 }
5025 count = 0;
5026 }
5027 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005028
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005029 /* Sort the results. Keep menu's in the specified order. */
5030 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005031 {
5032 if (xp->xp_context == EXPAND_EXPRESSION
5033 || xp->xp_context == EXPAND_FUNCTIONS
5034 || xp->xp_context == EXPAND_USER_FUNC)
5035 /* <SNR> functions should be sorted to the end. */
5036 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5037 sort_func_compare);
5038 else
5039 sort_strings(*file, *num_file);
5040 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005041
Bram Moolenaar4f688582007-07-24 12:34:30 +00005042#ifdef FEAT_CMDL_COMPL
5043 /* Reset the variables used for special highlight names expansion, so that
5044 * they don't show up when getting normal highlight names by ID. */
5045 reset_expand_highlight();
5046#endif
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 return OK;
5049}
5050
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005051/*
5052 * Complete a shell command.
5053 * Returns FAIL or OK;
5054 */
5055 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056expand_shellcmd(
5057 char_u *filepat, /* pattern to match with command names */
5058 int *num_file, /* return: number of matches */
5059 char_u ***file, /* return: array with matches */
5060 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005061{
5062 char_u *pat;
5063 int i;
5064 char_u *path;
5065 int mustfree = FALSE;
5066 garray_T ga;
5067 char_u *buf = alloc(MAXPATHL);
5068 size_t l;
5069 char_u *s, *e;
5070 int flags = flagsarg;
5071 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005072 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005073
5074 if (buf == NULL)
5075 return FAIL;
5076
5077 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5078 * space */
5079 pat = vim_strsave(filepat);
5080 for (i = 0; pat[i]; ++i)
5081 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005082 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005083
Bram Moolenaarb5971142015-03-21 17:32:19 +01005084 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005085
5086 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005087 if (mch_isFullName(pat))
5088 path = (char_u *)" ";
5089 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005090 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5091 path = (char_u *)".";
5092 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005093 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005094 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005095 if (path == NULL)
5096 path = (char_u *)"";
5097 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005098
5099 /*
5100 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005101 * collect them in "ga". When "." is not in $PATH also expand for the
5102 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005103 */
5104 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005105 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005106 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005107 if (*s == NUL)
5108 {
5109 if (did_curdir)
5110 break;
5111 /* Find directories in the current directory, path is empty. */
5112 did_curdir = TRUE;
5113 }
5114 else if (*s == '.')
5115 did_curdir = TRUE;
5116
Bram Moolenaar68c31742006-09-02 15:54:18 +00005117 if (*s == ' ')
5118 ++s; /* Skip space used for absolute path name. */
5119
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005120#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005121 e = vim_strchr(s, ';');
5122#else
5123 e = vim_strchr(s, ':');
5124#endif
5125 if (e == NULL)
5126 e = s + STRLEN(s);
5127
5128 l = e - s;
5129 if (l > MAXPATHL - 5)
5130 break;
5131 vim_strncpy(buf, s, l);
5132 add_pathsep(buf);
5133 l = STRLEN(buf);
5134 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5135
5136 /* Expand matches in one directory of $PATH. */
5137 ret = expand_wildcards(1, &buf, num_file, file, flags);
5138 if (ret == OK)
5139 {
5140 if (ga_grow(&ga, *num_file) == FAIL)
5141 FreeWild(*num_file, *file);
5142 else
5143 {
5144 for (i = 0; i < *num_file; ++i)
5145 {
5146 s = (*file)[i];
5147 if (STRLEN(s) > l)
5148 {
5149 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005150 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005151 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5152 }
5153 else
5154 vim_free(s);
5155 }
5156 vim_free(*file);
5157 }
5158 }
5159 if (*e != NUL)
5160 ++e;
5161 }
5162 *file = ga.ga_data;
5163 *num_file = ga.ga_len;
5164
5165 vim_free(buf);
5166 vim_free(pat);
5167 if (mustfree)
5168 vim_free(path);
5169 return OK;
5170}
5171
5172
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005174static 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 +00005175
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005177 * Call "user_expand_func()" to invoke a user defined Vim script function and
5178 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005180 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005181call_user_expand_func(
5182 void *(*user_expand_func)(char_u *, int, char_u **, int),
5183 expand_T *xp,
5184 int *num_file,
5185 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005187 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005188 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005191 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005192 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
Bram Moolenaarb7515462013-06-29 12:58:33 +02005194 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005195 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 *num_file = 0;
5197 *file = NULL;
5198
Bram Moolenaarb7515462013-06-29 12:58:33 +02005199 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005200 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005201 keep = ccline.cmdbuff[ccline.cmdlen];
5202 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005203 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005204
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005205 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005206 args[1] = xp->xp_line;
5207 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 args[2] = num;
5209
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005210 /* Save the cmdline, we don't know what the function may do. */
5211 save_ccline = ccline;
5212 ccline.cmdbuff = NULL;
5213 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005215
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005216 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005217
5218 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005220 if (ccline.cmdbuff != NULL)
5221 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005222
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005223 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005224 return ret;
5225}
5226
5227/*
5228 * Expand names with a function defined by the user.
5229 */
5230 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005231ExpandUserDefined(
5232 expand_T *xp,
5233 regmatch_T *regmatch,
5234 int *num_file,
5235 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005236{
5237 char_u *retstr;
5238 char_u *s;
5239 char_u *e;
5240 char_u keep;
5241 garray_T ga;
5242
5243 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5244 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return FAIL;
5246
5247 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005248 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
5250 e = vim_strchr(s, '\n');
5251 if (e == NULL)
5252 e = s + STRLEN(s);
5253 keep = *e;
5254 *e = 0;
5255
5256 if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0)
5257 {
5258 *e = keep;
5259 if (*e != NUL)
5260 ++e;
5261 continue;
5262 }
5263
5264 if (ga_grow(&ga, 1) == FAIL)
5265 break;
5266
5267 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5268 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 *e = keep;
5271 if (*e != NUL)
5272 ++e;
5273 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005274 vim_free(retstr);
5275 *file = ga.ga_data;
5276 *num_file = ga.ga_len;
5277 return OK;
5278}
5279
5280/*
5281 * Expand names with a list returned by a function defined by the user.
5282 */
5283 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005284ExpandUserList(
5285 expand_T *xp,
5286 int *num_file,
5287 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005288{
5289 list_T *retlist;
5290 listitem_T *li;
5291 garray_T ga;
5292
5293 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5294 if (retlist == NULL)
5295 return FAIL;
5296
5297 ga_init2(&ga, (int)sizeof(char *), 3);
5298 /* Loop over the items in the list. */
5299 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5300 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005301 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5302 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005303
5304 if (ga_grow(&ga, 1) == FAIL)
5305 break;
5306
5307 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005308 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005309 ++ga.ga_len;
5310 }
5311 list_unref(retlist);
5312
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 *file = ga.ga_data;
5314 *num_file = ga.ga_len;
5315 return OK;
5316}
5317#endif
5318
5319/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005320 * Expand color scheme, compiler or filetype names.
5321 * Search from 'runtimepath':
5322 * 'runtimepath'/{dirnames}/{pat}.vim
5323 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5324 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5325 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5326 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005327 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
5329 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005330ExpandRTDir(
5331 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005332 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005333 int *num_file,
5334 char_u ***file,
5335 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 char_u *s;
5338 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005339 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005341 int i;
5342 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
5344 *num_file = 0;
5345 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005346 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005347 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005349 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005351 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5352 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005354 ga_clear_strings(&ga);
5355 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005357 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005358 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005359 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005361
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005362 if (flags & DIP_START) {
5363 for (i = 0; dirnames[i] != NULL; ++i)
5364 {
5365 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5366 if (s == NULL)
5367 {
5368 ga_clear_strings(&ga);
5369 return FAIL;
5370 }
5371 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5372 globpath(p_pp, s, &ga, 0);
5373 vim_free(s);
5374 }
5375 }
5376
5377 if (flags & DIP_OPT) {
5378 for (i = 0; dirnames[i] != NULL; ++i)
5379 {
5380 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5381 if (s == NULL)
5382 {
5383 ga_clear_strings(&ga);
5384 return FAIL;
5385 }
5386 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5387 globpath(p_pp, s, &ga, 0);
5388 vim_free(s);
5389 }
5390 }
5391
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005392 for (i = 0; i < ga.ga_len; ++i)
5393 {
5394 match = ((char_u **)ga.ga_data)[i];
5395 s = match;
5396 e = s + STRLEN(s);
5397 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5398 {
5399 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005400 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005401 if (s < match || vim_ispathsep(*s))
5402 break;
5403 ++s;
5404 *e = NUL;
5405 mch_memmove(match, s, e - s + 1);
5406 }
5407 }
5408
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005409 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005410 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005411
5412 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005413 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005414 remove_duplicates(&ga);
5415
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 *file = ga.ga_data;
5417 *num_file = ga.ga_len;
5418 return OK;
5419}
5420
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005421/*
5422 * Expand loadplugin names:
5423 * 'packpath'/pack/ * /opt/{pat}
5424 */
5425 static int
5426ExpandPackAddDir(
5427 char_u *pat,
5428 int *num_file,
5429 char_u ***file)
5430{
5431 char_u *s;
5432 char_u *e;
5433 char_u *match;
5434 garray_T ga;
5435 int i;
5436 int pat_len;
5437
5438 *num_file = 0;
5439 *file = NULL;
5440 pat_len = (int)STRLEN(pat);
5441 ga_init2(&ga, (int)sizeof(char *), 10);
5442
5443 s = alloc((unsigned)(pat_len + 26));
5444 if (s == NULL)
5445 {
5446 ga_clear_strings(&ga);
5447 return FAIL;
5448 }
5449 sprintf((char *)s, "pack/*/opt/%s*", pat);
5450 globpath(p_pp, s, &ga, 0);
5451 vim_free(s);
5452
5453 for (i = 0; i < ga.ga_len; ++i)
5454 {
5455 match = ((char_u **)ga.ga_data)[i];
5456 s = gettail(match);
5457 e = s + STRLEN(s);
5458 mch_memmove(match, s, e - s + 1);
5459 }
5460
5461 if (ga.ga_len == 0)
5462 return FAIL;
5463
5464 /* Sort and remove duplicates which can happen when specifying multiple
5465 * directories in dirnames. */
5466 remove_duplicates(&ga);
5467
5468 *file = ga.ga_data;
5469 *num_file = ga.ga_len;
5470 return OK;
5471}
5472
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473#endif
5474
5475#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5476/*
5477 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005478 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005480 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005481globpath(
5482 char_u *path,
5483 char_u *file,
5484 garray_T *ga,
5485 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486{
5487 expand_T xpc;
5488 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 int num_p;
5491 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 buf = alloc(MAXPATHL);
5494 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005495 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005497 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 /* Loop over all entries in {path}. */
5501 while (*path != NUL)
5502 {
5503 /* Copy one item of the path to buf[] and concatenate the file name. */
5504 copy_option_part(&path, buf, MAXPATHL, ",");
5505 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5506 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005507# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005508 /* Using the platform's path separator (\) makes vim incorrectly
5509 * treat it as an escape character, use '/' instead. */
5510 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5511 STRCAT(buf, "/");
5512# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005514# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005516 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5517 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005519 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005521 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 for (i = 0; i < num_p; ++i)
5524 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005525 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005526 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005527 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005530
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 FreeWild(num_p, p);
5532 }
5533 }
5534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
5536 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537}
5538
5539#endif
5540
5541#if defined(FEAT_CMDHIST) || defined(PROTO)
5542
5543/*********************************
5544 * Command line history stuff *
5545 *********************************/
5546
5547/*
5548 * Translate a history character to the associated type number.
5549 */
5550 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005551hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 if (c == ':')
5554 return HIST_CMD;
5555 if (c == '=')
5556 return HIST_EXPR;
5557 if (c == '@')
5558 return HIST_INPUT;
5559 if (c == '>')
5560 return HIST_DEBUG;
5561 return HIST_SEARCH; /* must be '?' or '/' */
5562}
5563
5564/*
5565 * Table of history names.
5566 * These names are used in :history and various hist...() functions.
5567 * It is sufficient to give the significant prefix of a history name.
5568 */
5569
5570static char *(history_names[]) =
5571{
5572 "cmd",
5573 "search",
5574 "expr",
5575 "input",
5576 "debug",
5577 NULL
5578};
5579
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005580#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5581/*
5582 * Function given to ExpandGeneric() to obtain the possible first
5583 * arguments of the ":history command.
5584 */
5585 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005586get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005587{
5588 static char_u compl[2] = { NUL, NUL };
5589 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005590 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005591 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5592
5593 if (idx < short_names_count)
5594 {
5595 compl[0] = (char_u)short_names[idx];
5596 return compl;
5597 }
5598 if (idx < short_names_count + history_name_count)
5599 return (char_u *)history_names[idx - short_names_count];
5600 if (idx == short_names_count + history_name_count)
5601 return (char_u *)"all";
5602 return NULL;
5603}
5604#endif
5605
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606/*
5607 * init_history() - Initialize the command line history.
5608 * Also used to re-allocate the history when the size changes.
5609 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005611init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
5613 int newlen; /* new length of history table */
5614 histentry_T *temp;
5615 int i;
5616 int j;
5617 int type;
5618
5619 /*
5620 * If size of history table changed, reallocate it
5621 */
5622 newlen = (int)p_hi;
5623 if (newlen != hislen) /* history length changed */
5624 {
5625 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5626 {
5627 if (newlen)
5628 {
5629 temp = (histentry_T *)lalloc(
5630 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5631 if (temp == NULL) /* out of memory! */
5632 {
5633 if (type == 0) /* first one: just keep the old length */
5634 {
5635 newlen = hislen;
5636 break;
5637 }
5638 /* Already changed one table, now we can only have zero
5639 * length for all tables. */
5640 newlen = 0;
5641 type = -1;
5642 continue;
5643 }
5644 }
5645 else
5646 temp = NULL;
5647 if (newlen == 0 || temp != NULL)
5648 {
5649 if (hisidx[type] < 0) /* there are no entries yet */
5650 {
5651 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005652 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 }
5654 else if (newlen > hislen) /* array becomes bigger */
5655 {
5656 for (i = 0; i <= hisidx[type]; ++i)
5657 temp[i] = history[type][i];
5658 j = i;
5659 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005660 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 for ( ; j < hislen; ++i, ++j)
5662 temp[i] = history[type][j];
5663 }
5664 else /* array becomes smaller or 0 */
5665 {
5666 j = hisidx[type];
5667 for (i = newlen - 1; ; --i)
5668 {
5669 if (i >= 0) /* copy newest entries */
5670 temp[i] = history[type][j];
5671 else /* remove older entries */
5672 vim_free(history[type][j].hisstr);
5673 if (--j < 0)
5674 j = hislen - 1;
5675 if (j == hisidx[type])
5676 break;
5677 }
5678 hisidx[type] = newlen - 1;
5679 }
5680 vim_free(history[type]);
5681 history[type] = temp;
5682 }
5683 }
5684 hislen = newlen;
5685 }
5686}
5687
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005688 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005689clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005690{
5691 hisptr->hisnum = 0;
5692 hisptr->viminfo = FALSE;
5693 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005694 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005695}
5696
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697/*
5698 * Check if command line 'str' is already in history.
5699 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5700 */
5701 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005702in_history(
5703 int type,
5704 char_u *str,
5705 int move_to_front, /* Move the entry to the front if it exists */
5706 int sep,
5707 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708{
5709 int i;
5710 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005711 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712
5713 if (hisidx[type] < 0)
5714 return FALSE;
5715 i = hisidx[type];
5716 do
5717 {
5718 if (history[type][i].hisstr == NULL)
5719 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005720
5721 /* For search history, check that the separator character matches as
5722 * well. */
5723 p = history[type][i].hisstr;
5724 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005725 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005726 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (!move_to_front)
5729 return TRUE;
5730 last_i = i;
5731 break;
5732 }
5733 if (--i < 0)
5734 i = hislen - 1;
5735 } while (i != hisidx[type]);
5736
5737 if (last_i >= 0)
5738 {
5739 str = history[type][i].hisstr;
5740 while (i != hisidx[type])
5741 {
5742 if (++i >= hislen)
5743 i = 0;
5744 history[type][last_i] = history[type][i];
5745 last_i = i;
5746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005748 history[type][i].viminfo = FALSE;
5749 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005750 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 return TRUE;
5752 }
5753 return FALSE;
5754}
5755
5756/*
5757 * Convert history name (from table above) to its HIST_ equivalent.
5758 * When "name" is empty, return "cmd" history.
5759 * Returns -1 for unknown history name.
5760 */
5761 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005762get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763{
5764 int i;
5765 int len = (int)STRLEN(name);
5766
5767 /* No argument: use current history. */
5768 if (len == 0)
5769 return hist_char2type(ccline.cmdfirstc);
5770
5771 for (i = 0; history_names[i] != NULL; ++i)
5772 if (STRNICMP(name, history_names[i], len) == 0)
5773 return i;
5774
5775 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5776 return hist_char2type(name[0]);
5777
5778 return -1;
5779}
5780
5781static int last_maptick = -1; /* last seen maptick */
5782
5783/*
5784 * Add the given string to the given history. If the string is already in the
5785 * history then it is moved to the front. "histype" may be one of he HIST_
5786 * values.
5787 */
5788 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005789add_to_history(
5790 int histype,
5791 char_u *new_entry,
5792 int in_map, /* consider maptick when inside a mapping */
5793 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794{
5795 histentry_T *hisptr;
5796 int len;
5797
5798 if (hislen == 0) /* no history */
5799 return;
5800
Bram Moolenaara939e432013-11-09 05:30:26 +01005801 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5802 return;
5803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 /*
5805 * Searches inside the same mapping overwrite each other, so that only
5806 * the last line is kept. Be careful not to remove a line that was moved
5807 * down, only lines that were added.
5808 */
5809 if (histype == HIST_SEARCH && in_map)
5810 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005811 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 {
5813 /* Current line is from the same mapping, remove it */
5814 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5815 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005816 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 --hisnum[histype];
5818 if (--hisidx[HIST_SEARCH] < 0)
5819 hisidx[HIST_SEARCH] = hislen - 1;
5820 }
5821 last_maptick = -1;
5822 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005823 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {
5825 if (++hisidx[histype] == hislen)
5826 hisidx[histype] = 0;
5827 hisptr = &history[histype][hisidx[histype]];
5828 vim_free(hisptr->hisstr);
5829
5830 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005831 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5833 if (hisptr->hisstr != NULL)
5834 hisptr->hisstr[len + 1] = sep;
5835
5836 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005837 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005838 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 if (histype == HIST_SEARCH && in_map)
5840 last_maptick = maptick;
5841 }
5842}
5843
5844#if defined(FEAT_EVAL) || defined(PROTO)
5845
5846/*
5847 * Get identifier of newest history entry.
5848 * "histype" may be one of the HIST_ values.
5849 */
5850 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005851get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5854 || hisidx[histype] < 0)
5855 return -1;
5856
5857 return history[histype][hisidx[histype]].hisnum;
5858}
5859
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005860/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 * Calculate history index from a number:
5862 * num > 0: seen as identifying number of a history entry
5863 * num < 0: relative position in history wrt newest entry
5864 * "histype" may be one of the HIST_ values.
5865 */
5866 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005867calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 int i;
5870 histentry_T *hist;
5871 int wrapped = FALSE;
5872
5873 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5874 || (i = hisidx[histype]) < 0 || num == 0)
5875 return -1;
5876
5877 hist = history[histype];
5878 if (num > 0)
5879 {
5880 while (hist[i].hisnum > num)
5881 if (--i < 0)
5882 {
5883 if (wrapped)
5884 break;
5885 i += hislen;
5886 wrapped = TRUE;
5887 }
5888 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5889 return i;
5890 }
5891 else if (-num <= hislen)
5892 {
5893 i += num + 1;
5894 if (i < 0)
5895 i += hislen;
5896 if (hist[i].hisstr != NULL)
5897 return i;
5898 }
5899 return -1;
5900}
5901
5902/*
5903 * Get a history entry by its index.
5904 * "histype" may be one of the HIST_ values.
5905 */
5906 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005907get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908{
5909 idx = calc_hist_idx(histype, idx);
5910 if (idx >= 0)
5911 return history[histype][idx].hisstr;
5912 else
5913 return (char_u *)"";
5914}
5915
5916/*
5917 * Clear all entries of a history.
5918 * "histype" may be one of the HIST_ values.
5919 */
5920 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005921clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922{
5923 int i;
5924 histentry_T *hisptr;
5925
5926 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5927 {
5928 hisptr = history[histype];
5929 for (i = hislen; i--;)
5930 {
5931 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005932 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005933 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 }
5935 hisidx[histype] = -1; /* mark history as cleared */
5936 hisnum[histype] = 0; /* reset identifier counter */
5937 return OK;
5938 }
5939 return FAIL;
5940}
5941
5942/*
5943 * Remove all entries matching {str} from a history.
5944 * "histype" may be one of the HIST_ values.
5945 */
5946 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005947del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 regmatch_T regmatch;
5950 histentry_T *hisptr;
5951 int idx;
5952 int i;
5953 int last;
5954 int found = FALSE;
5955
5956 regmatch.regprog = NULL;
5957 regmatch.rm_ic = FALSE; /* always match case */
5958 if (hislen != 0
5959 && histype >= 0
5960 && histype < HIST_COUNT
5961 && *str != NUL
5962 && (idx = hisidx[histype]) >= 0
5963 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
5964 != NULL)
5965 {
5966 i = last = idx;
5967 do
5968 {
5969 hisptr = &history[histype][i];
5970 if (hisptr->hisstr == NULL)
5971 break;
5972 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
5973 {
5974 found = TRUE;
5975 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005976 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 }
5978 else
5979 {
5980 if (i != last)
5981 {
5982 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005983 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984 }
5985 if (--last < 0)
5986 last += hislen;
5987 }
5988 if (--i < 0)
5989 i += hislen;
5990 } while (i != idx);
5991 if (history[histype][idx].hisstr == NULL)
5992 hisidx[histype] = -1;
5993 }
Bram Moolenaar473de612013-06-08 18:19:48 +02005994 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 return found;
5996}
5997
5998/*
5999 * Remove an indexed entry from a history.
6000 * "histype" may be one of the HIST_ values.
6001 */
6002 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006003del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004{
6005 int i, j;
6006
6007 i = calc_hist_idx(histype, idx);
6008 if (i < 0)
6009 return FALSE;
6010 idx = hisidx[histype];
6011 vim_free(history[histype][i].hisstr);
6012
6013 /* When deleting the last added search string in a mapping, reset
6014 * last_maptick, so that the last added search string isn't deleted again.
6015 */
6016 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6017 last_maptick = -1;
6018
6019 while (i != idx)
6020 {
6021 j = (i + 1) % hislen;
6022 history[histype][i] = history[histype][j];
6023 i = j;
6024 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006025 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026 if (--i < 0)
6027 i += hislen;
6028 hisidx[histype] = i;
6029 return TRUE;
6030}
6031
6032#endif /* FEAT_EVAL */
6033
6034#if defined(FEAT_CRYPT) || defined(PROTO)
6035/*
6036 * Very specific function to remove the value in ":set key=val" from the
6037 * history.
6038 */
6039 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006040remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 char_u *p;
6043 int i;
6044
6045 i = hisidx[HIST_CMD];
6046 if (i < 0)
6047 return;
6048 p = history[HIST_CMD][i].hisstr;
6049 if (p != NULL)
6050 for ( ; *p; ++p)
6051 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6052 {
6053 p = vim_strchr(p + 3, '=');
6054 if (p == NULL)
6055 break;
6056 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006057 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 if (p[i] == '\\' && p[i + 1])
6059 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006060 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 --p;
6062 }
6063}
6064#endif
6065
6066#endif /* FEAT_CMDHIST */
6067
Bram Moolenaar064154c2016-03-19 22:50:43 +01006068#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006069/*
6070 * Get pointer to the command line info to use. cmdline_paste() may clear
6071 * ccline and put the previous value in prev_ccline.
6072 */
6073 static struct cmdline_info *
6074get_ccline_ptr(void)
6075{
6076 if ((State & CMDLINE) == 0)
6077 return NULL;
6078 if (ccline.cmdbuff != NULL)
6079 return &ccline;
6080 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6081 return &prev_ccline;
6082 return NULL;
6083}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006084#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006085
Bram Moolenaar064154c2016-03-19 22:50:43 +01006086#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006087/*
6088 * Get the current command line in allocated memory.
6089 * Only works when the command line is being edited.
6090 * Returns NULL when something is wrong.
6091 */
6092 char_u *
6093get_cmdline_str(void)
6094{
6095 struct cmdline_info *p = get_ccline_ptr();
6096
6097 if (p == NULL)
6098 return NULL;
6099 return vim_strnsave(p->cmdbuff, p->cmdlen);
6100}
6101
6102/*
6103 * Get the current command line position, counted in bytes.
6104 * Zero is the first position.
6105 * Only works when the command line is being edited.
6106 * Returns -1 when something is wrong.
6107 */
6108 int
6109get_cmdline_pos(void)
6110{
6111 struct cmdline_info *p = get_ccline_ptr();
6112
6113 if (p == NULL)
6114 return -1;
6115 return p->cmdpos;
6116}
6117
6118/*
6119 * Set the command line byte position to "pos". Zero is the first position.
6120 * Only works when the command line is being edited.
6121 * Returns 1 when failed, 0 when OK.
6122 */
6123 int
6124set_cmdline_pos(
6125 int pos)
6126{
6127 struct cmdline_info *p = get_ccline_ptr();
6128
6129 if (p == NULL)
6130 return 1;
6131
6132 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6133 * changed the command line. */
6134 if (pos < 0)
6135 new_cmdpos = 0;
6136 else
6137 new_cmdpos = pos;
6138 return 0;
6139}
6140#endif
6141
6142#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6143/*
6144 * Get the current command-line type.
6145 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6146 * Only works when the command line is being edited.
6147 * Returns NUL when something is wrong.
6148 */
6149 int
6150get_cmdline_type(void)
6151{
6152 struct cmdline_info *p = get_ccline_ptr();
6153
6154 if (p == NULL)
6155 return NUL;
6156 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006157 return
6158# ifdef FEAT_EVAL
6159 (p->input_fn) ? '@' :
6160# endif
6161 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006162 return p->cmdfirstc;
6163}
6164#endif
6165
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6167/*
6168 * Get indices "num1,num2" that specify a range within a list (not a range of
6169 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6170 * Returns OK if parsed successfully, otherwise FAIL.
6171 */
6172 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006173get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
6175 int len;
6176 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006177 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178
6179 *str = skipwhite(*str);
6180 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6181 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006182 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 *str += len;
6184 *num1 = (int)num;
6185 first = TRUE;
6186 }
6187 *str = skipwhite(*str);
6188 if (**str == ',') /* parse "to" part of range */
6189 {
6190 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006191 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 if (len > 0)
6193 {
6194 *num2 = (int)num;
6195 *str = skipwhite(*str + len);
6196 }
6197 else if (!first) /* no number given at all */
6198 return FAIL;
6199 }
6200 else if (first) /* only one number given */
6201 *num2 = *num1;
6202 return OK;
6203}
6204#endif
6205
6206#if defined(FEAT_CMDHIST) || defined(PROTO)
6207/*
6208 * :history command - print a history
6209 */
6210 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006211ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 histentry_T *hist;
6214 int histype1 = HIST_CMD;
6215 int histype2 = HIST_CMD;
6216 int hisidx1 = 1;
6217 int hisidx2 = -1;
6218 int idx;
6219 int i, j, k;
6220 char_u *end;
6221 char_u *arg = eap->arg;
6222
6223 if (hislen == 0)
6224 {
6225 MSG(_("'history' option is zero"));
6226 return;
6227 }
6228
6229 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6230 {
6231 end = arg;
6232 while (ASCII_ISALPHA(*end)
6233 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6234 end++;
6235 i = *end;
6236 *end = NUL;
6237 histype1 = get_histtype(arg);
6238 if (histype1 == -1)
6239 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006240 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 {
6242 histype1 = 0;
6243 histype2 = HIST_COUNT-1;
6244 }
6245 else
6246 {
6247 *end = i;
6248 EMSG(_(e_trailing));
6249 return;
6250 }
6251 }
6252 else
6253 histype2 = histype1;
6254 *end = i;
6255 }
6256 else
6257 end = arg;
6258 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6259 {
6260 EMSG(_(e_trailing));
6261 return;
6262 }
6263
6264 for (; !got_int && histype1 <= histype2; ++histype1)
6265 {
6266 STRCPY(IObuff, "\n # ");
6267 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6268 MSG_PUTS_TITLE(IObuff);
6269 idx = hisidx[histype1];
6270 hist = history[histype1];
6271 j = hisidx1;
6272 k = hisidx2;
6273 if (j < 0)
6274 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6275 if (k < 0)
6276 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6277 if (idx >= 0 && j <= k)
6278 for (i = idx + 1; !got_int; ++i)
6279 {
6280 if (i == hislen)
6281 i = 0;
6282 if (hist[i].hisstr != NULL
6283 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6284 {
6285 msg_putchar('\n');
6286 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6287 hist[i].hisnum);
6288 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6289 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006290 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006291 else
6292 STRCAT(IObuff, hist[i].hisstr);
6293 msg_outtrans(IObuff);
6294 out_flush();
6295 }
6296 if (i == idx)
6297 break;
6298 }
6299 }
6300}
6301#endif
6302
6303#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006304/*
6305 * Buffers for history read from a viminfo file. Only valid while reading.
6306 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006307static histentry_T *viminfo_history[HIST_COUNT] =
6308 {NULL, NULL, NULL, NULL, NULL};
6309static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6310static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311static int viminfo_add_at_front = FALSE;
6312
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006313static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314
6315/*
6316 * Translate a history type number to the associated character.
6317 */
6318 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006319hist_type2char(
6320 int type,
6321 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 if (type == HIST_CMD)
6324 return ':';
6325 if (type == HIST_SEARCH)
6326 {
6327 if (use_question)
6328 return '?';
6329 else
6330 return '/';
6331 }
6332 if (type == HIST_EXPR)
6333 return '=';
6334 return '@';
6335}
6336
6337/*
6338 * Prepare for reading the history from the viminfo file.
6339 * This allocates history arrays to store the read history lines.
6340 */
6341 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 int i;
6345 int num;
6346 int type;
6347 int len;
6348
6349 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006350 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (asklen > hislen)
6352 asklen = hislen;
6353
6354 for (type = 0; type < HIST_COUNT; ++type)
6355 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006356 /* Count the number of empty spaces in the history list. Entries read
6357 * from viminfo previously are also considered empty. If there are
6358 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006360 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 num++;
6362 len = asklen;
6363 if (num > len)
6364 len = num;
6365 if (len <= 0)
6366 viminfo_history[type] = NULL;
6367 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006368 viminfo_history[type] = (histentry_T *)lalloc(
6369 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (viminfo_history[type] == NULL)
6371 len = 0;
6372 viminfo_hislen[type] = len;
6373 viminfo_hisidx[type] = 0;
6374 }
6375}
6376
6377/*
6378 * Accept a line from the viminfo, store it in the history array when it's
6379 * new.
6380 */
6381 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006382read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int type;
6385 long_u len;
6386 char_u *val;
6387 char_u *p;
6388
6389 type = hist_char2type(virp->vir_line[0]);
6390 if (viminfo_hisidx[type] < viminfo_hislen[type])
6391 {
6392 val = viminfo_readstring(virp, 1, TRUE);
6393 if (val != NULL && *val != NUL)
6394 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006395 int sep = (*val == ' ' ? NUL : *val);
6396
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006398 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
6400 /* Need to re-allocate to append the separator byte. */
6401 len = STRLEN(val);
6402 p = lalloc(len + 2, TRUE);
6403 if (p != NULL)
6404 {
6405 if (type == HIST_SEARCH)
6406 {
6407 /* Search entry: Move the separator from the first
6408 * column to after the NUL. */
6409 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006410 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411 }
6412 else
6413 {
6414 /* Not a search entry: No separator in the viminfo
6415 * file, add a NUL separator. */
6416 mch_memmove(p, val, (size_t)len + 1);
6417 p[len + 1] = NUL;
6418 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006419 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6420 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006421 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6422 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006423 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 }
6425 }
6426 }
6427 vim_free(val);
6428 }
6429 return viminfo_readline(virp);
6430}
6431
Bram Moolenaar07219f92013-04-14 23:19:36 +02006432/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006433 * Accept a new style history line from the viminfo, store it in the history
6434 * array when it's new.
6435 */
6436 void
6437handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006438 garray_T *values,
6439 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006440{
6441 int type;
6442 long_u len;
6443 char_u *val;
6444 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006445 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006446
6447 /* Check the format:
6448 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006449 if (values->ga_len < 4
6450 || vp[0].bv_type != BVAL_NR
6451 || vp[1].bv_type != BVAL_NR
6452 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6453 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006454 return;
6455
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006456 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006457 if (type >= HIST_COUNT)
6458 return;
6459 if (viminfo_hisidx[type] < viminfo_hislen[type])
6460 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006461 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006462 if (val != NULL && *val != NUL)
6463 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006464 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6465 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006466 int idx;
6467 int overwrite = FALSE;
6468
6469 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6470 {
6471 /* If lines were written by an older Vim we need to avoid
6472 * getting duplicates. See if the entry already exists. */
6473 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6474 {
6475 p = viminfo_history[type][idx].hisstr;
6476 if (STRCMP(val, p) == 0
6477 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6478 {
6479 overwrite = TRUE;
6480 break;
6481 }
6482 }
6483
6484 if (!overwrite)
6485 {
6486 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006487 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006488 p = lalloc(len + 2, TRUE);
6489 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006490 else
6491 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006492 if (p != NULL)
6493 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006494 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006495 if (!overwrite)
6496 {
6497 mch_memmove(p, val, (size_t)len + 1);
6498 /* Put the separator after the NUL. */
6499 p[len + 1] = sep;
6500 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006501 viminfo_history[type][idx].hisnum = 0;
6502 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006503 viminfo_hisidx[type]++;
6504 }
6505 }
6506 }
6507 }
6508 }
6509}
6510
6511/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006512 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006513 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006514 static void
6515concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 int idx;
6518 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006519
6520 idx = hisidx[type] + viminfo_hisidx[type];
6521 if (idx >= hislen)
6522 idx -= hislen;
6523 else if (idx < 0)
6524 idx = hislen - 1;
6525 if (viminfo_add_at_front)
6526 hisidx[type] = idx;
6527 else
6528 {
6529 if (hisidx[type] == -1)
6530 hisidx[type] = hislen - 1;
6531 do
6532 {
6533 if (history[type][idx].hisstr != NULL
6534 || history[type][idx].viminfo)
6535 break;
6536 if (++idx == hislen)
6537 idx = 0;
6538 } while (idx != hisidx[type]);
6539 if (idx != hisidx[type] && --idx < 0)
6540 idx = hislen - 1;
6541 }
6542 for (i = 0; i < viminfo_hisidx[type]; i++)
6543 {
6544 vim_free(history[type][idx].hisstr);
6545 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6546 history[type][idx].viminfo = TRUE;
6547 history[type][idx].time_set = viminfo_history[type][i].time_set;
6548 if (--idx < 0)
6549 idx = hislen - 1;
6550 }
6551 idx += 1;
6552 idx %= hislen;
6553 for (i = 0; i < viminfo_hisidx[type]; i++)
6554 {
6555 history[type][idx++].hisnum = ++hisnum[type];
6556 idx %= hislen;
6557 }
6558}
6559
6560#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6561 static int
6562#ifdef __BORLANDC__
6563_RTLENTRYF
6564#endif
6565sort_hist(const void *s1, const void *s2)
6566{
6567 histentry_T *p1 = *(histentry_T **)s1;
6568 histentry_T *p2 = *(histentry_T **)s2;
6569
6570 if (p1->time_set < p2->time_set) return -1;
6571 if (p1->time_set > p2->time_set) return 1;
6572 return 0;
6573}
6574#endif
6575
6576/*
6577 * Merge history lines from viminfo and lines typed in this Vim based on the
6578 * timestamp;
6579 */
6580 static void
6581merge_history(int type)
6582{
6583 int max_len;
6584 histentry_T **tot_hist;
6585 histentry_T *new_hist;
6586 int i;
6587 int len;
6588
6589 /* Make one long list with all entries. */
6590 max_len = hislen + viminfo_hisidx[type];
6591 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6592 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6593 if (tot_hist == NULL || new_hist == NULL)
6594 {
6595 vim_free(tot_hist);
6596 vim_free(new_hist);
6597 return;
6598 }
6599 for (i = 0; i < viminfo_hisidx[type]; i++)
6600 tot_hist[i] = &viminfo_history[type][i];
6601 len = i;
6602 for (i = 0; i < hislen; i++)
6603 if (history[type][i].hisstr != NULL)
6604 tot_hist[len++] = &history[type][i];
6605
6606 /* Sort the list on timestamp. */
6607 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6608
6609 /* Keep the newest ones. */
6610 for (i = 0; i < hislen; i++)
6611 {
6612 if (i < len)
6613 {
6614 new_hist[i] = *tot_hist[i];
6615 tot_hist[i]->hisstr = NULL;
6616 if (new_hist[i].hisnum == 0)
6617 new_hist[i].hisnum = ++hisnum[type];
6618 }
6619 else
6620 clear_hist_entry(&new_hist[i]);
6621 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006622 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006623
6624 /* Free what is not kept. */
6625 for (i = 0; i < viminfo_hisidx[type]; i++)
6626 vim_free(viminfo_history[type][i].hisstr);
6627 for (i = 0; i < hislen; i++)
6628 vim_free(history[type][i].hisstr);
6629 vim_free(history[type]);
6630 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006631 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006632}
6633
6634/*
6635 * Finish reading history lines from viminfo. Not used when writing viminfo.
6636 */
6637 void
6638finish_viminfo_history(vir_T *virp)
6639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006641 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642
6643 for (type = 0; type < HIST_COUNT; ++type)
6644 {
6645 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006646 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006647
6648 if (merge)
6649 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006651 concat_history(type);
6652
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 vim_free(viminfo_history[type]);
6654 viminfo_history[type] = NULL;
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006655 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 }
6657}
6658
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006659/*
6660 * Write history to viminfo file in "fp".
6661 * When "merge" is TRUE merge history lines with a previously read viminfo
6662 * file, data is in viminfo_history[].
6663 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006666write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667{
6668 int i;
6669 int type;
6670 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006671 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672
6673 init_history();
6674 if (hislen == 0)
6675 return;
6676 for (type = 0; type < HIST_COUNT; ++type)
6677 {
6678 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6679 if (num_saved == 0)
6680 continue;
6681 if (num_saved < 0) /* Use default */
6682 num_saved = hislen;
6683 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6684 type == HIST_CMD ? _("Command Line") :
6685 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006686 type == HIST_EXPR ? _("Expression") :
6687 type == HIST_INPUT ? _("Input Line") :
6688 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 if (num_saved > hislen)
6690 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006691
6692 /*
6693 * Merge typed and viminfo history:
6694 * round 1: history of typed commands.
6695 * round 2: history from recently read viminfo.
6696 */
6697 for (round = 1; round <= 2; ++round)
6698 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006699 if (round == 1)
6700 /* start at newest entry, somewhere in the list */
6701 i = hisidx[type];
6702 else if (viminfo_hisidx[type] > 0)
6703 /* start at newest entry, first in the list */
6704 i = 0;
6705 else
6706 /* empty list */
6707 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006708 if (i >= 0)
6709 while (num_saved > 0
6710 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006712 char_u *p;
6713 time_t timestamp;
6714 int c = NUL;
6715
6716 if (round == 1)
6717 {
6718 p = history[type][i].hisstr;
6719 timestamp = history[type][i].time_set;
6720 }
6721 else
6722 {
6723 p = viminfo_history[type] == NULL ? NULL
6724 : viminfo_history[type][i].hisstr;
6725 timestamp = viminfo_history[type] == NULL ? 0
6726 : viminfo_history[type][i].time_set;
6727 }
6728
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006729 if (p != NULL && (round == 2
6730 || !merge
6731 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006733 --num_saved;
6734 fputc(hist_type2char(type, TRUE), fp);
6735 /* For the search history: put the separator in the
6736 * second column; use a space if there isn't one. */
6737 if (type == HIST_SEARCH)
6738 {
6739 c = p[STRLEN(p) + 1];
6740 putc(c == NUL ? ' ' : c, fp);
6741 }
6742 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006743
6744 {
6745 char cbuf[NUMBUFLEN];
6746
6747 /* New style history with a bar line. Format:
6748 * |{bartype},{histtype},{timestamp},{separator},"text" */
6749 if (c == NUL)
6750 cbuf[0] = NUL;
6751 else
6752 sprintf(cbuf, "%d", c);
6753 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6754 type, (long)timestamp, cbuf);
6755 barline_writestring(fp, p, LSIZE - 20);
6756 putc('\n', fp);
6757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006759 if (round == 1)
6760 {
6761 /* Decrement index, loop around and stop when back at
6762 * the start. */
6763 if (--i < 0)
6764 i = hislen - 1;
6765 if (i == hisidx[type])
6766 break;
6767 }
6768 else
6769 {
6770 /* Increment index. Stop at the end in the while. */
6771 ++i;
6772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006774 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006775 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006776 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaar07219f92013-04-14 23:19:36 +02006778 vim_free(viminfo_history[type]);
6779 viminfo_history[type] = NULL;
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006780 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006781 }
6782}
6783#endif /* FEAT_VIMINFO */
6784
6785#if defined(FEAT_FKMAP) || defined(PROTO)
6786/*
6787 * Write a character at the current cursor+offset position.
6788 * It is directly written into the command buffer block.
6789 */
6790 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006791cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6794 {
6795 EMSG(_("E198: cmd_pchar beyond the command length"));
6796 return;
6797 }
6798 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6799 ccline.cmdbuff[ccline.cmdlen] = NUL;
6800}
6801
6802 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006803cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
6805 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6806 {
6807 /* EMSG(_("cmd_gchar beyond the command length")); */
6808 return NUL;
6809 }
6810 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6811}
6812#endif
6813
6814#if defined(FEAT_CMDWIN) || defined(PROTO)
6815/*
6816 * Open a window on the current command line and history. Allow editing in
6817 * the window. Returns when the window is closed.
6818 * Returns:
6819 * CR if the command is to be executed
6820 * Ctrl_C if it is to be abandoned
6821 * K_IGNORE if editing continues
6822 */
6823 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006824open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825{
6826 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006827 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006829 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006830 win_T *wp;
6831 int i;
6832 linenr_T lnum;
6833 int histtype;
6834 garray_T winsizes;
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006835#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 char_u typestr[2];
Bram Moolenaar7693ec62008-07-24 18:29:37 +00006837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 int save_restart_edit = restart_edit;
6839 int save_State = State;
6840 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006841#ifdef FEAT_RIGHTLEFT
6842 int save_cmdmsg_rl = cmdmsg_rl;
6843#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006844#ifdef FEAT_FOLDING
6845 int save_KeyTyped;
6846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847
6848 /* Can't do this recursively. Can't do it when typing a password. */
6849 if (cmdwin_type != 0
6850# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6851 || cmdline_star > 0
6852# endif
6853 )
6854 {
6855 beep_flush();
6856 return K_IGNORE;
6857 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006858 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006859
6860 /* Save current window sizes. */
6861 win_size_save(&winsizes);
6862
6863# ifdef FEAT_AUTOCMD
6864 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006865 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866# endif
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006867 /* don't use a new tab page */
6868 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006869 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006870
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 /* Create a window for the command-line buffer. */
6872 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6873 {
6874 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006875# ifdef FEAT_AUTOCMD
6876 unblock_autocmds();
6877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878 return K_IGNORE;
6879 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006880 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881
6882 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006883 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006884 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006887#ifdef FEAT_FOLDING
6888 curwin->w_p_fen = FALSE;
6889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006891 curwin->w_p_rl = cmdmsg_rl;
6892 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006894 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895
6896# ifdef FEAT_AUTOCMD
6897 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006898 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006899 /* But don't allow switching to another buffer. */
6900 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901# endif
6902
Bram Moolenaar46152342005-09-07 21:18:43 +00006903 /* Showing the prompt may have set need_wait_return, reset it. */
6904 need_wait_return = FALSE;
6905
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006906 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6908 {
6909 if (p_wc == TAB)
6910 {
6911 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6912 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6913 }
6914 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6915 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006916# ifdef FEAT_AUTOCMD
6917 --curbuf_lock;
6918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006920 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6921 * sets 'textwidth' to 78). */
6922 curbuf->b_p_tw = 0;
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 /* Fill the buffer with the history. */
6925 init_history();
6926 if (hislen > 0)
6927 {
6928 i = hisidx[histtype];
6929 if (i >= 0)
6930 {
6931 lnum = 0;
6932 do
6933 {
6934 if (++i == hislen)
6935 i = 0;
6936 if (history[histtype][i].hisstr != NULL)
6937 ml_append(lnum++, history[histtype][i].hisstr,
6938 (colnr_T)0, FALSE);
6939 }
6940 while (i != hisidx[histtype]);
6941 }
6942 }
6943
6944 /* Replace the empty last line with the current command-line and put the
6945 * cursor there. */
6946 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
6947 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6948 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00006949 changed_line_abv_curs();
6950 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00006951 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952
6953 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01006954 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 /* No Ex mode here! */
6957 exmode_active = 0;
6958
6959 State = NORMAL;
6960# ifdef FEAT_MOUSE
6961 setmouse();
6962# endif
6963
6964# ifdef FEAT_AUTOCMD
6965 /* Trigger CmdwinEnter autocommands. */
6966 typestr[0] = cmdwin_type;
6967 typestr[1] = NUL;
6968 apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00006969 if (restart_edit != 0) /* autocmd with ":startinsert" */
6970 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971# endif
6972
6973 i = RedrawingDisabled;
6974 RedrawingDisabled = 0;
6975
6976 /*
6977 * Call the main loop until <CR> or CTRL-C is typed.
6978 */
6979 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00006980 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981
6982 RedrawingDisabled = i;
6983
6984# ifdef FEAT_AUTOCMD
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006985
6986# ifdef FEAT_FOLDING
6987 save_KeyTyped = KeyTyped;
6988# endif
6989
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 /* Trigger CmdwinLeave autocommands. */
6991 apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006992
6993# ifdef FEAT_FOLDING
6994 /* Restore KeyTyped in case it is modified by autocommands */
6995 KeyTyped = save_KeyTyped;
6996# endif
6997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998# endif
6999
Bram Moolenaarccc18222007-05-10 18:25:20 +00007000 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007001 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 cmdwin_type = 0;
7003
7004 exmode_active = save_exmode;
7005
Bram Moolenaarf9821062008-06-20 16:31:07 +00007006 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007008 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 {
7010 cmdwin_result = Ctrl_C;
7011 EMSG(_("E199: Active window or buffer deleted"));
7012 }
7013 else
7014 {
7015# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
7016 /* autocmds may abort script processing */
7017 if (aborting() && cmdwin_result != K_IGNORE)
7018 cmdwin_result = Ctrl_C;
7019# endif
7020 /* Set the new command line from the cmdline buffer. */
7021 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007022 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007024 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7025
7026 if (histtype == HIST_CMD)
7027 {
7028 /* Execute the command directly. */
7029 ccline.cmdbuff = vim_strsave((char_u *)p);
7030 cmdwin_result = CAR;
7031 }
7032 else
7033 {
7034 /* First need to cancel what we were doing. */
7035 ccline.cmdbuff = NULL;
7036 stuffcharReadbuff(':');
7037 stuffReadbuff((char_u *)p);
7038 stuffcharReadbuff(CAR);
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041 else if (cmdwin_result == K_XF2) /* :qa typed */
7042 {
7043 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7044 cmdwin_result = CAR;
7045 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007046 else if (cmdwin_result == Ctrl_C)
7047 {
7048 /* :q or :close, don't execute any command
7049 * and don't modify the cmd window. */
7050 ccline.cmdbuff = NULL;
7051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 else
7053 ccline.cmdbuff = vim_strsave(ml_get_curline());
7054 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007055 {
7056 ccline.cmdbuff = vim_strsave((char_u *)"");
7057 ccline.cmdlen = 0;
7058 ccline.cmdbufflen = 1;
7059 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 else
7063 {
7064 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7065 ccline.cmdbufflen = ccline.cmdlen + 1;
7066 ccline.cmdpos = curwin->w_cursor.col;
7067 if (ccline.cmdpos > ccline.cmdlen)
7068 ccline.cmdpos = ccline.cmdlen;
7069 if (cmdwin_result == K_IGNORE)
7070 {
7071 set_cmdspos_cursor();
7072 redrawcmd();
7073 }
7074 }
7075
7076# ifdef FEAT_AUTOCMD
7077 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007078 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079# endif
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007080# ifdef FEAT_CONCEAL
7081 /* Avoid command-line window first character being concealed. */
7082 curwin->w_p_cole = 0;
7083# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007085 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 win_goto(old_curwin);
7087 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007088
7089 /* win_close() may have already wiped the buffer when 'bh' is
7090 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007091 if (bufref_valid(&bufref))
7092 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093
7094 /* Restore window sizes. */
7095 win_size_restore(&winsizes);
7096
7097# ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007098 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099# endif
7100 }
7101
7102 ga_clear(&winsizes);
7103 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007104# ifdef FEAT_RIGHTLEFT
7105 cmdmsg_rl = save_cmdmsg_rl;
7106# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 State = save_State;
7109# ifdef FEAT_MOUSE
7110 setmouse();
7111# endif
7112
7113 return cmdwin_result;
7114}
7115#endif /* FEAT_CMDWIN */
7116
7117/*
7118 * Used for commands that either take a simple command string argument, or:
7119 * cmd << endmarker
7120 * {script}
7121 * endmarker
7122 * Returns a pointer to allocated memory with {script} or NULL.
7123 */
7124 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007125script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 char_u *theline;
7128 char *end_pattern = NULL;
7129 char dot[] = ".";
7130 garray_T ga;
7131
7132 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7133 return NULL;
7134
7135 ga_init2(&ga, 1, 0x400);
7136
7137 if (cmd[2] != NUL)
7138 end_pattern = (char *)skipwhite(cmd + 2);
7139 else
7140 end_pattern = dot;
7141
7142 for (;;)
7143 {
7144 theline = eap->getline(
7145#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007146 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147#endif
7148 NUL, eap->cookie, 0);
7149
7150 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007151 {
7152 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 ga_concat(&ga, theline);
7157 ga_append(&ga, '\n');
7158 vim_free(theline);
7159 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007160 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161
7162 return (char_u *)ga.ga_data;
7163}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007164
7165#ifdef FEAT_SEARCH_EXTRA
7166 static void
7167set_search_match(pos_T *t)
7168{
7169 /*
7170 * First move cursor to end of match, then to the start. This
7171 * moves the whole match onto the screen when 'nowrap' is set.
7172 */
7173 t->lnum += search_match_lines;
7174 t->col = search_match_endcol;
7175 if (t->lnum > curbuf->b_ml.ml_line_count)
7176 {
7177 t->lnum = curbuf->b_ml.ml_line_count;
7178 coladvance((colnr_T)MAXCOL);
7179 }
7180}
7181#endif