blob: 0124098da5195cee19989b4bd25e327519b9b47c [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{
3575 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3576 return FALSE;
3577
3578 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3579}
3580
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003581#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3582 static int
3583#ifdef __BORLANDC__
3584_RTLENTRYF
3585#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003586sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003587{
3588 char_u *p1 = *(char_u **)s1;
3589 char_u *p2 = *(char_u **)s2;
3590
3591 if (*p1 != '<' && *p2 == '<') return -1;
3592 if (*p1 == '<' && *p2 != '<') return 1;
3593 return STRCMP(p1, p2);
3594}
3595#endif
3596
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597/*
3598 * Return FAIL if this is not an appropriate context in which to do
3599 * completion of anything, return OK if it is (even if there are no matches).
3600 * For the caller, this means that the character is just passed through like a
3601 * normal character (instead of being expanded). This allows :s/^I^D etc.
3602 */
3603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003604nextwild(
3605 expand_T *xp,
3606 int type,
3607 int options, /* extra options for ExpandOne() */
3608 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 int i, j;
3611 char_u *p1;
3612 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int difflen;
3614 int v;
3615
3616 if (xp->xp_numfiles == -1)
3617 {
3618 set_expand_context(xp);
3619 cmd_showtail = expand_showtail(xp);
3620 }
3621
3622 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3623 {
3624 beep_flush();
3625 return OK; /* Something illegal on command line */
3626 }
3627 if (xp->xp_context == EXPAND_NOTHING)
3628 {
3629 /* Caller can use the character as a normal char instead */
3630 return FAIL;
3631 }
3632
3633 MSG_PUTS("..."); /* show that we are busy */
3634 out_flush();
3635
3636 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003637 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
3639 if (type == WILD_NEXT || type == WILD_PREV)
3640 {
3641 /*
3642 * Get next/previous match for a previous expanded pattern.
3643 */
3644 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3645 }
3646 else
3647 {
3648 /*
3649 * Translate string into pattern and expand it.
3650 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003651 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3652 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 p2 = NULL;
3654 else
3655 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003656 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003657 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3658 if (escape)
3659 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003660
3661 if (p_wic)
3662 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003663 p2 = ExpandOne(xp, p1,
3664 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003665 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003667 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 if (p2 != NULL && type == WILD_LONGEST)
3669 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003670 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 if (ccline.cmdbuff[i + j] == '*'
3672 || ccline.cmdbuff[i + j] == '?')
3673 break;
3674 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003675 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 }
3677 }
3678 }
3679
3680 if (p2 != NULL && !got_int)
3681 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003682 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003683 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003685 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 xp->xp_pattern = ccline.cmdbuff + i;
3687 }
3688 else
3689 v = OK;
3690 if (v == OK)
3691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003692 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3693 &ccline.cmdbuff[ccline.cmdpos],
3694 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3695 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 ccline.cmdlen += difflen;
3697 ccline.cmdpos += difflen;
3698 }
3699 }
3700 vim_free(p2);
3701
3702 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003703 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 /* When expanding a ":map" command and no matches are found, assume that
3706 * the key is supposed to be inserted literally */
3707 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3708 return FAIL;
3709
3710 if (xp->xp_numfiles <= 0 && p2 == NULL)
3711 beep_flush();
3712 else if (xp->xp_numfiles == 1)
3713 /* free expanded pattern */
3714 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3715
3716 return OK;
3717}
3718
3719/*
3720 * Do wildcard expansion on the string 'str'.
3721 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003722 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 * Return NULL for failure.
3724 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003725 * "orig" is the originally expanded string, copied to allocated memory. It
3726 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3727 * WILD_PREV "orig" should be NULL.
3728 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003729 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3730 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 *
3732 * mode = WILD_FREE: just free previously expanded matches
3733 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3734 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3735 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3736 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3737 * mode = WILD_ALL: return all matches concatenated
3738 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003739 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 *
3741 * options = WILD_LIST_NOTFOUND: list entries without a match
3742 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3743 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3744 * options = WILD_NO_BEEP: Don't beep for multiple matches
3745 * options = WILD_ADD_SLASH: add a slash after directory names
3746 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3747 * options = WILD_SILENT: don't print warning messages
3748 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003749 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 *
3751 * The variables xp->xp_context and xp->xp_backslash must have been set!
3752 */
3753 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003754ExpandOne(
3755 expand_T *xp,
3756 char_u *str,
3757 char_u *orig, /* allocated copy of original of expanded string */
3758 int options,
3759 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760{
3761 char_u *ss = NULL;
3762 static int findex;
3763 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003764 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 int i;
3766 long_u len;
3767 int non_suf_match; /* number without matching suffix */
3768
3769 /*
3770 * first handle the case of using an old match
3771 */
3772 if (mode == WILD_NEXT || mode == WILD_PREV)
3773 {
3774 if (xp->xp_numfiles > 0)
3775 {
3776 if (mode == WILD_PREV)
3777 {
3778 if (findex == -1)
3779 findex = xp->xp_numfiles;
3780 --findex;
3781 }
3782 else /* mode == WILD_NEXT */
3783 ++findex;
3784
3785 /*
3786 * When wrapping around, return the original string, set findex to
3787 * -1.
3788 */
3789 if (findex < 0)
3790 {
3791 if (orig_save == NULL)
3792 findex = xp->xp_numfiles - 1;
3793 else
3794 findex = -1;
3795 }
3796 if (findex >= xp->xp_numfiles)
3797 {
3798 if (orig_save == NULL)
3799 findex = 0;
3800 else
3801 findex = -1;
3802 }
3803#ifdef FEAT_WILDMENU
3804 if (p_wmnu)
3805 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3806 findex, cmd_showtail);
3807#endif
3808 if (findex == -1)
3809 return vim_strsave(orig_save);
3810 return vim_strsave(xp->xp_files[findex]);
3811 }
3812 else
3813 return NULL;
3814 }
3815
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003816 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3818 {
3819 FreeWild(xp->xp_numfiles, xp->xp_files);
3820 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003821 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 }
3823 findex = 0;
3824
3825 if (mode == WILD_FREE) /* only release file name */
3826 return NULL;
3827
3828 if (xp->xp_numfiles == -1)
3829 {
3830 vim_free(orig_save);
3831 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003832 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833
3834 /*
3835 * Do the expansion.
3836 */
3837 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3838 options) == FAIL)
3839 {
3840#ifdef FNAME_ILLEGAL
3841 /* Illegal file name has been silently skipped. But when there
3842 * are wildcards, the real problem is that there was no match,
3843 * causing the pattern to be added, which has illegal characters.
3844 */
3845 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3846 EMSG2(_(e_nomatch2), str);
3847#endif
3848 }
3849 else if (xp->xp_numfiles == 0)
3850 {
3851 if (!(options & WILD_SILENT))
3852 EMSG2(_(e_nomatch2), str);
3853 }
3854 else
3855 {
3856 /* Escape the matches for use on the command line. */
3857 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3858
3859 /*
3860 * Check for matching suffixes in file names.
3861 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003862 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3863 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 {
3865 if (xp->xp_numfiles)
3866 non_suf_match = xp->xp_numfiles;
3867 else
3868 non_suf_match = 1;
3869 if ((xp->xp_context == EXPAND_FILES
3870 || xp->xp_context == EXPAND_DIRECTORIES)
3871 && xp->xp_numfiles > 1)
3872 {
3873 /*
3874 * More than one match; check suffix.
3875 * The files will have been sorted on matching suffix in
3876 * expand_wildcards, only need to check the first two.
3877 */
3878 non_suf_match = 0;
3879 for (i = 0; i < 2; ++i)
3880 if (match_suffix(xp->xp_files[i]))
3881 ++non_suf_match;
3882 }
3883 if (non_suf_match != 1)
3884 {
3885 /* Can we ever get here unless it's while expanding
3886 * interactively? If not, we can get rid of this all
3887 * together. Don't really want to wait for this message
3888 * (and possibly have to hit return to continue!).
3889 */
3890 if (!(options & WILD_SILENT))
3891 EMSG(_(e_toomany));
3892 else if (!(options & WILD_NO_BEEP))
3893 beep_flush();
3894 }
3895 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3896 ss = vim_strsave(xp->xp_files[0]);
3897 }
3898 }
3899 }
3900
3901 /* Find longest common part */
3902 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3903 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003904 int mb_len = 1;
3905 int c0, ci;
3906
3907 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003909#ifdef FEAT_MBYTE
3910 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003912 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3913 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3914 }
3915 else
3916#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003917 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003918 for (i = 1; i < xp->xp_numfiles; ++i)
3919 {
3920#ifdef FEAT_MBYTE
3921 if (has_mbyte)
3922 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3923 else
3924#endif
3925 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003926 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003928 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003929 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003931 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 break;
3933 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003934 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 break;
3936 }
3937 if (i < xp->xp_numfiles)
3938 {
3939 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003940 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942 }
3943 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003944
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 ss = alloc((unsigned)len + 1);
3946 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003947 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 findex = -1; /* next p_wc gets first one */
3949 }
3950
3951 /* Concatenate all matching names */
3952 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3953 {
3954 len = 0;
3955 for (i = 0; i < xp->xp_numfiles; ++i)
3956 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3957 ss = lalloc(len, TRUE);
3958 if (ss != NULL)
3959 {
3960 *ss = NUL;
3961 for (i = 0; i < xp->xp_numfiles; ++i)
3962 {
3963 STRCAT(ss, xp->xp_files[i]);
3964 if (i != xp->xp_numfiles - 1)
3965 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3966 }
3967 }
3968 }
3969
3970 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3971 ExpandCleanup(xp);
3972
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003973 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003974 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003975 vim_free(orig);
3976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 return ss;
3978}
3979
3980/*
3981 * Prepare an expand structure for use.
3982 */
3983 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003984ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003986 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003987 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003989#ifndef BACKSLASH_IN_FILENAME
3990 xp->xp_shell = FALSE;
3991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 xp->xp_numfiles = -1;
3993 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003994#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3995 xp->xp_arg = NULL;
3996#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003997 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998}
3999
4000/*
4001 * Cleanup an expand structure after use.
4002 */
4003 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004004ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
4006 if (xp->xp_numfiles >= 0)
4007 {
4008 FreeWild(xp->xp_numfiles, xp->xp_files);
4009 xp->xp_numfiles = -1;
4010 }
4011}
4012
4013 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004014ExpandEscape(
4015 expand_T *xp,
4016 char_u *str,
4017 int numfiles,
4018 char_u **files,
4019 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
4021 int i;
4022 char_u *p;
4023
4024 /*
4025 * May change home directory back to "~"
4026 */
4027 if (options & WILD_HOME_REPLACE)
4028 tilde_replace(str, numfiles, files);
4029
4030 if (options & WILD_ESCAPE)
4031 {
4032 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004033 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004034 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 || xp->xp_context == EXPAND_BUFFERS
4036 || xp->xp_context == EXPAND_DIRECTORIES)
4037 {
4038 /*
4039 * Insert a backslash into a file name before a space, \, %, #
4040 * and wildmatch characters, except '~'.
4041 */
4042 for (i = 0; i < numfiles; ++i)
4043 {
4044 /* for ":set path=" we need to escape spaces twice */
4045 if (xp->xp_backslash == XP_BS_THREE)
4046 {
4047 p = vim_strsave_escaped(files[i], (char_u *)" ");
4048 if (p != NULL)
4049 {
4050 vim_free(files[i]);
4051 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004052#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 p = vim_strsave_escaped(files[i], (char_u *)" ");
4054 if (p != NULL)
4055 {
4056 vim_free(files[i]);
4057 files[i] = p;
4058 }
4059#endif
4060 }
4061 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004062#ifdef BACKSLASH_IN_FILENAME
4063 p = vim_strsave_fnameescape(files[i], FALSE);
4064#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004065 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004066#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 if (p != NULL)
4068 {
4069 vim_free(files[i]);
4070 files[i] = p;
4071 }
4072
4073 /* If 'str' starts with "\~", replace "~" at start of
4074 * files[i] with "\~". */
4075 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004076 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004079
4080 /* If the first file starts with a '+' escape it. Otherwise it
4081 * could be seen as "+cmd". */
4082 if (*files[0] == '+')
4083 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 }
4085 else if (xp->xp_context == EXPAND_TAGS)
4086 {
4087 /*
4088 * Insert a backslash before characters in a tag name that
4089 * would terminate the ":tag" command.
4090 */
4091 for (i = 0; i < numfiles; ++i)
4092 {
4093 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4094 if (p != NULL)
4095 {
4096 vim_free(files[i]);
4097 files[i] = p;
4098 }
4099 }
4100 }
4101 }
4102}
4103
4104/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004105 * Escape special characters in "fname" for when used as a file name argument
4106 * after a Vim command, or, when "shell" is non-zero, a shell command.
4107 * Returns the result in allocated memory.
4108 */
4109 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004110vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004111{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004112 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004113#ifdef BACKSLASH_IN_FILENAME
4114 char_u buf[20];
4115 int j = 0;
4116
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004117 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004118 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004119 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004120 buf[j++] = *p;
4121 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004122 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004123#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004124 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4125 if (shell && csh_like_shell() && p != NULL)
4126 {
4127 char_u *s;
4128
4129 /* For csh and similar shells need to put two backslashes before '!'.
4130 * One is taken by Vim, one by the shell. */
4131 s = vim_strsave_escaped(p, (char_u *)"!");
4132 vim_free(p);
4133 p = s;
4134 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004135#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004136
4137 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4138 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004139 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004140 escape_fname(&p);
4141
4142 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004143}
4144
4145/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004146 * Put a backslash before the file name in "pp", which is in allocated memory.
4147 */
4148 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004149escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004150{
4151 char_u *p;
4152
4153 p = alloc((unsigned)(STRLEN(*pp) + 2));
4154 if (p != NULL)
4155 {
4156 p[0] = '\\';
4157 STRCPY(p + 1, *pp);
4158 vim_free(*pp);
4159 *pp = p;
4160 }
4161}
4162
4163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 * For each file name in files[num_files]:
4165 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4166 */
4167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004168tilde_replace(
4169 char_u *orig_pat,
4170 int num_files,
4171 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172{
4173 int i;
4174 char_u *p;
4175
4176 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4177 {
4178 for (i = 0; i < num_files; ++i)
4179 {
4180 p = home_replace_save(NULL, files[i]);
4181 if (p != NULL)
4182 {
4183 vim_free(files[i]);
4184 files[i] = p;
4185 }
4186 }
4187 }
4188}
4189
4190/*
4191 * Show all matches for completion on the command line.
4192 * Returns EXPAND_NOTHING when the character that triggered expansion should
4193 * be inserted like a normal character.
4194 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004196showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
4198#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4199 int num_files;
4200 char_u **files_found;
4201 int i, j, k;
4202 int maxlen;
4203 int lines;
4204 int columns;
4205 char_u *p;
4206 int lastlen;
4207 int attr;
4208 int showtail;
4209
4210 if (xp->xp_numfiles == -1)
4211 {
4212 set_expand_context(xp);
4213 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4214 &num_files, &files_found);
4215 showtail = expand_showtail(xp);
4216 if (i != EXPAND_OK)
4217 return i;
4218
4219 }
4220 else
4221 {
4222 num_files = xp->xp_numfiles;
4223 files_found = xp->xp_files;
4224 showtail = cmd_showtail;
4225 }
4226
4227#ifdef FEAT_WILDMENU
4228 if (!wildmenu)
4229 {
4230#endif
4231 msg_didany = FALSE; /* lines_left will be set */
4232 msg_start(); /* prepare for paging */
4233 msg_putchar('\n');
4234 out_flush();
4235 cmdline_row = msg_row;
4236 msg_didany = FALSE; /* lines_left will be set again */
4237 msg_start(); /* prepare for paging */
4238#ifdef FEAT_WILDMENU
4239 }
4240#endif
4241
4242 if (got_int)
4243 got_int = FALSE; /* only int. the completion, not the cmd line */
4244#ifdef FEAT_WILDMENU
4245 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004246 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247#endif
4248 else
4249 {
4250 /* find the length of the longest file name */
4251 maxlen = 0;
4252 for (i = 0; i < num_files; ++i)
4253 {
4254 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004255 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 || xp->xp_context == EXPAND_BUFFERS))
4257 {
4258 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4259 j = vim_strsize(NameBuff);
4260 }
4261 else
4262 j = vim_strsize(L_SHOWFILE(i));
4263 if (j > maxlen)
4264 maxlen = j;
4265 }
4266
4267 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4268 lines = num_files;
4269 else
4270 {
4271 /* compute the number of columns and lines for the listing */
4272 maxlen += 2; /* two spaces between file names */
4273 columns = ((int)Columns + 2) / maxlen;
4274 if (columns < 1)
4275 columns = 1;
4276 lines = (num_files + columns - 1) / columns;
4277 }
4278
Bram Moolenaar8820b482017-03-16 17:23:31 +01004279 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
4281 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4282 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004283 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 msg_clr_eos();
4285 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004286 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
4288
4289 /* list the files line by line */
4290 for (i = 0; i < lines; ++i)
4291 {
4292 lastlen = 999;
4293 for (k = i; k < num_files; k += lines)
4294 {
4295 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4296 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004297 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 p = files_found[k] + STRLEN(files_found[k]) + 1;
4299 msg_advance(maxlen + 1);
4300 msg_puts(p);
4301 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004302 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 break;
4304 }
4305 for (j = maxlen - lastlen; --j >= 0; )
4306 msg_putchar(' ');
4307 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004308 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 || xp->xp_context == EXPAND_BUFFERS)
4310 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004311 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004312 if (xp->xp_numfiles != -1)
4313 {
4314 char_u *halved_slash;
4315 char_u *exp_path;
4316
4317 /* Expansion was done before and special characters
4318 * were escaped, need to halve backslashes. Also
4319 * $HOME has been replaced with ~/. */
4320 exp_path = expand_env_save_opt(files_found[k], TRUE);
4321 halved_slash = backslash_halve_save(
4322 exp_path != NULL ? exp_path : files_found[k]);
4323 j = mch_isdir(halved_slash != NULL ? halved_slash
4324 : files_found[k]);
4325 vim_free(exp_path);
4326 vim_free(halved_slash);
4327 }
4328 else
4329 /* Expansion was done here, file names are literal. */
4330 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 if (showtail)
4332 p = L_SHOWFILE(k);
4333 else
4334 {
4335 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4336 TRUE);
4337 p = NameBuff;
4338 }
4339 }
4340 else
4341 {
4342 j = FALSE;
4343 p = L_SHOWFILE(k);
4344 }
4345 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4346 }
4347 if (msg_col > 0) /* when not wrapped around */
4348 {
4349 msg_clr_eos();
4350 msg_putchar('\n');
4351 }
4352 out_flush(); /* show one line at a time */
4353 if (got_int)
4354 {
4355 got_int = FALSE;
4356 break;
4357 }
4358 }
4359
4360 /*
4361 * we redraw the command below the lines that we have just listed
4362 * This is a bit tricky, but it saves a lot of screen updating.
4363 */
4364 cmdline_row = msg_row; /* will put it back later */
4365 }
4366
4367 if (xp->xp_numfiles == -1)
4368 FreeWild(num_files, files_found);
4369
4370 return EXPAND_OK;
4371}
4372
4373/*
4374 * Private gettail for showmatches() (and win_redr_status_matches()):
4375 * Find tail of file name path, but ignore trailing "/".
4376 */
4377 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004378sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379{
4380 char_u *p;
4381 char_u *t = s;
4382 int had_sep = FALSE;
4383
4384 for (p = s; *p != NUL; )
4385 {
4386 if (vim_ispathsep(*p)
4387#ifdef BACKSLASH_IN_FILENAME
4388 && !rem_backslash(p)
4389#endif
4390 )
4391 had_sep = TRUE;
4392 else if (had_sep)
4393 {
4394 t = p;
4395 had_sep = FALSE;
4396 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004397 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399 return t;
4400}
4401
4402/*
4403 * Return TRUE if we only need to show the tail of completion matches.
4404 * When not completing file names or there is a wildcard in the path FALSE is
4405 * returned.
4406 */
4407 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410 char_u *s;
4411 char_u *end;
4412
4413 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004414 if (xp->xp_context != EXPAND_FILES
4415 && xp->xp_context != EXPAND_SHELLCMD
4416 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 return FALSE;
4418
4419 end = gettail(xp->xp_pattern);
4420 if (end == xp->xp_pattern) /* there is no path separator */
4421 return FALSE;
4422
4423 for (s = xp->xp_pattern; s < end; s++)
4424 {
4425 /* Skip escaped wildcards. Only when the backslash is not a path
4426 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4427 if (rem_backslash(s))
4428 ++s;
4429 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4430 return FALSE;
4431 }
4432 return TRUE;
4433}
4434
4435/*
4436 * Prepare a string for expansion.
4437 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004438 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 * When expanding other names: The string will be used with regcomp(). Copy
4440 * the name into allocated memory and prepend "^".
4441 */
4442 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004443addstar(
4444 char_u *fname,
4445 int len,
4446 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
4448 char_u *retval;
4449 int i, j;
4450 int new_len;
4451 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004452 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004454 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004455 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004456 && context != EXPAND_SHELLCMD
4457 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 /*
4460 * Matching will be done internally (on something other than files).
4461 * So we convert the file-matching-type wildcards into our kind for
4462 * use with vim_regcomp(). First work out how long it will be:
4463 */
4464
4465 /* For help tags the translation is done in find_help_tags().
4466 * For a tag pattern starting with "/" no translation is needed. */
4467 if (context == EXPAND_HELP
4468 || context == EXPAND_COLORS
4469 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004470 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004471 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004472 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004473 || ((context == EXPAND_TAGS_LISTFILES
4474 || context == EXPAND_TAGS)
4475 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 retval = vim_strnsave(fname, len);
4477 else
4478 {
4479 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4480 for (i = 0; i < len; i++)
4481 {
4482 if (fname[i] == '*' || fname[i] == '~')
4483 new_len++; /* '*' needs to be replaced by ".*"
4484 '~' needs to be replaced by "\~" */
4485
4486 /* Buffer names are like file names. "." should be literal */
4487 if (context == EXPAND_BUFFERS && fname[i] == '.')
4488 new_len++; /* "." becomes "\." */
4489
4490 /* Custom expansion takes care of special things, match
4491 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004492 if ((context == EXPAND_USER_DEFINED
4493 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 new_len++; /* '\' becomes "\\" */
4495 }
4496 retval = alloc(new_len);
4497 if (retval != NULL)
4498 {
4499 retval[0] = '^';
4500 j = 1;
4501 for (i = 0; i < len; i++, j++)
4502 {
4503 /* Skip backslash. But why? At least keep it for custom
4504 * expansion. */
4505 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004506 && context != EXPAND_USER_LIST
4507 && fname[i] == '\\'
4508 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 break;
4510
4511 switch (fname[i])
4512 {
4513 case '*': retval[j++] = '.';
4514 break;
4515 case '~': retval[j++] = '\\';
4516 break;
4517 case '?': retval[j] = '.';
4518 continue;
4519 case '.': if (context == EXPAND_BUFFERS)
4520 retval[j++] = '\\';
4521 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004522 case '\\': if (context == EXPAND_USER_DEFINED
4523 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 retval[j++] = '\\';
4525 break;
4526 }
4527 retval[j] = fname[i];
4528 }
4529 retval[j] = NUL;
4530 }
4531 }
4532 }
4533 else
4534 {
4535 retval = alloc(len + 4);
4536 if (retval != NULL)
4537 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004538 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004541 * Don't add a star to *, ~, ~user, $var or `cmd`.
4542 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 * ~ would be at the start of the file name, but not the tail.
4544 * $ could be anywhere in the tail.
4545 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004546 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 */
4548 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004549 ends_in_star = (len > 0 && retval[len - 1] == '*');
4550#ifndef BACKSLASH_IN_FILENAME
4551 for (i = len - 2; i >= 0; --i)
4552 {
4553 if (retval[i] != '\\')
4554 break;
4555 ends_in_star = !ends_in_star;
4556 }
4557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004559 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 && vim_strchr(tail, '$') == NULL
4561 && vim_strchr(retval, '`') == NULL)
4562 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004563 else if (len > 0 && retval[len - 1] == '$')
4564 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 retval[len] = NUL;
4566 }
4567 }
4568 return retval;
4569}
4570
4571/*
4572 * Must parse the command line so far to work out what context we are in.
4573 * Completion can then be done based on that context.
4574 * This routine sets the variables:
4575 * xp->xp_pattern The start of the pattern to be expanded within
4576 * the command line (ends at the cursor).
4577 * xp->xp_context The type of thing to expand. Will be one of:
4578 *
4579 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4580 * the command line, like an unknown command. Caller
4581 * should beep.
4582 * EXPAND_NOTHING Unrecognised context for completion, use char like
4583 * a normal char, rather than for completion. eg
4584 * :s/^I/
4585 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4586 * it.
4587 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4588 * EXPAND_FILES After command with XFILE set, or after setting
4589 * with P_EXPAND set. eg :e ^I, :w>>^I
4590 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4591 * when we know only directories are of interest. eg
4592 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004593 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4595 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4596 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4597 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4598 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4599 * EXPAND_EVENTS Complete event names
4600 * EXPAND_SYNTAX Complete :syntax command arguments
4601 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4602 * EXPAND_AUGROUP Complete autocommand group names
4603 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4604 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4605 * eg :unmap a^I , :cunab x^I
4606 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4607 * eg :call sub^I
4608 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4609 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4610 * names in expressions, eg :while s^I
4611 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004612 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 */
4614 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004615set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004617 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 if (ccline.cmdfirstc != ':'
4619#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004620 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004621 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622#endif
4623 )
4624 {
4625 xp->xp_context = EXPAND_NOTHING;
4626 return;
4627 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004628 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629}
4630
4631 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004632set_cmd_context(
4633 expand_T *xp,
4634 char_u *str, /* start of command line */
4635 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004636 int col, /* position of cursor */
4637 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638{
4639 int old_char = NUL;
4640 char_u *nextcomm;
4641
4642 /*
4643 * Avoid a UMR warning from Purify, only save the character if it has been
4644 * written before.
4645 */
4646 if (col < len)
4647 old_char = str[col];
4648 str[col] = NUL;
4649 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004650
4651#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004652 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004653 {
4654# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004655 /* pass CMD_SIZE because there is no real command */
4656 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004657# endif
4658 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004659 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004660 {
4661 xp->xp_context = ccline.xp_context;
4662 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004663# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004664 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004665# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004666 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004667 else
4668#endif
4669 while (nextcomm != NULL)
4670 nextcomm = set_one_cmd_context(xp, nextcomm);
4671
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004672 /* Store the string here so that call_user_expand_func() can get to them
4673 * easily. */
4674 xp->xp_line = str;
4675 xp->xp_col = col;
4676
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 str[col] = old_char;
4678}
4679
4680/*
4681 * Expand the command line "str" from context "xp".
4682 * "xp" must have been set by set_cmd_context().
4683 * xp->xp_pattern points into "str", to where the text that is to be expanded
4684 * starts.
4685 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4686 * cursor.
4687 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4688 * key that triggered expansion literally.
4689 * Returns EXPAND_OK otherwise.
4690 */
4691 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004692expand_cmdline(
4693 expand_T *xp,
4694 char_u *str, /* start of command line */
4695 int col, /* position of cursor */
4696 int *matchcount, /* return: nr of matches */
4697 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004700 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
4702 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4703 {
4704 beep_flush();
4705 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4706 }
4707 if (xp->xp_context == EXPAND_NOTHING)
4708 {
4709 /* Caller can use the character as a normal char instead */
4710 return EXPAND_NOTHING;
4711 }
4712
4713 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004714 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4715 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 if (file_str == NULL)
4717 return EXPAND_UNSUCCESSFUL;
4718
Bram Moolenaar94950a92010-12-02 16:01:29 +01004719 if (p_wic)
4720 options += WILD_ICASE;
4721
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004723 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 *matchcount = 0;
4726 *matches = NULL;
4727 }
4728 vim_free(file_str);
4729
4730 return EXPAND_OK;
4731}
4732
4733#ifdef FEAT_MULTI_LANG
4734/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004735 * Cleanup matches for help tags:
4736 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4737 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004739static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004742cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 int i, j;
4745 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004746 char_u buf[4];
4747 char_u *p = buf;
4748
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004749 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004750 {
4751 *p++ = '@';
4752 *p++ = p_hlg[0];
4753 *p++ = p_hlg[1];
4754 }
4755 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
4757 for (i = 0; i < num_file; ++i)
4758 {
4759 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004760 if (len <= 0)
4761 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004762 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
4764 /* Sorting on priority means the same item in another language may
4765 * be anywhere. Search all items for a match up to the "@en". */
4766 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004767 if (j != i && (int)STRLEN(file[j]) == len + 3
4768 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 break;
4770 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004771 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 file[i][len] = NUL;
4773 }
4774 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004775
4776 if (*buf != NUL)
4777 for (i = 0; i < num_file; ++i)
4778 {
4779 len = (int)STRLEN(file[i]) - 3;
4780 if (len <= 0)
4781 continue;
4782 if (STRCMP(file[i] + len, buf) == 0)
4783 {
4784 /* remove the default language */
4785 file[i][len] = NUL;
4786 }
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788}
4789#endif
4790
4791/*
4792 * Do the expansion based on xp->xp_context and "pat".
4793 */
4794 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004795ExpandFromContext(
4796 expand_T *xp,
4797 char_u *pat,
4798 int *num_file,
4799 char_u ***file,
4800 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801{
4802#ifdef FEAT_CMDL_COMPL
4803 regmatch_T regmatch;
4804#endif
4805 int ret;
4806 int flags;
4807
4808 flags = EW_DIR; /* include directories */
4809 if (options & WILD_LIST_NOTFOUND)
4810 flags |= EW_NOTFOUND;
4811 if (options & WILD_ADD_SLASH)
4812 flags |= EW_ADDSLASH;
4813 if (options & WILD_KEEP_ALL)
4814 flags |= EW_KEEPALL;
4815 if (options & WILD_SILENT)
4816 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004817 if (options & WILD_ALLLINKS)
4818 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004820 if (xp->xp_context == EXPAND_FILES
4821 || xp->xp_context == EXPAND_DIRECTORIES
4822 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
4824 /*
4825 * Expand file or directory names.
4826 */
4827 int free_pat = FALSE;
4828 int i;
4829
4830 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4831 * space */
4832 if (xp->xp_backslash != XP_BS_NONE)
4833 {
4834 free_pat = TRUE;
4835 pat = vim_strsave(pat);
4836 for (i = 0; pat[i]; ++i)
4837 if (pat[i] == '\\')
4838 {
4839 if (xp->xp_backslash == XP_BS_THREE
4840 && pat[i + 1] == '\\'
4841 && pat[i + 2] == '\\'
4842 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004843 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (xp->xp_backslash == XP_BS_ONE
4845 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004846 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 }
4849
4850 if (xp->xp_context == EXPAND_FILES)
4851 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004852 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4853 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 else
4855 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004856 if (options & WILD_ICASE)
4857 flags |= EW_ICASE;
4858
Bram Moolenaard7834d32009-12-02 16:14:36 +00004859 /* Expand wildcards, supporting %:h and the like. */
4860 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (free_pat)
4862 vim_free(pat);
4863 return ret;
4864 }
4865
4866 *file = (char_u **)"";
4867 *num_file = 0;
4868 if (xp->xp_context == EXPAND_HELP)
4869 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004870 /* With an empty argument we would get all the help tags, which is
4871 * very slow. Get matches for "help" instead. */
4872 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4873 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 {
4875#ifdef FEAT_MULTI_LANG
4876 cleanup_help_tags(*num_file, *file);
4877#endif
4878 return OK;
4879 }
4880 return FAIL;
4881 }
4882
4883#ifndef FEAT_CMDL_COMPL
4884 return FAIL;
4885#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004886 if (xp->xp_context == EXPAND_SHELLCMD)
4887 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 if (xp->xp_context == EXPAND_OLD_SETTING)
4889 return ExpandOldSetting(num_file, file);
4890 if (xp->xp_context == EXPAND_BUFFERS)
4891 return ExpandBufnames(pat, num_file, file, options);
4892 if (xp->xp_context == EXPAND_TAGS
4893 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4894 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4895 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004896 {
4897 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004898 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4899 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004900 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004902 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004903 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004904 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004905 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004906 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004907 {
4908 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004909 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004910 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004911 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004912 {
4913 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004914 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004915 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004916# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4917 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004918 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004919# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004920 if (xp->xp_context == EXPAND_PACKADD)
4921 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922
4923 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4924 if (regmatch.regprog == NULL)
4925 return FAIL;
4926
4927 /* set ignore-case according to p_ic, p_scs and pat */
4928 regmatch.rm_ic = ignorecase(pat);
4929
4930 if (xp->xp_context == EXPAND_SETTINGS
4931 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4932 ret = ExpandSettings(xp, &regmatch, num_file, file);
4933 else if (xp->xp_context == EXPAND_MAPPINGS)
4934 ret = ExpandMappings(&regmatch, num_file, file);
4935# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4936 else if (xp->xp_context == EXPAND_USER_DEFINED)
4937 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4938# endif
4939 else
4940 {
4941 static struct expgen
4942 {
4943 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004944 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004946 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 } tab[] =
4948 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004949 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4950 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004951 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004952 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004953#ifdef FEAT_CMDHIST
4954 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004957 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004958 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004959 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4960 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4961 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962#endif
4963#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004964 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4965 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4966 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4967 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968#endif
4969#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004970 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4971 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#endif
4973#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004974 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004976#ifdef FEAT_PROFILE
4977 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4978#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004979 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004980 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4981 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004982#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004983 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004984#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004985#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004986 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004987#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004988#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004989 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4992 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004993 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4994 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004996 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004997 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004998 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 };
5000 int i;
5001
5002 /*
5003 * Find a context in the table and call the ExpandGeneric() with the
5004 * right function to do the expansion.
5005 */
5006 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005007 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 if (xp->xp_context == tab[i].context)
5009 {
5010 if (tab[i].ic)
5011 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005012 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005013 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 break;
5015 }
5016 }
5017
Bram Moolenaar473de612013-06-08 18:19:48 +02005018 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019
5020 return ret;
5021#endif /* FEAT_CMDL_COMPL */
5022}
5023
5024#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5025/*
5026 * Expand a list of names.
5027 *
5028 * Generic function for command line completion. It calls a function to
5029 * obtain strings, one by one. The strings are matched against a regexp
5030 * program. Matching strings are copied into an array, which is returned.
5031 *
5032 * Returns OK when no problems encountered, FAIL for error (out of memory).
5033 */
5034 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005035ExpandGeneric(
5036 expand_T *xp,
5037 regmatch_T *regmatch,
5038 int *num_file,
5039 char_u ***file,
5040 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005042 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043{
5044 int i;
5045 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005046 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 char_u *str;
5048
5049 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005050 * round == 0: count the number of matching names
5051 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005053 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 for (i = 0; ; ++i)
5056 {
5057 str = (*func)(xp, i);
5058 if (str == NULL) /* end of list */
5059 break;
5060 if (*str == NUL) /* skip empty strings */
5061 continue;
5062
5063 if (vim_regexec(regmatch, str, (colnr_T)0))
5064 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005065 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005067 if (escaped)
5068 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5069 else
5070 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 (*file)[count] = str;
5072#ifdef FEAT_MENU
5073 if (func == get_menu_names && str != NULL)
5074 {
5075 /* test for separator added by get_menu_names() */
5076 str += STRLEN(str) - 1;
5077 if (*str == '\001')
5078 *str = '.';
5079 }
5080#endif
5081 }
5082 ++count;
5083 }
5084 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005085 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
5087 if (count == 0)
5088 return OK;
5089 *num_file = count;
5090 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5091 if (*file == NULL)
5092 {
5093 *file = (char_u **)"";
5094 return FAIL;
5095 }
5096 count = 0;
5097 }
5098 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005099
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005100 /* Sort the results. Keep menu's in the specified order. */
5101 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005102 {
5103 if (xp->xp_context == EXPAND_EXPRESSION
5104 || xp->xp_context == EXPAND_FUNCTIONS
5105 || xp->xp_context == EXPAND_USER_FUNC)
5106 /* <SNR> functions should be sorted to the end. */
5107 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5108 sort_func_compare);
5109 else
5110 sort_strings(*file, *num_file);
5111 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005112
Bram Moolenaar4f688582007-07-24 12:34:30 +00005113#ifdef FEAT_CMDL_COMPL
5114 /* Reset the variables used for special highlight names expansion, so that
5115 * they don't show up when getting normal highlight names by ID. */
5116 reset_expand_highlight();
5117#endif
5118
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 return OK;
5120}
5121
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005122/*
5123 * Complete a shell command.
5124 * Returns FAIL or OK;
5125 */
5126 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005127expand_shellcmd(
5128 char_u *filepat, /* pattern to match with command names */
5129 int *num_file, /* return: number of matches */
5130 char_u ***file, /* return: array with matches */
5131 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005132{
5133 char_u *pat;
5134 int i;
5135 char_u *path;
5136 int mustfree = FALSE;
5137 garray_T ga;
5138 char_u *buf = alloc(MAXPATHL);
5139 size_t l;
5140 char_u *s, *e;
5141 int flags = flagsarg;
5142 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005143 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005144
5145 if (buf == NULL)
5146 return FAIL;
5147
5148 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5149 * space */
5150 pat = vim_strsave(filepat);
5151 for (i = 0; pat[i]; ++i)
5152 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005153 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005154
Bram Moolenaarb5971142015-03-21 17:32:19 +01005155 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005156
5157 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005158 if (mch_isFullName(pat))
5159 path = (char_u *)" ";
5160 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005161 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5162 path = (char_u *)".";
5163 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005164 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005165 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005166 if (path == NULL)
5167 path = (char_u *)"";
5168 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005169
5170 /*
5171 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005172 * collect them in "ga". When "." is not in $PATH also expand for the
5173 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005174 */
5175 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005176 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005177 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005178 if (*s == NUL)
5179 {
5180 if (did_curdir)
5181 break;
5182 /* Find directories in the current directory, path is empty. */
5183 did_curdir = TRUE;
5184 }
5185 else if (*s == '.')
5186 did_curdir = TRUE;
5187
Bram Moolenaar68c31742006-09-02 15:54:18 +00005188 if (*s == ' ')
5189 ++s; /* Skip space used for absolute path name. */
5190
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005191#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005192 e = vim_strchr(s, ';');
5193#else
5194 e = vim_strchr(s, ':');
5195#endif
5196 if (e == NULL)
5197 e = s + STRLEN(s);
5198
5199 l = e - s;
5200 if (l > MAXPATHL - 5)
5201 break;
5202 vim_strncpy(buf, s, l);
5203 add_pathsep(buf);
5204 l = STRLEN(buf);
5205 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5206
5207 /* Expand matches in one directory of $PATH. */
5208 ret = expand_wildcards(1, &buf, num_file, file, flags);
5209 if (ret == OK)
5210 {
5211 if (ga_grow(&ga, *num_file) == FAIL)
5212 FreeWild(*num_file, *file);
5213 else
5214 {
5215 for (i = 0; i < *num_file; ++i)
5216 {
5217 s = (*file)[i];
5218 if (STRLEN(s) > l)
5219 {
5220 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005221 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005222 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5223 }
5224 else
5225 vim_free(s);
5226 }
5227 vim_free(*file);
5228 }
5229 }
5230 if (*e != NUL)
5231 ++e;
5232 }
5233 *file = ga.ga_data;
5234 *num_file = ga.ga_len;
5235
5236 vim_free(buf);
5237 vim_free(pat);
5238 if (mustfree)
5239 vim_free(path);
5240 return OK;
5241}
5242
5243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005245static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, char_u **, int), expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00005246
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005248 * Call "user_expand_func()" to invoke a user defined Vim script function and
5249 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005251 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005252call_user_expand_func(
5253 void *(*user_expand_func)(char_u *, int, char_u **, int),
5254 expand_T *xp,
5255 int *num_file,
5256 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005258 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005259 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005262 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005263 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaarb7515462013-06-29 12:58:33 +02005265 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005266 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 *num_file = 0;
5268 *file = NULL;
5269
Bram Moolenaarb7515462013-06-29 12:58:33 +02005270 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005271 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005272 keep = ccline.cmdbuff[ccline.cmdlen];
5273 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005274 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005275
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005276 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005277 args[1] = xp->xp_line;
5278 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 args[2] = num;
5280
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005281 /* Save the cmdline, we don't know what the function may do. */
5282 save_ccline = ccline;
5283 ccline.cmdbuff = NULL;
5284 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005286
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005287 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005288
5289 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005291 if (ccline.cmdbuff != NULL)
5292 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005293
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005294 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005295 return ret;
5296}
5297
5298/*
5299 * Expand names with a function defined by the user.
5300 */
5301 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302ExpandUserDefined(
5303 expand_T *xp,
5304 regmatch_T *regmatch,
5305 int *num_file,
5306 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005307{
5308 char_u *retstr;
5309 char_u *s;
5310 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005311 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005312 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005313 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005314
5315 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5316 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 return FAIL;
5318
5319 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005320 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 e = vim_strchr(s, '\n');
5323 if (e == NULL)
5324 e = s + STRLEN(s);
5325 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005326 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005328 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5329 *e = keep;
5330
5331 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005333 if (ga_grow(&ga, 1) == FAIL)
5334 break;
5335 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5336 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (*e != NUL)
5340 ++e;
5341 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005342 vim_free(retstr);
5343 *file = ga.ga_data;
5344 *num_file = ga.ga_len;
5345 return OK;
5346}
5347
5348/*
5349 * Expand names with a list returned by a function defined by the user.
5350 */
5351 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352ExpandUserList(
5353 expand_T *xp,
5354 int *num_file,
5355 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005356{
5357 list_T *retlist;
5358 listitem_T *li;
5359 garray_T ga;
5360
5361 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5362 if (retlist == NULL)
5363 return FAIL;
5364
5365 ga_init2(&ga, (int)sizeof(char *), 3);
5366 /* Loop over the items in the list. */
5367 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5368 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005369 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5370 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005371
5372 if (ga_grow(&ga, 1) == FAIL)
5373 break;
5374
5375 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005376 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005377 ++ga.ga_len;
5378 }
5379 list_unref(retlist);
5380
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 *file = ga.ga_data;
5382 *num_file = ga.ga_len;
5383 return OK;
5384}
5385#endif
5386
5387/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005388 * Expand color scheme, compiler or filetype names.
5389 * Search from 'runtimepath':
5390 * 'runtimepath'/{dirnames}/{pat}.vim
5391 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5392 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5393 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5394 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005395 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 */
5397 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005398ExpandRTDir(
5399 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005400 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005401 int *num_file,
5402 char_u ***file,
5403 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 char_u *s;
5406 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005407 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005409 int i;
5410 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
5412 *num_file = 0;
5413 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005414 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005415 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005417 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005419 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5420 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005422 ga_clear_strings(&ga);
5423 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005425 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005426 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005427 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005429
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005430 if (flags & DIP_START) {
5431 for (i = 0; dirnames[i] != NULL; ++i)
5432 {
5433 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5434 if (s == NULL)
5435 {
5436 ga_clear_strings(&ga);
5437 return FAIL;
5438 }
5439 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5440 globpath(p_pp, s, &ga, 0);
5441 vim_free(s);
5442 }
5443 }
5444
5445 if (flags & DIP_OPT) {
5446 for (i = 0; dirnames[i] != NULL; ++i)
5447 {
5448 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5449 if (s == NULL)
5450 {
5451 ga_clear_strings(&ga);
5452 return FAIL;
5453 }
5454 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5455 globpath(p_pp, s, &ga, 0);
5456 vim_free(s);
5457 }
5458 }
5459
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005460 for (i = 0; i < ga.ga_len; ++i)
5461 {
5462 match = ((char_u **)ga.ga_data)[i];
5463 s = match;
5464 e = s + STRLEN(s);
5465 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5466 {
5467 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005468 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005469 if (s < match || vim_ispathsep(*s))
5470 break;
5471 ++s;
5472 *e = NUL;
5473 mch_memmove(match, s, e - s + 1);
5474 }
5475 }
5476
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005477 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005478 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005479
5480 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005481 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005482 remove_duplicates(&ga);
5483
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 *file = ga.ga_data;
5485 *num_file = ga.ga_len;
5486 return OK;
5487}
5488
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005489/*
5490 * Expand loadplugin names:
5491 * 'packpath'/pack/ * /opt/{pat}
5492 */
5493 static int
5494ExpandPackAddDir(
5495 char_u *pat,
5496 int *num_file,
5497 char_u ***file)
5498{
5499 char_u *s;
5500 char_u *e;
5501 char_u *match;
5502 garray_T ga;
5503 int i;
5504 int pat_len;
5505
5506 *num_file = 0;
5507 *file = NULL;
5508 pat_len = (int)STRLEN(pat);
5509 ga_init2(&ga, (int)sizeof(char *), 10);
5510
5511 s = alloc((unsigned)(pat_len + 26));
5512 if (s == NULL)
5513 {
5514 ga_clear_strings(&ga);
5515 return FAIL;
5516 }
5517 sprintf((char *)s, "pack/*/opt/%s*", pat);
5518 globpath(p_pp, s, &ga, 0);
5519 vim_free(s);
5520
5521 for (i = 0; i < ga.ga_len; ++i)
5522 {
5523 match = ((char_u **)ga.ga_data)[i];
5524 s = gettail(match);
5525 e = s + STRLEN(s);
5526 mch_memmove(match, s, e - s + 1);
5527 }
5528
5529 if (ga.ga_len == 0)
5530 return FAIL;
5531
5532 /* Sort and remove duplicates which can happen when specifying multiple
5533 * directories in dirnames. */
5534 remove_duplicates(&ga);
5535
5536 *file = ga.ga_data;
5537 *num_file = ga.ga_len;
5538 return OK;
5539}
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#endif
5542
5543#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5544/*
5545 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005546 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005548 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549globpath(
5550 char_u *path,
5551 char_u *file,
5552 garray_T *ga,
5553 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554{
5555 expand_T xpc;
5556 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 int num_p;
5559 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561 buf = alloc(MAXPATHL);
5562 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005563 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005565 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005567
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 /* Loop over all entries in {path}. */
5569 while (*path != NUL)
5570 {
5571 /* Copy one item of the path to buf[] and concatenate the file name. */
5572 copy_option_part(&path, buf, MAXPATHL, ",");
5573 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5574 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005575# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005576 /* Using the platform's path separator (\) makes vim incorrectly
5577 * treat it as an escape character, use '/' instead. */
5578 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5579 STRCAT(buf, "/");
5580# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005582# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005584 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5585 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005587 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005589 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 for (i = 0; i < num_p; ++i)
5592 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005593 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005594 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005595 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005598
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 FreeWild(num_p, p);
5600 }
5601 }
5602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603
5604 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605}
5606
5607#endif
5608
5609#if defined(FEAT_CMDHIST) || defined(PROTO)
5610
5611/*********************************
5612 * Command line history stuff *
5613 *********************************/
5614
5615/*
5616 * Translate a history character to the associated type number.
5617 */
5618 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005619hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
5621 if (c == ':')
5622 return HIST_CMD;
5623 if (c == '=')
5624 return HIST_EXPR;
5625 if (c == '@')
5626 return HIST_INPUT;
5627 if (c == '>')
5628 return HIST_DEBUG;
5629 return HIST_SEARCH; /* must be '?' or '/' */
5630}
5631
5632/*
5633 * Table of history names.
5634 * These names are used in :history and various hist...() functions.
5635 * It is sufficient to give the significant prefix of a history name.
5636 */
5637
5638static char *(history_names[]) =
5639{
5640 "cmd",
5641 "search",
5642 "expr",
5643 "input",
5644 "debug",
5645 NULL
5646};
5647
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005648#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5649/*
5650 * Function given to ExpandGeneric() to obtain the possible first
5651 * arguments of the ":history command.
5652 */
5653 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005654get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005655{
5656 static char_u compl[2] = { NUL, NUL };
5657 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005658 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005659 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5660
5661 if (idx < short_names_count)
5662 {
5663 compl[0] = (char_u)short_names[idx];
5664 return compl;
5665 }
5666 if (idx < short_names_count + history_name_count)
5667 return (char_u *)history_names[idx - short_names_count];
5668 if (idx == short_names_count + history_name_count)
5669 return (char_u *)"all";
5670 return NULL;
5671}
5672#endif
5673
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674/*
5675 * init_history() - Initialize the command line history.
5676 * Also used to re-allocate the history when the size changes.
5677 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005679init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680{
5681 int newlen; /* new length of history table */
5682 histentry_T *temp;
5683 int i;
5684 int j;
5685 int type;
5686
5687 /*
5688 * If size of history table changed, reallocate it
5689 */
5690 newlen = (int)p_hi;
5691 if (newlen != hislen) /* history length changed */
5692 {
5693 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5694 {
5695 if (newlen)
5696 {
5697 temp = (histentry_T *)lalloc(
5698 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5699 if (temp == NULL) /* out of memory! */
5700 {
5701 if (type == 0) /* first one: just keep the old length */
5702 {
5703 newlen = hislen;
5704 break;
5705 }
5706 /* Already changed one table, now we can only have zero
5707 * length for all tables. */
5708 newlen = 0;
5709 type = -1;
5710 continue;
5711 }
5712 }
5713 else
5714 temp = NULL;
5715 if (newlen == 0 || temp != NULL)
5716 {
5717 if (hisidx[type] < 0) /* there are no entries yet */
5718 {
5719 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005720 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 else if (newlen > hislen) /* array becomes bigger */
5723 {
5724 for (i = 0; i <= hisidx[type]; ++i)
5725 temp[i] = history[type][i];
5726 j = i;
5727 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005728 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 for ( ; j < hislen; ++i, ++j)
5730 temp[i] = history[type][j];
5731 }
5732 else /* array becomes smaller or 0 */
5733 {
5734 j = hisidx[type];
5735 for (i = newlen - 1; ; --i)
5736 {
5737 if (i >= 0) /* copy newest entries */
5738 temp[i] = history[type][j];
5739 else /* remove older entries */
5740 vim_free(history[type][j].hisstr);
5741 if (--j < 0)
5742 j = hislen - 1;
5743 if (j == hisidx[type])
5744 break;
5745 }
5746 hisidx[type] = newlen - 1;
5747 }
5748 vim_free(history[type]);
5749 history[type] = temp;
5750 }
5751 }
5752 hislen = newlen;
5753 }
5754}
5755
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005756 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005757clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005758{
5759 hisptr->hisnum = 0;
5760 hisptr->viminfo = FALSE;
5761 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005762 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005763}
5764
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765/*
5766 * Check if command line 'str' is already in history.
5767 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5768 */
5769 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005770in_history(
5771 int type,
5772 char_u *str,
5773 int move_to_front, /* Move the entry to the front if it exists */
5774 int sep,
5775 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 int i;
5778 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005779 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 if (hisidx[type] < 0)
5782 return FALSE;
5783 i = hisidx[type];
5784 do
5785 {
5786 if (history[type][i].hisstr == NULL)
5787 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005788
5789 /* For search history, check that the separator character matches as
5790 * well. */
5791 p = history[type][i].hisstr;
5792 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005793 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005794 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 {
5796 if (!move_to_front)
5797 return TRUE;
5798 last_i = i;
5799 break;
5800 }
5801 if (--i < 0)
5802 i = hislen - 1;
5803 } while (i != hisidx[type]);
5804
5805 if (last_i >= 0)
5806 {
5807 str = history[type][i].hisstr;
5808 while (i != hisidx[type])
5809 {
5810 if (++i >= hislen)
5811 i = 0;
5812 history[type][last_i] = history[type][i];
5813 last_i = i;
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005816 history[type][i].viminfo = FALSE;
5817 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005818 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 return TRUE;
5820 }
5821 return FALSE;
5822}
5823
5824/*
5825 * Convert history name (from table above) to its HIST_ equivalent.
5826 * When "name" is empty, return "cmd" history.
5827 * Returns -1 for unknown history name.
5828 */
5829 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005830get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831{
5832 int i;
5833 int len = (int)STRLEN(name);
5834
5835 /* No argument: use current history. */
5836 if (len == 0)
5837 return hist_char2type(ccline.cmdfirstc);
5838
5839 for (i = 0; history_names[i] != NULL; ++i)
5840 if (STRNICMP(name, history_names[i], len) == 0)
5841 return i;
5842
5843 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5844 return hist_char2type(name[0]);
5845
5846 return -1;
5847}
5848
5849static int last_maptick = -1; /* last seen maptick */
5850
5851/*
5852 * Add the given string to the given history. If the string is already in the
5853 * history then it is moved to the front. "histype" may be one of he HIST_
5854 * values.
5855 */
5856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005857add_to_history(
5858 int histype,
5859 char_u *new_entry,
5860 int in_map, /* consider maptick when inside a mapping */
5861 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 histentry_T *hisptr;
5864 int len;
5865
5866 if (hislen == 0) /* no history */
5867 return;
5868
Bram Moolenaara939e432013-11-09 05:30:26 +01005869 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5870 return;
5871
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 /*
5873 * Searches inside the same mapping overwrite each other, so that only
5874 * the last line is kept. Be careful not to remove a line that was moved
5875 * down, only lines that were added.
5876 */
5877 if (histype == HIST_SEARCH && in_map)
5878 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005879 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 {
5881 /* Current line is from the same mapping, remove it */
5882 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5883 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005884 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 --hisnum[histype];
5886 if (--hisidx[HIST_SEARCH] < 0)
5887 hisidx[HIST_SEARCH] = hislen - 1;
5888 }
5889 last_maptick = -1;
5890 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005891 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 {
5893 if (++hisidx[histype] == hislen)
5894 hisidx[histype] = 0;
5895 hisptr = &history[histype][hisidx[histype]];
5896 vim_free(hisptr->hisstr);
5897
5898 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005899 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5901 if (hisptr->hisstr != NULL)
5902 hisptr->hisstr[len + 1] = sep;
5903
5904 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005905 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005906 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 if (histype == HIST_SEARCH && in_map)
5908 last_maptick = maptick;
5909 }
5910}
5911
5912#if defined(FEAT_EVAL) || defined(PROTO)
5913
5914/*
5915 * Get identifier of newest history entry.
5916 * "histype" may be one of the HIST_ values.
5917 */
5918 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005919get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5922 || hisidx[histype] < 0)
5923 return -1;
5924
5925 return history[histype][hisidx[histype]].hisnum;
5926}
5927
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005928/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 * Calculate history index from a number:
5930 * num > 0: seen as identifying number of a history entry
5931 * num < 0: relative position in history wrt newest entry
5932 * "histype" may be one of the HIST_ values.
5933 */
5934 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005935calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936{
5937 int i;
5938 histentry_T *hist;
5939 int wrapped = FALSE;
5940
5941 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5942 || (i = hisidx[histype]) < 0 || num == 0)
5943 return -1;
5944
5945 hist = history[histype];
5946 if (num > 0)
5947 {
5948 while (hist[i].hisnum > num)
5949 if (--i < 0)
5950 {
5951 if (wrapped)
5952 break;
5953 i += hislen;
5954 wrapped = TRUE;
5955 }
5956 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5957 return i;
5958 }
5959 else if (-num <= hislen)
5960 {
5961 i += num + 1;
5962 if (i < 0)
5963 i += hislen;
5964 if (hist[i].hisstr != NULL)
5965 return i;
5966 }
5967 return -1;
5968}
5969
5970/*
5971 * Get a history entry by its index.
5972 * "histype" may be one of the HIST_ values.
5973 */
5974 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005975get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 idx = calc_hist_idx(histype, idx);
5978 if (idx >= 0)
5979 return history[histype][idx].hisstr;
5980 else
5981 return (char_u *)"";
5982}
5983
5984/*
5985 * Clear all entries of a history.
5986 * "histype" may be one of the HIST_ values.
5987 */
5988 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005989clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990{
5991 int i;
5992 histentry_T *hisptr;
5993
5994 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5995 {
5996 hisptr = history[histype];
5997 for (i = hislen; i--;)
5998 {
5999 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006000 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006001 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 }
6003 hisidx[histype] = -1; /* mark history as cleared */
6004 hisnum[histype] = 0; /* reset identifier counter */
6005 return OK;
6006 }
6007 return FAIL;
6008}
6009
6010/*
6011 * Remove all entries matching {str} from a history.
6012 * "histype" may be one of the HIST_ values.
6013 */
6014 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006015del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 regmatch_T regmatch;
6018 histentry_T *hisptr;
6019 int idx;
6020 int i;
6021 int last;
6022 int found = FALSE;
6023
6024 regmatch.regprog = NULL;
6025 regmatch.rm_ic = FALSE; /* always match case */
6026 if (hislen != 0
6027 && histype >= 0
6028 && histype < HIST_COUNT
6029 && *str != NUL
6030 && (idx = hisidx[histype]) >= 0
6031 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6032 != NULL)
6033 {
6034 i = last = idx;
6035 do
6036 {
6037 hisptr = &history[histype][i];
6038 if (hisptr->hisstr == NULL)
6039 break;
6040 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6041 {
6042 found = TRUE;
6043 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006044 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046 else
6047 {
6048 if (i != last)
6049 {
6050 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006051 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 }
6053 if (--last < 0)
6054 last += hislen;
6055 }
6056 if (--i < 0)
6057 i += hislen;
6058 } while (i != idx);
6059 if (history[histype][idx].hisstr == NULL)
6060 hisidx[histype] = -1;
6061 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006062 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 return found;
6064}
6065
6066/*
6067 * Remove an indexed entry from a history.
6068 * "histype" may be one of the HIST_ values.
6069 */
6070 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006071del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072{
6073 int i, j;
6074
6075 i = calc_hist_idx(histype, idx);
6076 if (i < 0)
6077 return FALSE;
6078 idx = hisidx[histype];
6079 vim_free(history[histype][i].hisstr);
6080
6081 /* When deleting the last added search string in a mapping, reset
6082 * last_maptick, so that the last added search string isn't deleted again.
6083 */
6084 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6085 last_maptick = -1;
6086
6087 while (i != idx)
6088 {
6089 j = (i + 1) % hislen;
6090 history[histype][i] = history[histype][j];
6091 i = j;
6092 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006093 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 if (--i < 0)
6095 i += hislen;
6096 hisidx[histype] = i;
6097 return TRUE;
6098}
6099
6100#endif /* FEAT_EVAL */
6101
6102#if defined(FEAT_CRYPT) || defined(PROTO)
6103/*
6104 * Very specific function to remove the value in ":set key=val" from the
6105 * history.
6106 */
6107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006108remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109{
6110 char_u *p;
6111 int i;
6112
6113 i = hisidx[HIST_CMD];
6114 if (i < 0)
6115 return;
6116 p = history[HIST_CMD][i].hisstr;
6117 if (p != NULL)
6118 for ( ; *p; ++p)
6119 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6120 {
6121 p = vim_strchr(p + 3, '=');
6122 if (p == NULL)
6123 break;
6124 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006125 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 if (p[i] == '\\' && p[i + 1])
6127 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006128 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 --p;
6130 }
6131}
6132#endif
6133
6134#endif /* FEAT_CMDHIST */
6135
Bram Moolenaar064154c2016-03-19 22:50:43 +01006136#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006137/*
6138 * Get pointer to the command line info to use. cmdline_paste() may clear
6139 * ccline and put the previous value in prev_ccline.
6140 */
6141 static struct cmdline_info *
6142get_ccline_ptr(void)
6143{
6144 if ((State & CMDLINE) == 0)
6145 return NULL;
6146 if (ccline.cmdbuff != NULL)
6147 return &ccline;
6148 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6149 return &prev_ccline;
6150 return NULL;
6151}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006152#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006153
Bram Moolenaar064154c2016-03-19 22:50:43 +01006154#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006155/*
6156 * Get the current command line in allocated memory.
6157 * Only works when the command line is being edited.
6158 * Returns NULL when something is wrong.
6159 */
6160 char_u *
6161get_cmdline_str(void)
6162{
6163 struct cmdline_info *p = get_ccline_ptr();
6164
6165 if (p == NULL)
6166 return NULL;
6167 return vim_strnsave(p->cmdbuff, p->cmdlen);
6168}
6169
6170/*
6171 * Get the current command line position, counted in bytes.
6172 * Zero is the first position.
6173 * Only works when the command line is being edited.
6174 * Returns -1 when something is wrong.
6175 */
6176 int
6177get_cmdline_pos(void)
6178{
6179 struct cmdline_info *p = get_ccline_ptr();
6180
6181 if (p == NULL)
6182 return -1;
6183 return p->cmdpos;
6184}
6185
6186/*
6187 * Set the command line byte position to "pos". Zero is the first position.
6188 * Only works when the command line is being edited.
6189 * Returns 1 when failed, 0 when OK.
6190 */
6191 int
6192set_cmdline_pos(
6193 int pos)
6194{
6195 struct cmdline_info *p = get_ccline_ptr();
6196
6197 if (p == NULL)
6198 return 1;
6199
6200 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6201 * changed the command line. */
6202 if (pos < 0)
6203 new_cmdpos = 0;
6204 else
6205 new_cmdpos = pos;
6206 return 0;
6207}
6208#endif
6209
6210#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6211/*
6212 * Get the current command-line type.
6213 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6214 * Only works when the command line is being edited.
6215 * Returns NUL when something is wrong.
6216 */
6217 int
6218get_cmdline_type(void)
6219{
6220 struct cmdline_info *p = get_ccline_ptr();
6221
6222 if (p == NULL)
6223 return NUL;
6224 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006225 return
6226# ifdef FEAT_EVAL
6227 (p->input_fn) ? '@' :
6228# endif
6229 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006230 return p->cmdfirstc;
6231}
6232#endif
6233
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6235/*
6236 * Get indices "num1,num2" that specify a range within a list (not a range of
6237 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6238 * Returns OK if parsed successfully, otherwise FAIL.
6239 */
6240 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006241get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242{
6243 int len;
6244 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006245 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246
6247 *str = skipwhite(*str);
6248 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6249 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006250 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251 *str += len;
6252 *num1 = (int)num;
6253 first = TRUE;
6254 }
6255 *str = skipwhite(*str);
6256 if (**str == ',') /* parse "to" part of range */
6257 {
6258 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006259 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 if (len > 0)
6261 {
6262 *num2 = (int)num;
6263 *str = skipwhite(*str + len);
6264 }
6265 else if (!first) /* no number given at all */
6266 return FAIL;
6267 }
6268 else if (first) /* only one number given */
6269 *num2 = *num1;
6270 return OK;
6271}
6272#endif
6273
6274#if defined(FEAT_CMDHIST) || defined(PROTO)
6275/*
6276 * :history command - print a history
6277 */
6278 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
6281 histentry_T *hist;
6282 int histype1 = HIST_CMD;
6283 int histype2 = HIST_CMD;
6284 int hisidx1 = 1;
6285 int hisidx2 = -1;
6286 int idx;
6287 int i, j, k;
6288 char_u *end;
6289 char_u *arg = eap->arg;
6290
6291 if (hislen == 0)
6292 {
6293 MSG(_("'history' option is zero"));
6294 return;
6295 }
6296
6297 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6298 {
6299 end = arg;
6300 while (ASCII_ISALPHA(*end)
6301 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6302 end++;
6303 i = *end;
6304 *end = NUL;
6305 histype1 = get_histtype(arg);
6306 if (histype1 == -1)
6307 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006308 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 {
6310 histype1 = 0;
6311 histype2 = HIST_COUNT-1;
6312 }
6313 else
6314 {
6315 *end = i;
6316 EMSG(_(e_trailing));
6317 return;
6318 }
6319 }
6320 else
6321 histype2 = histype1;
6322 *end = i;
6323 }
6324 else
6325 end = arg;
6326 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6327 {
6328 EMSG(_(e_trailing));
6329 return;
6330 }
6331
6332 for (; !got_int && histype1 <= histype2; ++histype1)
6333 {
6334 STRCPY(IObuff, "\n # ");
6335 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6336 MSG_PUTS_TITLE(IObuff);
6337 idx = hisidx[histype1];
6338 hist = history[histype1];
6339 j = hisidx1;
6340 k = hisidx2;
6341 if (j < 0)
6342 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6343 if (k < 0)
6344 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6345 if (idx >= 0 && j <= k)
6346 for (i = idx + 1; !got_int; ++i)
6347 {
6348 if (i == hislen)
6349 i = 0;
6350 if (hist[i].hisstr != NULL
6351 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6352 {
6353 msg_putchar('\n');
6354 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6355 hist[i].hisnum);
6356 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6357 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006358 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 else
6360 STRCAT(IObuff, hist[i].hisstr);
6361 msg_outtrans(IObuff);
6362 out_flush();
6363 }
6364 if (i == idx)
6365 break;
6366 }
6367 }
6368}
6369#endif
6370
6371#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006372/*
6373 * Buffers for history read from a viminfo file. Only valid while reading.
6374 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006375static histentry_T *viminfo_history[HIST_COUNT] =
6376 {NULL, NULL, NULL, NULL, NULL};
6377static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6378static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379static int viminfo_add_at_front = FALSE;
6380
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006381static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382
6383/*
6384 * Translate a history type number to the associated character.
6385 */
6386 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006387hist_type2char(
6388 int type,
6389 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 if (type == HIST_CMD)
6392 return ':';
6393 if (type == HIST_SEARCH)
6394 {
6395 if (use_question)
6396 return '?';
6397 else
6398 return '/';
6399 }
6400 if (type == HIST_EXPR)
6401 return '=';
6402 return '@';
6403}
6404
6405/*
6406 * Prepare for reading the history from the viminfo file.
6407 * This allocates history arrays to store the read history lines.
6408 */
6409 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 int i;
6413 int num;
6414 int type;
6415 int len;
6416
6417 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006418 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 if (asklen > hislen)
6420 asklen = hislen;
6421
6422 for (type = 0; type < HIST_COUNT; ++type)
6423 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006424 /* Count the number of empty spaces in the history list. Entries read
6425 * from viminfo previously are also considered empty. If there are
6426 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006428 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 num++;
6430 len = asklen;
6431 if (num > len)
6432 len = num;
6433 if (len <= 0)
6434 viminfo_history[type] = NULL;
6435 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006436 viminfo_history[type] = (histentry_T *)lalloc(
6437 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006438 if (viminfo_history[type] == NULL)
6439 len = 0;
6440 viminfo_hislen[type] = len;
6441 viminfo_hisidx[type] = 0;
6442 }
6443}
6444
6445/*
6446 * Accept a line from the viminfo, store it in the history array when it's
6447 * new.
6448 */
6449 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006450read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451{
6452 int type;
6453 long_u len;
6454 char_u *val;
6455 char_u *p;
6456
6457 type = hist_char2type(virp->vir_line[0]);
6458 if (viminfo_hisidx[type] < viminfo_hislen[type])
6459 {
6460 val = viminfo_readstring(virp, 1, TRUE);
6461 if (val != NULL && *val != NUL)
6462 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006463 int sep = (*val == ' ' ? NUL : *val);
6464
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006466 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 {
6468 /* Need to re-allocate to append the separator byte. */
6469 len = STRLEN(val);
6470 p = lalloc(len + 2, TRUE);
6471 if (p != NULL)
6472 {
6473 if (type == HIST_SEARCH)
6474 {
6475 /* Search entry: Move the separator from the first
6476 * column to after the NUL. */
6477 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006478 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479 }
6480 else
6481 {
6482 /* Not a search entry: No separator in the viminfo
6483 * file, add a NUL separator. */
6484 mch_memmove(p, val, (size_t)len + 1);
6485 p[len + 1] = NUL;
6486 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006487 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6488 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006489 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6490 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006491 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492 }
6493 }
6494 }
6495 vim_free(val);
6496 }
6497 return viminfo_readline(virp);
6498}
6499
Bram Moolenaar07219f92013-04-14 23:19:36 +02006500/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006501 * Accept a new style history line from the viminfo, store it in the history
6502 * array when it's new.
6503 */
6504 void
6505handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006506 garray_T *values,
6507 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006508{
6509 int type;
6510 long_u len;
6511 char_u *val;
6512 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006513 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006514
6515 /* Check the format:
6516 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006517 if (values->ga_len < 4
6518 || vp[0].bv_type != BVAL_NR
6519 || vp[1].bv_type != BVAL_NR
6520 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6521 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006522 return;
6523
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006524 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006525 if (type >= HIST_COUNT)
6526 return;
6527 if (viminfo_hisidx[type] < viminfo_hislen[type])
6528 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006529 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006530 if (val != NULL && *val != NUL)
6531 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006532 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6533 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006534 int idx;
6535 int overwrite = FALSE;
6536
6537 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6538 {
6539 /* If lines were written by an older Vim we need to avoid
6540 * getting duplicates. See if the entry already exists. */
6541 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6542 {
6543 p = viminfo_history[type][idx].hisstr;
6544 if (STRCMP(val, p) == 0
6545 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6546 {
6547 overwrite = TRUE;
6548 break;
6549 }
6550 }
6551
6552 if (!overwrite)
6553 {
6554 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006555 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006556 p = lalloc(len + 2, TRUE);
6557 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006558 else
6559 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006560 if (p != NULL)
6561 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006562 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006563 if (!overwrite)
6564 {
6565 mch_memmove(p, val, (size_t)len + 1);
6566 /* Put the separator after the NUL. */
6567 p[len + 1] = sep;
6568 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006569 viminfo_history[type][idx].hisnum = 0;
6570 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006571 viminfo_hisidx[type]++;
6572 }
6573 }
6574 }
6575 }
6576 }
6577}
6578
6579/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006580 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006581 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006582 static void
6583concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584{
6585 int idx;
6586 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006587
6588 idx = hisidx[type] + viminfo_hisidx[type];
6589 if (idx >= hislen)
6590 idx -= hislen;
6591 else if (idx < 0)
6592 idx = hislen - 1;
6593 if (viminfo_add_at_front)
6594 hisidx[type] = idx;
6595 else
6596 {
6597 if (hisidx[type] == -1)
6598 hisidx[type] = hislen - 1;
6599 do
6600 {
6601 if (history[type][idx].hisstr != NULL
6602 || history[type][idx].viminfo)
6603 break;
6604 if (++idx == hislen)
6605 idx = 0;
6606 } while (idx != hisidx[type]);
6607 if (idx != hisidx[type] && --idx < 0)
6608 idx = hislen - 1;
6609 }
6610 for (i = 0; i < viminfo_hisidx[type]; i++)
6611 {
6612 vim_free(history[type][idx].hisstr);
6613 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6614 history[type][idx].viminfo = TRUE;
6615 history[type][idx].time_set = viminfo_history[type][i].time_set;
6616 if (--idx < 0)
6617 idx = hislen - 1;
6618 }
6619 idx += 1;
6620 idx %= hislen;
6621 for (i = 0; i < viminfo_hisidx[type]; i++)
6622 {
6623 history[type][idx++].hisnum = ++hisnum[type];
6624 idx %= hislen;
6625 }
6626}
6627
6628#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6629 static int
6630#ifdef __BORLANDC__
6631_RTLENTRYF
6632#endif
6633sort_hist(const void *s1, const void *s2)
6634{
6635 histentry_T *p1 = *(histentry_T **)s1;
6636 histentry_T *p2 = *(histentry_T **)s2;
6637
6638 if (p1->time_set < p2->time_set) return -1;
6639 if (p1->time_set > p2->time_set) return 1;
6640 return 0;
6641}
6642#endif
6643
6644/*
6645 * Merge history lines from viminfo and lines typed in this Vim based on the
6646 * timestamp;
6647 */
6648 static void
6649merge_history(int type)
6650{
6651 int max_len;
6652 histentry_T **tot_hist;
6653 histentry_T *new_hist;
6654 int i;
6655 int len;
6656
6657 /* Make one long list with all entries. */
6658 max_len = hislen + viminfo_hisidx[type];
6659 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6660 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6661 if (tot_hist == NULL || new_hist == NULL)
6662 {
6663 vim_free(tot_hist);
6664 vim_free(new_hist);
6665 return;
6666 }
6667 for (i = 0; i < viminfo_hisidx[type]; i++)
6668 tot_hist[i] = &viminfo_history[type][i];
6669 len = i;
6670 for (i = 0; i < hislen; i++)
6671 if (history[type][i].hisstr != NULL)
6672 tot_hist[len++] = &history[type][i];
6673
6674 /* Sort the list on timestamp. */
6675 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6676
6677 /* Keep the newest ones. */
6678 for (i = 0; i < hislen; i++)
6679 {
6680 if (i < len)
6681 {
6682 new_hist[i] = *tot_hist[i];
6683 tot_hist[i]->hisstr = NULL;
6684 if (new_hist[i].hisnum == 0)
6685 new_hist[i].hisnum = ++hisnum[type];
6686 }
6687 else
6688 clear_hist_entry(&new_hist[i]);
6689 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006690 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006691
6692 /* Free what is not kept. */
6693 for (i = 0; i < viminfo_hisidx[type]; i++)
6694 vim_free(viminfo_history[type][i].hisstr);
6695 for (i = 0; i < hislen; i++)
6696 vim_free(history[type][i].hisstr);
6697 vim_free(history[type]);
6698 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006699 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006700}
6701
6702/*
6703 * Finish reading history lines from viminfo. Not used when writing viminfo.
6704 */
6705 void
6706finish_viminfo_history(vir_T *virp)
6707{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006709 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710
6711 for (type = 0; type < HIST_COUNT; ++type)
6712 {
6713 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006714 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006715
6716 if (merge)
6717 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006719 concat_history(type);
6720
Bram Moolenaard23a8232018-02-10 18:45:26 +01006721 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006722 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 }
6724}
6725
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006726/*
6727 * Write history to viminfo file in "fp".
6728 * When "merge" is TRUE merge history lines with a previously read viminfo
6729 * file, data is in viminfo_history[].
6730 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6731 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006733write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006734{
6735 int i;
6736 int type;
6737 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006738 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739
6740 init_history();
6741 if (hislen == 0)
6742 return;
6743 for (type = 0; type < HIST_COUNT; ++type)
6744 {
6745 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6746 if (num_saved == 0)
6747 continue;
6748 if (num_saved < 0) /* Use default */
6749 num_saved = hislen;
6750 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6751 type == HIST_CMD ? _("Command Line") :
6752 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753 type == HIST_EXPR ? _("Expression") :
6754 type == HIST_INPUT ? _("Input Line") :
6755 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 if (num_saved > hislen)
6757 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006758
6759 /*
6760 * Merge typed and viminfo history:
6761 * round 1: history of typed commands.
6762 * round 2: history from recently read viminfo.
6763 */
6764 for (round = 1; round <= 2; ++round)
6765 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006766 if (round == 1)
6767 /* start at newest entry, somewhere in the list */
6768 i = hisidx[type];
6769 else if (viminfo_hisidx[type] > 0)
6770 /* start at newest entry, first in the list */
6771 i = 0;
6772 else
6773 /* empty list */
6774 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006775 if (i >= 0)
6776 while (num_saved > 0
6777 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779 char_u *p;
6780 time_t timestamp;
6781 int c = NUL;
6782
6783 if (round == 1)
6784 {
6785 p = history[type][i].hisstr;
6786 timestamp = history[type][i].time_set;
6787 }
6788 else
6789 {
6790 p = viminfo_history[type] == NULL ? NULL
6791 : viminfo_history[type][i].hisstr;
6792 timestamp = viminfo_history[type] == NULL ? 0
6793 : viminfo_history[type][i].time_set;
6794 }
6795
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006796 if (p != NULL && (round == 2
6797 || !merge
6798 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006800 --num_saved;
6801 fputc(hist_type2char(type, TRUE), fp);
6802 /* For the search history: put the separator in the
6803 * second column; use a space if there isn't one. */
6804 if (type == HIST_SEARCH)
6805 {
6806 c = p[STRLEN(p) + 1];
6807 putc(c == NUL ? ' ' : c, fp);
6808 }
6809 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006810
6811 {
6812 char cbuf[NUMBUFLEN];
6813
6814 /* New style history with a bar line. Format:
6815 * |{bartype},{histtype},{timestamp},{separator},"text" */
6816 if (c == NUL)
6817 cbuf[0] = NUL;
6818 else
6819 sprintf(cbuf, "%d", c);
6820 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6821 type, (long)timestamp, cbuf);
6822 barline_writestring(fp, p, LSIZE - 20);
6823 putc('\n', fp);
6824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006826 if (round == 1)
6827 {
6828 /* Decrement index, loop around and stop when back at
6829 * the start. */
6830 if (--i < 0)
6831 i = hislen - 1;
6832 if (i == hisidx[type])
6833 break;
6834 }
6835 else
6836 {
6837 /* Increment index. Stop at the end in the while. */
6838 ++i;
6839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006841 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006842 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006843 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006844 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006845 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006846 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847 }
6848}
6849#endif /* FEAT_VIMINFO */
6850
6851#if defined(FEAT_FKMAP) || defined(PROTO)
6852/*
6853 * Write a character at the current cursor+offset position.
6854 * It is directly written into the command buffer block.
6855 */
6856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006857cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858{
6859 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6860 {
6861 EMSG(_("E198: cmd_pchar beyond the command length"));
6862 return;
6863 }
6864 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6865 ccline.cmdbuff[ccline.cmdlen] = NUL;
6866}
6867
6868 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006869cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6872 {
6873 /* EMSG(_("cmd_gchar beyond the command length")); */
6874 return NUL;
6875 }
6876 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6877}
6878#endif
6879
6880#if defined(FEAT_CMDWIN) || defined(PROTO)
6881/*
6882 * Open a window on the current command line and history. Allow editing in
6883 * the window. Returns when the window is closed.
6884 * Returns:
6885 * CR if the command is to be executed
6886 * Ctrl_C if it is to be abandoned
6887 * K_IGNORE if editing continues
6888 */
6889 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006890open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891{
6892 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006893 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006895 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 win_T *wp;
6897 int i;
6898 linenr_T lnum;
6899 int histtype;
6900 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 int save_restart_edit = restart_edit;
6902 int save_State = State;
6903 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006904#ifdef FEAT_RIGHTLEFT
6905 int save_cmdmsg_rl = cmdmsg_rl;
6906#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006907#ifdef FEAT_FOLDING
6908 int save_KeyTyped;
6909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910
6911 /* Can't do this recursively. Can't do it when typing a password. */
6912 if (cmdwin_type != 0
6913# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6914 || cmdline_star > 0
6915# endif
6916 )
6917 {
6918 beep_flush();
6919 return K_IGNORE;
6920 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006921 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922
6923 /* Save current window sizes. */
6924 win_size_save(&winsizes);
6925
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006927 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006928
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006929 /* don't use a new tab page */
6930 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006931 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006932
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 /* Create a window for the command-line buffer. */
6934 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6935 {
6936 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006937 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 return K_IGNORE;
6939 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006940 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941
6942 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006943 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006944 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006947#ifdef FEAT_FOLDING
6948 curwin->w_p_fen = FALSE;
6949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006951 curwin->w_p_rl = cmdmsg_rl;
6952 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006954 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006957 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006958 /* But don't allow switching to another buffer. */
6959 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960
Bram Moolenaar46152342005-09-07 21:18:43 +00006961 /* Showing the prompt may have set need_wait_return, reset it. */
6962 need_wait_return = FALSE;
6963
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006964 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6966 {
6967 if (p_wc == TAB)
6968 {
6969 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6970 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6971 }
6972 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6973 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006974 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006976 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6977 * sets 'textwidth' to 78). */
6978 curbuf->b_p_tw = 0;
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 /* Fill the buffer with the history. */
6981 init_history();
6982 if (hislen > 0)
6983 {
6984 i = hisidx[histtype];
6985 if (i >= 0)
6986 {
6987 lnum = 0;
6988 do
6989 {
6990 if (++i == hislen)
6991 i = 0;
6992 if (history[histtype][i].hisstr != NULL)
6993 ml_append(lnum++, history[histtype][i].hisstr,
6994 (colnr_T)0, FALSE);
6995 }
6996 while (i != hisidx[histtype]);
6997 }
6998 }
6999
7000 /* Replace the empty last line with the current command-line and put the
7001 * cursor there. */
7002 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7003 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7004 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007005 changed_line_abv_curs();
7006 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007007 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008
7009 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007010 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011
7012 /* No Ex mode here! */
7013 exmode_active = 0;
7014
7015 State = NORMAL;
7016# ifdef FEAT_MOUSE
7017 setmouse();
7018# endif
7019
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007021 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007022 if (restart_edit != 0) /* autocmd with ":startinsert" */
7023 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024
7025 i = RedrawingDisabled;
7026 RedrawingDisabled = 0;
7027
7028 /*
7029 * Call the main loop until <CR> or CTRL-C is typed.
7030 */
7031 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007032 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033
7034 RedrawingDisabled = i;
7035
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007036# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007037 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007038# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007039
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007041 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007042
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007043# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007044 /* Restore KeyTyped in case it is modified by autocommands */
7045 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046# endif
7047
Bram Moolenaarccc18222007-05-10 18:25:20 +00007048 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007049 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050 cmdwin_type = 0;
7051
7052 exmode_active = save_exmode;
7053
Bram Moolenaarf9821062008-06-20 16:31:07 +00007054 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007056 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 {
7058 cmdwin_result = Ctrl_C;
7059 EMSG(_("E199: Active window or buffer deleted"));
7060 }
7061 else
7062 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007063# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 /* autocmds may abort script processing */
7065 if (aborting() && cmdwin_result != K_IGNORE)
7066 cmdwin_result = Ctrl_C;
7067# endif
7068 /* Set the new command line from the cmdline buffer. */
7069 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007070 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007072 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7073
7074 if (histtype == HIST_CMD)
7075 {
7076 /* Execute the command directly. */
7077 ccline.cmdbuff = vim_strsave((char_u *)p);
7078 cmdwin_result = CAR;
7079 }
7080 else
7081 {
7082 /* First need to cancel what we were doing. */
7083 ccline.cmdbuff = NULL;
7084 stuffcharReadbuff(':');
7085 stuffReadbuff((char_u *)p);
7086 stuffcharReadbuff(CAR);
7087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 }
7089 else if (cmdwin_result == K_XF2) /* :qa typed */
7090 {
7091 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7092 cmdwin_result = CAR;
7093 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007094 else if (cmdwin_result == Ctrl_C)
7095 {
7096 /* :q or :close, don't execute any command
7097 * and don't modify the cmd window. */
7098 ccline.cmdbuff = NULL;
7099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 else
7101 ccline.cmdbuff = vim_strsave(ml_get_curline());
7102 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007103 {
7104 ccline.cmdbuff = vim_strsave((char_u *)"");
7105 ccline.cmdlen = 0;
7106 ccline.cmdbufflen = 1;
7107 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 else
7111 {
7112 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7113 ccline.cmdbufflen = ccline.cmdlen + 1;
7114 ccline.cmdpos = curwin->w_cursor.col;
7115 if (ccline.cmdpos > ccline.cmdlen)
7116 ccline.cmdpos = ccline.cmdlen;
7117 if (cmdwin_result == K_IGNORE)
7118 {
7119 set_cmdspos_cursor();
7120 redrawcmd();
7121 }
7122 }
7123
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007125 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007126# ifdef FEAT_CONCEAL
7127 /* Avoid command-line window first character being concealed. */
7128 curwin->w_p_cole = 0;
7129# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007131 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 win_goto(old_curwin);
7133 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007134
7135 /* win_close() may have already wiped the buffer when 'bh' is
7136 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007137 if (bufref_valid(&bufref))
7138 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
7140 /* Restore window sizes. */
7141 win_size_restore(&winsizes);
7142
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007143 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 }
7145
7146 ga_clear(&winsizes);
7147 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007148# ifdef FEAT_RIGHTLEFT
7149 cmdmsg_rl = save_cmdmsg_rl;
7150# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 State = save_State;
7153# ifdef FEAT_MOUSE
7154 setmouse();
7155# endif
7156
7157 return cmdwin_result;
7158}
7159#endif /* FEAT_CMDWIN */
7160
7161/*
7162 * Used for commands that either take a simple command string argument, or:
7163 * cmd << endmarker
7164 * {script}
7165 * endmarker
7166 * Returns a pointer to allocated memory with {script} or NULL.
7167 */
7168 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 char_u *theline;
7172 char *end_pattern = NULL;
7173 char dot[] = ".";
7174 garray_T ga;
7175
7176 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7177 return NULL;
7178
7179 ga_init2(&ga, 1, 0x400);
7180
7181 if (cmd[2] != NUL)
7182 end_pattern = (char *)skipwhite(cmd + 2);
7183 else
7184 end_pattern = dot;
7185
7186 for (;;)
7187 {
7188 theline = eap->getline(
7189#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007190 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191#endif
7192 NUL, eap->cookie, 0);
7193
7194 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007195 {
7196 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
7200 ga_concat(&ga, theline);
7201 ga_append(&ga, '\n');
7202 vim_free(theline);
7203 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007204 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
7206 return (char_u *)ga.ga_data;
7207}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007208
7209#ifdef FEAT_SEARCH_EXTRA
7210 static void
7211set_search_match(pos_T *t)
7212{
7213 /*
7214 * First move cursor to end of match, then to the start. This
7215 * moves the whole match onto the screen when 'nowrap' is set.
7216 */
7217 t->lnum += search_match_lines;
7218 t->col = search_match_endcol;
7219 if (t->lnum > curbuf->b_ml.ml_line_count)
7220 {
7221 t->lnum = curbuf->b_ml.ml_line_count;
7222 coladvance((colnr_T)MAXCOL);
7223 }
7224}
7225#endif