blob: 063900a388d3295cf1b671e2233dc0157b28f922 [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 Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200144#ifdef FEAT_SEARCH_EXTRA
145static void set_search_match(pos_T *t);
146#endif
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200148
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149 static void
150trigger_cmd_autocmd(int typechar, int evt)
151{
152 char_u typestr[2];
153
154 typestr[0] = typechar;
155 typestr[1] = NUL;
156 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
157}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200160 * Abandon the command line.
161 */
162 static void
163abandon_cmdline(void)
164{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100165 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200166 if (msg_scrolled == 0)
167 compute_cmdrow();
168 MSG("");
169 redraw_cmdline = TRUE;
170}
171
Bram Moolenaaree219b02017-12-17 14:55:01 +0100172#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200173/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100174 * Guess that the pattern matches everything. Only finds specific cases, such
175 * as a trailing \|, which can happen while typing a pattern.
176 */
177 static int
178empty_pattern(char_u *p)
179{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100180 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100181
182 /* remove trailing \v and the like */
183 while (n >= 2 && p[n - 2] == '\\'
184 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
185 n -= 2;
186 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
187}
Bram Moolenaaree219b02017-12-17 14:55:01 +0100188#endif
Bram Moolenaar66216052017-12-16 16:33:44 +0100189
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200190#ifdef FEAT_SEARCH_EXTRA
191typedef struct {
192 colnr_T vs_curswant;
193 colnr_T vs_leftcol;
194 linenr_T vs_topline;
195# ifdef FEAT_DIFF
196 int vs_topfill;
197# endif
198 linenr_T vs_botline;
199 linenr_T vs_empty_rows;
200} viewstate_T;
201
202 static void
203save_viewstate(viewstate_T *vs)
204{
205 vs->vs_curswant = curwin->w_curswant;
206 vs->vs_leftcol = curwin->w_leftcol;
207 vs->vs_topline = curwin->w_topline;
208# ifdef FEAT_DIFF
209 vs->vs_topfill = curwin->w_topfill;
210# endif
211 vs->vs_botline = curwin->w_botline;
212 vs->vs_empty_rows = curwin->w_empty_rows;
213}
214
215 static void
216restore_viewstate(viewstate_T *vs)
217{
218 curwin->w_curswant = vs->vs_curswant;
219 curwin->w_leftcol = vs->vs_leftcol;
220 curwin->w_topline = vs->vs_topline;
221# ifdef FEAT_DIFF
222 curwin->w_topfill = vs->vs_topfill;
223# endif
224 curwin->w_botline = vs->vs_botline;
225 curwin->w_empty_rows = vs->vs_empty_rows;
226}
227#endif
228
Bram Moolenaar66216052017-12-16 16:33:44 +0100229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 * getcmdline() - accept a command line starting with firstc.
231 *
232 * firstc == ':' get ":" command line.
233 * firstc == '/' or '?' get search pattern
234 * firstc == '=' get expression
235 * firstc == '@' get text for input() function
236 * firstc == '>' get text for debug mode
237 * firstc == NUL get text for :insert command
238 * firstc == -1 like NUL, and break on CTRL-C
239 *
240 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
241 * command line.
242 *
243 * Careful: getcmdline() can be called recursively!
244 *
245 * Return pointer to allocated string if there is a commandline, NULL
246 * otherwise.
247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100249getcmdline(
250 int firstc,
251 long count UNUSED, /* only used for incremental search */
252 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
254 int c;
255 int i;
256 int j;
257 int gotesc = FALSE; /* TRUE when <ESC> just typed */
258 int do_abbr; /* when TRUE check for abbr. */
259#ifdef FEAT_CMDHIST
260 char_u *lookfor = NULL; /* string to match */
261 int hiscnt; /* current history line in use */
262 int histype; /* history type to be used */
263#endif
264#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200265 pos_T search_start; /* where 'incsearch' starts searching */
266 pos_T save_cursor;
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200267 viewstate_T init_viewstate;
268 viewstate_T old_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200269 pos_T match_start = curwin->w_cursor;
270 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000271 int did_incsearch = FALSE;
272 int incsearch_postponed = FALSE;
273#endif
274 int did_wild_list = FALSE; /* did wild_list() recently */
275 int wim_index = 0; /* index in wim_flags[] */
276 int res;
277 int save_msg_scroll = msg_scroll;
278 int save_State = State; /* remember State when called */
279 int some_key_typed = FALSE; /* one of the keys was typed */
280#ifdef FEAT_MOUSE
281 /* mouse drag and release events are ignored, unless they are
282 * preceded with a mouse down event */
283 int ignore_drag_release = TRUE;
284#endif
285#ifdef FEAT_EVAL
286 int break_ctrl_c = FALSE;
287#endif
288 expand_T xpc;
289 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100290#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000291 /* Everything that may work recursively should save and restore the
292 * current command line in save_ccline. That includes update_screen(), a
293 * custom status line may invoke ":normal". */
294 struct cmdline_info save_ccline;
295#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200296 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298#ifdef FEAT_EVAL
299 if (firstc == -1)
300 {
301 firstc = NUL;
302 break_ctrl_c = TRUE;
303 }
304#endif
305#ifdef FEAT_RIGHTLEFT
306 /* start without Hebrew mapping for a command line */
307 if (firstc == ':' || firstc == '=' || firstc == '>')
308 cmd_hkmap = 0;
309#endif
310
311 ccline.overstrike = FALSE; /* always start in insert mode */
312#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100313 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200314 save_cursor = curwin->w_cursor; /* may be restored later */
315 search_start = curwin->w_cursor;
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200316 save_viewstate(&init_viewstate);
317 save_viewstate(&old_viewstate);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#endif
319
320 /*
321 * set some variables for redrawcmd()
322 */
323 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000324 ccline.cmdindent = (firstc > 0 ? indent : 0);
325
326 /* alloc initial ccline.cmdbuff */
327 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 if (ccline.cmdbuff == NULL)
329 return NULL; /* out of memory */
330 ccline.cmdlen = ccline.cmdpos = 0;
331 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100332 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000334 /* autoindent for :insert and :append */
335 if (firstc <= 0)
336 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200337 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000338 ccline.cmdbuff[indent] = NUL;
339 ccline.cmdpos = indent;
340 ccline.cmdspos = indent;
341 ccline.cmdlen = indent;
342 }
343
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000345 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347#ifdef FEAT_RIGHTLEFT
348 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
349 && (firstc == '/' || firstc == '?'))
350 cmdmsg_rl = TRUE;
351 else
352 cmdmsg_rl = FALSE;
353#endif
354
355 redir_off = TRUE; /* don't redirect the typed command */
356 if (!cmd_silent)
357 {
358 i = msg_scrolled;
359 msg_scrolled = 0; /* avoid wait_return message */
360 gotocmdline(TRUE);
361 msg_scrolled += i;
362 redrawcmdprompt(); /* draw prompt or indent */
363 set_cmdspos();
364 }
365 xpc.xp_context = EXPAND_NOTHING;
366 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367#ifndef BACKSLASH_IN_FILENAME
368 xpc.xp_shell = FALSE;
369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000371#if defined(FEAT_EVAL)
372 if (ccline.input_fn)
373 {
374 xpc.xp_context = ccline.xp_context;
375 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000376# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000377 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000378# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000379 }
380#endif
381
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 /*
383 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
384 * doing ":@0" when register 0 doesn't contain a CR.
385 */
386 msg_scroll = FALSE;
387
388 State = CMDLINE;
389
390 if (firstc == '/' || firstc == '?' || firstc == '@')
391 {
392 /* Use ":lmap" mappings for search pattern and input(). */
393 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
394 b_im_ptr = &curbuf->b_p_iminsert;
395 else
396 b_im_ptr = &curbuf->b_p_imsearch;
397 if (*b_im_ptr == B_IMODE_LMAP)
398 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100399#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 im_set_active(*b_im_ptr == B_IMODE_IM);
401#endif
402 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100403#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404 else if (p_imcmdline)
405 im_set_active(TRUE);
406#endif
407
408#ifdef FEAT_MOUSE
409 setmouse();
410#endif
411#ifdef CURSOR_SHAPE
412 ui_cursor_shape(); /* may show different cursor shape */
413#endif
414
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000415 /* When inside an autocommand for writing "exiting" may be set and
416 * terminal mode set to cooked. Need to set raw mode here then. */
417 settmode(TMODE_RAW);
418
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200419 /* Trigger CmdlineEnter autocommands. */
420 cmdline_type = firstc == NUL ? '-' : firstc;
421 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200422
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423#ifdef FEAT_CMDHIST
424 init_history();
425 hiscnt = hislen; /* set hiscnt to impossible history value */
426 histype = hist_char2type(firstc);
427#endif
428
429#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000430 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#endif
432
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200433 /* If something above caused an error, reset the flags, we do want to type
434 * and execute commands. Display may be messed up a bit. */
435 if (did_emsg)
436 redrawcmd();
437 did_emsg = FALSE;
438 got_int = FALSE;
439
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 /*
441 * Collect the command string, handling editing keys.
442 */
443 for (;;)
444 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000445 redir_off = TRUE; /* Don't redirect the typed command.
446 Repeated, because a ":redir" inside
447 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448#ifdef USE_ON_FLY_SCROLL
449 dont_scroll = FALSE; /* allow scrolling here */
450#endif
451 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
452
Bram Moolenaar72532d32018-04-07 19:09:09 +0200453 did_emsg = FALSE; /* There can't really be a reason why an error
454 that occurs while typing a command should
455 cause the command not to be executed. */
456
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000458
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100459 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
460 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000461 do
462 {
463 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100464 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 if (KeyTyped)
467 {
468 some_key_typed = TRUE;
469#ifdef FEAT_RIGHTLEFT
470 if (cmd_hkmap)
471 c = hkmap(c);
472# ifdef FEAT_FKMAP
473 if (cmd_fkmap)
474 c = cmdl_fkmap(c);
475# endif
476 if (cmdmsg_rl && !KeyStuffed)
477 {
478 /* Invert horizontal movements and operations. Only when
479 * typed by the user directly, not when the result of a
480 * mapping. */
481 switch (c)
482 {
483 case K_RIGHT: c = K_LEFT; break;
484 case K_S_RIGHT: c = K_S_LEFT; break;
485 case K_C_RIGHT: c = K_C_LEFT; break;
486 case K_LEFT: c = K_RIGHT; break;
487 case K_S_LEFT: c = K_S_RIGHT; break;
488 case K_C_LEFT: c = K_C_RIGHT; break;
489 }
490 }
491#endif
492 }
493
494 /*
495 * Ignore got_int when CTRL-C was typed here.
496 * Don't ignore it in :global, we really need to break then, e.g., for
497 * ":g/pat/normal /pat" (without the <CR>).
498 * Don't ignore it for the input() function.
499 */
500 if ((c == Ctrl_C
501#ifdef UNIX
502 || c == intr_char
503#endif
504 )
505#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
506 && firstc != '@'
507#endif
508#ifdef FEAT_EVAL
509 && !break_ctrl_c
510#endif
511 && !global_busy)
512 got_int = FALSE;
513
514#ifdef FEAT_CMDHIST
515 /* free old command line when finished moving around in the history
516 * list */
517 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000518 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000519 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 && c != K_PAGEDOWN && c != K_PAGEUP
521 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000522 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100524 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526
527 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000528 * When there are matching completions to select <S-Tab> works like
529 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000531 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 c = Ctrl_P;
533
534#ifdef FEAT_WILDMENU
535 /* Special translations for 'wildmenu' */
536 if (did_wild_list && p_wmnu)
537 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000538 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000540 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 c = Ctrl_N;
542 }
543 /* Hitting CR after "emenu Name.": complete submenu */
544 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
545 && ccline.cmdpos > 1
546 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
547 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
548 && (c == '\n' || c == '\r' || c == K_KENTER))
549 c = K_DOWN;
550#endif
551
552 /* free expanded names when finished walking through matches */
553 if (xpc.xp_numfiles != -1
554 && !(c == p_wc && KeyTyped) && c != p_wcm
555 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
556 && c != Ctrl_L)
557 {
558 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
559 did_wild_list = FALSE;
560#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000561 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#endif
563 xpc.xp_context = EXPAND_NOTHING;
564 wim_index = 0;
565#ifdef FEAT_WILDMENU
566 if (p_wmnu && wild_menu_showing != 0)
567 {
568 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000569 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000570
571 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000572 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574 if (wild_menu_showing == WM_SCROLLED)
575 {
576 /* Entered command line, move it up */
577 cmdline_row--;
578 redrawcmd();
579 }
580 else if (save_p_ls != -1)
581 {
582 /* restore 'laststatus' and 'winminheight' */
583 p_ls = save_p_ls;
584 p_wmh = save_p_wmh;
585 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000586 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000588 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 redrawcmd();
590 save_p_ls = -1;
591 }
592 else
593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 redraw_statuslines();
596 }
597 KeyTyped = skt;
598 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000599 if (ccline.input_fn)
600 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 }
602#endif
603 }
604
605#ifdef FEAT_WILDMENU
606 /* Special translations for 'wildmenu' */
607 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
608 {
609 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000610 if (c == K_DOWN && ccline.cmdpos > 0
611 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000613 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 {
615 /* Hitting <Up>: Remove one submenu name in front of the
616 * cursor */
617 int found = FALSE;
618
619 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
620 i = 0;
621 while (--j > 0)
622 {
623 /* check for start of menu name */
624 if (ccline.cmdbuff[j] == ' '
625 && ccline.cmdbuff[j - 1] != '\\')
626 {
627 i = j + 1;
628 break;
629 }
630 /* check for start of submenu name */
631 if (ccline.cmdbuff[j] == '.'
632 && ccline.cmdbuff[j - 1] != '\\')
633 {
634 if (found)
635 {
636 i = j + 1;
637 break;
638 }
639 else
640 found = TRUE;
641 }
642 }
643 if (i > 0)
644 cmdline_del(i);
645 c = p_wc;
646 xpc.xp_context = EXPAND_NOTHING;
647 }
648 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000649 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000650 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000651 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 {
653 char_u upseg[5];
654
655 upseg[0] = PATHSEP;
656 upseg[1] = '.';
657 upseg[2] = '.';
658 upseg[3] = PATHSEP;
659 upseg[4] = NUL;
660
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000661 if (c == K_DOWN
662 && ccline.cmdpos > 0
663 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
664 && (ccline.cmdpos < 3
665 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
667 {
668 /* go down a directory */
669 c = p_wc;
670 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000671 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 {
673 /* If in a direct ancestor, strip off one ../ to go down */
674 int found = FALSE;
675
676 j = ccline.cmdpos;
677 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
678 while (--j > i)
679 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000680#ifdef FEAT_MBYTE
681 if (has_mbyte)
682 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 if (vim_ispathsep(ccline.cmdbuff[j]))
685 {
686 found = TRUE;
687 break;
688 }
689 }
690 if (found
691 && ccline.cmdbuff[j - 1] == '.'
692 && ccline.cmdbuff[j - 2] == '.'
693 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
694 {
695 cmdline_del(j - 2);
696 c = p_wc;
697 }
698 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000699 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 {
701 /* go up a directory */
702 int found = FALSE;
703
704 j = ccline.cmdpos - 1;
705 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
706 while (--j > i)
707 {
708#ifdef FEAT_MBYTE
709 if (has_mbyte)
710 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
711#endif
712 if (vim_ispathsep(ccline.cmdbuff[j])
713#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100714 && vim_strchr((char_u *)" *?[{`$%#",
715 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716#endif
717 )
718 {
719 if (found)
720 {
721 i = j + 1;
722 break;
723 }
724 else
725 found = TRUE;
726 }
727 }
728
729 if (!found)
730 j = i;
731 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
732 j += 4;
733 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
734 && j == i)
735 j += 3;
736 else
737 j = 0;
738 if (j > 0)
739 {
740 /* TODO this is only for DOS/UNIX systems - need to put in
741 * machine-specific stuff here and in upseg init */
742 cmdline_del(j);
743 put_on_cmdline(upseg + 1, 3, FALSE);
744 }
745 else if (ccline.cmdpos > i)
746 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100747
748 /* Now complete in the new directory. Set KeyTyped in case the
749 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100751 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 }
753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754
755#endif /* FEAT_WILDMENU */
756
757 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
758 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
759 if (c == Ctrl_BSL)
760 {
761 ++no_mapping;
762 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000763 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 --no_mapping;
765 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200766 /* CTRL-\ e doesn't work when obtaining an expression, unless it
767 * is in a mapping. */
768 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
769 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 {
771 vungetc(c);
772 c = Ctrl_BSL;
773 }
774#ifdef FEAT_EVAL
775 else if (c == 'e')
776 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200777 char_u *p = NULL;
778 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
780 /*
781 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000782 * Need to save and restore the current command line, to be
783 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 */
785 if (ccline.cmdpos == ccline.cmdlen)
786 new_cmdpos = 99999; /* keep it at the end */
787 else
788 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000789
790 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000792 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 if (c == '=')
794 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000795 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000796 * to avoid nasty things like going to another buffer when
797 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000798 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000799 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000801 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000802 restore_cmdline(&save_ccline);
803
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200804 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200806 len = (int)STRLEN(p);
807 if (realloc_cmdbuff(len + 1) == OK)
808 {
809 ccline.cmdlen = len;
810 STRCPY(ccline.cmdbuff, p);
811 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200813 /* Restore the cursor or use the position set with
814 * set_cmdline_pos(). */
815 if (new_cmdpos > ccline.cmdlen)
816 ccline.cmdpos = ccline.cmdlen;
817 else
818 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200820 KeyTyped = FALSE; /* Don't do p_wc completion. */
821 redrawcmd();
822 goto cmdline_changed;
823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 }
825 }
826 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100827 got_int = FALSE; /* don't abandon the command line */
828 did_emsg = FALSE;
829 emsg_on_display = FALSE;
830 redrawcmd();
831 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 }
833#endif
834 else
835 {
836 if (c == Ctrl_G && p_im && restart_edit == 0)
837 restart_edit = 'a';
838 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
839 in history */
840 goto returncmd; /* back to Normal mode */
841 }
842 }
843
844#ifdef FEAT_CMDWIN
845 if (c == cedit_key || c == K_CMDWIN)
846 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200847 if (ex_normal_busy == 0 && got_int == FALSE)
848 {
849 /*
850 * Open a window to edit the command line (and history).
851 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200852 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200853 some_key_typed = TRUE;
854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 }
856# ifdef FEAT_DIGRAPHS
857 else
858# endif
859#endif
860#ifdef FEAT_DIGRAPHS
861 c = do_digraph(c);
862#endif
863
864 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
865 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
866 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000867 /* In Ex mode a backslash escapes a newline. */
868 if (exmode_active
869 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000870 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000871 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000872 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000874 if (c == K_KENTER)
875 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000877 else
878 {
879 gotesc = FALSE; /* Might have typed ESC previously, don't
880 truncate the cmdline now. */
881 if (ccheck_abbr(c + ABBR_OFF))
882 goto cmdline_changed;
883 if (!cmd_silent)
884 {
885 windgoto(msg_row, 0);
886 out_flush();
887 }
888 break;
889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 }
891
892 /*
893 * Completion for 'wildchar' or 'wildcharm' key.
894 * - hitting <ESC> twice means: abandon command line.
895 * - wildcard expansion is only done when the 'wildchar' key is really
896 * typed, not when it comes from a macro
897 */
898 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
899 {
900 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
901 {
902 /* if 'wildmode' contains "list" may still need to list */
903 if (xpc.xp_numfiles > 1
904 && !did_wild_list
905 && (wim_flags[wim_index] & WIM_LIST))
906 {
907 (void)showmatches(&xpc, FALSE);
908 redrawcmd();
909 did_wild_list = TRUE;
910 }
911 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100912 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
913 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100915 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
916 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 else
918 res = OK; /* don't insert 'wildchar' now */
919 }
920 else /* typed p_wc first time */
921 {
922 wim_index = 0;
923 j = ccline.cmdpos;
924 /* if 'wildmode' first contains "longest", get longest
925 * common part */
926 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100927 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
928 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100930 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
931 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 /* if interrupted while completing, behave like it failed */
934 if (got_int)
935 {
936 (void)vpeekc(); /* remove <C-C> from input stream */
937 got_int = FALSE; /* don't abandon the command line */
938 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
939#ifdef FEAT_WILDMENU
940 xpc.xp_context = EXPAND_NOTHING;
941#endif
942 goto cmdline_changed;
943 }
944
945 /* when more than one match, and 'wildmode' first contains
946 * "list", or no change and 'wildmode' contains "longest,list",
947 * list all matches */
948 if (res == OK && xpc.xp_numfiles > 1)
949 {
950 /* a "longest" that didn't do anything is skipped (but not
951 * "list:longest") */
952 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
953 wim_index = 1;
954 if ((wim_flags[wim_index] & WIM_LIST)
955#ifdef FEAT_WILDMENU
956 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
957#endif
958 )
959 {
960 if (!(wim_flags[0] & WIM_LONGEST))
961 {
962#ifdef FEAT_WILDMENU
963 int p_wmnu_save = p_wmnu;
964 p_wmnu = 0;
965#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100966 /* remove match */
967 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968#ifdef FEAT_WILDMENU
969 p_wmnu = p_wmnu_save;
970#endif
971 }
972#ifdef FEAT_WILDMENU
973 (void)showmatches(&xpc, p_wmnu
974 && ((wim_flags[wim_index] & WIM_LIST) == 0));
975#else
976 (void)showmatches(&xpc, FALSE);
977#endif
978 redrawcmd();
979 did_wild_list = TRUE;
980 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100981 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
982 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100984 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
985 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
987 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200988 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 }
990#ifdef FEAT_WILDMENU
991 else if (xpc.xp_numfiles == -1)
992 xpc.xp_context = EXPAND_NOTHING;
993#endif
994 }
995 if (wim_index < 3)
996 ++wim_index;
997 if (c == ESC)
998 gotesc = TRUE;
999 if (res == OK)
1000 goto cmdline_changed;
1001 }
1002
1003 gotesc = FALSE;
1004
1005 /* <S-Tab> goes to last match, in a clumsy way */
1006 if (c == K_S_TAB && KeyTyped)
1007 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001008 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1009 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1010 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 goto cmdline_changed;
1012 }
1013
1014 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1015 c = NL;
1016
1017 do_abbr = TRUE; /* default: check for abbreviation */
1018
1019 /*
1020 * Big switch for a typed command line character.
1021 */
1022 switch (c)
1023 {
1024 case K_BS:
1025 case Ctrl_H:
1026 case K_DEL:
1027 case K_KDEL:
1028 case Ctrl_W:
1029#ifdef FEAT_FKMAP
1030 if (cmd_fkmap && c == K_BS)
1031 c = K_DEL;
1032#endif
1033 if (c == K_KDEL)
1034 c = K_DEL;
1035
1036 /*
1037 * delete current character is the same as backspace on next
1038 * character, except at end of line
1039 */
1040 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1041 ++ccline.cmdpos;
1042#ifdef FEAT_MBYTE
1043 if (has_mbyte && c == K_DEL)
1044 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1045 ccline.cmdbuff + ccline.cmdpos);
1046#endif
1047 if (ccline.cmdpos > 0)
1048 {
1049 char_u *p;
1050
1051 j = ccline.cmdpos;
1052 p = ccline.cmdbuff + j;
1053#ifdef FEAT_MBYTE
1054 if (has_mbyte)
1055 {
1056 p = mb_prevptr(ccline.cmdbuff, p);
1057 if (c == Ctrl_W)
1058 {
1059 while (p > ccline.cmdbuff && vim_isspace(*p))
1060 p = mb_prevptr(ccline.cmdbuff, p);
1061 i = mb_get_class(p);
1062 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1063 p = mb_prevptr(ccline.cmdbuff, p);
1064 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001065 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067 }
1068 else
1069#endif
1070 if (c == Ctrl_W)
1071 {
1072 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1073 --p;
1074 i = vim_iswordc(p[-1]);
1075 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1076 && vim_iswordc(p[-1]) == i)
1077 --p;
1078 }
1079 else
1080 --p;
1081 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1082 ccline.cmdlen -= j - ccline.cmdpos;
1083 i = ccline.cmdpos;
1084 while (i < ccline.cmdlen)
1085 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1086
1087 /* Truncate at the end, required for multi-byte chars. */
1088 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001089#ifdef FEAT_SEARCH_EXTRA
1090 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001091 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001092 search_start = save_cursor;
1093 /* save view settings, so that the screen
1094 * won't be restored at the wrong position */
Bram Moolenaar9b25af32018-04-28 13:56:09 +02001095 old_viewstate = init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001096 }
1097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 redrawcmd();
1099 }
1100 else if (ccline.cmdlen == 0 && c != Ctrl_W
1101 && ccline.cmdprompt == NULL && indent == 0)
1102 {
1103 /* In ex and debug mode it doesn't make sense to return. */
1104 if (exmode_active
1105#ifdef FEAT_EVAL
1106 || ccline.cmdfirstc == '>'
1107#endif
1108 )
1109 goto cmdline_not_changed;
1110
Bram Moolenaard23a8232018-02-10 18:45:26 +01001111 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (!cmd_silent)
1113 {
1114#ifdef FEAT_RIGHTLEFT
1115 if (cmdmsg_rl)
1116 msg_col = Columns;
1117 else
1118#endif
1119 msg_col = 0;
1120 msg_putchar(' '); /* delete ':' */
1121 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001122#ifdef FEAT_SEARCH_EXTRA
1123 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001124 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 redraw_cmdline = TRUE;
1127 goto returncmd; /* back to cmd mode */
1128 }
1129 goto cmdline_changed;
1130
1131 case K_INS:
1132 case K_KINS:
1133#ifdef FEAT_FKMAP
1134 /* if Farsi mode set, we are in reverse insert mode -
1135 Do not change the mode */
1136 if (cmd_fkmap)
1137 beep_flush();
1138 else
1139#endif
1140 ccline.overstrike = !ccline.overstrike;
1141#ifdef CURSOR_SHAPE
1142 ui_cursor_shape(); /* may show different cursor shape */
1143#endif
1144 goto cmdline_not_changed;
1145
1146 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001147 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 /* ":lmap" mappings exists, toggle use of mappings. */
1150 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001151#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 im_set_active(FALSE); /* Disable input method */
1153#endif
1154 if (b_im_ptr != NULL)
1155 {
1156 if (State & LANGMAP)
1157 *b_im_ptr = B_IMODE_LMAP;
1158 else
1159 *b_im_ptr = B_IMODE_NONE;
1160 }
1161 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001162#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 else
1164 {
1165 /* There are no ":lmap" mappings, toggle IM. When
1166 * 'imdisable' is set don't try getting the status, it's
1167 * always off. */
1168 if ((p_imdisable && b_im_ptr != NULL)
1169 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1170 {
1171 im_set_active(FALSE); /* Disable input method */
1172 if (b_im_ptr != NULL)
1173 *b_im_ptr = B_IMODE_NONE;
1174 }
1175 else
1176 {
1177 im_set_active(TRUE); /* Enable input method */
1178 if (b_im_ptr != NULL)
1179 *b_im_ptr = B_IMODE_IM;
1180 }
1181 }
1182#endif
1183 if (b_im_ptr != NULL)
1184 {
1185 if (b_im_ptr == &curbuf->b_p_iminsert)
1186 set_iminsert_global();
1187 else
1188 set_imsearch_global();
1189 }
1190#ifdef CURSOR_SHAPE
1191 ui_cursor_shape(); /* may show different cursor shape */
1192#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001193#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 /* Show/unshow value of 'keymap' in status lines later. */
1195 status_redraw_curbuf();
1196#endif
1197 goto cmdline_not_changed;
1198
1199/* case '@': only in very old vi */
1200 case Ctrl_U:
1201 /* delete all characters left of the cursor */
1202 j = ccline.cmdpos;
1203 ccline.cmdlen -= j;
1204 i = ccline.cmdpos = 0;
1205 while (i < ccline.cmdlen)
1206 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1207 /* Truncate at the end, required for multi-byte chars. */
1208 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001209#ifdef FEAT_SEARCH_EXTRA
1210 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001211 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 redrawcmd();
1214 goto cmdline_changed;
1215
1216#ifdef FEAT_CLIPBOARD
1217 case Ctrl_Y:
1218 /* Copy the modeless selection, if there is one. */
1219 if (clip_star.state != SELECT_CLEARED)
1220 {
1221 if (clip_star.state == SELECT_DONE)
1222 clip_copy_modeless_selection(TRUE);
1223 goto cmdline_not_changed;
1224 }
1225 break;
1226#endif
1227
1228 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1229 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001230 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001231 * ":normal" runs out of characters. */
1232 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001233 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 goto cmdline_not_changed;
1235
1236 gotesc = TRUE; /* will free ccline.cmdbuff after
1237 putting it in history */
1238 goto returncmd; /* back to cmd mode */
1239
1240 case Ctrl_R: /* insert register */
1241#ifdef USE_ON_FLY_SCROLL
1242 dont_scroll = TRUE; /* disallow scrolling here */
1243#endif
1244 putcmdline('"', TRUE);
1245 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001246 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (i == Ctrl_O)
1248 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1249 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001250 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001251 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 --no_mapping;
1253#ifdef FEAT_EVAL
1254 /*
1255 * Insert the result of an expression.
1256 * Need to save the current command line, to be able to enter
1257 * a new one...
1258 */
1259 new_cmdpos = -1;
1260 if (c == '=')
1261 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1263 {
1264 beep_flush();
1265 c = ESC;
1266 }
1267 else
1268 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001269 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001271 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 }
1273 }
1274#endif
1275 if (c != ESC) /* use ESC to cancel inserting register */
1276 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001277 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001278
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001279#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001280 /* When there was a serious error abort getting the
1281 * command line. */
1282 if (aborting())
1283 {
1284 gotesc = TRUE; /* will free ccline.cmdbuff after
1285 putting it in history */
1286 goto returncmd; /* back to cmd mode */
1287 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 KeyTyped = FALSE; /* Don't do p_wc completion. */
1290#ifdef FEAT_EVAL
1291 if (new_cmdpos >= 0)
1292 {
1293 /* set_cmdline_pos() was used */
1294 if (new_cmdpos > ccline.cmdlen)
1295 ccline.cmdpos = ccline.cmdlen;
1296 else
1297 ccline.cmdpos = new_cmdpos;
1298 }
1299#endif
1300 }
1301 redrawcmd();
1302 goto cmdline_changed;
1303
1304 case Ctrl_D:
1305 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1306 break; /* Use ^D as normal char instead */
1307
1308 redrawcmd();
1309 continue; /* don't do incremental search now */
1310
1311 case K_RIGHT:
1312 case K_S_RIGHT:
1313 case K_C_RIGHT:
1314 do
1315 {
1316 if (ccline.cmdpos >= ccline.cmdlen)
1317 break;
1318 i = cmdline_charsize(ccline.cmdpos);
1319 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1320 break;
1321 ccline.cmdspos += i;
1322#ifdef FEAT_MBYTE
1323 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001324 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 + ccline.cmdpos);
1326 else
1327#endif
1328 ++ccline.cmdpos;
1329 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001330 while ((c == K_S_RIGHT || c == K_C_RIGHT
1331 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1333#ifdef FEAT_MBYTE
1334 if (has_mbyte)
1335 set_cmdspos_cursor();
1336#endif
1337 goto cmdline_not_changed;
1338
1339 case K_LEFT:
1340 case K_S_LEFT:
1341 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001342 if (ccline.cmdpos == 0)
1343 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 do
1345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 --ccline.cmdpos;
1347#ifdef FEAT_MBYTE
1348 if (has_mbyte) /* move to first byte of char */
1349 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1350 ccline.cmdbuff + ccline.cmdpos);
1351#endif
1352 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1353 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001354 while (ccline.cmdpos > 0
1355 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001356 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1358#ifdef FEAT_MBYTE
1359 if (has_mbyte)
1360 set_cmdspos_cursor();
1361#endif
1362 goto cmdline_not_changed;
1363
1364 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001365 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001366 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaar4770d092006-01-12 23:22:24 +00001368#ifdef FEAT_GUI_W32
1369 /* On Win32 ignore <M-F4>, we get it when closing the window was
1370 * cancelled. */
1371 case K_F4:
1372 if (mod_mask == MOD_MASK_ALT)
1373 {
1374 redrawcmd(); /* somehow the cmdline is cleared */
1375 goto cmdline_not_changed;
1376 }
1377 break;
1378#endif
1379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#ifdef FEAT_MOUSE
1381 case K_MIDDLEDRAG:
1382 case K_MIDDLERELEASE:
1383 goto cmdline_not_changed; /* Ignore mouse */
1384
1385 case K_MIDDLEMOUSE:
1386# ifdef FEAT_GUI
1387 /* When GUI is active, also paste when 'mouse' is empty */
1388 if (!gui.in_use)
1389# endif
1390 if (!mouse_has(MOUSE_COMMAND))
1391 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001392# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001394 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001396# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001397 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 redrawcmd();
1399 goto cmdline_changed;
1400
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001401# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001403 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 redrawcmd();
1405 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001406# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407
1408 case K_LEFTDRAG:
1409 case K_LEFTRELEASE:
1410 case K_RIGHTDRAG:
1411 case K_RIGHTRELEASE:
1412 /* Ignore drag and release events when the button-down wasn't
1413 * seen before. */
1414 if (ignore_drag_release)
1415 goto cmdline_not_changed;
1416 /* FALLTHROUGH */
1417 case K_LEFTMOUSE:
1418 case K_RIGHTMOUSE:
1419 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1420 ignore_drag_release = TRUE;
1421 else
1422 ignore_drag_release = FALSE;
1423# ifdef FEAT_GUI
1424 /* When GUI is active, also move when 'mouse' is empty */
1425 if (!gui.in_use)
1426# endif
1427 if (!mouse_has(MOUSE_COMMAND))
1428 goto cmdline_not_changed; /* Ignore mouse */
1429# ifdef FEAT_CLIPBOARD
1430 if (mouse_row < cmdline_row && clip_star.available)
1431 {
1432 int button, is_click, is_drag;
1433
1434 /*
1435 * Handle modeless selection.
1436 */
1437 button = get_mouse_button(KEY2TERMCAP1(c),
1438 &is_click, &is_drag);
1439 if (mouse_model_popup() && button == MOUSE_LEFT
1440 && (mod_mask & MOD_MASK_SHIFT))
1441 {
1442 /* Translate shift-left to right button. */
1443 button = MOUSE_RIGHT;
1444 mod_mask &= ~MOD_MASK_SHIFT;
1445 }
1446 clip_modeless(button, is_click, is_drag);
1447 goto cmdline_not_changed;
1448 }
1449# endif
1450
1451 set_cmdspos();
1452 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1453 ++ccline.cmdpos)
1454 {
1455 i = cmdline_charsize(ccline.cmdpos);
1456 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1457 && mouse_col < ccline.cmdspos % Columns + i)
1458 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001459# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 if (has_mbyte)
1461 {
1462 /* Count ">" for double-wide char that doesn't fit. */
1463 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001464 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465 + ccline.cmdpos) - 1;
1466 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001467# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 ccline.cmdspos += i;
1469 }
1470 goto cmdline_not_changed;
1471
1472 /* Mouse scroll wheel: ignored here */
1473 case K_MOUSEDOWN:
1474 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001475 case K_MOUSELEFT:
1476 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 /* Alternate buttons ignored here */
1478 case K_X1MOUSE:
1479 case K_X1DRAG:
1480 case K_X1RELEASE:
1481 case K_X2MOUSE:
1482 case K_X2DRAG:
1483 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001484 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 goto cmdline_not_changed;
1486
1487#endif /* FEAT_MOUSE */
1488
1489#ifdef FEAT_GUI
1490 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1491 case K_LEFTRELEASE_NM:
1492 goto cmdline_not_changed;
1493
1494 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001495 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 {
1497 gui_do_scroll();
1498 redrawcmd();
1499 }
1500 goto cmdline_not_changed;
1501
1502 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001503 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001505 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 redrawcmd();
1507 }
1508 goto cmdline_not_changed;
1509#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001510#ifdef FEAT_GUI_TABLINE
1511 case K_TABLINE:
1512 case K_TABMENU:
1513 /* Don't want to change any tabs here. Make sure the same tab
1514 * is still selected. */
1515 if (gui_use_tabline())
1516 gui_mch_set_curtab(tabpage_index(curtab));
1517 goto cmdline_not_changed;
1518#endif
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 case K_SELECT: /* end of Select mode mapping - ignore */
1521 goto cmdline_not_changed;
1522
1523 case Ctrl_B: /* begin of command line */
1524 case K_HOME:
1525 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 case K_S_HOME:
1527 case K_C_HOME:
1528 ccline.cmdpos = 0;
1529 set_cmdspos();
1530 goto cmdline_not_changed;
1531
1532 case Ctrl_E: /* end of command line */
1533 case K_END:
1534 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 case K_S_END:
1536 case K_C_END:
1537 ccline.cmdpos = ccline.cmdlen;
1538 set_cmdspos_cursor();
1539 goto cmdline_not_changed;
1540
1541 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001542 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 break;
1544 goto cmdline_changed;
1545
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001546 case Ctrl_L:
1547#ifdef FEAT_SEARCH_EXTRA
1548 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1549 {
1550 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001551 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001552 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001553 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001554 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001555 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001556 c = gchar_cursor();
1557 /* If 'ignorecase' and 'smartcase' are set and the
1558 * command line has no uppercase characters, convert
1559 * the character to lowercase */
1560 if (p_ic && p_scs
1561 && !pat_has_uppercase(ccline.cmdbuff))
1562 c = MB_TOLOWER(c);
1563 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001564 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001565 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001566 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001567 != NULL)
1568 {
1569 /* put a backslash before special
1570 * characters */
1571 stuffcharReadbuff(c);
1572 c = '\\';
1573 }
1574 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001575 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001576 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001577 }
1578 goto cmdline_not_changed;
1579 }
1580#endif
1581
1582 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001583 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 break;
1585 goto cmdline_changed;
1586
1587 case Ctrl_N: /* next match */
1588 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001589 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001591 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1592 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001594 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001597 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 case K_UP:
1599 case K_DOWN:
1600 case K_S_UP:
1601 case K_S_DOWN:
1602 case K_PAGEUP:
1603 case K_KPAGEUP:
1604 case K_PAGEDOWN:
1605 case K_KPAGEDOWN:
1606 if (hislen == 0 || firstc == NUL) /* no history */
1607 goto cmdline_not_changed;
1608
1609 i = hiscnt;
1610
1611 /* save current command string so it can be restored later */
1612 if (lookfor == NULL)
1613 {
1614 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1615 goto cmdline_not_changed;
1616 lookfor[ccline.cmdpos] = NUL;
1617 }
1618
1619 j = (int)STRLEN(lookfor);
1620 for (;;)
1621 {
1622 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001623 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001624 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 {
1626 if (hiscnt == hislen) /* first time */
1627 hiscnt = hisidx[histype];
1628 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1629 hiscnt = hislen - 1;
1630 else if (hiscnt != hisidx[histype] + 1)
1631 --hiscnt;
1632 else /* at top of list */
1633 {
1634 hiscnt = i;
1635 break;
1636 }
1637 }
1638 else /* one step forwards */
1639 {
1640 /* on last entry, clear the line */
1641 if (hiscnt == hisidx[histype])
1642 {
1643 hiscnt = hislen;
1644 break;
1645 }
1646
1647 /* not on a history line, nothing to do */
1648 if (hiscnt == hislen)
1649 break;
1650 if (hiscnt == hislen - 1) /* wrap around */
1651 hiscnt = 0;
1652 else
1653 ++hiscnt;
1654 }
1655 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1656 {
1657 hiscnt = i;
1658 break;
1659 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001660 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001661 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 || STRNCMP(history[histype][hiscnt].hisstr,
1663 lookfor, (size_t)j) == 0)
1664 break;
1665 }
1666
1667 if (hiscnt != i) /* jumped to other entry */
1668 {
1669 char_u *p;
1670 int len;
1671 int old_firstc;
1672
1673 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001674 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (hiscnt == hislen)
1676 p = lookfor; /* back to the old one */
1677 else
1678 p = history[histype][hiscnt].hisstr;
1679
1680 if (histype == HIST_SEARCH
1681 && p != lookfor
1682 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1683 {
1684 /* Correct for the separator character used when
1685 * adding the history entry vs the one used now.
1686 * First loop: count length.
1687 * Second loop: copy the characters. */
1688 for (i = 0; i <= 1; ++i)
1689 {
1690 len = 0;
1691 for (j = 0; p[j] != NUL; ++j)
1692 {
1693 /* Replace old sep with new sep, unless it is
1694 * escaped. */
1695 if (p[j] == old_firstc
1696 && (j == 0 || p[j - 1] != '\\'))
1697 {
1698 if (i > 0)
1699 ccline.cmdbuff[len] = firstc;
1700 }
1701 else
1702 {
1703 /* Escape new sep, unless it is already
1704 * escaped. */
1705 if (p[j] == firstc
1706 && (j == 0 || p[j - 1] != '\\'))
1707 {
1708 if (i > 0)
1709 ccline.cmdbuff[len] = '\\';
1710 ++len;
1711 }
1712 if (i > 0)
1713 ccline.cmdbuff[len] = p[j];
1714 }
1715 ++len;
1716 }
1717 if (i == 0)
1718 {
1719 alloc_cmdbuff(len);
1720 if (ccline.cmdbuff == NULL)
1721 goto returncmd;
1722 }
1723 }
1724 ccline.cmdbuff[len] = NUL;
1725 }
1726 else
1727 {
1728 alloc_cmdbuff((int)STRLEN(p));
1729 if (ccline.cmdbuff == NULL)
1730 goto returncmd;
1731 STRCPY(ccline.cmdbuff, p);
1732 }
1733
1734 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1735 redrawcmd();
1736 goto cmdline_changed;
1737 }
1738 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001739#endif
1740 goto cmdline_not_changed;
1741
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001742#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001743 case Ctrl_G: /* next match */
1744 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001745 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1746 {
1747 pos_T t;
Bram Moolenaard0480092017-11-16 22:20:39 +01001748 char_u *pat;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001749 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001750
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001751 if (ccline.cmdlen == 0)
1752 goto cmdline_not_changed;
1753
Bram Moolenaard0480092017-11-16 22:20:39 +01001754 if (firstc == ccline.cmdbuff[0])
1755 pat = last_search_pattern();
1756 else
1757 pat = ccline.cmdbuff;
1758
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001759 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001760 cursor_off();
1761 out_flush();
1762 if (c == Ctrl_G)
1763 {
1764 t = match_end;
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001765 if (LT_POS(match_start, match_end))
1766 /* start searching at the end of the match
1767 * not at the beginning of the next column */
1768 (void)decl(&t);
Bram Moolenaar11956692016-08-27 16:26:56 +02001769 search_flags += SEARCH_COL;
1770 }
1771 else
1772 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001773 if (!p_hls)
1774 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001775 ++emsg_off;
1776 i = searchit(curwin, curbuf, &t,
1777 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaard0480092017-11-16 22:20:39 +01001778 pat, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001779 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001780 --emsg_off;
1781 if (i)
1782 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001783 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001784 match_end = t;
1785 match_start = t;
1786 if (c == Ctrl_T && firstc == '/')
1787 {
1788 /* move just before the current match, so that
1789 * when nv_search finishes the cursor will be
1790 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001791 search_start = t;
1792 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001793 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001794 else if (c == Ctrl_G && firstc == '?')
1795 {
1796 /* move just after the current match, so that
1797 * when nv_search finishes the cursor will be
1798 * put back on the match */
1799 search_start = t;
1800 (void)incl(&search_start);
1801 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001802 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001803 {
1804 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001805 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001806 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001807 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001808 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001809 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001810 }
1811
1812 set_search_match(&match_end);
1813 curwin->w_cursor = match_start;
1814 changed_cline_bef_curs();
1815 update_topline();
1816 validate_cursor();
1817 highlight_match = TRUE;
Bram Moolenaar9b25af32018-04-28 13:56:09 +02001818 save_viewstate(&old_viewstate);
Bram Moolenaar11956692016-08-27 16:26:56 +02001819 update_screen(NOT_VALID);
1820 redrawcmdline();
1821 }
1822 else
1823 vim_beep(BO_ERROR);
Bram Moolenaara1d5c152017-12-16 19:05:22 +01001824 restore_last_search_pattern();
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001825 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001826 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001827 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#endif
1829
1830 case Ctrl_V:
1831 case Ctrl_Q:
1832#ifdef FEAT_MOUSE
1833 ignore_drag_release = TRUE;
1834#endif
1835 putcmdline('^', TRUE);
1836 c = get_literal(); /* get next (two) character(s) */
1837 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001838 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_MBYTE
1840 /* may need to remove ^ when composing char was typed */
1841 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1842 {
1843 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1844 msg_putchar(' ');
1845 cursorcmd();
1846 }
1847#endif
1848 break;
1849
1850#ifdef FEAT_DIGRAPHS
1851 case Ctrl_K:
1852#ifdef FEAT_MOUSE
1853 ignore_drag_release = TRUE;
1854#endif
1855 putcmdline('?', TRUE);
1856#ifdef USE_ON_FLY_SCROLL
1857 dont_scroll = TRUE; /* disallow scrolling here */
1858#endif
1859 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001860 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (c != NUL)
1862 break;
1863
1864 redrawcmd();
1865 goto cmdline_not_changed;
1866#endif /* FEAT_DIGRAPHS */
1867
1868#ifdef FEAT_RIGHTLEFT
1869 case Ctrl__: /* CTRL-_: switch language mode */
1870 if (!p_ari)
1871 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001872# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (p_altkeymap)
1874 {
1875 cmd_fkmap = !cmd_fkmap;
1876 if (cmd_fkmap) /* in Farsi always in Insert mode */
1877 ccline.overstrike = FALSE;
1878 }
1879 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 cmd_hkmap = !cmd_hkmap;
1882 goto cmdline_not_changed;
1883#endif
1884
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001885 case K_PS:
1886 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1887 goto cmdline_changed;
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 default:
1890#ifdef UNIX
1891 if (c == intr_char)
1892 {
1893 gotesc = TRUE; /* will free ccline.cmdbuff after
1894 putting it in history */
1895 goto returncmd; /* back to Normal mode */
1896 }
1897#endif
1898 /*
1899 * Normal character with no special meaning. Just set mod_mask
1900 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1901 * the string <S-Space>. This should only happen after ^V.
1902 */
1903 if (!IS_SPECIAL(c))
1904 mod_mask = 0x0;
1905 break;
1906 }
1907 /*
1908 * End of switch on command line character.
1909 * We come here if we have a normal character.
1910 */
1911
Bram Moolenaarede3e632013-06-23 16:16:19 +02001912 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#ifdef FEAT_MBYTE
1914 /* Add ABBR_OFF for characters above 0x100, this is
1915 * what check_abbr() expects. */
1916 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1917#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001918 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 goto cmdline_changed;
1920
1921 /*
1922 * put the character in the command line
1923 */
1924 if (IS_SPECIAL(c) || mod_mask != 0)
1925 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1926 else
1927 {
1928#ifdef FEAT_MBYTE
1929 if (has_mbyte)
1930 {
1931 j = (*mb_char2bytes)(c, IObuff);
1932 IObuff[j] = NUL; /* exclude composing chars */
1933 put_on_cmdline(IObuff, j, TRUE);
1934 }
1935 else
1936#endif
1937 {
1938 IObuff[0] = c;
1939 put_on_cmdline(IObuff, 1, TRUE);
1940 }
1941 }
1942 goto cmdline_changed;
1943
1944/*
1945 * This part implements incremental searches for "/" and "?"
1946 * Jump to cmdline_not_changed when a character has been read but the command
1947 * line did not change. Then we only search and redraw if something changed in
1948 * the past.
1949 * Jump to cmdline_changed when the command line did change.
1950 * (Sorry for the goto's, I know it is ugly).
1951 */
1952cmdline_not_changed:
1953#ifdef FEAT_SEARCH_EXTRA
1954 if (!incsearch_postponed)
1955 continue;
1956#endif
1957
1958cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01001959 /* Trigger CmdlineChanged autocommands. */
1960 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01001961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962#ifdef FEAT_SEARCH_EXTRA
1963 /*
1964 * 'incsearch' highlighting.
1965 */
1966 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1967 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001968 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001969#ifdef FEAT_RELTIME
1970 proftime_T tm;
1971#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001972
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 /* if there is a character waiting, search and redraw later */
1974 if (char_avail())
1975 {
1976 incsearch_postponed = TRUE;
1977 continue;
1978 }
1979 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001980 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001981 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982
1983 /* If there is no command line, don't do anything */
1984 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001985 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 i = 0;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001987 set_no_hlsearch(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001988 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 else
1991 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001992 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 cursor_off(); /* so the user knows we're busy */
1994 out_flush();
1995 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001996#ifdef FEAT_RELTIME
1997 /* Set the time limit to half a second. */
1998 profile_setlimit(500L, &tm);
1999#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002000 if (!p_hls)
2001 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002003 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002004#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002005 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002006#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02002007 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002008#endif
2009 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 --emsg_off;
2011 /* if interrupted while searching, behave like it failed */
2012 if (got_int)
2013 {
2014 (void)vpeekc(); /* remove <C-C> from input stream */
2015 got_int = FALSE; /* don't abandon the command line */
2016 i = 0;
2017 }
2018 else if (char_avail())
2019 /* cancelled searching because a char was typed */
2020 incsearch_postponed = TRUE;
2021 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002022 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 highlight_match = TRUE; /* highlight position */
2024 else
2025 highlight_match = FALSE; /* remove highlight */
2026
2027 /* first restore the old curwin values, so the screen is
2028 * positioned in the same way as the actual search command */
Bram Moolenaar9b25af32018-04-28 13:56:09 +02002029 restore_viewstate(&old_viewstate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 changed_cline_bef_curs();
2031 update_topline();
2032
2033 if (i != 0)
2034 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002035 pos_T save_pos = curwin->w_cursor;
2036
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002037 match_start = curwin->w_cursor;
2038 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002040 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002041 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002042 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002044 else
2045 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002046
Bram Moolenaar66216052017-12-16 16:33:44 +01002047 /* Disable 'hlsearch' highlighting if the pattern matches
2048 * everything. Avoids a flash when typing "foo\|". */
2049 if (empty_pattern(ccline.cmdbuff))
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002050 set_no_hlsearch(TRUE);
Bram Moolenaar66216052017-12-16 16:33:44 +01002051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002053 /* May redraw the status line to show the cursor position. */
2054 if (p_ru && curwin->w_status_height > 0)
2055 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002057 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002058 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002059 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002060 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002061
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002062 /* Leave it at the end to make CTRL-R CTRL-W work. */
2063 if (i != 0)
2064 curwin->w_cursor = end_pos;
2065
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 msg_starthere();
2067 redrawcmdline();
2068 did_incsearch = TRUE;
2069 }
2070#else /* FEAT_SEARCH_EXTRA */
2071 ;
2072#endif
2073
2074#ifdef FEAT_RIGHTLEFT
2075 if (cmdmsg_rl
2076# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002077 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078# endif
2079 )
2080 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002081 * right-left typing. Not efficient, but it works.
2082 * Do it only when there are no characters left to read
2083 * to avoid useless intermediate redraws. */
2084 if (vpeekc() == NUL)
2085 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086#endif
2087 }
2088
2089returncmd:
2090
2091#ifdef FEAT_RIGHTLEFT
2092 cmdmsg_rl = FALSE;
2093#endif
2094
2095#ifdef FEAT_FKMAP
2096 cmd_fkmap = 0;
2097#endif
2098
2099 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002100 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101
2102#ifdef FEAT_SEARCH_EXTRA
2103 if (did_incsearch)
2104 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002105 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002106 curwin->w_cursor = save_cursor;
2107 else
2108 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002109 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002110 {
2111 /* put the '" mark at the original position */
2112 curwin->w_cursor = save_cursor;
2113 setpcmark();
2114 }
2115 curwin->w_cursor = search_start;
2116 }
Bram Moolenaar9b25af32018-04-28 13:56:09 +02002117 restore_viewstate(&old_viewstate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 highlight_match = FALSE;
2119 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002120 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 }
2122#endif
2123
2124 if (ccline.cmdbuff != NULL)
2125 {
2126 /*
2127 * Put line in history buffer (":" and "=" only when it was typed).
2128 */
2129#ifdef FEAT_CMDHIST
2130 if (ccline.cmdlen && firstc != NUL
2131 && (some_key_typed || histype == HIST_SEARCH))
2132 {
2133 add_to_history(histype, ccline.cmdbuff, TRUE,
2134 histype == HIST_SEARCH ? firstc : NUL);
2135 if (firstc == ':')
2136 {
2137 vim_free(new_last_cmdline);
2138 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2139 }
2140 }
2141#endif
2142
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002143 if (gotesc)
2144 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146
2147 /*
2148 * If the screen was shifted up, redraw the whole screen (later).
2149 * If the line is too long, clear it, so ruler and shown command do
2150 * not get printed in the middle of it.
2151 */
2152 msg_check();
2153 msg_scroll = save_msg_scroll;
2154 redir_off = FALSE;
2155
2156 /* When the command line was typed, no need for a wait-return prompt. */
2157 if (some_key_typed)
2158 need_wait_return = FALSE;
2159
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002160 /* Trigger CmdlineLeave autocommands. */
2161 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002162
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002164#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2166 im_save_status(b_im_ptr);
2167 im_set_active(FALSE);
2168#endif
2169#ifdef FEAT_MOUSE
2170 setmouse();
2171#endif
2172#ifdef CURSOR_SHAPE
2173 ui_cursor_shape(); /* may show different cursor shape */
2174#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002175 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002177 {
2178 char_u *p = ccline.cmdbuff;
2179
2180 /* Make ccline empty, getcmdline() may try to use it. */
2181 ccline.cmdbuff = NULL;
2182 return p;
2183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184}
2185
2186#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2187/*
2188 * Get a command line with a prompt.
2189 * This is prepared to be called recursively from getcmdline() (e.g. by
2190 * f_input() when evaluating an expression from CTRL-R =).
2191 * Returns the command line in allocated memory, or NULL.
2192 */
2193 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002194getcmdline_prompt(
2195 int firstc,
2196 char_u *prompt, /* command line prompt */
2197 int attr, /* attributes for prompt */
2198 int xp_context, /* type of expansion */
2199 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 char_u *s;
2202 struct cmdline_info save_ccline;
2203 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002204 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002206 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 ccline.cmdprompt = prompt;
2208 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002209# ifdef FEAT_EVAL
2210 ccline.xp_context = xp_context;
2211 ccline.xp_arg = xp_arg;
2212 ccline.input_fn = (firstc == '@');
2213# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002214 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002216 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002217 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002218 /* Restore msg_col, the prompt from input() may have changed it.
2219 * But only if called recursively and the commandline is therefore being
2220 * restored to an old one; if not, the input() prompt stays on the screen,
2221 * so we need its modified msg_col left intact. */
2222 if (ccline.cmdbuff != NULL)
2223 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224
2225 return s;
2226}
2227#endif
2228
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002229/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002230 * Return TRUE when the text must not be changed and we can't switch to
2231 * another window or buffer. Used when editing the command line, evaluating
2232 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002233 */
2234 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002235text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002236{
2237#ifdef FEAT_CMDWIN
2238 if (cmdwin_type != 0)
2239 return TRUE;
2240#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002241 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002242}
2243
2244/*
2245 * Give an error message for a command that isn't allowed while the cmdline
2246 * window is open or editing the cmdline in another way.
2247 */
2248 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002249text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002250{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002251 EMSG(_(get_text_locked_msg()));
2252}
2253
2254 char_u *
2255get_text_locked_msg(void)
2256{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002257#ifdef FEAT_CMDWIN
2258 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002259 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002260#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002261 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002262}
2263
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002264/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002265 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2266 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002267 */
2268 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002269curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002270{
2271 if (curbuf_lock > 0)
2272 {
2273 EMSG(_("E788: Not allowed to edit another buffer now"));
2274 return TRUE;
2275 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002276 return allbuf_locked();
2277}
2278
2279/*
2280 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2281 * message.
2282 */
2283 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002284allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002285{
2286 if (allbuf_lock > 0)
2287 {
2288 EMSG(_("E811: Not allowed to change buffer information now"));
2289 return TRUE;
2290 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002291 return FALSE;
2292}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002293
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002295cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296{
2297#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2298 if (cmdline_star > 0) /* showing '*', always 1 position */
2299 return 1;
2300#endif
2301 return ptr2cells(ccline.cmdbuff + idx);
2302}
2303
2304/*
2305 * Compute the offset of the cursor on the command line for the prompt and
2306 * indent.
2307 */
2308 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002309set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002311 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 ccline.cmdspos = 1 + ccline.cmdindent;
2313 else
2314 ccline.cmdspos = 0 + ccline.cmdindent;
2315}
2316
2317/*
2318 * Compute the screen position for the cursor on the command line.
2319 */
2320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002321set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323 int i, m, c;
2324
2325 set_cmdspos();
2326 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002329 if (m < 0) /* overflow, Columns or Rows at weird value */
2330 m = MAXCOL;
2331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 else
2333 m = MAXCOL;
2334 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2335 {
2336 c = cmdline_charsize(i);
2337#ifdef FEAT_MBYTE
2338 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2339 if (has_mbyte)
2340 correct_cmdspos(i, c);
2341#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002342 /* If the cmdline doesn't fit, show cursor on last visible char.
2343 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if ((ccline.cmdspos += c) >= m)
2345 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 ccline.cmdspos -= c;
2347 break;
2348 }
2349#ifdef FEAT_MBYTE
2350 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002351 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352#endif
2353 }
2354}
2355
2356#ifdef FEAT_MBYTE
2357/*
2358 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2359 * character that doesn't fit, so that a ">" must be displayed.
2360 */
2361 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002362correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002364 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2366 && ccline.cmdspos % Columns + cells > Columns)
2367 ccline.cmdspos++;
2368}
2369#endif
2370
2371/*
2372 * Get an Ex command line for the ":" command.
2373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002375getexline(
2376 int c, /* normally ':', NUL for ":append" */
2377 void *cookie UNUSED,
2378 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379{
2380 /* When executing a register, remove ':' that's in front of each line. */
2381 if (exec_from_reg && vpeekc() == ':')
2382 (void)vgetc();
2383 return getcmdline(c, 1L, indent);
2384}
2385
2386/*
2387 * Get an Ex command line for Ex mode.
2388 * In Ex mode we only use the OS supplied line editing features and no
2389 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002390 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002393getexmodeline(
2394 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002395 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002396 void *cookie UNUSED,
2397 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002399 garray_T line_ga;
2400 char_u *pend;
2401 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002402 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002403 int escaped = FALSE; /* CTRL-V typed */
2404 int vcol = 0;
2405 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002406 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002407 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 /* Switch cursor on now. This avoids that it happens after the "\n", which
2410 * confuses the system function that computes tabstops. */
2411 cursor_on();
2412
2413 /* always start in column 0; write a newline if necessary */
2414 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002415 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002417 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002419 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002420 if (p_prompt)
2421 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 while (indent-- > 0)
2423 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 }
2426
2427 ga_init2(&line_ga, 1, 30);
2428
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002429 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002430 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002431 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002432 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002433 while (indent >= 8)
2434 {
2435 ga_append(&line_ga, TAB);
2436 msg_puts((char_u *)" ");
2437 indent -= 8;
2438 }
2439 while (indent-- > 0)
2440 {
2441 ga_append(&line_ga, ' ');
2442 msg_putchar(' ');
2443 }
2444 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002445 ++no_mapping;
2446 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002447
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 /*
2449 * Get the line, one character at a time.
2450 */
2451 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002452 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002454 long sw;
2455 char_u *s;
2456
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (ga_grow(&line_ga, 40) == FAIL)
2458 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
Bram Moolenaarba748c82017-02-26 14:00:07 +01002460 /*
2461 * Get one character at a time.
2462 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002463 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002464
2465 /* Check for a ":normal" command and no more characters left. */
2466 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2467 c1 = '\n';
2468 else
2469 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
2471 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002472 * Handle line editing.
2473 * Previously this was left to the system, putting the terminal in
2474 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002476 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002478 msg_putchar('\n');
2479 break;
2480 }
2481
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002482 if (c1 == K_PS)
2483 {
2484 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2485 goto redraw;
2486 }
2487
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002488 if (!escaped)
2489 {
2490 /* CR typed means "enter", which is NL */
2491 if (c1 == '\r')
2492 c1 = '\n';
2493
2494 if (c1 == BS || c1 == K_BS
2495 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002497 if (line_ga.ga_len > 0)
2498 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002499#ifdef FEAT_MBYTE
2500 if (has_mbyte)
2501 {
2502 p = (char_u *)line_ga.ga_data;
2503 p[line_ga.ga_len] = NUL;
2504 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2505 line_ga.ga_len -= len;
2506 }
2507 else
2508#endif
2509 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002510 goto redraw;
2511 }
2512 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002515 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002517 msg_col = startcol;
2518 msg_clr_eos();
2519 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002520 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002521 }
2522
2523 if (c1 == Ctrl_T)
2524 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002525 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002526 p = (char_u *)line_ga.ga_data;
2527 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002528 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002529 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002530add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002531 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002533 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002534 p = (char_u *)line_ga.ga_data;
2535 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002536 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2537 *s = ' ';
2538 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002540redraw:
2541 /* redraw the line */
2542 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002543 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002544 p = (char_u *)line_ga.ga_data;
2545 p[line_ga.ga_len] = NUL;
2546 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002548 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002550 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002552 msg_putchar(' ');
2553 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002554 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002556 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002558 len = MB_PTR2LEN(p);
2559 msg_outtrans_len(p, len);
2560 vcol += ptr2cells(p);
2561 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
2563 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002564 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002565 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002566 continue;
2567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002569 if (c1 == Ctrl_D)
2570 {
2571 /* Delete one shiftwidth. */
2572 p = (char_u *)line_ga.ga_data;
2573 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002575 if (prev_char == '^')
2576 ex_keep_indent = TRUE;
2577 indent = 0;
2578 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 }
2580 else
2581 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002583 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002584 if (indent > 0)
2585 {
2586 --indent;
2587 indent -= indent % get_sw_value(curbuf);
2588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002590 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002591 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002592 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002593 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2594 --line_ga.ga_len;
2595 }
2596 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598
2599 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2600 {
2601 escaped = TRUE;
2602 continue;
2603 }
2604
2605 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2606 if (IS_SPECIAL(c1))
2607 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002609
2610 if (IS_SPECIAL(c1))
2611 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002612#ifdef FEAT_MBYTE
2613 if (has_mbyte)
2614 len = (*mb_char2bytes)(c1,
2615 (char_u *)line_ga.ga_data + line_ga.ga_len);
2616 else
2617#endif
2618 {
2619 len = 1;
2620 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2621 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002622 if (c1 == '\n')
2623 msg_putchar('\n');
2624 else if (c1 == TAB)
2625 {
2626 /* Don't use chartabsize(), 'ts' can be different */
2627 do
2628 {
2629 msg_putchar(' ');
2630 } while (++vcol % 8);
2631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002634 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002635 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002636 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002638 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002639 escaped = FALSE;
2640
2641 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002642 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002644 /* We are done when a NL is entered, but not when it comes after an
2645 * odd number of backslashes, that results in a NUL. */
2646 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002648 int bcount = 0;
2649
2650 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2651 ++bcount;
2652
2653 if (bcount > 0)
2654 {
2655 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2656 * "\NL", etc. */
2657 line_ga.ga_len -= (bcount + 1) / 2;
2658 pend -= (bcount + 1) / 2;
2659 pend[-1] = '\n';
2660 }
2661
2662 if ((bcount & 1) == 0)
2663 {
2664 --line_ga.ga_len;
2665 --pend;
2666 *pend = NUL;
2667 break;
2668 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670 }
2671
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 --no_mapping;
2673 --allow_keys;
2674
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 /* make following messages go to the next line */
2676 msg_didout = FALSE;
2677 msg_col = 0;
2678 if (msg_row < Rows - 1)
2679 ++msg_row;
2680 emsg_on_display = FALSE; /* don't want ui_delay() */
2681
2682 if (got_int)
2683 ga_clear(&line_ga);
2684
2685 return (char_u *)line_ga.ga_data;
2686}
2687
Bram Moolenaare344bea2005-09-01 20:46:49 +00002688# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2689 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690/*
2691 * Return TRUE if ccline.overstrike is on.
2692 */
2693 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002694cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 return ccline.overstrike;
2697}
2698
2699/*
2700 * Return TRUE if the cursor is at the end of the cmdline.
2701 */
2702 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002703cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 return (ccline.cmdpos >= ccline.cmdlen);
2706}
2707#endif
2708
Bram Moolenaar9372a112005-12-06 19:59:18 +00002709#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710/*
2711 * Return the virtual column number at the current cursor position.
2712 * This is used by the IM code to obtain the start of the preedit string.
2713 */
2714 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002715cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716{
2717 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2718 return MAXCOL;
2719
2720# ifdef FEAT_MBYTE
2721 if (has_mbyte)
2722 {
2723 colnr_T col;
2724 int i = 0;
2725
2726 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002727 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728
2729 return col;
2730 }
2731 else
2732# endif
2733 return ccline.cmdpos;
2734}
2735#endif
2736
2737#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2738/*
2739 * If part of the command line is an IM preedit string, redraw it with
2740 * IM feedback attributes. The cursor position is restored after drawing.
2741 */
2742 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002743redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744{
2745 if ((State & CMDLINE)
2746 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002747 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 && !p_imdisable
2749 && im_is_preediting())
2750 {
2751 int cmdpos = 0;
2752 int cmdspos;
2753 int old_row;
2754 int old_col;
2755 colnr_T col;
2756
2757 old_row = msg_row;
2758 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002759 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760
2761# ifdef FEAT_MBYTE
2762 if (has_mbyte)
2763 {
2764 for (col = 0; col < preedit_start_col
2765 && cmdpos < ccline.cmdlen; ++col)
2766 {
2767 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002768 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
2770 }
2771 else
2772# endif
2773 {
2774 cmdspos += preedit_start_col;
2775 cmdpos += preedit_start_col;
2776 }
2777
2778 msg_row = cmdline_row + (cmdspos / (int)Columns);
2779 msg_col = cmdspos % (int)Columns;
2780 if (msg_row >= Rows)
2781 msg_row = Rows - 1;
2782
2783 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2784 {
2785 int char_len;
2786 int char_attr;
2787
2788 char_attr = im_get_feedback_attr(col);
2789 if (char_attr < 0)
2790 break; /* end of preedit string */
2791
2792# ifdef FEAT_MBYTE
2793 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002794 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
2796# endif
2797 char_len = 1;
2798
2799 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2800 cmdpos += char_len;
2801 }
2802
2803 msg_row = old_row;
2804 msg_col = old_col;
2805 }
2806}
2807#endif /* FEAT_XIM && FEAT_GUI_GTK */
2808
2809/*
2810 * Allocate a new command line buffer.
2811 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2812 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2813 */
2814 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002815alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816{
2817 /*
2818 * give some extra space to avoid having to allocate all the time
2819 */
2820 if (len < 80)
2821 len = 100;
2822 else
2823 len += 20;
2824
2825 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2826 ccline.cmdbufflen = len;
2827}
2828
2829/*
2830 * Re-allocate the command line to length len + something extra.
2831 * return FAIL for failure, OK otherwise
2832 */
2833 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002834realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
2836 char_u *p;
2837
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002838 if (len < ccline.cmdbufflen)
2839 return OK; /* no need to resize */
2840
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 p = ccline.cmdbuff;
2842 alloc_cmdbuff(len); /* will get some more */
2843 if (ccline.cmdbuff == NULL) /* out of memory */
2844 {
2845 ccline.cmdbuff = p; /* keep the old one */
2846 return FAIL;
2847 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002848 /* There isn't always a NUL after the command, but it may need to be
2849 * there, thus copy up to the NUL and add a NUL. */
2850 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2851 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002853
2854 if (ccline.xpc != NULL
2855 && ccline.xpc->xp_pattern != NULL
2856 && ccline.xpc->xp_context != EXPAND_NOTHING
2857 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2858 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002859 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002860
2861 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2862 * to point into the newly allocated memory. */
2863 if (i >= 0 && i <= ccline.cmdlen)
2864 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2865 }
2866
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 return OK;
2868}
2869
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002870#if defined(FEAT_ARABIC) || defined(PROTO)
2871static char_u *arshape_buf = NULL;
2872
2873# if defined(EXITFREE) || defined(PROTO)
2874 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002875free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002876{
2877 vim_free(arshape_buf);
2878}
2879# endif
2880#endif
2881
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882/*
2883 * Draw part of the cmdline at the current cursor position. But draw stars
2884 * when cmdline_star is TRUE.
2885 */
2886 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002887draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
2889#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2890 int i;
2891
2892 if (cmdline_star > 0)
2893 for (i = 0; i < len; ++i)
2894 {
2895 msg_putchar('*');
2896# ifdef FEAT_MBYTE
2897 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002898 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899# endif
2900 }
2901 else
2902#endif
2903#ifdef FEAT_ARABIC
2904 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906 static int buflen = 0;
2907 char_u *p;
2908 int j;
2909 int newlen = 0;
2910 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002911 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 int prev_c = 0;
2913 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002914 int u8c;
2915 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
2918 /*
2919 * Do arabic shaping into a temporary buffer. This is very
2920 * inefficient!
2921 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002922 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
2924 /* Re-allocate the buffer. We keep it around to avoid a lot of
2925 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002926 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002927 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002928 arshape_buf = alloc(buflen);
2929 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 return; /* out of memory */
2931 }
2932
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002933 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2934 {
2935 /* Prepend a space to draw the leading composing char on. */
2936 arshape_buf[0] = ' ';
2937 newlen = 1;
2938 }
2939
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 for (j = start; j < start + len; j += mb_l)
2941 {
2942 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002943 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002944 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 if (ARABIC_CHAR(u8c))
2946 {
2947 /* Do Arabic shaping. */
2948 if (cmdmsg_rl)
2949 {
2950 /* displaying from right to left */
2951 pc = prev_c;
2952 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002953 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (j + mb_l >= start + len)
2955 nc = NUL;
2956 else
2957 nc = utf_ptr2char(p + mb_l);
2958 }
2959 else
2960 {
2961 /* displaying from left to right */
2962 if (j + mb_l >= start + len)
2963 pc = NUL;
2964 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002965 {
2966 int pcc[MAX_MCO];
2967
2968 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002970 pc1 = pcc[0];
2971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 nc = prev_c;
2973 }
2974 prev_c = u8c;
2975
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002976 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002978 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002979 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002981 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2982 if (u8cc[1] != 0)
2983 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002984 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 }
2986 }
2987 else
2988 {
2989 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002990 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 newlen += mb_l;
2992 }
2993 }
2994
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002995 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 }
2997 else
2998#endif
2999 msg_outtrans_len(ccline.cmdbuff + start, len);
3000}
3001
3002/*
3003 * Put a character on the command line. Shifts the following text to the
3004 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3005 * "c" must be printable (fit in one display cell)!
3006 */
3007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003008putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 if (cmd_silent)
3011 return;
3012 msg_no_more = TRUE;
3013 msg_putchar(c);
3014 if (shift)
3015 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3016 msg_no_more = FALSE;
3017 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003018 extra_char = c;
3019 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020}
3021
3022/*
3023 * Undo a putcmdline(c, FALSE).
3024 */
3025 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003026unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027{
3028 if (cmd_silent)
3029 return;
3030 msg_no_more = TRUE;
3031 if (ccline.cmdlen == ccline.cmdpos)
3032 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003033#ifdef FEAT_MBYTE
3034 else if (has_mbyte)
3035 draw_cmdline(ccline.cmdpos,
3036 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 else
3039 draw_cmdline(ccline.cmdpos, 1);
3040 msg_no_more = FALSE;
3041 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003042 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043}
3044
3045/*
3046 * Put the given string, of the given length, onto the command line.
3047 * If len is -1, then STRLEN() is used to calculate the length.
3048 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3049 * part will be redrawn, otherwise it will not. If this function is called
3050 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3051 * called afterwards.
3052 */
3053 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003054put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
3056 int retval;
3057 int i;
3058 int m;
3059 int c;
3060
3061 if (len < 0)
3062 len = (int)STRLEN(str);
3063
3064 /* Check if ccline.cmdbuff needs to be longer */
3065 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003066 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else
3068 retval = OK;
3069 if (retval == OK)
3070 {
3071 if (!ccline.overstrike)
3072 {
3073 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3074 ccline.cmdbuff + ccline.cmdpos,
3075 (size_t)(ccline.cmdlen - ccline.cmdpos));
3076 ccline.cmdlen += len;
3077 }
3078 else
3079 {
3080#ifdef FEAT_MBYTE
3081 if (has_mbyte)
3082 {
3083 /* Count nr of characters in the new string. */
3084 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003085 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 ++m;
3087 /* Count nr of bytes in cmdline that are overwritten by these
3088 * characters. */
3089 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003090 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 --m;
3092 if (i < ccline.cmdlen)
3093 {
3094 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3095 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3096 ccline.cmdlen += ccline.cmdpos + len - i;
3097 }
3098 else
3099 ccline.cmdlen = ccline.cmdpos + len;
3100 }
3101 else
3102#endif
3103 if (ccline.cmdpos + len > ccline.cmdlen)
3104 ccline.cmdlen = ccline.cmdpos + len;
3105 }
3106 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3107 ccline.cmdbuff[ccline.cmdlen] = NUL;
3108
3109#ifdef FEAT_MBYTE
3110 if (enc_utf8)
3111 {
3112 /* When the inserted text starts with a composing character,
3113 * backup to the character before it. There could be two of them.
3114 */
3115 i = 0;
3116 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3117 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3118 {
3119 i = (*mb_head_off)(ccline.cmdbuff,
3120 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3121 ccline.cmdpos -= i;
3122 len += i;
3123 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3124 }
3125# ifdef FEAT_ARABIC
3126 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3127 {
3128 /* Check the previous character for Arabic combining pair. */
3129 i = (*mb_head_off)(ccline.cmdbuff,
3130 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3131 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3132 + ccline.cmdpos - i), c))
3133 {
3134 ccline.cmdpos -= i;
3135 len += i;
3136 }
3137 else
3138 i = 0;
3139 }
3140# endif
3141 if (i != 0)
3142 {
3143 /* Also backup the cursor position. */
3144 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3145 ccline.cmdspos -= i;
3146 msg_col -= i;
3147 if (msg_col < 0)
3148 {
3149 msg_col += Columns;
3150 --msg_row;
3151 }
3152 }
3153 }
3154#endif
3155
3156 if (redraw && !cmd_silent)
3157 {
3158 msg_no_more = TRUE;
3159 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003160 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3162 /* Avoid clearing the rest of the line too often. */
3163 if (cmdline_row != i || ccline.overstrike)
3164 msg_clr_eos();
3165 msg_no_more = FALSE;
3166 }
3167#ifdef FEAT_FKMAP
3168 /*
3169 * If we are in Farsi command mode, the character input must be in
3170 * Insert mode. So do not advance the cmdpos.
3171 */
3172 if (!cmd_fkmap)
3173#endif
3174 {
3175 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003178 if (m < 0) /* overflow, Columns or Rows at weird value */
3179 m = MAXCOL;
3180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 else
3182 m = MAXCOL;
3183 for (i = 0; i < len; ++i)
3184 {
3185 c = cmdline_charsize(ccline.cmdpos);
3186#ifdef FEAT_MBYTE
3187 /* count ">" for a double-wide char that doesn't fit. */
3188 if (has_mbyte)
3189 correct_cmdspos(ccline.cmdpos, c);
3190#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003191 /* Stop cursor at the end of the screen, but do increment the
3192 * insert position, so that entering a very long command
3193 * works, even though you can't see it. */
3194 if (ccline.cmdspos + c < m)
3195 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196#ifdef FEAT_MBYTE
3197 if (has_mbyte)
3198 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003199 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 if (c > len - i - 1)
3201 c = len - i - 1;
3202 ccline.cmdpos += c;
3203 i += c;
3204 }
3205#endif
3206 ++ccline.cmdpos;
3207 }
3208 }
3209 }
3210 if (redraw)
3211 msg_check();
3212 return retval;
3213}
3214
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003215static struct cmdline_info prev_ccline;
3216static int prev_ccline_used = FALSE;
3217
3218/*
3219 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3220 * and overwrite it. But get_cmdline_str() may need it, thus make it
3221 * available globally in prev_ccline.
3222 */
3223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003224save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003225{
3226 if (!prev_ccline_used)
3227 {
3228 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3229 prev_ccline_used = TRUE;
3230 }
3231 *ccp = prev_ccline;
3232 prev_ccline = ccline;
3233 ccline.cmdbuff = NULL;
3234 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003235 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003236}
3237
3238/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003239 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003240 */
3241 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003242restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003243{
3244 ccline = prev_ccline;
3245 prev_ccline = *ccp;
3246}
3247
Bram Moolenaar5a305422006-04-28 22:38:25 +00003248#if defined(FEAT_EVAL) || defined(PROTO)
3249/*
3250 * Save the command line into allocated memory. Returns a pointer to be
3251 * passed to restore_cmdline_alloc() later.
3252 * Returns NULL when failed.
3253 */
3254 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003255save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003256{
3257 struct cmdline_info *p;
3258
3259 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3260 if (p != NULL)
3261 save_cmdline(p);
3262 return (char_u *)p;
3263}
3264
3265/*
3266 * Restore the command line from the return value of save_cmdline_alloc().
3267 */
3268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003269restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003270{
3271 if (p != NULL)
3272 {
3273 restore_cmdline((struct cmdline_info *)p);
3274 vim_free(p);
3275 }
3276}
3277#endif
3278
Bram Moolenaar8299df92004-07-10 09:47:34 +00003279/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003280 * Paste a yank register into the command line.
3281 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003282 * insert_reg() can't be used here, because special characters from the
3283 * register contents will be interpreted as commands.
3284 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003285 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003286 */
3287 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003288cmdline_paste(
3289 int regname,
3290 int literally, /* Insert text literally instead of "as typed" */
3291 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003292{
3293 long i;
3294 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003295 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003296 int allocated;
3297 struct cmdline_info save_ccline;
3298
3299 /* check for valid regname; also accept special characters for CTRL-R in
3300 * the command line */
3301 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003302 && regname != Ctrl_A && regname != Ctrl_L
3303 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003304 return FAIL;
3305
3306 /* A register containing CTRL-R can cause an endless loop. Allow using
3307 * CTRL-C to break the loop. */
3308 line_breakcheck();
3309 if (got_int)
3310 return FAIL;
3311
3312#ifdef FEAT_CLIPBOARD
3313 regname = may_get_selection(regname);
3314#endif
3315
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003316 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003317 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003318 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003319 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003320 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003321 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003322 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003323
3324 if (i)
3325 {
3326 /* Got the value of a special register in "arg". */
3327 if (arg == NULL)
3328 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003329
3330 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3331 * part of the word. */
3332 p = arg;
3333 if (p_is && regname == Ctrl_W)
3334 {
3335 char_u *w;
3336 int len;
3337
3338 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003339 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003340 {
3341#ifdef FEAT_MBYTE
3342 if (has_mbyte)
3343 {
3344 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3345 if (!vim_iswordc(mb_ptr2char(w - len)))
3346 break;
3347 w -= len;
3348 }
3349 else
3350#endif
3351 {
3352 if (!vim_iswordc(w[-1]))
3353 break;
3354 --w;
3355 }
3356 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003357 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003358 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3359 p += len;
3360 }
3361
3362 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003363 if (allocated)
3364 vim_free(arg);
3365 return OK;
3366 }
3367
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003368 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003369}
3370
3371/*
3372 * Put a string on the command line.
3373 * When "literally" is TRUE, insert literally.
3374 * When "literally" is FALSE, insert as typed, but don't leave the command
3375 * line.
3376 */
3377 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003378cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003379{
3380 int c, cv;
3381
3382 if (literally)
3383 put_on_cmdline(s, -1, TRUE);
3384 else
3385 while (*s != NUL)
3386 {
3387 cv = *s;
3388 if (cv == Ctrl_V && s[1])
3389 ++s;
3390#ifdef FEAT_MBYTE
3391 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003392 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003393 else
3394#endif
3395 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003396 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3397 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003398#ifdef UNIX
3399 || c == intr_char
3400#endif
3401 || (c == Ctrl_BSL && *s == Ctrl_N))
3402 stuffcharReadbuff(Ctrl_V);
3403 stuffcharReadbuff(c);
3404 }
3405}
3406
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407#ifdef FEAT_WILDMENU
3408/*
3409 * Delete characters on the command line, from "from" to the current
3410 * position.
3411 */
3412 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003413cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414{
3415 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3416 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3417 ccline.cmdlen -= ccline.cmdpos - from;
3418 ccline.cmdpos = from;
3419}
3420#endif
3421
3422/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003423 * This function is called when the screen size changes and with incremental
3424 * search and in other situations where the command line may have been
3425 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 */
3427 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003428redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003430 redrawcmdline_ex(TRUE);
3431}
3432
3433 void
3434redrawcmdline_ex(int do_compute_cmdrow)
3435{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (cmd_silent)
3437 return;
3438 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003439 if (do_compute_cmdrow)
3440 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 redrawcmd();
3442 cursorcmd();
3443}
3444
3445 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003446redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447{
3448 int i;
3449
3450 if (cmd_silent)
3451 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003452 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 msg_putchar(ccline.cmdfirstc);
3454 if (ccline.cmdprompt != NULL)
3455 {
3456 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3457 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3458 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003459 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 --ccline.cmdindent;
3461 }
3462 else
3463 for (i = ccline.cmdindent; i > 0; --i)
3464 msg_putchar(' ');
3465}
3466
3467/*
3468 * Redraw what is currently on the command line.
3469 */
3470 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472{
3473 if (cmd_silent)
3474 return;
3475
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003476 /* when 'incsearch' is set there may be no command line while redrawing */
3477 if (ccline.cmdbuff == NULL)
3478 {
3479 windgoto(cmdline_row, 0);
3480 msg_clr_eos();
3481 return;
3482 }
3483
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 msg_start();
3485 redrawcmdprompt();
3486
3487 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3488 msg_no_more = TRUE;
3489 draw_cmdline(0, ccline.cmdlen);
3490 msg_clr_eos();
3491 msg_no_more = FALSE;
3492
3493 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003494 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003495 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 /*
3498 * An emsg() before may have set msg_scroll. This is used in normal mode,
3499 * in cmdline mode we can reset them now.
3500 */
3501 msg_scroll = FALSE; /* next message overwrites cmdline */
3502
3503 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3504 * in cmdline mode */
3505 skip_redraw = FALSE;
3506}
3507
3508 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003509compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003511 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 cmdline_row = Rows - 1;
3513 else
3514 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003515 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516}
3517
3518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003519cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520{
3521 if (cmd_silent)
3522 return;
3523
3524#ifdef FEAT_RIGHTLEFT
3525 if (cmdmsg_rl)
3526 {
3527 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3528 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3529 if (msg_row <= 0)
3530 msg_row = Rows - 1;
3531 }
3532 else
3533#endif
3534 {
3535 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3536 msg_col = ccline.cmdspos % (int)Columns;
3537 if (msg_row >= Rows)
3538 msg_row = Rows - 1;
3539 }
3540
3541 windgoto(msg_row, msg_col);
3542#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003543 if (p_imst == IM_ON_THE_SPOT)
3544 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#endif
3546#ifdef MCH_CURSOR_SHAPE
3547 mch_update_cursor();
3548#endif
3549}
3550
3551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003552gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
3554 msg_start();
3555#ifdef FEAT_RIGHTLEFT
3556 if (cmdmsg_rl)
3557 msg_col = Columns - 1;
3558 else
3559#endif
3560 msg_col = 0; /* always start in column 0 */
3561 if (clr) /* clear the bottom line(s) */
3562 msg_clr_eos(); /* will reset clear_cmdline */
3563 windgoto(cmdline_row, 0);
3564}
3565
3566/*
3567 * Check the word in front of the cursor for an abbreviation.
3568 * Called when the non-id character "c" has been entered.
3569 * When an abbreviation is recognized it is removed from the text with
3570 * backspaces and the replacement string is inserted, followed by "c".
3571 */
3572 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003573ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003575 int spos = 0;
3576
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3578 return FALSE;
3579
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003580 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3581 * Actually accepts any mark. */
3582 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3583 spos++;
3584 if (ccline.cmdlen - spos > 5
3585 && ccline.cmdbuff[spos] == '\''
3586 && ccline.cmdbuff[spos + 2] == ','
3587 && ccline.cmdbuff[spos + 3] == '\'')
3588 spos += 5;
3589 else
3590 /* check abbreviation from the beginning of the commandline */
3591 spos = 0;
3592
3593 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594}
3595
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003596#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3597 static int
3598#ifdef __BORLANDC__
3599_RTLENTRYF
3600#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003601sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003602{
3603 char_u *p1 = *(char_u **)s1;
3604 char_u *p2 = *(char_u **)s2;
3605
3606 if (*p1 != '<' && *p2 == '<') return -1;
3607 if (*p1 == '<' && *p2 != '<') return 1;
3608 return STRCMP(p1, p2);
3609}
3610#endif
3611
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612/*
3613 * Return FAIL if this is not an appropriate context in which to do
3614 * completion of anything, return OK if it is (even if there are no matches).
3615 * For the caller, this means that the character is just passed through like a
3616 * normal character (instead of being expanded). This allows :s/^I^D etc.
3617 */
3618 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003619nextwild(
3620 expand_T *xp,
3621 int type,
3622 int options, /* extra options for ExpandOne() */
3623 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624{
3625 int i, j;
3626 char_u *p1;
3627 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 int difflen;
3629 int v;
3630
3631 if (xp->xp_numfiles == -1)
3632 {
3633 set_expand_context(xp);
3634 cmd_showtail = expand_showtail(xp);
3635 }
3636
3637 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3638 {
3639 beep_flush();
3640 return OK; /* Something illegal on command line */
3641 }
3642 if (xp->xp_context == EXPAND_NOTHING)
3643 {
3644 /* Caller can use the character as a normal char instead */
3645 return FAIL;
3646 }
3647
3648 MSG_PUTS("..."); /* show that we are busy */
3649 out_flush();
3650
3651 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003652 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653
3654 if (type == WILD_NEXT || type == WILD_PREV)
3655 {
3656 /*
3657 * Get next/previous match for a previous expanded pattern.
3658 */
3659 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3660 }
3661 else
3662 {
3663 /*
3664 * Translate string into pattern and expand it.
3665 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003666 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3667 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 p2 = NULL;
3669 else
3670 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003671 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003672 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3673 if (escape)
3674 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003675
3676 if (p_wic)
3677 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003678 p2 = ExpandOne(xp, p1,
3679 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003680 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003682 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 if (p2 != NULL && type == WILD_LONGEST)
3684 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003685 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 if (ccline.cmdbuff[i + j] == '*'
3687 || ccline.cmdbuff[i + j] == '?')
3688 break;
3689 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003690 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 }
3692 }
3693 }
3694
3695 if (p2 != NULL && !got_int)
3696 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003697 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003698 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003700 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 xp->xp_pattern = ccline.cmdbuff + i;
3702 }
3703 else
3704 v = OK;
3705 if (v == OK)
3706 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003707 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3708 &ccline.cmdbuff[ccline.cmdpos],
3709 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3710 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 ccline.cmdlen += difflen;
3712 ccline.cmdpos += difflen;
3713 }
3714 }
3715 vim_free(p2);
3716
3717 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003718 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /* When expanding a ":map" command and no matches are found, assume that
3721 * the key is supposed to be inserted literally */
3722 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3723 return FAIL;
3724
3725 if (xp->xp_numfiles <= 0 && p2 == NULL)
3726 beep_flush();
3727 else if (xp->xp_numfiles == 1)
3728 /* free expanded pattern */
3729 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3730
3731 return OK;
3732}
3733
3734/*
3735 * Do wildcard expansion on the string 'str'.
3736 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003737 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * Return NULL for failure.
3739 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003740 * "orig" is the originally expanded string, copied to allocated memory. It
3741 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3742 * WILD_PREV "orig" should be NULL.
3743 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003744 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3745 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 *
3747 * mode = WILD_FREE: just free previously expanded matches
3748 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3749 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3750 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3751 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3752 * mode = WILD_ALL: return all matches concatenated
3753 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003754 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 *
3756 * options = WILD_LIST_NOTFOUND: list entries without a match
3757 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3758 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3759 * options = WILD_NO_BEEP: Don't beep for multiple matches
3760 * options = WILD_ADD_SLASH: add a slash after directory names
3761 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3762 * options = WILD_SILENT: don't print warning messages
3763 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003764 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 *
3766 * The variables xp->xp_context and xp->xp_backslash must have been set!
3767 */
3768 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003769ExpandOne(
3770 expand_T *xp,
3771 char_u *str,
3772 char_u *orig, /* allocated copy of original of expanded string */
3773 int options,
3774 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
3776 char_u *ss = NULL;
3777 static int findex;
3778 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003779 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 int i;
3781 long_u len;
3782 int non_suf_match; /* number without matching suffix */
3783
3784 /*
3785 * first handle the case of using an old match
3786 */
3787 if (mode == WILD_NEXT || mode == WILD_PREV)
3788 {
3789 if (xp->xp_numfiles > 0)
3790 {
3791 if (mode == WILD_PREV)
3792 {
3793 if (findex == -1)
3794 findex = xp->xp_numfiles;
3795 --findex;
3796 }
3797 else /* mode == WILD_NEXT */
3798 ++findex;
3799
3800 /*
3801 * When wrapping around, return the original string, set findex to
3802 * -1.
3803 */
3804 if (findex < 0)
3805 {
3806 if (orig_save == NULL)
3807 findex = xp->xp_numfiles - 1;
3808 else
3809 findex = -1;
3810 }
3811 if (findex >= xp->xp_numfiles)
3812 {
3813 if (orig_save == NULL)
3814 findex = 0;
3815 else
3816 findex = -1;
3817 }
3818#ifdef FEAT_WILDMENU
3819 if (p_wmnu)
3820 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3821 findex, cmd_showtail);
3822#endif
3823 if (findex == -1)
3824 return vim_strsave(orig_save);
3825 return vim_strsave(xp->xp_files[findex]);
3826 }
3827 else
3828 return NULL;
3829 }
3830
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003831 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3833 {
3834 FreeWild(xp->xp_numfiles, xp->xp_files);
3835 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003836 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 }
3838 findex = 0;
3839
3840 if (mode == WILD_FREE) /* only release file name */
3841 return NULL;
3842
3843 if (xp->xp_numfiles == -1)
3844 {
3845 vim_free(orig_save);
3846 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003847 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848
3849 /*
3850 * Do the expansion.
3851 */
3852 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3853 options) == FAIL)
3854 {
3855#ifdef FNAME_ILLEGAL
3856 /* Illegal file name has been silently skipped. But when there
3857 * are wildcards, the real problem is that there was no match,
3858 * causing the pattern to be added, which has illegal characters.
3859 */
3860 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3861 EMSG2(_(e_nomatch2), str);
3862#endif
3863 }
3864 else if (xp->xp_numfiles == 0)
3865 {
3866 if (!(options & WILD_SILENT))
3867 EMSG2(_(e_nomatch2), str);
3868 }
3869 else
3870 {
3871 /* Escape the matches for use on the command line. */
3872 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3873
3874 /*
3875 * Check for matching suffixes in file names.
3876 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003877 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3878 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 {
3880 if (xp->xp_numfiles)
3881 non_suf_match = xp->xp_numfiles;
3882 else
3883 non_suf_match = 1;
3884 if ((xp->xp_context == EXPAND_FILES
3885 || xp->xp_context == EXPAND_DIRECTORIES)
3886 && xp->xp_numfiles > 1)
3887 {
3888 /*
3889 * More than one match; check suffix.
3890 * The files will have been sorted on matching suffix in
3891 * expand_wildcards, only need to check the first two.
3892 */
3893 non_suf_match = 0;
3894 for (i = 0; i < 2; ++i)
3895 if (match_suffix(xp->xp_files[i]))
3896 ++non_suf_match;
3897 }
3898 if (non_suf_match != 1)
3899 {
3900 /* Can we ever get here unless it's while expanding
3901 * interactively? If not, we can get rid of this all
3902 * together. Don't really want to wait for this message
3903 * (and possibly have to hit return to continue!).
3904 */
3905 if (!(options & WILD_SILENT))
3906 EMSG(_(e_toomany));
3907 else if (!(options & WILD_NO_BEEP))
3908 beep_flush();
3909 }
3910 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3911 ss = vim_strsave(xp->xp_files[0]);
3912 }
3913 }
3914 }
3915
3916 /* Find longest common part */
3917 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3918 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003919 int mb_len = 1;
3920 int c0, ci;
3921
3922 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003924#ifdef FEAT_MBYTE
3925 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003927 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3928 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3929 }
3930 else
3931#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003932 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003933 for (i = 1; i < xp->xp_numfiles; ++i)
3934 {
3935#ifdef FEAT_MBYTE
3936 if (has_mbyte)
3937 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3938 else
3939#endif
3940 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003941 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003943 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003944 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003946 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003949 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 break;
3951 }
3952 if (i < xp->xp_numfiles)
3953 {
3954 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003955 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957 }
3958 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003959
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 ss = alloc((unsigned)len + 1);
3961 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003962 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 findex = -1; /* next p_wc gets first one */
3964 }
3965
3966 /* Concatenate all matching names */
3967 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3968 {
3969 len = 0;
3970 for (i = 0; i < xp->xp_numfiles; ++i)
3971 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3972 ss = lalloc(len, TRUE);
3973 if (ss != NULL)
3974 {
3975 *ss = NUL;
3976 for (i = 0; i < xp->xp_numfiles; ++i)
3977 {
3978 STRCAT(ss, xp->xp_files[i]);
3979 if (i != xp->xp_numfiles - 1)
3980 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3981 }
3982 }
3983 }
3984
3985 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3986 ExpandCleanup(xp);
3987
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003988 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003989 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003990 vim_free(orig);
3991
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 return ss;
3993}
3994
3995/*
3996 * Prepare an expand structure for use.
3997 */
3998 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003999ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004001 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004002 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004004#ifndef BACKSLASH_IN_FILENAME
4005 xp->xp_shell = FALSE;
4006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 xp->xp_numfiles = -1;
4008 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004009#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4010 xp->xp_arg = NULL;
4011#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004012 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013}
4014
4015/*
4016 * Cleanup an expand structure after use.
4017 */
4018 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004019ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
4021 if (xp->xp_numfiles >= 0)
4022 {
4023 FreeWild(xp->xp_numfiles, xp->xp_files);
4024 xp->xp_numfiles = -1;
4025 }
4026}
4027
4028 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004029ExpandEscape(
4030 expand_T *xp,
4031 char_u *str,
4032 int numfiles,
4033 char_u **files,
4034 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035{
4036 int i;
4037 char_u *p;
4038
4039 /*
4040 * May change home directory back to "~"
4041 */
4042 if (options & WILD_HOME_REPLACE)
4043 tilde_replace(str, numfiles, files);
4044
4045 if (options & WILD_ESCAPE)
4046 {
4047 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004048 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004049 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004050 || xp->xp_context == EXPAND_BUFFERS
4051 || xp->xp_context == EXPAND_DIRECTORIES)
4052 {
4053 /*
4054 * Insert a backslash into a file name before a space, \, %, #
4055 * and wildmatch characters, except '~'.
4056 */
4057 for (i = 0; i < numfiles; ++i)
4058 {
4059 /* for ":set path=" we need to escape spaces twice */
4060 if (xp->xp_backslash == XP_BS_THREE)
4061 {
4062 p = vim_strsave_escaped(files[i], (char_u *)" ");
4063 if (p != NULL)
4064 {
4065 vim_free(files[i]);
4066 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004067#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 p = vim_strsave_escaped(files[i], (char_u *)" ");
4069 if (p != NULL)
4070 {
4071 vim_free(files[i]);
4072 files[i] = p;
4073 }
4074#endif
4075 }
4076 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004077#ifdef BACKSLASH_IN_FILENAME
4078 p = vim_strsave_fnameescape(files[i], FALSE);
4079#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004080 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004081#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 if (p != NULL)
4083 {
4084 vim_free(files[i]);
4085 files[i] = p;
4086 }
4087
4088 /* If 'str' starts with "\~", replace "~" at start of
4089 * files[i] with "\~". */
4090 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004091 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004094
4095 /* If the first file starts with a '+' escape it. Otherwise it
4096 * could be seen as "+cmd". */
4097 if (*files[0] == '+')
4098 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 else if (xp->xp_context == EXPAND_TAGS)
4101 {
4102 /*
4103 * Insert a backslash before characters in a tag name that
4104 * would terminate the ":tag" command.
4105 */
4106 for (i = 0; i < numfiles; ++i)
4107 {
4108 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4109 if (p != NULL)
4110 {
4111 vim_free(files[i]);
4112 files[i] = p;
4113 }
4114 }
4115 }
4116 }
4117}
4118
4119/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004120 * Escape special characters in "fname" for when used as a file name argument
4121 * after a Vim command, or, when "shell" is non-zero, a shell command.
4122 * Returns the result in allocated memory.
4123 */
4124 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004125vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004126{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004127 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004128#ifdef BACKSLASH_IN_FILENAME
4129 char_u buf[20];
4130 int j = 0;
4131
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004132 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004133 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004134 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004135 buf[j++] = *p;
4136 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004137 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004138#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004139 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4140 if (shell && csh_like_shell() && p != NULL)
4141 {
4142 char_u *s;
4143
4144 /* For csh and similar shells need to put two backslashes before '!'.
4145 * One is taken by Vim, one by the shell. */
4146 s = vim_strsave_escaped(p, (char_u *)"!");
4147 vim_free(p);
4148 p = s;
4149 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004150#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004151
4152 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4153 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004154 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004155 escape_fname(&p);
4156
4157 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004158}
4159
4160/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004161 * Put a backslash before the file name in "pp", which is in allocated memory.
4162 */
4163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004164escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004165{
4166 char_u *p;
4167
4168 p = alloc((unsigned)(STRLEN(*pp) + 2));
4169 if (p != NULL)
4170 {
4171 p[0] = '\\';
4172 STRCPY(p + 1, *pp);
4173 vim_free(*pp);
4174 *pp = p;
4175 }
4176}
4177
4178/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 * For each file name in files[num_files]:
4180 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4181 */
4182 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004183tilde_replace(
4184 char_u *orig_pat,
4185 int num_files,
4186 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187{
4188 int i;
4189 char_u *p;
4190
4191 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4192 {
4193 for (i = 0; i < num_files; ++i)
4194 {
4195 p = home_replace_save(NULL, files[i]);
4196 if (p != NULL)
4197 {
4198 vim_free(files[i]);
4199 files[i] = p;
4200 }
4201 }
4202 }
4203}
4204
4205/*
4206 * Show all matches for completion on the command line.
4207 * Returns EXPAND_NOTHING when the character that triggered expansion should
4208 * be inserted like a normal character.
4209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004211showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212{
4213#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4214 int num_files;
4215 char_u **files_found;
4216 int i, j, k;
4217 int maxlen;
4218 int lines;
4219 int columns;
4220 char_u *p;
4221 int lastlen;
4222 int attr;
4223 int showtail;
4224
4225 if (xp->xp_numfiles == -1)
4226 {
4227 set_expand_context(xp);
4228 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4229 &num_files, &files_found);
4230 showtail = expand_showtail(xp);
4231 if (i != EXPAND_OK)
4232 return i;
4233
4234 }
4235 else
4236 {
4237 num_files = xp->xp_numfiles;
4238 files_found = xp->xp_files;
4239 showtail = cmd_showtail;
4240 }
4241
4242#ifdef FEAT_WILDMENU
4243 if (!wildmenu)
4244 {
4245#endif
4246 msg_didany = FALSE; /* lines_left will be set */
4247 msg_start(); /* prepare for paging */
4248 msg_putchar('\n');
4249 out_flush();
4250 cmdline_row = msg_row;
4251 msg_didany = FALSE; /* lines_left will be set again */
4252 msg_start(); /* prepare for paging */
4253#ifdef FEAT_WILDMENU
4254 }
4255#endif
4256
4257 if (got_int)
4258 got_int = FALSE; /* only int. the completion, not the cmd line */
4259#ifdef FEAT_WILDMENU
4260 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004261 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262#endif
4263 else
4264 {
4265 /* find the length of the longest file name */
4266 maxlen = 0;
4267 for (i = 0; i < num_files; ++i)
4268 {
4269 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004270 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 || xp->xp_context == EXPAND_BUFFERS))
4272 {
4273 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4274 j = vim_strsize(NameBuff);
4275 }
4276 else
4277 j = vim_strsize(L_SHOWFILE(i));
4278 if (j > maxlen)
4279 maxlen = j;
4280 }
4281
4282 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4283 lines = num_files;
4284 else
4285 {
4286 /* compute the number of columns and lines for the listing */
4287 maxlen += 2; /* two spaces between file names */
4288 columns = ((int)Columns + 2) / maxlen;
4289 if (columns < 1)
4290 columns = 1;
4291 lines = (num_files + columns - 1) / columns;
4292 }
4293
Bram Moolenaar8820b482017-03-16 17:23:31 +01004294 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295
4296 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4297 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004298 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 msg_clr_eos();
4300 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004301 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303
4304 /* list the files line by line */
4305 for (i = 0; i < lines; ++i)
4306 {
4307 lastlen = 999;
4308 for (k = i; k < num_files; k += lines)
4309 {
4310 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4311 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004312 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 p = files_found[k] + STRLEN(files_found[k]) + 1;
4314 msg_advance(maxlen + 1);
4315 msg_puts(p);
4316 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004317 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 break;
4319 }
4320 for (j = maxlen - lastlen; --j >= 0; )
4321 msg_putchar(' ');
4322 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004323 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 || xp->xp_context == EXPAND_BUFFERS)
4325 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004326 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004327 if (xp->xp_numfiles != -1)
4328 {
4329 char_u *halved_slash;
4330 char_u *exp_path;
4331
4332 /* Expansion was done before and special characters
4333 * were escaped, need to halve backslashes. Also
4334 * $HOME has been replaced with ~/. */
4335 exp_path = expand_env_save_opt(files_found[k], TRUE);
4336 halved_slash = backslash_halve_save(
4337 exp_path != NULL ? exp_path : files_found[k]);
4338 j = mch_isdir(halved_slash != NULL ? halved_slash
4339 : files_found[k]);
4340 vim_free(exp_path);
4341 vim_free(halved_slash);
4342 }
4343 else
4344 /* Expansion was done here, file names are literal. */
4345 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 if (showtail)
4347 p = L_SHOWFILE(k);
4348 else
4349 {
4350 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4351 TRUE);
4352 p = NameBuff;
4353 }
4354 }
4355 else
4356 {
4357 j = FALSE;
4358 p = L_SHOWFILE(k);
4359 }
4360 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4361 }
4362 if (msg_col > 0) /* when not wrapped around */
4363 {
4364 msg_clr_eos();
4365 msg_putchar('\n');
4366 }
4367 out_flush(); /* show one line at a time */
4368 if (got_int)
4369 {
4370 got_int = FALSE;
4371 break;
4372 }
4373 }
4374
4375 /*
4376 * we redraw the command below the lines that we have just listed
4377 * This is a bit tricky, but it saves a lot of screen updating.
4378 */
4379 cmdline_row = msg_row; /* will put it back later */
4380 }
4381
4382 if (xp->xp_numfiles == -1)
4383 FreeWild(num_files, files_found);
4384
4385 return EXPAND_OK;
4386}
4387
4388/*
4389 * Private gettail for showmatches() (and win_redr_status_matches()):
4390 * Find tail of file name path, but ignore trailing "/".
4391 */
4392 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004393sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394{
4395 char_u *p;
4396 char_u *t = s;
4397 int had_sep = FALSE;
4398
4399 for (p = s; *p != NUL; )
4400 {
4401 if (vim_ispathsep(*p)
4402#ifdef BACKSLASH_IN_FILENAME
4403 && !rem_backslash(p)
4404#endif
4405 )
4406 had_sep = TRUE;
4407 else if (had_sep)
4408 {
4409 t = p;
4410 had_sep = FALSE;
4411 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004412 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 }
4414 return t;
4415}
4416
4417/*
4418 * Return TRUE if we only need to show the tail of completion matches.
4419 * When not completing file names or there is a wildcard in the path FALSE is
4420 * returned.
4421 */
4422 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004423expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424{
4425 char_u *s;
4426 char_u *end;
4427
4428 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004429 if (xp->xp_context != EXPAND_FILES
4430 && xp->xp_context != EXPAND_SHELLCMD
4431 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 return FALSE;
4433
4434 end = gettail(xp->xp_pattern);
4435 if (end == xp->xp_pattern) /* there is no path separator */
4436 return FALSE;
4437
4438 for (s = xp->xp_pattern; s < end; s++)
4439 {
4440 /* Skip escaped wildcards. Only when the backslash is not a path
4441 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4442 if (rem_backslash(s))
4443 ++s;
4444 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4445 return FALSE;
4446 }
4447 return TRUE;
4448}
4449
4450/*
4451 * Prepare a string for expansion.
4452 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004453 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 * When expanding other names: The string will be used with regcomp(). Copy
4455 * the name into allocated memory and prepend "^".
4456 */
4457 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004458addstar(
4459 char_u *fname,
4460 int len,
4461 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462{
4463 char_u *retval;
4464 int i, j;
4465 int new_len;
4466 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004467 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004469 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004470 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 && context != EXPAND_SHELLCMD
4472 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
4474 /*
4475 * Matching will be done internally (on something other than files).
4476 * So we convert the file-matching-type wildcards into our kind for
4477 * use with vim_regcomp(). First work out how long it will be:
4478 */
4479
4480 /* For help tags the translation is done in find_help_tags().
4481 * For a tag pattern starting with "/" no translation is needed. */
4482 if (context == EXPAND_HELP
4483 || context == EXPAND_COLORS
4484 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004485 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004486 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004487 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004488 || ((context == EXPAND_TAGS_LISTFILES
4489 || context == EXPAND_TAGS)
4490 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 retval = vim_strnsave(fname, len);
4492 else
4493 {
4494 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4495 for (i = 0; i < len; i++)
4496 {
4497 if (fname[i] == '*' || fname[i] == '~')
4498 new_len++; /* '*' needs to be replaced by ".*"
4499 '~' needs to be replaced by "\~" */
4500
4501 /* Buffer names are like file names. "." should be literal */
4502 if (context == EXPAND_BUFFERS && fname[i] == '.')
4503 new_len++; /* "." becomes "\." */
4504
4505 /* Custom expansion takes care of special things, match
4506 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004507 if ((context == EXPAND_USER_DEFINED
4508 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 new_len++; /* '\' becomes "\\" */
4510 }
4511 retval = alloc(new_len);
4512 if (retval != NULL)
4513 {
4514 retval[0] = '^';
4515 j = 1;
4516 for (i = 0; i < len; i++, j++)
4517 {
4518 /* Skip backslash. But why? At least keep it for custom
4519 * expansion. */
4520 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004521 && context != EXPAND_USER_LIST
4522 && fname[i] == '\\'
4523 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 break;
4525
4526 switch (fname[i])
4527 {
4528 case '*': retval[j++] = '.';
4529 break;
4530 case '~': retval[j++] = '\\';
4531 break;
4532 case '?': retval[j] = '.';
4533 continue;
4534 case '.': if (context == EXPAND_BUFFERS)
4535 retval[j++] = '\\';
4536 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004537 case '\\': if (context == EXPAND_USER_DEFINED
4538 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 retval[j++] = '\\';
4540 break;
4541 }
4542 retval[j] = fname[i];
4543 }
4544 retval[j] = NUL;
4545 }
4546 }
4547 }
4548 else
4549 {
4550 retval = alloc(len + 4);
4551 if (retval != NULL)
4552 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004553 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
4555 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004556 * Don't add a star to *, ~, ~user, $var or `cmd`.
4557 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 * ~ would be at the start of the file name, but not the tail.
4559 * $ could be anywhere in the tail.
4560 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004561 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 */
4563 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004564 ends_in_star = (len > 0 && retval[len - 1] == '*');
4565#ifndef BACKSLASH_IN_FILENAME
4566 for (i = len - 2; i >= 0; --i)
4567 {
4568 if (retval[i] != '\\')
4569 break;
4570 ends_in_star = !ends_in_star;
4571 }
4572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004574 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 && vim_strchr(tail, '$') == NULL
4576 && vim_strchr(retval, '`') == NULL)
4577 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004578 else if (len > 0 && retval[len - 1] == '$')
4579 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 retval[len] = NUL;
4581 }
4582 }
4583 return retval;
4584}
4585
4586/*
4587 * Must parse the command line so far to work out what context we are in.
4588 * Completion can then be done based on that context.
4589 * This routine sets the variables:
4590 * xp->xp_pattern The start of the pattern to be expanded within
4591 * the command line (ends at the cursor).
4592 * xp->xp_context The type of thing to expand. Will be one of:
4593 *
4594 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4595 * the command line, like an unknown command. Caller
4596 * should beep.
4597 * EXPAND_NOTHING Unrecognised context for completion, use char like
4598 * a normal char, rather than for completion. eg
4599 * :s/^I/
4600 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4601 * it.
4602 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4603 * EXPAND_FILES After command with XFILE set, or after setting
4604 * with P_EXPAND set. eg :e ^I, :w>>^I
4605 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4606 * when we know only directories are of interest. eg
4607 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004608 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4610 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4611 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4612 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4613 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4614 * EXPAND_EVENTS Complete event names
4615 * EXPAND_SYNTAX Complete :syntax command arguments
4616 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4617 * EXPAND_AUGROUP Complete autocommand group names
4618 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4619 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4620 * eg :unmap a^I , :cunab x^I
4621 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4622 * eg :call sub^I
4623 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4624 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4625 * names in expressions, eg :while s^I
4626 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004627 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 */
4629 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004632 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (ccline.cmdfirstc != ':'
4634#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004635 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004636 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637#endif
4638 )
4639 {
4640 xp->xp_context = EXPAND_NOTHING;
4641 return;
4642 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004643 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644}
4645
4646 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004647set_cmd_context(
4648 expand_T *xp,
4649 char_u *str, /* start of command line */
4650 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004651 int col, /* position of cursor */
4652 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
4654 int old_char = NUL;
4655 char_u *nextcomm;
4656
4657 /*
4658 * Avoid a UMR warning from Purify, only save the character if it has been
4659 * written before.
4660 */
4661 if (col < len)
4662 old_char = str[col];
4663 str[col] = NUL;
4664 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004665
4666#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004667 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004668 {
4669# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004670 /* pass CMD_SIZE because there is no real command */
4671 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004672# endif
4673 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004674 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004675 {
4676 xp->xp_context = ccline.xp_context;
4677 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004678# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004679 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004680# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004681 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004682 else
4683#endif
4684 while (nextcomm != NULL)
4685 nextcomm = set_one_cmd_context(xp, nextcomm);
4686
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004687 /* Store the string here so that call_user_expand_func() can get to them
4688 * easily. */
4689 xp->xp_line = str;
4690 xp->xp_col = col;
4691
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 str[col] = old_char;
4693}
4694
4695/*
4696 * Expand the command line "str" from context "xp".
4697 * "xp" must have been set by set_cmd_context().
4698 * xp->xp_pattern points into "str", to where the text that is to be expanded
4699 * starts.
4700 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4701 * cursor.
4702 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4703 * key that triggered expansion literally.
4704 * Returns EXPAND_OK otherwise.
4705 */
4706 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004707expand_cmdline(
4708 expand_T *xp,
4709 char_u *str, /* start of command line */
4710 int col, /* position of cursor */
4711 int *matchcount, /* return: nr of matches */
4712 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
4714 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004715 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
4717 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4718 {
4719 beep_flush();
4720 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4721 }
4722 if (xp->xp_context == EXPAND_NOTHING)
4723 {
4724 /* Caller can use the character as a normal char instead */
4725 return EXPAND_NOTHING;
4726 }
4727
4728 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004729 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4730 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (file_str == NULL)
4732 return EXPAND_UNSUCCESSFUL;
4733
Bram Moolenaar94950a92010-12-02 16:01:29 +01004734 if (p_wic)
4735 options += WILD_ICASE;
4736
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004738 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 *matchcount = 0;
4741 *matches = NULL;
4742 }
4743 vim_free(file_str);
4744
4745 return EXPAND_OK;
4746}
4747
4748#ifdef FEAT_MULTI_LANG
4749/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004750 * Cleanup matches for help tags:
4751 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4752 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004754static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004757cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758{
4759 int i, j;
4760 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004761 char_u buf[4];
4762 char_u *p = buf;
4763
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004764 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004765 {
4766 *p++ = '@';
4767 *p++ = p_hlg[0];
4768 *p++ = p_hlg[1];
4769 }
4770 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771
4772 for (i = 0; i < num_file; ++i)
4773 {
4774 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004775 if (len <= 0)
4776 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004777 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 /* Sorting on priority means the same item in another language may
4780 * be anywhere. Search all items for a match up to the "@en". */
4781 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004782 if (j != i && (int)STRLEN(file[j]) == len + 3
4783 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 break;
4785 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004786 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 file[i][len] = NUL;
4788 }
4789 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004790
4791 if (*buf != NUL)
4792 for (i = 0; i < num_file; ++i)
4793 {
4794 len = (int)STRLEN(file[i]) - 3;
4795 if (len <= 0)
4796 continue;
4797 if (STRCMP(file[i] + len, buf) == 0)
4798 {
4799 /* remove the default language */
4800 file[i][len] = NUL;
4801 }
4802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803}
4804#endif
4805
4806/*
4807 * Do the expansion based on xp->xp_context and "pat".
4808 */
4809 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004810ExpandFromContext(
4811 expand_T *xp,
4812 char_u *pat,
4813 int *num_file,
4814 char_u ***file,
4815 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816{
4817#ifdef FEAT_CMDL_COMPL
4818 regmatch_T regmatch;
4819#endif
4820 int ret;
4821 int flags;
4822
4823 flags = EW_DIR; /* include directories */
4824 if (options & WILD_LIST_NOTFOUND)
4825 flags |= EW_NOTFOUND;
4826 if (options & WILD_ADD_SLASH)
4827 flags |= EW_ADDSLASH;
4828 if (options & WILD_KEEP_ALL)
4829 flags |= EW_KEEPALL;
4830 if (options & WILD_SILENT)
4831 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004832 if (options & WILD_ALLLINKS)
4833 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004835 if (xp->xp_context == EXPAND_FILES
4836 || xp->xp_context == EXPAND_DIRECTORIES
4837 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 {
4839 /*
4840 * Expand file or directory names.
4841 */
4842 int free_pat = FALSE;
4843 int i;
4844
4845 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4846 * space */
4847 if (xp->xp_backslash != XP_BS_NONE)
4848 {
4849 free_pat = TRUE;
4850 pat = vim_strsave(pat);
4851 for (i = 0; pat[i]; ++i)
4852 if (pat[i] == '\\')
4853 {
4854 if (xp->xp_backslash == XP_BS_THREE
4855 && pat[i + 1] == '\\'
4856 && pat[i + 2] == '\\'
4857 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004858 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (xp->xp_backslash == XP_BS_ONE
4860 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004861 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
4863 }
4864
4865 if (xp->xp_context == EXPAND_FILES)
4866 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004867 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4868 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 else
4870 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004871 if (options & WILD_ICASE)
4872 flags |= EW_ICASE;
4873
Bram Moolenaard7834d32009-12-02 16:14:36 +00004874 /* Expand wildcards, supporting %:h and the like. */
4875 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 if (free_pat)
4877 vim_free(pat);
4878 return ret;
4879 }
4880
4881 *file = (char_u **)"";
4882 *num_file = 0;
4883 if (xp->xp_context == EXPAND_HELP)
4884 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004885 /* With an empty argument we would get all the help tags, which is
4886 * very slow. Get matches for "help" instead. */
4887 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4888 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
4890#ifdef FEAT_MULTI_LANG
4891 cleanup_help_tags(*num_file, *file);
4892#endif
4893 return OK;
4894 }
4895 return FAIL;
4896 }
4897
4898#ifndef FEAT_CMDL_COMPL
4899 return FAIL;
4900#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004901 if (xp->xp_context == EXPAND_SHELLCMD)
4902 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 if (xp->xp_context == EXPAND_OLD_SETTING)
4904 return ExpandOldSetting(num_file, file);
4905 if (xp->xp_context == EXPAND_BUFFERS)
4906 return ExpandBufnames(pat, num_file, file, options);
4907 if (xp->xp_context == EXPAND_TAGS
4908 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4909 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4910 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004911 {
4912 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004913 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4914 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004917 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004918 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004919 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004920 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004921 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004922 {
4923 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004924 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004925 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004926 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004927 {
4928 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004929 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004930 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004931# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4932 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004933 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004934# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004935 if (xp->xp_context == EXPAND_PACKADD)
4936 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937
4938 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4939 if (regmatch.regprog == NULL)
4940 return FAIL;
4941
4942 /* set ignore-case according to p_ic, p_scs and pat */
4943 regmatch.rm_ic = ignorecase(pat);
4944
4945 if (xp->xp_context == EXPAND_SETTINGS
4946 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4947 ret = ExpandSettings(xp, &regmatch, num_file, file);
4948 else if (xp->xp_context == EXPAND_MAPPINGS)
4949 ret = ExpandMappings(&regmatch, num_file, file);
4950# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4951 else if (xp->xp_context == EXPAND_USER_DEFINED)
4952 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4953# endif
4954 else
4955 {
4956 static struct expgen
4957 {
4958 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004959 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004961 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 } tab[] =
4963 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004964 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4965 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004966 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004967 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004968#ifdef FEAT_CMDHIST
4969 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004972 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004973 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004974 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4975 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4976 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977#endif
4978#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004979 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4980 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4981 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4982 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#endif
4984#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004985 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4986 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987#endif
4988#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004989 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004991#ifdef FEAT_PROFILE
4992 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4993#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004994 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004995 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4996 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004997#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004998 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004999#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005000#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005001 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005002#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005003#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005004 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005005#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5007 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005008 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5009 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005011 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005012 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005013 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 };
5015 int i;
5016
5017 /*
5018 * Find a context in the table and call the ExpandGeneric() with the
5019 * right function to do the expansion.
5020 */
5021 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005022 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 if (xp->xp_context == tab[i].context)
5024 {
5025 if (tab[i].ic)
5026 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005027 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005028 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 break;
5030 }
5031 }
5032
Bram Moolenaar473de612013-06-08 18:19:48 +02005033 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 return ret;
5036#endif /* FEAT_CMDL_COMPL */
5037}
5038
5039#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5040/*
5041 * Expand a list of names.
5042 *
5043 * Generic function for command line completion. It calls a function to
5044 * obtain strings, one by one. The strings are matched against a regexp
5045 * program. Matching strings are copied into an array, which is returned.
5046 *
5047 * Returns OK when no problems encountered, FAIL for error (out of memory).
5048 */
5049 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005050ExpandGeneric(
5051 expand_T *xp,
5052 regmatch_T *regmatch,
5053 int *num_file,
5054 char_u ***file,
5055 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005057 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
5059 int i;
5060 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005061 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 char_u *str;
5063
5064 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005065 * round == 0: count the number of matching names
5066 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005068 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 for (i = 0; ; ++i)
5071 {
5072 str = (*func)(xp, i);
5073 if (str == NULL) /* end of list */
5074 break;
5075 if (*str == NUL) /* skip empty strings */
5076 continue;
5077
5078 if (vim_regexec(regmatch, str, (colnr_T)0))
5079 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005080 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005082 if (escaped)
5083 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5084 else
5085 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 (*file)[count] = str;
5087#ifdef FEAT_MENU
5088 if (func == get_menu_names && str != NULL)
5089 {
5090 /* test for separator added by get_menu_names() */
5091 str += STRLEN(str) - 1;
5092 if (*str == '\001')
5093 *str = '.';
5094 }
5095#endif
5096 }
5097 ++count;
5098 }
5099 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005100 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
5102 if (count == 0)
5103 return OK;
5104 *num_file = count;
5105 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5106 if (*file == NULL)
5107 {
5108 *file = (char_u **)"";
5109 return FAIL;
5110 }
5111 count = 0;
5112 }
5113 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005114
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005115 /* Sort the results. Keep menu's in the specified order. */
5116 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005117 {
5118 if (xp->xp_context == EXPAND_EXPRESSION
5119 || xp->xp_context == EXPAND_FUNCTIONS
5120 || xp->xp_context == EXPAND_USER_FUNC)
5121 /* <SNR> functions should be sorted to the end. */
5122 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5123 sort_func_compare);
5124 else
5125 sort_strings(*file, *num_file);
5126 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005127
Bram Moolenaar4f688582007-07-24 12:34:30 +00005128#ifdef FEAT_CMDL_COMPL
5129 /* Reset the variables used for special highlight names expansion, so that
5130 * they don't show up when getting normal highlight names by ID. */
5131 reset_expand_highlight();
5132#endif
5133
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 return OK;
5135}
5136
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005137/*
5138 * Complete a shell command.
5139 * Returns FAIL or OK;
5140 */
5141 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005142expand_shellcmd(
5143 char_u *filepat, /* pattern to match with command names */
5144 int *num_file, /* return: number of matches */
5145 char_u ***file, /* return: array with matches */
5146 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005147{
5148 char_u *pat;
5149 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005150 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005151 int mustfree = FALSE;
5152 garray_T ga;
5153 char_u *buf = alloc(MAXPATHL);
5154 size_t l;
5155 char_u *s, *e;
5156 int flags = flagsarg;
5157 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005158 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005159 hashtab_T found_ht;
5160 hashitem_T *hi;
5161 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005162
5163 if (buf == NULL)
5164 return FAIL;
5165
5166 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5167 * space */
5168 pat = vim_strsave(filepat);
5169 for (i = 0; pat[i]; ++i)
5170 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005171 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005172
Bram Moolenaarb5971142015-03-21 17:32:19 +01005173 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005174
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005175 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5176 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005177 path = (char_u *)".";
5178 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005179 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005180 /* For an absolute name we don't use $PATH. */
5181 if (!mch_isFullName(pat))
5182 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005183 if (path == NULL)
5184 path = (char_u *)"";
5185 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005186
5187 /*
5188 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005189 * collect them in "ga". When "." is not in $PATH also expand for the
5190 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005191 */
5192 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005193 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005194 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005195 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005196 if (*s == NUL)
5197 {
5198 if (did_curdir)
5199 break;
5200 /* Find directories in the current directory, path is empty. */
5201 did_curdir = TRUE;
5202 }
5203 else if (*s == '.')
5204 did_curdir = TRUE;
5205
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005206#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005207 e = vim_strchr(s, ';');
5208#else
5209 e = vim_strchr(s, ':');
5210#endif
5211 if (e == NULL)
5212 e = s + STRLEN(s);
5213
5214 l = e - s;
5215 if (l > MAXPATHL - 5)
5216 break;
5217 vim_strncpy(buf, s, l);
5218 add_pathsep(buf);
5219 l = STRLEN(buf);
5220 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5221
5222 /* Expand matches in one directory of $PATH. */
5223 ret = expand_wildcards(1, &buf, num_file, file, flags);
5224 if (ret == OK)
5225 {
5226 if (ga_grow(&ga, *num_file) == FAIL)
5227 FreeWild(*num_file, *file);
5228 else
5229 {
5230 for (i = 0; i < *num_file; ++i)
5231 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005232 char_u *name = (*file)[i];
5233
5234 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005235 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005236 // Check if this name was already found.
5237 hash = hash_hash(name + l);
5238 hi = hash_lookup(&found_ht, name + l, hash);
5239 if (HASHITEM_EMPTY(hi))
5240 {
5241 // Remove the path that was prepended.
5242 STRMOVE(name, name + l);
5243 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5244 hash_add_item(&found_ht, hi, name, hash);
5245 name = NULL;
5246 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005247 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005248 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005249 }
5250 vim_free(*file);
5251 }
5252 }
5253 if (*e != NUL)
5254 ++e;
5255 }
5256 *file = ga.ga_data;
5257 *num_file = ga.ga_len;
5258
5259 vim_free(buf);
5260 vim_free(pat);
5261 if (mustfree)
5262 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005263 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005264 return OK;
5265}
5266
5267
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaarffa96842018-06-12 22:05:14 +02005269static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, typval_T *, int), expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005270
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005272 * Call "user_expand_func()" to invoke a user defined Vim script function and
5273 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005275 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005276call_user_expand_func(
Bram Moolenaarffa96842018-06-12 22:05:14 +02005277 void *(*user_expand_func)(char_u *, int, typval_T *, int),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005278 expand_T *xp,
5279 int *num_file,
5280 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005282 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005283 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005285 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005286 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005287 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288
Bram Moolenaarb7515462013-06-29 12:58:33 +02005289 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005290 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 *num_file = 0;
5292 *file = NULL;
5293
Bram Moolenaarb7515462013-06-29 12:58:33 +02005294 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005295 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005296 keep = ccline.cmdbuff[ccline.cmdlen];
5297 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005298 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005299
Bram Moolenaarffa96842018-06-12 22:05:14 +02005300 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5301
5302 args[0].v_type = VAR_STRING;
5303 args[0].vval.v_string = pat;
5304 args[1].v_type = VAR_STRING;
5305 args[1].vval.v_string = xp->xp_line;
5306 args[2].v_type = VAR_NUMBER;
5307 args[2].vval.v_number = xp->xp_col;
5308 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005310 /* Save the cmdline, we don't know what the function may do. */
5311 save_ccline = ccline;
5312 ccline.cmdbuff = NULL;
5313 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005315
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005316 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005317
5318 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005320 if (ccline.cmdbuff != NULL)
5321 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005322
Bram Moolenaarffa96842018-06-12 22:05:14 +02005323 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005324 return ret;
5325}
5326
5327/*
5328 * Expand names with a function defined by the user.
5329 */
5330 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005331ExpandUserDefined(
5332 expand_T *xp,
5333 regmatch_T *regmatch,
5334 int *num_file,
5335 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005336{
5337 char_u *retstr;
5338 char_u *s;
5339 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005340 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005341 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005342 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005343
5344 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5345 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 return FAIL;
5347
5348 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005349 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
5351 e = vim_strchr(s, '\n');
5352 if (e == NULL)
5353 e = s + STRLEN(s);
5354 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005355 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005357 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5358 *e = keep;
5359
5360 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005362 if (ga_grow(&ga, 1) == FAIL)
5363 break;
5364 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5365 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 if (*e != NUL)
5369 ++e;
5370 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005371 vim_free(retstr);
5372 *file = ga.ga_data;
5373 *num_file = ga.ga_len;
5374 return OK;
5375}
5376
5377/*
5378 * Expand names with a list returned by a function defined by the user.
5379 */
5380 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005381ExpandUserList(
5382 expand_T *xp,
5383 int *num_file,
5384 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005385{
5386 list_T *retlist;
5387 listitem_T *li;
5388 garray_T ga;
5389
5390 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5391 if (retlist == NULL)
5392 return FAIL;
5393
5394 ga_init2(&ga, (int)sizeof(char *), 3);
5395 /* Loop over the items in the list. */
5396 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5397 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005398 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5399 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005400
5401 if (ga_grow(&ga, 1) == FAIL)
5402 break;
5403
5404 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005405 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005406 ++ga.ga_len;
5407 }
5408 list_unref(retlist);
5409
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 *file = ga.ga_data;
5411 *num_file = ga.ga_len;
5412 return OK;
5413}
5414#endif
5415
5416/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005417 * Expand color scheme, compiler or filetype names.
5418 * Search from 'runtimepath':
5419 * 'runtimepath'/{dirnames}/{pat}.vim
5420 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5421 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5422 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5423 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005424 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 */
5426 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005427ExpandRTDir(
5428 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005429 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005430 int *num_file,
5431 char_u ***file,
5432 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 char_u *s;
5435 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005436 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005438 int i;
5439 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 *num_file = 0;
5442 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005443 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005444 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005446 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005448 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5449 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005451 ga_clear_strings(&ga);
5452 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005454 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005455 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005456 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005458
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005459 if (flags & DIP_START) {
5460 for (i = 0; dirnames[i] != NULL; ++i)
5461 {
5462 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5463 if (s == NULL)
5464 {
5465 ga_clear_strings(&ga);
5466 return FAIL;
5467 }
5468 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5469 globpath(p_pp, s, &ga, 0);
5470 vim_free(s);
5471 }
5472 }
5473
5474 if (flags & DIP_OPT) {
5475 for (i = 0; dirnames[i] != NULL; ++i)
5476 {
5477 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5478 if (s == NULL)
5479 {
5480 ga_clear_strings(&ga);
5481 return FAIL;
5482 }
5483 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5484 globpath(p_pp, s, &ga, 0);
5485 vim_free(s);
5486 }
5487 }
5488
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005489 for (i = 0; i < ga.ga_len; ++i)
5490 {
5491 match = ((char_u **)ga.ga_data)[i];
5492 s = match;
5493 e = s + STRLEN(s);
5494 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5495 {
5496 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005497 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005498 if (s < match || vim_ispathsep(*s))
5499 break;
5500 ++s;
5501 *e = NUL;
5502 mch_memmove(match, s, e - s + 1);
5503 }
5504 }
5505
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005506 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005507 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005508
5509 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005510 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005511 remove_duplicates(&ga);
5512
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 *file = ga.ga_data;
5514 *num_file = ga.ga_len;
5515 return OK;
5516}
5517
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005518/*
5519 * Expand loadplugin names:
5520 * 'packpath'/pack/ * /opt/{pat}
5521 */
5522 static int
5523ExpandPackAddDir(
5524 char_u *pat,
5525 int *num_file,
5526 char_u ***file)
5527{
5528 char_u *s;
5529 char_u *e;
5530 char_u *match;
5531 garray_T ga;
5532 int i;
5533 int pat_len;
5534
5535 *num_file = 0;
5536 *file = NULL;
5537 pat_len = (int)STRLEN(pat);
5538 ga_init2(&ga, (int)sizeof(char *), 10);
5539
5540 s = alloc((unsigned)(pat_len + 26));
5541 if (s == NULL)
5542 {
5543 ga_clear_strings(&ga);
5544 return FAIL;
5545 }
5546 sprintf((char *)s, "pack/*/opt/%s*", pat);
5547 globpath(p_pp, s, &ga, 0);
5548 vim_free(s);
5549
5550 for (i = 0; i < ga.ga_len; ++i)
5551 {
5552 match = ((char_u **)ga.ga_data)[i];
5553 s = gettail(match);
5554 e = s + STRLEN(s);
5555 mch_memmove(match, s, e - s + 1);
5556 }
5557
5558 if (ga.ga_len == 0)
5559 return FAIL;
5560
5561 /* Sort and remove duplicates which can happen when specifying multiple
5562 * directories in dirnames. */
5563 remove_duplicates(&ga);
5564
5565 *file = ga.ga_data;
5566 *num_file = ga.ga_len;
5567 return OK;
5568}
5569
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570#endif
5571
5572#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5573/*
5574 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005575 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005577 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005578globpath(
5579 char_u *path,
5580 char_u *file,
5581 garray_T *ga,
5582 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583{
5584 expand_T xpc;
5585 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 int num_p;
5588 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
5590 buf = alloc(MAXPATHL);
5591 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005592 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005594 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 /* Loop over all entries in {path}. */
5598 while (*path != NUL)
5599 {
5600 /* Copy one item of the path to buf[] and concatenate the file name. */
5601 copy_option_part(&path, buf, MAXPATHL, ",");
5602 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5603 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005604# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005605 /* Using the platform's path separator (\) makes vim incorrectly
5606 * treat it as an escape character, use '/' instead. */
5607 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5608 STRCAT(buf, "/");
5609# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005611# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005613 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5614 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005616 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005618 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 for (i = 0; i < num_p; ++i)
5621 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005622 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005623 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005624 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005627
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 FreeWild(num_p, p);
5629 }
5630 }
5631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632
5633 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634}
5635
5636#endif
5637
5638#if defined(FEAT_CMDHIST) || defined(PROTO)
5639
5640/*********************************
5641 * Command line history stuff *
5642 *********************************/
5643
5644/*
5645 * Translate a history character to the associated type number.
5646 */
5647 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005648hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649{
5650 if (c == ':')
5651 return HIST_CMD;
5652 if (c == '=')
5653 return HIST_EXPR;
5654 if (c == '@')
5655 return HIST_INPUT;
5656 if (c == '>')
5657 return HIST_DEBUG;
5658 return HIST_SEARCH; /* must be '?' or '/' */
5659}
5660
5661/*
5662 * Table of history names.
5663 * These names are used in :history and various hist...() functions.
5664 * It is sufficient to give the significant prefix of a history name.
5665 */
5666
5667static char *(history_names[]) =
5668{
5669 "cmd",
5670 "search",
5671 "expr",
5672 "input",
5673 "debug",
5674 NULL
5675};
5676
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005677#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5678/*
5679 * Function given to ExpandGeneric() to obtain the possible first
5680 * arguments of the ":history command.
5681 */
5682 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005684{
5685 static char_u compl[2] = { NUL, NUL };
5686 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005687 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005688 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5689
5690 if (idx < short_names_count)
5691 {
5692 compl[0] = (char_u)short_names[idx];
5693 return compl;
5694 }
5695 if (idx < short_names_count + history_name_count)
5696 return (char_u *)history_names[idx - short_names_count];
5697 if (idx == short_names_count + history_name_count)
5698 return (char_u *)"all";
5699 return NULL;
5700}
5701#endif
5702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703/*
5704 * init_history() - Initialize the command line history.
5705 * Also used to re-allocate the history when the size changes.
5706 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005707 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005708init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 int newlen; /* new length of history table */
5711 histentry_T *temp;
5712 int i;
5713 int j;
5714 int type;
5715
5716 /*
5717 * If size of history table changed, reallocate it
5718 */
5719 newlen = (int)p_hi;
5720 if (newlen != hislen) /* history length changed */
5721 {
5722 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5723 {
5724 if (newlen)
5725 {
5726 temp = (histentry_T *)lalloc(
5727 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5728 if (temp == NULL) /* out of memory! */
5729 {
5730 if (type == 0) /* first one: just keep the old length */
5731 {
5732 newlen = hislen;
5733 break;
5734 }
5735 /* Already changed one table, now we can only have zero
5736 * length for all tables. */
5737 newlen = 0;
5738 type = -1;
5739 continue;
5740 }
5741 }
5742 else
5743 temp = NULL;
5744 if (newlen == 0 || temp != NULL)
5745 {
5746 if (hisidx[type] < 0) /* there are no entries yet */
5747 {
5748 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005749 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 }
5751 else if (newlen > hislen) /* array becomes bigger */
5752 {
5753 for (i = 0; i <= hisidx[type]; ++i)
5754 temp[i] = history[type][i];
5755 j = i;
5756 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005757 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 for ( ; j < hislen; ++i, ++j)
5759 temp[i] = history[type][j];
5760 }
5761 else /* array becomes smaller or 0 */
5762 {
5763 j = hisidx[type];
5764 for (i = newlen - 1; ; --i)
5765 {
5766 if (i >= 0) /* copy newest entries */
5767 temp[i] = history[type][j];
5768 else /* remove older entries */
5769 vim_free(history[type][j].hisstr);
5770 if (--j < 0)
5771 j = hislen - 1;
5772 if (j == hisidx[type])
5773 break;
5774 }
5775 hisidx[type] = newlen - 1;
5776 }
5777 vim_free(history[type]);
5778 history[type] = temp;
5779 }
5780 }
5781 hislen = newlen;
5782 }
5783}
5784
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005785 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005786clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005787{
5788 hisptr->hisnum = 0;
5789 hisptr->viminfo = FALSE;
5790 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005791 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005792}
5793
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794/*
5795 * Check if command line 'str' is already in history.
5796 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5797 */
5798 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005799in_history(
5800 int type,
5801 char_u *str,
5802 int move_to_front, /* Move the entry to the front if it exists */
5803 int sep,
5804 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805{
5806 int i;
5807 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005808 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809
5810 if (hisidx[type] < 0)
5811 return FALSE;
5812 i = hisidx[type];
5813 do
5814 {
5815 if (history[type][i].hisstr == NULL)
5816 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005817
5818 /* For search history, check that the separator character matches as
5819 * well. */
5820 p = history[type][i].hisstr;
5821 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005822 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005823 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 {
5825 if (!move_to_front)
5826 return TRUE;
5827 last_i = i;
5828 break;
5829 }
5830 if (--i < 0)
5831 i = hislen - 1;
5832 } while (i != hisidx[type]);
5833
5834 if (last_i >= 0)
5835 {
5836 str = history[type][i].hisstr;
5837 while (i != hisidx[type])
5838 {
5839 if (++i >= hislen)
5840 i = 0;
5841 history[type][last_i] = history[type][i];
5842 last_i = i;
5843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005845 history[type][i].viminfo = FALSE;
5846 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005847 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 return TRUE;
5849 }
5850 return FALSE;
5851}
5852
5853/*
5854 * Convert history name (from table above) to its HIST_ equivalent.
5855 * When "name" is empty, return "cmd" history.
5856 * Returns -1 for unknown history name.
5857 */
5858 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005859get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 int i;
5862 int len = (int)STRLEN(name);
5863
5864 /* No argument: use current history. */
5865 if (len == 0)
5866 return hist_char2type(ccline.cmdfirstc);
5867
5868 for (i = 0; history_names[i] != NULL; ++i)
5869 if (STRNICMP(name, history_names[i], len) == 0)
5870 return i;
5871
5872 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5873 return hist_char2type(name[0]);
5874
5875 return -1;
5876}
5877
5878static int last_maptick = -1; /* last seen maptick */
5879
5880/*
5881 * Add the given string to the given history. If the string is already in the
5882 * history then it is moved to the front. "histype" may be one of he HIST_
5883 * values.
5884 */
5885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005886add_to_history(
5887 int histype,
5888 char_u *new_entry,
5889 int in_map, /* consider maptick when inside a mapping */
5890 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 histentry_T *hisptr;
5893 int len;
5894
5895 if (hislen == 0) /* no history */
5896 return;
5897
Bram Moolenaara939e432013-11-09 05:30:26 +01005898 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5899 return;
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 /*
5902 * Searches inside the same mapping overwrite each other, so that only
5903 * the last line is kept. Be careful not to remove a line that was moved
5904 * down, only lines that were added.
5905 */
5906 if (histype == HIST_SEARCH && in_map)
5907 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005908 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 {
5910 /* Current line is from the same mapping, remove it */
5911 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5912 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005913 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 --hisnum[histype];
5915 if (--hisidx[HIST_SEARCH] < 0)
5916 hisidx[HIST_SEARCH] = hislen - 1;
5917 }
5918 last_maptick = -1;
5919 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005920 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 {
5922 if (++hisidx[histype] == hislen)
5923 hisidx[histype] = 0;
5924 hisptr = &history[histype][hisidx[histype]];
5925 vim_free(hisptr->hisstr);
5926
5927 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005928 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5930 if (hisptr->hisstr != NULL)
5931 hisptr->hisstr[len + 1] = sep;
5932
5933 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005934 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005935 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 if (histype == HIST_SEARCH && in_map)
5937 last_maptick = maptick;
5938 }
5939}
5940
5941#if defined(FEAT_EVAL) || defined(PROTO)
5942
5943/*
5944 * Get identifier of newest history entry.
5945 * "histype" may be one of the HIST_ values.
5946 */
5947 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005948get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949{
5950 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5951 || hisidx[histype] < 0)
5952 return -1;
5953
5954 return history[histype][hisidx[histype]].hisnum;
5955}
5956
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005958 * Calculate history index from a number:
5959 * num > 0: seen as identifying number of a history entry
5960 * num < 0: relative position in history wrt newest entry
5961 * "histype" may be one of the HIST_ values.
5962 */
5963 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005964calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965{
5966 int i;
5967 histentry_T *hist;
5968 int wrapped = FALSE;
5969
5970 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5971 || (i = hisidx[histype]) < 0 || num == 0)
5972 return -1;
5973
5974 hist = history[histype];
5975 if (num > 0)
5976 {
5977 while (hist[i].hisnum > num)
5978 if (--i < 0)
5979 {
5980 if (wrapped)
5981 break;
5982 i += hislen;
5983 wrapped = TRUE;
5984 }
5985 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5986 return i;
5987 }
5988 else if (-num <= hislen)
5989 {
5990 i += num + 1;
5991 if (i < 0)
5992 i += hislen;
5993 if (hist[i].hisstr != NULL)
5994 return i;
5995 }
5996 return -1;
5997}
5998
5999/*
6000 * Get a history entry by its index.
6001 * "histype" may be one of the HIST_ values.
6002 */
6003 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006004get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 idx = calc_hist_idx(histype, idx);
6007 if (idx >= 0)
6008 return history[histype][idx].hisstr;
6009 else
6010 return (char_u *)"";
6011}
6012
6013/*
6014 * Clear all entries of a history.
6015 * "histype" may be one of the HIST_ values.
6016 */
6017 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
6020 int i;
6021 histentry_T *hisptr;
6022
6023 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6024 {
6025 hisptr = history[histype];
6026 for (i = hislen; i--;)
6027 {
6028 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006029 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006030 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 }
6032 hisidx[histype] = -1; /* mark history as cleared */
6033 hisnum[histype] = 0; /* reset identifier counter */
6034 return OK;
6035 }
6036 return FAIL;
6037}
6038
6039/*
6040 * Remove all entries matching {str} from a history.
6041 * "histype" may be one of the HIST_ values.
6042 */
6043 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006044del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 regmatch_T regmatch;
6047 histentry_T *hisptr;
6048 int idx;
6049 int i;
6050 int last;
6051 int found = FALSE;
6052
6053 regmatch.regprog = NULL;
6054 regmatch.rm_ic = FALSE; /* always match case */
6055 if (hislen != 0
6056 && histype >= 0
6057 && histype < HIST_COUNT
6058 && *str != NUL
6059 && (idx = hisidx[histype]) >= 0
6060 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6061 != NULL)
6062 {
6063 i = last = idx;
6064 do
6065 {
6066 hisptr = &history[histype][i];
6067 if (hisptr->hisstr == NULL)
6068 break;
6069 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6070 {
6071 found = TRUE;
6072 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006073 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 }
6075 else
6076 {
6077 if (i != last)
6078 {
6079 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006080 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 }
6082 if (--last < 0)
6083 last += hislen;
6084 }
6085 if (--i < 0)
6086 i += hislen;
6087 } while (i != idx);
6088 if (history[histype][idx].hisstr == NULL)
6089 hisidx[histype] = -1;
6090 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006091 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 return found;
6093}
6094
6095/*
6096 * Remove an indexed entry from a history.
6097 * "histype" may be one of the HIST_ values.
6098 */
6099 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006100del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 int i, j;
6103
6104 i = calc_hist_idx(histype, idx);
6105 if (i < 0)
6106 return FALSE;
6107 idx = hisidx[histype];
6108 vim_free(history[histype][i].hisstr);
6109
6110 /* When deleting the last added search string in a mapping, reset
6111 * last_maptick, so that the last added search string isn't deleted again.
6112 */
6113 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6114 last_maptick = -1;
6115
6116 while (i != idx)
6117 {
6118 j = (i + 1) % hislen;
6119 history[histype][i] = history[histype][j];
6120 i = j;
6121 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006122 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 if (--i < 0)
6124 i += hislen;
6125 hisidx[histype] = i;
6126 return TRUE;
6127}
6128
6129#endif /* FEAT_EVAL */
6130
6131#if defined(FEAT_CRYPT) || defined(PROTO)
6132/*
6133 * Very specific function to remove the value in ":set key=val" from the
6134 * history.
6135 */
6136 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006137remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138{
6139 char_u *p;
6140 int i;
6141
6142 i = hisidx[HIST_CMD];
6143 if (i < 0)
6144 return;
6145 p = history[HIST_CMD][i].hisstr;
6146 if (p != NULL)
6147 for ( ; *p; ++p)
6148 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6149 {
6150 p = vim_strchr(p + 3, '=');
6151 if (p == NULL)
6152 break;
6153 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006154 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (p[i] == '\\' && p[i + 1])
6156 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006157 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 --p;
6159 }
6160}
6161#endif
6162
6163#endif /* FEAT_CMDHIST */
6164
Bram Moolenaar064154c2016-03-19 22:50:43 +01006165#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006166/*
6167 * Get pointer to the command line info to use. cmdline_paste() may clear
6168 * ccline and put the previous value in prev_ccline.
6169 */
6170 static struct cmdline_info *
6171get_ccline_ptr(void)
6172{
6173 if ((State & CMDLINE) == 0)
6174 return NULL;
6175 if (ccline.cmdbuff != NULL)
6176 return &ccline;
6177 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6178 return &prev_ccline;
6179 return NULL;
6180}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006181#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006182
Bram Moolenaar064154c2016-03-19 22:50:43 +01006183#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006184/*
6185 * Get the current command line in allocated memory.
6186 * Only works when the command line is being edited.
6187 * Returns NULL when something is wrong.
6188 */
6189 char_u *
6190get_cmdline_str(void)
6191{
6192 struct cmdline_info *p = get_ccline_ptr();
6193
6194 if (p == NULL)
6195 return NULL;
6196 return vim_strnsave(p->cmdbuff, p->cmdlen);
6197}
6198
6199/*
6200 * Get the current command line position, counted in bytes.
6201 * Zero is the first position.
6202 * Only works when the command line is being edited.
6203 * Returns -1 when something is wrong.
6204 */
6205 int
6206get_cmdline_pos(void)
6207{
6208 struct cmdline_info *p = get_ccline_ptr();
6209
6210 if (p == NULL)
6211 return -1;
6212 return p->cmdpos;
6213}
6214
6215/*
6216 * Set the command line byte position to "pos". Zero is the first position.
6217 * Only works when the command line is being edited.
6218 * Returns 1 when failed, 0 when OK.
6219 */
6220 int
6221set_cmdline_pos(
6222 int pos)
6223{
6224 struct cmdline_info *p = get_ccline_ptr();
6225
6226 if (p == NULL)
6227 return 1;
6228
6229 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6230 * changed the command line. */
6231 if (pos < 0)
6232 new_cmdpos = 0;
6233 else
6234 new_cmdpos = pos;
6235 return 0;
6236}
6237#endif
6238
6239#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6240/*
6241 * Get the current command-line type.
6242 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6243 * Only works when the command line is being edited.
6244 * Returns NUL when something is wrong.
6245 */
6246 int
6247get_cmdline_type(void)
6248{
6249 struct cmdline_info *p = get_ccline_ptr();
6250
6251 if (p == NULL)
6252 return NUL;
6253 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006254 return
6255# ifdef FEAT_EVAL
6256 (p->input_fn) ? '@' :
6257# endif
6258 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006259 return p->cmdfirstc;
6260}
6261#endif
6262
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6264/*
6265 * Get indices "num1,num2" that specify a range within a list (not a range of
6266 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6267 * Returns OK if parsed successfully, otherwise FAIL.
6268 */
6269 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006270get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 int len;
6273 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006274 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275
6276 *str = skipwhite(*str);
6277 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6278 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006279 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 *str += len;
6281 *num1 = (int)num;
6282 first = TRUE;
6283 }
6284 *str = skipwhite(*str);
6285 if (**str == ',') /* parse "to" part of range */
6286 {
6287 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006288 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (len > 0)
6290 {
6291 *num2 = (int)num;
6292 *str = skipwhite(*str + len);
6293 }
6294 else if (!first) /* no number given at all */
6295 return FAIL;
6296 }
6297 else if (first) /* only one number given */
6298 *num2 = *num1;
6299 return OK;
6300}
6301#endif
6302
6303#if defined(FEAT_CMDHIST) || defined(PROTO)
6304/*
6305 * :history command - print a history
6306 */
6307 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006308ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 histentry_T *hist;
6311 int histype1 = HIST_CMD;
6312 int histype2 = HIST_CMD;
6313 int hisidx1 = 1;
6314 int hisidx2 = -1;
6315 int idx;
6316 int i, j, k;
6317 char_u *end;
6318 char_u *arg = eap->arg;
6319
6320 if (hislen == 0)
6321 {
6322 MSG(_("'history' option is zero"));
6323 return;
6324 }
6325
6326 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6327 {
6328 end = arg;
6329 while (ASCII_ISALPHA(*end)
6330 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6331 end++;
6332 i = *end;
6333 *end = NUL;
6334 histype1 = get_histtype(arg);
6335 if (histype1 == -1)
6336 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006337 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 {
6339 histype1 = 0;
6340 histype2 = HIST_COUNT-1;
6341 }
6342 else
6343 {
6344 *end = i;
6345 EMSG(_(e_trailing));
6346 return;
6347 }
6348 }
6349 else
6350 histype2 = histype1;
6351 *end = i;
6352 }
6353 else
6354 end = arg;
6355 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6356 {
6357 EMSG(_(e_trailing));
6358 return;
6359 }
6360
6361 for (; !got_int && histype1 <= histype2; ++histype1)
6362 {
6363 STRCPY(IObuff, "\n # ");
6364 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6365 MSG_PUTS_TITLE(IObuff);
6366 idx = hisidx[histype1];
6367 hist = history[histype1];
6368 j = hisidx1;
6369 k = hisidx2;
6370 if (j < 0)
6371 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6372 if (k < 0)
6373 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6374 if (idx >= 0 && j <= k)
6375 for (i = idx + 1; !got_int; ++i)
6376 {
6377 if (i == hislen)
6378 i = 0;
6379 if (hist[i].hisstr != NULL
6380 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6381 {
6382 msg_putchar('\n');
6383 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6384 hist[i].hisnum);
6385 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6386 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006387 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 else
6389 STRCAT(IObuff, hist[i].hisstr);
6390 msg_outtrans(IObuff);
6391 out_flush();
6392 }
6393 if (i == idx)
6394 break;
6395 }
6396 }
6397}
6398#endif
6399
6400#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006401/*
6402 * Buffers for history read from a viminfo file. Only valid while reading.
6403 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006404static histentry_T *viminfo_history[HIST_COUNT] =
6405 {NULL, NULL, NULL, NULL, NULL};
6406static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6407static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408static int viminfo_add_at_front = FALSE;
6409
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006410static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411
6412/*
6413 * Translate a history type number to the associated character.
6414 */
6415 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006416hist_type2char(
6417 int type,
6418 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419{
6420 if (type == HIST_CMD)
6421 return ':';
6422 if (type == HIST_SEARCH)
6423 {
6424 if (use_question)
6425 return '?';
6426 else
6427 return '/';
6428 }
6429 if (type == HIST_EXPR)
6430 return '=';
6431 return '@';
6432}
6433
6434/*
6435 * Prepare for reading the history from the viminfo file.
6436 * This allocates history arrays to store the read history lines.
6437 */
6438 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006439prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440{
6441 int i;
6442 int num;
6443 int type;
6444 int len;
6445
6446 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006447 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 if (asklen > hislen)
6449 asklen = hislen;
6450
6451 for (type = 0; type < HIST_COUNT; ++type)
6452 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006453 /* Count the number of empty spaces in the history list. Entries read
6454 * from viminfo previously are also considered empty. If there are
6455 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006457 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458 num++;
6459 len = asklen;
6460 if (num > len)
6461 len = num;
6462 if (len <= 0)
6463 viminfo_history[type] = NULL;
6464 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006465 viminfo_history[type] = (histentry_T *)lalloc(
6466 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (viminfo_history[type] == NULL)
6468 len = 0;
6469 viminfo_hislen[type] = len;
6470 viminfo_hisidx[type] = 0;
6471 }
6472}
6473
6474/*
6475 * Accept a line from the viminfo, store it in the history array when it's
6476 * new.
6477 */
6478 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006479read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480{
6481 int type;
6482 long_u len;
6483 char_u *val;
6484 char_u *p;
6485
6486 type = hist_char2type(virp->vir_line[0]);
6487 if (viminfo_hisidx[type] < viminfo_hislen[type])
6488 {
6489 val = viminfo_readstring(virp, 1, TRUE);
6490 if (val != NULL && *val != NUL)
6491 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006492 int sep = (*val == ' ' ? NUL : *val);
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006495 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 {
6497 /* Need to re-allocate to append the separator byte. */
6498 len = STRLEN(val);
6499 p = lalloc(len + 2, TRUE);
6500 if (p != NULL)
6501 {
6502 if (type == HIST_SEARCH)
6503 {
6504 /* Search entry: Move the separator from the first
6505 * column to after the NUL. */
6506 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006507 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508 }
6509 else
6510 {
6511 /* Not a search entry: No separator in the viminfo
6512 * file, add a NUL separator. */
6513 mch_memmove(p, val, (size_t)len + 1);
6514 p[len + 1] = NUL;
6515 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006516 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6517 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006518 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6519 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006520 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522 }
6523 }
6524 vim_free(val);
6525 }
6526 return viminfo_readline(virp);
6527}
6528
Bram Moolenaar07219f92013-04-14 23:19:36 +02006529/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006530 * Accept a new style history line from the viminfo, store it in the history
6531 * array when it's new.
6532 */
6533 void
6534handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006535 garray_T *values,
6536 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006537{
6538 int type;
6539 long_u len;
6540 char_u *val;
6541 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006542 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006543
6544 /* Check the format:
6545 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006546 if (values->ga_len < 4
6547 || vp[0].bv_type != BVAL_NR
6548 || vp[1].bv_type != BVAL_NR
6549 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6550 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006551 return;
6552
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006553 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006554 if (type >= HIST_COUNT)
6555 return;
6556 if (viminfo_hisidx[type] < viminfo_hislen[type])
6557 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006558 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006559 if (val != NULL && *val != NUL)
6560 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006561 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6562 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006563 int idx;
6564 int overwrite = FALSE;
6565
6566 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6567 {
6568 /* If lines were written by an older Vim we need to avoid
6569 * getting duplicates. See if the entry already exists. */
6570 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6571 {
6572 p = viminfo_history[type][idx].hisstr;
6573 if (STRCMP(val, p) == 0
6574 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6575 {
6576 overwrite = TRUE;
6577 break;
6578 }
6579 }
6580
6581 if (!overwrite)
6582 {
6583 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006584 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006585 p = lalloc(len + 2, TRUE);
6586 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006587 else
6588 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006589 if (p != NULL)
6590 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006591 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006592 if (!overwrite)
6593 {
6594 mch_memmove(p, val, (size_t)len + 1);
6595 /* Put the separator after the NUL. */
6596 p[len + 1] = sep;
6597 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006598 viminfo_history[type][idx].hisnum = 0;
6599 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006600 viminfo_hisidx[type]++;
6601 }
6602 }
6603 }
6604 }
6605 }
6606}
6607
6608/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006609 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006610 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006611 static void
6612concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613{
6614 int idx;
6615 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006616
6617 idx = hisidx[type] + viminfo_hisidx[type];
6618 if (idx >= hislen)
6619 idx -= hislen;
6620 else if (idx < 0)
6621 idx = hislen - 1;
6622 if (viminfo_add_at_front)
6623 hisidx[type] = idx;
6624 else
6625 {
6626 if (hisidx[type] == -1)
6627 hisidx[type] = hislen - 1;
6628 do
6629 {
6630 if (history[type][idx].hisstr != NULL
6631 || history[type][idx].viminfo)
6632 break;
6633 if (++idx == hislen)
6634 idx = 0;
6635 } while (idx != hisidx[type]);
6636 if (idx != hisidx[type] && --idx < 0)
6637 idx = hislen - 1;
6638 }
6639 for (i = 0; i < viminfo_hisidx[type]; i++)
6640 {
6641 vim_free(history[type][idx].hisstr);
6642 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6643 history[type][idx].viminfo = TRUE;
6644 history[type][idx].time_set = viminfo_history[type][i].time_set;
6645 if (--idx < 0)
6646 idx = hislen - 1;
6647 }
6648 idx += 1;
6649 idx %= hislen;
6650 for (i = 0; i < viminfo_hisidx[type]; i++)
6651 {
6652 history[type][idx++].hisnum = ++hisnum[type];
6653 idx %= hislen;
6654 }
6655}
6656
6657#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6658 static int
6659#ifdef __BORLANDC__
6660_RTLENTRYF
6661#endif
6662sort_hist(const void *s1, const void *s2)
6663{
6664 histentry_T *p1 = *(histentry_T **)s1;
6665 histentry_T *p2 = *(histentry_T **)s2;
6666
6667 if (p1->time_set < p2->time_set) return -1;
6668 if (p1->time_set > p2->time_set) return 1;
6669 return 0;
6670}
6671#endif
6672
6673/*
6674 * Merge history lines from viminfo and lines typed in this Vim based on the
6675 * timestamp;
6676 */
6677 static void
6678merge_history(int type)
6679{
6680 int max_len;
6681 histentry_T **tot_hist;
6682 histentry_T *new_hist;
6683 int i;
6684 int len;
6685
6686 /* Make one long list with all entries. */
6687 max_len = hislen + viminfo_hisidx[type];
6688 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6689 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6690 if (tot_hist == NULL || new_hist == NULL)
6691 {
6692 vim_free(tot_hist);
6693 vim_free(new_hist);
6694 return;
6695 }
6696 for (i = 0; i < viminfo_hisidx[type]; i++)
6697 tot_hist[i] = &viminfo_history[type][i];
6698 len = i;
6699 for (i = 0; i < hislen; i++)
6700 if (history[type][i].hisstr != NULL)
6701 tot_hist[len++] = &history[type][i];
6702
6703 /* Sort the list on timestamp. */
6704 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6705
6706 /* Keep the newest ones. */
6707 for (i = 0; i < hislen; i++)
6708 {
6709 if (i < len)
6710 {
6711 new_hist[i] = *tot_hist[i];
6712 tot_hist[i]->hisstr = NULL;
6713 if (new_hist[i].hisnum == 0)
6714 new_hist[i].hisnum = ++hisnum[type];
6715 }
6716 else
6717 clear_hist_entry(&new_hist[i]);
6718 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006719 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006720
6721 /* Free what is not kept. */
6722 for (i = 0; i < viminfo_hisidx[type]; i++)
6723 vim_free(viminfo_history[type][i].hisstr);
6724 for (i = 0; i < hislen; i++)
6725 vim_free(history[type][i].hisstr);
6726 vim_free(history[type]);
6727 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006728 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006729}
6730
6731/*
6732 * Finish reading history lines from viminfo. Not used when writing viminfo.
6733 */
6734 void
6735finish_viminfo_history(vir_T *virp)
6736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006738 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
6740 for (type = 0; type < HIST_COUNT; ++type)
6741 {
6742 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006743 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006744
6745 if (merge)
6746 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006748 concat_history(type);
6749
Bram Moolenaard23a8232018-02-10 18:45:26 +01006750 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006751 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 }
6753}
6754
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006755/*
6756 * Write history to viminfo file in "fp".
6757 * When "merge" is TRUE merge history lines with a previously read viminfo
6758 * file, data is in viminfo_history[].
6759 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006762write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763{
6764 int i;
6765 int type;
6766 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006767 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768
6769 init_history();
6770 if (hislen == 0)
6771 return;
6772 for (type = 0; type < HIST_COUNT; ++type)
6773 {
6774 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6775 if (num_saved == 0)
6776 continue;
6777 if (num_saved < 0) /* Use default */
6778 num_saved = hislen;
6779 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6780 type == HIST_CMD ? _("Command Line") :
6781 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006782 type == HIST_EXPR ? _("Expression") :
6783 type == HIST_INPUT ? _("Input Line") :
6784 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 if (num_saved > hislen)
6786 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006787
6788 /*
6789 * Merge typed and viminfo history:
6790 * round 1: history of typed commands.
6791 * round 2: history from recently read viminfo.
6792 */
6793 for (round = 1; round <= 2; ++round)
6794 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006795 if (round == 1)
6796 /* start at newest entry, somewhere in the list */
6797 i = hisidx[type];
6798 else if (viminfo_hisidx[type] > 0)
6799 /* start at newest entry, first in the list */
6800 i = 0;
6801 else
6802 /* empty list */
6803 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006804 if (i >= 0)
6805 while (num_saved > 0
6806 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 char_u *p;
6809 time_t timestamp;
6810 int c = NUL;
6811
6812 if (round == 1)
6813 {
6814 p = history[type][i].hisstr;
6815 timestamp = history[type][i].time_set;
6816 }
6817 else
6818 {
6819 p = viminfo_history[type] == NULL ? NULL
6820 : viminfo_history[type][i].hisstr;
6821 timestamp = viminfo_history[type] == NULL ? 0
6822 : viminfo_history[type][i].time_set;
6823 }
6824
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006825 if (p != NULL && (round == 2
6826 || !merge
6827 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006829 --num_saved;
6830 fputc(hist_type2char(type, TRUE), fp);
6831 /* For the search history: put the separator in the
6832 * second column; use a space if there isn't one. */
6833 if (type == HIST_SEARCH)
6834 {
6835 c = p[STRLEN(p) + 1];
6836 putc(c == NUL ? ' ' : c, fp);
6837 }
6838 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006839
6840 {
6841 char cbuf[NUMBUFLEN];
6842
6843 /* New style history with a bar line. Format:
6844 * |{bartype},{histtype},{timestamp},{separator},"text" */
6845 if (c == NUL)
6846 cbuf[0] = NUL;
6847 else
6848 sprintf(cbuf, "%d", c);
6849 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6850 type, (long)timestamp, cbuf);
6851 barline_writestring(fp, p, LSIZE - 20);
6852 putc('\n', fp);
6853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006855 if (round == 1)
6856 {
6857 /* Decrement index, loop around and stop when back at
6858 * the start. */
6859 if (--i < 0)
6860 i = hislen - 1;
6861 if (i == hisidx[type])
6862 break;
6863 }
6864 else
6865 {
6866 /* Increment index. Stop at the end in the while. */
6867 ++i;
6868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006870 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006871 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006872 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006873 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006874 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006875 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 }
6877}
6878#endif /* FEAT_VIMINFO */
6879
6880#if defined(FEAT_FKMAP) || defined(PROTO)
6881/*
6882 * Write a character at the current cursor+offset position.
6883 * It is directly written into the command buffer block.
6884 */
6885 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006886cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887{
6888 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6889 {
6890 EMSG(_("E198: cmd_pchar beyond the command length"));
6891 return;
6892 }
6893 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6894 ccline.cmdbuff[ccline.cmdlen] = NUL;
6895}
6896
6897 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006898cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
6900 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6901 {
6902 /* EMSG(_("cmd_gchar beyond the command length")); */
6903 return NUL;
6904 }
6905 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6906}
6907#endif
6908
6909#if defined(FEAT_CMDWIN) || defined(PROTO)
6910/*
6911 * Open a window on the current command line and history. Allow editing in
6912 * the window. Returns when the window is closed.
6913 * Returns:
6914 * CR if the command is to be executed
6915 * Ctrl_C if it is to be abandoned
6916 * K_IGNORE if editing continues
6917 */
6918 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006919open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920{
6921 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006922 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006924 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 win_T *wp;
6926 int i;
6927 linenr_T lnum;
6928 int histtype;
6929 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 int save_restart_edit = restart_edit;
6931 int save_State = State;
6932 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006933#ifdef FEAT_RIGHTLEFT
6934 int save_cmdmsg_rl = cmdmsg_rl;
6935#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006936#ifdef FEAT_FOLDING
6937 int save_KeyTyped;
6938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
6940 /* Can't do this recursively. Can't do it when typing a password. */
6941 if (cmdwin_type != 0
6942# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6943 || cmdline_star > 0
6944# endif
6945 )
6946 {
6947 beep_flush();
6948 return K_IGNORE;
6949 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006950 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
6952 /* Save current window sizes. */
6953 win_size_save(&winsizes);
6954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006956 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006957
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006958 /* don't use a new tab page */
6959 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006960 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 /* Create a window for the command-line buffer. */
6963 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6964 {
6965 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006966 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 return K_IGNORE;
6968 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006969 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006972 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006973 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006976#ifdef FEAT_FOLDING
6977 curwin->w_p_fen = FALSE;
6978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006980 curwin->w_p_rl = cmdmsg_rl;
6981 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006983 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006986 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006987 /* But don't allow switching to another buffer. */
6988 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
Bram Moolenaar46152342005-09-07 21:18:43 +00006990 /* Showing the prompt may have set need_wait_return, reset it. */
6991 need_wait_return = FALSE;
6992
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006993 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6995 {
6996 if (p_wc == TAB)
6997 {
6998 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6999 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7000 }
7001 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7002 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007003 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007005 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7006 * sets 'textwidth' to 78). */
7007 curbuf->b_p_tw = 0;
7008
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 /* Fill the buffer with the history. */
7010 init_history();
7011 if (hislen > 0)
7012 {
7013 i = hisidx[histtype];
7014 if (i >= 0)
7015 {
7016 lnum = 0;
7017 do
7018 {
7019 if (++i == hislen)
7020 i = 0;
7021 if (history[histtype][i].hisstr != NULL)
7022 ml_append(lnum++, history[histtype][i].hisstr,
7023 (colnr_T)0, FALSE);
7024 }
7025 while (i != hisidx[histtype]);
7026 }
7027 }
7028
7029 /* Replace the empty last line with the current command-line and put the
7030 * cursor there. */
7031 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7032 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7033 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007034 changed_line_abv_curs();
7035 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007036 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007039 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040
7041 /* No Ex mode here! */
7042 exmode_active = 0;
7043
7044 State = NORMAL;
7045# ifdef FEAT_MOUSE
7046 setmouse();
7047# endif
7048
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007050 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007051 if (restart_edit != 0) /* autocmd with ":startinsert" */
7052 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
7054 i = RedrawingDisabled;
7055 RedrawingDisabled = 0;
7056
7057 /*
7058 * Call the main loop until <CR> or CTRL-C is typed.
7059 */
7060 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007061 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062
7063 RedrawingDisabled = i;
7064
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007065# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007066 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007067# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007070 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007071
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007072# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007073 /* Restore KeyTyped in case it is modified by autocommands */
7074 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075# endif
7076
Bram Moolenaarccc18222007-05-10 18:25:20 +00007077 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007078 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 cmdwin_type = 0;
7080
7081 exmode_active = save_exmode;
7082
Bram Moolenaarf9821062008-06-20 16:31:07 +00007083 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007085 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
7087 cmdwin_result = Ctrl_C;
7088 EMSG(_("E199: Active window or buffer deleted"));
7089 }
7090 else
7091 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007092# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 /* autocmds may abort script processing */
7094 if (aborting() && cmdwin_result != K_IGNORE)
7095 cmdwin_result = Ctrl_C;
7096# endif
7097 /* Set the new command line from the cmdline buffer. */
7098 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007099 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007101 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7102
7103 if (histtype == HIST_CMD)
7104 {
7105 /* Execute the command directly. */
7106 ccline.cmdbuff = vim_strsave((char_u *)p);
7107 cmdwin_result = CAR;
7108 }
7109 else
7110 {
7111 /* First need to cancel what we were doing. */
7112 ccline.cmdbuff = NULL;
7113 stuffcharReadbuff(':');
7114 stuffReadbuff((char_u *)p);
7115 stuffcharReadbuff(CAR);
7116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 }
7118 else if (cmdwin_result == K_XF2) /* :qa typed */
7119 {
7120 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7121 cmdwin_result = CAR;
7122 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007123 else if (cmdwin_result == Ctrl_C)
7124 {
7125 /* :q or :close, don't execute any command
7126 * and don't modify the cmd window. */
7127 ccline.cmdbuff = NULL;
7128 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 else
7130 ccline.cmdbuff = vim_strsave(ml_get_curline());
7131 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007132 {
7133 ccline.cmdbuff = vim_strsave((char_u *)"");
7134 ccline.cmdlen = 0;
7135 ccline.cmdbufflen = 1;
7136 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 else
7140 {
7141 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7142 ccline.cmdbufflen = ccline.cmdlen + 1;
7143 ccline.cmdpos = curwin->w_cursor.col;
7144 if (ccline.cmdpos > ccline.cmdlen)
7145 ccline.cmdpos = ccline.cmdlen;
7146 if (cmdwin_result == K_IGNORE)
7147 {
7148 set_cmdspos_cursor();
7149 redrawcmd();
7150 }
7151 }
7152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007154 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007155# ifdef FEAT_CONCEAL
7156 /* Avoid command-line window first character being concealed. */
7157 curwin->w_p_cole = 0;
7158# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007160 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 win_goto(old_curwin);
7162 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007163
7164 /* win_close() may have already wiped the buffer when 'bh' is
7165 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007166 if (bufref_valid(&bufref))
7167 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
7169 /* Restore window sizes. */
7170 win_size_restore(&winsizes);
7171
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007172 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174
7175 ga_clear(&winsizes);
7176 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007177# ifdef FEAT_RIGHTLEFT
7178 cmdmsg_rl = save_cmdmsg_rl;
7179# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
7181 State = save_State;
7182# ifdef FEAT_MOUSE
7183 setmouse();
7184# endif
7185
7186 return cmdwin_result;
7187}
7188#endif /* FEAT_CMDWIN */
7189
7190/*
7191 * Used for commands that either take a simple command string argument, or:
7192 * cmd << endmarker
7193 * {script}
7194 * endmarker
7195 * Returns a pointer to allocated memory with {script} or NULL.
7196 */
7197 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007198script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 char_u *theline;
7201 char *end_pattern = NULL;
7202 char dot[] = ".";
7203 garray_T ga;
7204
7205 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7206 return NULL;
7207
7208 ga_init2(&ga, 1, 0x400);
7209
7210 if (cmd[2] != NUL)
7211 end_pattern = (char *)skipwhite(cmd + 2);
7212 else
7213 end_pattern = dot;
7214
7215 for (;;)
7216 {
7217 theline = eap->getline(
7218#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007219 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220#endif
7221 NUL, eap->cookie, 0);
7222
7223 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007224 {
7225 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
7229 ga_concat(&ga, theline);
7230 ga_append(&ga, '\n');
7231 vim_free(theline);
7232 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007233 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 return (char_u *)ga.ga_data;
7236}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007237
7238#ifdef FEAT_SEARCH_EXTRA
7239 static void
7240set_search_match(pos_T *t)
7241{
7242 /*
7243 * First move cursor to end of match, then to the start. This
7244 * moves the whole match onto the screen when 'nowrap' is set.
7245 */
7246 t->lnum += search_match_lines;
7247 t->col = search_match_endcol;
7248 if (t->lnum > curbuf->b_ml.ml_line_count)
7249 {
7250 t->lnum = curbuf->b_ml.ml_line_count;
7251 coladvance((colnr_T)MAXCOL);
7252 }
7253}
7254#endif