blob: ef8b4bdac683e0cb4b72ab238764be33b297e612 [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
190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 * getcmdline() - accept a command line starting with firstc.
192 *
193 * firstc == ':' get ":" command line.
194 * firstc == '/' or '?' get search pattern
195 * firstc == '=' get expression
196 * firstc == '@' get text for input() function
197 * firstc == '>' get text for debug mode
198 * firstc == NUL get text for :insert command
199 * firstc == -1 like NUL, and break on CTRL-C
200 *
201 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
202 * command line.
203 *
204 * Careful: getcmdline() can be called recursively!
205 *
206 * Return pointer to allocated string if there is a commandline, NULL
207 * otherwise.
208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100210getcmdline(
211 int firstc,
212 long count UNUSED, /* only used for incremental search */
213 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214{
215 int c;
216 int i;
217 int j;
218 int gotesc = FALSE; /* TRUE when <ESC> just typed */
219 int do_abbr; /* when TRUE check for abbr. */
220#ifdef FEAT_CMDHIST
221 char_u *lookfor = NULL; /* string to match */
222 int hiscnt; /* current history line in use */
223 int histype; /* history type to be used */
224#endif
225#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaardda933d2016-09-03 21:04:58 +0200226 pos_T search_start; /* where 'incsearch' starts searching */
227 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 colnr_T old_curswant;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200229 colnr_T init_curswant = curwin->w_curswant;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230 colnr_T old_leftcol;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200231 colnr_T init_leftcol = curwin->w_leftcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 linenr_T old_topline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200233 linenr_T init_topline = curwin->w_topline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +0200234 pos_T match_start = curwin->w_cursor;
235 pos_T match_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236# ifdef FEAT_DIFF
237 int old_topfill;
Bram Moolenaard0480092017-11-16 22:20:39 +0100238 int init_topfill = curwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239# endif
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200240 linenr_T old_botline, old_empty_rows;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200241 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200242 linenr_T init_empty_rows = curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243 int did_incsearch = FALSE;
244 int incsearch_postponed = FALSE;
245#endif
246 int did_wild_list = FALSE; /* did wild_list() recently */
247 int wim_index = 0; /* index in wim_flags[] */
248 int res;
249 int save_msg_scroll = msg_scroll;
250 int save_State = State; /* remember State when called */
251 int some_key_typed = FALSE; /* one of the keys was typed */
252#ifdef FEAT_MOUSE
253 /* mouse drag and release events are ignored, unless they are
254 * preceded with a mouse down event */
255 int ignore_drag_release = TRUE;
256#endif
257#ifdef FEAT_EVAL
258 int break_ctrl_c = FALSE;
259#endif
260 expand_T xpc;
261 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100262#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000263 /* Everything that may work recursively should save and restore the
264 * current command line in save_ccline. That includes update_screen(), a
265 * custom status line may invoke ":normal". */
266 struct cmdline_info save_ccline;
267#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200268 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270#ifdef FEAT_EVAL
271 if (firstc == -1)
272 {
273 firstc = NUL;
274 break_ctrl_c = TRUE;
275 }
276#endif
277#ifdef FEAT_RIGHTLEFT
278 /* start without Hebrew mapping for a command line */
279 if (firstc == ':' || firstc == '=' || firstc == '>')
280 cmd_hkmap = 0;
281#endif
282
283 ccline.overstrike = FALSE; /* always start in insert mode */
284#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100285 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200286 save_cursor = curwin->w_cursor; /* may be restored later */
287 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 old_curswant = curwin->w_curswant;
289 old_leftcol = curwin->w_leftcol;
290 old_topline = curwin->w_topline;
291# ifdef FEAT_DIFF
292 old_topfill = curwin->w_topfill;
293# endif
294 old_botline = curwin->w_botline;
Bram Moolenaar9d34d902018-04-27 22:18:12 +0200295 old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296#endif
297
298 /*
299 * set some variables for redrawcmd()
300 */
301 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000302 ccline.cmdindent = (firstc > 0 ? indent : 0);
303
304 /* alloc initial ccline.cmdbuff */
305 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (ccline.cmdbuff == NULL)
307 return NULL; /* out of memory */
308 ccline.cmdlen = ccline.cmdpos = 0;
309 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100310 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000312 /* autoindent for :insert and :append */
313 if (firstc <= 0)
314 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200315 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000316 ccline.cmdbuff[indent] = NUL;
317 ccline.cmdpos = indent;
318 ccline.cmdspos = indent;
319 ccline.cmdlen = indent;
320 }
321
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000323 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324
325#ifdef FEAT_RIGHTLEFT
326 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
327 && (firstc == '/' || firstc == '?'))
328 cmdmsg_rl = TRUE;
329 else
330 cmdmsg_rl = FALSE;
331#endif
332
333 redir_off = TRUE; /* don't redirect the typed command */
334 if (!cmd_silent)
335 {
336 i = msg_scrolled;
337 msg_scrolled = 0; /* avoid wait_return message */
338 gotocmdline(TRUE);
339 msg_scrolled += i;
340 redrawcmdprompt(); /* draw prompt or indent */
341 set_cmdspos();
342 }
343 xpc.xp_context = EXPAND_NOTHING;
344 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000345#ifndef BACKSLASH_IN_FILENAME
346 xpc.xp_shell = FALSE;
347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000349#if defined(FEAT_EVAL)
350 if (ccline.input_fn)
351 {
352 xpc.xp_context = ccline.xp_context;
353 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000354# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000355 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000356# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000357 }
358#endif
359
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360 /*
361 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
362 * doing ":@0" when register 0 doesn't contain a CR.
363 */
364 msg_scroll = FALSE;
365
366 State = CMDLINE;
367
368 if (firstc == '/' || firstc == '?' || firstc == '@')
369 {
370 /* Use ":lmap" mappings for search pattern and input(). */
371 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
372 b_im_ptr = &curbuf->b_p_iminsert;
373 else
374 b_im_ptr = &curbuf->b_p_imsearch;
375 if (*b_im_ptr == B_IMODE_LMAP)
376 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100377#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378 im_set_active(*b_im_ptr == B_IMODE_IM);
379#endif
380 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100381#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 else if (p_imcmdline)
383 im_set_active(TRUE);
384#endif
385
386#ifdef FEAT_MOUSE
387 setmouse();
388#endif
389#ifdef CURSOR_SHAPE
390 ui_cursor_shape(); /* may show different cursor shape */
391#endif
392
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000393 /* When inside an autocommand for writing "exiting" may be set and
394 * terminal mode set to cooked. Need to set raw mode here then. */
395 settmode(TMODE_RAW);
396
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200397 /* Trigger CmdlineEnter autocommands. */
398 cmdline_type = firstc == NUL ? '-' : firstc;
399 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200400
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401#ifdef FEAT_CMDHIST
402 init_history();
403 hiscnt = hislen; /* set hiscnt to impossible history value */
404 histype = hist_char2type(firstc);
405#endif
406
407#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000408 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#endif
410
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200411 /* If something above caused an error, reset the flags, we do want to type
412 * and execute commands. Display may be messed up a bit. */
413 if (did_emsg)
414 redrawcmd();
415 did_emsg = FALSE;
416 got_int = FALSE;
417
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418 /*
419 * Collect the command string, handling editing keys.
420 */
421 for (;;)
422 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000423 redir_off = TRUE; /* Don't redirect the typed command.
424 Repeated, because a ":redir" inside
425 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426#ifdef USE_ON_FLY_SCROLL
427 dont_scroll = FALSE; /* allow scrolling here */
428#endif
429 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
430
Bram Moolenaar72532d32018-04-07 19:09:09 +0200431 did_emsg = FALSE; /* There can't really be a reason why an error
432 that occurs while typing a command should
433 cause the command not to be executed. */
434
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000436
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100437 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
438 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000439 do
440 {
441 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100442 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000443
Bram Moolenaar071d4272004-06-13 20:20:40 +0000444 if (KeyTyped)
445 {
446 some_key_typed = TRUE;
447#ifdef FEAT_RIGHTLEFT
448 if (cmd_hkmap)
449 c = hkmap(c);
450# ifdef FEAT_FKMAP
451 if (cmd_fkmap)
452 c = cmdl_fkmap(c);
453# endif
454 if (cmdmsg_rl && !KeyStuffed)
455 {
456 /* Invert horizontal movements and operations. Only when
457 * typed by the user directly, not when the result of a
458 * mapping. */
459 switch (c)
460 {
461 case K_RIGHT: c = K_LEFT; break;
462 case K_S_RIGHT: c = K_S_LEFT; break;
463 case K_C_RIGHT: c = K_C_LEFT; break;
464 case K_LEFT: c = K_RIGHT; break;
465 case K_S_LEFT: c = K_S_RIGHT; break;
466 case K_C_LEFT: c = K_C_RIGHT; break;
467 }
468 }
469#endif
470 }
471
472 /*
473 * Ignore got_int when CTRL-C was typed here.
474 * Don't ignore it in :global, we really need to break then, e.g., for
475 * ":g/pat/normal /pat" (without the <CR>).
476 * Don't ignore it for the input() function.
477 */
478 if ((c == Ctrl_C
479#ifdef UNIX
480 || c == intr_char
481#endif
482 )
483#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
484 && firstc != '@'
485#endif
486#ifdef FEAT_EVAL
487 && !break_ctrl_c
488#endif
489 && !global_busy)
490 got_int = FALSE;
491
492#ifdef FEAT_CMDHIST
493 /* free old command line when finished moving around in the history
494 * list */
495 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000496 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000497 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 && c != K_PAGEDOWN && c != K_PAGEUP
499 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000500 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100502 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#endif
504
505 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000506 * When there are matching completions to select <S-Tab> works like
507 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000509 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 c = Ctrl_P;
511
512#ifdef FEAT_WILDMENU
513 /* Special translations for 'wildmenu' */
514 if (did_wild_list && p_wmnu)
515 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000516 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000518 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 c = Ctrl_N;
520 }
521 /* Hitting CR after "emenu Name.": complete submenu */
522 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
523 && ccline.cmdpos > 1
524 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
525 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
526 && (c == '\n' || c == '\r' || c == K_KENTER))
527 c = K_DOWN;
528#endif
529
530 /* free expanded names when finished walking through matches */
531 if (xpc.xp_numfiles != -1
532 && !(c == p_wc && KeyTyped) && c != p_wcm
533 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
534 && c != Ctrl_L)
535 {
536 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
537 did_wild_list = FALSE;
538#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000539 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540#endif
541 xpc.xp_context = EXPAND_NOTHING;
542 wim_index = 0;
543#ifdef FEAT_WILDMENU
544 if (p_wmnu && wild_menu_showing != 0)
545 {
546 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000547 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000548
549 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000550 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551
552 if (wild_menu_showing == WM_SCROLLED)
553 {
554 /* Entered command line, move it up */
555 cmdline_row--;
556 redrawcmd();
557 }
558 else if (save_p_ls != -1)
559 {
560 /* restore 'laststatus' and 'winminheight' */
561 p_ls = save_p_ls;
562 p_wmh = save_p_wmh;
563 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000564 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000566 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 redrawcmd();
568 save_p_ls = -1;
569 }
570 else
571 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 redraw_statuslines();
574 }
575 KeyTyped = skt;
576 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000577 if (ccline.input_fn)
578 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 }
580#endif
581 }
582
583#ifdef FEAT_WILDMENU
584 /* Special translations for 'wildmenu' */
585 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
586 {
587 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000588 if (c == K_DOWN && ccline.cmdpos > 0
589 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000591 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 {
593 /* Hitting <Up>: Remove one submenu name in front of the
594 * cursor */
595 int found = FALSE;
596
597 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
598 i = 0;
599 while (--j > 0)
600 {
601 /* check for start of menu name */
602 if (ccline.cmdbuff[j] == ' '
603 && ccline.cmdbuff[j - 1] != '\\')
604 {
605 i = j + 1;
606 break;
607 }
608 /* check for start of submenu name */
609 if (ccline.cmdbuff[j] == '.'
610 && ccline.cmdbuff[j - 1] != '\\')
611 {
612 if (found)
613 {
614 i = j + 1;
615 break;
616 }
617 else
618 found = TRUE;
619 }
620 }
621 if (i > 0)
622 cmdline_del(i);
623 c = p_wc;
624 xpc.xp_context = EXPAND_NOTHING;
625 }
626 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000627 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000628 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000629 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 {
631 char_u upseg[5];
632
633 upseg[0] = PATHSEP;
634 upseg[1] = '.';
635 upseg[2] = '.';
636 upseg[3] = PATHSEP;
637 upseg[4] = NUL;
638
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000639 if (c == K_DOWN
640 && ccline.cmdpos > 0
641 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
642 && (ccline.cmdpos < 3
643 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
645 {
646 /* go down a directory */
647 c = p_wc;
648 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000649 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
651 /* If in a direct ancestor, strip off one ../ to go down */
652 int found = FALSE;
653
654 j = ccline.cmdpos;
655 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
656 while (--j > i)
657 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000658#ifdef FEAT_MBYTE
659 if (has_mbyte)
660 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
661#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 if (vim_ispathsep(ccline.cmdbuff[j]))
663 {
664 found = TRUE;
665 break;
666 }
667 }
668 if (found
669 && ccline.cmdbuff[j - 1] == '.'
670 && ccline.cmdbuff[j - 2] == '.'
671 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
672 {
673 cmdline_del(j - 2);
674 c = p_wc;
675 }
676 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000677 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 {
679 /* go up a directory */
680 int found = FALSE;
681
682 j = ccline.cmdpos - 1;
683 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
684 while (--j > i)
685 {
686#ifdef FEAT_MBYTE
687 if (has_mbyte)
688 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
689#endif
690 if (vim_ispathsep(ccline.cmdbuff[j])
691#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100692 && vim_strchr((char_u *)" *?[{`$%#",
693 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
695 )
696 {
697 if (found)
698 {
699 i = j + 1;
700 break;
701 }
702 else
703 found = TRUE;
704 }
705 }
706
707 if (!found)
708 j = i;
709 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
710 j += 4;
711 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
712 && j == i)
713 j += 3;
714 else
715 j = 0;
716 if (j > 0)
717 {
718 /* TODO this is only for DOS/UNIX systems - need to put in
719 * machine-specific stuff here and in upseg init */
720 cmdline_del(j);
721 put_on_cmdline(upseg + 1, 3, FALSE);
722 }
723 else if (ccline.cmdpos > i)
724 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100725
726 /* Now complete in the new directory. Set KeyTyped in case the
727 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100729 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 }
731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733#endif /* FEAT_WILDMENU */
734
735 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
736 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
737 if (c == Ctrl_BSL)
738 {
739 ++no_mapping;
740 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000741 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 --no_mapping;
743 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200744 /* CTRL-\ e doesn't work when obtaining an expression, unless it
745 * is in a mapping. */
746 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
747 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 {
749 vungetc(c);
750 c = Ctrl_BSL;
751 }
752#ifdef FEAT_EVAL
753 else if (c == 'e')
754 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200755 char_u *p = NULL;
756 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757
758 /*
759 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000760 * Need to save and restore the current command line, to be
761 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 */
763 if (ccline.cmdpos == ccline.cmdlen)
764 new_cmdpos = 99999; /* keep it at the end */
765 else
766 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000767
768 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000770 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 if (c == '=')
772 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000773 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000774 * to avoid nasty things like going to another buffer when
775 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000776 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000777 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000779 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000780 restore_cmdline(&save_ccline);
781
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200782 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200784 len = (int)STRLEN(p);
785 if (realloc_cmdbuff(len + 1) == OK)
786 {
787 ccline.cmdlen = len;
788 STRCPY(ccline.cmdbuff, p);
789 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200791 /* Restore the cursor or use the position set with
792 * set_cmdline_pos(). */
793 if (new_cmdpos > ccline.cmdlen)
794 ccline.cmdpos = ccline.cmdlen;
795 else
796 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200798 KeyTyped = FALSE; /* Don't do p_wc completion. */
799 redrawcmd();
800 goto cmdline_changed;
801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 }
803 }
804 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100805 got_int = FALSE; /* don't abandon the command line */
806 did_emsg = FALSE;
807 emsg_on_display = FALSE;
808 redrawcmd();
809 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 }
811#endif
812 else
813 {
814 if (c == Ctrl_G && p_im && restart_edit == 0)
815 restart_edit = 'a';
816 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
817 in history */
818 goto returncmd; /* back to Normal mode */
819 }
820 }
821
822#ifdef FEAT_CMDWIN
823 if (c == cedit_key || c == K_CMDWIN)
824 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200825 if (ex_normal_busy == 0 && got_int == FALSE)
826 {
827 /*
828 * Open a window to edit the command line (and history).
829 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200830 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200831 some_key_typed = TRUE;
832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834# ifdef FEAT_DIGRAPHS
835 else
836# endif
837#endif
838#ifdef FEAT_DIGRAPHS
839 c = do_digraph(c);
840#endif
841
842 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
843 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
844 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000845 /* In Ex mode a backslash escapes a newline. */
846 if (exmode_active
847 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000848 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000849 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000850 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000852 if (c == K_KENTER)
853 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000855 else
856 {
857 gotesc = FALSE; /* Might have typed ESC previously, don't
858 truncate the cmdline now. */
859 if (ccheck_abbr(c + ABBR_OFF))
860 goto cmdline_changed;
861 if (!cmd_silent)
862 {
863 windgoto(msg_row, 0);
864 out_flush();
865 }
866 break;
867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 }
869
870 /*
871 * Completion for 'wildchar' or 'wildcharm' key.
872 * - hitting <ESC> twice means: abandon command line.
873 * - wildcard expansion is only done when the 'wildchar' key is really
874 * typed, not when it comes from a macro
875 */
876 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
877 {
878 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
879 {
880 /* if 'wildmode' contains "list" may still need to list */
881 if (xpc.xp_numfiles > 1
882 && !did_wild_list
883 && (wim_flags[wim_index] & WIM_LIST))
884 {
885 (void)showmatches(&xpc, FALSE);
886 redrawcmd();
887 did_wild_list = TRUE;
888 }
889 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100890 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
891 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100893 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
894 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 else
896 res = OK; /* don't insert 'wildchar' now */
897 }
898 else /* typed p_wc first time */
899 {
900 wim_index = 0;
901 j = ccline.cmdpos;
902 /* if 'wildmode' first contains "longest", get longest
903 * common part */
904 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100905 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
906 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100908 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
909 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
911 /* if interrupted while completing, behave like it failed */
912 if (got_int)
913 {
914 (void)vpeekc(); /* remove <C-C> from input stream */
915 got_int = FALSE; /* don't abandon the command line */
916 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
917#ifdef FEAT_WILDMENU
918 xpc.xp_context = EXPAND_NOTHING;
919#endif
920 goto cmdline_changed;
921 }
922
923 /* when more than one match, and 'wildmode' first contains
924 * "list", or no change and 'wildmode' contains "longest,list",
925 * list all matches */
926 if (res == OK && xpc.xp_numfiles > 1)
927 {
928 /* a "longest" that didn't do anything is skipped (but not
929 * "list:longest") */
930 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
931 wim_index = 1;
932 if ((wim_flags[wim_index] & WIM_LIST)
933#ifdef FEAT_WILDMENU
934 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
935#endif
936 )
937 {
938 if (!(wim_flags[0] & WIM_LONGEST))
939 {
940#ifdef FEAT_WILDMENU
941 int p_wmnu_save = p_wmnu;
942 p_wmnu = 0;
943#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100944 /* remove match */
945 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946#ifdef FEAT_WILDMENU
947 p_wmnu = p_wmnu_save;
948#endif
949 }
950#ifdef FEAT_WILDMENU
951 (void)showmatches(&xpc, p_wmnu
952 && ((wim_flags[wim_index] & WIM_LIST) == 0));
953#else
954 (void)showmatches(&xpc, FALSE);
955#endif
956 redrawcmd();
957 did_wild_list = TRUE;
958 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100959 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
960 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100962 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
963 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 }
965 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200966 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 }
968#ifdef FEAT_WILDMENU
969 else if (xpc.xp_numfiles == -1)
970 xpc.xp_context = EXPAND_NOTHING;
971#endif
972 }
973 if (wim_index < 3)
974 ++wim_index;
975 if (c == ESC)
976 gotesc = TRUE;
977 if (res == OK)
978 goto cmdline_changed;
979 }
980
981 gotesc = FALSE;
982
983 /* <S-Tab> goes to last match, in a clumsy way */
984 if (c == K_S_TAB && KeyTyped)
985 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100986 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
987 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
988 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 goto cmdline_changed;
990 }
991
992 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
993 c = NL;
994
995 do_abbr = TRUE; /* default: check for abbreviation */
996
997 /*
998 * Big switch for a typed command line character.
999 */
1000 switch (c)
1001 {
1002 case K_BS:
1003 case Ctrl_H:
1004 case K_DEL:
1005 case K_KDEL:
1006 case Ctrl_W:
1007#ifdef FEAT_FKMAP
1008 if (cmd_fkmap && c == K_BS)
1009 c = K_DEL;
1010#endif
1011 if (c == K_KDEL)
1012 c = K_DEL;
1013
1014 /*
1015 * delete current character is the same as backspace on next
1016 * character, except at end of line
1017 */
1018 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1019 ++ccline.cmdpos;
1020#ifdef FEAT_MBYTE
1021 if (has_mbyte && c == K_DEL)
1022 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1023 ccline.cmdbuff + ccline.cmdpos);
1024#endif
1025 if (ccline.cmdpos > 0)
1026 {
1027 char_u *p;
1028
1029 j = ccline.cmdpos;
1030 p = ccline.cmdbuff + j;
1031#ifdef FEAT_MBYTE
1032 if (has_mbyte)
1033 {
1034 p = mb_prevptr(ccline.cmdbuff, p);
1035 if (c == Ctrl_W)
1036 {
1037 while (p > ccline.cmdbuff && vim_isspace(*p))
1038 p = mb_prevptr(ccline.cmdbuff, p);
1039 i = mb_get_class(p);
1040 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1041 p = mb_prevptr(ccline.cmdbuff, p);
1042 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001043 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045 }
1046 else
1047#endif
1048 if (c == Ctrl_W)
1049 {
1050 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1051 --p;
1052 i = vim_iswordc(p[-1]);
1053 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1054 && vim_iswordc(p[-1]) == i)
1055 --p;
1056 }
1057 else
1058 --p;
1059 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1060 ccline.cmdlen -= j - ccline.cmdpos;
1061 i = ccline.cmdpos;
1062 while (i < ccline.cmdlen)
1063 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1064
1065 /* Truncate at the end, required for multi-byte chars. */
1066 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001067#ifdef FEAT_SEARCH_EXTRA
1068 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001069 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001070 search_start = save_cursor;
1071 /* save view settings, so that the screen
1072 * won't be restored at the wrong position */
1073 old_curswant = init_curswant;
1074 old_leftcol = init_leftcol;
1075 old_topline = init_topline;
1076# ifdef FEAT_DIFF
1077 old_topfill = init_topfill;
1078# endif
1079 old_botline = init_botline;
Bram Moolenaar9d34d902018-04-27 22:18:12 +02001080 old_empty_rows = init_empty_rows;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001081 }
1082#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 redrawcmd();
1084 }
1085 else if (ccline.cmdlen == 0 && c != Ctrl_W
1086 && ccline.cmdprompt == NULL && indent == 0)
1087 {
1088 /* In ex and debug mode it doesn't make sense to return. */
1089 if (exmode_active
1090#ifdef FEAT_EVAL
1091 || ccline.cmdfirstc == '>'
1092#endif
1093 )
1094 goto cmdline_not_changed;
1095
Bram Moolenaard23a8232018-02-10 18:45:26 +01001096 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (!cmd_silent)
1098 {
1099#ifdef FEAT_RIGHTLEFT
1100 if (cmdmsg_rl)
1101 msg_col = Columns;
1102 else
1103#endif
1104 msg_col = 0;
1105 msg_putchar(' '); /* delete ':' */
1106 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001107#ifdef FEAT_SEARCH_EXTRA
1108 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001109 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001110#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 redraw_cmdline = TRUE;
1112 goto returncmd; /* back to cmd mode */
1113 }
1114 goto cmdline_changed;
1115
1116 case K_INS:
1117 case K_KINS:
1118#ifdef FEAT_FKMAP
1119 /* if Farsi mode set, we are in reverse insert mode -
1120 Do not change the mode */
1121 if (cmd_fkmap)
1122 beep_flush();
1123 else
1124#endif
1125 ccline.overstrike = !ccline.overstrike;
1126#ifdef CURSOR_SHAPE
1127 ui_cursor_shape(); /* may show different cursor shape */
1128#endif
1129 goto cmdline_not_changed;
1130
1131 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001132 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 {
1134 /* ":lmap" mappings exists, toggle use of mappings. */
1135 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001136#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 im_set_active(FALSE); /* Disable input method */
1138#endif
1139 if (b_im_ptr != NULL)
1140 {
1141 if (State & LANGMAP)
1142 *b_im_ptr = B_IMODE_LMAP;
1143 else
1144 *b_im_ptr = B_IMODE_NONE;
1145 }
1146 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001147#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 else
1149 {
1150 /* There are no ":lmap" mappings, toggle IM. When
1151 * 'imdisable' is set don't try getting the status, it's
1152 * always off. */
1153 if ((p_imdisable && b_im_ptr != NULL)
1154 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1155 {
1156 im_set_active(FALSE); /* Disable input method */
1157 if (b_im_ptr != NULL)
1158 *b_im_ptr = B_IMODE_NONE;
1159 }
1160 else
1161 {
1162 im_set_active(TRUE); /* Enable input method */
1163 if (b_im_ptr != NULL)
1164 *b_im_ptr = B_IMODE_IM;
1165 }
1166 }
1167#endif
1168 if (b_im_ptr != NULL)
1169 {
1170 if (b_im_ptr == &curbuf->b_p_iminsert)
1171 set_iminsert_global();
1172 else
1173 set_imsearch_global();
1174 }
1175#ifdef CURSOR_SHAPE
1176 ui_cursor_shape(); /* may show different cursor shape */
1177#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001178#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 /* Show/unshow value of 'keymap' in status lines later. */
1180 status_redraw_curbuf();
1181#endif
1182 goto cmdline_not_changed;
1183
1184/* case '@': only in very old vi */
1185 case Ctrl_U:
1186 /* delete all characters left of the cursor */
1187 j = ccline.cmdpos;
1188 ccline.cmdlen -= j;
1189 i = ccline.cmdpos = 0;
1190 while (i < ccline.cmdlen)
1191 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1192 /* Truncate at the end, required for multi-byte chars. */
1193 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001194#ifdef FEAT_SEARCH_EXTRA
1195 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001196 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 redrawcmd();
1199 goto cmdline_changed;
1200
1201#ifdef FEAT_CLIPBOARD
1202 case Ctrl_Y:
1203 /* Copy the modeless selection, if there is one. */
1204 if (clip_star.state != SELECT_CLEARED)
1205 {
1206 if (clip_star.state == SELECT_DONE)
1207 clip_copy_modeless_selection(TRUE);
1208 goto cmdline_not_changed;
1209 }
1210 break;
1211#endif
1212
1213 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1214 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001215 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001216 * ":normal" runs out of characters. */
1217 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001218 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 goto cmdline_not_changed;
1220
1221 gotesc = TRUE; /* will free ccline.cmdbuff after
1222 putting it in history */
1223 goto returncmd; /* back to cmd mode */
1224
1225 case Ctrl_R: /* insert register */
1226#ifdef USE_ON_FLY_SCROLL
1227 dont_scroll = TRUE; /* disallow scrolling here */
1228#endif
1229 putcmdline('"', TRUE);
1230 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001231 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 if (i == Ctrl_O)
1233 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1234 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001235 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001236 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 --no_mapping;
1238#ifdef FEAT_EVAL
1239 /*
1240 * Insert the result of an expression.
1241 * Need to save the current command line, to be able to enter
1242 * a new one...
1243 */
1244 new_cmdpos = -1;
1245 if (c == '=')
1246 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1248 {
1249 beep_flush();
1250 c = ESC;
1251 }
1252 else
1253 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001254 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001256 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258 }
1259#endif
1260 if (c != ESC) /* use ESC to cancel inserting register */
1261 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001262 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001263
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001264#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001265 /* When there was a serious error abort getting the
1266 * command line. */
1267 if (aborting())
1268 {
1269 gotesc = TRUE; /* will free ccline.cmdbuff after
1270 putting it in history */
1271 goto returncmd; /* back to cmd mode */
1272 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001273#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 KeyTyped = FALSE; /* Don't do p_wc completion. */
1275#ifdef FEAT_EVAL
1276 if (new_cmdpos >= 0)
1277 {
1278 /* set_cmdline_pos() was used */
1279 if (new_cmdpos > ccline.cmdlen)
1280 ccline.cmdpos = ccline.cmdlen;
1281 else
1282 ccline.cmdpos = new_cmdpos;
1283 }
1284#endif
1285 }
1286 redrawcmd();
1287 goto cmdline_changed;
1288
1289 case Ctrl_D:
1290 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1291 break; /* Use ^D as normal char instead */
1292
1293 redrawcmd();
1294 continue; /* don't do incremental search now */
1295
1296 case K_RIGHT:
1297 case K_S_RIGHT:
1298 case K_C_RIGHT:
1299 do
1300 {
1301 if (ccline.cmdpos >= ccline.cmdlen)
1302 break;
1303 i = cmdline_charsize(ccline.cmdpos);
1304 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1305 break;
1306 ccline.cmdspos += i;
1307#ifdef FEAT_MBYTE
1308 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001309 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 + ccline.cmdpos);
1311 else
1312#endif
1313 ++ccline.cmdpos;
1314 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001315 while ((c == K_S_RIGHT || c == K_C_RIGHT
1316 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1318#ifdef FEAT_MBYTE
1319 if (has_mbyte)
1320 set_cmdspos_cursor();
1321#endif
1322 goto cmdline_not_changed;
1323
1324 case K_LEFT:
1325 case K_S_LEFT:
1326 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001327 if (ccline.cmdpos == 0)
1328 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 do
1330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 --ccline.cmdpos;
1332#ifdef FEAT_MBYTE
1333 if (has_mbyte) /* move to first byte of char */
1334 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1335 ccline.cmdbuff + ccline.cmdpos);
1336#endif
1337 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1338 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001339 while (ccline.cmdpos > 0
1340 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001341 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1343#ifdef FEAT_MBYTE
1344 if (has_mbyte)
1345 set_cmdspos_cursor();
1346#endif
1347 goto cmdline_not_changed;
1348
1349 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001350 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001351 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaar4770d092006-01-12 23:22:24 +00001353#ifdef FEAT_GUI_W32
1354 /* On Win32 ignore <M-F4>, we get it when closing the window was
1355 * cancelled. */
1356 case K_F4:
1357 if (mod_mask == MOD_MASK_ALT)
1358 {
1359 redrawcmd(); /* somehow the cmdline is cleared */
1360 goto cmdline_not_changed;
1361 }
1362 break;
1363#endif
1364
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365#ifdef FEAT_MOUSE
1366 case K_MIDDLEDRAG:
1367 case K_MIDDLERELEASE:
1368 goto cmdline_not_changed; /* Ignore mouse */
1369
1370 case K_MIDDLEMOUSE:
1371# ifdef FEAT_GUI
1372 /* When GUI is active, also paste when 'mouse' is empty */
1373 if (!gui.in_use)
1374# endif
1375 if (!mouse_has(MOUSE_COMMAND))
1376 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001377# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001379 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001381# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001382 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 redrawcmd();
1384 goto cmdline_changed;
1385
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001386# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001388 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 redrawcmd();
1390 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 case K_LEFTDRAG:
1394 case K_LEFTRELEASE:
1395 case K_RIGHTDRAG:
1396 case K_RIGHTRELEASE:
1397 /* Ignore drag and release events when the button-down wasn't
1398 * seen before. */
1399 if (ignore_drag_release)
1400 goto cmdline_not_changed;
1401 /* FALLTHROUGH */
1402 case K_LEFTMOUSE:
1403 case K_RIGHTMOUSE:
1404 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1405 ignore_drag_release = TRUE;
1406 else
1407 ignore_drag_release = FALSE;
1408# ifdef FEAT_GUI
1409 /* When GUI is active, also move when 'mouse' is empty */
1410 if (!gui.in_use)
1411# endif
1412 if (!mouse_has(MOUSE_COMMAND))
1413 goto cmdline_not_changed; /* Ignore mouse */
1414# ifdef FEAT_CLIPBOARD
1415 if (mouse_row < cmdline_row && clip_star.available)
1416 {
1417 int button, is_click, is_drag;
1418
1419 /*
1420 * Handle modeless selection.
1421 */
1422 button = get_mouse_button(KEY2TERMCAP1(c),
1423 &is_click, &is_drag);
1424 if (mouse_model_popup() && button == MOUSE_LEFT
1425 && (mod_mask & MOD_MASK_SHIFT))
1426 {
1427 /* Translate shift-left to right button. */
1428 button = MOUSE_RIGHT;
1429 mod_mask &= ~MOD_MASK_SHIFT;
1430 }
1431 clip_modeless(button, is_click, is_drag);
1432 goto cmdline_not_changed;
1433 }
1434# endif
1435
1436 set_cmdspos();
1437 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1438 ++ccline.cmdpos)
1439 {
1440 i = cmdline_charsize(ccline.cmdpos);
1441 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1442 && mouse_col < ccline.cmdspos % Columns + i)
1443 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001444# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445 if (has_mbyte)
1446 {
1447 /* Count ">" for double-wide char that doesn't fit. */
1448 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001449 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 + ccline.cmdpos) - 1;
1451 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001452# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 ccline.cmdspos += i;
1454 }
1455 goto cmdline_not_changed;
1456
1457 /* Mouse scroll wheel: ignored here */
1458 case K_MOUSEDOWN:
1459 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001460 case K_MOUSELEFT:
1461 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 /* Alternate buttons ignored here */
1463 case K_X1MOUSE:
1464 case K_X1DRAG:
1465 case K_X1RELEASE:
1466 case K_X2MOUSE:
1467 case K_X2DRAG:
1468 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001469 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 goto cmdline_not_changed;
1471
1472#endif /* FEAT_MOUSE */
1473
1474#ifdef FEAT_GUI
1475 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1476 case K_LEFTRELEASE_NM:
1477 goto cmdline_not_changed;
1478
1479 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001480 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 {
1482 gui_do_scroll();
1483 redrawcmd();
1484 }
1485 goto cmdline_not_changed;
1486
1487 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001488 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001490 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 redrawcmd();
1492 }
1493 goto cmdline_not_changed;
1494#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001495#ifdef FEAT_GUI_TABLINE
1496 case K_TABLINE:
1497 case K_TABMENU:
1498 /* Don't want to change any tabs here. Make sure the same tab
1499 * is still selected. */
1500 if (gui_use_tabline())
1501 gui_mch_set_curtab(tabpage_index(curtab));
1502 goto cmdline_not_changed;
1503#endif
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 case K_SELECT: /* end of Select mode mapping - ignore */
1506 goto cmdline_not_changed;
1507
1508 case Ctrl_B: /* begin of command line */
1509 case K_HOME:
1510 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 case K_S_HOME:
1512 case K_C_HOME:
1513 ccline.cmdpos = 0;
1514 set_cmdspos();
1515 goto cmdline_not_changed;
1516
1517 case Ctrl_E: /* end of command line */
1518 case K_END:
1519 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520 case K_S_END:
1521 case K_C_END:
1522 ccline.cmdpos = ccline.cmdlen;
1523 set_cmdspos_cursor();
1524 goto cmdline_not_changed;
1525
1526 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001527 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 break;
1529 goto cmdline_changed;
1530
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001531 case Ctrl_L:
1532#ifdef FEAT_SEARCH_EXTRA
1533 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1534 {
1535 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001536 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001537 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001538 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001539 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001540 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001541 c = gchar_cursor();
1542 /* If 'ignorecase' and 'smartcase' are set and the
1543 * command line has no uppercase characters, convert
1544 * the character to lowercase */
1545 if (p_ic && p_scs
1546 && !pat_has_uppercase(ccline.cmdbuff))
1547 c = MB_TOLOWER(c);
1548 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001549 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001550 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001551 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001552 != NULL)
1553 {
1554 /* put a backslash before special
1555 * characters */
1556 stuffcharReadbuff(c);
1557 c = '\\';
1558 }
1559 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001560 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001561 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001562 }
1563 goto cmdline_not_changed;
1564 }
1565#endif
1566
1567 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001568 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 break;
1570 goto cmdline_changed;
1571
1572 case Ctrl_N: /* next match */
1573 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001574 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001576 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1577 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001579 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001582 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 case K_UP:
1584 case K_DOWN:
1585 case K_S_UP:
1586 case K_S_DOWN:
1587 case K_PAGEUP:
1588 case K_KPAGEUP:
1589 case K_PAGEDOWN:
1590 case K_KPAGEDOWN:
1591 if (hislen == 0 || firstc == NUL) /* no history */
1592 goto cmdline_not_changed;
1593
1594 i = hiscnt;
1595
1596 /* save current command string so it can be restored later */
1597 if (lookfor == NULL)
1598 {
1599 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1600 goto cmdline_not_changed;
1601 lookfor[ccline.cmdpos] = NUL;
1602 }
1603
1604 j = (int)STRLEN(lookfor);
1605 for (;;)
1606 {
1607 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001608 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001609 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 {
1611 if (hiscnt == hislen) /* first time */
1612 hiscnt = hisidx[histype];
1613 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1614 hiscnt = hislen - 1;
1615 else if (hiscnt != hisidx[histype] + 1)
1616 --hiscnt;
1617 else /* at top of list */
1618 {
1619 hiscnt = i;
1620 break;
1621 }
1622 }
1623 else /* one step forwards */
1624 {
1625 /* on last entry, clear the line */
1626 if (hiscnt == hisidx[histype])
1627 {
1628 hiscnt = hislen;
1629 break;
1630 }
1631
1632 /* not on a history line, nothing to do */
1633 if (hiscnt == hislen)
1634 break;
1635 if (hiscnt == hislen - 1) /* wrap around */
1636 hiscnt = 0;
1637 else
1638 ++hiscnt;
1639 }
1640 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1641 {
1642 hiscnt = i;
1643 break;
1644 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001645 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001646 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 || STRNCMP(history[histype][hiscnt].hisstr,
1648 lookfor, (size_t)j) == 0)
1649 break;
1650 }
1651
1652 if (hiscnt != i) /* jumped to other entry */
1653 {
1654 char_u *p;
1655 int len;
1656 int old_firstc;
1657
1658 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001659 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (hiscnt == hislen)
1661 p = lookfor; /* back to the old one */
1662 else
1663 p = history[histype][hiscnt].hisstr;
1664
1665 if (histype == HIST_SEARCH
1666 && p != lookfor
1667 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1668 {
1669 /* Correct for the separator character used when
1670 * adding the history entry vs the one used now.
1671 * First loop: count length.
1672 * Second loop: copy the characters. */
1673 for (i = 0; i <= 1; ++i)
1674 {
1675 len = 0;
1676 for (j = 0; p[j] != NUL; ++j)
1677 {
1678 /* Replace old sep with new sep, unless it is
1679 * escaped. */
1680 if (p[j] == old_firstc
1681 && (j == 0 || p[j - 1] != '\\'))
1682 {
1683 if (i > 0)
1684 ccline.cmdbuff[len] = firstc;
1685 }
1686 else
1687 {
1688 /* Escape new sep, unless it is already
1689 * escaped. */
1690 if (p[j] == firstc
1691 && (j == 0 || p[j - 1] != '\\'))
1692 {
1693 if (i > 0)
1694 ccline.cmdbuff[len] = '\\';
1695 ++len;
1696 }
1697 if (i > 0)
1698 ccline.cmdbuff[len] = p[j];
1699 }
1700 ++len;
1701 }
1702 if (i == 0)
1703 {
1704 alloc_cmdbuff(len);
1705 if (ccline.cmdbuff == NULL)
1706 goto returncmd;
1707 }
1708 }
1709 ccline.cmdbuff[len] = NUL;
1710 }
1711 else
1712 {
1713 alloc_cmdbuff((int)STRLEN(p));
1714 if (ccline.cmdbuff == NULL)
1715 goto returncmd;
1716 STRCPY(ccline.cmdbuff, p);
1717 }
1718
1719 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1720 redrawcmd();
1721 goto cmdline_changed;
1722 }
1723 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001724#endif
1725 goto cmdline_not_changed;
1726
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001727#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001728 case Ctrl_G: /* next match */
1729 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001730 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1731 {
1732 pos_T t;
Bram Moolenaard0480092017-11-16 22:20:39 +01001733 char_u *pat;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001734 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001735
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001736 if (ccline.cmdlen == 0)
1737 goto cmdline_not_changed;
1738
Bram Moolenaard0480092017-11-16 22:20:39 +01001739 if (firstc == ccline.cmdbuff[0])
1740 pat = last_search_pattern();
1741 else
1742 pat = ccline.cmdbuff;
1743
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001744 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001745 cursor_off();
1746 out_flush();
1747 if (c == Ctrl_G)
1748 {
1749 t = match_end;
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001750 if (LT_POS(match_start, match_end))
1751 /* start searching at the end of the match
1752 * not at the beginning of the next column */
1753 (void)decl(&t);
Bram Moolenaar11956692016-08-27 16:26:56 +02001754 search_flags += SEARCH_COL;
1755 }
1756 else
1757 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001758 if (!p_hls)
1759 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001760 ++emsg_off;
1761 i = searchit(curwin, curbuf, &t,
1762 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaard0480092017-11-16 22:20:39 +01001763 pat, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001764 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001765 --emsg_off;
1766 if (i)
1767 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001768 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001769 match_end = t;
1770 match_start = t;
1771 if (c == Ctrl_T && firstc == '/')
1772 {
1773 /* move just before the current match, so that
1774 * when nv_search finishes the cursor will be
1775 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001776 search_start = t;
1777 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001778 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001779 else if (c == Ctrl_G && firstc == '?')
1780 {
1781 /* move just after the current match, so that
1782 * when nv_search finishes the cursor will be
1783 * put back on the match */
1784 search_start = t;
1785 (void)incl(&search_start);
1786 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001787 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001788 {
1789 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001790 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001791 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001792 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001793 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001794 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001795 }
1796
1797 set_search_match(&match_end);
1798 curwin->w_cursor = match_start;
1799 changed_cline_bef_curs();
1800 update_topline();
1801 validate_cursor();
1802 highlight_match = TRUE;
1803 old_curswant = curwin->w_curswant;
1804 old_leftcol = curwin->w_leftcol;
1805 old_topline = curwin->w_topline;
1806# ifdef FEAT_DIFF
1807 old_topfill = curwin->w_topfill;
1808# endif
1809 old_botline = curwin->w_botline;
Bram Moolenaar9d34d902018-04-27 22:18:12 +02001810 old_empty_rows = curwin->w_empty_rows;
Bram Moolenaar11956692016-08-27 16:26:56 +02001811 update_screen(NOT_VALID);
1812 redrawcmdline();
1813 }
1814 else
1815 vim_beep(BO_ERROR);
Bram Moolenaara1d5c152017-12-16 19:05:22 +01001816 restore_last_search_pattern();
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001817 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001818 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001819 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820#endif
1821
1822 case Ctrl_V:
1823 case Ctrl_Q:
1824#ifdef FEAT_MOUSE
1825 ignore_drag_release = TRUE;
1826#endif
1827 putcmdline('^', TRUE);
1828 c = get_literal(); /* get next (two) character(s) */
1829 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001830 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#ifdef FEAT_MBYTE
1832 /* may need to remove ^ when composing char was typed */
1833 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1834 {
1835 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1836 msg_putchar(' ');
1837 cursorcmd();
1838 }
1839#endif
1840 break;
1841
1842#ifdef FEAT_DIGRAPHS
1843 case Ctrl_K:
1844#ifdef FEAT_MOUSE
1845 ignore_drag_release = TRUE;
1846#endif
1847 putcmdline('?', TRUE);
1848#ifdef USE_ON_FLY_SCROLL
1849 dont_scroll = TRUE; /* disallow scrolling here */
1850#endif
1851 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001852 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (c != NUL)
1854 break;
1855
1856 redrawcmd();
1857 goto cmdline_not_changed;
1858#endif /* FEAT_DIGRAPHS */
1859
1860#ifdef FEAT_RIGHTLEFT
1861 case Ctrl__: /* CTRL-_: switch language mode */
1862 if (!p_ari)
1863 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001864# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 if (p_altkeymap)
1866 {
1867 cmd_fkmap = !cmd_fkmap;
1868 if (cmd_fkmap) /* in Farsi always in Insert mode */
1869 ccline.overstrike = FALSE;
1870 }
1871 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 cmd_hkmap = !cmd_hkmap;
1874 goto cmdline_not_changed;
1875#endif
1876
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001877 case K_PS:
1878 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1879 goto cmdline_changed;
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 default:
1882#ifdef UNIX
1883 if (c == intr_char)
1884 {
1885 gotesc = TRUE; /* will free ccline.cmdbuff after
1886 putting it in history */
1887 goto returncmd; /* back to Normal mode */
1888 }
1889#endif
1890 /*
1891 * Normal character with no special meaning. Just set mod_mask
1892 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1893 * the string <S-Space>. This should only happen after ^V.
1894 */
1895 if (!IS_SPECIAL(c))
1896 mod_mask = 0x0;
1897 break;
1898 }
1899 /*
1900 * End of switch on command line character.
1901 * We come here if we have a normal character.
1902 */
1903
Bram Moolenaarede3e632013-06-23 16:16:19 +02001904 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#ifdef FEAT_MBYTE
1906 /* Add ABBR_OFF for characters above 0x100, this is
1907 * what check_abbr() expects. */
1908 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1909#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001910 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 goto cmdline_changed;
1912
1913 /*
1914 * put the character in the command line
1915 */
1916 if (IS_SPECIAL(c) || mod_mask != 0)
1917 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1918 else
1919 {
1920#ifdef FEAT_MBYTE
1921 if (has_mbyte)
1922 {
1923 j = (*mb_char2bytes)(c, IObuff);
1924 IObuff[j] = NUL; /* exclude composing chars */
1925 put_on_cmdline(IObuff, j, TRUE);
1926 }
1927 else
1928#endif
1929 {
1930 IObuff[0] = c;
1931 put_on_cmdline(IObuff, 1, TRUE);
1932 }
1933 }
1934 goto cmdline_changed;
1935
1936/*
1937 * This part implements incremental searches for "/" and "?"
1938 * Jump to cmdline_not_changed when a character has been read but the command
1939 * line did not change. Then we only search and redraw if something changed in
1940 * the past.
1941 * Jump to cmdline_changed when the command line did change.
1942 * (Sorry for the goto's, I know it is ugly).
1943 */
1944cmdline_not_changed:
1945#ifdef FEAT_SEARCH_EXTRA
1946 if (!incsearch_postponed)
1947 continue;
1948#endif
1949
1950cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01001951 /* Trigger CmdlineChanged autocommands. */
1952 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01001953
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#ifdef FEAT_SEARCH_EXTRA
1955 /*
1956 * 'incsearch' highlighting.
1957 */
1958 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1959 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001960 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001961#ifdef FEAT_RELTIME
1962 proftime_T tm;
1963#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 /* if there is a character waiting, search and redraw later */
1966 if (char_avail())
1967 {
1968 incsearch_postponed = TRUE;
1969 continue;
1970 }
1971 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001972 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001973 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974
1975 /* If there is no command line, don't do anything */
1976 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001977 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 i = 0;
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02001979 set_no_hlsearch(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001980 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 else
1983 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001984 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 cursor_off(); /* so the user knows we're busy */
1986 out_flush();
1987 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001988#ifdef FEAT_RELTIME
1989 /* Set the time limit to half a second. */
1990 profile_setlimit(500L, &tm);
1991#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001992 if (!p_hls)
1993 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001995 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001996#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001997 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001998#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001999 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00002000#endif
2001 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 --emsg_off;
2003 /* if interrupted while searching, behave like it failed */
2004 if (got_int)
2005 {
2006 (void)vpeekc(); /* remove <C-C> from input stream */
2007 got_int = FALSE; /* don't abandon the command line */
2008 i = 0;
2009 }
2010 else if (char_avail())
2011 /* cancelled searching because a char was typed */
2012 incsearch_postponed = TRUE;
2013 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002014 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 highlight_match = TRUE; /* highlight position */
2016 else
2017 highlight_match = FALSE; /* remove highlight */
2018
2019 /* first restore the old curwin values, so the screen is
2020 * positioned in the same way as the actual search command */
2021 curwin->w_leftcol = old_leftcol;
2022 curwin->w_topline = old_topline;
2023# ifdef FEAT_DIFF
2024 curwin->w_topfill = old_topfill;
2025# endif
2026 curwin->w_botline = old_botline;
Bram Moolenaar9d34d902018-04-27 22:18:12 +02002027 curwin->w_empty_rows = old_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 changed_cline_bef_curs();
2029 update_topline();
2030
2031 if (i != 0)
2032 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002033 pos_T save_pos = curwin->w_cursor;
2034
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002035 match_start = curwin->w_cursor;
2036 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002038 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002039 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002040 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002042 else
2043 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002044
Bram Moolenaar66216052017-12-16 16:33:44 +01002045 /* Disable 'hlsearch' highlighting if the pattern matches
2046 * everything. Avoids a flash when typing "foo\|". */
2047 if (empty_pattern(ccline.cmdbuff))
Bram Moolenaar451fc7b2018-04-27 22:53:07 +02002048 set_no_hlsearch(TRUE);
Bram Moolenaar66216052017-12-16 16:33:44 +01002049
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002051 /* May redraw the status line to show the cursor position. */
2052 if (p_ru && curwin->w_status_height > 0)
2053 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002055 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002056 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002057 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002058 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002059
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002060 /* Leave it at the end to make CTRL-R CTRL-W work. */
2061 if (i != 0)
2062 curwin->w_cursor = end_pos;
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 msg_starthere();
2065 redrawcmdline();
2066 did_incsearch = TRUE;
2067 }
2068#else /* FEAT_SEARCH_EXTRA */
2069 ;
2070#endif
2071
2072#ifdef FEAT_RIGHTLEFT
2073 if (cmdmsg_rl
2074# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002075 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076# endif
2077 )
2078 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002079 * right-left typing. Not efficient, but it works.
2080 * Do it only when there are no characters left to read
2081 * to avoid useless intermediate redraws. */
2082 if (vpeekc() == NUL)
2083 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084#endif
2085 }
2086
2087returncmd:
2088
2089#ifdef FEAT_RIGHTLEFT
2090 cmdmsg_rl = FALSE;
2091#endif
2092
2093#ifdef FEAT_FKMAP
2094 cmd_fkmap = 0;
2095#endif
2096
2097 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002098 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099
2100#ifdef FEAT_SEARCH_EXTRA
2101 if (did_incsearch)
2102 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002103 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002104 curwin->w_cursor = save_cursor;
2105 else
2106 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002107 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002108 {
2109 /* put the '" mark at the original position */
2110 curwin->w_cursor = save_cursor;
2111 setpcmark();
2112 }
2113 curwin->w_cursor = search_start;
2114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 curwin->w_curswant = old_curswant;
2116 curwin->w_leftcol = old_leftcol;
2117 curwin->w_topline = old_topline;
2118# ifdef FEAT_DIFF
2119 curwin->w_topfill = old_topfill;
2120# endif
2121 curwin->w_botline = old_botline;
Bram Moolenaar9d34d902018-04-27 22:18:12 +02002122 curwin->w_empty_rows = old_empty_rows;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 highlight_match = FALSE;
2124 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002125 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 }
2127#endif
2128
2129 if (ccline.cmdbuff != NULL)
2130 {
2131 /*
2132 * Put line in history buffer (":" and "=" only when it was typed).
2133 */
2134#ifdef FEAT_CMDHIST
2135 if (ccline.cmdlen && firstc != NUL
2136 && (some_key_typed || histype == HIST_SEARCH))
2137 {
2138 add_to_history(histype, ccline.cmdbuff, TRUE,
2139 histype == HIST_SEARCH ? firstc : NUL);
2140 if (firstc == ':')
2141 {
2142 vim_free(new_last_cmdline);
2143 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2144 }
2145 }
2146#endif
2147
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002148 if (gotesc)
2149 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 }
2151
2152 /*
2153 * If the screen was shifted up, redraw the whole screen (later).
2154 * If the line is too long, clear it, so ruler and shown command do
2155 * not get printed in the middle of it.
2156 */
2157 msg_check();
2158 msg_scroll = save_msg_scroll;
2159 redir_off = FALSE;
2160
2161 /* When the command line was typed, no need for a wait-return prompt. */
2162 if (some_key_typed)
2163 need_wait_return = FALSE;
2164
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002165 /* Trigger CmdlineLeave autocommands. */
2166 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002169#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2171 im_save_status(b_im_ptr);
2172 im_set_active(FALSE);
2173#endif
2174#ifdef FEAT_MOUSE
2175 setmouse();
2176#endif
2177#ifdef CURSOR_SHAPE
2178 ui_cursor_shape(); /* may show different cursor shape */
2179#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002180 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002182 {
2183 char_u *p = ccline.cmdbuff;
2184
2185 /* Make ccline empty, getcmdline() may try to use it. */
2186 ccline.cmdbuff = NULL;
2187 return p;
2188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189}
2190
2191#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2192/*
2193 * Get a command line with a prompt.
2194 * This is prepared to be called recursively from getcmdline() (e.g. by
2195 * f_input() when evaluating an expression from CTRL-R =).
2196 * Returns the command line in allocated memory, or NULL.
2197 */
2198 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002199getcmdline_prompt(
2200 int firstc,
2201 char_u *prompt, /* command line prompt */
2202 int attr, /* attributes for prompt */
2203 int xp_context, /* type of expansion */
2204 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205{
2206 char_u *s;
2207 struct cmdline_info save_ccline;
2208 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002209 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002211 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 ccline.cmdprompt = prompt;
2213 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002214# ifdef FEAT_EVAL
2215 ccline.xp_context = xp_context;
2216 ccline.xp_arg = xp_arg;
2217 ccline.input_fn = (firstc == '@');
2218# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002219 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002221 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002222 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002223 /* Restore msg_col, the prompt from input() may have changed it.
2224 * But only if called recursively and the commandline is therefore being
2225 * restored to an old one; if not, the input() prompt stays on the screen,
2226 * so we need its modified msg_col left intact. */
2227 if (ccline.cmdbuff != NULL)
2228 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 return s;
2231}
2232#endif
2233
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002234/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002235 * Return TRUE when the text must not be changed and we can't switch to
2236 * another window or buffer. Used when editing the command line, evaluating
2237 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002238 */
2239 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002240text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002241{
2242#ifdef FEAT_CMDWIN
2243 if (cmdwin_type != 0)
2244 return TRUE;
2245#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002246 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002247}
2248
2249/*
2250 * Give an error message for a command that isn't allowed while the cmdline
2251 * window is open or editing the cmdline in another way.
2252 */
2253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002254text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002255{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002256 EMSG(_(get_text_locked_msg()));
2257}
2258
2259 char_u *
2260get_text_locked_msg(void)
2261{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002262#ifdef FEAT_CMDWIN
2263 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002264 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002265#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002266 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002267}
2268
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002270 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2271 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002272 */
2273 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002274curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002275{
2276 if (curbuf_lock > 0)
2277 {
2278 EMSG(_("E788: Not allowed to edit another buffer now"));
2279 return TRUE;
2280 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002281 return allbuf_locked();
2282}
2283
2284/*
2285 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2286 * message.
2287 */
2288 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002289allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002290{
2291 if (allbuf_lock > 0)
2292 {
2293 EMSG(_("E811: Not allowed to change buffer information now"));
2294 return TRUE;
2295 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002296 return FALSE;
2297}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002298
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002300cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2303 if (cmdline_star > 0) /* showing '*', always 1 position */
2304 return 1;
2305#endif
2306 return ptr2cells(ccline.cmdbuff + idx);
2307}
2308
2309/*
2310 * Compute the offset of the cursor on the command line for the prompt and
2311 * indent.
2312 */
2313 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002314set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002316 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 ccline.cmdspos = 1 + ccline.cmdindent;
2318 else
2319 ccline.cmdspos = 0 + ccline.cmdindent;
2320}
2321
2322/*
2323 * Compute the screen position for the cursor on the command line.
2324 */
2325 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002326set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
2328 int i, m, c;
2329
2330 set_cmdspos();
2331 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002332 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002334 if (m < 0) /* overflow, Columns or Rows at weird value */
2335 m = MAXCOL;
2336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 else
2338 m = MAXCOL;
2339 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2340 {
2341 c = cmdline_charsize(i);
2342#ifdef FEAT_MBYTE
2343 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2344 if (has_mbyte)
2345 correct_cmdspos(i, c);
2346#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002347 /* If the cmdline doesn't fit, show cursor on last visible char.
2348 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if ((ccline.cmdspos += c) >= m)
2350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 ccline.cmdspos -= c;
2352 break;
2353 }
2354#ifdef FEAT_MBYTE
2355 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002356 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357#endif
2358 }
2359}
2360
2361#ifdef FEAT_MBYTE
2362/*
2363 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2364 * character that doesn't fit, so that a ">" must be displayed.
2365 */
2366 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002367correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002369 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2371 && ccline.cmdspos % Columns + cells > Columns)
2372 ccline.cmdspos++;
2373}
2374#endif
2375
2376/*
2377 * Get an Ex command line for the ":" command.
2378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002380getexline(
2381 int c, /* normally ':', NUL for ":append" */
2382 void *cookie UNUSED,
2383 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385 /* When executing a register, remove ':' that's in front of each line. */
2386 if (exec_from_reg && vpeekc() == ':')
2387 (void)vgetc();
2388 return getcmdline(c, 1L, indent);
2389}
2390
2391/*
2392 * Get an Ex command line for Ex mode.
2393 * In Ex mode we only use the OS supplied line editing features and no
2394 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002395 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002398getexmodeline(
2399 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002400 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002401 void *cookie UNUSED,
2402 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002404 garray_T line_ga;
2405 char_u *pend;
2406 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002407 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002408 int escaped = FALSE; /* CTRL-V typed */
2409 int vcol = 0;
2410 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002411 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002412 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413
2414 /* Switch cursor on now. This avoids that it happens after the "\n", which
2415 * confuses the system function that computes tabstops. */
2416 cursor_on();
2417
2418 /* always start in column 0; write a newline if necessary */
2419 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002420 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002422 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002424 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002425 if (p_prompt)
2426 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 while (indent-- > 0)
2428 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430 }
2431
2432 ga_init2(&line_ga, 1, 30);
2433
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002434 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002435 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002436 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002437 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002438 while (indent >= 8)
2439 {
2440 ga_append(&line_ga, TAB);
2441 msg_puts((char_u *)" ");
2442 indent -= 8;
2443 }
2444 while (indent-- > 0)
2445 {
2446 ga_append(&line_ga, ' ');
2447 msg_putchar(' ');
2448 }
2449 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002450 ++no_mapping;
2451 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /*
2454 * Get the line, one character at a time.
2455 */
2456 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002457 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002459 long sw;
2460 char_u *s;
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (ga_grow(&line_ga, 40) == FAIL)
2463 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464
Bram Moolenaarba748c82017-02-26 14:00:07 +01002465 /*
2466 * Get one character at a time.
2467 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002468 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002469
2470 /* Check for a ":normal" command and no more characters left. */
2471 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2472 c1 = '\n';
2473 else
2474 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
2476 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002477 * Handle line editing.
2478 * Previously this was left to the system, putting the terminal in
2479 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002481 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002483 msg_putchar('\n');
2484 break;
2485 }
2486
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002487 if (c1 == K_PS)
2488 {
2489 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2490 goto redraw;
2491 }
2492
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002493 if (!escaped)
2494 {
2495 /* CR typed means "enter", which is NL */
2496 if (c1 == '\r')
2497 c1 = '\n';
2498
2499 if (c1 == BS || c1 == K_BS
2500 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002502 if (line_ga.ga_len > 0)
2503 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002504#ifdef FEAT_MBYTE
2505 if (has_mbyte)
2506 {
2507 p = (char_u *)line_ga.ga_data;
2508 p[line_ga.ga_len] = NUL;
2509 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2510 line_ga.ga_len -= len;
2511 }
2512 else
2513#endif
2514 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002515 goto redraw;
2516 }
2517 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002522 msg_col = startcol;
2523 msg_clr_eos();
2524 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002525 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002526 }
2527
2528 if (c1 == Ctrl_T)
2529 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002530 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002531 p = (char_u *)line_ga.ga_data;
2532 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002533 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002534 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002535add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002536 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002538 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002539 p = (char_u *)line_ga.ga_data;
2540 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002541 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2542 *s = ' ';
2543 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002545redraw:
2546 /* redraw the line */
2547 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002548 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002549 p = (char_u *)line_ga.ga_data;
2550 p[line_ga.ga_len] = NUL;
2551 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002553 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002557 msg_putchar(' ');
2558 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002559 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002561 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002563 len = MB_PTR2LEN(p);
2564 msg_outtrans_len(p, len);
2565 vcol += ptr2cells(p);
2566 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 }
2568 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002569 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002570 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002571 continue;
2572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002574 if (c1 == Ctrl_D)
2575 {
2576 /* Delete one shiftwidth. */
2577 p = (char_u *)line_ga.ga_data;
2578 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002580 if (prev_char == '^')
2581 ex_keep_indent = TRUE;
2582 indent = 0;
2583 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 }
2585 else
2586 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002587 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002588 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002589 if (indent > 0)
2590 {
2591 --indent;
2592 indent -= indent % get_sw_value(curbuf);
2593 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002595 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002596 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002597 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2599 --line_ga.ga_len;
2600 }
2601 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002603
2604 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2605 {
2606 escaped = TRUE;
2607 continue;
2608 }
2609
2610 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2611 if (IS_SPECIAL(c1))
2612 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002614
2615 if (IS_SPECIAL(c1))
2616 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 len = (*mb_char2bytes)(c1,
2620 (char_u *)line_ga.ga_data + line_ga.ga_len);
2621 else
2622#endif
2623 {
2624 len = 1;
2625 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2626 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002627 if (c1 == '\n')
2628 msg_putchar('\n');
2629 else if (c1 == TAB)
2630 {
2631 /* Don't use chartabsize(), 'ts' can be different */
2632 do
2633 {
2634 msg_putchar(' ');
2635 } while (++vcol % 8);
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002639 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002640 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002641 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002643 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002644 escaped = FALSE;
2645
2646 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002647 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002648
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002649 /* We are done when a NL is entered, but not when it comes after an
2650 * odd number of backslashes, that results in a NUL. */
2651 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002653 int bcount = 0;
2654
2655 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2656 ++bcount;
2657
2658 if (bcount > 0)
2659 {
2660 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2661 * "\NL", etc. */
2662 line_ga.ga_len -= (bcount + 1) / 2;
2663 pend -= (bcount + 1) / 2;
2664 pend[-1] = '\n';
2665 }
2666
2667 if ((bcount & 1) == 0)
2668 {
2669 --line_ga.ga_len;
2670 --pend;
2671 *pend = NUL;
2672 break;
2673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 }
2675 }
2676
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 --no_mapping;
2678 --allow_keys;
2679
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 /* make following messages go to the next line */
2681 msg_didout = FALSE;
2682 msg_col = 0;
2683 if (msg_row < Rows - 1)
2684 ++msg_row;
2685 emsg_on_display = FALSE; /* don't want ui_delay() */
2686
2687 if (got_int)
2688 ga_clear(&line_ga);
2689
2690 return (char_u *)line_ga.ga_data;
2691}
2692
Bram Moolenaare344bea2005-09-01 20:46:49 +00002693# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2694 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695/*
2696 * Return TRUE if ccline.overstrike is on.
2697 */
2698 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002699cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700{
2701 return ccline.overstrike;
2702}
2703
2704/*
2705 * Return TRUE if the cursor is at the end of the cmdline.
2706 */
2707 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002708cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709{
2710 return (ccline.cmdpos >= ccline.cmdlen);
2711}
2712#endif
2713
Bram Moolenaar9372a112005-12-06 19:59:18 +00002714#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715/*
2716 * Return the virtual column number at the current cursor position.
2717 * This is used by the IM code to obtain the start of the preedit string.
2718 */
2719 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002720cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2723 return MAXCOL;
2724
2725# ifdef FEAT_MBYTE
2726 if (has_mbyte)
2727 {
2728 colnr_T col;
2729 int i = 0;
2730
2731 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002732 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733
2734 return col;
2735 }
2736 else
2737# endif
2738 return ccline.cmdpos;
2739}
2740#endif
2741
2742#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2743/*
2744 * If part of the command line is an IM preedit string, redraw it with
2745 * IM feedback attributes. The cursor position is restored after drawing.
2746 */
2747 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002748redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
2750 if ((State & CMDLINE)
2751 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002752 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 && !p_imdisable
2754 && im_is_preediting())
2755 {
2756 int cmdpos = 0;
2757 int cmdspos;
2758 int old_row;
2759 int old_col;
2760 colnr_T col;
2761
2762 old_row = msg_row;
2763 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002764 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
2766# ifdef FEAT_MBYTE
2767 if (has_mbyte)
2768 {
2769 for (col = 0; col < preedit_start_col
2770 && cmdpos < ccline.cmdlen; ++col)
2771 {
2772 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002773 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 }
2776 else
2777# endif
2778 {
2779 cmdspos += preedit_start_col;
2780 cmdpos += preedit_start_col;
2781 }
2782
2783 msg_row = cmdline_row + (cmdspos / (int)Columns);
2784 msg_col = cmdspos % (int)Columns;
2785 if (msg_row >= Rows)
2786 msg_row = Rows - 1;
2787
2788 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2789 {
2790 int char_len;
2791 int char_attr;
2792
2793 char_attr = im_get_feedback_attr(col);
2794 if (char_attr < 0)
2795 break; /* end of preedit string */
2796
2797# ifdef FEAT_MBYTE
2798 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002799 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 else
2801# endif
2802 char_len = 1;
2803
2804 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2805 cmdpos += char_len;
2806 }
2807
2808 msg_row = old_row;
2809 msg_col = old_col;
2810 }
2811}
2812#endif /* FEAT_XIM && FEAT_GUI_GTK */
2813
2814/*
2815 * Allocate a new command line buffer.
2816 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2817 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2818 */
2819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002820alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
2822 /*
2823 * give some extra space to avoid having to allocate all the time
2824 */
2825 if (len < 80)
2826 len = 100;
2827 else
2828 len += 20;
2829
2830 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2831 ccline.cmdbufflen = len;
2832}
2833
2834/*
2835 * Re-allocate the command line to length len + something extra.
2836 * return FAIL for failure, OK otherwise
2837 */
2838 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002839realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 char_u *p;
2842
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002843 if (len < ccline.cmdbufflen)
2844 return OK; /* no need to resize */
2845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 p = ccline.cmdbuff;
2847 alloc_cmdbuff(len); /* will get some more */
2848 if (ccline.cmdbuff == NULL) /* out of memory */
2849 {
2850 ccline.cmdbuff = p; /* keep the old one */
2851 return FAIL;
2852 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002853 /* There isn't always a NUL after the command, but it may need to be
2854 * there, thus copy up to the NUL and add a NUL. */
2855 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2856 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002858
2859 if (ccline.xpc != NULL
2860 && ccline.xpc->xp_pattern != NULL
2861 && ccline.xpc->xp_context != EXPAND_NOTHING
2862 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2863 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002864 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002865
2866 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2867 * to point into the newly allocated memory. */
2868 if (i >= 0 && i <= ccline.cmdlen)
2869 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2870 }
2871
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 return OK;
2873}
2874
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002875#if defined(FEAT_ARABIC) || defined(PROTO)
2876static char_u *arshape_buf = NULL;
2877
2878# if defined(EXITFREE) || defined(PROTO)
2879 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002880free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002881{
2882 vim_free(arshape_buf);
2883}
2884# endif
2885#endif
2886
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887/*
2888 * Draw part of the cmdline at the current cursor position. But draw stars
2889 * when cmdline_star is TRUE.
2890 */
2891 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002892draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893{
2894#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2895 int i;
2896
2897 if (cmdline_star > 0)
2898 for (i = 0; i < len; ++i)
2899 {
2900 msg_putchar('*');
2901# ifdef FEAT_MBYTE
2902 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002903 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904# endif
2905 }
2906 else
2907#endif
2908#ifdef FEAT_ARABIC
2909 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2910 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 static int buflen = 0;
2912 char_u *p;
2913 int j;
2914 int newlen = 0;
2915 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002916 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 int prev_c = 0;
2918 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002919 int u8c;
2920 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922
2923 /*
2924 * Do arabic shaping into a temporary buffer. This is very
2925 * inefficient!
2926 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002927 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 {
2929 /* Re-allocate the buffer. We keep it around to avoid a lot of
2930 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002931 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002932 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002933 arshape_buf = alloc(buflen);
2934 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 return; /* out of memory */
2936 }
2937
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002938 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2939 {
2940 /* Prepend a space to draw the leading composing char on. */
2941 arshape_buf[0] = ' ';
2942 newlen = 1;
2943 }
2944
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 for (j = start; j < start + len; j += mb_l)
2946 {
2947 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002948 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002949 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 if (ARABIC_CHAR(u8c))
2951 {
2952 /* Do Arabic shaping. */
2953 if (cmdmsg_rl)
2954 {
2955 /* displaying from right to left */
2956 pc = prev_c;
2957 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002958 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (j + mb_l >= start + len)
2960 nc = NUL;
2961 else
2962 nc = utf_ptr2char(p + mb_l);
2963 }
2964 else
2965 {
2966 /* displaying from left to right */
2967 if (j + mb_l >= start + len)
2968 pc = NUL;
2969 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002970 {
2971 int pcc[MAX_MCO];
2972
2973 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002975 pc1 = pcc[0];
2976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 nc = prev_c;
2978 }
2979 prev_c = u8c;
2980
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002981 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002983 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002984 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002986 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2987 if (u8cc[1] != 0)
2988 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002989 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 }
2991 }
2992 else
2993 {
2994 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002995 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 newlen += mb_l;
2997 }
2998 }
2999
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003000 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 }
3002 else
3003#endif
3004 msg_outtrans_len(ccline.cmdbuff + start, len);
3005}
3006
3007/*
3008 * Put a character on the command line. Shifts the following text to the
3009 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3010 * "c" must be printable (fit in one display cell)!
3011 */
3012 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 if (cmd_silent)
3016 return;
3017 msg_no_more = TRUE;
3018 msg_putchar(c);
3019 if (shift)
3020 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3021 msg_no_more = FALSE;
3022 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003023 extra_char = c;
3024 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026
3027/*
3028 * Undo a putcmdline(c, FALSE).
3029 */
3030 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003031unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
3033 if (cmd_silent)
3034 return;
3035 msg_no_more = TRUE;
3036 if (ccline.cmdlen == ccline.cmdpos)
3037 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003038#ifdef FEAT_MBYTE
3039 else if (has_mbyte)
3040 draw_cmdline(ccline.cmdpos,
3041 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 else
3044 draw_cmdline(ccline.cmdpos, 1);
3045 msg_no_more = FALSE;
3046 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003047 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048}
3049
3050/*
3051 * Put the given string, of the given length, onto the command line.
3052 * If len is -1, then STRLEN() is used to calculate the length.
3053 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3054 * part will be redrawn, otherwise it will not. If this function is called
3055 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3056 * called afterwards.
3057 */
3058 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003059put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
3061 int retval;
3062 int i;
3063 int m;
3064 int c;
3065
3066 if (len < 0)
3067 len = (int)STRLEN(str);
3068
3069 /* Check if ccline.cmdbuff needs to be longer */
3070 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003071 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else
3073 retval = OK;
3074 if (retval == OK)
3075 {
3076 if (!ccline.overstrike)
3077 {
3078 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3079 ccline.cmdbuff + ccline.cmdpos,
3080 (size_t)(ccline.cmdlen - ccline.cmdpos));
3081 ccline.cmdlen += len;
3082 }
3083 else
3084 {
3085#ifdef FEAT_MBYTE
3086 if (has_mbyte)
3087 {
3088 /* Count nr of characters in the new string. */
3089 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003090 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 ++m;
3092 /* Count nr of bytes in cmdline that are overwritten by these
3093 * characters. */
3094 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003095 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 --m;
3097 if (i < ccline.cmdlen)
3098 {
3099 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3100 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3101 ccline.cmdlen += ccline.cmdpos + len - i;
3102 }
3103 else
3104 ccline.cmdlen = ccline.cmdpos + len;
3105 }
3106 else
3107#endif
3108 if (ccline.cmdpos + len > ccline.cmdlen)
3109 ccline.cmdlen = ccline.cmdpos + len;
3110 }
3111 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3112 ccline.cmdbuff[ccline.cmdlen] = NUL;
3113
3114#ifdef FEAT_MBYTE
3115 if (enc_utf8)
3116 {
3117 /* When the inserted text starts with a composing character,
3118 * backup to the character before it. There could be two of them.
3119 */
3120 i = 0;
3121 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3122 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3123 {
3124 i = (*mb_head_off)(ccline.cmdbuff,
3125 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3126 ccline.cmdpos -= i;
3127 len += i;
3128 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3129 }
3130# ifdef FEAT_ARABIC
3131 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3132 {
3133 /* Check the previous character for Arabic combining pair. */
3134 i = (*mb_head_off)(ccline.cmdbuff,
3135 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3136 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3137 + ccline.cmdpos - i), c))
3138 {
3139 ccline.cmdpos -= i;
3140 len += i;
3141 }
3142 else
3143 i = 0;
3144 }
3145# endif
3146 if (i != 0)
3147 {
3148 /* Also backup the cursor position. */
3149 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3150 ccline.cmdspos -= i;
3151 msg_col -= i;
3152 if (msg_col < 0)
3153 {
3154 msg_col += Columns;
3155 --msg_row;
3156 }
3157 }
3158 }
3159#endif
3160
3161 if (redraw && !cmd_silent)
3162 {
3163 msg_no_more = TRUE;
3164 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003165 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3167 /* Avoid clearing the rest of the line too often. */
3168 if (cmdline_row != i || ccline.overstrike)
3169 msg_clr_eos();
3170 msg_no_more = FALSE;
3171 }
3172#ifdef FEAT_FKMAP
3173 /*
3174 * If we are in Farsi command mode, the character input must be in
3175 * Insert mode. So do not advance the cmdpos.
3176 */
3177 if (!cmd_fkmap)
3178#endif
3179 {
3180 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003181 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003183 if (m < 0) /* overflow, Columns or Rows at weird value */
3184 m = MAXCOL;
3185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 else
3187 m = MAXCOL;
3188 for (i = 0; i < len; ++i)
3189 {
3190 c = cmdline_charsize(ccline.cmdpos);
3191#ifdef FEAT_MBYTE
3192 /* count ">" for a double-wide char that doesn't fit. */
3193 if (has_mbyte)
3194 correct_cmdspos(ccline.cmdpos, c);
3195#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003196 /* Stop cursor at the end of the screen, but do increment the
3197 * insert position, so that entering a very long command
3198 * works, even though you can't see it. */
3199 if (ccline.cmdspos + c < m)
3200 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201#ifdef FEAT_MBYTE
3202 if (has_mbyte)
3203 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003204 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 if (c > len - i - 1)
3206 c = len - i - 1;
3207 ccline.cmdpos += c;
3208 i += c;
3209 }
3210#endif
3211 ++ccline.cmdpos;
3212 }
3213 }
3214 }
3215 if (redraw)
3216 msg_check();
3217 return retval;
3218}
3219
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003220static struct cmdline_info prev_ccline;
3221static int prev_ccline_used = FALSE;
3222
3223/*
3224 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3225 * and overwrite it. But get_cmdline_str() may need it, thus make it
3226 * available globally in prev_ccline.
3227 */
3228 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003229save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003230{
3231 if (!prev_ccline_used)
3232 {
3233 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3234 prev_ccline_used = TRUE;
3235 }
3236 *ccp = prev_ccline;
3237 prev_ccline = ccline;
3238 ccline.cmdbuff = NULL;
3239 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003240 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003241}
3242
3243/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003244 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003245 */
3246 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003247restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003248{
3249 ccline = prev_ccline;
3250 prev_ccline = *ccp;
3251}
3252
Bram Moolenaar5a305422006-04-28 22:38:25 +00003253#if defined(FEAT_EVAL) || defined(PROTO)
3254/*
3255 * Save the command line into allocated memory. Returns a pointer to be
3256 * passed to restore_cmdline_alloc() later.
3257 * Returns NULL when failed.
3258 */
3259 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003260save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003261{
3262 struct cmdline_info *p;
3263
3264 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3265 if (p != NULL)
3266 save_cmdline(p);
3267 return (char_u *)p;
3268}
3269
3270/*
3271 * Restore the command line from the return value of save_cmdline_alloc().
3272 */
3273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003274restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003275{
3276 if (p != NULL)
3277 {
3278 restore_cmdline((struct cmdline_info *)p);
3279 vim_free(p);
3280 }
3281}
3282#endif
3283
Bram Moolenaar8299df92004-07-10 09:47:34 +00003284/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003285 * Paste a yank register into the command line.
3286 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003287 * insert_reg() can't be used here, because special characters from the
3288 * register contents will be interpreted as commands.
3289 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003290 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291 */
3292 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003293cmdline_paste(
3294 int regname,
3295 int literally, /* Insert text literally instead of "as typed" */
3296 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003297{
3298 long i;
3299 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003300 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003301 int allocated;
3302 struct cmdline_info save_ccline;
3303
3304 /* check for valid regname; also accept special characters for CTRL-R in
3305 * the command line */
3306 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3307 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3308 return FAIL;
3309
3310 /* A register containing CTRL-R can cause an endless loop. Allow using
3311 * CTRL-C to break the loop. */
3312 line_breakcheck();
3313 if (got_int)
3314 return FAIL;
3315
3316#ifdef FEAT_CLIPBOARD
3317 regname = may_get_selection(regname);
3318#endif
3319
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003320 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003321 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003322 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003323 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003324 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003325 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003326 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003327
3328 if (i)
3329 {
3330 /* Got the value of a special register in "arg". */
3331 if (arg == NULL)
3332 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003333
3334 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3335 * part of the word. */
3336 p = arg;
3337 if (p_is && regname == Ctrl_W)
3338 {
3339 char_u *w;
3340 int len;
3341
3342 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003343 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003344 {
3345#ifdef FEAT_MBYTE
3346 if (has_mbyte)
3347 {
3348 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3349 if (!vim_iswordc(mb_ptr2char(w - len)))
3350 break;
3351 w -= len;
3352 }
3353 else
3354#endif
3355 {
3356 if (!vim_iswordc(w[-1]))
3357 break;
3358 --w;
3359 }
3360 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003361 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003362 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3363 p += len;
3364 }
3365
3366 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003367 if (allocated)
3368 vim_free(arg);
3369 return OK;
3370 }
3371
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003372 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003373}
3374
3375/*
3376 * Put a string on the command line.
3377 * When "literally" is TRUE, insert literally.
3378 * When "literally" is FALSE, insert as typed, but don't leave the command
3379 * line.
3380 */
3381 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003382cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003383{
3384 int c, cv;
3385
3386 if (literally)
3387 put_on_cmdline(s, -1, TRUE);
3388 else
3389 while (*s != NUL)
3390 {
3391 cv = *s;
3392 if (cv == Ctrl_V && s[1])
3393 ++s;
3394#ifdef FEAT_MBYTE
3395 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003396 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003397 else
3398#endif
3399 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003400 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3401 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003402#ifdef UNIX
3403 || c == intr_char
3404#endif
3405 || (c == Ctrl_BSL && *s == Ctrl_N))
3406 stuffcharReadbuff(Ctrl_V);
3407 stuffcharReadbuff(c);
3408 }
3409}
3410
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411#ifdef FEAT_WILDMENU
3412/*
3413 * Delete characters on the command line, from "from" to the current
3414 * position.
3415 */
3416 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003417cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
3419 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3420 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3421 ccline.cmdlen -= ccline.cmdpos - from;
3422 ccline.cmdpos = from;
3423}
3424#endif
3425
3426/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003427 * This function is called when the screen size changes and with incremental
3428 * search and in other situations where the command line may have been
3429 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 */
3431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003432redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003434 redrawcmdline_ex(TRUE);
3435}
3436
3437 void
3438redrawcmdline_ex(int do_compute_cmdrow)
3439{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 if (cmd_silent)
3441 return;
3442 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003443 if (do_compute_cmdrow)
3444 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 redrawcmd();
3446 cursorcmd();
3447}
3448
3449 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003450redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451{
3452 int i;
3453
3454 if (cmd_silent)
3455 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003456 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 msg_putchar(ccline.cmdfirstc);
3458 if (ccline.cmdprompt != NULL)
3459 {
3460 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3461 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3462 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003463 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 --ccline.cmdindent;
3465 }
3466 else
3467 for (i = ccline.cmdindent; i > 0; --i)
3468 msg_putchar(' ');
3469}
3470
3471/*
3472 * Redraw what is currently on the command line.
3473 */
3474 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003475redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476{
3477 if (cmd_silent)
3478 return;
3479
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003480 /* when 'incsearch' is set there may be no command line while redrawing */
3481 if (ccline.cmdbuff == NULL)
3482 {
3483 windgoto(cmdline_row, 0);
3484 msg_clr_eos();
3485 return;
3486 }
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 msg_start();
3489 redrawcmdprompt();
3490
3491 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3492 msg_no_more = TRUE;
3493 draw_cmdline(0, ccline.cmdlen);
3494 msg_clr_eos();
3495 msg_no_more = FALSE;
3496
3497 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003498 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003499 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
3501 /*
3502 * An emsg() before may have set msg_scroll. This is used in normal mode,
3503 * in cmdline mode we can reset them now.
3504 */
3505 msg_scroll = FALSE; /* next message overwrites cmdline */
3506
3507 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3508 * in cmdline mode */
3509 skip_redraw = FALSE;
3510}
3511
3512 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003513compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003515 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 cmdline_row = Rows - 1;
3517 else
3518 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003519 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520}
3521
3522 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003523cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 if (cmd_silent)
3526 return;
3527
3528#ifdef FEAT_RIGHTLEFT
3529 if (cmdmsg_rl)
3530 {
3531 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3532 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3533 if (msg_row <= 0)
3534 msg_row = Rows - 1;
3535 }
3536 else
3537#endif
3538 {
3539 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3540 msg_col = ccline.cmdspos % (int)Columns;
3541 if (msg_row >= Rows)
3542 msg_row = Rows - 1;
3543 }
3544
3545 windgoto(msg_row, msg_col);
3546#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003547 if (p_imst == IM_ON_THE_SPOT)
3548 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549#endif
3550#ifdef MCH_CURSOR_SHAPE
3551 mch_update_cursor();
3552#endif
3553}
3554
3555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003556gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557{
3558 msg_start();
3559#ifdef FEAT_RIGHTLEFT
3560 if (cmdmsg_rl)
3561 msg_col = Columns - 1;
3562 else
3563#endif
3564 msg_col = 0; /* always start in column 0 */
3565 if (clr) /* clear the bottom line(s) */
3566 msg_clr_eos(); /* will reset clear_cmdline */
3567 windgoto(cmdline_row, 0);
3568}
3569
3570/*
3571 * Check the word in front of the cursor for an abbreviation.
3572 * Called when the non-id character "c" has been entered.
3573 * When an abbreviation is recognized it is removed from the text with
3574 * backspaces and the replacement string is inserted, followed by "c".
3575 */
3576 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003577ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
3579 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3580 return FALSE;
3581
3582 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3583}
3584
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003585#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3586 static int
3587#ifdef __BORLANDC__
3588_RTLENTRYF
3589#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003590sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003591{
3592 char_u *p1 = *(char_u **)s1;
3593 char_u *p2 = *(char_u **)s2;
3594
3595 if (*p1 != '<' && *p2 == '<') return -1;
3596 if (*p1 == '<' && *p2 != '<') return 1;
3597 return STRCMP(p1, p2);
3598}
3599#endif
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601/*
3602 * Return FAIL if this is not an appropriate context in which to do
3603 * completion of anything, return OK if it is (even if there are no matches).
3604 * For the caller, this means that the character is just passed through like a
3605 * normal character (instead of being expanded). This allows :s/^I^D etc.
3606 */
3607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003608nextwild(
3609 expand_T *xp,
3610 int type,
3611 int options, /* extra options for ExpandOne() */
3612 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613{
3614 int i, j;
3615 char_u *p1;
3616 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 int difflen;
3618 int v;
3619
3620 if (xp->xp_numfiles == -1)
3621 {
3622 set_expand_context(xp);
3623 cmd_showtail = expand_showtail(xp);
3624 }
3625
3626 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3627 {
3628 beep_flush();
3629 return OK; /* Something illegal on command line */
3630 }
3631 if (xp->xp_context == EXPAND_NOTHING)
3632 {
3633 /* Caller can use the character as a normal char instead */
3634 return FAIL;
3635 }
3636
3637 MSG_PUTS("..."); /* show that we are busy */
3638 out_flush();
3639
3640 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003641 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642
3643 if (type == WILD_NEXT || type == WILD_PREV)
3644 {
3645 /*
3646 * Get next/previous match for a previous expanded pattern.
3647 */
3648 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3649 }
3650 else
3651 {
3652 /*
3653 * Translate string into pattern and expand it.
3654 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003655 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3656 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 p2 = NULL;
3658 else
3659 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003660 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003661 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3662 if (escape)
3663 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003664
3665 if (p_wic)
3666 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003667 p2 = ExpandOne(xp, p1,
3668 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003669 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003671 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 if (p2 != NULL && type == WILD_LONGEST)
3673 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003674 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 if (ccline.cmdbuff[i + j] == '*'
3676 || ccline.cmdbuff[i + j] == '?')
3677 break;
3678 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003679 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 }
3682 }
3683
3684 if (p2 != NULL && !got_int)
3685 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003686 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003687 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003689 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 xp->xp_pattern = ccline.cmdbuff + i;
3691 }
3692 else
3693 v = OK;
3694 if (v == OK)
3695 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003696 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3697 &ccline.cmdbuff[ccline.cmdpos],
3698 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3699 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 ccline.cmdlen += difflen;
3701 ccline.cmdpos += difflen;
3702 }
3703 }
3704 vim_free(p2);
3705
3706 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003707 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 /* When expanding a ":map" command and no matches are found, assume that
3710 * the key is supposed to be inserted literally */
3711 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3712 return FAIL;
3713
3714 if (xp->xp_numfiles <= 0 && p2 == NULL)
3715 beep_flush();
3716 else if (xp->xp_numfiles == 1)
3717 /* free expanded pattern */
3718 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3719
3720 return OK;
3721}
3722
3723/*
3724 * Do wildcard expansion on the string 'str'.
3725 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003726 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 * Return NULL for failure.
3728 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003729 * "orig" is the originally expanded string, copied to allocated memory. It
3730 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3731 * WILD_PREV "orig" should be NULL.
3732 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003733 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3734 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 *
3736 * mode = WILD_FREE: just free previously expanded matches
3737 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3738 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3739 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3740 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3741 * mode = WILD_ALL: return all matches concatenated
3742 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003743 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 *
3745 * options = WILD_LIST_NOTFOUND: list entries without a match
3746 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3747 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3748 * options = WILD_NO_BEEP: Don't beep for multiple matches
3749 * options = WILD_ADD_SLASH: add a slash after directory names
3750 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3751 * options = WILD_SILENT: don't print warning messages
3752 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003753 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 *
3755 * The variables xp->xp_context and xp->xp_backslash must have been set!
3756 */
3757 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003758ExpandOne(
3759 expand_T *xp,
3760 char_u *str,
3761 char_u *orig, /* allocated copy of original of expanded string */
3762 int options,
3763 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 char_u *ss = NULL;
3766 static int findex;
3767 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003768 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 int i;
3770 long_u len;
3771 int non_suf_match; /* number without matching suffix */
3772
3773 /*
3774 * first handle the case of using an old match
3775 */
3776 if (mode == WILD_NEXT || mode == WILD_PREV)
3777 {
3778 if (xp->xp_numfiles > 0)
3779 {
3780 if (mode == WILD_PREV)
3781 {
3782 if (findex == -1)
3783 findex = xp->xp_numfiles;
3784 --findex;
3785 }
3786 else /* mode == WILD_NEXT */
3787 ++findex;
3788
3789 /*
3790 * When wrapping around, return the original string, set findex to
3791 * -1.
3792 */
3793 if (findex < 0)
3794 {
3795 if (orig_save == NULL)
3796 findex = xp->xp_numfiles - 1;
3797 else
3798 findex = -1;
3799 }
3800 if (findex >= xp->xp_numfiles)
3801 {
3802 if (orig_save == NULL)
3803 findex = 0;
3804 else
3805 findex = -1;
3806 }
3807#ifdef FEAT_WILDMENU
3808 if (p_wmnu)
3809 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3810 findex, cmd_showtail);
3811#endif
3812 if (findex == -1)
3813 return vim_strsave(orig_save);
3814 return vim_strsave(xp->xp_files[findex]);
3815 }
3816 else
3817 return NULL;
3818 }
3819
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003820 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3822 {
3823 FreeWild(xp->xp_numfiles, xp->xp_files);
3824 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003825 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827 findex = 0;
3828
3829 if (mode == WILD_FREE) /* only release file name */
3830 return NULL;
3831
3832 if (xp->xp_numfiles == -1)
3833 {
3834 vim_free(orig_save);
3835 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003836 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 /*
3839 * Do the expansion.
3840 */
3841 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3842 options) == FAIL)
3843 {
3844#ifdef FNAME_ILLEGAL
3845 /* Illegal file name has been silently skipped. But when there
3846 * are wildcards, the real problem is that there was no match,
3847 * causing the pattern to be added, which has illegal characters.
3848 */
3849 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3850 EMSG2(_(e_nomatch2), str);
3851#endif
3852 }
3853 else if (xp->xp_numfiles == 0)
3854 {
3855 if (!(options & WILD_SILENT))
3856 EMSG2(_(e_nomatch2), str);
3857 }
3858 else
3859 {
3860 /* Escape the matches for use on the command line. */
3861 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3862
3863 /*
3864 * Check for matching suffixes in file names.
3865 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003866 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3867 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 {
3869 if (xp->xp_numfiles)
3870 non_suf_match = xp->xp_numfiles;
3871 else
3872 non_suf_match = 1;
3873 if ((xp->xp_context == EXPAND_FILES
3874 || xp->xp_context == EXPAND_DIRECTORIES)
3875 && xp->xp_numfiles > 1)
3876 {
3877 /*
3878 * More than one match; check suffix.
3879 * The files will have been sorted on matching suffix in
3880 * expand_wildcards, only need to check the first two.
3881 */
3882 non_suf_match = 0;
3883 for (i = 0; i < 2; ++i)
3884 if (match_suffix(xp->xp_files[i]))
3885 ++non_suf_match;
3886 }
3887 if (non_suf_match != 1)
3888 {
3889 /* Can we ever get here unless it's while expanding
3890 * interactively? If not, we can get rid of this all
3891 * together. Don't really want to wait for this message
3892 * (and possibly have to hit return to continue!).
3893 */
3894 if (!(options & WILD_SILENT))
3895 EMSG(_(e_toomany));
3896 else if (!(options & WILD_NO_BEEP))
3897 beep_flush();
3898 }
3899 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3900 ss = vim_strsave(xp->xp_files[0]);
3901 }
3902 }
3903 }
3904
3905 /* Find longest common part */
3906 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3907 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003908 int mb_len = 1;
3909 int c0, ci;
3910
3911 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003913#ifdef FEAT_MBYTE
3914 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003915 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003916 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3917 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3918 }
3919 else
3920#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003921 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003922 for (i = 1; i < xp->xp_numfiles; ++i)
3923 {
3924#ifdef FEAT_MBYTE
3925 if (has_mbyte)
3926 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3927 else
3928#endif
3929 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003930 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003932 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003933 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003935 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 break;
3937 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003938 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 break;
3940 }
3941 if (i < xp->xp_numfiles)
3942 {
3943 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003944 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 break;
3946 }
3947 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 ss = alloc((unsigned)len + 1);
3950 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003951 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 findex = -1; /* next p_wc gets first one */
3953 }
3954
3955 /* Concatenate all matching names */
3956 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3957 {
3958 len = 0;
3959 for (i = 0; i < xp->xp_numfiles; ++i)
3960 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3961 ss = lalloc(len, TRUE);
3962 if (ss != NULL)
3963 {
3964 *ss = NUL;
3965 for (i = 0; i < xp->xp_numfiles; ++i)
3966 {
3967 STRCAT(ss, xp->xp_files[i]);
3968 if (i != xp->xp_numfiles - 1)
3969 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3970 }
3971 }
3972 }
3973
3974 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3975 ExpandCleanup(xp);
3976
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003977 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003978 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003979 vim_free(orig);
3980
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 return ss;
3982}
3983
3984/*
3985 * Prepare an expand structure for use.
3986 */
3987 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003988ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003990 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003991 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003993#ifndef BACKSLASH_IN_FILENAME
3994 xp->xp_shell = FALSE;
3995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 xp->xp_numfiles = -1;
3997 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003998#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3999 xp->xp_arg = NULL;
4000#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004001 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002}
4003
4004/*
4005 * Cleanup an expand structure after use.
4006 */
4007 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004008ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 if (xp->xp_numfiles >= 0)
4011 {
4012 FreeWild(xp->xp_numfiles, xp->xp_files);
4013 xp->xp_numfiles = -1;
4014 }
4015}
4016
4017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004018ExpandEscape(
4019 expand_T *xp,
4020 char_u *str,
4021 int numfiles,
4022 char_u **files,
4023 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024{
4025 int i;
4026 char_u *p;
4027
4028 /*
4029 * May change home directory back to "~"
4030 */
4031 if (options & WILD_HOME_REPLACE)
4032 tilde_replace(str, numfiles, files);
4033
4034 if (options & WILD_ESCAPE)
4035 {
4036 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004037 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004038 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 || xp->xp_context == EXPAND_BUFFERS
4040 || xp->xp_context == EXPAND_DIRECTORIES)
4041 {
4042 /*
4043 * Insert a backslash into a file name before a space, \, %, #
4044 * and wildmatch characters, except '~'.
4045 */
4046 for (i = 0; i < numfiles; ++i)
4047 {
4048 /* for ":set path=" we need to escape spaces twice */
4049 if (xp->xp_backslash == XP_BS_THREE)
4050 {
4051 p = vim_strsave_escaped(files[i], (char_u *)" ");
4052 if (p != NULL)
4053 {
4054 vim_free(files[i]);
4055 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004056#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 p = vim_strsave_escaped(files[i], (char_u *)" ");
4058 if (p != NULL)
4059 {
4060 vim_free(files[i]);
4061 files[i] = p;
4062 }
4063#endif
4064 }
4065 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004066#ifdef BACKSLASH_IN_FILENAME
4067 p = vim_strsave_fnameescape(files[i], FALSE);
4068#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004069 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (p != NULL)
4072 {
4073 vim_free(files[i]);
4074 files[i] = p;
4075 }
4076
4077 /* If 'str' starts with "\~", replace "~" at start of
4078 * files[i] with "\~". */
4079 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004080 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 }
4082 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004083
4084 /* If the first file starts with a '+' escape it. Otherwise it
4085 * could be seen as "+cmd". */
4086 if (*files[0] == '+')
4087 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004088 }
4089 else if (xp->xp_context == EXPAND_TAGS)
4090 {
4091 /*
4092 * Insert a backslash before characters in a tag name that
4093 * would terminate the ":tag" command.
4094 */
4095 for (i = 0; i < numfiles; ++i)
4096 {
4097 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4098 if (p != NULL)
4099 {
4100 vim_free(files[i]);
4101 files[i] = p;
4102 }
4103 }
4104 }
4105 }
4106}
4107
4108/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004109 * Escape special characters in "fname" for when used as a file name argument
4110 * after a Vim command, or, when "shell" is non-zero, a shell command.
4111 * Returns the result in allocated memory.
4112 */
4113 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004114vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004115{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004116 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004117#ifdef BACKSLASH_IN_FILENAME
4118 char_u buf[20];
4119 int j = 0;
4120
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004121 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004122 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004123 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004124 buf[j++] = *p;
4125 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004126 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004127#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004128 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4129 if (shell && csh_like_shell() && p != NULL)
4130 {
4131 char_u *s;
4132
4133 /* For csh and similar shells need to put two backslashes before '!'.
4134 * One is taken by Vim, one by the shell. */
4135 s = vim_strsave_escaped(p, (char_u *)"!");
4136 vim_free(p);
4137 p = s;
4138 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004139#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004140
4141 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4142 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004143 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004144 escape_fname(&p);
4145
4146 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004147}
4148
4149/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004150 * Put a backslash before the file name in "pp", which is in allocated memory.
4151 */
4152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004153escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004154{
4155 char_u *p;
4156
4157 p = alloc((unsigned)(STRLEN(*pp) + 2));
4158 if (p != NULL)
4159 {
4160 p[0] = '\\';
4161 STRCPY(p + 1, *pp);
4162 vim_free(*pp);
4163 *pp = p;
4164 }
4165}
4166
4167/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 * For each file name in files[num_files]:
4169 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4170 */
4171 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004172tilde_replace(
4173 char_u *orig_pat,
4174 int num_files,
4175 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176{
4177 int i;
4178 char_u *p;
4179
4180 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4181 {
4182 for (i = 0; i < num_files; ++i)
4183 {
4184 p = home_replace_save(NULL, files[i]);
4185 if (p != NULL)
4186 {
4187 vim_free(files[i]);
4188 files[i] = p;
4189 }
4190 }
4191 }
4192}
4193
4194/*
4195 * Show all matches for completion on the command line.
4196 * Returns EXPAND_NOTHING when the character that triggered expansion should
4197 * be inserted like a normal character.
4198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004200showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
4202#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4203 int num_files;
4204 char_u **files_found;
4205 int i, j, k;
4206 int maxlen;
4207 int lines;
4208 int columns;
4209 char_u *p;
4210 int lastlen;
4211 int attr;
4212 int showtail;
4213
4214 if (xp->xp_numfiles == -1)
4215 {
4216 set_expand_context(xp);
4217 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4218 &num_files, &files_found);
4219 showtail = expand_showtail(xp);
4220 if (i != EXPAND_OK)
4221 return i;
4222
4223 }
4224 else
4225 {
4226 num_files = xp->xp_numfiles;
4227 files_found = xp->xp_files;
4228 showtail = cmd_showtail;
4229 }
4230
4231#ifdef FEAT_WILDMENU
4232 if (!wildmenu)
4233 {
4234#endif
4235 msg_didany = FALSE; /* lines_left will be set */
4236 msg_start(); /* prepare for paging */
4237 msg_putchar('\n');
4238 out_flush();
4239 cmdline_row = msg_row;
4240 msg_didany = FALSE; /* lines_left will be set again */
4241 msg_start(); /* prepare for paging */
4242#ifdef FEAT_WILDMENU
4243 }
4244#endif
4245
4246 if (got_int)
4247 got_int = FALSE; /* only int. the completion, not the cmd line */
4248#ifdef FEAT_WILDMENU
4249 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004250 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251#endif
4252 else
4253 {
4254 /* find the length of the longest file name */
4255 maxlen = 0;
4256 for (i = 0; i < num_files; ++i)
4257 {
4258 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004259 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 || xp->xp_context == EXPAND_BUFFERS))
4261 {
4262 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4263 j = vim_strsize(NameBuff);
4264 }
4265 else
4266 j = vim_strsize(L_SHOWFILE(i));
4267 if (j > maxlen)
4268 maxlen = j;
4269 }
4270
4271 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4272 lines = num_files;
4273 else
4274 {
4275 /* compute the number of columns and lines for the listing */
4276 maxlen += 2; /* two spaces between file names */
4277 columns = ((int)Columns + 2) / maxlen;
4278 if (columns < 1)
4279 columns = 1;
4280 lines = (num_files + columns - 1) / columns;
4281 }
4282
Bram Moolenaar8820b482017-03-16 17:23:31 +01004283 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
4285 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4286 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004287 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 msg_clr_eos();
4289 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004290 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292
4293 /* list the files line by line */
4294 for (i = 0; i < lines; ++i)
4295 {
4296 lastlen = 999;
4297 for (k = i; k < num_files; k += lines)
4298 {
4299 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4300 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004301 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 p = files_found[k] + STRLEN(files_found[k]) + 1;
4303 msg_advance(maxlen + 1);
4304 msg_puts(p);
4305 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004306 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 break;
4308 }
4309 for (j = maxlen - lastlen; --j >= 0; )
4310 msg_putchar(' ');
4311 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004312 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 || xp->xp_context == EXPAND_BUFFERS)
4314 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004315 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004316 if (xp->xp_numfiles != -1)
4317 {
4318 char_u *halved_slash;
4319 char_u *exp_path;
4320
4321 /* Expansion was done before and special characters
4322 * were escaped, need to halve backslashes. Also
4323 * $HOME has been replaced with ~/. */
4324 exp_path = expand_env_save_opt(files_found[k], TRUE);
4325 halved_slash = backslash_halve_save(
4326 exp_path != NULL ? exp_path : files_found[k]);
4327 j = mch_isdir(halved_slash != NULL ? halved_slash
4328 : files_found[k]);
4329 vim_free(exp_path);
4330 vim_free(halved_slash);
4331 }
4332 else
4333 /* Expansion was done here, file names are literal. */
4334 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 if (showtail)
4336 p = L_SHOWFILE(k);
4337 else
4338 {
4339 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4340 TRUE);
4341 p = NameBuff;
4342 }
4343 }
4344 else
4345 {
4346 j = FALSE;
4347 p = L_SHOWFILE(k);
4348 }
4349 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4350 }
4351 if (msg_col > 0) /* when not wrapped around */
4352 {
4353 msg_clr_eos();
4354 msg_putchar('\n');
4355 }
4356 out_flush(); /* show one line at a time */
4357 if (got_int)
4358 {
4359 got_int = FALSE;
4360 break;
4361 }
4362 }
4363
4364 /*
4365 * we redraw the command below the lines that we have just listed
4366 * This is a bit tricky, but it saves a lot of screen updating.
4367 */
4368 cmdline_row = msg_row; /* will put it back later */
4369 }
4370
4371 if (xp->xp_numfiles == -1)
4372 FreeWild(num_files, files_found);
4373
4374 return EXPAND_OK;
4375}
4376
4377/*
4378 * Private gettail for showmatches() (and win_redr_status_matches()):
4379 * Find tail of file name path, but ignore trailing "/".
4380 */
4381 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004382sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 char_u *p;
4385 char_u *t = s;
4386 int had_sep = FALSE;
4387
4388 for (p = s; *p != NUL; )
4389 {
4390 if (vim_ispathsep(*p)
4391#ifdef BACKSLASH_IN_FILENAME
4392 && !rem_backslash(p)
4393#endif
4394 )
4395 had_sep = TRUE;
4396 else if (had_sep)
4397 {
4398 t = p;
4399 had_sep = FALSE;
4400 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004401 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 return t;
4404}
4405
4406/*
4407 * Return TRUE if we only need to show the tail of completion matches.
4408 * When not completing file names or there is a wildcard in the path FALSE is
4409 * returned.
4410 */
4411 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004412expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414 char_u *s;
4415 char_u *end;
4416
4417 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004418 if (xp->xp_context != EXPAND_FILES
4419 && xp->xp_context != EXPAND_SHELLCMD
4420 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 return FALSE;
4422
4423 end = gettail(xp->xp_pattern);
4424 if (end == xp->xp_pattern) /* there is no path separator */
4425 return FALSE;
4426
4427 for (s = xp->xp_pattern; s < end; s++)
4428 {
4429 /* Skip escaped wildcards. Only when the backslash is not a path
4430 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4431 if (rem_backslash(s))
4432 ++s;
4433 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4434 return FALSE;
4435 }
4436 return TRUE;
4437}
4438
4439/*
4440 * Prepare a string for expansion.
4441 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004442 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 * When expanding other names: The string will be used with regcomp(). Copy
4444 * the name into allocated memory and prepend "^".
4445 */
4446 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004447addstar(
4448 char_u *fname,
4449 int len,
4450 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 char_u *retval;
4453 int i, j;
4454 int new_len;
4455 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004456 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004458 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004459 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004460 && context != EXPAND_SHELLCMD
4461 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
4463 /*
4464 * Matching will be done internally (on something other than files).
4465 * So we convert the file-matching-type wildcards into our kind for
4466 * use with vim_regcomp(). First work out how long it will be:
4467 */
4468
4469 /* For help tags the translation is done in find_help_tags().
4470 * For a tag pattern starting with "/" no translation is needed. */
4471 if (context == EXPAND_HELP
4472 || context == EXPAND_COLORS
4473 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004474 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004475 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004476 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004477 || ((context == EXPAND_TAGS_LISTFILES
4478 || context == EXPAND_TAGS)
4479 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 retval = vim_strnsave(fname, len);
4481 else
4482 {
4483 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4484 for (i = 0; i < len; i++)
4485 {
4486 if (fname[i] == '*' || fname[i] == '~')
4487 new_len++; /* '*' needs to be replaced by ".*"
4488 '~' needs to be replaced by "\~" */
4489
4490 /* Buffer names are like file names. "." should be literal */
4491 if (context == EXPAND_BUFFERS && fname[i] == '.')
4492 new_len++; /* "." becomes "\." */
4493
4494 /* Custom expansion takes care of special things, match
4495 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004496 if ((context == EXPAND_USER_DEFINED
4497 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 new_len++; /* '\' becomes "\\" */
4499 }
4500 retval = alloc(new_len);
4501 if (retval != NULL)
4502 {
4503 retval[0] = '^';
4504 j = 1;
4505 for (i = 0; i < len; i++, j++)
4506 {
4507 /* Skip backslash. But why? At least keep it for custom
4508 * expansion. */
4509 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004510 && context != EXPAND_USER_LIST
4511 && fname[i] == '\\'
4512 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 break;
4514
4515 switch (fname[i])
4516 {
4517 case '*': retval[j++] = '.';
4518 break;
4519 case '~': retval[j++] = '\\';
4520 break;
4521 case '?': retval[j] = '.';
4522 continue;
4523 case '.': if (context == EXPAND_BUFFERS)
4524 retval[j++] = '\\';
4525 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004526 case '\\': if (context == EXPAND_USER_DEFINED
4527 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 retval[j++] = '\\';
4529 break;
4530 }
4531 retval[j] = fname[i];
4532 }
4533 retval[j] = NUL;
4534 }
4535 }
4536 }
4537 else
4538 {
4539 retval = alloc(len + 4);
4540 if (retval != NULL)
4541 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004542 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
4544 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004545 * Don't add a star to *, ~, ~user, $var or `cmd`.
4546 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 * ~ would be at the start of the file name, but not the tail.
4548 * $ could be anywhere in the tail.
4549 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004550 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 */
4552 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004553 ends_in_star = (len > 0 && retval[len - 1] == '*');
4554#ifndef BACKSLASH_IN_FILENAME
4555 for (i = len - 2; i >= 0; --i)
4556 {
4557 if (retval[i] != '\\')
4558 break;
4559 ends_in_star = !ends_in_star;
4560 }
4561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004563 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 && vim_strchr(tail, '$') == NULL
4565 && vim_strchr(retval, '`') == NULL)
4566 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004567 else if (len > 0 && retval[len - 1] == '$')
4568 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 retval[len] = NUL;
4570 }
4571 }
4572 return retval;
4573}
4574
4575/*
4576 * Must parse the command line so far to work out what context we are in.
4577 * Completion can then be done based on that context.
4578 * This routine sets the variables:
4579 * xp->xp_pattern The start of the pattern to be expanded within
4580 * the command line (ends at the cursor).
4581 * xp->xp_context The type of thing to expand. Will be one of:
4582 *
4583 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4584 * the command line, like an unknown command. Caller
4585 * should beep.
4586 * EXPAND_NOTHING Unrecognised context for completion, use char like
4587 * a normal char, rather than for completion. eg
4588 * :s/^I/
4589 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4590 * it.
4591 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4592 * EXPAND_FILES After command with XFILE set, or after setting
4593 * with P_EXPAND set. eg :e ^I, :w>>^I
4594 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4595 * when we know only directories are of interest. eg
4596 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004597 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4599 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4600 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4601 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4602 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4603 * EXPAND_EVENTS Complete event names
4604 * EXPAND_SYNTAX Complete :syntax command arguments
4605 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4606 * EXPAND_AUGROUP Complete autocommand group names
4607 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4608 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4609 * eg :unmap a^I , :cunab x^I
4610 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4611 * eg :call sub^I
4612 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4613 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4614 * names in expressions, eg :while s^I
4615 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004616 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 */
4618 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004619set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004621 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 if (ccline.cmdfirstc != ':'
4623#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004624 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004625 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626#endif
4627 )
4628 {
4629 xp->xp_context = EXPAND_NOTHING;
4630 return;
4631 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004632 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633}
4634
4635 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004636set_cmd_context(
4637 expand_T *xp,
4638 char_u *str, /* start of command line */
4639 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004640 int col, /* position of cursor */
4641 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 int old_char = NUL;
4644 char_u *nextcomm;
4645
4646 /*
4647 * Avoid a UMR warning from Purify, only save the character if it has been
4648 * written before.
4649 */
4650 if (col < len)
4651 old_char = str[col];
4652 str[col] = NUL;
4653 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004654
4655#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004656 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004657 {
4658# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004659 /* pass CMD_SIZE because there is no real command */
4660 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004661# endif
4662 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004663 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004664 {
4665 xp->xp_context = ccline.xp_context;
4666 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004667# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004668 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004669# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004670 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004671 else
4672#endif
4673 while (nextcomm != NULL)
4674 nextcomm = set_one_cmd_context(xp, nextcomm);
4675
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004676 /* Store the string here so that call_user_expand_func() can get to them
4677 * easily. */
4678 xp->xp_line = str;
4679 xp->xp_col = col;
4680
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 str[col] = old_char;
4682}
4683
4684/*
4685 * Expand the command line "str" from context "xp".
4686 * "xp" must have been set by set_cmd_context().
4687 * xp->xp_pattern points into "str", to where the text that is to be expanded
4688 * starts.
4689 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4690 * cursor.
4691 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4692 * key that triggered expansion literally.
4693 * Returns EXPAND_OK otherwise.
4694 */
4695 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004696expand_cmdline(
4697 expand_T *xp,
4698 char_u *str, /* start of command line */
4699 int col, /* position of cursor */
4700 int *matchcount, /* return: nr of matches */
4701 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702{
4703 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004704 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705
4706 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4707 {
4708 beep_flush();
4709 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4710 }
4711 if (xp->xp_context == EXPAND_NOTHING)
4712 {
4713 /* Caller can use the character as a normal char instead */
4714 return EXPAND_NOTHING;
4715 }
4716
4717 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004718 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4719 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 if (file_str == NULL)
4721 return EXPAND_UNSUCCESSFUL;
4722
Bram Moolenaar94950a92010-12-02 16:01:29 +01004723 if (p_wic)
4724 options += WILD_ICASE;
4725
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004727 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 {
4729 *matchcount = 0;
4730 *matches = NULL;
4731 }
4732 vim_free(file_str);
4733
4734 return EXPAND_OK;
4735}
4736
4737#ifdef FEAT_MULTI_LANG
4738/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004739 * Cleanup matches for help tags:
4740 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4741 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004743static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744
4745 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004746cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747{
4748 int i, j;
4749 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004750 char_u buf[4];
4751 char_u *p = buf;
4752
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004753 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004754 {
4755 *p++ = '@';
4756 *p++ = p_hlg[0];
4757 *p++ = p_hlg[1];
4758 }
4759 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760
4761 for (i = 0; i < num_file; ++i)
4762 {
4763 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004764 if (len <= 0)
4765 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004766 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
4768 /* Sorting on priority means the same item in another language may
4769 * be anywhere. Search all items for a match up to the "@en". */
4770 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004771 if (j != i && (int)STRLEN(file[j]) == len + 3
4772 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 break;
4774 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004775 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 file[i][len] = NUL;
4777 }
4778 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004779
4780 if (*buf != NUL)
4781 for (i = 0; i < num_file; ++i)
4782 {
4783 len = (int)STRLEN(file[i]) - 3;
4784 if (len <= 0)
4785 continue;
4786 if (STRCMP(file[i] + len, buf) == 0)
4787 {
4788 /* remove the default language */
4789 file[i][len] = NUL;
4790 }
4791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792}
4793#endif
4794
4795/*
4796 * Do the expansion based on xp->xp_context and "pat".
4797 */
4798 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004799ExpandFromContext(
4800 expand_T *xp,
4801 char_u *pat,
4802 int *num_file,
4803 char_u ***file,
4804 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
4806#ifdef FEAT_CMDL_COMPL
4807 regmatch_T regmatch;
4808#endif
4809 int ret;
4810 int flags;
4811
4812 flags = EW_DIR; /* include directories */
4813 if (options & WILD_LIST_NOTFOUND)
4814 flags |= EW_NOTFOUND;
4815 if (options & WILD_ADD_SLASH)
4816 flags |= EW_ADDSLASH;
4817 if (options & WILD_KEEP_ALL)
4818 flags |= EW_KEEPALL;
4819 if (options & WILD_SILENT)
4820 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004821 if (options & WILD_ALLLINKS)
4822 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004824 if (xp->xp_context == EXPAND_FILES
4825 || xp->xp_context == EXPAND_DIRECTORIES
4826 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 {
4828 /*
4829 * Expand file or directory names.
4830 */
4831 int free_pat = FALSE;
4832 int i;
4833
4834 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4835 * space */
4836 if (xp->xp_backslash != XP_BS_NONE)
4837 {
4838 free_pat = TRUE;
4839 pat = vim_strsave(pat);
4840 for (i = 0; pat[i]; ++i)
4841 if (pat[i] == '\\')
4842 {
4843 if (xp->xp_backslash == XP_BS_THREE
4844 && pat[i + 1] == '\\'
4845 && pat[i + 2] == '\\'
4846 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004847 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (xp->xp_backslash == XP_BS_ONE
4849 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004850 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 }
4853
4854 if (xp->xp_context == EXPAND_FILES)
4855 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004856 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4857 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 else
4859 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004860 if (options & WILD_ICASE)
4861 flags |= EW_ICASE;
4862
Bram Moolenaard7834d32009-12-02 16:14:36 +00004863 /* Expand wildcards, supporting %:h and the like. */
4864 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 if (free_pat)
4866 vim_free(pat);
4867 return ret;
4868 }
4869
4870 *file = (char_u **)"";
4871 *num_file = 0;
4872 if (xp->xp_context == EXPAND_HELP)
4873 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004874 /* With an empty argument we would get all the help tags, which is
4875 * very slow. Get matches for "help" instead. */
4876 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4877 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
4879#ifdef FEAT_MULTI_LANG
4880 cleanup_help_tags(*num_file, *file);
4881#endif
4882 return OK;
4883 }
4884 return FAIL;
4885 }
4886
4887#ifndef FEAT_CMDL_COMPL
4888 return FAIL;
4889#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004890 if (xp->xp_context == EXPAND_SHELLCMD)
4891 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (xp->xp_context == EXPAND_OLD_SETTING)
4893 return ExpandOldSetting(num_file, file);
4894 if (xp->xp_context == EXPAND_BUFFERS)
4895 return ExpandBufnames(pat, num_file, file, options);
4896 if (xp->xp_context == EXPAND_TAGS
4897 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4898 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4899 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004900 {
4901 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004902 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4903 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004906 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004907 char *directories[] = {"compiler", 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_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004911 {
4912 char *directories[] = {"syntax", 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 Moolenaar1587a1e2010-07-29 20:59:59 +02004915 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004916 {
4917 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004918 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004919 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004920# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4921 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004922 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004923# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004924 if (xp->xp_context == EXPAND_PACKADD)
4925 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4928 if (regmatch.regprog == NULL)
4929 return FAIL;
4930
4931 /* set ignore-case according to p_ic, p_scs and pat */
4932 regmatch.rm_ic = ignorecase(pat);
4933
4934 if (xp->xp_context == EXPAND_SETTINGS
4935 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4936 ret = ExpandSettings(xp, &regmatch, num_file, file);
4937 else if (xp->xp_context == EXPAND_MAPPINGS)
4938 ret = ExpandMappings(&regmatch, num_file, file);
4939# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4940 else if (xp->xp_context == EXPAND_USER_DEFINED)
4941 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4942# endif
4943 else
4944 {
4945 static struct expgen
4946 {
4947 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004948 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004950 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 } tab[] =
4952 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004953 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4954 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004955 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004956 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004957#ifdef FEAT_CMDHIST
4958 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004961 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004962 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004963 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4964 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4965 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
4967#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004968 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4969 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4970 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4971 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972#endif
4973#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004974 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4975 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#endif
4977#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004978 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004980#ifdef FEAT_PROFILE
4981 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4982#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004983 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004984 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4985 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004986#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004987 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004988#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004989#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004990 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004991#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004992#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004993 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4996 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004997 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4998 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005000 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005001 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005002 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 };
5004 int i;
5005
5006 /*
5007 * Find a context in the table and call the ExpandGeneric() with the
5008 * right function to do the expansion.
5009 */
5010 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005011 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 if (xp->xp_context == tab[i].context)
5013 {
5014 if (tab[i].ic)
5015 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005016 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005017 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 break;
5019 }
5020 }
5021
Bram Moolenaar473de612013-06-08 18:19:48 +02005022 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 return ret;
5025#endif /* FEAT_CMDL_COMPL */
5026}
5027
5028#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5029/*
5030 * Expand a list of names.
5031 *
5032 * Generic function for command line completion. It calls a function to
5033 * obtain strings, one by one. The strings are matched against a regexp
5034 * program. Matching strings are copied into an array, which is returned.
5035 *
5036 * Returns OK when no problems encountered, FAIL for error (out of memory).
5037 */
5038 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005039ExpandGeneric(
5040 expand_T *xp,
5041 regmatch_T *regmatch,
5042 int *num_file,
5043 char_u ***file,
5044 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005046 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047{
5048 int i;
5049 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005050 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 char_u *str;
5052
5053 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005054 * round == 0: count the number of matching names
5055 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005057 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 for (i = 0; ; ++i)
5060 {
5061 str = (*func)(xp, i);
5062 if (str == NULL) /* end of list */
5063 break;
5064 if (*str == NUL) /* skip empty strings */
5065 continue;
5066
5067 if (vim_regexec(regmatch, str, (colnr_T)0))
5068 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005069 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005071 if (escaped)
5072 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5073 else
5074 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 (*file)[count] = str;
5076#ifdef FEAT_MENU
5077 if (func == get_menu_names && str != NULL)
5078 {
5079 /* test for separator added by get_menu_names() */
5080 str += STRLEN(str) - 1;
5081 if (*str == '\001')
5082 *str = '.';
5083 }
5084#endif
5085 }
5086 ++count;
5087 }
5088 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005089 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 if (count == 0)
5092 return OK;
5093 *num_file = count;
5094 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5095 if (*file == NULL)
5096 {
5097 *file = (char_u **)"";
5098 return FAIL;
5099 }
5100 count = 0;
5101 }
5102 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005103
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005104 /* Sort the results. Keep menu's in the specified order. */
5105 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005106 {
5107 if (xp->xp_context == EXPAND_EXPRESSION
5108 || xp->xp_context == EXPAND_FUNCTIONS
5109 || xp->xp_context == EXPAND_USER_FUNC)
5110 /* <SNR> functions should be sorted to the end. */
5111 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5112 sort_func_compare);
5113 else
5114 sort_strings(*file, *num_file);
5115 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005116
Bram Moolenaar4f688582007-07-24 12:34:30 +00005117#ifdef FEAT_CMDL_COMPL
5118 /* Reset the variables used for special highlight names expansion, so that
5119 * they don't show up when getting normal highlight names by ID. */
5120 reset_expand_highlight();
5121#endif
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 return OK;
5124}
5125
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005126/*
5127 * Complete a shell command.
5128 * Returns FAIL or OK;
5129 */
5130 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005131expand_shellcmd(
5132 char_u *filepat, /* pattern to match with command names */
5133 int *num_file, /* return: number of matches */
5134 char_u ***file, /* return: array with matches */
5135 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005136{
5137 char_u *pat;
5138 int i;
5139 char_u *path;
5140 int mustfree = FALSE;
5141 garray_T ga;
5142 char_u *buf = alloc(MAXPATHL);
5143 size_t l;
5144 char_u *s, *e;
5145 int flags = flagsarg;
5146 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005147 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005148
5149 if (buf == NULL)
5150 return FAIL;
5151
5152 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5153 * space */
5154 pat = vim_strsave(filepat);
5155 for (i = 0; pat[i]; ++i)
5156 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005157 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005158
Bram Moolenaarb5971142015-03-21 17:32:19 +01005159 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005160
5161 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005162 if (mch_isFullName(pat))
5163 path = (char_u *)" ";
5164 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005165 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5166 path = (char_u *)".";
5167 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005168 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005169 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005170 if (path == NULL)
5171 path = (char_u *)"";
5172 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005173
5174 /*
5175 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005176 * collect them in "ga". When "." is not in $PATH also expand for the
5177 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005178 */
5179 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005180 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005181 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005182 if (*s == NUL)
5183 {
5184 if (did_curdir)
5185 break;
5186 /* Find directories in the current directory, path is empty. */
5187 did_curdir = TRUE;
5188 }
5189 else if (*s == '.')
5190 did_curdir = TRUE;
5191
Bram Moolenaar68c31742006-09-02 15:54:18 +00005192 if (*s == ' ')
5193 ++s; /* Skip space used for absolute path name. */
5194
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005195#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005196 e = vim_strchr(s, ';');
5197#else
5198 e = vim_strchr(s, ':');
5199#endif
5200 if (e == NULL)
5201 e = s + STRLEN(s);
5202
5203 l = e - s;
5204 if (l > MAXPATHL - 5)
5205 break;
5206 vim_strncpy(buf, s, l);
5207 add_pathsep(buf);
5208 l = STRLEN(buf);
5209 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5210
5211 /* Expand matches in one directory of $PATH. */
5212 ret = expand_wildcards(1, &buf, num_file, file, flags);
5213 if (ret == OK)
5214 {
5215 if (ga_grow(&ga, *num_file) == FAIL)
5216 FreeWild(*num_file, *file);
5217 else
5218 {
5219 for (i = 0; i < *num_file; ++i)
5220 {
5221 s = (*file)[i];
5222 if (STRLEN(s) > l)
5223 {
5224 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005225 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005226 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5227 }
5228 else
5229 vim_free(s);
5230 }
5231 vim_free(*file);
5232 }
5233 }
5234 if (*e != NUL)
5235 ++e;
5236 }
5237 *file = ga.ga_data;
5238 *num_file = ga.ga_len;
5239
5240 vim_free(buf);
5241 vim_free(pat);
5242 if (mustfree)
5243 vim_free(path);
5244 return OK;
5245}
5246
5247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005249static 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 +00005250
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005252 * Call "user_expand_func()" to invoke a user defined Vim script function and
5253 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005255 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005256call_user_expand_func(
5257 void *(*user_expand_func)(char_u *, int, char_u **, int),
5258 expand_T *xp,
5259 int *num_file,
5260 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005262 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005263 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005266 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005267 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268
Bram Moolenaarb7515462013-06-29 12:58:33 +02005269 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005270 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 *num_file = 0;
5272 *file = NULL;
5273
Bram Moolenaarb7515462013-06-29 12:58:33 +02005274 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005275 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005276 keep = ccline.cmdbuff[ccline.cmdlen];
5277 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005278 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005279
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005280 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005281 args[1] = xp->xp_line;
5282 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 args[2] = num;
5284
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005285 /* Save the cmdline, we don't know what the function may do. */
5286 save_ccline = ccline;
5287 ccline.cmdbuff = NULL;
5288 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005290
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005291 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005292
5293 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005295 if (ccline.cmdbuff != NULL)
5296 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005297
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005298 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005299 return ret;
5300}
5301
5302/*
5303 * Expand names with a function defined by the user.
5304 */
5305 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005306ExpandUserDefined(
5307 expand_T *xp,
5308 regmatch_T *regmatch,
5309 int *num_file,
5310 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005311{
5312 char_u *retstr;
5313 char_u *s;
5314 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005315 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005316 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005317 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005318
5319 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5320 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 return FAIL;
5322
5323 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005324 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 {
5326 e = vim_strchr(s, '\n');
5327 if (e == NULL)
5328 e = s + STRLEN(s);
5329 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005330 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005332 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5333 *e = keep;
5334
5335 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005337 if (ga_grow(&ga, 1) == FAIL)
5338 break;
5339 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5340 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (*e != NUL)
5344 ++e;
5345 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005346 vim_free(retstr);
5347 *file = ga.ga_data;
5348 *num_file = ga.ga_len;
5349 return OK;
5350}
5351
5352/*
5353 * Expand names with a list returned by a function defined by the user.
5354 */
5355 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005356ExpandUserList(
5357 expand_T *xp,
5358 int *num_file,
5359 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005360{
5361 list_T *retlist;
5362 listitem_T *li;
5363 garray_T ga;
5364
5365 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5366 if (retlist == NULL)
5367 return FAIL;
5368
5369 ga_init2(&ga, (int)sizeof(char *), 3);
5370 /* Loop over the items in the list. */
5371 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5372 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005373 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5374 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005375
5376 if (ga_grow(&ga, 1) == FAIL)
5377 break;
5378
5379 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005380 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005381 ++ga.ga_len;
5382 }
5383 list_unref(retlist);
5384
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 *file = ga.ga_data;
5386 *num_file = ga.ga_len;
5387 return OK;
5388}
5389#endif
5390
5391/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005392 * Expand color scheme, compiler or filetype names.
5393 * Search from 'runtimepath':
5394 * 'runtimepath'/{dirnames}/{pat}.vim
5395 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5396 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5397 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5398 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005399 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 */
5401 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005402ExpandRTDir(
5403 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005404 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005405 int *num_file,
5406 char_u ***file,
5407 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 char_u *s;
5410 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005411 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005413 int i;
5414 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
5416 *num_file = 0;
5417 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005418 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005419 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005421 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005423 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5424 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005426 ga_clear_strings(&ga);
5427 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005429 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005430 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005431 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005433
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005434 if (flags & DIP_START) {
5435 for (i = 0; dirnames[i] != NULL; ++i)
5436 {
5437 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5438 if (s == NULL)
5439 {
5440 ga_clear_strings(&ga);
5441 return FAIL;
5442 }
5443 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5444 globpath(p_pp, s, &ga, 0);
5445 vim_free(s);
5446 }
5447 }
5448
5449 if (flags & DIP_OPT) {
5450 for (i = 0; dirnames[i] != NULL; ++i)
5451 {
5452 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5453 if (s == NULL)
5454 {
5455 ga_clear_strings(&ga);
5456 return FAIL;
5457 }
5458 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5459 globpath(p_pp, s, &ga, 0);
5460 vim_free(s);
5461 }
5462 }
5463
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005464 for (i = 0; i < ga.ga_len; ++i)
5465 {
5466 match = ((char_u **)ga.ga_data)[i];
5467 s = match;
5468 e = s + STRLEN(s);
5469 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5470 {
5471 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005472 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005473 if (s < match || vim_ispathsep(*s))
5474 break;
5475 ++s;
5476 *e = NUL;
5477 mch_memmove(match, s, e - s + 1);
5478 }
5479 }
5480
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005481 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005482 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005483
5484 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005485 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005486 remove_duplicates(&ga);
5487
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 *file = ga.ga_data;
5489 *num_file = ga.ga_len;
5490 return OK;
5491}
5492
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005493/*
5494 * Expand loadplugin names:
5495 * 'packpath'/pack/ * /opt/{pat}
5496 */
5497 static int
5498ExpandPackAddDir(
5499 char_u *pat,
5500 int *num_file,
5501 char_u ***file)
5502{
5503 char_u *s;
5504 char_u *e;
5505 char_u *match;
5506 garray_T ga;
5507 int i;
5508 int pat_len;
5509
5510 *num_file = 0;
5511 *file = NULL;
5512 pat_len = (int)STRLEN(pat);
5513 ga_init2(&ga, (int)sizeof(char *), 10);
5514
5515 s = alloc((unsigned)(pat_len + 26));
5516 if (s == NULL)
5517 {
5518 ga_clear_strings(&ga);
5519 return FAIL;
5520 }
5521 sprintf((char *)s, "pack/*/opt/%s*", pat);
5522 globpath(p_pp, s, &ga, 0);
5523 vim_free(s);
5524
5525 for (i = 0; i < ga.ga_len; ++i)
5526 {
5527 match = ((char_u **)ga.ga_data)[i];
5528 s = gettail(match);
5529 e = s + STRLEN(s);
5530 mch_memmove(match, s, e - s + 1);
5531 }
5532
5533 if (ga.ga_len == 0)
5534 return FAIL;
5535
5536 /* Sort and remove duplicates which can happen when specifying multiple
5537 * directories in dirnames. */
5538 remove_duplicates(&ga);
5539
5540 *file = ga.ga_data;
5541 *num_file = ga.ga_len;
5542 return OK;
5543}
5544
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545#endif
5546
5547#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5548/*
5549 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005550 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005552 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005553globpath(
5554 char_u *path,
5555 char_u *file,
5556 garray_T *ga,
5557 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558{
5559 expand_T xpc;
5560 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 int num_p;
5563 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 buf = alloc(MAXPATHL);
5566 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005567 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005569 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 /* Loop over all entries in {path}. */
5573 while (*path != NUL)
5574 {
5575 /* Copy one item of the path to buf[] and concatenate the file name. */
5576 copy_option_part(&path, buf, MAXPATHL, ",");
5577 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5578 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005579# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005580 /* Using the platform's path separator (\) makes vim incorrectly
5581 * treat it as an escape character, use '/' instead. */
5582 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5583 STRCAT(buf, "/");
5584# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005586# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005588 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5589 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005591 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005593 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 for (i = 0; i < num_p; ++i)
5596 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005597 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005598 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005599 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 FreeWild(num_p, p);
5604 }
5605 }
5606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607
5608 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609}
5610
5611#endif
5612
5613#if defined(FEAT_CMDHIST) || defined(PROTO)
5614
5615/*********************************
5616 * Command line history stuff *
5617 *********************************/
5618
5619/*
5620 * Translate a history character to the associated type number.
5621 */
5622 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005623hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
5625 if (c == ':')
5626 return HIST_CMD;
5627 if (c == '=')
5628 return HIST_EXPR;
5629 if (c == '@')
5630 return HIST_INPUT;
5631 if (c == '>')
5632 return HIST_DEBUG;
5633 return HIST_SEARCH; /* must be '?' or '/' */
5634}
5635
5636/*
5637 * Table of history names.
5638 * These names are used in :history and various hist...() functions.
5639 * It is sufficient to give the significant prefix of a history name.
5640 */
5641
5642static char *(history_names[]) =
5643{
5644 "cmd",
5645 "search",
5646 "expr",
5647 "input",
5648 "debug",
5649 NULL
5650};
5651
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005652#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5653/*
5654 * Function given to ExpandGeneric() to obtain the possible first
5655 * arguments of the ":history command.
5656 */
5657 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005658get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005659{
5660 static char_u compl[2] = { NUL, NUL };
5661 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005662 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005663 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5664
5665 if (idx < short_names_count)
5666 {
5667 compl[0] = (char_u)short_names[idx];
5668 return compl;
5669 }
5670 if (idx < short_names_count + history_name_count)
5671 return (char_u *)history_names[idx - short_names_count];
5672 if (idx == short_names_count + history_name_count)
5673 return (char_u *)"all";
5674 return NULL;
5675}
5676#endif
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678/*
5679 * init_history() - Initialize the command line history.
5680 * Also used to re-allocate the history when the size changes.
5681 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005683init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684{
5685 int newlen; /* new length of history table */
5686 histentry_T *temp;
5687 int i;
5688 int j;
5689 int type;
5690
5691 /*
5692 * If size of history table changed, reallocate it
5693 */
5694 newlen = (int)p_hi;
5695 if (newlen != hislen) /* history length changed */
5696 {
5697 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5698 {
5699 if (newlen)
5700 {
5701 temp = (histentry_T *)lalloc(
5702 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5703 if (temp == NULL) /* out of memory! */
5704 {
5705 if (type == 0) /* first one: just keep the old length */
5706 {
5707 newlen = hislen;
5708 break;
5709 }
5710 /* Already changed one table, now we can only have zero
5711 * length for all tables. */
5712 newlen = 0;
5713 type = -1;
5714 continue;
5715 }
5716 }
5717 else
5718 temp = NULL;
5719 if (newlen == 0 || temp != NULL)
5720 {
5721 if (hisidx[type] < 0) /* there are no entries yet */
5722 {
5723 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005724 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
5726 else if (newlen > hislen) /* array becomes bigger */
5727 {
5728 for (i = 0; i <= hisidx[type]; ++i)
5729 temp[i] = history[type][i];
5730 j = i;
5731 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005732 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733 for ( ; j < hislen; ++i, ++j)
5734 temp[i] = history[type][j];
5735 }
5736 else /* array becomes smaller or 0 */
5737 {
5738 j = hisidx[type];
5739 for (i = newlen - 1; ; --i)
5740 {
5741 if (i >= 0) /* copy newest entries */
5742 temp[i] = history[type][j];
5743 else /* remove older entries */
5744 vim_free(history[type][j].hisstr);
5745 if (--j < 0)
5746 j = hislen - 1;
5747 if (j == hisidx[type])
5748 break;
5749 }
5750 hisidx[type] = newlen - 1;
5751 }
5752 vim_free(history[type]);
5753 history[type] = temp;
5754 }
5755 }
5756 hislen = newlen;
5757 }
5758}
5759
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005761clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005762{
5763 hisptr->hisnum = 0;
5764 hisptr->viminfo = FALSE;
5765 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005766 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005767}
5768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769/*
5770 * Check if command line 'str' is already in history.
5771 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5772 */
5773 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005774in_history(
5775 int type,
5776 char_u *str,
5777 int move_to_front, /* Move the entry to the front if it exists */
5778 int sep,
5779 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780{
5781 int i;
5782 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005783 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
5785 if (hisidx[type] < 0)
5786 return FALSE;
5787 i = hisidx[type];
5788 do
5789 {
5790 if (history[type][i].hisstr == NULL)
5791 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005792
5793 /* For search history, check that the separator character matches as
5794 * well. */
5795 p = history[type][i].hisstr;
5796 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005797 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005798 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 {
5800 if (!move_to_front)
5801 return TRUE;
5802 last_i = i;
5803 break;
5804 }
5805 if (--i < 0)
5806 i = hislen - 1;
5807 } while (i != hisidx[type]);
5808
5809 if (last_i >= 0)
5810 {
5811 str = history[type][i].hisstr;
5812 while (i != hisidx[type])
5813 {
5814 if (++i >= hislen)
5815 i = 0;
5816 history[type][last_i] = history[type][i];
5817 last_i = i;
5818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005820 history[type][i].viminfo = FALSE;
5821 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005822 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 return TRUE;
5824 }
5825 return FALSE;
5826}
5827
5828/*
5829 * Convert history name (from table above) to its HIST_ equivalent.
5830 * When "name" is empty, return "cmd" history.
5831 * Returns -1 for unknown history name.
5832 */
5833 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005834get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 int i;
5837 int len = (int)STRLEN(name);
5838
5839 /* No argument: use current history. */
5840 if (len == 0)
5841 return hist_char2type(ccline.cmdfirstc);
5842
5843 for (i = 0; history_names[i] != NULL; ++i)
5844 if (STRNICMP(name, history_names[i], len) == 0)
5845 return i;
5846
5847 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5848 return hist_char2type(name[0]);
5849
5850 return -1;
5851}
5852
5853static int last_maptick = -1; /* last seen maptick */
5854
5855/*
5856 * Add the given string to the given history. If the string is already in the
5857 * history then it is moved to the front. "histype" may be one of he HIST_
5858 * values.
5859 */
5860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005861add_to_history(
5862 int histype,
5863 char_u *new_entry,
5864 int in_map, /* consider maptick when inside a mapping */
5865 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 histentry_T *hisptr;
5868 int len;
5869
5870 if (hislen == 0) /* no history */
5871 return;
5872
Bram Moolenaara939e432013-11-09 05:30:26 +01005873 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5874 return;
5875
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 /*
5877 * Searches inside the same mapping overwrite each other, so that only
5878 * the last line is kept. Be careful not to remove a line that was moved
5879 * down, only lines that were added.
5880 */
5881 if (histype == HIST_SEARCH && in_map)
5882 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005883 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
5885 /* Current line is from the same mapping, remove it */
5886 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5887 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005888 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 --hisnum[histype];
5890 if (--hisidx[HIST_SEARCH] < 0)
5891 hisidx[HIST_SEARCH] = hislen - 1;
5892 }
5893 last_maptick = -1;
5894 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005895 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 {
5897 if (++hisidx[histype] == hislen)
5898 hisidx[histype] = 0;
5899 hisptr = &history[histype][hisidx[histype]];
5900 vim_free(hisptr->hisstr);
5901
5902 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005903 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5905 if (hisptr->hisstr != NULL)
5906 hisptr->hisstr[len + 1] = sep;
5907
5908 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005909 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005910 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (histype == HIST_SEARCH && in_map)
5912 last_maptick = maptick;
5913 }
5914}
5915
5916#if defined(FEAT_EVAL) || defined(PROTO)
5917
5918/*
5919 * Get identifier of newest history entry.
5920 * "histype" may be one of the HIST_ values.
5921 */
5922 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5926 || hisidx[histype] < 0)
5927 return -1;
5928
5929 return history[histype][hisidx[histype]].hisnum;
5930}
5931
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 * Calculate history index from a number:
5934 * num > 0: seen as identifying number of a history entry
5935 * num < 0: relative position in history wrt newest entry
5936 * "histype" may be one of the HIST_ values.
5937 */
5938 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005939calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940{
5941 int i;
5942 histentry_T *hist;
5943 int wrapped = FALSE;
5944
5945 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5946 || (i = hisidx[histype]) < 0 || num == 0)
5947 return -1;
5948
5949 hist = history[histype];
5950 if (num > 0)
5951 {
5952 while (hist[i].hisnum > num)
5953 if (--i < 0)
5954 {
5955 if (wrapped)
5956 break;
5957 i += hislen;
5958 wrapped = TRUE;
5959 }
5960 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5961 return i;
5962 }
5963 else if (-num <= hislen)
5964 {
5965 i += num + 1;
5966 if (i < 0)
5967 i += hislen;
5968 if (hist[i].hisstr != NULL)
5969 return i;
5970 }
5971 return -1;
5972}
5973
5974/*
5975 * Get a history entry by its index.
5976 * "histype" may be one of the HIST_ values.
5977 */
5978 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005979get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 idx = calc_hist_idx(histype, idx);
5982 if (idx >= 0)
5983 return history[histype][idx].hisstr;
5984 else
5985 return (char_u *)"";
5986}
5987
5988/*
5989 * Clear all entries of a history.
5990 * "histype" may be one of the HIST_ values.
5991 */
5992 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 int i;
5996 histentry_T *hisptr;
5997
5998 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5999 {
6000 hisptr = history[histype];
6001 for (i = hislen; i--;)
6002 {
6003 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006004 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006005 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006 }
6007 hisidx[histype] = -1; /* mark history as cleared */
6008 hisnum[histype] = 0; /* reset identifier counter */
6009 return OK;
6010 }
6011 return FAIL;
6012}
6013
6014/*
6015 * Remove all entries matching {str} from a history.
6016 * "histype" may be one of the HIST_ values.
6017 */
6018 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006019del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 regmatch_T regmatch;
6022 histentry_T *hisptr;
6023 int idx;
6024 int i;
6025 int last;
6026 int found = FALSE;
6027
6028 regmatch.regprog = NULL;
6029 regmatch.rm_ic = FALSE; /* always match case */
6030 if (hislen != 0
6031 && histype >= 0
6032 && histype < HIST_COUNT
6033 && *str != NUL
6034 && (idx = hisidx[histype]) >= 0
6035 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6036 != NULL)
6037 {
6038 i = last = idx;
6039 do
6040 {
6041 hisptr = &history[histype][i];
6042 if (hisptr->hisstr == NULL)
6043 break;
6044 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6045 {
6046 found = TRUE;
6047 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006048 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 }
6050 else
6051 {
6052 if (i != last)
6053 {
6054 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006055 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 }
6057 if (--last < 0)
6058 last += hislen;
6059 }
6060 if (--i < 0)
6061 i += hislen;
6062 } while (i != idx);
6063 if (history[histype][idx].hisstr == NULL)
6064 hisidx[histype] = -1;
6065 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006066 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 return found;
6068}
6069
6070/*
6071 * Remove an indexed entry from a history.
6072 * "histype" may be one of the HIST_ values.
6073 */
6074 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006075del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 int i, j;
6078
6079 i = calc_hist_idx(histype, idx);
6080 if (i < 0)
6081 return FALSE;
6082 idx = hisidx[histype];
6083 vim_free(history[histype][i].hisstr);
6084
6085 /* When deleting the last added search string in a mapping, reset
6086 * last_maptick, so that the last added search string isn't deleted again.
6087 */
6088 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6089 last_maptick = -1;
6090
6091 while (i != idx)
6092 {
6093 j = (i + 1) % hislen;
6094 history[histype][i] = history[histype][j];
6095 i = j;
6096 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006097 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 if (--i < 0)
6099 i += hislen;
6100 hisidx[histype] = i;
6101 return TRUE;
6102}
6103
6104#endif /* FEAT_EVAL */
6105
6106#if defined(FEAT_CRYPT) || defined(PROTO)
6107/*
6108 * Very specific function to remove the value in ":set key=val" from the
6109 * history.
6110 */
6111 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006112remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 char_u *p;
6115 int i;
6116
6117 i = hisidx[HIST_CMD];
6118 if (i < 0)
6119 return;
6120 p = history[HIST_CMD][i].hisstr;
6121 if (p != NULL)
6122 for ( ; *p; ++p)
6123 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6124 {
6125 p = vim_strchr(p + 3, '=');
6126 if (p == NULL)
6127 break;
6128 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006129 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 if (p[i] == '\\' && p[i + 1])
6131 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006132 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 --p;
6134 }
6135}
6136#endif
6137
6138#endif /* FEAT_CMDHIST */
6139
Bram Moolenaar064154c2016-03-19 22:50:43 +01006140#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006141/*
6142 * Get pointer to the command line info to use. cmdline_paste() may clear
6143 * ccline and put the previous value in prev_ccline.
6144 */
6145 static struct cmdline_info *
6146get_ccline_ptr(void)
6147{
6148 if ((State & CMDLINE) == 0)
6149 return NULL;
6150 if (ccline.cmdbuff != NULL)
6151 return &ccline;
6152 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6153 return &prev_ccline;
6154 return NULL;
6155}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006156#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006157
Bram Moolenaar064154c2016-03-19 22:50:43 +01006158#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006159/*
6160 * Get the current command line in allocated memory.
6161 * Only works when the command line is being edited.
6162 * Returns NULL when something is wrong.
6163 */
6164 char_u *
6165get_cmdline_str(void)
6166{
6167 struct cmdline_info *p = get_ccline_ptr();
6168
6169 if (p == NULL)
6170 return NULL;
6171 return vim_strnsave(p->cmdbuff, p->cmdlen);
6172}
6173
6174/*
6175 * Get the current command line position, counted in bytes.
6176 * Zero is the first position.
6177 * Only works when the command line is being edited.
6178 * Returns -1 when something is wrong.
6179 */
6180 int
6181get_cmdline_pos(void)
6182{
6183 struct cmdline_info *p = get_ccline_ptr();
6184
6185 if (p == NULL)
6186 return -1;
6187 return p->cmdpos;
6188}
6189
6190/*
6191 * Set the command line byte position to "pos". Zero is the first position.
6192 * Only works when the command line is being edited.
6193 * Returns 1 when failed, 0 when OK.
6194 */
6195 int
6196set_cmdline_pos(
6197 int pos)
6198{
6199 struct cmdline_info *p = get_ccline_ptr();
6200
6201 if (p == NULL)
6202 return 1;
6203
6204 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6205 * changed the command line. */
6206 if (pos < 0)
6207 new_cmdpos = 0;
6208 else
6209 new_cmdpos = pos;
6210 return 0;
6211}
6212#endif
6213
6214#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6215/*
6216 * Get the current command-line type.
6217 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6218 * Only works when the command line is being edited.
6219 * Returns NUL when something is wrong.
6220 */
6221 int
6222get_cmdline_type(void)
6223{
6224 struct cmdline_info *p = get_ccline_ptr();
6225
6226 if (p == NULL)
6227 return NUL;
6228 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006229 return
6230# ifdef FEAT_EVAL
6231 (p->input_fn) ? '@' :
6232# endif
6233 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006234 return p->cmdfirstc;
6235}
6236#endif
6237
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6239/*
6240 * Get indices "num1,num2" that specify a range within a list (not a range of
6241 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6242 * Returns OK if parsed successfully, otherwise FAIL.
6243 */
6244 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006245get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246{
6247 int len;
6248 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006249 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250
6251 *str = skipwhite(*str);
6252 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6253 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006254 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 *str += len;
6256 *num1 = (int)num;
6257 first = TRUE;
6258 }
6259 *str = skipwhite(*str);
6260 if (**str == ',') /* parse "to" part of range */
6261 {
6262 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006263 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 if (len > 0)
6265 {
6266 *num2 = (int)num;
6267 *str = skipwhite(*str + len);
6268 }
6269 else if (!first) /* no number given at all */
6270 return FAIL;
6271 }
6272 else if (first) /* only one number given */
6273 *num2 = *num1;
6274 return OK;
6275}
6276#endif
6277
6278#if defined(FEAT_CMDHIST) || defined(PROTO)
6279/*
6280 * :history command - print a history
6281 */
6282 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006283ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 histentry_T *hist;
6286 int histype1 = HIST_CMD;
6287 int histype2 = HIST_CMD;
6288 int hisidx1 = 1;
6289 int hisidx2 = -1;
6290 int idx;
6291 int i, j, k;
6292 char_u *end;
6293 char_u *arg = eap->arg;
6294
6295 if (hislen == 0)
6296 {
6297 MSG(_("'history' option is zero"));
6298 return;
6299 }
6300
6301 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6302 {
6303 end = arg;
6304 while (ASCII_ISALPHA(*end)
6305 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6306 end++;
6307 i = *end;
6308 *end = NUL;
6309 histype1 = get_histtype(arg);
6310 if (histype1 == -1)
6311 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006312 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 {
6314 histype1 = 0;
6315 histype2 = HIST_COUNT-1;
6316 }
6317 else
6318 {
6319 *end = i;
6320 EMSG(_(e_trailing));
6321 return;
6322 }
6323 }
6324 else
6325 histype2 = histype1;
6326 *end = i;
6327 }
6328 else
6329 end = arg;
6330 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6331 {
6332 EMSG(_(e_trailing));
6333 return;
6334 }
6335
6336 for (; !got_int && histype1 <= histype2; ++histype1)
6337 {
6338 STRCPY(IObuff, "\n # ");
6339 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6340 MSG_PUTS_TITLE(IObuff);
6341 idx = hisidx[histype1];
6342 hist = history[histype1];
6343 j = hisidx1;
6344 k = hisidx2;
6345 if (j < 0)
6346 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6347 if (k < 0)
6348 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6349 if (idx >= 0 && j <= k)
6350 for (i = idx + 1; !got_int; ++i)
6351 {
6352 if (i == hislen)
6353 i = 0;
6354 if (hist[i].hisstr != NULL
6355 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6356 {
6357 msg_putchar('\n');
6358 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6359 hist[i].hisnum);
6360 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6361 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006362 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 else
6364 STRCAT(IObuff, hist[i].hisstr);
6365 msg_outtrans(IObuff);
6366 out_flush();
6367 }
6368 if (i == idx)
6369 break;
6370 }
6371 }
6372}
6373#endif
6374
6375#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006376/*
6377 * Buffers for history read from a viminfo file. Only valid while reading.
6378 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006379static histentry_T *viminfo_history[HIST_COUNT] =
6380 {NULL, NULL, NULL, NULL, NULL};
6381static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6382static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383static int viminfo_add_at_front = FALSE;
6384
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006385static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386
6387/*
6388 * Translate a history type number to the associated character.
6389 */
6390 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006391hist_type2char(
6392 int type,
6393 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394{
6395 if (type == HIST_CMD)
6396 return ':';
6397 if (type == HIST_SEARCH)
6398 {
6399 if (use_question)
6400 return '?';
6401 else
6402 return '/';
6403 }
6404 if (type == HIST_EXPR)
6405 return '=';
6406 return '@';
6407}
6408
6409/*
6410 * Prepare for reading the history from the viminfo file.
6411 * This allocates history arrays to store the read history lines.
6412 */
6413 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006414prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415{
6416 int i;
6417 int num;
6418 int type;
6419 int len;
6420
6421 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006422 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 if (asklen > hislen)
6424 asklen = hislen;
6425
6426 for (type = 0; type < HIST_COUNT; ++type)
6427 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006428 /* Count the number of empty spaces in the history list. Entries read
6429 * from viminfo previously are also considered empty. If there are
6430 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006432 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 num++;
6434 len = asklen;
6435 if (num > len)
6436 len = num;
6437 if (len <= 0)
6438 viminfo_history[type] = NULL;
6439 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006440 viminfo_history[type] = (histentry_T *)lalloc(
6441 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 if (viminfo_history[type] == NULL)
6443 len = 0;
6444 viminfo_hislen[type] = len;
6445 viminfo_hisidx[type] = 0;
6446 }
6447}
6448
6449/*
6450 * Accept a line from the viminfo, store it in the history array when it's
6451 * new.
6452 */
6453 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006454read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455{
6456 int type;
6457 long_u len;
6458 char_u *val;
6459 char_u *p;
6460
6461 type = hist_char2type(virp->vir_line[0]);
6462 if (viminfo_hisidx[type] < viminfo_hislen[type])
6463 {
6464 val = viminfo_readstring(virp, 1, TRUE);
6465 if (val != NULL && *val != NUL)
6466 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006467 int sep = (*val == ' ' ? NUL : *val);
6468
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006470 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 {
6472 /* Need to re-allocate to append the separator byte. */
6473 len = STRLEN(val);
6474 p = lalloc(len + 2, TRUE);
6475 if (p != NULL)
6476 {
6477 if (type == HIST_SEARCH)
6478 {
6479 /* Search entry: Move the separator from the first
6480 * column to after the NUL. */
6481 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006482 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 }
6484 else
6485 {
6486 /* Not a search entry: No separator in the viminfo
6487 * file, add a NUL separator. */
6488 mch_memmove(p, val, (size_t)len + 1);
6489 p[len + 1] = NUL;
6490 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006491 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6492 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006493 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6494 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006495 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 }
6497 }
6498 }
6499 vim_free(val);
6500 }
6501 return viminfo_readline(virp);
6502}
6503
Bram Moolenaar07219f92013-04-14 23:19:36 +02006504/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006505 * Accept a new style history line from the viminfo, store it in the history
6506 * array when it's new.
6507 */
6508 void
6509handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006510 garray_T *values,
6511 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006512{
6513 int type;
6514 long_u len;
6515 char_u *val;
6516 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006517 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006518
6519 /* Check the format:
6520 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006521 if (values->ga_len < 4
6522 || vp[0].bv_type != BVAL_NR
6523 || vp[1].bv_type != BVAL_NR
6524 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6525 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006526 return;
6527
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006528 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006529 if (type >= HIST_COUNT)
6530 return;
6531 if (viminfo_hisidx[type] < viminfo_hislen[type])
6532 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006533 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006534 if (val != NULL && *val != NUL)
6535 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006536 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6537 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006538 int idx;
6539 int overwrite = FALSE;
6540
6541 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6542 {
6543 /* If lines were written by an older Vim we need to avoid
6544 * getting duplicates. See if the entry already exists. */
6545 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6546 {
6547 p = viminfo_history[type][idx].hisstr;
6548 if (STRCMP(val, p) == 0
6549 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6550 {
6551 overwrite = TRUE;
6552 break;
6553 }
6554 }
6555
6556 if (!overwrite)
6557 {
6558 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006559 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006560 p = lalloc(len + 2, TRUE);
6561 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006562 else
6563 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006564 if (p != NULL)
6565 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006566 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006567 if (!overwrite)
6568 {
6569 mch_memmove(p, val, (size_t)len + 1);
6570 /* Put the separator after the NUL. */
6571 p[len + 1] = sep;
6572 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006573 viminfo_history[type][idx].hisnum = 0;
6574 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006575 viminfo_hisidx[type]++;
6576 }
6577 }
6578 }
6579 }
6580 }
6581}
6582
6583/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006584 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006585 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006586 static void
6587concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588{
6589 int idx;
6590 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006591
6592 idx = hisidx[type] + viminfo_hisidx[type];
6593 if (idx >= hislen)
6594 idx -= hislen;
6595 else if (idx < 0)
6596 idx = hislen - 1;
6597 if (viminfo_add_at_front)
6598 hisidx[type] = idx;
6599 else
6600 {
6601 if (hisidx[type] == -1)
6602 hisidx[type] = hislen - 1;
6603 do
6604 {
6605 if (history[type][idx].hisstr != NULL
6606 || history[type][idx].viminfo)
6607 break;
6608 if (++idx == hislen)
6609 idx = 0;
6610 } while (idx != hisidx[type]);
6611 if (idx != hisidx[type] && --idx < 0)
6612 idx = hislen - 1;
6613 }
6614 for (i = 0; i < viminfo_hisidx[type]; i++)
6615 {
6616 vim_free(history[type][idx].hisstr);
6617 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6618 history[type][idx].viminfo = TRUE;
6619 history[type][idx].time_set = viminfo_history[type][i].time_set;
6620 if (--idx < 0)
6621 idx = hislen - 1;
6622 }
6623 idx += 1;
6624 idx %= hislen;
6625 for (i = 0; i < viminfo_hisidx[type]; i++)
6626 {
6627 history[type][idx++].hisnum = ++hisnum[type];
6628 idx %= hislen;
6629 }
6630}
6631
6632#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6633 static int
6634#ifdef __BORLANDC__
6635_RTLENTRYF
6636#endif
6637sort_hist(const void *s1, const void *s2)
6638{
6639 histentry_T *p1 = *(histentry_T **)s1;
6640 histentry_T *p2 = *(histentry_T **)s2;
6641
6642 if (p1->time_set < p2->time_set) return -1;
6643 if (p1->time_set > p2->time_set) return 1;
6644 return 0;
6645}
6646#endif
6647
6648/*
6649 * Merge history lines from viminfo and lines typed in this Vim based on the
6650 * timestamp;
6651 */
6652 static void
6653merge_history(int type)
6654{
6655 int max_len;
6656 histentry_T **tot_hist;
6657 histentry_T *new_hist;
6658 int i;
6659 int len;
6660
6661 /* Make one long list with all entries. */
6662 max_len = hislen + viminfo_hisidx[type];
6663 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6664 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6665 if (tot_hist == NULL || new_hist == NULL)
6666 {
6667 vim_free(tot_hist);
6668 vim_free(new_hist);
6669 return;
6670 }
6671 for (i = 0; i < viminfo_hisidx[type]; i++)
6672 tot_hist[i] = &viminfo_history[type][i];
6673 len = i;
6674 for (i = 0; i < hislen; i++)
6675 if (history[type][i].hisstr != NULL)
6676 tot_hist[len++] = &history[type][i];
6677
6678 /* Sort the list on timestamp. */
6679 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6680
6681 /* Keep the newest ones. */
6682 for (i = 0; i < hislen; i++)
6683 {
6684 if (i < len)
6685 {
6686 new_hist[i] = *tot_hist[i];
6687 tot_hist[i]->hisstr = NULL;
6688 if (new_hist[i].hisnum == 0)
6689 new_hist[i].hisnum = ++hisnum[type];
6690 }
6691 else
6692 clear_hist_entry(&new_hist[i]);
6693 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006694 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006695
6696 /* Free what is not kept. */
6697 for (i = 0; i < viminfo_hisidx[type]; i++)
6698 vim_free(viminfo_history[type][i].hisstr);
6699 for (i = 0; i < hislen; i++)
6700 vim_free(history[type][i].hisstr);
6701 vim_free(history[type]);
6702 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006703 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006704}
6705
6706/*
6707 * Finish reading history lines from viminfo. Not used when writing viminfo.
6708 */
6709 void
6710finish_viminfo_history(vir_T *virp)
6711{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006713 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714
6715 for (type = 0; type < HIST_COUNT; ++type)
6716 {
6717 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006718 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006719
6720 if (merge)
6721 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006723 concat_history(type);
6724
Bram Moolenaard23a8232018-02-10 18:45:26 +01006725 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006726 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728}
6729
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006730/*
6731 * Write history to viminfo file in "fp".
6732 * When "merge" is TRUE merge history lines with a previously read viminfo
6733 * file, data is in viminfo_history[].
6734 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006737write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738{
6739 int i;
6740 int type;
6741 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006742 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006743
6744 init_history();
6745 if (hislen == 0)
6746 return;
6747 for (type = 0; type < HIST_COUNT; ++type)
6748 {
6749 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6750 if (num_saved == 0)
6751 continue;
6752 if (num_saved < 0) /* Use default */
6753 num_saved = hislen;
6754 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6755 type == HIST_CMD ? _("Command Line") :
6756 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006757 type == HIST_EXPR ? _("Expression") :
6758 type == HIST_INPUT ? _("Input Line") :
6759 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (num_saved > hislen)
6761 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006762
6763 /*
6764 * Merge typed and viminfo history:
6765 * round 1: history of typed commands.
6766 * round 2: history from recently read viminfo.
6767 */
6768 for (round = 1; round <= 2; ++round)
6769 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006770 if (round == 1)
6771 /* start at newest entry, somewhere in the list */
6772 i = hisidx[type];
6773 else if (viminfo_hisidx[type] > 0)
6774 /* start at newest entry, first in the list */
6775 i = 0;
6776 else
6777 /* empty list */
6778 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006779 if (i >= 0)
6780 while (num_saved > 0
6781 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006783 char_u *p;
6784 time_t timestamp;
6785 int c = NUL;
6786
6787 if (round == 1)
6788 {
6789 p = history[type][i].hisstr;
6790 timestamp = history[type][i].time_set;
6791 }
6792 else
6793 {
6794 p = viminfo_history[type] == NULL ? NULL
6795 : viminfo_history[type][i].hisstr;
6796 timestamp = viminfo_history[type] == NULL ? 0
6797 : viminfo_history[type][i].time_set;
6798 }
6799
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006800 if (p != NULL && (round == 2
6801 || !merge
6802 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006804 --num_saved;
6805 fputc(hist_type2char(type, TRUE), fp);
6806 /* For the search history: put the separator in the
6807 * second column; use a space if there isn't one. */
6808 if (type == HIST_SEARCH)
6809 {
6810 c = p[STRLEN(p) + 1];
6811 putc(c == NUL ? ' ' : c, fp);
6812 }
6813 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006814
6815 {
6816 char cbuf[NUMBUFLEN];
6817
6818 /* New style history with a bar line. Format:
6819 * |{bartype},{histtype},{timestamp},{separator},"text" */
6820 if (c == NUL)
6821 cbuf[0] = NUL;
6822 else
6823 sprintf(cbuf, "%d", c);
6824 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6825 type, (long)timestamp, cbuf);
6826 barline_writestring(fp, p, LSIZE - 20);
6827 putc('\n', fp);
6828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006830 if (round == 1)
6831 {
6832 /* Decrement index, loop around and stop when back at
6833 * the start. */
6834 if (--i < 0)
6835 i = hislen - 1;
6836 if (i == hisidx[type])
6837 break;
6838 }
6839 else
6840 {
6841 /* Increment index. Stop at the end in the while. */
6842 ++i;
6843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006845 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006846 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006847 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006848 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006849 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006850 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 }
6852}
6853#endif /* FEAT_VIMINFO */
6854
6855#if defined(FEAT_FKMAP) || defined(PROTO)
6856/*
6857 * Write a character at the current cursor+offset position.
6858 * It is directly written into the command buffer block.
6859 */
6860 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006861cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006862{
6863 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6864 {
6865 EMSG(_("E198: cmd_pchar beyond the command length"));
6866 return;
6867 }
6868 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6869 ccline.cmdbuff[ccline.cmdlen] = NUL;
6870}
6871
6872 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006873cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874{
6875 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6876 {
6877 /* EMSG(_("cmd_gchar beyond the command length")); */
6878 return NUL;
6879 }
6880 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6881}
6882#endif
6883
6884#if defined(FEAT_CMDWIN) || defined(PROTO)
6885/*
6886 * Open a window on the current command line and history. Allow editing in
6887 * the window. Returns when the window is closed.
6888 * Returns:
6889 * CR if the command is to be executed
6890 * Ctrl_C if it is to be abandoned
6891 * K_IGNORE if editing continues
6892 */
6893 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006894open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006897 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006898 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006899 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 win_T *wp;
6901 int i;
6902 linenr_T lnum;
6903 int histtype;
6904 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 int save_restart_edit = restart_edit;
6906 int save_State = State;
6907 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006908#ifdef FEAT_RIGHTLEFT
6909 int save_cmdmsg_rl = cmdmsg_rl;
6910#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006911#ifdef FEAT_FOLDING
6912 int save_KeyTyped;
6913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914
6915 /* Can't do this recursively. Can't do it when typing a password. */
6916 if (cmdwin_type != 0
6917# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6918 || cmdline_star > 0
6919# endif
6920 )
6921 {
6922 beep_flush();
6923 return K_IGNORE;
6924 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006925 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 /* Save current window sizes. */
6928 win_size_save(&winsizes);
6929
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006931 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006932
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006933 /* don't use a new tab page */
6934 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006935 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006936
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 /* Create a window for the command-line buffer. */
6938 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6939 {
6940 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006941 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 return K_IGNORE;
6943 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006944 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006947 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006948 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006951#ifdef FEAT_FOLDING
6952 curwin->w_p_fen = FALSE;
6953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006955 curwin->w_p_rl = cmdmsg_rl;
6956 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006958 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006961 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006962 /* But don't allow switching to another buffer. */
6963 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
Bram Moolenaar46152342005-09-07 21:18:43 +00006965 /* Showing the prompt may have set need_wait_return, reset it. */
6966 need_wait_return = FALSE;
6967
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006968 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6970 {
6971 if (p_wc == TAB)
6972 {
6973 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6974 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6975 }
6976 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6977 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006978 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006979
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006980 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6981 * sets 'textwidth' to 78). */
6982 curbuf->b_p_tw = 0;
6983
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 /* Fill the buffer with the history. */
6985 init_history();
6986 if (hislen > 0)
6987 {
6988 i = hisidx[histtype];
6989 if (i >= 0)
6990 {
6991 lnum = 0;
6992 do
6993 {
6994 if (++i == hislen)
6995 i = 0;
6996 if (history[histtype][i].hisstr != NULL)
6997 ml_append(lnum++, history[histtype][i].hisstr,
6998 (colnr_T)0, FALSE);
6999 }
7000 while (i != hisidx[histtype]);
7001 }
7002 }
7003
7004 /* Replace the empty last line with the current command-line and put the
7005 * cursor there. */
7006 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7007 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7008 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007009 changed_line_abv_curs();
7010 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007011 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012
7013 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007014 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
7016 /* No Ex mode here! */
7017 exmode_active = 0;
7018
7019 State = NORMAL;
7020# ifdef FEAT_MOUSE
7021 setmouse();
7022# endif
7023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007025 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007026 if (restart_edit != 0) /* autocmd with ":startinsert" */
7027 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028
7029 i = RedrawingDisabled;
7030 RedrawingDisabled = 0;
7031
7032 /*
7033 * Call the main loop until <CR> or CTRL-C is typed.
7034 */
7035 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007036 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037
7038 RedrawingDisabled = i;
7039
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007040# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007041 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007042# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007045 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007046
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007047# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007048 /* Restore KeyTyped in case it is modified by autocommands */
7049 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050# endif
7051
Bram Moolenaarccc18222007-05-10 18:25:20 +00007052 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007053 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 cmdwin_type = 0;
7055
7056 exmode_active = save_exmode;
7057
Bram Moolenaarf9821062008-06-20 16:31:07 +00007058 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007060 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007061 {
7062 cmdwin_result = Ctrl_C;
7063 EMSG(_("E199: Active window or buffer deleted"));
7064 }
7065 else
7066 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007067# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 /* autocmds may abort script processing */
7069 if (aborting() && cmdwin_result != K_IGNORE)
7070 cmdwin_result = Ctrl_C;
7071# endif
7072 /* Set the new command line from the cmdline buffer. */
7073 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007074 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007076 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7077
7078 if (histtype == HIST_CMD)
7079 {
7080 /* Execute the command directly. */
7081 ccline.cmdbuff = vim_strsave((char_u *)p);
7082 cmdwin_result = CAR;
7083 }
7084 else
7085 {
7086 /* First need to cancel what we were doing. */
7087 ccline.cmdbuff = NULL;
7088 stuffcharReadbuff(':');
7089 stuffReadbuff((char_u *)p);
7090 stuffcharReadbuff(CAR);
7091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 }
7093 else if (cmdwin_result == K_XF2) /* :qa typed */
7094 {
7095 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7096 cmdwin_result = CAR;
7097 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007098 else if (cmdwin_result == Ctrl_C)
7099 {
7100 /* :q or :close, don't execute any command
7101 * and don't modify the cmd window. */
7102 ccline.cmdbuff = NULL;
7103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 else
7105 ccline.cmdbuff = vim_strsave(ml_get_curline());
7106 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007107 {
7108 ccline.cmdbuff = vim_strsave((char_u *)"");
7109 ccline.cmdlen = 0;
7110 ccline.cmdbufflen = 1;
7111 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 else
7115 {
7116 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7117 ccline.cmdbufflen = ccline.cmdlen + 1;
7118 ccline.cmdpos = curwin->w_cursor.col;
7119 if (ccline.cmdpos > ccline.cmdlen)
7120 ccline.cmdpos = ccline.cmdlen;
7121 if (cmdwin_result == K_IGNORE)
7122 {
7123 set_cmdspos_cursor();
7124 redrawcmd();
7125 }
7126 }
7127
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007129 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007130# ifdef FEAT_CONCEAL
7131 /* Avoid command-line window first character being concealed. */
7132 curwin->w_p_cole = 0;
7133# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007135 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 win_goto(old_curwin);
7137 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007138
7139 /* win_close() may have already wiped the buffer when 'bh' is
7140 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007141 if (bufref_valid(&bufref))
7142 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143
7144 /* Restore window sizes. */
7145 win_size_restore(&winsizes);
7146
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007147 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 }
7149
7150 ga_clear(&winsizes);
7151 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007152# ifdef FEAT_RIGHTLEFT
7153 cmdmsg_rl = save_cmdmsg_rl;
7154# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156 State = save_State;
7157# ifdef FEAT_MOUSE
7158 setmouse();
7159# endif
7160
7161 return cmdwin_result;
7162}
7163#endif /* FEAT_CMDWIN */
7164
7165/*
7166 * Used for commands that either take a simple command string argument, or:
7167 * cmd << endmarker
7168 * {script}
7169 * endmarker
7170 * Returns a pointer to allocated memory with {script} or NULL.
7171 */
7172 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007173script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174{
7175 char_u *theline;
7176 char *end_pattern = NULL;
7177 char dot[] = ".";
7178 garray_T ga;
7179
7180 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7181 return NULL;
7182
7183 ga_init2(&ga, 1, 0x400);
7184
7185 if (cmd[2] != NUL)
7186 end_pattern = (char *)skipwhite(cmd + 2);
7187 else
7188 end_pattern = dot;
7189
7190 for (;;)
7191 {
7192 theline = eap->getline(
7193#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007194 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195#endif
7196 NUL, eap->cookie, 0);
7197
7198 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007199 {
7200 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204 ga_concat(&ga, theline);
7205 ga_append(&ga, '\n');
7206 vim_free(theline);
7207 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007208 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209
7210 return (char_u *)ga.ga_data;
7211}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007212
7213#ifdef FEAT_SEARCH_EXTRA
7214 static void
7215set_search_match(pos_T *t)
7216{
7217 /*
7218 * First move cursor to end of match, then to the start. This
7219 * moves the whole match onto the screen when 'nowrap' is set.
7220 */
7221 t->lnum += search_match_lines;
7222 t->col = search_match_endcol;
7223 if (t->lnum > curbuf->b_ml.ml_line_count)
7224 {
7225 t->lnum = curbuf->b_ml.ml_line_count;
7226 coladvance((colnr_T)MAXCOL);
7227 }
7228}
7229#endif