blob: 64914aa672e6f69c53c69e1d38e02931c67b1369 [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
3302 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3303 return FAIL;
3304
3305 /* A register containing CTRL-R can cause an endless loop. Allow using
3306 * CTRL-C to break the loop. */
3307 line_breakcheck();
3308 if (got_int)
3309 return FAIL;
3310
3311#ifdef FEAT_CLIPBOARD
3312 regname = may_get_selection(regname);
3313#endif
3314
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003315 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003316 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003317 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003318 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003319 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003320 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003321 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003322
3323 if (i)
3324 {
3325 /* Got the value of a special register in "arg". */
3326 if (arg == NULL)
3327 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003328
3329 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3330 * part of the word. */
3331 p = arg;
3332 if (p_is && regname == Ctrl_W)
3333 {
3334 char_u *w;
3335 int len;
3336
3337 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003338 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003339 {
3340#ifdef FEAT_MBYTE
3341 if (has_mbyte)
3342 {
3343 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3344 if (!vim_iswordc(mb_ptr2char(w - len)))
3345 break;
3346 w -= len;
3347 }
3348 else
3349#endif
3350 {
3351 if (!vim_iswordc(w[-1]))
3352 break;
3353 --w;
3354 }
3355 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003356 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003357 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3358 p += len;
3359 }
3360
3361 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003362 if (allocated)
3363 vim_free(arg);
3364 return OK;
3365 }
3366
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003367 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003368}
3369
3370/*
3371 * Put a string on the command line.
3372 * When "literally" is TRUE, insert literally.
3373 * When "literally" is FALSE, insert as typed, but don't leave the command
3374 * line.
3375 */
3376 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003377cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003378{
3379 int c, cv;
3380
3381 if (literally)
3382 put_on_cmdline(s, -1, TRUE);
3383 else
3384 while (*s != NUL)
3385 {
3386 cv = *s;
3387 if (cv == Ctrl_V && s[1])
3388 ++s;
3389#ifdef FEAT_MBYTE
3390 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003391 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003392 else
3393#endif
3394 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003395 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3396 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003397#ifdef UNIX
3398 || c == intr_char
3399#endif
3400 || (c == Ctrl_BSL && *s == Ctrl_N))
3401 stuffcharReadbuff(Ctrl_V);
3402 stuffcharReadbuff(c);
3403 }
3404}
3405
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#ifdef FEAT_WILDMENU
3407/*
3408 * Delete characters on the command line, from "from" to the current
3409 * position.
3410 */
3411 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003412cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
3414 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3415 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3416 ccline.cmdlen -= ccline.cmdpos - from;
3417 ccline.cmdpos = from;
3418}
3419#endif
3420
3421/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003422 * This function is called when the screen size changes and with incremental
3423 * search and in other situations where the command line may have been
3424 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 */
3426 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003427redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003429 redrawcmdline_ex(TRUE);
3430}
3431
3432 void
3433redrawcmdline_ex(int do_compute_cmdrow)
3434{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 if (cmd_silent)
3436 return;
3437 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003438 if (do_compute_cmdrow)
3439 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 redrawcmd();
3441 cursorcmd();
3442}
3443
3444 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003445redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446{
3447 int i;
3448
3449 if (cmd_silent)
3450 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003451 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 msg_putchar(ccline.cmdfirstc);
3453 if (ccline.cmdprompt != NULL)
3454 {
3455 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3456 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3457 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003458 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 --ccline.cmdindent;
3460 }
3461 else
3462 for (i = ccline.cmdindent; i > 0; --i)
3463 msg_putchar(' ');
3464}
3465
3466/*
3467 * Redraw what is currently on the command line.
3468 */
3469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003470redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471{
3472 if (cmd_silent)
3473 return;
3474
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003475 /* when 'incsearch' is set there may be no command line while redrawing */
3476 if (ccline.cmdbuff == NULL)
3477 {
3478 windgoto(cmdline_row, 0);
3479 msg_clr_eos();
3480 return;
3481 }
3482
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 msg_start();
3484 redrawcmdprompt();
3485
3486 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3487 msg_no_more = TRUE;
3488 draw_cmdline(0, ccline.cmdlen);
3489 msg_clr_eos();
3490 msg_no_more = FALSE;
3491
3492 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003493 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003494 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 /*
3497 * An emsg() before may have set msg_scroll. This is used in normal mode,
3498 * in cmdline mode we can reset them now.
3499 */
3500 msg_scroll = FALSE; /* next message overwrites cmdline */
3501
3502 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3503 * in cmdline mode */
3504 skip_redraw = FALSE;
3505}
3506
3507 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003508compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003510 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 cmdline_row = Rows - 1;
3512 else
3513 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003514 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515}
3516
3517 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003518cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519{
3520 if (cmd_silent)
3521 return;
3522
3523#ifdef FEAT_RIGHTLEFT
3524 if (cmdmsg_rl)
3525 {
3526 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3527 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3528 if (msg_row <= 0)
3529 msg_row = Rows - 1;
3530 }
3531 else
3532#endif
3533 {
3534 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3535 msg_col = ccline.cmdspos % (int)Columns;
3536 if (msg_row >= Rows)
3537 msg_row = Rows - 1;
3538 }
3539
3540 windgoto(msg_row, msg_col);
3541#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003542 if (p_imst == IM_ON_THE_SPOT)
3543 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#endif
3545#ifdef MCH_CURSOR_SHAPE
3546 mch_update_cursor();
3547#endif
3548}
3549
3550 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003551gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552{
3553 msg_start();
3554#ifdef FEAT_RIGHTLEFT
3555 if (cmdmsg_rl)
3556 msg_col = Columns - 1;
3557 else
3558#endif
3559 msg_col = 0; /* always start in column 0 */
3560 if (clr) /* clear the bottom line(s) */
3561 msg_clr_eos(); /* will reset clear_cmdline */
3562 windgoto(cmdline_row, 0);
3563}
3564
3565/*
3566 * Check the word in front of the cursor for an abbreviation.
3567 * Called when the non-id character "c" has been entered.
3568 * When an abbreviation is recognized it is removed from the text with
3569 * backspaces and the replacement string is inserted, followed by "c".
3570 */
3571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003572ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573{
3574 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3575 return FALSE;
3576
3577 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3578}
3579
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003580#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3581 static int
3582#ifdef __BORLANDC__
3583_RTLENTRYF
3584#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003585sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003586{
3587 char_u *p1 = *(char_u **)s1;
3588 char_u *p2 = *(char_u **)s2;
3589
3590 if (*p1 != '<' && *p2 == '<') return -1;
3591 if (*p1 == '<' && *p2 != '<') return 1;
3592 return STRCMP(p1, p2);
3593}
3594#endif
3595
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596/*
3597 * Return FAIL if this is not an appropriate context in which to do
3598 * completion of anything, return OK if it is (even if there are no matches).
3599 * For the caller, this means that the character is just passed through like a
3600 * normal character (instead of being expanded). This allows :s/^I^D etc.
3601 */
3602 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003603nextwild(
3604 expand_T *xp,
3605 int type,
3606 int options, /* extra options for ExpandOne() */
3607 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608{
3609 int i, j;
3610 char_u *p1;
3611 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 int difflen;
3613 int v;
3614
3615 if (xp->xp_numfiles == -1)
3616 {
3617 set_expand_context(xp);
3618 cmd_showtail = expand_showtail(xp);
3619 }
3620
3621 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3622 {
3623 beep_flush();
3624 return OK; /* Something illegal on command line */
3625 }
3626 if (xp->xp_context == EXPAND_NOTHING)
3627 {
3628 /* Caller can use the character as a normal char instead */
3629 return FAIL;
3630 }
3631
3632 MSG_PUTS("..."); /* show that we are busy */
3633 out_flush();
3634
3635 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003636 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 if (type == WILD_NEXT || type == WILD_PREV)
3639 {
3640 /*
3641 * Get next/previous match for a previous expanded pattern.
3642 */
3643 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3644 }
3645 else
3646 {
3647 /*
3648 * Translate string into pattern and expand it.
3649 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003650 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3651 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 p2 = NULL;
3653 else
3654 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003655 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003656 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3657 if (escape)
3658 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003659
3660 if (p_wic)
3661 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003662 p2 = ExpandOne(xp, p1,
3663 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003664 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003666 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (p2 != NULL && type == WILD_LONGEST)
3668 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003669 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (ccline.cmdbuff[i + j] == '*'
3671 || ccline.cmdbuff[i + j] == '?')
3672 break;
3673 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003674 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676 }
3677 }
3678
3679 if (p2 != NULL && !got_int)
3680 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003681 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003682 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003684 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 xp->xp_pattern = ccline.cmdbuff + i;
3686 }
3687 else
3688 v = OK;
3689 if (v == OK)
3690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003691 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3692 &ccline.cmdbuff[ccline.cmdpos],
3693 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3694 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 ccline.cmdlen += difflen;
3696 ccline.cmdpos += difflen;
3697 }
3698 }
3699 vim_free(p2);
3700
3701 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003702 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 /* When expanding a ":map" command and no matches are found, assume that
3705 * the key is supposed to be inserted literally */
3706 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3707 return FAIL;
3708
3709 if (xp->xp_numfiles <= 0 && p2 == NULL)
3710 beep_flush();
3711 else if (xp->xp_numfiles == 1)
3712 /* free expanded pattern */
3713 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3714
3715 return OK;
3716}
3717
3718/*
3719 * Do wildcard expansion on the string 'str'.
3720 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003721 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 * Return NULL for failure.
3723 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003724 * "orig" is the originally expanded string, copied to allocated memory. It
3725 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3726 * WILD_PREV "orig" should be NULL.
3727 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003728 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3729 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 *
3731 * mode = WILD_FREE: just free previously expanded matches
3732 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3733 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3734 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3735 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3736 * mode = WILD_ALL: return all matches concatenated
3737 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003738 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 *
3740 * options = WILD_LIST_NOTFOUND: list entries without a match
3741 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3742 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3743 * options = WILD_NO_BEEP: Don't beep for multiple matches
3744 * options = WILD_ADD_SLASH: add a slash after directory names
3745 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3746 * options = WILD_SILENT: don't print warning messages
3747 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003748 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 *
3750 * The variables xp->xp_context and xp->xp_backslash must have been set!
3751 */
3752 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003753ExpandOne(
3754 expand_T *xp,
3755 char_u *str,
3756 char_u *orig, /* allocated copy of original of expanded string */
3757 int options,
3758 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759{
3760 char_u *ss = NULL;
3761 static int findex;
3762 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003763 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 int i;
3765 long_u len;
3766 int non_suf_match; /* number without matching suffix */
3767
3768 /*
3769 * first handle the case of using an old match
3770 */
3771 if (mode == WILD_NEXT || mode == WILD_PREV)
3772 {
3773 if (xp->xp_numfiles > 0)
3774 {
3775 if (mode == WILD_PREV)
3776 {
3777 if (findex == -1)
3778 findex = xp->xp_numfiles;
3779 --findex;
3780 }
3781 else /* mode == WILD_NEXT */
3782 ++findex;
3783
3784 /*
3785 * When wrapping around, return the original string, set findex to
3786 * -1.
3787 */
3788 if (findex < 0)
3789 {
3790 if (orig_save == NULL)
3791 findex = xp->xp_numfiles - 1;
3792 else
3793 findex = -1;
3794 }
3795 if (findex >= xp->xp_numfiles)
3796 {
3797 if (orig_save == NULL)
3798 findex = 0;
3799 else
3800 findex = -1;
3801 }
3802#ifdef FEAT_WILDMENU
3803 if (p_wmnu)
3804 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3805 findex, cmd_showtail);
3806#endif
3807 if (findex == -1)
3808 return vim_strsave(orig_save);
3809 return vim_strsave(xp->xp_files[findex]);
3810 }
3811 else
3812 return NULL;
3813 }
3814
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003815 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3817 {
3818 FreeWild(xp->xp_numfiles, xp->xp_files);
3819 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003820 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 }
3822 findex = 0;
3823
3824 if (mode == WILD_FREE) /* only release file name */
3825 return NULL;
3826
3827 if (xp->xp_numfiles == -1)
3828 {
3829 vim_free(orig_save);
3830 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003831 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833 /*
3834 * Do the expansion.
3835 */
3836 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3837 options) == FAIL)
3838 {
3839#ifdef FNAME_ILLEGAL
3840 /* Illegal file name has been silently skipped. But when there
3841 * are wildcards, the real problem is that there was no match,
3842 * causing the pattern to be added, which has illegal characters.
3843 */
3844 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3845 EMSG2(_(e_nomatch2), str);
3846#endif
3847 }
3848 else if (xp->xp_numfiles == 0)
3849 {
3850 if (!(options & WILD_SILENT))
3851 EMSG2(_(e_nomatch2), str);
3852 }
3853 else
3854 {
3855 /* Escape the matches for use on the command line. */
3856 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3857
3858 /*
3859 * Check for matching suffixes in file names.
3860 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003861 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3862 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 {
3864 if (xp->xp_numfiles)
3865 non_suf_match = xp->xp_numfiles;
3866 else
3867 non_suf_match = 1;
3868 if ((xp->xp_context == EXPAND_FILES
3869 || xp->xp_context == EXPAND_DIRECTORIES)
3870 && xp->xp_numfiles > 1)
3871 {
3872 /*
3873 * More than one match; check suffix.
3874 * The files will have been sorted on matching suffix in
3875 * expand_wildcards, only need to check the first two.
3876 */
3877 non_suf_match = 0;
3878 for (i = 0; i < 2; ++i)
3879 if (match_suffix(xp->xp_files[i]))
3880 ++non_suf_match;
3881 }
3882 if (non_suf_match != 1)
3883 {
3884 /* Can we ever get here unless it's while expanding
3885 * interactively? If not, we can get rid of this all
3886 * together. Don't really want to wait for this message
3887 * (and possibly have to hit return to continue!).
3888 */
3889 if (!(options & WILD_SILENT))
3890 EMSG(_(e_toomany));
3891 else if (!(options & WILD_NO_BEEP))
3892 beep_flush();
3893 }
3894 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3895 ss = vim_strsave(xp->xp_files[0]);
3896 }
3897 }
3898 }
3899
3900 /* Find longest common part */
3901 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3902 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003903 int mb_len = 1;
3904 int c0, ci;
3905
3906 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003908#ifdef FEAT_MBYTE
3909 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003911 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3912 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3913 }
3914 else
3915#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003916 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003917 for (i = 1; i < xp->xp_numfiles; ++i)
3918 {
3919#ifdef FEAT_MBYTE
3920 if (has_mbyte)
3921 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3922 else
3923#endif
3924 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003925 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003927 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003928 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003930 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003933 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 break;
3935 }
3936 if (i < xp->xp_numfiles)
3937 {
3938 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003939 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 break;
3941 }
3942 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003943
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 ss = alloc((unsigned)len + 1);
3945 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003946 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 findex = -1; /* next p_wc gets first one */
3948 }
3949
3950 /* Concatenate all matching names */
3951 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3952 {
3953 len = 0;
3954 for (i = 0; i < xp->xp_numfiles; ++i)
3955 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3956 ss = lalloc(len, TRUE);
3957 if (ss != NULL)
3958 {
3959 *ss = NUL;
3960 for (i = 0; i < xp->xp_numfiles; ++i)
3961 {
3962 STRCAT(ss, xp->xp_files[i]);
3963 if (i != xp->xp_numfiles - 1)
3964 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3965 }
3966 }
3967 }
3968
3969 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3970 ExpandCleanup(xp);
3971
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003972 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003973 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003974 vim_free(orig);
3975
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 return ss;
3977}
3978
3979/*
3980 * Prepare an expand structure for use.
3981 */
3982 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003983ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003985 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003986 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003988#ifndef BACKSLASH_IN_FILENAME
3989 xp->xp_shell = FALSE;
3990#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 xp->xp_numfiles = -1;
3992 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003993#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3994 xp->xp_arg = NULL;
3995#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003996 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997}
3998
3999/*
4000 * Cleanup an expand structure after use.
4001 */
4002 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004003ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004{
4005 if (xp->xp_numfiles >= 0)
4006 {
4007 FreeWild(xp->xp_numfiles, xp->xp_files);
4008 xp->xp_numfiles = -1;
4009 }
4010}
4011
4012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004013ExpandEscape(
4014 expand_T *xp,
4015 char_u *str,
4016 int numfiles,
4017 char_u **files,
4018 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019{
4020 int i;
4021 char_u *p;
4022
4023 /*
4024 * May change home directory back to "~"
4025 */
4026 if (options & WILD_HOME_REPLACE)
4027 tilde_replace(str, numfiles, files);
4028
4029 if (options & WILD_ESCAPE)
4030 {
4031 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004032 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004033 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 || xp->xp_context == EXPAND_BUFFERS
4035 || xp->xp_context == EXPAND_DIRECTORIES)
4036 {
4037 /*
4038 * Insert a backslash into a file name before a space, \, %, #
4039 * and wildmatch characters, except '~'.
4040 */
4041 for (i = 0; i < numfiles; ++i)
4042 {
4043 /* for ":set path=" we need to escape spaces twice */
4044 if (xp->xp_backslash == XP_BS_THREE)
4045 {
4046 p = vim_strsave_escaped(files[i], (char_u *)" ");
4047 if (p != NULL)
4048 {
4049 vim_free(files[i]);
4050 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004051#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 p = vim_strsave_escaped(files[i], (char_u *)" ");
4053 if (p != NULL)
4054 {
4055 vim_free(files[i]);
4056 files[i] = p;
4057 }
4058#endif
4059 }
4060 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004061#ifdef BACKSLASH_IN_FILENAME
4062 p = vim_strsave_fnameescape(files[i], FALSE);
4063#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004064 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004065#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 if (p != NULL)
4067 {
4068 vim_free(files[i]);
4069 files[i] = p;
4070 }
4071
4072 /* If 'str' starts with "\~", replace "~" at start of
4073 * files[i] with "\~". */
4074 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004075 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004078
4079 /* If the first file starts with a '+' escape it. Otherwise it
4080 * could be seen as "+cmd". */
4081 if (*files[0] == '+')
4082 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 else if (xp->xp_context == EXPAND_TAGS)
4085 {
4086 /*
4087 * Insert a backslash before characters in a tag name that
4088 * would terminate the ":tag" command.
4089 */
4090 for (i = 0; i < numfiles; ++i)
4091 {
4092 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4093 if (p != NULL)
4094 {
4095 vim_free(files[i]);
4096 files[i] = p;
4097 }
4098 }
4099 }
4100 }
4101}
4102
4103/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004104 * Escape special characters in "fname" for when used as a file name argument
4105 * after a Vim command, or, when "shell" is non-zero, a shell command.
4106 * Returns the result in allocated memory.
4107 */
4108 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004109vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004110{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004111 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004112#ifdef BACKSLASH_IN_FILENAME
4113 char_u buf[20];
4114 int j = 0;
4115
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004116 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004117 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004118 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004119 buf[j++] = *p;
4120 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004121 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004122#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004123 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4124 if (shell && csh_like_shell() && p != NULL)
4125 {
4126 char_u *s;
4127
4128 /* For csh and similar shells need to put two backslashes before '!'.
4129 * One is taken by Vim, one by the shell. */
4130 s = vim_strsave_escaped(p, (char_u *)"!");
4131 vim_free(p);
4132 p = s;
4133 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004134#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004135
4136 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4137 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004138 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004139 escape_fname(&p);
4140
4141 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004142}
4143
4144/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004145 * Put a backslash before the file name in "pp", which is in allocated memory.
4146 */
4147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004148escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004149{
4150 char_u *p;
4151
4152 p = alloc((unsigned)(STRLEN(*pp) + 2));
4153 if (p != NULL)
4154 {
4155 p[0] = '\\';
4156 STRCPY(p + 1, *pp);
4157 vim_free(*pp);
4158 *pp = p;
4159 }
4160}
4161
4162/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 * For each file name in files[num_files]:
4164 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4165 */
4166 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004167tilde_replace(
4168 char_u *orig_pat,
4169 int num_files,
4170 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171{
4172 int i;
4173 char_u *p;
4174
4175 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4176 {
4177 for (i = 0; i < num_files; ++i)
4178 {
4179 p = home_replace_save(NULL, files[i]);
4180 if (p != NULL)
4181 {
4182 vim_free(files[i]);
4183 files[i] = p;
4184 }
4185 }
4186 }
4187}
4188
4189/*
4190 * Show all matches for completion on the command line.
4191 * Returns EXPAND_NOTHING when the character that triggered expansion should
4192 * be inserted like a normal character.
4193 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004195showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196{
4197#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4198 int num_files;
4199 char_u **files_found;
4200 int i, j, k;
4201 int maxlen;
4202 int lines;
4203 int columns;
4204 char_u *p;
4205 int lastlen;
4206 int attr;
4207 int showtail;
4208
4209 if (xp->xp_numfiles == -1)
4210 {
4211 set_expand_context(xp);
4212 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4213 &num_files, &files_found);
4214 showtail = expand_showtail(xp);
4215 if (i != EXPAND_OK)
4216 return i;
4217
4218 }
4219 else
4220 {
4221 num_files = xp->xp_numfiles;
4222 files_found = xp->xp_files;
4223 showtail = cmd_showtail;
4224 }
4225
4226#ifdef FEAT_WILDMENU
4227 if (!wildmenu)
4228 {
4229#endif
4230 msg_didany = FALSE; /* lines_left will be set */
4231 msg_start(); /* prepare for paging */
4232 msg_putchar('\n');
4233 out_flush();
4234 cmdline_row = msg_row;
4235 msg_didany = FALSE; /* lines_left will be set again */
4236 msg_start(); /* prepare for paging */
4237#ifdef FEAT_WILDMENU
4238 }
4239#endif
4240
4241 if (got_int)
4242 got_int = FALSE; /* only int. the completion, not the cmd line */
4243#ifdef FEAT_WILDMENU
4244 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004245 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246#endif
4247 else
4248 {
4249 /* find the length of the longest file name */
4250 maxlen = 0;
4251 for (i = 0; i < num_files; ++i)
4252 {
4253 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004254 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 || xp->xp_context == EXPAND_BUFFERS))
4256 {
4257 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4258 j = vim_strsize(NameBuff);
4259 }
4260 else
4261 j = vim_strsize(L_SHOWFILE(i));
4262 if (j > maxlen)
4263 maxlen = j;
4264 }
4265
4266 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4267 lines = num_files;
4268 else
4269 {
4270 /* compute the number of columns and lines for the listing */
4271 maxlen += 2; /* two spaces between file names */
4272 columns = ((int)Columns + 2) / maxlen;
4273 if (columns < 1)
4274 columns = 1;
4275 lines = (num_files + columns - 1) / columns;
4276 }
4277
Bram Moolenaar8820b482017-03-16 17:23:31 +01004278 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279
4280 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4281 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004282 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 msg_clr_eos();
4284 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004285 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 }
4287
4288 /* list the files line by line */
4289 for (i = 0; i < lines; ++i)
4290 {
4291 lastlen = 999;
4292 for (k = i; k < num_files; k += lines)
4293 {
4294 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4295 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004296 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 p = files_found[k] + STRLEN(files_found[k]) + 1;
4298 msg_advance(maxlen + 1);
4299 msg_puts(p);
4300 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004301 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 break;
4303 }
4304 for (j = maxlen - lastlen; --j >= 0; )
4305 msg_putchar(' ');
4306 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004307 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 || xp->xp_context == EXPAND_BUFFERS)
4309 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004310 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004311 if (xp->xp_numfiles != -1)
4312 {
4313 char_u *halved_slash;
4314 char_u *exp_path;
4315
4316 /* Expansion was done before and special characters
4317 * were escaped, need to halve backslashes. Also
4318 * $HOME has been replaced with ~/. */
4319 exp_path = expand_env_save_opt(files_found[k], TRUE);
4320 halved_slash = backslash_halve_save(
4321 exp_path != NULL ? exp_path : files_found[k]);
4322 j = mch_isdir(halved_slash != NULL ? halved_slash
4323 : files_found[k]);
4324 vim_free(exp_path);
4325 vim_free(halved_slash);
4326 }
4327 else
4328 /* Expansion was done here, file names are literal. */
4329 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 if (showtail)
4331 p = L_SHOWFILE(k);
4332 else
4333 {
4334 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4335 TRUE);
4336 p = NameBuff;
4337 }
4338 }
4339 else
4340 {
4341 j = FALSE;
4342 p = L_SHOWFILE(k);
4343 }
4344 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4345 }
4346 if (msg_col > 0) /* when not wrapped around */
4347 {
4348 msg_clr_eos();
4349 msg_putchar('\n');
4350 }
4351 out_flush(); /* show one line at a time */
4352 if (got_int)
4353 {
4354 got_int = FALSE;
4355 break;
4356 }
4357 }
4358
4359 /*
4360 * we redraw the command below the lines that we have just listed
4361 * This is a bit tricky, but it saves a lot of screen updating.
4362 */
4363 cmdline_row = msg_row; /* will put it back later */
4364 }
4365
4366 if (xp->xp_numfiles == -1)
4367 FreeWild(num_files, files_found);
4368
4369 return EXPAND_OK;
4370}
4371
4372/*
4373 * Private gettail for showmatches() (and win_redr_status_matches()):
4374 * Find tail of file name path, but ignore trailing "/".
4375 */
4376 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004377sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378{
4379 char_u *p;
4380 char_u *t = s;
4381 int had_sep = FALSE;
4382
4383 for (p = s; *p != NUL; )
4384 {
4385 if (vim_ispathsep(*p)
4386#ifdef BACKSLASH_IN_FILENAME
4387 && !rem_backslash(p)
4388#endif
4389 )
4390 had_sep = TRUE;
4391 else if (had_sep)
4392 {
4393 t = p;
4394 had_sep = FALSE;
4395 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004396 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 return t;
4399}
4400
4401/*
4402 * Return TRUE if we only need to show the tail of completion matches.
4403 * When not completing file names or there is a wildcard in the path FALSE is
4404 * returned.
4405 */
4406 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004407expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408{
4409 char_u *s;
4410 char_u *end;
4411
4412 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004413 if (xp->xp_context != EXPAND_FILES
4414 && xp->xp_context != EXPAND_SHELLCMD
4415 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 return FALSE;
4417
4418 end = gettail(xp->xp_pattern);
4419 if (end == xp->xp_pattern) /* there is no path separator */
4420 return FALSE;
4421
4422 for (s = xp->xp_pattern; s < end; s++)
4423 {
4424 /* Skip escaped wildcards. Only when the backslash is not a path
4425 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4426 if (rem_backslash(s))
4427 ++s;
4428 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4429 return FALSE;
4430 }
4431 return TRUE;
4432}
4433
4434/*
4435 * Prepare a string for expansion.
4436 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004437 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 * When expanding other names: The string will be used with regcomp(). Copy
4439 * the name into allocated memory and prepend "^".
4440 */
4441 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004442addstar(
4443 char_u *fname,
4444 int len,
4445 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446{
4447 char_u *retval;
4448 int i, j;
4449 int new_len;
4450 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004451 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004453 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004454 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004455 && context != EXPAND_SHELLCMD
4456 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
4458 /*
4459 * Matching will be done internally (on something other than files).
4460 * So we convert the file-matching-type wildcards into our kind for
4461 * use with vim_regcomp(). First work out how long it will be:
4462 */
4463
4464 /* For help tags the translation is done in find_help_tags().
4465 * For a tag pattern starting with "/" no translation is needed. */
4466 if (context == EXPAND_HELP
4467 || context == EXPAND_COLORS
4468 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004469 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004470 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004471 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004472 || ((context == EXPAND_TAGS_LISTFILES
4473 || context == EXPAND_TAGS)
4474 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 retval = vim_strnsave(fname, len);
4476 else
4477 {
4478 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4479 for (i = 0; i < len; i++)
4480 {
4481 if (fname[i] == '*' || fname[i] == '~')
4482 new_len++; /* '*' needs to be replaced by ".*"
4483 '~' needs to be replaced by "\~" */
4484
4485 /* Buffer names are like file names. "." should be literal */
4486 if (context == EXPAND_BUFFERS && fname[i] == '.')
4487 new_len++; /* "." becomes "\." */
4488
4489 /* Custom expansion takes care of special things, match
4490 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004491 if ((context == EXPAND_USER_DEFINED
4492 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 new_len++; /* '\' becomes "\\" */
4494 }
4495 retval = alloc(new_len);
4496 if (retval != NULL)
4497 {
4498 retval[0] = '^';
4499 j = 1;
4500 for (i = 0; i < len; i++, j++)
4501 {
4502 /* Skip backslash. But why? At least keep it for custom
4503 * expansion. */
4504 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004505 && context != EXPAND_USER_LIST
4506 && fname[i] == '\\'
4507 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 break;
4509
4510 switch (fname[i])
4511 {
4512 case '*': retval[j++] = '.';
4513 break;
4514 case '~': retval[j++] = '\\';
4515 break;
4516 case '?': retval[j] = '.';
4517 continue;
4518 case '.': if (context == EXPAND_BUFFERS)
4519 retval[j++] = '\\';
4520 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004521 case '\\': if (context == EXPAND_USER_DEFINED
4522 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 retval[j++] = '\\';
4524 break;
4525 }
4526 retval[j] = fname[i];
4527 }
4528 retval[j] = NUL;
4529 }
4530 }
4531 }
4532 else
4533 {
4534 retval = alloc(len + 4);
4535 if (retval != NULL)
4536 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004537 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538
4539 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004540 * Don't add a star to *, ~, ~user, $var or `cmd`.
4541 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 * ~ would be at the start of the file name, but not the tail.
4543 * $ could be anywhere in the tail.
4544 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004545 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 */
4547 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004548 ends_in_star = (len > 0 && retval[len - 1] == '*');
4549#ifndef BACKSLASH_IN_FILENAME
4550 for (i = len - 2; i >= 0; --i)
4551 {
4552 if (retval[i] != '\\')
4553 break;
4554 ends_in_star = !ends_in_star;
4555 }
4556#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004558 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 && vim_strchr(tail, '$') == NULL
4560 && vim_strchr(retval, '`') == NULL)
4561 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004562 else if (len > 0 && retval[len - 1] == '$')
4563 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 retval[len] = NUL;
4565 }
4566 }
4567 return retval;
4568}
4569
4570/*
4571 * Must parse the command line so far to work out what context we are in.
4572 * Completion can then be done based on that context.
4573 * This routine sets the variables:
4574 * xp->xp_pattern The start of the pattern to be expanded within
4575 * the command line (ends at the cursor).
4576 * xp->xp_context The type of thing to expand. Will be one of:
4577 *
4578 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4579 * the command line, like an unknown command. Caller
4580 * should beep.
4581 * EXPAND_NOTHING Unrecognised context for completion, use char like
4582 * a normal char, rather than for completion. eg
4583 * :s/^I/
4584 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4585 * it.
4586 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4587 * EXPAND_FILES After command with XFILE set, or after setting
4588 * with P_EXPAND set. eg :e ^I, :w>>^I
4589 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4590 * when we know only directories are of interest. eg
4591 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004592 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4594 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4595 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4596 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4597 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4598 * EXPAND_EVENTS Complete event names
4599 * EXPAND_SYNTAX Complete :syntax command arguments
4600 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4601 * EXPAND_AUGROUP Complete autocommand group names
4602 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4603 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4604 * eg :unmap a^I , :cunab x^I
4605 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4606 * eg :call sub^I
4607 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4608 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4609 * names in expressions, eg :while s^I
4610 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004611 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 */
4613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004614set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004616 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (ccline.cmdfirstc != ':'
4618#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004619 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004620 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#endif
4622 )
4623 {
4624 xp->xp_context = EXPAND_NOTHING;
4625 return;
4626 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004627 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628}
4629
4630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631set_cmd_context(
4632 expand_T *xp,
4633 char_u *str, /* start of command line */
4634 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004635 int col, /* position of cursor */
4636 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
4638 int old_char = NUL;
4639 char_u *nextcomm;
4640
4641 /*
4642 * Avoid a UMR warning from Purify, only save the character if it has been
4643 * written before.
4644 */
4645 if (col < len)
4646 old_char = str[col];
4647 str[col] = NUL;
4648 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004649
4650#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004651 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004652 {
4653# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004654 /* pass CMD_SIZE because there is no real command */
4655 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004656# endif
4657 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004658 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004659 {
4660 xp->xp_context = ccline.xp_context;
4661 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004662# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004663 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004664# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004665 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004666 else
4667#endif
4668 while (nextcomm != NULL)
4669 nextcomm = set_one_cmd_context(xp, nextcomm);
4670
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004671 /* Store the string here so that call_user_expand_func() can get to them
4672 * easily. */
4673 xp->xp_line = str;
4674 xp->xp_col = col;
4675
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 str[col] = old_char;
4677}
4678
4679/*
4680 * Expand the command line "str" from context "xp".
4681 * "xp" must have been set by set_cmd_context().
4682 * xp->xp_pattern points into "str", to where the text that is to be expanded
4683 * starts.
4684 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4685 * cursor.
4686 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4687 * key that triggered expansion literally.
4688 * Returns EXPAND_OK otherwise.
4689 */
4690 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004691expand_cmdline(
4692 expand_T *xp,
4693 char_u *str, /* start of command line */
4694 int col, /* position of cursor */
4695 int *matchcount, /* return: nr of matches */
4696 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
4698 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004699 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4702 {
4703 beep_flush();
4704 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4705 }
4706 if (xp->xp_context == EXPAND_NOTHING)
4707 {
4708 /* Caller can use the character as a normal char instead */
4709 return EXPAND_NOTHING;
4710 }
4711
4712 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004713 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4714 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (file_str == NULL)
4716 return EXPAND_UNSUCCESSFUL;
4717
Bram Moolenaar94950a92010-12-02 16:01:29 +01004718 if (p_wic)
4719 options += WILD_ICASE;
4720
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004722 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 {
4724 *matchcount = 0;
4725 *matches = NULL;
4726 }
4727 vim_free(file_str);
4728
4729 return EXPAND_OK;
4730}
4731
4732#ifdef FEAT_MULTI_LANG
4733/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004734 * Cleanup matches for help tags:
4735 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4736 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004738static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739
4740 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004741cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 int i, j;
4744 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004745 char_u buf[4];
4746 char_u *p = buf;
4747
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004748 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004749 {
4750 *p++ = '@';
4751 *p++ = p_hlg[0];
4752 *p++ = p_hlg[1];
4753 }
4754 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 for (i = 0; i < num_file; ++i)
4757 {
4758 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004759 if (len <= 0)
4760 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004761 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 /* Sorting on priority means the same item in another language may
4764 * be anywhere. Search all items for a match up to the "@en". */
4765 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004766 if (j != i && (int)STRLEN(file[j]) == len + 3
4767 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 break;
4769 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004770 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 file[i][len] = NUL;
4772 }
4773 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004774
4775 if (*buf != NUL)
4776 for (i = 0; i < num_file; ++i)
4777 {
4778 len = (int)STRLEN(file[i]) - 3;
4779 if (len <= 0)
4780 continue;
4781 if (STRCMP(file[i] + len, buf) == 0)
4782 {
4783 /* remove the default language */
4784 file[i][len] = NUL;
4785 }
4786 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787}
4788#endif
4789
4790/*
4791 * Do the expansion based on xp->xp_context and "pat".
4792 */
4793 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004794ExpandFromContext(
4795 expand_T *xp,
4796 char_u *pat,
4797 int *num_file,
4798 char_u ***file,
4799 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800{
4801#ifdef FEAT_CMDL_COMPL
4802 regmatch_T regmatch;
4803#endif
4804 int ret;
4805 int flags;
4806
4807 flags = EW_DIR; /* include directories */
4808 if (options & WILD_LIST_NOTFOUND)
4809 flags |= EW_NOTFOUND;
4810 if (options & WILD_ADD_SLASH)
4811 flags |= EW_ADDSLASH;
4812 if (options & WILD_KEEP_ALL)
4813 flags |= EW_KEEPALL;
4814 if (options & WILD_SILENT)
4815 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004816 if (options & WILD_ALLLINKS)
4817 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004819 if (xp->xp_context == EXPAND_FILES
4820 || xp->xp_context == EXPAND_DIRECTORIES
4821 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
4823 /*
4824 * Expand file or directory names.
4825 */
4826 int free_pat = FALSE;
4827 int i;
4828
4829 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4830 * space */
4831 if (xp->xp_backslash != XP_BS_NONE)
4832 {
4833 free_pat = TRUE;
4834 pat = vim_strsave(pat);
4835 for (i = 0; pat[i]; ++i)
4836 if (pat[i] == '\\')
4837 {
4838 if (xp->xp_backslash == XP_BS_THREE
4839 && pat[i + 1] == '\\'
4840 && pat[i + 2] == '\\'
4841 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004842 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (xp->xp_backslash == XP_BS_ONE
4844 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004845 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
4847 }
4848
4849 if (xp->xp_context == EXPAND_FILES)
4850 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004851 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4852 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 else
4854 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004855 if (options & WILD_ICASE)
4856 flags |= EW_ICASE;
4857
Bram Moolenaard7834d32009-12-02 16:14:36 +00004858 /* Expand wildcards, supporting %:h and the like. */
4859 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 if (free_pat)
4861 vim_free(pat);
4862 return ret;
4863 }
4864
4865 *file = (char_u **)"";
4866 *num_file = 0;
4867 if (xp->xp_context == EXPAND_HELP)
4868 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004869 /* With an empty argument we would get all the help tags, which is
4870 * very slow. Get matches for "help" instead. */
4871 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4872 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
4874#ifdef FEAT_MULTI_LANG
4875 cleanup_help_tags(*num_file, *file);
4876#endif
4877 return OK;
4878 }
4879 return FAIL;
4880 }
4881
4882#ifndef FEAT_CMDL_COMPL
4883 return FAIL;
4884#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004885 if (xp->xp_context == EXPAND_SHELLCMD)
4886 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (xp->xp_context == EXPAND_OLD_SETTING)
4888 return ExpandOldSetting(num_file, file);
4889 if (xp->xp_context == EXPAND_BUFFERS)
4890 return ExpandBufnames(pat, num_file, file, options);
4891 if (xp->xp_context == EXPAND_TAGS
4892 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4893 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4894 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004895 {
4896 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004897 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4898 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004901 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004902 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004903 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004904 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004905 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004906 {
4907 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004908 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004909 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004910 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004911 {
4912 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004913 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004914 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004915# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4916 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004917 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004918# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004919 if (xp->xp_context == EXPAND_PACKADD)
4920 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921
4922 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4923 if (regmatch.regprog == NULL)
4924 return FAIL;
4925
4926 /* set ignore-case according to p_ic, p_scs and pat */
4927 regmatch.rm_ic = ignorecase(pat);
4928
4929 if (xp->xp_context == EXPAND_SETTINGS
4930 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4931 ret = ExpandSettings(xp, &regmatch, num_file, file);
4932 else if (xp->xp_context == EXPAND_MAPPINGS)
4933 ret = ExpandMappings(&regmatch, num_file, file);
4934# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4935 else if (xp->xp_context == EXPAND_USER_DEFINED)
4936 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4937# endif
4938 else
4939 {
4940 static struct expgen
4941 {
4942 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004943 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004945 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 } tab[] =
4947 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004948 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4949 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004950 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004951 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004952#ifdef FEAT_CMDHIST
4953 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004956 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004957 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004958 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4959 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4960 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961#endif
4962#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004963 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4964 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4965 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4966 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967#endif
4968#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004969 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4970 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971#endif
4972#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004973 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004975#ifdef FEAT_PROFILE
4976 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4977#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004978 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004979 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4980 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004981#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004982 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004983#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004984#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004985 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004986#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004987#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004988 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4991 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004992 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4993 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004995 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004996 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004997 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 };
4999 int i;
5000
5001 /*
5002 * Find a context in the table and call the ExpandGeneric() with the
5003 * right function to do the expansion.
5004 */
5005 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005006 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 if (xp->xp_context == tab[i].context)
5008 {
5009 if (tab[i].ic)
5010 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005011 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005012 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 break;
5014 }
5015 }
5016
Bram Moolenaar473de612013-06-08 18:19:48 +02005017 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 return ret;
5020#endif /* FEAT_CMDL_COMPL */
5021}
5022
5023#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5024/*
5025 * Expand a list of names.
5026 *
5027 * Generic function for command line completion. It calls a function to
5028 * obtain strings, one by one. The strings are matched against a regexp
5029 * program. Matching strings are copied into an array, which is returned.
5030 *
5031 * Returns OK when no problems encountered, FAIL for error (out of memory).
5032 */
5033 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034ExpandGeneric(
5035 expand_T *xp,
5036 regmatch_T *regmatch,
5037 int *num_file,
5038 char_u ***file,
5039 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005041 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042{
5043 int i;
5044 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005045 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 char_u *str;
5047
5048 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005049 * round == 0: count the number of matching names
5050 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005052 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 {
5054 for (i = 0; ; ++i)
5055 {
5056 str = (*func)(xp, i);
5057 if (str == NULL) /* end of list */
5058 break;
5059 if (*str == NUL) /* skip empty strings */
5060 continue;
5061
5062 if (vim_regexec(regmatch, str, (colnr_T)0))
5063 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005064 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005066 if (escaped)
5067 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5068 else
5069 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 (*file)[count] = str;
5071#ifdef FEAT_MENU
5072 if (func == get_menu_names && str != NULL)
5073 {
5074 /* test for separator added by get_menu_names() */
5075 str += STRLEN(str) - 1;
5076 if (*str == '\001')
5077 *str = '.';
5078 }
5079#endif
5080 }
5081 ++count;
5082 }
5083 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005084 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 if (count == 0)
5087 return OK;
5088 *num_file = count;
5089 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5090 if (*file == NULL)
5091 {
5092 *file = (char_u **)"";
5093 return FAIL;
5094 }
5095 count = 0;
5096 }
5097 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005098
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005099 /* Sort the results. Keep menu's in the specified order. */
5100 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005101 {
5102 if (xp->xp_context == EXPAND_EXPRESSION
5103 || xp->xp_context == EXPAND_FUNCTIONS
5104 || xp->xp_context == EXPAND_USER_FUNC)
5105 /* <SNR> functions should be sorted to the end. */
5106 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5107 sort_func_compare);
5108 else
5109 sort_strings(*file, *num_file);
5110 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005111
Bram Moolenaar4f688582007-07-24 12:34:30 +00005112#ifdef FEAT_CMDL_COMPL
5113 /* Reset the variables used for special highlight names expansion, so that
5114 * they don't show up when getting normal highlight names by ID. */
5115 reset_expand_highlight();
5116#endif
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 return OK;
5119}
5120
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005121/*
5122 * Complete a shell command.
5123 * Returns FAIL or OK;
5124 */
5125 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005126expand_shellcmd(
5127 char_u *filepat, /* pattern to match with command names */
5128 int *num_file, /* return: number of matches */
5129 char_u ***file, /* return: array with matches */
5130 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005131{
5132 char_u *pat;
5133 int i;
5134 char_u *path;
5135 int mustfree = FALSE;
5136 garray_T ga;
5137 char_u *buf = alloc(MAXPATHL);
5138 size_t l;
5139 char_u *s, *e;
5140 int flags = flagsarg;
5141 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005142 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005143
5144 if (buf == NULL)
5145 return FAIL;
5146
5147 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5148 * space */
5149 pat = vim_strsave(filepat);
5150 for (i = 0; pat[i]; ++i)
5151 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005152 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005153
Bram Moolenaarb5971142015-03-21 17:32:19 +01005154 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005155
5156 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005157 if (mch_isFullName(pat))
5158 path = (char_u *)" ";
5159 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005160 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5161 path = (char_u *)".";
5162 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005163 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005164 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005165 if (path == NULL)
5166 path = (char_u *)"";
5167 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005168
5169 /*
5170 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005171 * collect them in "ga". When "." is not in $PATH also expand for the
5172 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005173 */
5174 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005175 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005176 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005177 if (*s == NUL)
5178 {
5179 if (did_curdir)
5180 break;
5181 /* Find directories in the current directory, path is empty. */
5182 did_curdir = TRUE;
5183 }
5184 else if (*s == '.')
5185 did_curdir = TRUE;
5186
Bram Moolenaar68c31742006-09-02 15:54:18 +00005187 if (*s == ' ')
5188 ++s; /* Skip space used for absolute path name. */
5189
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005190#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005191 e = vim_strchr(s, ';');
5192#else
5193 e = vim_strchr(s, ':');
5194#endif
5195 if (e == NULL)
5196 e = s + STRLEN(s);
5197
5198 l = e - s;
5199 if (l > MAXPATHL - 5)
5200 break;
5201 vim_strncpy(buf, s, l);
5202 add_pathsep(buf);
5203 l = STRLEN(buf);
5204 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5205
5206 /* Expand matches in one directory of $PATH. */
5207 ret = expand_wildcards(1, &buf, num_file, file, flags);
5208 if (ret == OK)
5209 {
5210 if (ga_grow(&ga, *num_file) == FAIL)
5211 FreeWild(*num_file, *file);
5212 else
5213 {
5214 for (i = 0; i < *num_file; ++i)
5215 {
5216 s = (*file)[i];
5217 if (STRLEN(s) > l)
5218 {
5219 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005220 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005221 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5222 }
5223 else
5224 vim_free(s);
5225 }
5226 vim_free(*file);
5227 }
5228 }
5229 if (*e != NUL)
5230 ++e;
5231 }
5232 *file = ga.ga_data;
5233 *num_file = ga.ga_len;
5234
5235 vim_free(buf);
5236 vim_free(pat);
5237 if (mustfree)
5238 vim_free(path);
5239 return OK;
5240}
5241
5242
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005244static 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 +00005245
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005247 * Call "user_expand_func()" to invoke a user defined Vim script function and
5248 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005250 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005251call_user_expand_func(
5252 void *(*user_expand_func)(char_u *, int, char_u **, int),
5253 expand_T *xp,
5254 int *num_file,
5255 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005257 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005258 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005261 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005262 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaarb7515462013-06-29 12:58:33 +02005264 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005265 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 *num_file = 0;
5267 *file = NULL;
5268
Bram Moolenaarb7515462013-06-29 12:58:33 +02005269 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005270 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005271 keep = ccline.cmdbuff[ccline.cmdlen];
5272 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005273 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005274
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005275 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005276 args[1] = xp->xp_line;
5277 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 args[2] = num;
5279
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005280 /* Save the cmdline, we don't know what the function may do. */
5281 save_ccline = ccline;
5282 ccline.cmdbuff = NULL;
5283 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005285
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005286 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005287
5288 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005290 if (ccline.cmdbuff != NULL)
5291 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005292
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005293 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005294 return ret;
5295}
5296
5297/*
5298 * Expand names with a function defined by the user.
5299 */
5300 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005301ExpandUserDefined(
5302 expand_T *xp,
5303 regmatch_T *regmatch,
5304 int *num_file,
5305 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005306{
5307 char_u *retstr;
5308 char_u *s;
5309 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005310 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005311 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005312 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005313
5314 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5315 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 return FAIL;
5317
5318 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005319 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
5321 e = vim_strchr(s, '\n');
5322 if (e == NULL)
5323 e = s + STRLEN(s);
5324 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005325 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005327 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5328 *e = keep;
5329
5330 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005332 if (ga_grow(&ga, 1) == FAIL)
5333 break;
5334 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5335 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 if (*e != NUL)
5339 ++e;
5340 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005341 vim_free(retstr);
5342 *file = ga.ga_data;
5343 *num_file = ga.ga_len;
5344 return OK;
5345}
5346
5347/*
5348 * Expand names with a list returned by a function defined by the user.
5349 */
5350 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005351ExpandUserList(
5352 expand_T *xp,
5353 int *num_file,
5354 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005355{
5356 list_T *retlist;
5357 listitem_T *li;
5358 garray_T ga;
5359
5360 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5361 if (retlist == NULL)
5362 return FAIL;
5363
5364 ga_init2(&ga, (int)sizeof(char *), 3);
5365 /* Loop over the items in the list. */
5366 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5367 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005368 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5369 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005370
5371 if (ga_grow(&ga, 1) == FAIL)
5372 break;
5373
5374 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005375 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005376 ++ga.ga_len;
5377 }
5378 list_unref(retlist);
5379
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 *file = ga.ga_data;
5381 *num_file = ga.ga_len;
5382 return OK;
5383}
5384#endif
5385
5386/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005387 * Expand color scheme, compiler or filetype names.
5388 * Search from 'runtimepath':
5389 * 'runtimepath'/{dirnames}/{pat}.vim
5390 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5391 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5392 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5393 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005394 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 */
5396 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005397ExpandRTDir(
5398 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005399 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005400 int *num_file,
5401 char_u ***file,
5402 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 char_u *s;
5405 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005406 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005408 int i;
5409 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 *num_file = 0;
5412 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005413 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005414 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005416 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005418 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5419 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005421 ga_clear_strings(&ga);
5422 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005424 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005425 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005426 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005428
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005429 if (flags & DIP_START) {
5430 for (i = 0; dirnames[i] != NULL; ++i)
5431 {
5432 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5433 if (s == NULL)
5434 {
5435 ga_clear_strings(&ga);
5436 return FAIL;
5437 }
5438 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5439 globpath(p_pp, s, &ga, 0);
5440 vim_free(s);
5441 }
5442 }
5443
5444 if (flags & DIP_OPT) {
5445 for (i = 0; dirnames[i] != NULL; ++i)
5446 {
5447 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5448 if (s == NULL)
5449 {
5450 ga_clear_strings(&ga);
5451 return FAIL;
5452 }
5453 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5454 globpath(p_pp, s, &ga, 0);
5455 vim_free(s);
5456 }
5457 }
5458
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005459 for (i = 0; i < ga.ga_len; ++i)
5460 {
5461 match = ((char_u **)ga.ga_data)[i];
5462 s = match;
5463 e = s + STRLEN(s);
5464 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5465 {
5466 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005467 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005468 if (s < match || vim_ispathsep(*s))
5469 break;
5470 ++s;
5471 *e = NUL;
5472 mch_memmove(match, s, e - s + 1);
5473 }
5474 }
5475
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005476 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005477 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005478
5479 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005480 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005481 remove_duplicates(&ga);
5482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 *file = ga.ga_data;
5484 *num_file = ga.ga_len;
5485 return OK;
5486}
5487
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005488/*
5489 * Expand loadplugin names:
5490 * 'packpath'/pack/ * /opt/{pat}
5491 */
5492 static int
5493ExpandPackAddDir(
5494 char_u *pat,
5495 int *num_file,
5496 char_u ***file)
5497{
5498 char_u *s;
5499 char_u *e;
5500 char_u *match;
5501 garray_T ga;
5502 int i;
5503 int pat_len;
5504
5505 *num_file = 0;
5506 *file = NULL;
5507 pat_len = (int)STRLEN(pat);
5508 ga_init2(&ga, (int)sizeof(char *), 10);
5509
5510 s = alloc((unsigned)(pat_len + 26));
5511 if (s == NULL)
5512 {
5513 ga_clear_strings(&ga);
5514 return FAIL;
5515 }
5516 sprintf((char *)s, "pack/*/opt/%s*", pat);
5517 globpath(p_pp, s, &ga, 0);
5518 vim_free(s);
5519
5520 for (i = 0; i < ga.ga_len; ++i)
5521 {
5522 match = ((char_u **)ga.ga_data)[i];
5523 s = gettail(match);
5524 e = s + STRLEN(s);
5525 mch_memmove(match, s, e - s + 1);
5526 }
5527
5528 if (ga.ga_len == 0)
5529 return FAIL;
5530
5531 /* Sort and remove duplicates which can happen when specifying multiple
5532 * directories in dirnames. */
5533 remove_duplicates(&ga);
5534
5535 *file = ga.ga_data;
5536 *num_file = ga.ga_len;
5537 return OK;
5538}
5539
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540#endif
5541
5542#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5543/*
5544 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005545 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005547 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005548globpath(
5549 char_u *path,
5550 char_u *file,
5551 garray_T *ga,
5552 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553{
5554 expand_T xpc;
5555 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 int num_p;
5558 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 buf = alloc(MAXPATHL);
5561 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005562 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005564 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005566
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 /* Loop over all entries in {path}. */
5568 while (*path != NUL)
5569 {
5570 /* Copy one item of the path to buf[] and concatenate the file name. */
5571 copy_option_part(&path, buf, MAXPATHL, ",");
5572 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5573 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005574# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005575 /* Using the platform's path separator (\) makes vim incorrectly
5576 * treat it as an escape character, use '/' instead. */
5577 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5578 STRCAT(buf, "/");
5579# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005581# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005583 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5584 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005586 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005588 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 for (i = 0; i < num_p; ++i)
5591 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005592 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005593 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005594 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 FreeWild(num_p, p);
5599 }
5600 }
5601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602
5603 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604}
5605
5606#endif
5607
5608#if defined(FEAT_CMDHIST) || defined(PROTO)
5609
5610/*********************************
5611 * Command line history stuff *
5612 *********************************/
5613
5614/*
5615 * Translate a history character to the associated type number.
5616 */
5617 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
5620 if (c == ':')
5621 return HIST_CMD;
5622 if (c == '=')
5623 return HIST_EXPR;
5624 if (c == '@')
5625 return HIST_INPUT;
5626 if (c == '>')
5627 return HIST_DEBUG;
5628 return HIST_SEARCH; /* must be '?' or '/' */
5629}
5630
5631/*
5632 * Table of history names.
5633 * These names are used in :history and various hist...() functions.
5634 * It is sufficient to give the significant prefix of a history name.
5635 */
5636
5637static char *(history_names[]) =
5638{
5639 "cmd",
5640 "search",
5641 "expr",
5642 "input",
5643 "debug",
5644 NULL
5645};
5646
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005647#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5648/*
5649 * Function given to ExpandGeneric() to obtain the possible first
5650 * arguments of the ":history command.
5651 */
5652 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005653get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005654{
5655 static char_u compl[2] = { NUL, NUL };
5656 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005657 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005658 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5659
5660 if (idx < short_names_count)
5661 {
5662 compl[0] = (char_u)short_names[idx];
5663 return compl;
5664 }
5665 if (idx < short_names_count + history_name_count)
5666 return (char_u *)history_names[idx - short_names_count];
5667 if (idx == short_names_count + history_name_count)
5668 return (char_u *)"all";
5669 return NULL;
5670}
5671#endif
5672
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673/*
5674 * init_history() - Initialize the command line history.
5675 * Also used to re-allocate the history when the size changes.
5676 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005677 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005678init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 int newlen; /* new length of history table */
5681 histentry_T *temp;
5682 int i;
5683 int j;
5684 int type;
5685
5686 /*
5687 * If size of history table changed, reallocate it
5688 */
5689 newlen = (int)p_hi;
5690 if (newlen != hislen) /* history length changed */
5691 {
5692 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5693 {
5694 if (newlen)
5695 {
5696 temp = (histentry_T *)lalloc(
5697 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5698 if (temp == NULL) /* out of memory! */
5699 {
5700 if (type == 0) /* first one: just keep the old length */
5701 {
5702 newlen = hislen;
5703 break;
5704 }
5705 /* Already changed one table, now we can only have zero
5706 * length for all tables. */
5707 newlen = 0;
5708 type = -1;
5709 continue;
5710 }
5711 }
5712 else
5713 temp = NULL;
5714 if (newlen == 0 || temp != NULL)
5715 {
5716 if (hisidx[type] < 0) /* there are no entries yet */
5717 {
5718 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005719 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 }
5721 else if (newlen > hislen) /* array becomes bigger */
5722 {
5723 for (i = 0; i <= hisidx[type]; ++i)
5724 temp[i] = history[type][i];
5725 j = i;
5726 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005727 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 for ( ; j < hislen; ++i, ++j)
5729 temp[i] = history[type][j];
5730 }
5731 else /* array becomes smaller or 0 */
5732 {
5733 j = hisidx[type];
5734 for (i = newlen - 1; ; --i)
5735 {
5736 if (i >= 0) /* copy newest entries */
5737 temp[i] = history[type][j];
5738 else /* remove older entries */
5739 vim_free(history[type][j].hisstr);
5740 if (--j < 0)
5741 j = hislen - 1;
5742 if (j == hisidx[type])
5743 break;
5744 }
5745 hisidx[type] = newlen - 1;
5746 }
5747 vim_free(history[type]);
5748 history[type] = temp;
5749 }
5750 }
5751 hislen = newlen;
5752 }
5753}
5754
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005755 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005756clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005757{
5758 hisptr->hisnum = 0;
5759 hisptr->viminfo = FALSE;
5760 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005761 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005762}
5763
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764/*
5765 * Check if command line 'str' is already in history.
5766 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5767 */
5768 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005769in_history(
5770 int type,
5771 char_u *str,
5772 int move_to_front, /* Move the entry to the front if it exists */
5773 int sep,
5774 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 int i;
5777 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005778 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 if (hisidx[type] < 0)
5781 return FALSE;
5782 i = hisidx[type];
5783 do
5784 {
5785 if (history[type][i].hisstr == NULL)
5786 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005787
5788 /* For search history, check that the separator character matches as
5789 * well. */
5790 p = history[type][i].hisstr;
5791 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005792 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005793 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
5795 if (!move_to_front)
5796 return TRUE;
5797 last_i = i;
5798 break;
5799 }
5800 if (--i < 0)
5801 i = hislen - 1;
5802 } while (i != hisidx[type]);
5803
5804 if (last_i >= 0)
5805 {
5806 str = history[type][i].hisstr;
5807 while (i != hisidx[type])
5808 {
5809 if (++i >= hislen)
5810 i = 0;
5811 history[type][last_i] = history[type][i];
5812 last_i = i;
5813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005815 history[type][i].viminfo = FALSE;
5816 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005817 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 return TRUE;
5819 }
5820 return FALSE;
5821}
5822
5823/*
5824 * Convert history name (from table above) to its HIST_ equivalent.
5825 * When "name" is empty, return "cmd" history.
5826 * Returns -1 for unknown history name.
5827 */
5828 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005829get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830{
5831 int i;
5832 int len = (int)STRLEN(name);
5833
5834 /* No argument: use current history. */
5835 if (len == 0)
5836 return hist_char2type(ccline.cmdfirstc);
5837
5838 for (i = 0; history_names[i] != NULL; ++i)
5839 if (STRNICMP(name, history_names[i], len) == 0)
5840 return i;
5841
5842 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5843 return hist_char2type(name[0]);
5844
5845 return -1;
5846}
5847
5848static int last_maptick = -1; /* last seen maptick */
5849
5850/*
5851 * Add the given string to the given history. If the string is already in the
5852 * history then it is moved to the front. "histype" may be one of he HIST_
5853 * values.
5854 */
5855 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005856add_to_history(
5857 int histype,
5858 char_u *new_entry,
5859 int in_map, /* consider maptick when inside a mapping */
5860 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 histentry_T *hisptr;
5863 int len;
5864
5865 if (hislen == 0) /* no history */
5866 return;
5867
Bram Moolenaara939e432013-11-09 05:30:26 +01005868 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5869 return;
5870
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 /*
5872 * Searches inside the same mapping overwrite each other, so that only
5873 * the last line is kept. Be careful not to remove a line that was moved
5874 * down, only lines that were added.
5875 */
5876 if (histype == HIST_SEARCH && in_map)
5877 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005878 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 {
5880 /* Current line is from the same mapping, remove it */
5881 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5882 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005883 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 --hisnum[histype];
5885 if (--hisidx[HIST_SEARCH] < 0)
5886 hisidx[HIST_SEARCH] = hislen - 1;
5887 }
5888 last_maptick = -1;
5889 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005890 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 {
5892 if (++hisidx[histype] == hislen)
5893 hisidx[histype] = 0;
5894 hisptr = &history[histype][hisidx[histype]];
5895 vim_free(hisptr->hisstr);
5896
5897 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005898 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5900 if (hisptr->hisstr != NULL)
5901 hisptr->hisstr[len + 1] = sep;
5902
5903 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005904 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005905 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (histype == HIST_SEARCH && in_map)
5907 last_maptick = maptick;
5908 }
5909}
5910
5911#if defined(FEAT_EVAL) || defined(PROTO)
5912
5913/*
5914 * Get identifier of newest history entry.
5915 * "histype" may be one of the HIST_ values.
5916 */
5917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005918get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919{
5920 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5921 || hisidx[histype] < 0)
5922 return -1;
5923
5924 return history[histype][hisidx[histype]].hisnum;
5925}
5926
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005927/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 * Calculate history index from a number:
5929 * num > 0: seen as identifying number of a history entry
5930 * num < 0: relative position in history wrt newest entry
5931 * "histype" may be one of the HIST_ values.
5932 */
5933 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005934calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935{
5936 int i;
5937 histentry_T *hist;
5938 int wrapped = FALSE;
5939
5940 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5941 || (i = hisidx[histype]) < 0 || num == 0)
5942 return -1;
5943
5944 hist = history[histype];
5945 if (num > 0)
5946 {
5947 while (hist[i].hisnum > num)
5948 if (--i < 0)
5949 {
5950 if (wrapped)
5951 break;
5952 i += hislen;
5953 wrapped = TRUE;
5954 }
5955 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5956 return i;
5957 }
5958 else if (-num <= hislen)
5959 {
5960 i += num + 1;
5961 if (i < 0)
5962 i += hislen;
5963 if (hist[i].hisstr != NULL)
5964 return i;
5965 }
5966 return -1;
5967}
5968
5969/*
5970 * Get a history entry by its index.
5971 * "histype" may be one of the HIST_ values.
5972 */
5973 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 idx = calc_hist_idx(histype, idx);
5977 if (idx >= 0)
5978 return history[histype][idx].hisstr;
5979 else
5980 return (char_u *)"";
5981}
5982
5983/*
5984 * Clear all entries of a history.
5985 * "histype" may be one of the HIST_ values.
5986 */
5987 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005988clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989{
5990 int i;
5991 histentry_T *hisptr;
5992
5993 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5994 {
5995 hisptr = history[histype];
5996 for (i = hislen; i--;)
5997 {
5998 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005999 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006000 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
6002 hisidx[histype] = -1; /* mark history as cleared */
6003 hisnum[histype] = 0; /* reset identifier counter */
6004 return OK;
6005 }
6006 return FAIL;
6007}
6008
6009/*
6010 * Remove all entries matching {str} from a history.
6011 * "histype" may be one of the HIST_ values.
6012 */
6013 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006014del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015{
6016 regmatch_T regmatch;
6017 histentry_T *hisptr;
6018 int idx;
6019 int i;
6020 int last;
6021 int found = FALSE;
6022
6023 regmatch.regprog = NULL;
6024 regmatch.rm_ic = FALSE; /* always match case */
6025 if (hislen != 0
6026 && histype >= 0
6027 && histype < HIST_COUNT
6028 && *str != NUL
6029 && (idx = hisidx[histype]) >= 0
6030 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6031 != NULL)
6032 {
6033 i = last = idx;
6034 do
6035 {
6036 hisptr = &history[histype][i];
6037 if (hisptr->hisstr == NULL)
6038 break;
6039 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6040 {
6041 found = TRUE;
6042 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006043 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
6045 else
6046 {
6047 if (i != last)
6048 {
6049 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006050 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 }
6052 if (--last < 0)
6053 last += hislen;
6054 }
6055 if (--i < 0)
6056 i += hislen;
6057 } while (i != idx);
6058 if (history[histype][idx].hisstr == NULL)
6059 hisidx[histype] = -1;
6060 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006061 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 return found;
6063}
6064
6065/*
6066 * Remove an indexed entry from a history.
6067 * "histype" may be one of the HIST_ values.
6068 */
6069 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006070del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 int i, j;
6073
6074 i = calc_hist_idx(histype, idx);
6075 if (i < 0)
6076 return FALSE;
6077 idx = hisidx[histype];
6078 vim_free(history[histype][i].hisstr);
6079
6080 /* When deleting the last added search string in a mapping, reset
6081 * last_maptick, so that the last added search string isn't deleted again.
6082 */
6083 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6084 last_maptick = -1;
6085
6086 while (i != idx)
6087 {
6088 j = (i + 1) % hislen;
6089 history[histype][i] = history[histype][j];
6090 i = j;
6091 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006092 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 if (--i < 0)
6094 i += hislen;
6095 hisidx[histype] = i;
6096 return TRUE;
6097}
6098
6099#endif /* FEAT_EVAL */
6100
6101#if defined(FEAT_CRYPT) || defined(PROTO)
6102/*
6103 * Very specific function to remove the value in ":set key=val" from the
6104 * history.
6105 */
6106 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006107remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 char_u *p;
6110 int i;
6111
6112 i = hisidx[HIST_CMD];
6113 if (i < 0)
6114 return;
6115 p = history[HIST_CMD][i].hisstr;
6116 if (p != NULL)
6117 for ( ; *p; ++p)
6118 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6119 {
6120 p = vim_strchr(p + 3, '=');
6121 if (p == NULL)
6122 break;
6123 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006124 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 if (p[i] == '\\' && p[i + 1])
6126 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006127 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 --p;
6129 }
6130}
6131#endif
6132
6133#endif /* FEAT_CMDHIST */
6134
Bram Moolenaar064154c2016-03-19 22:50:43 +01006135#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006136/*
6137 * Get pointer to the command line info to use. cmdline_paste() may clear
6138 * ccline and put the previous value in prev_ccline.
6139 */
6140 static struct cmdline_info *
6141get_ccline_ptr(void)
6142{
6143 if ((State & CMDLINE) == 0)
6144 return NULL;
6145 if (ccline.cmdbuff != NULL)
6146 return &ccline;
6147 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6148 return &prev_ccline;
6149 return NULL;
6150}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006151#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006152
Bram Moolenaar064154c2016-03-19 22:50:43 +01006153#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006154/*
6155 * Get the current command line in allocated memory.
6156 * Only works when the command line is being edited.
6157 * Returns NULL when something is wrong.
6158 */
6159 char_u *
6160get_cmdline_str(void)
6161{
6162 struct cmdline_info *p = get_ccline_ptr();
6163
6164 if (p == NULL)
6165 return NULL;
6166 return vim_strnsave(p->cmdbuff, p->cmdlen);
6167}
6168
6169/*
6170 * Get the current command line position, counted in bytes.
6171 * Zero is the first position.
6172 * Only works when the command line is being edited.
6173 * Returns -1 when something is wrong.
6174 */
6175 int
6176get_cmdline_pos(void)
6177{
6178 struct cmdline_info *p = get_ccline_ptr();
6179
6180 if (p == NULL)
6181 return -1;
6182 return p->cmdpos;
6183}
6184
6185/*
6186 * Set the command line byte position to "pos". Zero is the first position.
6187 * Only works when the command line is being edited.
6188 * Returns 1 when failed, 0 when OK.
6189 */
6190 int
6191set_cmdline_pos(
6192 int pos)
6193{
6194 struct cmdline_info *p = get_ccline_ptr();
6195
6196 if (p == NULL)
6197 return 1;
6198
6199 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6200 * changed the command line. */
6201 if (pos < 0)
6202 new_cmdpos = 0;
6203 else
6204 new_cmdpos = pos;
6205 return 0;
6206}
6207#endif
6208
6209#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6210/*
6211 * Get the current command-line type.
6212 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6213 * Only works when the command line is being edited.
6214 * Returns NUL when something is wrong.
6215 */
6216 int
6217get_cmdline_type(void)
6218{
6219 struct cmdline_info *p = get_ccline_ptr();
6220
6221 if (p == NULL)
6222 return NUL;
6223 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006224 return
6225# ifdef FEAT_EVAL
6226 (p->input_fn) ? '@' :
6227# endif
6228 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006229 return p->cmdfirstc;
6230}
6231#endif
6232
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6234/*
6235 * Get indices "num1,num2" that specify a range within a list (not a range of
6236 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6237 * Returns OK if parsed successfully, otherwise FAIL.
6238 */
6239 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006240get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 int len;
6243 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006244 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245
6246 *str = skipwhite(*str);
6247 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6248 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006249 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 *str += len;
6251 *num1 = (int)num;
6252 first = TRUE;
6253 }
6254 *str = skipwhite(*str);
6255 if (**str == ',') /* parse "to" part of range */
6256 {
6257 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006258 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 if (len > 0)
6260 {
6261 *num2 = (int)num;
6262 *str = skipwhite(*str + len);
6263 }
6264 else if (!first) /* no number given at all */
6265 return FAIL;
6266 }
6267 else if (first) /* only one number given */
6268 *num2 = *num1;
6269 return OK;
6270}
6271#endif
6272
6273#if defined(FEAT_CMDHIST) || defined(PROTO)
6274/*
6275 * :history command - print a history
6276 */
6277 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006278ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279{
6280 histentry_T *hist;
6281 int histype1 = HIST_CMD;
6282 int histype2 = HIST_CMD;
6283 int hisidx1 = 1;
6284 int hisidx2 = -1;
6285 int idx;
6286 int i, j, k;
6287 char_u *end;
6288 char_u *arg = eap->arg;
6289
6290 if (hislen == 0)
6291 {
6292 MSG(_("'history' option is zero"));
6293 return;
6294 }
6295
6296 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6297 {
6298 end = arg;
6299 while (ASCII_ISALPHA(*end)
6300 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6301 end++;
6302 i = *end;
6303 *end = NUL;
6304 histype1 = get_histtype(arg);
6305 if (histype1 == -1)
6306 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006307 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 {
6309 histype1 = 0;
6310 histype2 = HIST_COUNT-1;
6311 }
6312 else
6313 {
6314 *end = i;
6315 EMSG(_(e_trailing));
6316 return;
6317 }
6318 }
6319 else
6320 histype2 = histype1;
6321 *end = i;
6322 }
6323 else
6324 end = arg;
6325 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6326 {
6327 EMSG(_(e_trailing));
6328 return;
6329 }
6330
6331 for (; !got_int && histype1 <= histype2; ++histype1)
6332 {
6333 STRCPY(IObuff, "\n # ");
6334 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6335 MSG_PUTS_TITLE(IObuff);
6336 idx = hisidx[histype1];
6337 hist = history[histype1];
6338 j = hisidx1;
6339 k = hisidx2;
6340 if (j < 0)
6341 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6342 if (k < 0)
6343 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6344 if (idx >= 0 && j <= k)
6345 for (i = idx + 1; !got_int; ++i)
6346 {
6347 if (i == hislen)
6348 i = 0;
6349 if (hist[i].hisstr != NULL
6350 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6351 {
6352 msg_putchar('\n');
6353 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6354 hist[i].hisnum);
6355 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6356 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006357 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 else
6359 STRCAT(IObuff, hist[i].hisstr);
6360 msg_outtrans(IObuff);
6361 out_flush();
6362 }
6363 if (i == idx)
6364 break;
6365 }
6366 }
6367}
6368#endif
6369
6370#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006371/*
6372 * Buffers for history read from a viminfo file. Only valid while reading.
6373 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006374static histentry_T *viminfo_history[HIST_COUNT] =
6375 {NULL, NULL, NULL, NULL, NULL};
6376static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6377static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378static int viminfo_add_at_front = FALSE;
6379
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006380static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
6382/*
6383 * Translate a history type number to the associated character.
6384 */
6385 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006386hist_type2char(
6387 int type,
6388 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389{
6390 if (type == HIST_CMD)
6391 return ':';
6392 if (type == HIST_SEARCH)
6393 {
6394 if (use_question)
6395 return '?';
6396 else
6397 return '/';
6398 }
6399 if (type == HIST_EXPR)
6400 return '=';
6401 return '@';
6402}
6403
6404/*
6405 * Prepare for reading the history from the viminfo file.
6406 * This allocates history arrays to store the read history lines.
6407 */
6408 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006409prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006410{
6411 int i;
6412 int num;
6413 int type;
6414 int len;
6415
6416 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006417 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 if (asklen > hislen)
6419 asklen = hislen;
6420
6421 for (type = 0; type < HIST_COUNT; ++type)
6422 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006423 /* Count the number of empty spaces in the history list. Entries read
6424 * from viminfo previously are also considered empty. If there are
6425 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006427 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 num++;
6429 len = asklen;
6430 if (num > len)
6431 len = num;
6432 if (len <= 0)
6433 viminfo_history[type] = NULL;
6434 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006435 viminfo_history[type] = (histentry_T *)lalloc(
6436 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (viminfo_history[type] == NULL)
6438 len = 0;
6439 viminfo_hislen[type] = len;
6440 viminfo_hisidx[type] = 0;
6441 }
6442}
6443
6444/*
6445 * Accept a line from the viminfo, store it in the history array when it's
6446 * new.
6447 */
6448 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 int type;
6452 long_u len;
6453 char_u *val;
6454 char_u *p;
6455
6456 type = hist_char2type(virp->vir_line[0]);
6457 if (viminfo_hisidx[type] < viminfo_hislen[type])
6458 {
6459 val = viminfo_readstring(virp, 1, TRUE);
6460 if (val != NULL && *val != NUL)
6461 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006462 int sep = (*val == ' ' ? NUL : *val);
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006465 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 {
6467 /* Need to re-allocate to append the separator byte. */
6468 len = STRLEN(val);
6469 p = lalloc(len + 2, TRUE);
6470 if (p != NULL)
6471 {
6472 if (type == HIST_SEARCH)
6473 {
6474 /* Search entry: Move the separator from the first
6475 * column to after the NUL. */
6476 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006477 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 else
6480 {
6481 /* Not a search entry: No separator in the viminfo
6482 * file, add a NUL separator. */
6483 mch_memmove(p, val, (size_t)len + 1);
6484 p[len + 1] = NUL;
6485 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006486 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6487 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006488 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6489 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006490 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 }
6492 }
6493 }
6494 vim_free(val);
6495 }
6496 return viminfo_readline(virp);
6497}
6498
Bram Moolenaar07219f92013-04-14 23:19:36 +02006499/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006500 * Accept a new style history line from the viminfo, store it in the history
6501 * array when it's new.
6502 */
6503 void
6504handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006505 garray_T *values,
6506 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006507{
6508 int type;
6509 long_u len;
6510 char_u *val;
6511 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006512 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006513
6514 /* Check the format:
6515 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006516 if (values->ga_len < 4
6517 || vp[0].bv_type != BVAL_NR
6518 || vp[1].bv_type != BVAL_NR
6519 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6520 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006521 return;
6522
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006523 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006524 if (type >= HIST_COUNT)
6525 return;
6526 if (viminfo_hisidx[type] < viminfo_hislen[type])
6527 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006528 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006529 if (val != NULL && *val != NUL)
6530 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006531 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6532 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006533 int idx;
6534 int overwrite = FALSE;
6535
6536 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6537 {
6538 /* If lines were written by an older Vim we need to avoid
6539 * getting duplicates. See if the entry already exists. */
6540 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6541 {
6542 p = viminfo_history[type][idx].hisstr;
6543 if (STRCMP(val, p) == 0
6544 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6545 {
6546 overwrite = TRUE;
6547 break;
6548 }
6549 }
6550
6551 if (!overwrite)
6552 {
6553 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006554 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006555 p = lalloc(len + 2, TRUE);
6556 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006557 else
6558 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006559 if (p != NULL)
6560 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006561 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006562 if (!overwrite)
6563 {
6564 mch_memmove(p, val, (size_t)len + 1);
6565 /* Put the separator after the NUL. */
6566 p[len + 1] = sep;
6567 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006568 viminfo_history[type][idx].hisnum = 0;
6569 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006570 viminfo_hisidx[type]++;
6571 }
6572 }
6573 }
6574 }
6575 }
6576}
6577
6578/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006579 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006580 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006581 static void
6582concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 int idx;
6585 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006586
6587 idx = hisidx[type] + viminfo_hisidx[type];
6588 if (idx >= hislen)
6589 idx -= hislen;
6590 else if (idx < 0)
6591 idx = hislen - 1;
6592 if (viminfo_add_at_front)
6593 hisidx[type] = idx;
6594 else
6595 {
6596 if (hisidx[type] == -1)
6597 hisidx[type] = hislen - 1;
6598 do
6599 {
6600 if (history[type][idx].hisstr != NULL
6601 || history[type][idx].viminfo)
6602 break;
6603 if (++idx == hislen)
6604 idx = 0;
6605 } while (idx != hisidx[type]);
6606 if (idx != hisidx[type] && --idx < 0)
6607 idx = hislen - 1;
6608 }
6609 for (i = 0; i < viminfo_hisidx[type]; i++)
6610 {
6611 vim_free(history[type][idx].hisstr);
6612 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6613 history[type][idx].viminfo = TRUE;
6614 history[type][idx].time_set = viminfo_history[type][i].time_set;
6615 if (--idx < 0)
6616 idx = hislen - 1;
6617 }
6618 idx += 1;
6619 idx %= hislen;
6620 for (i = 0; i < viminfo_hisidx[type]; i++)
6621 {
6622 history[type][idx++].hisnum = ++hisnum[type];
6623 idx %= hislen;
6624 }
6625}
6626
6627#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6628 static int
6629#ifdef __BORLANDC__
6630_RTLENTRYF
6631#endif
6632sort_hist(const void *s1, const void *s2)
6633{
6634 histentry_T *p1 = *(histentry_T **)s1;
6635 histentry_T *p2 = *(histentry_T **)s2;
6636
6637 if (p1->time_set < p2->time_set) return -1;
6638 if (p1->time_set > p2->time_set) return 1;
6639 return 0;
6640}
6641#endif
6642
6643/*
6644 * Merge history lines from viminfo and lines typed in this Vim based on the
6645 * timestamp;
6646 */
6647 static void
6648merge_history(int type)
6649{
6650 int max_len;
6651 histentry_T **tot_hist;
6652 histentry_T *new_hist;
6653 int i;
6654 int len;
6655
6656 /* Make one long list with all entries. */
6657 max_len = hislen + viminfo_hisidx[type];
6658 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6659 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6660 if (tot_hist == NULL || new_hist == NULL)
6661 {
6662 vim_free(tot_hist);
6663 vim_free(new_hist);
6664 return;
6665 }
6666 for (i = 0; i < viminfo_hisidx[type]; i++)
6667 tot_hist[i] = &viminfo_history[type][i];
6668 len = i;
6669 for (i = 0; i < hislen; i++)
6670 if (history[type][i].hisstr != NULL)
6671 tot_hist[len++] = &history[type][i];
6672
6673 /* Sort the list on timestamp. */
6674 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6675
6676 /* Keep the newest ones. */
6677 for (i = 0; i < hislen; i++)
6678 {
6679 if (i < len)
6680 {
6681 new_hist[i] = *tot_hist[i];
6682 tot_hist[i]->hisstr = NULL;
6683 if (new_hist[i].hisnum == 0)
6684 new_hist[i].hisnum = ++hisnum[type];
6685 }
6686 else
6687 clear_hist_entry(&new_hist[i]);
6688 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006689 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006690
6691 /* Free what is not kept. */
6692 for (i = 0; i < viminfo_hisidx[type]; i++)
6693 vim_free(viminfo_history[type][i].hisstr);
6694 for (i = 0; i < hislen; i++)
6695 vim_free(history[type][i].hisstr);
6696 vim_free(history[type]);
6697 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006698 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006699}
6700
6701/*
6702 * Finish reading history lines from viminfo. Not used when writing viminfo.
6703 */
6704 void
6705finish_viminfo_history(vir_T *virp)
6706{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006708 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 for (type = 0; type < HIST_COUNT; ++type)
6711 {
6712 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006713 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006714
6715 if (merge)
6716 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006718 concat_history(type);
6719
Bram Moolenaard23a8232018-02-10 18:45:26 +01006720 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006721 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 }
6723}
6724
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006725/*
6726 * Write history to viminfo file in "fp".
6727 * When "merge" is TRUE merge history lines with a previously read viminfo
6728 * file, data is in viminfo_history[].
6729 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6730 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006732write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 int i;
6735 int type;
6736 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006737 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738
6739 init_history();
6740 if (hislen == 0)
6741 return;
6742 for (type = 0; type < HIST_COUNT; ++type)
6743 {
6744 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6745 if (num_saved == 0)
6746 continue;
6747 if (num_saved < 0) /* Use default */
6748 num_saved = hislen;
6749 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6750 type == HIST_CMD ? _("Command Line") :
6751 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006752 type == HIST_EXPR ? _("Expression") :
6753 type == HIST_INPUT ? _("Input Line") :
6754 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 if (num_saved > hislen)
6756 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006757
6758 /*
6759 * Merge typed and viminfo history:
6760 * round 1: history of typed commands.
6761 * round 2: history from recently read viminfo.
6762 */
6763 for (round = 1; round <= 2; ++round)
6764 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006765 if (round == 1)
6766 /* start at newest entry, somewhere in the list */
6767 i = hisidx[type];
6768 else if (viminfo_hisidx[type] > 0)
6769 /* start at newest entry, first in the list */
6770 i = 0;
6771 else
6772 /* empty list */
6773 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006774 if (i >= 0)
6775 while (num_saved > 0
6776 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006778 char_u *p;
6779 time_t timestamp;
6780 int c = NUL;
6781
6782 if (round == 1)
6783 {
6784 p = history[type][i].hisstr;
6785 timestamp = history[type][i].time_set;
6786 }
6787 else
6788 {
6789 p = viminfo_history[type] == NULL ? NULL
6790 : viminfo_history[type][i].hisstr;
6791 timestamp = viminfo_history[type] == NULL ? 0
6792 : viminfo_history[type][i].time_set;
6793 }
6794
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006795 if (p != NULL && (round == 2
6796 || !merge
6797 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006798 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006799 --num_saved;
6800 fputc(hist_type2char(type, TRUE), fp);
6801 /* For the search history: put the separator in the
6802 * second column; use a space if there isn't one. */
6803 if (type == HIST_SEARCH)
6804 {
6805 c = p[STRLEN(p) + 1];
6806 putc(c == NUL ? ' ' : c, fp);
6807 }
6808 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006809
6810 {
6811 char cbuf[NUMBUFLEN];
6812
6813 /* New style history with a bar line. Format:
6814 * |{bartype},{histtype},{timestamp},{separator},"text" */
6815 if (c == NUL)
6816 cbuf[0] = NUL;
6817 else
6818 sprintf(cbuf, "%d", c);
6819 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6820 type, (long)timestamp, cbuf);
6821 barline_writestring(fp, p, LSIZE - 20);
6822 putc('\n', fp);
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006825 if (round == 1)
6826 {
6827 /* Decrement index, loop around and stop when back at
6828 * the start. */
6829 if (--i < 0)
6830 i = hislen - 1;
6831 if (i == hisidx[type])
6832 break;
6833 }
6834 else
6835 {
6836 /* Increment index. Stop at the end in the while. */
6837 ++i;
6838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006840 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006841 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006842 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006843 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006844 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006845 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
6847}
6848#endif /* FEAT_VIMINFO */
6849
6850#if defined(FEAT_FKMAP) || defined(PROTO)
6851/*
6852 * Write a character at the current cursor+offset position.
6853 * It is directly written into the command buffer block.
6854 */
6855 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006856cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857{
6858 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6859 {
6860 EMSG(_("E198: cmd_pchar beyond the command length"));
6861 return;
6862 }
6863 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6864 ccline.cmdbuff[ccline.cmdlen] = NUL;
6865}
6866
6867 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006868cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869{
6870 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6871 {
6872 /* EMSG(_("cmd_gchar beyond the command length")); */
6873 return NUL;
6874 }
6875 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6876}
6877#endif
6878
6879#if defined(FEAT_CMDWIN) || defined(PROTO)
6880/*
6881 * Open a window on the current command line and history. Allow editing in
6882 * the window. Returns when the window is closed.
6883 * Returns:
6884 * CR if the command is to be executed
6885 * Ctrl_C if it is to be abandoned
6886 * K_IGNORE if editing continues
6887 */
6888 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006889open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890{
6891 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006892 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006894 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 win_T *wp;
6896 int i;
6897 linenr_T lnum;
6898 int histtype;
6899 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 int save_restart_edit = restart_edit;
6901 int save_State = State;
6902 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006903#ifdef FEAT_RIGHTLEFT
6904 int save_cmdmsg_rl = cmdmsg_rl;
6905#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006906#ifdef FEAT_FOLDING
6907 int save_KeyTyped;
6908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909
6910 /* Can't do this recursively. Can't do it when typing a password. */
6911 if (cmdwin_type != 0
6912# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6913 || cmdline_star > 0
6914# endif
6915 )
6916 {
6917 beep_flush();
6918 return K_IGNORE;
6919 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006920 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
6922 /* Save current window sizes. */
6923 win_size_save(&winsizes);
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006926 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006927
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006928 /* don't use a new tab page */
6929 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006930 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 /* Create a window for the command-line buffer. */
6933 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6934 {
6935 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006936 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 return K_IGNORE;
6938 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006939 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940
6941 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006942 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006943 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006946#ifdef FEAT_FOLDING
6947 curwin->w_p_fen = FALSE;
6948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006950 curwin->w_p_rl = cmdmsg_rl;
6951 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006953 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006956 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006957 /* But don't allow switching to another buffer. */
6958 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaar46152342005-09-07 21:18:43 +00006960 /* Showing the prompt may have set need_wait_return, reset it. */
6961 need_wait_return = FALSE;
6962
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006963 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6965 {
6966 if (p_wc == TAB)
6967 {
6968 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6969 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6970 }
6971 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6972 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006973 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006975 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6976 * sets 'textwidth' to 78). */
6977 curbuf->b_p_tw = 0;
6978
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979 /* Fill the buffer with the history. */
6980 init_history();
6981 if (hislen > 0)
6982 {
6983 i = hisidx[histtype];
6984 if (i >= 0)
6985 {
6986 lnum = 0;
6987 do
6988 {
6989 if (++i == hislen)
6990 i = 0;
6991 if (history[histtype][i].hisstr != NULL)
6992 ml_append(lnum++, history[histtype][i].hisstr,
6993 (colnr_T)0, FALSE);
6994 }
6995 while (i != hisidx[histtype]);
6996 }
6997 }
6998
6999 /* Replace the empty last line with the current command-line and put the
7000 * cursor there. */
7001 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7002 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7003 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007004 changed_line_abv_curs();
7005 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007006 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007
7008 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007009 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 /* No Ex mode here! */
7012 exmode_active = 0;
7013
7014 State = NORMAL;
7015# ifdef FEAT_MOUSE
7016 setmouse();
7017# endif
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007020 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007021 if (restart_edit != 0) /* autocmd with ":startinsert" */
7022 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 i = RedrawingDisabled;
7025 RedrawingDisabled = 0;
7026
7027 /*
7028 * Call the main loop until <CR> or CTRL-C is typed.
7029 */
7030 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007031 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033 RedrawingDisabled = i;
7034
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007035# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007036 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007037# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007040 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007041
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007042# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007043 /* Restore KeyTyped in case it is modified by autocommands */
7044 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045# endif
7046
Bram Moolenaarccc18222007-05-10 18:25:20 +00007047 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007048 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 cmdwin_type = 0;
7050
7051 exmode_active = save_exmode;
7052
Bram Moolenaarf9821062008-06-20 16:31:07 +00007053 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007055 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 {
7057 cmdwin_result = Ctrl_C;
7058 EMSG(_("E199: Active window or buffer deleted"));
7059 }
7060 else
7061 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007062# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 /* autocmds may abort script processing */
7064 if (aborting() && cmdwin_result != K_IGNORE)
7065 cmdwin_result = Ctrl_C;
7066# endif
7067 /* Set the new command line from the cmdline buffer. */
7068 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007069 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007071 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7072
7073 if (histtype == HIST_CMD)
7074 {
7075 /* Execute the command directly. */
7076 ccline.cmdbuff = vim_strsave((char_u *)p);
7077 cmdwin_result = CAR;
7078 }
7079 else
7080 {
7081 /* First need to cancel what we were doing. */
7082 ccline.cmdbuff = NULL;
7083 stuffcharReadbuff(':');
7084 stuffReadbuff((char_u *)p);
7085 stuffcharReadbuff(CAR);
7086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 }
7088 else if (cmdwin_result == K_XF2) /* :qa typed */
7089 {
7090 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7091 cmdwin_result = CAR;
7092 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007093 else if (cmdwin_result == Ctrl_C)
7094 {
7095 /* :q or :close, don't execute any command
7096 * and don't modify the cmd window. */
7097 ccline.cmdbuff = NULL;
7098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 else
7100 ccline.cmdbuff = vim_strsave(ml_get_curline());
7101 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007102 {
7103 ccline.cmdbuff = vim_strsave((char_u *)"");
7104 ccline.cmdlen = 0;
7105 ccline.cmdbufflen = 1;
7106 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 else
7110 {
7111 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7112 ccline.cmdbufflen = ccline.cmdlen + 1;
7113 ccline.cmdpos = curwin->w_cursor.col;
7114 if (ccline.cmdpos > ccline.cmdlen)
7115 ccline.cmdpos = ccline.cmdlen;
7116 if (cmdwin_result == K_IGNORE)
7117 {
7118 set_cmdspos_cursor();
7119 redrawcmd();
7120 }
7121 }
7122
Bram Moolenaar071d4272004-06-13 20:20:40 +00007123 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007124 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007125# ifdef FEAT_CONCEAL
7126 /* Avoid command-line window first character being concealed. */
7127 curwin->w_p_cole = 0;
7128# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007130 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 win_goto(old_curwin);
7132 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007133
7134 /* win_close() may have already wiped the buffer when 'bh' is
7135 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007136 if (bufref_valid(&bufref))
7137 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138
7139 /* Restore window sizes. */
7140 win_size_restore(&winsizes);
7141
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007142 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 }
7144
7145 ga_clear(&winsizes);
7146 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007147# ifdef FEAT_RIGHTLEFT
7148 cmdmsg_rl = save_cmdmsg_rl;
7149# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150
7151 State = save_State;
7152# ifdef FEAT_MOUSE
7153 setmouse();
7154# endif
7155
7156 return cmdwin_result;
7157}
7158#endif /* FEAT_CMDWIN */
7159
7160/*
7161 * Used for commands that either take a simple command string argument, or:
7162 * cmd << endmarker
7163 * {script}
7164 * endmarker
7165 * Returns a pointer to allocated memory with {script} or NULL.
7166 */
7167 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007168script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 char_u *theline;
7171 char *end_pattern = NULL;
7172 char dot[] = ".";
7173 garray_T ga;
7174
7175 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7176 return NULL;
7177
7178 ga_init2(&ga, 1, 0x400);
7179
7180 if (cmd[2] != NUL)
7181 end_pattern = (char *)skipwhite(cmd + 2);
7182 else
7183 end_pattern = dot;
7184
7185 for (;;)
7186 {
7187 theline = eap->getline(
7188#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007189 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190#endif
7191 NUL, eap->cookie, 0);
7192
7193 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007194 {
7195 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198
7199 ga_concat(&ga, theline);
7200 ga_append(&ga, '\n');
7201 vim_free(theline);
7202 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007203 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
7205 return (char_u *)ga.ga_data;
7206}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007207
7208#ifdef FEAT_SEARCH_EXTRA
7209 static void
7210set_search_match(pos_T *t)
7211{
7212 /*
7213 * First move cursor to end of match, then to the start. This
7214 * moves the whole match onto the screen when 'nowrap' is set.
7215 */
7216 t->lnum += search_match_lines;
7217 t->col = search_match_endcol;
7218 if (t->lnum > curbuf->b_ml.ml_line_count)
7219 {
7220 t->lnum = curbuf->b_ml.ml_line_count;
7221 coladvance((colnr_T)MAXCOL);
7222 }
7223}
7224#endif