blob: 9fdfac5a28bcdd4e0d80deba537f6649a700be3d [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
240 linenr_T old_botline;
Bram Moolenaardda933d2016-09-03 21:04:58 +0200241 linenr_T init_botline = curwin->w_botline;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 int did_incsearch = FALSE;
243 int incsearch_postponed = FALSE;
244#endif
245 int did_wild_list = FALSE; /* did wild_list() recently */
246 int wim_index = 0; /* index in wim_flags[] */
247 int res;
248 int save_msg_scroll = msg_scroll;
249 int save_State = State; /* remember State when called */
250 int some_key_typed = FALSE; /* one of the keys was typed */
251#ifdef FEAT_MOUSE
252 /* mouse drag and release events are ignored, unless they are
253 * preceded with a mouse down event */
254 int ignore_drag_release = TRUE;
255#endif
256#ifdef FEAT_EVAL
257 int break_ctrl_c = FALSE;
258#endif
259 expand_T xpc;
260 long *b_im_ptr = NULL;
Bram Moolenaar4d850512017-02-09 18:25:14 +0100261#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000262 /* Everything that may work recursively should save and restore the
263 * current command line in save_ccline. That includes update_screen(), a
264 * custom status line may invoke ":normal". */
265 struct cmdline_info save_ccline;
266#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200267 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269#ifdef FEAT_EVAL
270 if (firstc == -1)
271 {
272 firstc = NUL;
273 break_ctrl_c = TRUE;
274 }
275#endif
276#ifdef FEAT_RIGHTLEFT
277 /* start without Hebrew mapping for a command line */
278 if (firstc == ':' || firstc == '=' || firstc == '>')
279 cmd_hkmap = 0;
280#endif
281
282 ccline.overstrike = FALSE; /* always start in insert mode */
283#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100284 CLEAR_POS(&match_end);
Bram Moolenaardda933d2016-09-03 21:04:58 +0200285 save_cursor = curwin->w_cursor; /* may be restored later */
286 search_start = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 old_curswant = curwin->w_curswant;
288 old_leftcol = curwin->w_leftcol;
289 old_topline = curwin->w_topline;
290# ifdef FEAT_DIFF
291 old_topfill = curwin->w_topfill;
292# endif
293 old_botline = curwin->w_botline;
294#endif
295
296 /*
297 * set some variables for redrawcmd()
298 */
299 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000300 ccline.cmdindent = (firstc > 0 ? indent : 0);
301
302 /* alloc initial ccline.cmdbuff */
303 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304 if (ccline.cmdbuff == NULL)
305 return NULL; /* out of memory */
306 ccline.cmdlen = ccline.cmdpos = 0;
307 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100308 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000310 /* autoindent for :insert and :append */
311 if (firstc <= 0)
312 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200313 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000314 ccline.cmdbuff[indent] = NUL;
315 ccline.cmdpos = indent;
316 ccline.cmdspos = indent;
317 ccline.cmdlen = indent;
318 }
319
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000321 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322
323#ifdef FEAT_RIGHTLEFT
324 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
325 && (firstc == '/' || firstc == '?'))
326 cmdmsg_rl = TRUE;
327 else
328 cmdmsg_rl = FALSE;
329#endif
330
331 redir_off = TRUE; /* don't redirect the typed command */
332 if (!cmd_silent)
333 {
334 i = msg_scrolled;
335 msg_scrolled = 0; /* avoid wait_return message */
336 gotocmdline(TRUE);
337 msg_scrolled += i;
338 redrawcmdprompt(); /* draw prompt or indent */
339 set_cmdspos();
340 }
341 xpc.xp_context = EXPAND_NOTHING;
342 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000343#ifndef BACKSLASH_IN_FILENAME
344 xpc.xp_shell = FALSE;
345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000347#if defined(FEAT_EVAL)
348 if (ccline.input_fn)
349 {
350 xpc.xp_context = ccline.xp_context;
351 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000352# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000353 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000354# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000355 }
356#endif
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 /*
359 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
360 * doing ":@0" when register 0 doesn't contain a CR.
361 */
362 msg_scroll = FALSE;
363
364 State = CMDLINE;
365
366 if (firstc == '/' || firstc == '?' || firstc == '@')
367 {
368 /* Use ":lmap" mappings for search pattern and input(). */
369 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
370 b_im_ptr = &curbuf->b_p_iminsert;
371 else
372 b_im_ptr = &curbuf->b_p_imsearch;
373 if (*b_im_ptr == B_IMODE_LMAP)
374 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100375#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 im_set_active(*b_im_ptr == B_IMODE_IM);
377#endif
378 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100379#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 else if (p_imcmdline)
381 im_set_active(TRUE);
382#endif
383
384#ifdef FEAT_MOUSE
385 setmouse();
386#endif
387#ifdef CURSOR_SHAPE
388 ui_cursor_shape(); /* may show different cursor shape */
389#endif
390
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000391 /* When inside an autocommand for writing "exiting" may be set and
392 * terminal mode set to cooked. Need to set raw mode here then. */
393 settmode(TMODE_RAW);
394
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200395 /* Trigger CmdlineEnter autocommands. */
396 cmdline_type = firstc == NUL ? '-' : firstc;
397 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200398
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399#ifdef FEAT_CMDHIST
400 init_history();
401 hiscnt = hislen; /* set hiscnt to impossible history value */
402 histype = hist_char2type(firstc);
403#endif
404
405#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000406 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000407#endif
408
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200409 /* If something above caused an error, reset the flags, we do want to type
410 * and execute commands. Display may be messed up a bit. */
411 if (did_emsg)
412 redrawcmd();
413 did_emsg = FALSE;
414 got_int = FALSE;
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 /*
417 * Collect the command string, handling editing keys.
418 */
419 for (;;)
420 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000421 redir_off = TRUE; /* Don't redirect the typed command.
422 Repeated, because a ":redir" inside
423 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424#ifdef USE_ON_FLY_SCROLL
425 dont_scroll = FALSE; /* allow scrolling here */
426#endif
427 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
428
Bram Moolenaar72532d32018-04-07 19:09:09 +0200429 did_emsg = FALSE; /* There can't really be a reason why an error
430 that occurs while typing a command should
431 cause the command not to be executed. */
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000434
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100435 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
436 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000437 do
438 {
439 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100440 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000441
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 if (KeyTyped)
443 {
444 some_key_typed = TRUE;
445#ifdef FEAT_RIGHTLEFT
446 if (cmd_hkmap)
447 c = hkmap(c);
448# ifdef FEAT_FKMAP
449 if (cmd_fkmap)
450 c = cmdl_fkmap(c);
451# endif
452 if (cmdmsg_rl && !KeyStuffed)
453 {
454 /* Invert horizontal movements and operations. Only when
455 * typed by the user directly, not when the result of a
456 * mapping. */
457 switch (c)
458 {
459 case K_RIGHT: c = K_LEFT; break;
460 case K_S_RIGHT: c = K_S_LEFT; break;
461 case K_C_RIGHT: c = K_C_LEFT; break;
462 case K_LEFT: c = K_RIGHT; break;
463 case K_S_LEFT: c = K_S_RIGHT; break;
464 case K_C_LEFT: c = K_C_RIGHT; break;
465 }
466 }
467#endif
468 }
469
470 /*
471 * Ignore got_int when CTRL-C was typed here.
472 * Don't ignore it in :global, we really need to break then, e.g., for
473 * ":g/pat/normal /pat" (without the <CR>).
474 * Don't ignore it for the input() function.
475 */
476 if ((c == Ctrl_C
477#ifdef UNIX
478 || c == intr_char
479#endif
480 )
481#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
482 && firstc != '@'
483#endif
484#ifdef FEAT_EVAL
485 && !break_ctrl_c
486#endif
487 && !global_busy)
488 got_int = FALSE;
489
490#ifdef FEAT_CMDHIST
491 /* free old command line when finished moving around in the history
492 * list */
493 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000494 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000495 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 && c != K_PAGEDOWN && c != K_PAGEUP
497 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000498 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100500 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501#endif
502
503 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000504 * When there are matching completions to select <S-Tab> works like
505 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000507 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000508 c = Ctrl_P;
509
510#ifdef FEAT_WILDMENU
511 /* Special translations for 'wildmenu' */
512 if (did_wild_list && p_wmnu)
513 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000514 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000516 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 c = Ctrl_N;
518 }
519 /* Hitting CR after "emenu Name.": complete submenu */
520 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
521 && ccline.cmdpos > 1
522 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
523 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
524 && (c == '\n' || c == '\r' || c == K_KENTER))
525 c = K_DOWN;
526#endif
527
528 /* free expanded names when finished walking through matches */
529 if (xpc.xp_numfiles != -1
530 && !(c == p_wc && KeyTyped) && c != p_wcm
531 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
532 && c != Ctrl_L)
533 {
534 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
535 did_wild_list = FALSE;
536#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000537 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#endif
539 xpc.xp_context = EXPAND_NOTHING;
540 wim_index = 0;
541#ifdef FEAT_WILDMENU
542 if (p_wmnu && wild_menu_showing != 0)
543 {
544 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000545 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000546
547 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000548 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549
550 if (wild_menu_showing == WM_SCROLLED)
551 {
552 /* Entered command line, move it up */
553 cmdline_row--;
554 redrawcmd();
555 }
556 else if (save_p_ls != -1)
557 {
558 /* restore 'laststatus' and 'winminheight' */
559 p_ls = save_p_ls;
560 p_wmh = save_p_wmh;
561 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000562 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000564 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 redrawcmd();
566 save_p_ls = -1;
567 }
568 else
569 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 redraw_statuslines();
572 }
573 KeyTyped = skt;
574 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000575 if (ccline.input_fn)
576 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578#endif
579 }
580
581#ifdef FEAT_WILDMENU
582 /* Special translations for 'wildmenu' */
583 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
584 {
585 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000586 if (c == K_DOWN && ccline.cmdpos > 0
587 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000589 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 {
591 /* Hitting <Up>: Remove one submenu name in front of the
592 * cursor */
593 int found = FALSE;
594
595 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
596 i = 0;
597 while (--j > 0)
598 {
599 /* check for start of menu name */
600 if (ccline.cmdbuff[j] == ' '
601 && ccline.cmdbuff[j - 1] != '\\')
602 {
603 i = j + 1;
604 break;
605 }
606 /* check for start of submenu name */
607 if (ccline.cmdbuff[j] == '.'
608 && ccline.cmdbuff[j - 1] != '\\')
609 {
610 if (found)
611 {
612 i = j + 1;
613 break;
614 }
615 else
616 found = TRUE;
617 }
618 }
619 if (i > 0)
620 cmdline_del(i);
621 c = p_wc;
622 xpc.xp_context = EXPAND_NOTHING;
623 }
624 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000625 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000626 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000627 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 {
629 char_u upseg[5];
630
631 upseg[0] = PATHSEP;
632 upseg[1] = '.';
633 upseg[2] = '.';
634 upseg[3] = PATHSEP;
635 upseg[4] = NUL;
636
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000637 if (c == K_DOWN
638 && ccline.cmdpos > 0
639 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
640 && (ccline.cmdpos < 3
641 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
643 {
644 /* go down a directory */
645 c = p_wc;
646 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000647 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648 {
649 /* If in a direct ancestor, strip off one ../ to go down */
650 int found = FALSE;
651
652 j = ccline.cmdpos;
653 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
654 while (--j > i)
655 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000656#ifdef FEAT_MBYTE
657 if (has_mbyte)
658 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (vim_ispathsep(ccline.cmdbuff[j]))
661 {
662 found = TRUE;
663 break;
664 }
665 }
666 if (found
667 && ccline.cmdbuff[j - 1] == '.'
668 && ccline.cmdbuff[j - 2] == '.'
669 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
670 {
671 cmdline_del(j - 2);
672 c = p_wc;
673 }
674 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000675 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 {
677 /* go up a directory */
678 int found = FALSE;
679
680 j = ccline.cmdpos - 1;
681 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
682 while (--j > i)
683 {
684#ifdef FEAT_MBYTE
685 if (has_mbyte)
686 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
687#endif
688 if (vim_ispathsep(ccline.cmdbuff[j])
689#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +0100690 && vim_strchr((char_u *)" *?[{`$%#",
691 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#endif
693 )
694 {
695 if (found)
696 {
697 i = j + 1;
698 break;
699 }
700 else
701 found = TRUE;
702 }
703 }
704
705 if (!found)
706 j = i;
707 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
708 j += 4;
709 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
710 && j == i)
711 j += 3;
712 else
713 j = 0;
714 if (j > 0)
715 {
716 /* TODO this is only for DOS/UNIX systems - need to put in
717 * machine-specific stuff here and in upseg init */
718 cmdline_del(j);
719 put_on_cmdline(upseg + 1, 3, FALSE);
720 }
721 else if (ccline.cmdpos > i)
722 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +0100723
724 /* Now complete in the new directory. Set KeyTyped in case the
725 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +0100727 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 }
729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
731#endif /* FEAT_WILDMENU */
732
733 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
734 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
735 if (c == Ctrl_BSL)
736 {
737 ++no_mapping;
738 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000739 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 --no_mapping;
741 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +0200742 /* CTRL-\ e doesn't work when obtaining an expression, unless it
743 * is in a mapping. */
744 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
745 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
747 vungetc(c);
748 c = Ctrl_BSL;
749 }
750#ifdef FEAT_EVAL
751 else if (c == 'e')
752 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +0200753 char_u *p = NULL;
754 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756 /*
757 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +0000758 * Need to save and restore the current command line, to be
759 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 */
761 if (ccline.cmdpos == ccline.cmdlen)
762 new_cmdpos = 99999; /* keep it at the end */
763 else
764 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000765
766 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000768 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769 if (c == '=')
770 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000771 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000772 * to avoid nasty things like going to another buffer when
773 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000774 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000775 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000777 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +0000778 restore_cmdline(&save_ccline);
779
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200780 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200782 len = (int)STRLEN(p);
783 if (realloc_cmdbuff(len + 1) == OK)
784 {
785 ccline.cmdlen = len;
786 STRCPY(ccline.cmdbuff, p);
787 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200789 /* Restore the cursor or use the position set with
790 * set_cmdline_pos(). */
791 if (new_cmdpos > ccline.cmdlen)
792 ccline.cmdpos = ccline.cmdlen;
793 else
794 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +0200796 KeyTyped = FALSE; /* Don't do p_wc completion. */
797 redrawcmd();
798 goto cmdline_changed;
799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 }
801 }
802 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +0100803 got_int = FALSE; /* don't abandon the command line */
804 did_emsg = FALSE;
805 emsg_on_display = FALSE;
806 redrawcmd();
807 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809#endif
810 else
811 {
812 if (c == Ctrl_G && p_im && restart_edit == 0)
813 restart_edit = 'a';
814 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
815 in history */
816 goto returncmd; /* back to Normal mode */
817 }
818 }
819
820#ifdef FEAT_CMDWIN
821 if (c == cedit_key || c == K_CMDWIN)
822 {
Bram Moolenaar58da7072014-09-09 18:45:49 +0200823 if (ex_normal_busy == 0 && got_int == FALSE)
824 {
825 /*
826 * Open a window to edit the command line (and history).
827 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200828 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +0200829 some_key_typed = TRUE;
830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 }
832# ifdef FEAT_DIGRAPHS
833 else
834# endif
835#endif
836#ifdef FEAT_DIGRAPHS
837 c = do_digraph(c);
838#endif
839
840 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
841 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
842 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000843 /* In Ex mode a backslash escapes a newline. */
844 if (exmode_active
845 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000846 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000847 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000848 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000850 if (c == K_KENTER)
851 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +0000853 else
854 {
855 gotesc = FALSE; /* Might have typed ESC previously, don't
856 truncate the cmdline now. */
857 if (ccheck_abbr(c + ABBR_OFF))
858 goto cmdline_changed;
859 if (!cmd_silent)
860 {
861 windgoto(msg_row, 0);
862 out_flush();
863 }
864 break;
865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 }
867
868 /*
869 * Completion for 'wildchar' or 'wildcharm' key.
870 * - hitting <ESC> twice means: abandon command line.
871 * - wildcard expansion is only done when the 'wildchar' key is really
872 * typed, not when it comes from a macro
873 */
874 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
875 {
876 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
877 {
878 /* if 'wildmode' contains "list" may still need to list */
879 if (xpc.xp_numfiles > 1
880 && !did_wild_list
881 && (wim_flags[wim_index] & WIM_LIST))
882 {
883 (void)showmatches(&xpc, FALSE);
884 redrawcmd();
885 did_wild_list = TRUE;
886 }
887 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100888 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
889 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100891 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
892 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 else
894 res = OK; /* don't insert 'wildchar' now */
895 }
896 else /* typed p_wc first time */
897 {
898 wim_index = 0;
899 j = ccline.cmdpos;
900 /* if 'wildmode' first contains "longest", get longest
901 * common part */
902 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100903 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
904 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 else
Bram Moolenaarb3479632012-11-28 16:49:58 +0100906 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
907 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909 /* if interrupted while completing, behave like it failed */
910 if (got_int)
911 {
912 (void)vpeekc(); /* remove <C-C> from input stream */
913 got_int = FALSE; /* don't abandon the command line */
914 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
915#ifdef FEAT_WILDMENU
916 xpc.xp_context = EXPAND_NOTHING;
917#endif
918 goto cmdline_changed;
919 }
920
921 /* when more than one match, and 'wildmode' first contains
922 * "list", or no change and 'wildmode' contains "longest,list",
923 * list all matches */
924 if (res == OK && xpc.xp_numfiles > 1)
925 {
926 /* a "longest" that didn't do anything is skipped (but not
927 * "list:longest") */
928 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
929 wim_index = 1;
930 if ((wim_flags[wim_index] & WIM_LIST)
931#ifdef FEAT_WILDMENU
932 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
933#endif
934 )
935 {
936 if (!(wim_flags[0] & WIM_LONGEST))
937 {
938#ifdef FEAT_WILDMENU
939 int p_wmnu_save = p_wmnu;
940 p_wmnu = 0;
941#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +0100942 /* remove match */
943 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#ifdef FEAT_WILDMENU
945 p_wmnu = p_wmnu_save;
946#endif
947 }
948#ifdef FEAT_WILDMENU
949 (void)showmatches(&xpc, p_wmnu
950 && ((wim_flags[wim_index] & WIM_LIST) == 0));
951#else
952 (void)showmatches(&xpc, FALSE);
953#endif
954 redrawcmd();
955 did_wild_list = TRUE;
956 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100957 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
958 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +0100960 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
961 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 }
963 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200964 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 }
966#ifdef FEAT_WILDMENU
967 else if (xpc.xp_numfiles == -1)
968 xpc.xp_context = EXPAND_NOTHING;
969#endif
970 }
971 if (wim_index < 3)
972 ++wim_index;
973 if (c == ESC)
974 gotesc = TRUE;
975 if (res == OK)
976 goto cmdline_changed;
977 }
978
979 gotesc = FALSE;
980
981 /* <S-Tab> goes to last match, in a clumsy way */
982 if (c == K_S_TAB && KeyTyped)
983 {
Bram Moolenaarb3479632012-11-28 16:49:58 +0100984 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
985 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
986 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 goto cmdline_changed;
988 }
989
990 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
991 c = NL;
992
993 do_abbr = TRUE; /* default: check for abbreviation */
994
995 /*
996 * Big switch for a typed command line character.
997 */
998 switch (c)
999 {
1000 case K_BS:
1001 case Ctrl_H:
1002 case K_DEL:
1003 case K_KDEL:
1004 case Ctrl_W:
1005#ifdef FEAT_FKMAP
1006 if (cmd_fkmap && c == K_BS)
1007 c = K_DEL;
1008#endif
1009 if (c == K_KDEL)
1010 c = K_DEL;
1011
1012 /*
1013 * delete current character is the same as backspace on next
1014 * character, except at end of line
1015 */
1016 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1017 ++ccline.cmdpos;
1018#ifdef FEAT_MBYTE
1019 if (has_mbyte && c == K_DEL)
1020 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1021 ccline.cmdbuff + ccline.cmdpos);
1022#endif
1023 if (ccline.cmdpos > 0)
1024 {
1025 char_u *p;
1026
1027 j = ccline.cmdpos;
1028 p = ccline.cmdbuff + j;
1029#ifdef FEAT_MBYTE
1030 if (has_mbyte)
1031 {
1032 p = mb_prevptr(ccline.cmdbuff, p);
1033 if (c == Ctrl_W)
1034 {
1035 while (p > ccline.cmdbuff && vim_isspace(*p))
1036 p = mb_prevptr(ccline.cmdbuff, p);
1037 i = mb_get_class(p);
1038 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1039 p = mb_prevptr(ccline.cmdbuff, p);
1040 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001041 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 }
1043 }
1044 else
1045#endif
1046 if (c == Ctrl_W)
1047 {
1048 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1049 --p;
1050 i = vim_iswordc(p[-1]);
1051 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1052 && vim_iswordc(p[-1]) == i)
1053 --p;
1054 }
1055 else
1056 --p;
1057 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1058 ccline.cmdlen -= j - ccline.cmdpos;
1059 i = ccline.cmdpos;
1060 while (i < ccline.cmdlen)
1061 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1062
1063 /* Truncate at the end, required for multi-byte chars. */
1064 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001065#ifdef FEAT_SEARCH_EXTRA
1066 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001067 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001068 search_start = save_cursor;
1069 /* save view settings, so that the screen
1070 * won't be restored at the wrong position */
1071 old_curswant = init_curswant;
1072 old_leftcol = init_leftcol;
1073 old_topline = init_topline;
1074# ifdef FEAT_DIFF
1075 old_topfill = init_topfill;
1076# endif
1077 old_botline = init_botline;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001078 }
1079#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 redrawcmd();
1081 }
1082 else if (ccline.cmdlen == 0 && c != Ctrl_W
1083 && ccline.cmdprompt == NULL && indent == 0)
1084 {
1085 /* In ex and debug mode it doesn't make sense to return. */
1086 if (exmode_active
1087#ifdef FEAT_EVAL
1088 || ccline.cmdfirstc == '>'
1089#endif
1090 )
1091 goto cmdline_not_changed;
1092
Bram Moolenaard23a8232018-02-10 18:45:26 +01001093 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (!cmd_silent)
1095 {
1096#ifdef FEAT_RIGHTLEFT
1097 if (cmdmsg_rl)
1098 msg_col = Columns;
1099 else
1100#endif
1101 msg_col = 0;
1102 msg_putchar(' '); /* delete ':' */
1103 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001104#ifdef FEAT_SEARCH_EXTRA
1105 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001106 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 redraw_cmdline = TRUE;
1109 goto returncmd; /* back to cmd mode */
1110 }
1111 goto cmdline_changed;
1112
1113 case K_INS:
1114 case K_KINS:
1115#ifdef FEAT_FKMAP
1116 /* if Farsi mode set, we are in reverse insert mode -
1117 Do not change the mode */
1118 if (cmd_fkmap)
1119 beep_flush();
1120 else
1121#endif
1122 ccline.overstrike = !ccline.overstrike;
1123#ifdef CURSOR_SHAPE
1124 ui_cursor_shape(); /* may show different cursor shape */
1125#endif
1126 goto cmdline_not_changed;
1127
1128 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001129 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 {
1131 /* ":lmap" mappings exists, toggle use of mappings. */
1132 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001133#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 im_set_active(FALSE); /* Disable input method */
1135#endif
1136 if (b_im_ptr != NULL)
1137 {
1138 if (State & LANGMAP)
1139 *b_im_ptr = B_IMODE_LMAP;
1140 else
1141 *b_im_ptr = B_IMODE_NONE;
1142 }
1143 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001144#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 else
1146 {
1147 /* There are no ":lmap" mappings, toggle IM. When
1148 * 'imdisable' is set don't try getting the status, it's
1149 * always off. */
1150 if ((p_imdisable && b_im_ptr != NULL)
1151 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1152 {
1153 im_set_active(FALSE); /* Disable input method */
1154 if (b_im_ptr != NULL)
1155 *b_im_ptr = B_IMODE_NONE;
1156 }
1157 else
1158 {
1159 im_set_active(TRUE); /* Enable input method */
1160 if (b_im_ptr != NULL)
1161 *b_im_ptr = B_IMODE_IM;
1162 }
1163 }
1164#endif
1165 if (b_im_ptr != NULL)
1166 {
1167 if (b_im_ptr == &curbuf->b_p_iminsert)
1168 set_iminsert_global();
1169 else
1170 set_imsearch_global();
1171 }
1172#ifdef CURSOR_SHAPE
1173 ui_cursor_shape(); /* may show different cursor shape */
1174#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001175#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 /* Show/unshow value of 'keymap' in status lines later. */
1177 status_redraw_curbuf();
1178#endif
1179 goto cmdline_not_changed;
1180
1181/* case '@': only in very old vi */
1182 case Ctrl_U:
1183 /* delete all characters left of the cursor */
1184 j = ccline.cmdpos;
1185 ccline.cmdlen -= j;
1186 i = ccline.cmdpos = 0;
1187 while (i < ccline.cmdlen)
1188 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1189 /* Truncate at the end, required for multi-byte chars. */
1190 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001191#ifdef FEAT_SEARCH_EXTRA
1192 if (ccline.cmdlen == 0)
Bram Moolenaardda933d2016-09-03 21:04:58 +02001193 search_start = save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 redrawcmd();
1196 goto cmdline_changed;
1197
1198#ifdef FEAT_CLIPBOARD
1199 case Ctrl_Y:
1200 /* Copy the modeless selection, if there is one. */
1201 if (clip_star.state != SELECT_CLEARED)
1202 {
1203 if (clip_star.state == SELECT_DONE)
1204 clip_copy_modeless_selection(TRUE);
1205 goto cmdline_not_changed;
1206 }
1207 break;
1208#endif
1209
1210 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1211 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001212 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001213 * ":normal" runs out of characters. */
1214 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001215 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 goto cmdline_not_changed;
1217
1218 gotesc = TRUE; /* will free ccline.cmdbuff after
1219 putting it in history */
1220 goto returncmd; /* back to cmd mode */
1221
1222 case Ctrl_R: /* insert register */
1223#ifdef USE_ON_FLY_SCROLL
1224 dont_scroll = TRUE; /* disallow scrolling here */
1225#endif
1226 putcmdline('"', TRUE);
1227 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001228 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (i == Ctrl_O)
1230 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1231 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001232 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001233 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 --no_mapping;
1235#ifdef FEAT_EVAL
1236 /*
1237 * Insert the result of an expression.
1238 * Need to save the current command line, to be able to enter
1239 * a new one...
1240 */
1241 new_cmdpos = -1;
1242 if (c == '=')
1243 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1245 {
1246 beep_flush();
1247 c = ESC;
1248 }
1249 else
1250 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001251 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001253 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 }
1255 }
1256#endif
1257 if (c != ESC) /* use ESC to cancel inserting register */
1258 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001259 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001260
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001261#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001262 /* When there was a serious error abort getting the
1263 * command line. */
1264 if (aborting())
1265 {
1266 gotesc = TRUE; /* will free ccline.cmdbuff after
1267 putting it in history */
1268 goto returncmd; /* back to cmd mode */
1269 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 KeyTyped = FALSE; /* Don't do p_wc completion. */
1272#ifdef FEAT_EVAL
1273 if (new_cmdpos >= 0)
1274 {
1275 /* set_cmdline_pos() was used */
1276 if (new_cmdpos > ccline.cmdlen)
1277 ccline.cmdpos = ccline.cmdlen;
1278 else
1279 ccline.cmdpos = new_cmdpos;
1280 }
1281#endif
1282 }
1283 redrawcmd();
1284 goto cmdline_changed;
1285
1286 case Ctrl_D:
1287 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1288 break; /* Use ^D as normal char instead */
1289
1290 redrawcmd();
1291 continue; /* don't do incremental search now */
1292
1293 case K_RIGHT:
1294 case K_S_RIGHT:
1295 case K_C_RIGHT:
1296 do
1297 {
1298 if (ccline.cmdpos >= ccline.cmdlen)
1299 break;
1300 i = cmdline_charsize(ccline.cmdpos);
1301 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1302 break;
1303 ccline.cmdspos += i;
1304#ifdef FEAT_MBYTE
1305 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001306 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 + ccline.cmdpos);
1308 else
1309#endif
1310 ++ccline.cmdpos;
1311 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001312 while ((c == K_S_RIGHT || c == K_C_RIGHT
1313 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1315#ifdef FEAT_MBYTE
1316 if (has_mbyte)
1317 set_cmdspos_cursor();
1318#endif
1319 goto cmdline_not_changed;
1320
1321 case K_LEFT:
1322 case K_S_LEFT:
1323 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001324 if (ccline.cmdpos == 0)
1325 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 do
1327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 --ccline.cmdpos;
1329#ifdef FEAT_MBYTE
1330 if (has_mbyte) /* move to first byte of char */
1331 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1332 ccline.cmdbuff + ccline.cmdpos);
1333#endif
1334 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1335 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001336 while (ccline.cmdpos > 0
1337 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001338 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1340#ifdef FEAT_MBYTE
1341 if (has_mbyte)
1342 set_cmdspos_cursor();
1343#endif
1344 goto cmdline_not_changed;
1345
1346 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001347 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001348 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349
Bram Moolenaar4770d092006-01-12 23:22:24 +00001350#ifdef FEAT_GUI_W32
1351 /* On Win32 ignore <M-F4>, we get it when closing the window was
1352 * cancelled. */
1353 case K_F4:
1354 if (mod_mask == MOD_MASK_ALT)
1355 {
1356 redrawcmd(); /* somehow the cmdline is cleared */
1357 goto cmdline_not_changed;
1358 }
1359 break;
1360#endif
1361
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef FEAT_MOUSE
1363 case K_MIDDLEDRAG:
1364 case K_MIDDLERELEASE:
1365 goto cmdline_not_changed; /* Ignore mouse */
1366
1367 case K_MIDDLEMOUSE:
1368# ifdef FEAT_GUI
1369 /* When GUI is active, also paste when 'mouse' is empty */
1370 if (!gui.in_use)
1371# endif
1372 if (!mouse_has(MOUSE_COMMAND))
1373 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001374# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001376 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001378# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001379 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 redrawcmd();
1381 goto cmdline_changed;
1382
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001383# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001385 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 redrawcmd();
1387 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001388# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389
1390 case K_LEFTDRAG:
1391 case K_LEFTRELEASE:
1392 case K_RIGHTDRAG:
1393 case K_RIGHTRELEASE:
1394 /* Ignore drag and release events when the button-down wasn't
1395 * seen before. */
1396 if (ignore_drag_release)
1397 goto cmdline_not_changed;
1398 /* FALLTHROUGH */
1399 case K_LEFTMOUSE:
1400 case K_RIGHTMOUSE:
1401 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1402 ignore_drag_release = TRUE;
1403 else
1404 ignore_drag_release = FALSE;
1405# ifdef FEAT_GUI
1406 /* When GUI is active, also move when 'mouse' is empty */
1407 if (!gui.in_use)
1408# endif
1409 if (!mouse_has(MOUSE_COMMAND))
1410 goto cmdline_not_changed; /* Ignore mouse */
1411# ifdef FEAT_CLIPBOARD
1412 if (mouse_row < cmdline_row && clip_star.available)
1413 {
1414 int button, is_click, is_drag;
1415
1416 /*
1417 * Handle modeless selection.
1418 */
1419 button = get_mouse_button(KEY2TERMCAP1(c),
1420 &is_click, &is_drag);
1421 if (mouse_model_popup() && button == MOUSE_LEFT
1422 && (mod_mask & MOD_MASK_SHIFT))
1423 {
1424 /* Translate shift-left to right button. */
1425 button = MOUSE_RIGHT;
1426 mod_mask &= ~MOD_MASK_SHIFT;
1427 }
1428 clip_modeless(button, is_click, is_drag);
1429 goto cmdline_not_changed;
1430 }
1431# endif
1432
1433 set_cmdspos();
1434 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1435 ++ccline.cmdpos)
1436 {
1437 i = cmdline_charsize(ccline.cmdpos);
1438 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1439 && mouse_col < ccline.cmdspos % Columns + i)
1440 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001441# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 if (has_mbyte)
1443 {
1444 /* Count ">" for double-wide char that doesn't fit. */
1445 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001446 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 + ccline.cmdpos) - 1;
1448 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001449# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 ccline.cmdspos += i;
1451 }
1452 goto cmdline_not_changed;
1453
1454 /* Mouse scroll wheel: ignored here */
1455 case K_MOUSEDOWN:
1456 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001457 case K_MOUSELEFT:
1458 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 /* Alternate buttons ignored here */
1460 case K_X1MOUSE:
1461 case K_X1DRAG:
1462 case K_X1RELEASE:
1463 case K_X2MOUSE:
1464 case K_X2DRAG:
1465 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001466 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 goto cmdline_not_changed;
1468
1469#endif /* FEAT_MOUSE */
1470
1471#ifdef FEAT_GUI
1472 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1473 case K_LEFTRELEASE_NM:
1474 goto cmdline_not_changed;
1475
1476 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001477 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 {
1479 gui_do_scroll();
1480 redrawcmd();
1481 }
1482 goto cmdline_not_changed;
1483
1484 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001485 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001487 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 redrawcmd();
1489 }
1490 goto cmdline_not_changed;
1491#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001492#ifdef FEAT_GUI_TABLINE
1493 case K_TABLINE:
1494 case K_TABMENU:
1495 /* Don't want to change any tabs here. Make sure the same tab
1496 * is still selected. */
1497 if (gui_use_tabline())
1498 gui_mch_set_curtab(tabpage_index(curtab));
1499 goto cmdline_not_changed;
1500#endif
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 case K_SELECT: /* end of Select mode mapping - ignore */
1503 goto cmdline_not_changed;
1504
1505 case Ctrl_B: /* begin of command line */
1506 case K_HOME:
1507 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 case K_S_HOME:
1509 case K_C_HOME:
1510 ccline.cmdpos = 0;
1511 set_cmdspos();
1512 goto cmdline_not_changed;
1513
1514 case Ctrl_E: /* end of command line */
1515 case K_END:
1516 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 case K_S_END:
1518 case K_C_END:
1519 ccline.cmdpos = ccline.cmdlen;
1520 set_cmdspos_cursor();
1521 goto cmdline_not_changed;
1522
1523 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001524 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 break;
1526 goto cmdline_changed;
1527
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001528 case Ctrl_L:
1529#ifdef FEAT_SEARCH_EXTRA
1530 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1531 {
1532 /* Add a character from under the cursor for 'incsearch' */
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001533 if (did_incsearch)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001534 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001535 curwin->w_cursor = match_end;
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001536 if (!EQUAL_POS(curwin->w_cursor, search_start))
Bram Moolenaar93db9752006-11-21 10:29:45 +00001537 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001538 c = gchar_cursor();
1539 /* If 'ignorecase' and 'smartcase' are set and the
1540 * command line has no uppercase characters, convert
1541 * the character to lowercase */
1542 if (p_ic && p_scs
1543 && !pat_has_uppercase(ccline.cmdbuff))
1544 c = MB_TOLOWER(c);
1545 if (c != NUL)
Bram Moolenaar93db9752006-11-21 10:29:45 +00001546 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001547 if (c == firstc || vim_strchr((char_u *)(
Bram Moolenaara693d052017-06-29 22:23:06 +02001548 p_magic ? "\\~^$.*[" : "\\^$"), c)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001549 != NULL)
1550 {
1551 /* put a backslash before special
1552 * characters */
1553 stuffcharReadbuff(c);
1554 c = '\\';
1555 }
1556 break;
Bram Moolenaar93db9752006-11-21 10:29:45 +00001557 }
Bram Moolenaar93db9752006-11-21 10:29:45 +00001558 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001559 }
1560 goto cmdline_not_changed;
1561 }
1562#endif
1563
1564 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001565 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 break;
1567 goto cmdline_changed;
1568
1569 case Ctrl_N: /* next match */
1570 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001571 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001573 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1574 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001576 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001579 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 case K_UP:
1581 case K_DOWN:
1582 case K_S_UP:
1583 case K_S_DOWN:
1584 case K_PAGEUP:
1585 case K_KPAGEUP:
1586 case K_PAGEDOWN:
1587 case K_KPAGEDOWN:
1588 if (hislen == 0 || firstc == NUL) /* no history */
1589 goto cmdline_not_changed;
1590
1591 i = hiscnt;
1592
1593 /* save current command string so it can be restored later */
1594 if (lookfor == NULL)
1595 {
1596 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1597 goto cmdline_not_changed;
1598 lookfor[ccline.cmdpos] = NUL;
1599 }
1600
1601 j = (int)STRLEN(lookfor);
1602 for (;;)
1603 {
1604 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001605 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001606 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 {
1608 if (hiscnt == hislen) /* first time */
1609 hiscnt = hisidx[histype];
1610 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1611 hiscnt = hislen - 1;
1612 else if (hiscnt != hisidx[histype] + 1)
1613 --hiscnt;
1614 else /* at top of list */
1615 {
1616 hiscnt = i;
1617 break;
1618 }
1619 }
1620 else /* one step forwards */
1621 {
1622 /* on last entry, clear the line */
1623 if (hiscnt == hisidx[histype])
1624 {
1625 hiscnt = hislen;
1626 break;
1627 }
1628
1629 /* not on a history line, nothing to do */
1630 if (hiscnt == hislen)
1631 break;
1632 if (hiscnt == hislen - 1) /* wrap around */
1633 hiscnt = 0;
1634 else
1635 ++hiscnt;
1636 }
1637 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1638 {
1639 hiscnt = i;
1640 break;
1641 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001642 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001643 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 || STRNCMP(history[histype][hiscnt].hisstr,
1645 lookfor, (size_t)j) == 0)
1646 break;
1647 }
1648
1649 if (hiscnt != i) /* jumped to other entry */
1650 {
1651 char_u *p;
1652 int len;
1653 int old_firstc;
1654
1655 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001656 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (hiscnt == hislen)
1658 p = lookfor; /* back to the old one */
1659 else
1660 p = history[histype][hiscnt].hisstr;
1661
1662 if (histype == HIST_SEARCH
1663 && p != lookfor
1664 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1665 {
1666 /* Correct for the separator character used when
1667 * adding the history entry vs the one used now.
1668 * First loop: count length.
1669 * Second loop: copy the characters. */
1670 for (i = 0; i <= 1; ++i)
1671 {
1672 len = 0;
1673 for (j = 0; p[j] != NUL; ++j)
1674 {
1675 /* Replace old sep with new sep, unless it is
1676 * escaped. */
1677 if (p[j] == old_firstc
1678 && (j == 0 || p[j - 1] != '\\'))
1679 {
1680 if (i > 0)
1681 ccline.cmdbuff[len] = firstc;
1682 }
1683 else
1684 {
1685 /* Escape new sep, unless it is already
1686 * escaped. */
1687 if (p[j] == firstc
1688 && (j == 0 || p[j - 1] != '\\'))
1689 {
1690 if (i > 0)
1691 ccline.cmdbuff[len] = '\\';
1692 ++len;
1693 }
1694 if (i > 0)
1695 ccline.cmdbuff[len] = p[j];
1696 }
1697 ++len;
1698 }
1699 if (i == 0)
1700 {
1701 alloc_cmdbuff(len);
1702 if (ccline.cmdbuff == NULL)
1703 goto returncmd;
1704 }
1705 }
1706 ccline.cmdbuff[len] = NUL;
1707 }
1708 else
1709 {
1710 alloc_cmdbuff((int)STRLEN(p));
1711 if (ccline.cmdbuff == NULL)
1712 goto returncmd;
1713 STRCPY(ccline.cmdbuff, p);
1714 }
1715
1716 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
1717 redrawcmd();
1718 goto cmdline_changed;
1719 }
1720 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02001721#endif
1722 goto cmdline_not_changed;
1723
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001724#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02001725 case Ctrl_G: /* next match */
1726 case Ctrl_T: /* previous match */
Bram Moolenaar11956692016-08-27 16:26:56 +02001727 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1728 {
1729 pos_T t;
Bram Moolenaard0480092017-11-16 22:20:39 +01001730 char_u *pat;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001731 int search_flags = SEARCH_NOOF;
Bram Moolenaar11956692016-08-27 16:26:56 +02001732
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001733 if (ccline.cmdlen == 0)
1734 goto cmdline_not_changed;
1735
Bram Moolenaard0480092017-11-16 22:20:39 +01001736 if (firstc == ccline.cmdbuff[0])
1737 pat = last_search_pattern();
1738 else
1739 pat = ccline.cmdbuff;
1740
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001741 save_last_search_pattern();
Bram Moolenaar11956692016-08-27 16:26:56 +02001742 cursor_off();
1743 out_flush();
1744 if (c == Ctrl_G)
1745 {
1746 t = match_end;
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001747 if (LT_POS(match_start, match_end))
1748 /* start searching at the end of the match
1749 * not at the beginning of the next column */
1750 (void)decl(&t);
Bram Moolenaar11956692016-08-27 16:26:56 +02001751 search_flags += SEARCH_COL;
1752 }
1753 else
1754 t = match_start;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001755 if (!p_hls)
1756 search_flags += SEARCH_KEEP;
Bram Moolenaar11956692016-08-27 16:26:56 +02001757 ++emsg_off;
1758 i = searchit(curwin, curbuf, &t,
1759 c == Ctrl_G ? FORWARD : BACKWARD,
Bram Moolenaard0480092017-11-16 22:20:39 +01001760 pat, count, search_flags,
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001761 RE_SEARCH, 0, NULL, NULL);
Bram Moolenaar11956692016-08-27 16:26:56 +02001762 --emsg_off;
1763 if (i)
1764 {
Bram Moolenaardda933d2016-09-03 21:04:58 +02001765 search_start = match_start;
Bram Moolenaar11956692016-08-27 16:26:56 +02001766 match_end = t;
1767 match_start = t;
1768 if (c == Ctrl_T && firstc == '/')
1769 {
1770 /* move just before the current match, so that
1771 * when nv_search finishes the cursor will be
1772 * put back on the match */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001773 search_start = t;
1774 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001775 }
Bram Moolenaarda5116d2017-07-01 23:11:17 +02001776 else if (c == Ctrl_G && firstc == '?')
1777 {
1778 /* move just after the current match, so that
1779 * when nv_search finishes the cursor will be
1780 * put back on the match */
1781 search_start = t;
1782 (void)incl(&search_start);
1783 }
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001784 if (LT_POS(t, search_start) && c == Ctrl_G)
Bram Moolenaar11956692016-08-27 16:26:56 +02001785 {
1786 /* wrap around */
Bram Moolenaardda933d2016-09-03 21:04:58 +02001787 search_start = t;
Bram Moolenaar11956692016-08-27 16:26:56 +02001788 if (firstc == '?')
Bram Moolenaardda933d2016-09-03 21:04:58 +02001789 (void)incl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001790 else
Bram Moolenaardda933d2016-09-03 21:04:58 +02001791 (void)decl(&search_start);
Bram Moolenaar11956692016-08-27 16:26:56 +02001792 }
1793
1794 set_search_match(&match_end);
1795 curwin->w_cursor = match_start;
1796 changed_cline_bef_curs();
1797 update_topline();
1798 validate_cursor();
1799 highlight_match = TRUE;
1800 old_curswant = curwin->w_curswant;
1801 old_leftcol = curwin->w_leftcol;
1802 old_topline = curwin->w_topline;
1803# ifdef FEAT_DIFF
1804 old_topfill = curwin->w_topfill;
1805# endif
1806 old_botline = curwin->w_botline;
1807 update_screen(NOT_VALID);
1808 redrawcmdline();
1809 }
1810 else
1811 vim_beep(BO_ERROR);
Bram Moolenaara1d5c152017-12-16 19:05:22 +01001812 restore_last_search_pattern();
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001813 goto cmdline_not_changed;
Bram Moolenaar11956692016-08-27 16:26:56 +02001814 }
Bram Moolenaar349e7d92016-09-03 20:04:34 +02001815 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816#endif
1817
1818 case Ctrl_V:
1819 case Ctrl_Q:
1820#ifdef FEAT_MOUSE
1821 ignore_drag_release = TRUE;
1822#endif
1823 putcmdline('^', TRUE);
1824 c = get_literal(); /* get next (two) character(s) */
1825 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001826 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#ifdef FEAT_MBYTE
1828 /* may need to remove ^ when composing char was typed */
1829 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
1830 {
1831 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
1832 msg_putchar(' ');
1833 cursorcmd();
1834 }
1835#endif
1836 break;
1837
1838#ifdef FEAT_DIGRAPHS
1839 case Ctrl_K:
1840#ifdef FEAT_MOUSE
1841 ignore_drag_release = TRUE;
1842#endif
1843 putcmdline('?', TRUE);
1844#ifdef USE_ON_FLY_SCROLL
1845 dont_scroll = TRUE; /* disallow scrolling here */
1846#endif
1847 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02001848 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (c != NUL)
1850 break;
1851
1852 redrawcmd();
1853 goto cmdline_not_changed;
1854#endif /* FEAT_DIGRAPHS */
1855
1856#ifdef FEAT_RIGHTLEFT
1857 case Ctrl__: /* CTRL-_: switch language mode */
1858 if (!p_ari)
1859 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001860# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 if (p_altkeymap)
1862 {
1863 cmd_fkmap = !cmd_fkmap;
1864 if (cmd_fkmap) /* in Farsi always in Insert mode */
1865 ccline.overstrike = FALSE;
1866 }
1867 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 cmd_hkmap = !cmd_hkmap;
1870 goto cmdline_not_changed;
1871#endif
1872
Bram Moolenaarabbc4482017-01-24 15:57:55 +01001873 case K_PS:
1874 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
1875 goto cmdline_changed;
1876
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 default:
1878#ifdef UNIX
1879 if (c == intr_char)
1880 {
1881 gotesc = TRUE; /* will free ccline.cmdbuff after
1882 putting it in history */
1883 goto returncmd; /* back to Normal mode */
1884 }
1885#endif
1886 /*
1887 * Normal character with no special meaning. Just set mod_mask
1888 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
1889 * the string <S-Space>. This should only happen after ^V.
1890 */
1891 if (!IS_SPECIAL(c))
1892 mod_mask = 0x0;
1893 break;
1894 }
1895 /*
1896 * End of switch on command line character.
1897 * We come here if we have a normal character.
1898 */
1899
Bram Moolenaarede3e632013-06-23 16:16:19 +02001900 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#ifdef FEAT_MBYTE
1902 /* Add ABBR_OFF for characters above 0x100, this is
1903 * what check_abbr() expects. */
1904 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1905#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02001906 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 goto cmdline_changed;
1908
1909 /*
1910 * put the character in the command line
1911 */
1912 if (IS_SPECIAL(c) || mod_mask != 0)
1913 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
1914 else
1915 {
1916#ifdef FEAT_MBYTE
1917 if (has_mbyte)
1918 {
1919 j = (*mb_char2bytes)(c, IObuff);
1920 IObuff[j] = NUL; /* exclude composing chars */
1921 put_on_cmdline(IObuff, j, TRUE);
1922 }
1923 else
1924#endif
1925 {
1926 IObuff[0] = c;
1927 put_on_cmdline(IObuff, 1, TRUE);
1928 }
1929 }
1930 goto cmdline_changed;
1931
1932/*
1933 * This part implements incremental searches for "/" and "?"
1934 * Jump to cmdline_not_changed when a character has been read but the command
1935 * line did not change. Then we only search and redraw if something changed in
1936 * the past.
1937 * Jump to cmdline_changed when the command line did change.
1938 * (Sorry for the goto's, I know it is ugly).
1939 */
1940cmdline_not_changed:
1941#ifdef FEAT_SEARCH_EXTRA
1942 if (!incsearch_postponed)
1943 continue;
1944#endif
1945
1946cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01001947 /* Trigger CmdlineChanged autocommands. */
1948 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01001949
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950#ifdef FEAT_SEARCH_EXTRA
1951 /*
1952 * 'incsearch' highlighting.
1953 */
1954 if (p_is && !cmd_silent && (firstc == '/' || firstc == '?'))
1955 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001956 pos_T end_pos;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001957#ifdef FEAT_RELTIME
1958 proftime_T tm;
1959#endif
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001960
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 /* if there is a character waiting, search and redraw later */
1962 if (char_avail())
1963 {
1964 incsearch_postponed = TRUE;
1965 continue;
1966 }
1967 incsearch_postponed = FALSE;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001968 curwin->w_cursor = search_start; /* start at old position */
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001969 save_last_search_pattern();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970
1971 /* If there is no command line, don't do anything */
1972 if (ccline.cmdlen == 0)
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001973 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 i = 0;
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001975 SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01001976 redraw_all_later(SOME_VALID);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 else
1979 {
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001980 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 cursor_off(); /* so the user knows we're busy */
1982 out_flush();
1983 ++emsg_off; /* So it doesn't beep if bad expr */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001984#ifdef FEAT_RELTIME
1985 /* Set the time limit to half a second. */
1986 profile_setlimit(500L, &tm);
1987#endif
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001988 if (!p_hls)
1989 search_flags += SEARCH_KEEP;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 i = do_search(NULL, firstc, ccline.cmdbuff, count,
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01001991 search_flags,
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001992#ifdef FEAT_RELTIME
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001993 &tm, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001994#else
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02001995 NULL, NULL
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001996#endif
1997 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 --emsg_off;
1999 /* if interrupted while searching, behave like it failed */
2000 if (got_int)
2001 {
2002 (void)vpeekc(); /* remove <C-C> from input stream */
2003 got_int = FALSE; /* don't abandon the command line */
2004 i = 0;
2005 }
2006 else if (char_avail())
2007 /* cancelled searching because a char was typed */
2008 incsearch_postponed = TRUE;
2009 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002010 if (i != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 highlight_match = TRUE; /* highlight position */
2012 else
2013 highlight_match = FALSE; /* remove highlight */
2014
2015 /* first restore the old curwin values, so the screen is
2016 * positioned in the same way as the actual search command */
2017 curwin->w_leftcol = old_leftcol;
2018 curwin->w_topline = old_topline;
2019# ifdef FEAT_DIFF
2020 curwin->w_topfill = old_topfill;
2021# endif
2022 curwin->w_botline = old_botline;
2023 changed_cline_bef_curs();
2024 update_topline();
2025
2026 if (i != 0)
2027 {
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002028 pos_T save_pos = curwin->w_cursor;
2029
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002030 match_start = curwin->w_cursor;
2031 set_search_match(&curwin->w_cursor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 validate_cursor();
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002033 end_pos = curwin->w_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002034 match_end = end_pos;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002035 curwin->w_cursor = save_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 }
Bram Moolenaardb552d602006-03-23 22:59:57 +00002037 else
2038 end_pos = curwin->w_cursor; /* shutup gcc 4 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002039
Bram Moolenaar66216052017-12-16 16:33:44 +01002040 /* Disable 'hlsearch' highlighting if the pattern matches
2041 * everything. Avoids a flash when typing "foo\|". */
2042 if (empty_pattern(ccline.cmdbuff))
2043 SET_NO_HLSEARCH(TRUE);
2044
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 validate_cursor();
Bram Moolenaar27a23192006-09-14 09:27:26 +00002046 /* May redraw the status line to show the cursor position. */
2047 if (p_ru && curwin->w_status_height > 0)
2048 curwin->w_redr_status = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002050 save_cmdline(&save_ccline);
Bram Moolenaar600dddc2006-03-12 22:05:10 +00002051 update_screen(SOME_VALID);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002052 restore_cmdline(&save_ccline);
Bram Moolenaar2e51d9a2017-10-29 16:40:30 +01002053 restore_last_search_pattern();
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00002054
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002055 /* Leave it at the end to make CTRL-R CTRL-W work. */
2056 if (i != 0)
2057 curwin->w_cursor = end_pos;
2058
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 msg_starthere();
2060 redrawcmdline();
2061 did_incsearch = TRUE;
2062 }
2063#else /* FEAT_SEARCH_EXTRA */
2064 ;
2065#endif
2066
2067#ifdef FEAT_RIGHTLEFT
2068 if (cmdmsg_rl
2069# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002070 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071# endif
2072 )
2073 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002074 * right-left typing. Not efficient, but it works.
2075 * Do it only when there are no characters left to read
2076 * to avoid useless intermediate redraws. */
2077 if (vpeekc() == NUL)
2078 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079#endif
2080 }
2081
2082returncmd:
2083
2084#ifdef FEAT_RIGHTLEFT
2085 cmdmsg_rl = FALSE;
2086#endif
2087
2088#ifdef FEAT_FKMAP
2089 cmd_fkmap = 0;
2090#endif
2091
2092 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002093 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094
2095#ifdef FEAT_SEARCH_EXTRA
2096 if (did_incsearch)
2097 {
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02002098 if (gotesc)
Bram Moolenaardda933d2016-09-03 21:04:58 +02002099 curwin->w_cursor = save_cursor;
2100 else
2101 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002102 if (!EQUAL_POS(save_cursor, search_start))
Bram Moolenaardda933d2016-09-03 21:04:58 +02002103 {
2104 /* put the '" mark at the original position */
2105 curwin->w_cursor = save_cursor;
2106 setpcmark();
2107 }
2108 curwin->w_cursor = search_start;
2109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 curwin->w_curswant = old_curswant;
2111 curwin->w_leftcol = old_leftcol;
2112 curwin->w_topline = old_topline;
2113# ifdef FEAT_DIFF
2114 curwin->w_topfill = old_topfill;
2115# endif
2116 curwin->w_botline = old_botline;
2117 highlight_match = FALSE;
2118 validate_cursor(); /* needed for TAB */
Bram Moolenaarf8f8b2e2017-11-02 19:08:48 +01002119 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121#endif
2122
2123 if (ccline.cmdbuff != NULL)
2124 {
2125 /*
2126 * Put line in history buffer (":" and "=" only when it was typed).
2127 */
2128#ifdef FEAT_CMDHIST
2129 if (ccline.cmdlen && firstc != NUL
2130 && (some_key_typed || histype == HIST_SEARCH))
2131 {
2132 add_to_history(histype, ccline.cmdbuff, TRUE,
2133 histype == HIST_SEARCH ? firstc : NUL);
2134 if (firstc == ':')
2135 {
2136 vim_free(new_last_cmdline);
2137 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2138 }
2139 }
2140#endif
2141
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002142 if (gotesc)
2143 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 }
2145
2146 /*
2147 * If the screen was shifted up, redraw the whole screen (later).
2148 * If the line is too long, clear it, so ruler and shown command do
2149 * not get printed in the middle of it.
2150 */
2151 msg_check();
2152 msg_scroll = save_msg_scroll;
2153 redir_off = FALSE;
2154
2155 /* When the command line was typed, no need for a wait-return prompt. */
2156 if (some_key_typed)
2157 need_wait_return = FALSE;
2158
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002159 /* Trigger CmdlineLeave autocommands. */
2160 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002163#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2165 im_save_status(b_im_ptr);
2166 im_set_active(FALSE);
2167#endif
2168#ifdef FEAT_MOUSE
2169 setmouse();
2170#endif
2171#ifdef CURSOR_SHAPE
2172 ui_cursor_shape(); /* may show different cursor shape */
2173#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002174 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002176 {
2177 char_u *p = ccline.cmdbuff;
2178
2179 /* Make ccline empty, getcmdline() may try to use it. */
2180 ccline.cmdbuff = NULL;
2181 return p;
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183}
2184
2185#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2186/*
2187 * Get a command line with a prompt.
2188 * This is prepared to be called recursively from getcmdline() (e.g. by
2189 * f_input() when evaluating an expression from CTRL-R =).
2190 * Returns the command line in allocated memory, or NULL.
2191 */
2192 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002193getcmdline_prompt(
2194 int firstc,
2195 char_u *prompt, /* command line prompt */
2196 int attr, /* attributes for prompt */
2197 int xp_context, /* type of expansion */
2198 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199{
2200 char_u *s;
2201 struct cmdline_info save_ccline;
2202 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002203 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002205 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 ccline.cmdprompt = prompt;
2207 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002208# ifdef FEAT_EVAL
2209 ccline.xp_context = xp_context;
2210 ccline.xp_arg = xp_arg;
2211 ccline.input_fn = (firstc == '@');
2212# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002213 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002215 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002216 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002217 /* Restore msg_col, the prompt from input() may have changed it.
2218 * But only if called recursively and the commandline is therefore being
2219 * restored to an old one; if not, the input() prompt stays on the screen,
2220 * so we need its modified msg_col left intact. */
2221 if (ccline.cmdbuff != NULL)
2222 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223
2224 return s;
2225}
2226#endif
2227
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002228/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002229 * Return TRUE when the text must not be changed and we can't switch to
2230 * another window or buffer. Used when editing the command line, evaluating
2231 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002232 */
2233 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002234text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002235{
2236#ifdef FEAT_CMDWIN
2237 if (cmdwin_type != 0)
2238 return TRUE;
2239#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002240 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002241}
2242
2243/*
2244 * Give an error message for a command that isn't allowed while the cmdline
2245 * window is open or editing the cmdline in another way.
2246 */
2247 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002248text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002249{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002250 EMSG(_(get_text_locked_msg()));
2251}
2252
2253 char_u *
2254get_text_locked_msg(void)
2255{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002256#ifdef FEAT_CMDWIN
2257 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002258 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002259#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002260 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002261}
2262
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002263/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002264 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2265 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002266 */
2267 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002268curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002269{
2270 if (curbuf_lock > 0)
2271 {
2272 EMSG(_("E788: Not allowed to edit another buffer now"));
2273 return TRUE;
2274 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002275 return allbuf_locked();
2276}
2277
2278/*
2279 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2280 * message.
2281 */
2282 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002283allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002284{
2285 if (allbuf_lock > 0)
2286 {
2287 EMSG(_("E811: Not allowed to change buffer information now"));
2288 return TRUE;
2289 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002290 return FALSE;
2291}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002292
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002294cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
2296#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2297 if (cmdline_star > 0) /* showing '*', always 1 position */
2298 return 1;
2299#endif
2300 return ptr2cells(ccline.cmdbuff + idx);
2301}
2302
2303/*
2304 * Compute the offset of the cursor on the command line for the prompt and
2305 * indent.
2306 */
2307 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002308set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002310 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 ccline.cmdspos = 1 + ccline.cmdindent;
2312 else
2313 ccline.cmdspos = 0 + ccline.cmdindent;
2314}
2315
2316/*
2317 * Compute the screen position for the cursor on the command line.
2318 */
2319 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002320set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321{
2322 int i, m, c;
2323
2324 set_cmdspos();
2325 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002328 if (m < 0) /* overflow, Columns or Rows at weird value */
2329 m = MAXCOL;
2330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 else
2332 m = MAXCOL;
2333 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2334 {
2335 c = cmdline_charsize(i);
2336#ifdef FEAT_MBYTE
2337 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2338 if (has_mbyte)
2339 correct_cmdspos(i, c);
2340#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002341 /* If the cmdline doesn't fit, show cursor on last visible char.
2342 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if ((ccline.cmdspos += c) >= m)
2344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 ccline.cmdspos -= c;
2346 break;
2347 }
2348#ifdef FEAT_MBYTE
2349 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002350 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#endif
2352 }
2353}
2354
2355#ifdef FEAT_MBYTE
2356/*
2357 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2358 * character that doesn't fit, so that a ">" must be displayed.
2359 */
2360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002361correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002363 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2365 && ccline.cmdspos % Columns + cells > Columns)
2366 ccline.cmdspos++;
2367}
2368#endif
2369
2370/*
2371 * Get an Ex command line for the ":" command.
2372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002374getexline(
2375 int c, /* normally ':', NUL for ":append" */
2376 void *cookie UNUSED,
2377 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 /* When executing a register, remove ':' that's in front of each line. */
2380 if (exec_from_reg && vpeekc() == ':')
2381 (void)vgetc();
2382 return getcmdline(c, 1L, indent);
2383}
2384
2385/*
2386 * Get an Ex command line for Ex mode.
2387 * In Ex mode we only use the OS supplied line editing features and no
2388 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002389 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002392getexmodeline(
2393 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002394 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002395 void *cookie UNUSED,
2396 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002398 garray_T line_ga;
2399 char_u *pend;
2400 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002401 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002402 int escaped = FALSE; /* CTRL-V typed */
2403 int vcol = 0;
2404 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002405 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002406 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 /* Switch cursor on now. This avoids that it happens after the "\n", which
2409 * confuses the system function that computes tabstops. */
2410 cursor_on();
2411
2412 /* always start in column 0; write a newline if necessary */
2413 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002414 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002416 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002418 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002419 if (p_prompt)
2420 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 while (indent-- > 0)
2422 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
2426 ga_init2(&line_ga, 1, 30);
2427
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002428 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002429 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002430 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002431 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002432 while (indent >= 8)
2433 {
2434 ga_append(&line_ga, TAB);
2435 msg_puts((char_u *)" ");
2436 indent -= 8;
2437 }
2438 while (indent-- > 0)
2439 {
2440 ga_append(&line_ga, ' ');
2441 msg_putchar(' ');
2442 }
2443 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002444 ++no_mapping;
2445 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 /*
2448 * Get the line, one character at a time.
2449 */
2450 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002451 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002453 long sw;
2454 char_u *s;
2455
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (ga_grow(&line_ga, 40) == FAIL)
2457 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
Bram Moolenaarba748c82017-02-26 14:00:07 +01002459 /*
2460 * Get one character at a time.
2461 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002462 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002463
2464 /* Check for a ":normal" command and no more characters left. */
2465 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2466 c1 = '\n';
2467 else
2468 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002471 * Handle line editing.
2472 * Previously this was left to the system, putting the terminal in
2473 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002475 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002477 msg_putchar('\n');
2478 break;
2479 }
2480
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002481 if (c1 == K_PS)
2482 {
2483 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2484 goto redraw;
2485 }
2486
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002487 if (!escaped)
2488 {
2489 /* CR typed means "enter", which is NL */
2490 if (c1 == '\r')
2491 c1 = '\n';
2492
2493 if (c1 == BS || c1 == K_BS
2494 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002496 if (line_ga.ga_len > 0)
2497 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002498#ifdef FEAT_MBYTE
2499 if (has_mbyte)
2500 {
2501 p = (char_u *)line_ga.ga_data;
2502 p[line_ga.ga_len] = NUL;
2503 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2504 line_ga.ga_len -= len;
2505 }
2506 else
2507#endif
2508 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002509 goto redraw;
2510 }
2511 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 }
2513
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002514 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002516 msg_col = startcol;
2517 msg_clr_eos();
2518 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002519 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002520 }
2521
2522 if (c1 == Ctrl_T)
2523 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002524 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002525 p = (char_u *)line_ga.ga_data;
2526 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002527 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002528 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002529add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002530 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002532 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002533 p = (char_u *)line_ga.ga_data;
2534 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002535 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2536 *s = ' ';
2537 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002539redraw:
2540 /* redraw the line */
2541 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002542 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002543 p = (char_u *)line_ga.ga_data;
2544 p[line_ga.ga_len] = NUL;
2545 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002547 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002549 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 msg_putchar(' ');
2552 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002553 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002557 len = MB_PTR2LEN(p);
2558 msg_outtrans_len(p, len);
2559 vcol += ptr2cells(p);
2560 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 }
2562 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002563 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002564 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002565 continue;
2566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 if (c1 == Ctrl_D)
2569 {
2570 /* Delete one shiftwidth. */
2571 p = (char_u *)line_ga.ga_data;
2572 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002574 if (prev_char == '^')
2575 ex_keep_indent = TRUE;
2576 indent = 0;
2577 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
2579 else
2580 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002581 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002582 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002583 if (indent > 0)
2584 {
2585 --indent;
2586 indent -= indent % get_sw_value(curbuf);
2587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002589 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002590 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002591 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002592 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2593 --line_ga.ga_len;
2594 }
2595 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002597
2598 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2599 {
2600 escaped = TRUE;
2601 continue;
2602 }
2603
2604 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2605 if (IS_SPECIAL(c1))
2606 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002608
2609 if (IS_SPECIAL(c1))
2610 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002611#ifdef FEAT_MBYTE
2612 if (has_mbyte)
2613 len = (*mb_char2bytes)(c1,
2614 (char_u *)line_ga.ga_data + line_ga.ga_len);
2615 else
2616#endif
2617 {
2618 len = 1;
2619 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2620 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002621 if (c1 == '\n')
2622 msg_putchar('\n');
2623 else if (c1 == TAB)
2624 {
2625 /* Don't use chartabsize(), 'ts' can be different */
2626 do
2627 {
2628 msg_putchar(' ');
2629 } while (++vcol % 8);
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002633 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002634 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002637 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002638 escaped = FALSE;
2639
2640 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002641 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002642
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002643 /* We are done when a NL is entered, but not when it comes after an
2644 * odd number of backslashes, that results in a NUL. */
2645 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002647 int bcount = 0;
2648
2649 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2650 ++bcount;
2651
2652 if (bcount > 0)
2653 {
2654 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2655 * "\NL", etc. */
2656 line_ga.ga_len -= (bcount + 1) / 2;
2657 pend -= (bcount + 1) / 2;
2658 pend[-1] = '\n';
2659 }
2660
2661 if ((bcount & 1) == 0)
2662 {
2663 --line_ga.ga_len;
2664 --pend;
2665 *pend = NUL;
2666 break;
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
2669 }
2670
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002671 --no_mapping;
2672 --allow_keys;
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 /* make following messages go to the next line */
2675 msg_didout = FALSE;
2676 msg_col = 0;
2677 if (msg_row < Rows - 1)
2678 ++msg_row;
2679 emsg_on_display = FALSE; /* don't want ui_delay() */
2680
2681 if (got_int)
2682 ga_clear(&line_ga);
2683
2684 return (char_u *)line_ga.ga_data;
2685}
2686
Bram Moolenaare344bea2005-09-01 20:46:49 +00002687# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2688 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689/*
2690 * Return TRUE if ccline.overstrike is on.
2691 */
2692 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002693cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 return ccline.overstrike;
2696}
2697
2698/*
2699 * Return TRUE if the cursor is at the end of the cmdline.
2700 */
2701 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002702cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
2704 return (ccline.cmdpos >= ccline.cmdlen);
2705}
2706#endif
2707
Bram Moolenaar9372a112005-12-06 19:59:18 +00002708#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709/*
2710 * Return the virtual column number at the current cursor position.
2711 * This is used by the IM code to obtain the start of the preedit string.
2712 */
2713 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002714cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
2716 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2717 return MAXCOL;
2718
2719# ifdef FEAT_MBYTE
2720 if (has_mbyte)
2721 {
2722 colnr_T col;
2723 int i = 0;
2724
2725 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002726 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 return col;
2729 }
2730 else
2731# endif
2732 return ccline.cmdpos;
2733}
2734#endif
2735
2736#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2737/*
2738 * If part of the command line is an IM preedit string, redraw it with
2739 * IM feedback attributes. The cursor position is restored after drawing.
2740 */
2741 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002742redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 if ((State & CMDLINE)
2745 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002746 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 && !p_imdisable
2748 && im_is_preediting())
2749 {
2750 int cmdpos = 0;
2751 int cmdspos;
2752 int old_row;
2753 int old_col;
2754 colnr_T col;
2755
2756 old_row = msg_row;
2757 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002758 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759
2760# ifdef FEAT_MBYTE
2761 if (has_mbyte)
2762 {
2763 for (col = 0; col < preedit_start_col
2764 && cmdpos < ccline.cmdlen; ++col)
2765 {
2766 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002767 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 }
2769 }
2770 else
2771# endif
2772 {
2773 cmdspos += preedit_start_col;
2774 cmdpos += preedit_start_col;
2775 }
2776
2777 msg_row = cmdline_row + (cmdspos / (int)Columns);
2778 msg_col = cmdspos % (int)Columns;
2779 if (msg_row >= Rows)
2780 msg_row = Rows - 1;
2781
2782 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2783 {
2784 int char_len;
2785 int char_attr;
2786
2787 char_attr = im_get_feedback_attr(col);
2788 if (char_attr < 0)
2789 break; /* end of preedit string */
2790
2791# ifdef FEAT_MBYTE
2792 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002793 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 else
2795# endif
2796 char_len = 1;
2797
2798 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2799 cmdpos += char_len;
2800 }
2801
2802 msg_row = old_row;
2803 msg_col = old_col;
2804 }
2805}
2806#endif /* FEAT_XIM && FEAT_GUI_GTK */
2807
2808/*
2809 * Allocate a new command line buffer.
2810 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2811 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2812 */
2813 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002814alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 /*
2817 * give some extra space to avoid having to allocate all the time
2818 */
2819 if (len < 80)
2820 len = 100;
2821 else
2822 len += 20;
2823
2824 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2825 ccline.cmdbufflen = len;
2826}
2827
2828/*
2829 * Re-allocate the command line to length len + something extra.
2830 * return FAIL for failure, OK otherwise
2831 */
2832 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002833realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834{
2835 char_u *p;
2836
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002837 if (len < ccline.cmdbufflen)
2838 return OK; /* no need to resize */
2839
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 p = ccline.cmdbuff;
2841 alloc_cmdbuff(len); /* will get some more */
2842 if (ccline.cmdbuff == NULL) /* out of memory */
2843 {
2844 ccline.cmdbuff = p; /* keep the old one */
2845 return FAIL;
2846 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002847 /* There isn't always a NUL after the command, but it may need to be
2848 * there, thus copy up to the NUL and add a NUL. */
2849 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2850 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002852
2853 if (ccline.xpc != NULL
2854 && ccline.xpc->xp_pattern != NULL
2855 && ccline.xpc->xp_context != EXPAND_NOTHING
2856 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2857 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002858 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002859
2860 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2861 * to point into the newly allocated memory. */
2862 if (i >= 0 && i <= ccline.cmdlen)
2863 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2864 }
2865
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 return OK;
2867}
2868
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002869#if defined(FEAT_ARABIC) || defined(PROTO)
2870static char_u *arshape_buf = NULL;
2871
2872# if defined(EXITFREE) || defined(PROTO)
2873 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002874free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002875{
2876 vim_free(arshape_buf);
2877}
2878# endif
2879#endif
2880
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881/*
2882 * Draw part of the cmdline at the current cursor position. But draw stars
2883 * when cmdline_star is TRUE.
2884 */
2885 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002886draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2889 int i;
2890
2891 if (cmdline_star > 0)
2892 for (i = 0; i < len; ++i)
2893 {
2894 msg_putchar('*');
2895# ifdef FEAT_MBYTE
2896 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002897 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898# endif
2899 }
2900 else
2901#endif
2902#ifdef FEAT_ARABIC
2903 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 static int buflen = 0;
2906 char_u *p;
2907 int j;
2908 int newlen = 0;
2909 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002910 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 int prev_c = 0;
2912 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002913 int u8c;
2914 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
2917 /*
2918 * Do arabic shaping into a temporary buffer. This is very
2919 * inefficient!
2920 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002921 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 {
2923 /* Re-allocate the buffer. We keep it around to avoid a lot of
2924 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002925 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002926 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002927 arshape_buf = alloc(buflen);
2928 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 return; /* out of memory */
2930 }
2931
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00002932 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
2933 {
2934 /* Prepend a space to draw the leading composing char on. */
2935 arshape_buf[0] = ' ';
2936 newlen = 1;
2937 }
2938
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 for (j = start; j < start + len; j += mb_l)
2940 {
2941 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002942 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002943 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 if (ARABIC_CHAR(u8c))
2945 {
2946 /* Do Arabic shaping. */
2947 if (cmdmsg_rl)
2948 {
2949 /* displaying from right to left */
2950 pc = prev_c;
2951 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002952 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 if (j + mb_l >= start + len)
2954 nc = NUL;
2955 else
2956 nc = utf_ptr2char(p + mb_l);
2957 }
2958 else
2959 {
2960 /* displaying from left to right */
2961 if (j + mb_l >= start + len)
2962 pc = NUL;
2963 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002964 {
2965 int pcc[MAX_MCO];
2966
2967 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002969 pc1 = pcc[0];
2970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 nc = prev_c;
2972 }
2973 prev_c = u8c;
2974
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002975 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002977 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002978 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002980 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
2981 if (u8cc[1] != 0)
2982 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002983 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 }
2985 }
2986 else
2987 {
2988 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002989 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 newlen += mb_l;
2991 }
2992 }
2993
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002994 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 }
2996 else
2997#endif
2998 msg_outtrans_len(ccline.cmdbuff + start, len);
2999}
3000
3001/*
3002 * Put a character on the command line. Shifts the following text to the
3003 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3004 * "c" must be printable (fit in one display cell)!
3005 */
3006 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003007putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008{
3009 if (cmd_silent)
3010 return;
3011 msg_no_more = TRUE;
3012 msg_putchar(c);
3013 if (shift)
3014 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3015 msg_no_more = FALSE;
3016 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003017 extra_char = c;
3018 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021/*
3022 * Undo a putcmdline(c, FALSE).
3023 */
3024 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 if (cmd_silent)
3028 return;
3029 msg_no_more = TRUE;
3030 if (ccline.cmdlen == ccline.cmdpos)
3031 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003032#ifdef FEAT_MBYTE
3033 else if (has_mbyte)
3034 draw_cmdline(ccline.cmdpos,
3035 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 else
3038 draw_cmdline(ccline.cmdpos, 1);
3039 msg_no_more = FALSE;
3040 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003041 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042}
3043
3044/*
3045 * Put the given string, of the given length, onto the command line.
3046 * If len is -1, then STRLEN() is used to calculate the length.
3047 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3048 * part will be redrawn, otherwise it will not. If this function is called
3049 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3050 * called afterwards.
3051 */
3052 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 int retval;
3056 int i;
3057 int m;
3058 int c;
3059
3060 if (len < 0)
3061 len = (int)STRLEN(str);
3062
3063 /* Check if ccline.cmdbuff needs to be longer */
3064 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003065 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 else
3067 retval = OK;
3068 if (retval == OK)
3069 {
3070 if (!ccline.overstrike)
3071 {
3072 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3073 ccline.cmdbuff + ccline.cmdpos,
3074 (size_t)(ccline.cmdlen - ccline.cmdpos));
3075 ccline.cmdlen += len;
3076 }
3077 else
3078 {
3079#ifdef FEAT_MBYTE
3080 if (has_mbyte)
3081 {
3082 /* Count nr of characters in the new string. */
3083 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003084 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 ++m;
3086 /* Count nr of bytes in cmdline that are overwritten by these
3087 * characters. */
3088 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003089 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 --m;
3091 if (i < ccline.cmdlen)
3092 {
3093 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3094 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3095 ccline.cmdlen += ccline.cmdpos + len - i;
3096 }
3097 else
3098 ccline.cmdlen = ccline.cmdpos + len;
3099 }
3100 else
3101#endif
3102 if (ccline.cmdpos + len > ccline.cmdlen)
3103 ccline.cmdlen = ccline.cmdpos + len;
3104 }
3105 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3106 ccline.cmdbuff[ccline.cmdlen] = NUL;
3107
3108#ifdef FEAT_MBYTE
3109 if (enc_utf8)
3110 {
3111 /* When the inserted text starts with a composing character,
3112 * backup to the character before it. There could be two of them.
3113 */
3114 i = 0;
3115 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3116 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3117 {
3118 i = (*mb_head_off)(ccline.cmdbuff,
3119 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3120 ccline.cmdpos -= i;
3121 len += i;
3122 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3123 }
3124# ifdef FEAT_ARABIC
3125 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3126 {
3127 /* Check the previous character for Arabic combining pair. */
3128 i = (*mb_head_off)(ccline.cmdbuff,
3129 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3130 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3131 + ccline.cmdpos - i), c))
3132 {
3133 ccline.cmdpos -= i;
3134 len += i;
3135 }
3136 else
3137 i = 0;
3138 }
3139# endif
3140 if (i != 0)
3141 {
3142 /* Also backup the cursor position. */
3143 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3144 ccline.cmdspos -= i;
3145 msg_col -= i;
3146 if (msg_col < 0)
3147 {
3148 msg_col += Columns;
3149 --msg_row;
3150 }
3151 }
3152 }
3153#endif
3154
3155 if (redraw && !cmd_silent)
3156 {
3157 msg_no_more = TRUE;
3158 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003159 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3161 /* Avoid clearing the rest of the line too often. */
3162 if (cmdline_row != i || ccline.overstrike)
3163 msg_clr_eos();
3164 msg_no_more = FALSE;
3165 }
3166#ifdef FEAT_FKMAP
3167 /*
3168 * If we are in Farsi command mode, the character input must be in
3169 * Insert mode. So do not advance the cmdpos.
3170 */
3171 if (!cmd_fkmap)
3172#endif
3173 {
3174 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003175 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003177 if (m < 0) /* overflow, Columns or Rows at weird value */
3178 m = MAXCOL;
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
3181 m = MAXCOL;
3182 for (i = 0; i < len; ++i)
3183 {
3184 c = cmdline_charsize(ccline.cmdpos);
3185#ifdef FEAT_MBYTE
3186 /* count ">" for a double-wide char that doesn't fit. */
3187 if (has_mbyte)
3188 correct_cmdspos(ccline.cmdpos, c);
3189#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003190 /* Stop cursor at the end of the screen, but do increment the
3191 * insert position, so that entering a very long command
3192 * works, even though you can't see it. */
3193 if (ccline.cmdspos + c < m)
3194 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195#ifdef FEAT_MBYTE
3196 if (has_mbyte)
3197 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003198 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (c > len - i - 1)
3200 c = len - i - 1;
3201 ccline.cmdpos += c;
3202 i += c;
3203 }
3204#endif
3205 ++ccline.cmdpos;
3206 }
3207 }
3208 }
3209 if (redraw)
3210 msg_check();
3211 return retval;
3212}
3213
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003214static struct cmdline_info prev_ccline;
3215static int prev_ccline_used = FALSE;
3216
3217/*
3218 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3219 * and overwrite it. But get_cmdline_str() may need it, thus make it
3220 * available globally in prev_ccline.
3221 */
3222 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003223save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003224{
3225 if (!prev_ccline_used)
3226 {
3227 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3228 prev_ccline_used = TRUE;
3229 }
3230 *ccp = prev_ccline;
3231 prev_ccline = ccline;
3232 ccline.cmdbuff = NULL;
3233 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003234 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003235}
3236
3237/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003238 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003239 */
3240 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003241restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003242{
3243 ccline = prev_ccline;
3244 prev_ccline = *ccp;
3245}
3246
Bram Moolenaar5a305422006-04-28 22:38:25 +00003247#if defined(FEAT_EVAL) || defined(PROTO)
3248/*
3249 * Save the command line into allocated memory. Returns a pointer to be
3250 * passed to restore_cmdline_alloc() later.
3251 * Returns NULL when failed.
3252 */
3253 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003254save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003255{
3256 struct cmdline_info *p;
3257
3258 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3259 if (p != NULL)
3260 save_cmdline(p);
3261 return (char_u *)p;
3262}
3263
3264/*
3265 * Restore the command line from the return value of save_cmdline_alloc().
3266 */
3267 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003268restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003269{
3270 if (p != NULL)
3271 {
3272 restore_cmdline((struct cmdline_info *)p);
3273 vim_free(p);
3274 }
3275}
3276#endif
3277
Bram Moolenaar8299df92004-07-10 09:47:34 +00003278/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003279 * Paste a yank register into the command line.
3280 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003281 * insert_reg() can't be used here, because special characters from the
3282 * register contents will be interpreted as commands.
3283 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003284 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003285 */
3286 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003287cmdline_paste(
3288 int regname,
3289 int literally, /* Insert text literally instead of "as typed" */
3290 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003291{
3292 long i;
3293 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003294 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003295 int allocated;
3296 struct cmdline_info save_ccline;
3297
3298 /* check for valid regname; also accept special characters for CTRL-R in
3299 * the command line */
3300 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
3301 && regname != Ctrl_A && !valid_yank_reg(regname, FALSE))
3302 return FAIL;
3303
3304 /* A register containing CTRL-R can cause an endless loop. Allow using
3305 * CTRL-C to break the loop. */
3306 line_breakcheck();
3307 if (got_int)
3308 return FAIL;
3309
3310#ifdef FEAT_CLIPBOARD
3311 regname = may_get_selection(regname);
3312#endif
3313
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003314 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003315 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003316 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003317 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003318 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003319 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003320 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003321
3322 if (i)
3323 {
3324 /* Got the value of a special register in "arg". */
3325 if (arg == NULL)
3326 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003327
3328 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3329 * part of the word. */
3330 p = arg;
3331 if (p_is && regname == Ctrl_W)
3332 {
3333 char_u *w;
3334 int len;
3335
3336 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003337 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003338 {
3339#ifdef FEAT_MBYTE
3340 if (has_mbyte)
3341 {
3342 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3343 if (!vim_iswordc(mb_ptr2char(w - len)))
3344 break;
3345 w -= len;
3346 }
3347 else
3348#endif
3349 {
3350 if (!vim_iswordc(w[-1]))
3351 break;
3352 --w;
3353 }
3354 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003355 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003356 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3357 p += len;
3358 }
3359
3360 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003361 if (allocated)
3362 vim_free(arg);
3363 return OK;
3364 }
3365
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003366 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003367}
3368
3369/*
3370 * Put a string on the command line.
3371 * When "literally" is TRUE, insert literally.
3372 * When "literally" is FALSE, insert as typed, but don't leave the command
3373 * line.
3374 */
3375 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003376cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003377{
3378 int c, cv;
3379
3380 if (literally)
3381 put_on_cmdline(s, -1, TRUE);
3382 else
3383 while (*s != NUL)
3384 {
3385 cv = *s;
3386 if (cv == Ctrl_V && s[1])
3387 ++s;
3388#ifdef FEAT_MBYTE
3389 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003390 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003391 else
3392#endif
3393 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003394 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3395 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003396#ifdef UNIX
3397 || c == intr_char
3398#endif
3399 || (c == Ctrl_BSL && *s == Ctrl_N))
3400 stuffcharReadbuff(Ctrl_V);
3401 stuffcharReadbuff(c);
3402 }
3403}
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405#ifdef FEAT_WILDMENU
3406/*
3407 * Delete characters on the command line, from "from" to the current
3408 * position.
3409 */
3410 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003411cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412{
3413 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3414 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3415 ccline.cmdlen -= ccline.cmdpos - from;
3416 ccline.cmdpos = from;
3417}
3418#endif
3419
3420/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003421 * This function is called when the screen size changes and with incremental
3422 * search and in other situations where the command line may have been
3423 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 */
3425 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003426redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003428 redrawcmdline_ex(TRUE);
3429}
3430
3431 void
3432redrawcmdline_ex(int do_compute_cmdrow)
3433{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 if (cmd_silent)
3435 return;
3436 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003437 if (do_compute_cmdrow)
3438 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 redrawcmd();
3440 cursorcmd();
3441}
3442
3443 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003444redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445{
3446 int i;
3447
3448 if (cmd_silent)
3449 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003450 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 msg_putchar(ccline.cmdfirstc);
3452 if (ccline.cmdprompt != NULL)
3453 {
3454 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3455 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3456 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003457 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 --ccline.cmdindent;
3459 }
3460 else
3461 for (i = ccline.cmdindent; i > 0; --i)
3462 msg_putchar(' ');
3463}
3464
3465/*
3466 * Redraw what is currently on the command line.
3467 */
3468 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003469redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 if (cmd_silent)
3472 return;
3473
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003474 /* when 'incsearch' is set there may be no command line while redrawing */
3475 if (ccline.cmdbuff == NULL)
3476 {
3477 windgoto(cmdline_row, 0);
3478 msg_clr_eos();
3479 return;
3480 }
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 msg_start();
3483 redrawcmdprompt();
3484
3485 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3486 msg_no_more = TRUE;
3487 draw_cmdline(0, ccline.cmdlen);
3488 msg_clr_eos();
3489 msg_no_more = FALSE;
3490
3491 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003492 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003493 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003494
3495 /*
3496 * An emsg() before may have set msg_scroll. This is used in normal mode,
3497 * in cmdline mode we can reset them now.
3498 */
3499 msg_scroll = FALSE; /* next message overwrites cmdline */
3500
3501 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3502 * in cmdline mode */
3503 skip_redraw = FALSE;
3504}
3505
3506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003509 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 cmdline_row = Rows - 1;
3511 else
3512 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003513 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514}
3515
3516 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003517cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518{
3519 if (cmd_silent)
3520 return;
3521
3522#ifdef FEAT_RIGHTLEFT
3523 if (cmdmsg_rl)
3524 {
3525 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3526 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3527 if (msg_row <= 0)
3528 msg_row = Rows - 1;
3529 }
3530 else
3531#endif
3532 {
3533 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3534 msg_col = ccline.cmdspos % (int)Columns;
3535 if (msg_row >= Rows)
3536 msg_row = Rows - 1;
3537 }
3538
3539 windgoto(msg_row, msg_col);
3540#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003541 if (p_imst == IM_ON_THE_SPOT)
3542 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543#endif
3544#ifdef MCH_CURSOR_SHAPE
3545 mch_update_cursor();
3546#endif
3547}
3548
3549 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003550gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 msg_start();
3553#ifdef FEAT_RIGHTLEFT
3554 if (cmdmsg_rl)
3555 msg_col = Columns - 1;
3556 else
3557#endif
3558 msg_col = 0; /* always start in column 0 */
3559 if (clr) /* clear the bottom line(s) */
3560 msg_clr_eos(); /* will reset clear_cmdline */
3561 windgoto(cmdline_row, 0);
3562}
3563
3564/*
3565 * Check the word in front of the cursor for an abbreviation.
3566 * Called when the non-id character "c" has been entered.
3567 * When an abbreviation is recognized it is removed from the text with
3568 * backspaces and the replacement string is inserted, followed by "c".
3569 */
3570 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003571ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572{
3573 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3574 return FALSE;
3575
3576 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0);
3577}
3578
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003579#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3580 static int
3581#ifdef __BORLANDC__
3582_RTLENTRYF
3583#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003584sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003585{
3586 char_u *p1 = *(char_u **)s1;
3587 char_u *p2 = *(char_u **)s2;
3588
3589 if (*p1 != '<' && *p2 == '<') return -1;
3590 if (*p1 == '<' && *p2 != '<') return 1;
3591 return STRCMP(p1, p2);
3592}
3593#endif
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595/*
3596 * Return FAIL if this is not an appropriate context in which to do
3597 * completion of anything, return OK if it is (even if there are no matches).
3598 * For the caller, this means that the character is just passed through like a
3599 * normal character (instead of being expanded). This allows :s/^I^D etc.
3600 */
3601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003602nextwild(
3603 expand_T *xp,
3604 int type,
3605 int options, /* extra options for ExpandOne() */
3606 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
3608 int i, j;
3609 char_u *p1;
3610 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 int difflen;
3612 int v;
3613
3614 if (xp->xp_numfiles == -1)
3615 {
3616 set_expand_context(xp);
3617 cmd_showtail = expand_showtail(xp);
3618 }
3619
3620 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3621 {
3622 beep_flush();
3623 return OK; /* Something illegal on command line */
3624 }
3625 if (xp->xp_context == EXPAND_NOTHING)
3626 {
3627 /* Caller can use the character as a normal char instead */
3628 return FAIL;
3629 }
3630
3631 MSG_PUTS("..."); /* show that we are busy */
3632 out_flush();
3633
3634 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003635 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 if (type == WILD_NEXT || type == WILD_PREV)
3638 {
3639 /*
3640 * Get next/previous match for a previous expanded pattern.
3641 */
3642 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3643 }
3644 else
3645 {
3646 /*
3647 * Translate string into pattern and expand it.
3648 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003649 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3650 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 p2 = NULL;
3652 else
3653 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003654 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003655 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3656 if (escape)
3657 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003658
3659 if (p_wic)
3660 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003661 p2 = ExpandOne(xp, p1,
3662 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003663 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003665 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (p2 != NULL && type == WILD_LONGEST)
3667 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003668 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (ccline.cmdbuff[i + j] == '*'
3670 || ccline.cmdbuff[i + j] == '?')
3671 break;
3672 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003673 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
3675 }
3676 }
3677
3678 if (p2 != NULL && !got_int)
3679 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003680 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003681 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003683 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 xp->xp_pattern = ccline.cmdbuff + i;
3685 }
3686 else
3687 v = OK;
3688 if (v == OK)
3689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003690 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3691 &ccline.cmdbuff[ccline.cmdpos],
3692 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3693 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 ccline.cmdlen += difflen;
3695 ccline.cmdpos += difflen;
3696 }
3697 }
3698 vim_free(p2);
3699
3700 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003701 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702
3703 /* When expanding a ":map" command and no matches are found, assume that
3704 * the key is supposed to be inserted literally */
3705 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3706 return FAIL;
3707
3708 if (xp->xp_numfiles <= 0 && p2 == NULL)
3709 beep_flush();
3710 else if (xp->xp_numfiles == 1)
3711 /* free expanded pattern */
3712 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3713
3714 return OK;
3715}
3716
3717/*
3718 * Do wildcard expansion on the string 'str'.
3719 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003720 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 * Return NULL for failure.
3722 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003723 * "orig" is the originally expanded string, copied to allocated memory. It
3724 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3725 * WILD_PREV "orig" should be NULL.
3726 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003727 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3728 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 *
3730 * mode = WILD_FREE: just free previously expanded matches
3731 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3732 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3733 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3734 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3735 * mode = WILD_ALL: return all matches concatenated
3736 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003737 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 *
3739 * options = WILD_LIST_NOTFOUND: list entries without a match
3740 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3741 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3742 * options = WILD_NO_BEEP: Don't beep for multiple matches
3743 * options = WILD_ADD_SLASH: add a slash after directory names
3744 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3745 * options = WILD_SILENT: don't print warning messages
3746 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003747 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 *
3749 * The variables xp->xp_context and xp->xp_backslash must have been set!
3750 */
3751 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003752ExpandOne(
3753 expand_T *xp,
3754 char_u *str,
3755 char_u *orig, /* allocated copy of original of expanded string */
3756 int options,
3757 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758{
3759 char_u *ss = NULL;
3760 static int findex;
3761 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003762 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 int i;
3764 long_u len;
3765 int non_suf_match; /* number without matching suffix */
3766
3767 /*
3768 * first handle the case of using an old match
3769 */
3770 if (mode == WILD_NEXT || mode == WILD_PREV)
3771 {
3772 if (xp->xp_numfiles > 0)
3773 {
3774 if (mode == WILD_PREV)
3775 {
3776 if (findex == -1)
3777 findex = xp->xp_numfiles;
3778 --findex;
3779 }
3780 else /* mode == WILD_NEXT */
3781 ++findex;
3782
3783 /*
3784 * When wrapping around, return the original string, set findex to
3785 * -1.
3786 */
3787 if (findex < 0)
3788 {
3789 if (orig_save == NULL)
3790 findex = xp->xp_numfiles - 1;
3791 else
3792 findex = -1;
3793 }
3794 if (findex >= xp->xp_numfiles)
3795 {
3796 if (orig_save == NULL)
3797 findex = 0;
3798 else
3799 findex = -1;
3800 }
3801#ifdef FEAT_WILDMENU
3802 if (p_wmnu)
3803 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3804 findex, cmd_showtail);
3805#endif
3806 if (findex == -1)
3807 return vim_strsave(orig_save);
3808 return vim_strsave(xp->xp_files[findex]);
3809 }
3810 else
3811 return NULL;
3812 }
3813
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003814 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3816 {
3817 FreeWild(xp->xp_numfiles, xp->xp_files);
3818 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003819 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 }
3821 findex = 0;
3822
3823 if (mode == WILD_FREE) /* only release file name */
3824 return NULL;
3825
3826 if (xp->xp_numfiles == -1)
3827 {
3828 vim_free(orig_save);
3829 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003830 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 /*
3833 * Do the expansion.
3834 */
3835 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3836 options) == FAIL)
3837 {
3838#ifdef FNAME_ILLEGAL
3839 /* Illegal file name has been silently skipped. But when there
3840 * are wildcards, the real problem is that there was no match,
3841 * causing the pattern to be added, which has illegal characters.
3842 */
3843 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3844 EMSG2(_(e_nomatch2), str);
3845#endif
3846 }
3847 else if (xp->xp_numfiles == 0)
3848 {
3849 if (!(options & WILD_SILENT))
3850 EMSG2(_(e_nomatch2), str);
3851 }
3852 else
3853 {
3854 /* Escape the matches for use on the command line. */
3855 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3856
3857 /*
3858 * Check for matching suffixes in file names.
3859 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003860 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3861 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 {
3863 if (xp->xp_numfiles)
3864 non_suf_match = xp->xp_numfiles;
3865 else
3866 non_suf_match = 1;
3867 if ((xp->xp_context == EXPAND_FILES
3868 || xp->xp_context == EXPAND_DIRECTORIES)
3869 && xp->xp_numfiles > 1)
3870 {
3871 /*
3872 * More than one match; check suffix.
3873 * The files will have been sorted on matching suffix in
3874 * expand_wildcards, only need to check the first two.
3875 */
3876 non_suf_match = 0;
3877 for (i = 0; i < 2; ++i)
3878 if (match_suffix(xp->xp_files[i]))
3879 ++non_suf_match;
3880 }
3881 if (non_suf_match != 1)
3882 {
3883 /* Can we ever get here unless it's while expanding
3884 * interactively? If not, we can get rid of this all
3885 * together. Don't really want to wait for this message
3886 * (and possibly have to hit return to continue!).
3887 */
3888 if (!(options & WILD_SILENT))
3889 EMSG(_(e_toomany));
3890 else if (!(options & WILD_NO_BEEP))
3891 beep_flush();
3892 }
3893 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3894 ss = vim_strsave(xp->xp_files[0]);
3895 }
3896 }
3897 }
3898
3899 /* Find longest common part */
3900 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3901 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003902 int mb_len = 1;
3903 int c0, ci;
3904
3905 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003907#ifdef FEAT_MBYTE
3908 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003910 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
3911 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
3912 }
3913 else
3914#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01003915 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003916 for (i = 1; i < xp->xp_numfiles; ++i)
3917 {
3918#ifdef FEAT_MBYTE
3919 if (has_mbyte)
3920 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
3921 else
3922#endif
3923 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003924 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003926 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01003927 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003929 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 break;
3931 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003932 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 break;
3934 }
3935 if (i < xp->xp_numfiles)
3936 {
3937 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02003938 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 break;
3940 }
3941 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003942
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 ss = alloc((unsigned)len + 1);
3944 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00003945 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 findex = -1; /* next p_wc gets first one */
3947 }
3948
3949 /* Concatenate all matching names */
3950 if (mode == WILD_ALL && xp->xp_numfiles > 0)
3951 {
3952 len = 0;
3953 for (i = 0; i < xp->xp_numfiles; ++i)
3954 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
3955 ss = lalloc(len, TRUE);
3956 if (ss != NULL)
3957 {
3958 *ss = NUL;
3959 for (i = 0; i < xp->xp_numfiles; ++i)
3960 {
3961 STRCAT(ss, xp->xp_files[i]);
3962 if (i != xp->xp_numfiles - 1)
3963 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
3964 }
3965 }
3966 }
3967
3968 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
3969 ExpandCleanup(xp);
3970
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003971 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00003972 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003973 vim_free(orig);
3974
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 return ss;
3976}
3977
3978/*
3979 * Prepare an expand structure for use.
3980 */
3981 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003982ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003984 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003985 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003987#ifndef BACKSLASH_IN_FILENAME
3988 xp->xp_shell = FALSE;
3989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 xp->xp_numfiles = -1;
3991 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003992#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
3993 xp->xp_arg = NULL;
3994#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02003995 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996}
3997
3998/*
3999 * Cleanup an expand structure after use.
4000 */
4001 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004002ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003{
4004 if (xp->xp_numfiles >= 0)
4005 {
4006 FreeWild(xp->xp_numfiles, xp->xp_files);
4007 xp->xp_numfiles = -1;
4008 }
4009}
4010
4011 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004012ExpandEscape(
4013 expand_T *xp,
4014 char_u *str,
4015 int numfiles,
4016 char_u **files,
4017 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018{
4019 int i;
4020 char_u *p;
4021
4022 /*
4023 * May change home directory back to "~"
4024 */
4025 if (options & WILD_HOME_REPLACE)
4026 tilde_replace(str, numfiles, files);
4027
4028 if (options & WILD_ESCAPE)
4029 {
4030 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004031 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004032 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 || xp->xp_context == EXPAND_BUFFERS
4034 || xp->xp_context == EXPAND_DIRECTORIES)
4035 {
4036 /*
4037 * Insert a backslash into a file name before a space, \, %, #
4038 * and wildmatch characters, except '~'.
4039 */
4040 for (i = 0; i < numfiles; ++i)
4041 {
4042 /* for ":set path=" we need to escape spaces twice */
4043 if (xp->xp_backslash == XP_BS_THREE)
4044 {
4045 p = vim_strsave_escaped(files[i], (char_u *)" ");
4046 if (p != NULL)
4047 {
4048 vim_free(files[i]);
4049 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004050#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 p = vim_strsave_escaped(files[i], (char_u *)" ");
4052 if (p != NULL)
4053 {
4054 vim_free(files[i]);
4055 files[i] = p;
4056 }
4057#endif
4058 }
4059 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004060#ifdef BACKSLASH_IN_FILENAME
4061 p = vim_strsave_fnameescape(files[i], FALSE);
4062#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004063 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 if (p != NULL)
4066 {
4067 vim_free(files[i]);
4068 files[i] = p;
4069 }
4070
4071 /* If 'str' starts with "\~", replace "~" at start of
4072 * files[i] with "\~". */
4073 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004074 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004077
4078 /* If the first file starts with a '+' escape it. Otherwise it
4079 * could be seen as "+cmd". */
4080 if (*files[0] == '+')
4081 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
4083 else if (xp->xp_context == EXPAND_TAGS)
4084 {
4085 /*
4086 * Insert a backslash before characters in a tag name that
4087 * would terminate the ":tag" command.
4088 */
4089 for (i = 0; i < numfiles; ++i)
4090 {
4091 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4092 if (p != NULL)
4093 {
4094 vim_free(files[i]);
4095 files[i] = p;
4096 }
4097 }
4098 }
4099 }
4100}
4101
4102/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004103 * Escape special characters in "fname" for when used as a file name argument
4104 * after a Vim command, or, when "shell" is non-zero, a shell command.
4105 * Returns the result in allocated memory.
4106 */
4107 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004108vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004109{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004110 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004111#ifdef BACKSLASH_IN_FILENAME
4112 char_u buf[20];
4113 int j = 0;
4114
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004115 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004116 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004117 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004118 buf[j++] = *p;
4119 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004120 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004121#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004122 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4123 if (shell && csh_like_shell() && p != NULL)
4124 {
4125 char_u *s;
4126
4127 /* For csh and similar shells need to put two backslashes before '!'.
4128 * One is taken by Vim, one by the shell. */
4129 s = vim_strsave_escaped(p, (char_u *)"!");
4130 vim_free(p);
4131 p = s;
4132 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004133#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004134
4135 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4136 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004137 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004138 escape_fname(&p);
4139
4140 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004141}
4142
4143/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004144 * Put a backslash before the file name in "pp", which is in allocated memory.
4145 */
4146 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004147escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004148{
4149 char_u *p;
4150
4151 p = alloc((unsigned)(STRLEN(*pp) + 2));
4152 if (p != NULL)
4153 {
4154 p[0] = '\\';
4155 STRCPY(p + 1, *pp);
4156 vim_free(*pp);
4157 *pp = p;
4158 }
4159}
4160
4161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 * For each file name in files[num_files]:
4163 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4164 */
4165 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004166tilde_replace(
4167 char_u *orig_pat,
4168 int num_files,
4169 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170{
4171 int i;
4172 char_u *p;
4173
4174 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4175 {
4176 for (i = 0; i < num_files; ++i)
4177 {
4178 p = home_replace_save(NULL, files[i]);
4179 if (p != NULL)
4180 {
4181 vim_free(files[i]);
4182 files[i] = p;
4183 }
4184 }
4185 }
4186}
4187
4188/*
4189 * Show all matches for completion on the command line.
4190 * Returns EXPAND_NOTHING when the character that triggered expansion should
4191 * be inserted like a normal character.
4192 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004194showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4197 int num_files;
4198 char_u **files_found;
4199 int i, j, k;
4200 int maxlen;
4201 int lines;
4202 int columns;
4203 char_u *p;
4204 int lastlen;
4205 int attr;
4206 int showtail;
4207
4208 if (xp->xp_numfiles == -1)
4209 {
4210 set_expand_context(xp);
4211 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4212 &num_files, &files_found);
4213 showtail = expand_showtail(xp);
4214 if (i != EXPAND_OK)
4215 return i;
4216
4217 }
4218 else
4219 {
4220 num_files = xp->xp_numfiles;
4221 files_found = xp->xp_files;
4222 showtail = cmd_showtail;
4223 }
4224
4225#ifdef FEAT_WILDMENU
4226 if (!wildmenu)
4227 {
4228#endif
4229 msg_didany = FALSE; /* lines_left will be set */
4230 msg_start(); /* prepare for paging */
4231 msg_putchar('\n');
4232 out_flush();
4233 cmdline_row = msg_row;
4234 msg_didany = FALSE; /* lines_left will be set again */
4235 msg_start(); /* prepare for paging */
4236#ifdef FEAT_WILDMENU
4237 }
4238#endif
4239
4240 if (got_int)
4241 got_int = FALSE; /* only int. the completion, not the cmd line */
4242#ifdef FEAT_WILDMENU
4243 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004244 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#endif
4246 else
4247 {
4248 /* find the length of the longest file name */
4249 maxlen = 0;
4250 for (i = 0; i < num_files; ++i)
4251 {
4252 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004253 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 || xp->xp_context == EXPAND_BUFFERS))
4255 {
4256 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4257 j = vim_strsize(NameBuff);
4258 }
4259 else
4260 j = vim_strsize(L_SHOWFILE(i));
4261 if (j > maxlen)
4262 maxlen = j;
4263 }
4264
4265 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4266 lines = num_files;
4267 else
4268 {
4269 /* compute the number of columns and lines for the listing */
4270 maxlen += 2; /* two spaces between file names */
4271 columns = ((int)Columns + 2) / maxlen;
4272 if (columns < 1)
4273 columns = 1;
4274 lines = (num_files + columns - 1) / columns;
4275 }
4276
Bram Moolenaar8820b482017-03-16 17:23:31 +01004277 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278
4279 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4280 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004281 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 msg_clr_eos();
4283 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004284 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286
4287 /* list the files line by line */
4288 for (i = 0; i < lines; ++i)
4289 {
4290 lastlen = 999;
4291 for (k = i; k < num_files; k += lines)
4292 {
4293 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4294 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004295 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 p = files_found[k] + STRLEN(files_found[k]) + 1;
4297 msg_advance(maxlen + 1);
4298 msg_puts(p);
4299 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004300 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 break;
4302 }
4303 for (j = maxlen - lastlen; --j >= 0; )
4304 msg_putchar(' ');
4305 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004306 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 || xp->xp_context == EXPAND_BUFFERS)
4308 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004309 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004310 if (xp->xp_numfiles != -1)
4311 {
4312 char_u *halved_slash;
4313 char_u *exp_path;
4314
4315 /* Expansion was done before and special characters
4316 * were escaped, need to halve backslashes. Also
4317 * $HOME has been replaced with ~/. */
4318 exp_path = expand_env_save_opt(files_found[k], TRUE);
4319 halved_slash = backslash_halve_save(
4320 exp_path != NULL ? exp_path : files_found[k]);
4321 j = mch_isdir(halved_slash != NULL ? halved_slash
4322 : files_found[k]);
4323 vim_free(exp_path);
4324 vim_free(halved_slash);
4325 }
4326 else
4327 /* Expansion was done here, file names are literal. */
4328 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 if (showtail)
4330 p = L_SHOWFILE(k);
4331 else
4332 {
4333 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4334 TRUE);
4335 p = NameBuff;
4336 }
4337 }
4338 else
4339 {
4340 j = FALSE;
4341 p = L_SHOWFILE(k);
4342 }
4343 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4344 }
4345 if (msg_col > 0) /* when not wrapped around */
4346 {
4347 msg_clr_eos();
4348 msg_putchar('\n');
4349 }
4350 out_flush(); /* show one line at a time */
4351 if (got_int)
4352 {
4353 got_int = FALSE;
4354 break;
4355 }
4356 }
4357
4358 /*
4359 * we redraw the command below the lines that we have just listed
4360 * This is a bit tricky, but it saves a lot of screen updating.
4361 */
4362 cmdline_row = msg_row; /* will put it back later */
4363 }
4364
4365 if (xp->xp_numfiles == -1)
4366 FreeWild(num_files, files_found);
4367
4368 return EXPAND_OK;
4369}
4370
4371/*
4372 * Private gettail for showmatches() (and win_redr_status_matches()):
4373 * Find tail of file name path, but ignore trailing "/".
4374 */
4375 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004376sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377{
4378 char_u *p;
4379 char_u *t = s;
4380 int had_sep = FALSE;
4381
4382 for (p = s; *p != NUL; )
4383 {
4384 if (vim_ispathsep(*p)
4385#ifdef BACKSLASH_IN_FILENAME
4386 && !rem_backslash(p)
4387#endif
4388 )
4389 had_sep = TRUE;
4390 else if (had_sep)
4391 {
4392 t = p;
4393 had_sep = FALSE;
4394 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004395 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 return t;
4398}
4399
4400/*
4401 * Return TRUE if we only need to show the tail of completion matches.
4402 * When not completing file names or there is a wildcard in the path FALSE is
4403 * returned.
4404 */
4405 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004406expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407{
4408 char_u *s;
4409 char_u *end;
4410
4411 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004412 if (xp->xp_context != EXPAND_FILES
4413 && xp->xp_context != EXPAND_SHELLCMD
4414 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 return FALSE;
4416
4417 end = gettail(xp->xp_pattern);
4418 if (end == xp->xp_pattern) /* there is no path separator */
4419 return FALSE;
4420
4421 for (s = xp->xp_pattern; s < end; s++)
4422 {
4423 /* Skip escaped wildcards. Only when the backslash is not a path
4424 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4425 if (rem_backslash(s))
4426 ++s;
4427 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4428 return FALSE;
4429 }
4430 return TRUE;
4431}
4432
4433/*
4434 * Prepare a string for expansion.
4435 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004436 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 * When expanding other names: The string will be used with regcomp(). Copy
4438 * the name into allocated memory and prepend "^".
4439 */
4440 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004441addstar(
4442 char_u *fname,
4443 int len,
4444 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445{
4446 char_u *retval;
4447 int i, j;
4448 int new_len;
4449 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004450 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004452 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004453 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004454 && context != EXPAND_SHELLCMD
4455 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
4457 /*
4458 * Matching will be done internally (on something other than files).
4459 * So we convert the file-matching-type wildcards into our kind for
4460 * use with vim_regcomp(). First work out how long it will be:
4461 */
4462
4463 /* For help tags the translation is done in find_help_tags().
4464 * For a tag pattern starting with "/" no translation is needed. */
4465 if (context == EXPAND_HELP
4466 || context == EXPAND_COLORS
4467 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004468 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004469 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004470 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004471 || ((context == EXPAND_TAGS_LISTFILES
4472 || context == EXPAND_TAGS)
4473 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 retval = vim_strnsave(fname, len);
4475 else
4476 {
4477 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4478 for (i = 0; i < len; i++)
4479 {
4480 if (fname[i] == '*' || fname[i] == '~')
4481 new_len++; /* '*' needs to be replaced by ".*"
4482 '~' needs to be replaced by "\~" */
4483
4484 /* Buffer names are like file names. "." should be literal */
4485 if (context == EXPAND_BUFFERS && fname[i] == '.')
4486 new_len++; /* "." becomes "\." */
4487
4488 /* Custom expansion takes care of special things, match
4489 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004490 if ((context == EXPAND_USER_DEFINED
4491 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 new_len++; /* '\' becomes "\\" */
4493 }
4494 retval = alloc(new_len);
4495 if (retval != NULL)
4496 {
4497 retval[0] = '^';
4498 j = 1;
4499 for (i = 0; i < len; i++, j++)
4500 {
4501 /* Skip backslash. But why? At least keep it for custom
4502 * expansion. */
4503 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004504 && context != EXPAND_USER_LIST
4505 && fname[i] == '\\'
4506 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 break;
4508
4509 switch (fname[i])
4510 {
4511 case '*': retval[j++] = '.';
4512 break;
4513 case '~': retval[j++] = '\\';
4514 break;
4515 case '?': retval[j] = '.';
4516 continue;
4517 case '.': if (context == EXPAND_BUFFERS)
4518 retval[j++] = '\\';
4519 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004520 case '\\': if (context == EXPAND_USER_DEFINED
4521 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 retval[j++] = '\\';
4523 break;
4524 }
4525 retval[j] = fname[i];
4526 }
4527 retval[j] = NUL;
4528 }
4529 }
4530 }
4531 else
4532 {
4533 retval = alloc(len + 4);
4534 if (retval != NULL)
4535 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004536 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004539 * Don't add a star to *, ~, ~user, $var or `cmd`.
4540 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 * ~ would be at the start of the file name, but not the tail.
4542 * $ could be anywhere in the tail.
4543 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004544 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 */
4546 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004547 ends_in_star = (len > 0 && retval[len - 1] == '*');
4548#ifndef BACKSLASH_IN_FILENAME
4549 for (i = len - 2; i >= 0; --i)
4550 {
4551 if (retval[i] != '\\')
4552 break;
4553 ends_in_star = !ends_in_star;
4554 }
4555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004557 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 && vim_strchr(tail, '$') == NULL
4559 && vim_strchr(retval, '`') == NULL)
4560 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004561 else if (len > 0 && retval[len - 1] == '$')
4562 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 retval[len] = NUL;
4564 }
4565 }
4566 return retval;
4567}
4568
4569/*
4570 * Must parse the command line so far to work out what context we are in.
4571 * Completion can then be done based on that context.
4572 * This routine sets the variables:
4573 * xp->xp_pattern The start of the pattern to be expanded within
4574 * the command line (ends at the cursor).
4575 * xp->xp_context The type of thing to expand. Will be one of:
4576 *
4577 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4578 * the command line, like an unknown command. Caller
4579 * should beep.
4580 * EXPAND_NOTHING Unrecognised context for completion, use char like
4581 * a normal char, rather than for completion. eg
4582 * :s/^I/
4583 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4584 * it.
4585 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4586 * EXPAND_FILES After command with XFILE set, or after setting
4587 * with P_EXPAND set. eg :e ^I, :w>>^I
4588 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4589 * when we know only directories are of interest. eg
4590 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004591 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4593 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4594 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4595 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4596 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4597 * EXPAND_EVENTS Complete event names
4598 * EXPAND_SYNTAX Complete :syntax command arguments
4599 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4600 * EXPAND_AUGROUP Complete autocommand group names
4601 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4602 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4603 * eg :unmap a^I , :cunab x^I
4604 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4605 * eg :call sub^I
4606 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4607 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4608 * names in expressions, eg :while s^I
4609 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004610 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 */
4612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004613set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004615 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 if (ccline.cmdfirstc != ':'
4617#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004618 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004619 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620#endif
4621 )
4622 {
4623 xp->xp_context = EXPAND_NOTHING;
4624 return;
4625 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004626 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627}
4628
4629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004630set_cmd_context(
4631 expand_T *xp,
4632 char_u *str, /* start of command line */
4633 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004634 int col, /* position of cursor */
4635 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 int old_char = NUL;
4638 char_u *nextcomm;
4639
4640 /*
4641 * Avoid a UMR warning from Purify, only save the character if it has been
4642 * written before.
4643 */
4644 if (col < len)
4645 old_char = str[col];
4646 str[col] = NUL;
4647 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004648
4649#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004650 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004651 {
4652# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004653 /* pass CMD_SIZE because there is no real command */
4654 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004655# endif
4656 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004657 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004658 {
4659 xp->xp_context = ccline.xp_context;
4660 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004661# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004662 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004663# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004664 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004665 else
4666#endif
4667 while (nextcomm != NULL)
4668 nextcomm = set_one_cmd_context(xp, nextcomm);
4669
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004670 /* Store the string here so that call_user_expand_func() can get to them
4671 * easily. */
4672 xp->xp_line = str;
4673 xp->xp_col = col;
4674
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 str[col] = old_char;
4676}
4677
4678/*
4679 * Expand the command line "str" from context "xp".
4680 * "xp" must have been set by set_cmd_context().
4681 * xp->xp_pattern points into "str", to where the text that is to be expanded
4682 * starts.
4683 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4684 * cursor.
4685 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4686 * key that triggered expansion literally.
4687 * Returns EXPAND_OK otherwise.
4688 */
4689 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004690expand_cmdline(
4691 expand_T *xp,
4692 char_u *str, /* start of command line */
4693 int col, /* position of cursor */
4694 int *matchcount, /* return: nr of matches */
4695 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004698 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
4700 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4701 {
4702 beep_flush();
4703 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4704 }
4705 if (xp->xp_context == EXPAND_NOTHING)
4706 {
4707 /* Caller can use the character as a normal char instead */
4708 return EXPAND_NOTHING;
4709 }
4710
4711 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004712 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4713 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (file_str == NULL)
4715 return EXPAND_UNSUCCESSFUL;
4716
Bram Moolenaar94950a92010-12-02 16:01:29 +01004717 if (p_wic)
4718 options += WILD_ICASE;
4719
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004721 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 *matchcount = 0;
4724 *matches = NULL;
4725 }
4726 vim_free(file_str);
4727
4728 return EXPAND_OK;
4729}
4730
4731#ifdef FEAT_MULTI_LANG
4732/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004733 * Cleanup matches for help tags:
4734 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4735 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004737static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738
4739 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004740cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741{
4742 int i, j;
4743 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004744 char_u buf[4];
4745 char_u *p = buf;
4746
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004747 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004748 {
4749 *p++ = '@';
4750 *p++ = p_hlg[0];
4751 *p++ = p_hlg[1];
4752 }
4753 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 for (i = 0; i < num_file; ++i)
4756 {
4757 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004758 if (len <= 0)
4759 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004760 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
4762 /* Sorting on priority means the same item in another language may
4763 * be anywhere. Search all items for a match up to the "@en". */
4764 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004765 if (j != i && (int)STRLEN(file[j]) == len + 3
4766 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 break;
4768 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004769 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 file[i][len] = NUL;
4771 }
4772 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004773
4774 if (*buf != NUL)
4775 for (i = 0; i < num_file; ++i)
4776 {
4777 len = (int)STRLEN(file[i]) - 3;
4778 if (len <= 0)
4779 continue;
4780 if (STRCMP(file[i] + len, buf) == 0)
4781 {
4782 /* remove the default language */
4783 file[i][len] = NUL;
4784 }
4785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786}
4787#endif
4788
4789/*
4790 * Do the expansion based on xp->xp_context and "pat".
4791 */
4792 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004793ExpandFromContext(
4794 expand_T *xp,
4795 char_u *pat,
4796 int *num_file,
4797 char_u ***file,
4798 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799{
4800#ifdef FEAT_CMDL_COMPL
4801 regmatch_T regmatch;
4802#endif
4803 int ret;
4804 int flags;
4805
4806 flags = EW_DIR; /* include directories */
4807 if (options & WILD_LIST_NOTFOUND)
4808 flags |= EW_NOTFOUND;
4809 if (options & WILD_ADD_SLASH)
4810 flags |= EW_ADDSLASH;
4811 if (options & WILD_KEEP_ALL)
4812 flags |= EW_KEEPALL;
4813 if (options & WILD_SILENT)
4814 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004815 if (options & WILD_ALLLINKS)
4816 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004818 if (xp->xp_context == EXPAND_FILES
4819 || xp->xp_context == EXPAND_DIRECTORIES
4820 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
4822 /*
4823 * Expand file or directory names.
4824 */
4825 int free_pat = FALSE;
4826 int i;
4827
4828 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4829 * space */
4830 if (xp->xp_backslash != XP_BS_NONE)
4831 {
4832 free_pat = TRUE;
4833 pat = vim_strsave(pat);
4834 for (i = 0; pat[i]; ++i)
4835 if (pat[i] == '\\')
4836 {
4837 if (xp->xp_backslash == XP_BS_THREE
4838 && pat[i + 1] == '\\'
4839 && pat[i + 2] == '\\'
4840 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004841 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 if (xp->xp_backslash == XP_BS_ONE
4843 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004844 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 }
4846 }
4847
4848 if (xp->xp_context == EXPAND_FILES)
4849 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004850 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4851 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 else
4853 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004854 if (options & WILD_ICASE)
4855 flags |= EW_ICASE;
4856
Bram Moolenaard7834d32009-12-02 16:14:36 +00004857 /* Expand wildcards, supporting %:h and the like. */
4858 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (free_pat)
4860 vim_free(pat);
4861 return ret;
4862 }
4863
4864 *file = (char_u **)"";
4865 *num_file = 0;
4866 if (xp->xp_context == EXPAND_HELP)
4867 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004868 /* With an empty argument we would get all the help tags, which is
4869 * very slow. Get matches for "help" instead. */
4870 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4871 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873#ifdef FEAT_MULTI_LANG
4874 cleanup_help_tags(*num_file, *file);
4875#endif
4876 return OK;
4877 }
4878 return FAIL;
4879 }
4880
4881#ifndef FEAT_CMDL_COMPL
4882 return FAIL;
4883#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004884 if (xp->xp_context == EXPAND_SHELLCMD)
4885 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 if (xp->xp_context == EXPAND_OLD_SETTING)
4887 return ExpandOldSetting(num_file, file);
4888 if (xp->xp_context == EXPAND_BUFFERS)
4889 return ExpandBufnames(pat, num_file, file, options);
4890 if (xp->xp_context == EXPAND_TAGS
4891 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4892 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4893 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004894 {
4895 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004896 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4897 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004900 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004901 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004902 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004903 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004904 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004905 {
4906 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004907 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004908 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004909 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004910 {
4911 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004912 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004913 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004914# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4915 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00004916 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004917# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004918 if (xp->xp_context == EXPAND_PACKADD)
4919 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4922 if (regmatch.regprog == NULL)
4923 return FAIL;
4924
4925 /* set ignore-case according to p_ic, p_scs and pat */
4926 regmatch.rm_ic = ignorecase(pat);
4927
4928 if (xp->xp_context == EXPAND_SETTINGS
4929 || xp->xp_context == EXPAND_BOOL_SETTINGS)
4930 ret = ExpandSettings(xp, &regmatch, num_file, file);
4931 else if (xp->xp_context == EXPAND_MAPPINGS)
4932 ret = ExpandMappings(&regmatch, num_file, file);
4933# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
4934 else if (xp->xp_context == EXPAND_USER_DEFINED)
4935 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
4936# endif
4937 else
4938 {
4939 static struct expgen
4940 {
4941 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01004942 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004944 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 } tab[] =
4946 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004947 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
4948 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02004949 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02004950 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02004951#ifdef FEAT_CMDHIST
4952 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
4953#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004955 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01004956 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004957 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
4958 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
4959 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960#endif
4961#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004962 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
4963 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
4964 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
4965 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966#endif
4967#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004968 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
4969 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970#endif
4971#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004972 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02004974#ifdef FEAT_PROFILE
4975 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
4976#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004977 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004978 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
4979 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004980#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004981 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00004982#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004983#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004984 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00004985#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004986#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004987 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01004988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
4990 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004991 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
4992 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02004994 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02004995 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02004996 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 };
4998 int i;
4999
5000 /*
5001 * Find a context in the table and call the ExpandGeneric() with the
5002 * right function to do the expansion.
5003 */
5004 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005005 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (xp->xp_context == tab[i].context)
5007 {
5008 if (tab[i].ic)
5009 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005010 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005011 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 break;
5013 }
5014 }
5015
Bram Moolenaar473de612013-06-08 18:19:48 +02005016 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 return ret;
5019#endif /* FEAT_CMDL_COMPL */
5020}
5021
5022#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5023/*
5024 * Expand a list of names.
5025 *
5026 * Generic function for command line completion. It calls a function to
5027 * obtain strings, one by one. The strings are matched against a regexp
5028 * program. Matching strings are copied into an array, which is returned.
5029 *
5030 * Returns OK when no problems encountered, FAIL for error (out of memory).
5031 */
5032 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005033ExpandGeneric(
5034 expand_T *xp,
5035 regmatch_T *regmatch,
5036 int *num_file,
5037 char_u ***file,
5038 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005040 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041{
5042 int i;
5043 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005044 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 char_u *str;
5046
5047 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005048 * round == 0: count the number of matching names
5049 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005051 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
5053 for (i = 0; ; ++i)
5054 {
5055 str = (*func)(xp, i);
5056 if (str == NULL) /* end of list */
5057 break;
5058 if (*str == NUL) /* skip empty strings */
5059 continue;
5060
5061 if (vim_regexec(regmatch, str, (colnr_T)0))
5062 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005063 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005065 if (escaped)
5066 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5067 else
5068 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 (*file)[count] = str;
5070#ifdef FEAT_MENU
5071 if (func == get_menu_names && str != NULL)
5072 {
5073 /* test for separator added by get_menu_names() */
5074 str += STRLEN(str) - 1;
5075 if (*str == '\001')
5076 *str = '.';
5077 }
5078#endif
5079 }
5080 ++count;
5081 }
5082 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005083 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085 if (count == 0)
5086 return OK;
5087 *num_file = count;
5088 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5089 if (*file == NULL)
5090 {
5091 *file = (char_u **)"";
5092 return FAIL;
5093 }
5094 count = 0;
5095 }
5096 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005097
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005098 /* Sort the results. Keep menu's in the specified order. */
5099 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005100 {
5101 if (xp->xp_context == EXPAND_EXPRESSION
5102 || xp->xp_context == EXPAND_FUNCTIONS
5103 || xp->xp_context == EXPAND_USER_FUNC)
5104 /* <SNR> functions should be sorted to the end. */
5105 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5106 sort_func_compare);
5107 else
5108 sort_strings(*file, *num_file);
5109 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005110
Bram Moolenaar4f688582007-07-24 12:34:30 +00005111#ifdef FEAT_CMDL_COMPL
5112 /* Reset the variables used for special highlight names expansion, so that
5113 * they don't show up when getting normal highlight names by ID. */
5114 reset_expand_highlight();
5115#endif
5116
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 return OK;
5118}
5119
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005120/*
5121 * Complete a shell command.
5122 * Returns FAIL or OK;
5123 */
5124 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005125expand_shellcmd(
5126 char_u *filepat, /* pattern to match with command names */
5127 int *num_file, /* return: number of matches */
5128 char_u ***file, /* return: array with matches */
5129 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005130{
5131 char_u *pat;
5132 int i;
5133 char_u *path;
5134 int mustfree = FALSE;
5135 garray_T ga;
5136 char_u *buf = alloc(MAXPATHL);
5137 size_t l;
5138 char_u *s, *e;
5139 int flags = flagsarg;
5140 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005141 int did_curdir = FALSE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005142
5143 if (buf == NULL)
5144 return FAIL;
5145
5146 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5147 * space */
5148 pat = vim_strsave(filepat);
5149 for (i = 0; pat[i]; ++i)
5150 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005151 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005152
Bram Moolenaarb5971142015-03-21 17:32:19 +01005153 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005154
5155 /* For an absolute name we don't use $PATH. */
Bram Moolenaar68c31742006-09-02 15:54:18 +00005156 if (mch_isFullName(pat))
5157 path = (char_u *)" ";
5158 else if ((pat[0] == '.' && (vim_ispathsep(pat[1])
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005159 || (pat[1] == '.' && vim_ispathsep(pat[2])))))
5160 path = (char_u *)".";
5161 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005162 {
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005163 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005164 if (path == NULL)
5165 path = (char_u *)"";
5166 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005167
5168 /*
5169 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005170 * collect them in "ga". When "." is not in $PATH also expand for the
5171 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005172 */
5173 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005174 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005175 {
Bram Moolenaarb5971142015-03-21 17:32:19 +01005176 if (*s == NUL)
5177 {
5178 if (did_curdir)
5179 break;
5180 /* Find directories in the current directory, path is empty. */
5181 did_curdir = TRUE;
5182 }
5183 else if (*s == '.')
5184 did_curdir = TRUE;
5185
Bram Moolenaar68c31742006-09-02 15:54:18 +00005186 if (*s == ' ')
5187 ++s; /* Skip space used for absolute path name. */
5188
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005189#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005190 e = vim_strchr(s, ';');
5191#else
5192 e = vim_strchr(s, ':');
5193#endif
5194 if (e == NULL)
5195 e = s + STRLEN(s);
5196
5197 l = e - s;
5198 if (l > MAXPATHL - 5)
5199 break;
5200 vim_strncpy(buf, s, l);
5201 add_pathsep(buf);
5202 l = STRLEN(buf);
5203 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5204
5205 /* Expand matches in one directory of $PATH. */
5206 ret = expand_wildcards(1, &buf, num_file, file, flags);
5207 if (ret == OK)
5208 {
5209 if (ga_grow(&ga, *num_file) == FAIL)
5210 FreeWild(*num_file, *file);
5211 else
5212 {
5213 for (i = 0; i < *num_file; ++i)
5214 {
5215 s = (*file)[i];
5216 if (STRLEN(s) > l)
5217 {
5218 /* Remove the path again. */
Bram Moolenaar446cb832008-06-24 21:56:24 +00005219 STRMOVE(s, s + l);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005220 ((char_u **)ga.ga_data)[ga.ga_len++] = s;
5221 }
5222 else
5223 vim_free(s);
5224 }
5225 vim_free(*file);
5226 }
5227 }
5228 if (*e != NUL)
5229 ++e;
5230 }
5231 *file = ga.ga_data;
5232 *num_file = ga.ga_len;
5233
5234 vim_free(buf);
5235 vim_free(pat);
5236 if (mustfree)
5237 vim_free(path);
5238 return OK;
5239}
5240
5241
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005243static 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 +00005244
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005246 * Call "user_expand_func()" to invoke a user defined Vim script function and
5247 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005249 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005250call_user_expand_func(
5251 void *(*user_expand_func)(char_u *, int, char_u **, int),
5252 expand_T *xp,
5253 int *num_file,
5254 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005256 int keep = 0;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005257 char_u num[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 char_u *args[3];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 int save_current_SID = current_SID;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005260 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005261 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaarb7515462013-06-29 12:58:33 +02005263 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005264 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 *num_file = 0;
5266 *file = NULL;
5267
Bram Moolenaarb7515462013-06-29 12:58:33 +02005268 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005269 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005270 keep = ccline.cmdbuff[ccline.cmdlen];
5271 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005272 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005273
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005274 args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
Bram Moolenaarb7515462013-06-29 12:58:33 +02005275 args[1] = xp->xp_line;
5276 sprintf((char *)num, "%d", xp->xp_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 args[2] = num;
5278
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005279 /* Save the cmdline, we don't know what the function may do. */
5280 save_ccline = ccline;
5281 ccline.cmdbuff = NULL;
5282 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005284
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005285 ret = user_expand_func(xp->xp_arg, 3, args, FALSE);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005286
5287 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005289 if (ccline.cmdbuff != NULL)
5290 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005291
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005292 vim_free(args[0]);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005293 return ret;
5294}
5295
5296/*
5297 * Expand names with a function defined by the user.
5298 */
5299 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005300ExpandUserDefined(
5301 expand_T *xp,
5302 regmatch_T *regmatch,
5303 int *num_file,
5304 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005305{
5306 char_u *retstr;
5307 char_u *s;
5308 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005309 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005310 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005311 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005312
5313 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5314 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return FAIL;
5316
5317 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005318 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 {
5320 e = vim_strchr(s, '\n');
5321 if (e == NULL)
5322 e = s + STRLEN(s);
5323 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005324 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005326 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5327 *e = keep;
5328
5329 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005331 if (ga_grow(&ga, 1) == FAIL)
5332 break;
5333 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5334 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 if (*e != NUL)
5338 ++e;
5339 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005340 vim_free(retstr);
5341 *file = ga.ga_data;
5342 *num_file = ga.ga_len;
5343 return OK;
5344}
5345
5346/*
5347 * Expand names with a list returned by a function defined by the user.
5348 */
5349 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350ExpandUserList(
5351 expand_T *xp,
5352 int *num_file,
5353 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005354{
5355 list_T *retlist;
5356 listitem_T *li;
5357 garray_T ga;
5358
5359 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5360 if (retlist == NULL)
5361 return FAIL;
5362
5363 ga_init2(&ga, (int)sizeof(char *), 3);
5364 /* Loop over the items in the list. */
5365 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5366 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005367 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5368 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005369
5370 if (ga_grow(&ga, 1) == FAIL)
5371 break;
5372
5373 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005374 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005375 ++ga.ga_len;
5376 }
5377 list_unref(retlist);
5378
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 *file = ga.ga_data;
5380 *num_file = ga.ga_len;
5381 return OK;
5382}
5383#endif
5384
5385/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005386 * Expand color scheme, compiler or filetype names.
5387 * Search from 'runtimepath':
5388 * 'runtimepath'/{dirnames}/{pat}.vim
5389 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5390 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5391 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5392 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005393 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 */
5395 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005396ExpandRTDir(
5397 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005398 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005399 int *num_file,
5400 char_u ***file,
5401 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 char_u *s;
5404 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005405 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005407 int i;
5408 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410 *num_file = 0;
5411 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005412 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005413 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005415 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005417 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5418 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005420 ga_clear_strings(&ga);
5421 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005423 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005424 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005425 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005427
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005428 if (flags & DIP_START) {
5429 for (i = 0; dirnames[i] != NULL; ++i)
5430 {
5431 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5432 if (s == NULL)
5433 {
5434 ga_clear_strings(&ga);
5435 return FAIL;
5436 }
5437 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5438 globpath(p_pp, s, &ga, 0);
5439 vim_free(s);
5440 }
5441 }
5442
5443 if (flags & DIP_OPT) {
5444 for (i = 0; dirnames[i] != NULL; ++i)
5445 {
5446 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5447 if (s == NULL)
5448 {
5449 ga_clear_strings(&ga);
5450 return FAIL;
5451 }
5452 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5453 globpath(p_pp, s, &ga, 0);
5454 vim_free(s);
5455 }
5456 }
5457
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005458 for (i = 0; i < ga.ga_len; ++i)
5459 {
5460 match = ((char_u **)ga.ga_data)[i];
5461 s = match;
5462 e = s + STRLEN(s);
5463 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5464 {
5465 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005466 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005467 if (s < match || vim_ispathsep(*s))
5468 break;
5469 ++s;
5470 *e = NUL;
5471 mch_memmove(match, s, e - s + 1);
5472 }
5473 }
5474
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005475 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005476 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005477
5478 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005479 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005480 remove_duplicates(&ga);
5481
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 *file = ga.ga_data;
5483 *num_file = ga.ga_len;
5484 return OK;
5485}
5486
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005487/*
5488 * Expand loadplugin names:
5489 * 'packpath'/pack/ * /opt/{pat}
5490 */
5491 static int
5492ExpandPackAddDir(
5493 char_u *pat,
5494 int *num_file,
5495 char_u ***file)
5496{
5497 char_u *s;
5498 char_u *e;
5499 char_u *match;
5500 garray_T ga;
5501 int i;
5502 int pat_len;
5503
5504 *num_file = 0;
5505 *file = NULL;
5506 pat_len = (int)STRLEN(pat);
5507 ga_init2(&ga, (int)sizeof(char *), 10);
5508
5509 s = alloc((unsigned)(pat_len + 26));
5510 if (s == NULL)
5511 {
5512 ga_clear_strings(&ga);
5513 return FAIL;
5514 }
5515 sprintf((char *)s, "pack/*/opt/%s*", pat);
5516 globpath(p_pp, s, &ga, 0);
5517 vim_free(s);
5518
5519 for (i = 0; i < ga.ga_len; ++i)
5520 {
5521 match = ((char_u **)ga.ga_data)[i];
5522 s = gettail(match);
5523 e = s + STRLEN(s);
5524 mch_memmove(match, s, e - s + 1);
5525 }
5526
5527 if (ga.ga_len == 0)
5528 return FAIL;
5529
5530 /* Sort and remove duplicates which can happen when specifying multiple
5531 * directories in dirnames. */
5532 remove_duplicates(&ga);
5533
5534 *file = ga.ga_data;
5535 *num_file = ga.ga_len;
5536 return OK;
5537}
5538
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539#endif
5540
5541#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5542/*
5543 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005544 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005546 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547globpath(
5548 char_u *path,
5549 char_u *file,
5550 garray_T *ga,
5551 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
5553 expand_T xpc;
5554 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 int num_p;
5557 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558
5559 buf = alloc(MAXPATHL);
5560 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005561 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005563 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 /* Loop over all entries in {path}. */
5567 while (*path != NUL)
5568 {
5569 /* Copy one item of the path to buf[] and concatenate the file name. */
5570 copy_option_part(&path, buf, MAXPATHL, ",");
5571 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5572 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005573# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005574 /* Using the platform's path separator (\) makes vim incorrectly
5575 * treat it as an escape character, use '/' instead. */
5576 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5577 STRCAT(buf, "/");
5578# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005582 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5583 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005585 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005587 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 for (i = 0; i < num_p; ++i)
5590 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005591 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005592 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005593 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 FreeWild(num_p, p);
5598 }
5599 }
5600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
5602 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603}
5604
5605#endif
5606
5607#if defined(FEAT_CMDHIST) || defined(PROTO)
5608
5609/*********************************
5610 * Command line history stuff *
5611 *********************************/
5612
5613/*
5614 * Translate a history character to the associated type number.
5615 */
5616 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005617hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618{
5619 if (c == ':')
5620 return HIST_CMD;
5621 if (c == '=')
5622 return HIST_EXPR;
5623 if (c == '@')
5624 return HIST_INPUT;
5625 if (c == '>')
5626 return HIST_DEBUG;
5627 return HIST_SEARCH; /* must be '?' or '/' */
5628}
5629
5630/*
5631 * Table of history names.
5632 * These names are used in :history and various hist...() functions.
5633 * It is sufficient to give the significant prefix of a history name.
5634 */
5635
5636static char *(history_names[]) =
5637{
5638 "cmd",
5639 "search",
5640 "expr",
5641 "input",
5642 "debug",
5643 NULL
5644};
5645
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005646#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5647/*
5648 * Function given to ExpandGeneric() to obtain the possible first
5649 * arguments of the ":history command.
5650 */
5651 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005653{
5654 static char_u compl[2] = { NUL, NUL };
5655 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005656 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005657 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5658
5659 if (idx < short_names_count)
5660 {
5661 compl[0] = (char_u)short_names[idx];
5662 return compl;
5663 }
5664 if (idx < short_names_count + history_name_count)
5665 return (char_u *)history_names[idx - short_names_count];
5666 if (idx == short_names_count + history_name_count)
5667 return (char_u *)"all";
5668 return NULL;
5669}
5670#endif
5671
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672/*
5673 * init_history() - Initialize the command line history.
5674 * Also used to re-allocate the history when the size changes.
5675 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005676 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005677init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678{
5679 int newlen; /* new length of history table */
5680 histentry_T *temp;
5681 int i;
5682 int j;
5683 int type;
5684
5685 /*
5686 * If size of history table changed, reallocate it
5687 */
5688 newlen = (int)p_hi;
5689 if (newlen != hislen) /* history length changed */
5690 {
5691 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5692 {
5693 if (newlen)
5694 {
5695 temp = (histentry_T *)lalloc(
5696 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5697 if (temp == NULL) /* out of memory! */
5698 {
5699 if (type == 0) /* first one: just keep the old length */
5700 {
5701 newlen = hislen;
5702 break;
5703 }
5704 /* Already changed one table, now we can only have zero
5705 * length for all tables. */
5706 newlen = 0;
5707 type = -1;
5708 continue;
5709 }
5710 }
5711 else
5712 temp = NULL;
5713 if (newlen == 0 || temp != NULL)
5714 {
5715 if (hisidx[type] < 0) /* there are no entries yet */
5716 {
5717 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005718 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720 else if (newlen > hislen) /* array becomes bigger */
5721 {
5722 for (i = 0; i <= hisidx[type]; ++i)
5723 temp[i] = history[type][i];
5724 j = i;
5725 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005726 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 for ( ; j < hislen; ++i, ++j)
5728 temp[i] = history[type][j];
5729 }
5730 else /* array becomes smaller or 0 */
5731 {
5732 j = hisidx[type];
5733 for (i = newlen - 1; ; --i)
5734 {
5735 if (i >= 0) /* copy newest entries */
5736 temp[i] = history[type][j];
5737 else /* remove older entries */
5738 vim_free(history[type][j].hisstr);
5739 if (--j < 0)
5740 j = hislen - 1;
5741 if (j == hisidx[type])
5742 break;
5743 }
5744 hisidx[type] = newlen - 1;
5745 }
5746 vim_free(history[type]);
5747 history[type] = temp;
5748 }
5749 }
5750 hislen = newlen;
5751 }
5752}
5753
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005754 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005755clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005756{
5757 hisptr->hisnum = 0;
5758 hisptr->viminfo = FALSE;
5759 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005760 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005761}
5762
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763/*
5764 * Check if command line 'str' is already in history.
5765 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5766 */
5767 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005768in_history(
5769 int type,
5770 char_u *str,
5771 int move_to_front, /* Move the entry to the front if it exists */
5772 int sep,
5773 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 int i;
5776 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005777 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778
5779 if (hisidx[type] < 0)
5780 return FALSE;
5781 i = hisidx[type];
5782 do
5783 {
5784 if (history[type][i].hisstr == NULL)
5785 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005786
5787 /* For search history, check that the separator character matches as
5788 * well. */
5789 p = history[type][i].hisstr;
5790 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005791 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005792 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 {
5794 if (!move_to_front)
5795 return TRUE;
5796 last_i = i;
5797 break;
5798 }
5799 if (--i < 0)
5800 i = hislen - 1;
5801 } while (i != hisidx[type]);
5802
5803 if (last_i >= 0)
5804 {
5805 str = history[type][i].hisstr;
5806 while (i != hisidx[type])
5807 {
5808 if (++i >= hislen)
5809 i = 0;
5810 history[type][last_i] = history[type][i];
5811 last_i = i;
5812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005814 history[type][i].viminfo = FALSE;
5815 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005816 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 return TRUE;
5818 }
5819 return FALSE;
5820}
5821
5822/*
5823 * Convert history name (from table above) to its HIST_ equivalent.
5824 * When "name" is empty, return "cmd" history.
5825 * Returns -1 for unknown history name.
5826 */
5827 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005828get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 int i;
5831 int len = (int)STRLEN(name);
5832
5833 /* No argument: use current history. */
5834 if (len == 0)
5835 return hist_char2type(ccline.cmdfirstc);
5836
5837 for (i = 0; history_names[i] != NULL; ++i)
5838 if (STRNICMP(name, history_names[i], len) == 0)
5839 return i;
5840
5841 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5842 return hist_char2type(name[0]);
5843
5844 return -1;
5845}
5846
5847static int last_maptick = -1; /* last seen maptick */
5848
5849/*
5850 * Add the given string to the given history. If the string is already in the
5851 * history then it is moved to the front. "histype" may be one of he HIST_
5852 * values.
5853 */
5854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005855add_to_history(
5856 int histype,
5857 char_u *new_entry,
5858 int in_map, /* consider maptick when inside a mapping */
5859 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 histentry_T *hisptr;
5862 int len;
5863
5864 if (hislen == 0) /* no history */
5865 return;
5866
Bram Moolenaara939e432013-11-09 05:30:26 +01005867 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5868 return;
5869
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 /*
5871 * Searches inside the same mapping overwrite each other, so that only
5872 * the last line is kept. Be careful not to remove a line that was moved
5873 * down, only lines that were added.
5874 */
5875 if (histype == HIST_SEARCH && in_map)
5876 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005877 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
5879 /* Current line is from the same mapping, remove it */
5880 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5881 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005882 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 --hisnum[histype];
5884 if (--hisidx[HIST_SEARCH] < 0)
5885 hisidx[HIST_SEARCH] = hislen - 1;
5886 }
5887 last_maptick = -1;
5888 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02005889 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
5891 if (++hisidx[histype] == hislen)
5892 hisidx[histype] = 0;
5893 hisptr = &history[histype][hisidx[histype]];
5894 vim_free(hisptr->hisstr);
5895
5896 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005897 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
5899 if (hisptr->hisstr != NULL)
5900 hisptr->hisstr[len + 1] = sep;
5901
5902 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005903 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005904 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 if (histype == HIST_SEARCH && in_map)
5906 last_maptick = maptick;
5907 }
5908}
5909
5910#if defined(FEAT_EVAL) || defined(PROTO)
5911
5912/*
5913 * Get identifier of newest history entry.
5914 * "histype" may be one of the HIST_ values.
5915 */
5916 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005917get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918{
5919 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5920 || hisidx[histype] < 0)
5921 return -1;
5922
5923 return history[histype][hisidx[histype]].hisnum;
5924}
5925
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00005926/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 * Calculate history index from a number:
5928 * num > 0: seen as identifying number of a history entry
5929 * num < 0: relative position in history wrt newest entry
5930 * "histype" may be one of the HIST_ values.
5931 */
5932 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005933calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934{
5935 int i;
5936 histentry_T *hist;
5937 int wrapped = FALSE;
5938
5939 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
5940 || (i = hisidx[histype]) < 0 || num == 0)
5941 return -1;
5942
5943 hist = history[histype];
5944 if (num > 0)
5945 {
5946 while (hist[i].hisnum > num)
5947 if (--i < 0)
5948 {
5949 if (wrapped)
5950 break;
5951 i += hislen;
5952 wrapped = TRUE;
5953 }
5954 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
5955 return i;
5956 }
5957 else if (-num <= hislen)
5958 {
5959 i += num + 1;
5960 if (i < 0)
5961 i += hislen;
5962 if (hist[i].hisstr != NULL)
5963 return i;
5964 }
5965 return -1;
5966}
5967
5968/*
5969 * Get a history entry by its index.
5970 * "histype" may be one of the HIST_ values.
5971 */
5972 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005973get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 idx = calc_hist_idx(histype, idx);
5976 if (idx >= 0)
5977 return history[histype][idx].hisstr;
5978 else
5979 return (char_u *)"";
5980}
5981
5982/*
5983 * Clear all entries of a history.
5984 * "histype" may be one of the HIST_ values.
5985 */
5986 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int i;
5990 histentry_T *hisptr;
5991
5992 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
5993 {
5994 hisptr = history[histype];
5995 for (i = hislen; i--;)
5996 {
5997 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005998 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01005999 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
6001 hisidx[histype] = -1; /* mark history as cleared */
6002 hisnum[histype] = 0; /* reset identifier counter */
6003 return OK;
6004 }
6005 return FAIL;
6006}
6007
6008/*
6009 * Remove all entries matching {str} from a history.
6010 * "histype" may be one of the HIST_ values.
6011 */
6012 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006013del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006014{
6015 regmatch_T regmatch;
6016 histentry_T *hisptr;
6017 int idx;
6018 int i;
6019 int last;
6020 int found = FALSE;
6021
6022 regmatch.regprog = NULL;
6023 regmatch.rm_ic = FALSE; /* always match case */
6024 if (hislen != 0
6025 && histype >= 0
6026 && histype < HIST_COUNT
6027 && *str != NUL
6028 && (idx = hisidx[histype]) >= 0
6029 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6030 != NULL)
6031 {
6032 i = last = idx;
6033 do
6034 {
6035 hisptr = &history[histype][i];
6036 if (hisptr->hisstr == NULL)
6037 break;
6038 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6039 {
6040 found = TRUE;
6041 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006042 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 }
6044 else
6045 {
6046 if (i != last)
6047 {
6048 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006049 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 }
6051 if (--last < 0)
6052 last += hislen;
6053 }
6054 if (--i < 0)
6055 i += hislen;
6056 } while (i != idx);
6057 if (history[histype][idx].hisstr == NULL)
6058 hisidx[histype] = -1;
6059 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006060 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 return found;
6062}
6063
6064/*
6065 * Remove an indexed entry from a history.
6066 * "histype" may be one of the HIST_ values.
6067 */
6068 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 int i, j;
6072
6073 i = calc_hist_idx(histype, idx);
6074 if (i < 0)
6075 return FALSE;
6076 idx = hisidx[histype];
6077 vim_free(history[histype][i].hisstr);
6078
6079 /* When deleting the last added search string in a mapping, reset
6080 * last_maptick, so that the last added search string isn't deleted again.
6081 */
6082 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6083 last_maptick = -1;
6084
6085 while (i != idx)
6086 {
6087 j = (i + 1) % hislen;
6088 history[histype][i] = history[histype][j];
6089 i = j;
6090 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006091 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (--i < 0)
6093 i += hislen;
6094 hisidx[histype] = i;
6095 return TRUE;
6096}
6097
6098#endif /* FEAT_EVAL */
6099
6100#if defined(FEAT_CRYPT) || defined(PROTO)
6101/*
6102 * Very specific function to remove the value in ":set key=val" from the
6103 * history.
6104 */
6105 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006106remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107{
6108 char_u *p;
6109 int i;
6110
6111 i = hisidx[HIST_CMD];
6112 if (i < 0)
6113 return;
6114 p = history[HIST_CMD][i].hisstr;
6115 if (p != NULL)
6116 for ( ; *p; ++p)
6117 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6118 {
6119 p = vim_strchr(p + 3, '=');
6120 if (p == NULL)
6121 break;
6122 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006123 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (p[i] == '\\' && p[i + 1])
6125 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006126 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 --p;
6128 }
6129}
6130#endif
6131
6132#endif /* FEAT_CMDHIST */
6133
Bram Moolenaar064154c2016-03-19 22:50:43 +01006134#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006135/*
6136 * Get pointer to the command line info to use. cmdline_paste() may clear
6137 * ccline and put the previous value in prev_ccline.
6138 */
6139 static struct cmdline_info *
6140get_ccline_ptr(void)
6141{
6142 if ((State & CMDLINE) == 0)
6143 return NULL;
6144 if (ccline.cmdbuff != NULL)
6145 return &ccline;
6146 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6147 return &prev_ccline;
6148 return NULL;
6149}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006150#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006151
Bram Moolenaar064154c2016-03-19 22:50:43 +01006152#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006153/*
6154 * Get the current command line in allocated memory.
6155 * Only works when the command line is being edited.
6156 * Returns NULL when something is wrong.
6157 */
6158 char_u *
6159get_cmdline_str(void)
6160{
6161 struct cmdline_info *p = get_ccline_ptr();
6162
6163 if (p == NULL)
6164 return NULL;
6165 return vim_strnsave(p->cmdbuff, p->cmdlen);
6166}
6167
6168/*
6169 * Get the current command line position, counted in bytes.
6170 * Zero is the first position.
6171 * Only works when the command line is being edited.
6172 * Returns -1 when something is wrong.
6173 */
6174 int
6175get_cmdline_pos(void)
6176{
6177 struct cmdline_info *p = get_ccline_ptr();
6178
6179 if (p == NULL)
6180 return -1;
6181 return p->cmdpos;
6182}
6183
6184/*
6185 * Set the command line byte position to "pos". Zero is the first position.
6186 * Only works when the command line is being edited.
6187 * Returns 1 when failed, 0 when OK.
6188 */
6189 int
6190set_cmdline_pos(
6191 int pos)
6192{
6193 struct cmdline_info *p = get_ccline_ptr();
6194
6195 if (p == NULL)
6196 return 1;
6197
6198 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6199 * changed the command line. */
6200 if (pos < 0)
6201 new_cmdpos = 0;
6202 else
6203 new_cmdpos = pos;
6204 return 0;
6205}
6206#endif
6207
6208#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6209/*
6210 * Get the current command-line type.
6211 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6212 * Only works when the command line is being edited.
6213 * Returns NUL when something is wrong.
6214 */
6215 int
6216get_cmdline_type(void)
6217{
6218 struct cmdline_info *p = get_ccline_ptr();
6219
6220 if (p == NULL)
6221 return NUL;
6222 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006223 return
6224# ifdef FEAT_EVAL
6225 (p->input_fn) ? '@' :
6226# endif
6227 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006228 return p->cmdfirstc;
6229}
6230#endif
6231
Bram Moolenaar071d4272004-06-13 20:20:40 +00006232#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6233/*
6234 * Get indices "num1,num2" that specify a range within a list (not a range of
6235 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6236 * Returns OK if parsed successfully, otherwise FAIL.
6237 */
6238 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006239get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240{
6241 int len;
6242 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006243 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244
6245 *str = skipwhite(*str);
6246 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6247 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006248 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 *str += len;
6250 *num1 = (int)num;
6251 first = TRUE;
6252 }
6253 *str = skipwhite(*str);
6254 if (**str == ',') /* parse "to" part of range */
6255 {
6256 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006257 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 if (len > 0)
6259 {
6260 *num2 = (int)num;
6261 *str = skipwhite(*str + len);
6262 }
6263 else if (!first) /* no number given at all */
6264 return FAIL;
6265 }
6266 else if (first) /* only one number given */
6267 *num2 = *num1;
6268 return OK;
6269}
6270#endif
6271
6272#if defined(FEAT_CMDHIST) || defined(PROTO)
6273/*
6274 * :history command - print a history
6275 */
6276 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006277ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278{
6279 histentry_T *hist;
6280 int histype1 = HIST_CMD;
6281 int histype2 = HIST_CMD;
6282 int hisidx1 = 1;
6283 int hisidx2 = -1;
6284 int idx;
6285 int i, j, k;
6286 char_u *end;
6287 char_u *arg = eap->arg;
6288
6289 if (hislen == 0)
6290 {
6291 MSG(_("'history' option is zero"));
6292 return;
6293 }
6294
6295 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6296 {
6297 end = arg;
6298 while (ASCII_ISALPHA(*end)
6299 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6300 end++;
6301 i = *end;
6302 *end = NUL;
6303 histype1 = get_histtype(arg);
6304 if (histype1 == -1)
6305 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006306 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 {
6308 histype1 = 0;
6309 histype2 = HIST_COUNT-1;
6310 }
6311 else
6312 {
6313 *end = i;
6314 EMSG(_(e_trailing));
6315 return;
6316 }
6317 }
6318 else
6319 histype2 = histype1;
6320 *end = i;
6321 }
6322 else
6323 end = arg;
6324 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6325 {
6326 EMSG(_(e_trailing));
6327 return;
6328 }
6329
6330 for (; !got_int && histype1 <= histype2; ++histype1)
6331 {
6332 STRCPY(IObuff, "\n # ");
6333 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6334 MSG_PUTS_TITLE(IObuff);
6335 idx = hisidx[histype1];
6336 hist = history[histype1];
6337 j = hisidx1;
6338 k = hisidx2;
6339 if (j < 0)
6340 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6341 if (k < 0)
6342 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6343 if (idx >= 0 && j <= k)
6344 for (i = idx + 1; !got_int; ++i)
6345 {
6346 if (i == hislen)
6347 i = 0;
6348 if (hist[i].hisstr != NULL
6349 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6350 {
6351 msg_putchar('\n');
6352 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6353 hist[i].hisnum);
6354 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6355 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006356 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 else
6358 STRCAT(IObuff, hist[i].hisstr);
6359 msg_outtrans(IObuff);
6360 out_flush();
6361 }
6362 if (i == idx)
6363 break;
6364 }
6365 }
6366}
6367#endif
6368
6369#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006370/*
6371 * Buffers for history read from a viminfo file. Only valid while reading.
6372 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006373static histentry_T *viminfo_history[HIST_COUNT] =
6374 {NULL, NULL, NULL, NULL, NULL};
6375static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6376static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377static int viminfo_add_at_front = FALSE;
6378
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006379static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
6381/*
6382 * Translate a history type number to the associated character.
6383 */
6384 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006385hist_type2char(
6386 int type,
6387 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388{
6389 if (type == HIST_CMD)
6390 return ':';
6391 if (type == HIST_SEARCH)
6392 {
6393 if (use_question)
6394 return '?';
6395 else
6396 return '/';
6397 }
6398 if (type == HIST_EXPR)
6399 return '=';
6400 return '@';
6401}
6402
6403/*
6404 * Prepare for reading the history from the viminfo file.
6405 * This allocates history arrays to store the read history lines.
6406 */
6407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006408prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 int i;
6411 int num;
6412 int type;
6413 int len;
6414
6415 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006416 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 if (asklen > hislen)
6418 asklen = hislen;
6419
6420 for (type = 0; type < HIST_COUNT; ++type)
6421 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006422 /* Count the number of empty spaces in the history list. Entries read
6423 * from viminfo previously are also considered empty. If there are
6424 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006426 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427 num++;
6428 len = asklen;
6429 if (num > len)
6430 len = num;
6431 if (len <= 0)
6432 viminfo_history[type] = NULL;
6433 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006434 viminfo_history[type] = (histentry_T *)lalloc(
6435 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 if (viminfo_history[type] == NULL)
6437 len = 0;
6438 viminfo_hislen[type] = len;
6439 viminfo_hisidx[type] = 0;
6440 }
6441}
6442
6443/*
6444 * Accept a line from the viminfo, store it in the history array when it's
6445 * new.
6446 */
6447 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006448read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449{
6450 int type;
6451 long_u len;
6452 char_u *val;
6453 char_u *p;
6454
6455 type = hist_char2type(virp->vir_line[0]);
6456 if (viminfo_hisidx[type] < viminfo_hislen[type])
6457 {
6458 val = viminfo_readstring(virp, 1, TRUE);
6459 if (val != NULL && *val != NUL)
6460 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006461 int sep = (*val == ' ' ? NUL : *val);
6462
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006464 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 {
6466 /* Need to re-allocate to append the separator byte. */
6467 len = STRLEN(val);
6468 p = lalloc(len + 2, TRUE);
6469 if (p != NULL)
6470 {
6471 if (type == HIST_SEARCH)
6472 {
6473 /* Search entry: Move the separator from the first
6474 * column to after the NUL. */
6475 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006476 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 }
6478 else
6479 {
6480 /* Not a search entry: No separator in the viminfo
6481 * file, add a NUL separator. */
6482 mch_memmove(p, val, (size_t)len + 1);
6483 p[len + 1] = NUL;
6484 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006485 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6486 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006487 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6488 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006489 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 }
6491 }
6492 }
6493 vim_free(val);
6494 }
6495 return viminfo_readline(virp);
6496}
6497
Bram Moolenaar07219f92013-04-14 23:19:36 +02006498/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006499 * Accept a new style history line from the viminfo, store it in the history
6500 * array when it's new.
6501 */
6502 void
6503handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006504 garray_T *values,
6505 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006506{
6507 int type;
6508 long_u len;
6509 char_u *val;
6510 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006511 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006512
6513 /* Check the format:
6514 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006515 if (values->ga_len < 4
6516 || vp[0].bv_type != BVAL_NR
6517 || vp[1].bv_type != BVAL_NR
6518 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6519 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006520 return;
6521
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006522 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006523 if (type >= HIST_COUNT)
6524 return;
6525 if (viminfo_hisidx[type] < viminfo_hislen[type])
6526 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006527 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006528 if (val != NULL && *val != NUL)
6529 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006530 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6531 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006532 int idx;
6533 int overwrite = FALSE;
6534
6535 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6536 {
6537 /* If lines were written by an older Vim we need to avoid
6538 * getting duplicates. See if the entry already exists. */
6539 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6540 {
6541 p = viminfo_history[type][idx].hisstr;
6542 if (STRCMP(val, p) == 0
6543 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6544 {
6545 overwrite = TRUE;
6546 break;
6547 }
6548 }
6549
6550 if (!overwrite)
6551 {
6552 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006553 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006554 p = lalloc(len + 2, TRUE);
6555 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006556 else
6557 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006558 if (p != NULL)
6559 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006560 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006561 if (!overwrite)
6562 {
6563 mch_memmove(p, val, (size_t)len + 1);
6564 /* Put the separator after the NUL. */
6565 p[len + 1] = sep;
6566 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006567 viminfo_history[type][idx].hisnum = 0;
6568 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006569 viminfo_hisidx[type]++;
6570 }
6571 }
6572 }
6573 }
6574 }
6575}
6576
6577/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006578 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006579 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006580 static void
6581concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582{
6583 int idx;
6584 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006585
6586 idx = hisidx[type] + viminfo_hisidx[type];
6587 if (idx >= hislen)
6588 idx -= hislen;
6589 else if (idx < 0)
6590 idx = hislen - 1;
6591 if (viminfo_add_at_front)
6592 hisidx[type] = idx;
6593 else
6594 {
6595 if (hisidx[type] == -1)
6596 hisidx[type] = hislen - 1;
6597 do
6598 {
6599 if (history[type][idx].hisstr != NULL
6600 || history[type][idx].viminfo)
6601 break;
6602 if (++idx == hislen)
6603 idx = 0;
6604 } while (idx != hisidx[type]);
6605 if (idx != hisidx[type] && --idx < 0)
6606 idx = hislen - 1;
6607 }
6608 for (i = 0; i < viminfo_hisidx[type]; i++)
6609 {
6610 vim_free(history[type][idx].hisstr);
6611 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6612 history[type][idx].viminfo = TRUE;
6613 history[type][idx].time_set = viminfo_history[type][i].time_set;
6614 if (--idx < 0)
6615 idx = hislen - 1;
6616 }
6617 idx += 1;
6618 idx %= hislen;
6619 for (i = 0; i < viminfo_hisidx[type]; i++)
6620 {
6621 history[type][idx++].hisnum = ++hisnum[type];
6622 idx %= hislen;
6623 }
6624}
6625
6626#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6627 static int
6628#ifdef __BORLANDC__
6629_RTLENTRYF
6630#endif
6631sort_hist(const void *s1, const void *s2)
6632{
6633 histentry_T *p1 = *(histentry_T **)s1;
6634 histentry_T *p2 = *(histentry_T **)s2;
6635
6636 if (p1->time_set < p2->time_set) return -1;
6637 if (p1->time_set > p2->time_set) return 1;
6638 return 0;
6639}
6640#endif
6641
6642/*
6643 * Merge history lines from viminfo and lines typed in this Vim based on the
6644 * timestamp;
6645 */
6646 static void
6647merge_history(int type)
6648{
6649 int max_len;
6650 histentry_T **tot_hist;
6651 histentry_T *new_hist;
6652 int i;
6653 int len;
6654
6655 /* Make one long list with all entries. */
6656 max_len = hislen + viminfo_hisidx[type];
6657 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6658 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6659 if (tot_hist == NULL || new_hist == NULL)
6660 {
6661 vim_free(tot_hist);
6662 vim_free(new_hist);
6663 return;
6664 }
6665 for (i = 0; i < viminfo_hisidx[type]; i++)
6666 tot_hist[i] = &viminfo_history[type][i];
6667 len = i;
6668 for (i = 0; i < hislen; i++)
6669 if (history[type][i].hisstr != NULL)
6670 tot_hist[len++] = &history[type][i];
6671
6672 /* Sort the list on timestamp. */
6673 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6674
6675 /* Keep the newest ones. */
6676 for (i = 0; i < hislen; i++)
6677 {
6678 if (i < len)
6679 {
6680 new_hist[i] = *tot_hist[i];
6681 tot_hist[i]->hisstr = NULL;
6682 if (new_hist[i].hisnum == 0)
6683 new_hist[i].hisnum = ++hisnum[type];
6684 }
6685 else
6686 clear_hist_entry(&new_hist[i]);
6687 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006688 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006689
6690 /* Free what is not kept. */
6691 for (i = 0; i < viminfo_hisidx[type]; i++)
6692 vim_free(viminfo_history[type][i].hisstr);
6693 for (i = 0; i < hislen; i++)
6694 vim_free(history[type][i].hisstr);
6695 vim_free(history[type]);
6696 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006697 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006698}
6699
6700/*
6701 * Finish reading history lines from viminfo. Not used when writing viminfo.
6702 */
6703 void
6704finish_viminfo_history(vir_T *virp)
6705{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006707 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708
6709 for (type = 0; type < HIST_COUNT; ++type)
6710 {
6711 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006712 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006713
6714 if (merge)
6715 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006717 concat_history(type);
6718
Bram Moolenaard23a8232018-02-10 18:45:26 +01006719 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006720 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 }
6722}
6723
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006724/*
6725 * Write history to viminfo file in "fp".
6726 * When "merge" is TRUE merge history lines with a previously read viminfo
6727 * file, data is in viminfo_history[].
6728 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006731write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006732{
6733 int i;
6734 int type;
6735 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006736 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737
6738 init_history();
6739 if (hislen == 0)
6740 return;
6741 for (type = 0; type < HIST_COUNT; ++type)
6742 {
6743 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6744 if (num_saved == 0)
6745 continue;
6746 if (num_saved < 0) /* Use default */
6747 num_saved = hislen;
6748 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6749 type == HIST_CMD ? _("Command Line") :
6750 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006751 type == HIST_EXPR ? _("Expression") :
6752 type == HIST_INPUT ? _("Input Line") :
6753 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006754 if (num_saved > hislen)
6755 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006756
6757 /*
6758 * Merge typed and viminfo history:
6759 * round 1: history of typed commands.
6760 * round 2: history from recently read viminfo.
6761 */
6762 for (round = 1; round <= 2; ++round)
6763 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006764 if (round == 1)
6765 /* start at newest entry, somewhere in the list */
6766 i = hisidx[type];
6767 else if (viminfo_hisidx[type] > 0)
6768 /* start at newest entry, first in the list */
6769 i = 0;
6770 else
6771 /* empty list */
6772 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006773 if (i >= 0)
6774 while (num_saved > 0
6775 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 char_u *p;
6778 time_t timestamp;
6779 int c = NUL;
6780
6781 if (round == 1)
6782 {
6783 p = history[type][i].hisstr;
6784 timestamp = history[type][i].time_set;
6785 }
6786 else
6787 {
6788 p = viminfo_history[type] == NULL ? NULL
6789 : viminfo_history[type][i].hisstr;
6790 timestamp = viminfo_history[type] == NULL ? 0
6791 : viminfo_history[type][i].time_set;
6792 }
6793
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006794 if (p != NULL && (round == 2
6795 || !merge
6796 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006798 --num_saved;
6799 fputc(hist_type2char(type, TRUE), fp);
6800 /* For the search history: put the separator in the
6801 * second column; use a space if there isn't one. */
6802 if (type == HIST_SEARCH)
6803 {
6804 c = p[STRLEN(p) + 1];
6805 putc(c == NUL ? ' ' : c, fp);
6806 }
6807 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808
6809 {
6810 char cbuf[NUMBUFLEN];
6811
6812 /* New style history with a bar line. Format:
6813 * |{bartype},{histtype},{timestamp},{separator},"text" */
6814 if (c == NUL)
6815 cbuf[0] = NUL;
6816 else
6817 sprintf(cbuf, "%d", c);
6818 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6819 type, (long)timestamp, cbuf);
6820 barline_writestring(fp, p, LSIZE - 20);
6821 putc('\n', fp);
6822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006824 if (round == 1)
6825 {
6826 /* Decrement index, loop around and stop when back at
6827 * the start. */
6828 if (--i < 0)
6829 i = hislen - 1;
6830 if (i == hisidx[type])
6831 break;
6832 }
6833 else
6834 {
6835 /* Increment index. Stop at the end in the while. */
6836 ++i;
6837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006838 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006839 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006840 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006841 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006843 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006844 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 }
6846}
6847#endif /* FEAT_VIMINFO */
6848
6849#if defined(FEAT_FKMAP) || defined(PROTO)
6850/*
6851 * Write a character at the current cursor+offset position.
6852 * It is directly written into the command buffer block.
6853 */
6854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006855cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006856{
6857 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6858 {
6859 EMSG(_("E198: cmd_pchar beyond the command length"));
6860 return;
6861 }
6862 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6863 ccline.cmdbuff[ccline.cmdlen] = NUL;
6864}
6865
6866 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006867cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868{
6869 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6870 {
6871 /* EMSG(_("cmd_gchar beyond the command length")); */
6872 return NUL;
6873 }
6874 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6875}
6876#endif
6877
6878#if defined(FEAT_CMDWIN) || defined(PROTO)
6879/*
6880 * Open a window on the current command line and history. Allow editing in
6881 * the window. Returns when the window is closed.
6882 * Returns:
6883 * CR if the command is to be executed
6884 * Ctrl_C if it is to be abandoned
6885 * K_IGNORE if editing continues
6886 */
6887 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006888open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889{
6890 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006891 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006893 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 win_T *wp;
6895 int i;
6896 linenr_T lnum;
6897 int histtype;
6898 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899 int save_restart_edit = restart_edit;
6900 int save_State = State;
6901 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00006902#ifdef FEAT_RIGHTLEFT
6903 int save_cmdmsg_rl = cmdmsg_rl;
6904#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02006905#ifdef FEAT_FOLDING
6906 int save_KeyTyped;
6907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908
6909 /* Can't do this recursively. Can't do it when typing a password. */
6910 if (cmdwin_type != 0
6911# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
6912 || cmdline_star > 0
6913# endif
6914 )
6915 {
6916 beep_flush();
6917 return K_IGNORE;
6918 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02006919 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 /* Save current window sizes. */
6922 win_size_save(&winsizes);
6923
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006925 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006926
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006927 /* don't use a new tab page */
6928 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02006929 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00006930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 /* Create a window for the command-line buffer. */
6932 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
6933 {
6934 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006935 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 return K_IGNORE;
6937 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006938 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
6940 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00006941 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00006942 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00006945#ifdef FEAT_FOLDING
6946 curwin->w_p_fen = FALSE;
6947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00006949 curwin->w_p_rl = cmdmsg_rl;
6950 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02006952 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006955 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02006956 /* But don't allow switching to another buffer. */
6957 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958
Bram Moolenaar46152342005-09-07 21:18:43 +00006959 /* Showing the prompt may have set need_wait_return, reset it. */
6960 need_wait_return = FALSE;
6961
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00006962 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
6964 {
6965 if (p_wc == TAB)
6966 {
6967 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
6968 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
6969 }
6970 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
6971 }
Bram Moolenaar18141832017-06-25 21:17:25 +02006972 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006974 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
6975 * sets 'textwidth' to 78). */
6976 curbuf->b_p_tw = 0;
6977
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 /* Fill the buffer with the history. */
6979 init_history();
6980 if (hislen > 0)
6981 {
6982 i = hisidx[histtype];
6983 if (i >= 0)
6984 {
6985 lnum = 0;
6986 do
6987 {
6988 if (++i == hislen)
6989 i = 0;
6990 if (history[histtype][i].hisstr != NULL)
6991 ml_append(lnum++, history[histtype][i].hisstr,
6992 (colnr_T)0, FALSE);
6993 }
6994 while (i != hisidx[histtype]);
6995 }
6996 }
6997
6998 /* Replace the empty last line with the current command-line and put the
6999 * cursor there. */
7000 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7001 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7002 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007003 changed_line_abv_curs();
7004 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007005 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006
7007 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007008 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009
7010 /* No Ex mode here! */
7011 exmode_active = 0;
7012
7013 State = NORMAL;
7014# ifdef FEAT_MOUSE
7015 setmouse();
7016# endif
7017
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007019 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007020 if (restart_edit != 0) /* autocmd with ":startinsert" */
7021 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022
7023 i = RedrawingDisabled;
7024 RedrawingDisabled = 0;
7025
7026 /*
7027 * Call the main loop until <CR> or CTRL-C is typed.
7028 */
7029 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007030 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031
7032 RedrawingDisabled = i;
7033
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007034# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007035 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007036# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007037
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007039 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007040
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007041# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007042 /* Restore KeyTyped in case it is modified by autocommands */
7043 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044# endif
7045
Bram Moolenaarccc18222007-05-10 18:25:20 +00007046 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007047 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 cmdwin_type = 0;
7049
7050 exmode_active = save_exmode;
7051
Bram Moolenaarf9821062008-06-20 16:31:07 +00007052 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007054 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 {
7056 cmdwin_result = Ctrl_C;
7057 EMSG(_("E199: Active window or buffer deleted"));
7058 }
7059 else
7060 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007061# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 /* autocmds may abort script processing */
7063 if (aborting() && cmdwin_result != K_IGNORE)
7064 cmdwin_result = Ctrl_C;
7065# endif
7066 /* Set the new command line from the cmdline buffer. */
7067 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007068 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007070 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7071
7072 if (histtype == HIST_CMD)
7073 {
7074 /* Execute the command directly. */
7075 ccline.cmdbuff = vim_strsave((char_u *)p);
7076 cmdwin_result = CAR;
7077 }
7078 else
7079 {
7080 /* First need to cancel what we were doing. */
7081 ccline.cmdbuff = NULL;
7082 stuffcharReadbuff(':');
7083 stuffReadbuff((char_u *)p);
7084 stuffcharReadbuff(CAR);
7085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 }
7087 else if (cmdwin_result == K_XF2) /* :qa typed */
7088 {
7089 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7090 cmdwin_result = CAR;
7091 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007092 else if (cmdwin_result == Ctrl_C)
7093 {
7094 /* :q or :close, don't execute any command
7095 * and don't modify the cmd window. */
7096 ccline.cmdbuff = NULL;
7097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 else
7099 ccline.cmdbuff = vim_strsave(ml_get_curline());
7100 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007101 {
7102 ccline.cmdbuff = vim_strsave((char_u *)"");
7103 ccline.cmdlen = 0;
7104 ccline.cmdbufflen = 1;
7105 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 else
7109 {
7110 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7111 ccline.cmdbufflen = ccline.cmdlen + 1;
7112 ccline.cmdpos = curwin->w_cursor.col;
7113 if (ccline.cmdpos > ccline.cmdlen)
7114 ccline.cmdpos = ccline.cmdlen;
7115 if (cmdwin_result == K_IGNORE)
7116 {
7117 set_cmdspos_cursor();
7118 redrawcmd();
7119 }
7120 }
7121
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007123 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007124# ifdef FEAT_CONCEAL
7125 /* Avoid command-line window first character being concealed. */
7126 curwin->w_p_cole = 0;
7127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007129 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 win_goto(old_curwin);
7131 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007132
7133 /* win_close() may have already wiped the buffer when 'bh' is
7134 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007135 if (bufref_valid(&bufref))
7136 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 /* Restore window sizes. */
7139 win_size_restore(&winsizes);
7140
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007141 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
7143
7144 ga_clear(&winsizes);
7145 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007146# ifdef FEAT_RIGHTLEFT
7147 cmdmsg_rl = save_cmdmsg_rl;
7148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 State = save_State;
7151# ifdef FEAT_MOUSE
7152 setmouse();
7153# endif
7154
7155 return cmdwin_result;
7156}
7157#endif /* FEAT_CMDWIN */
7158
7159/*
7160 * Used for commands that either take a simple command string argument, or:
7161 * cmd << endmarker
7162 * {script}
7163 * endmarker
7164 * Returns a pointer to allocated memory with {script} or NULL.
7165 */
7166 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007167script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 char_u *theline;
7170 char *end_pattern = NULL;
7171 char dot[] = ".";
7172 garray_T ga;
7173
7174 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7175 return NULL;
7176
7177 ga_init2(&ga, 1, 0x400);
7178
7179 if (cmd[2] != NUL)
7180 end_pattern = (char *)skipwhite(cmd + 2);
7181 else
7182 end_pattern = dot;
7183
7184 for (;;)
7185 {
7186 theline = eap->getline(
7187#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007188 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189#endif
7190 NUL, eap->cookie, 0);
7191
7192 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007193 {
7194 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197
7198 ga_concat(&ga, theline);
7199 ga_append(&ga, '\n');
7200 vim_free(theline);
7201 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007202 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203
7204 return (char_u *)ga.ga_data;
7205}
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02007206
7207#ifdef FEAT_SEARCH_EXTRA
7208 static void
7209set_search_match(pos_T *t)
7210{
7211 /*
7212 * First move cursor to end of match, then to the start. This
7213 * moves the whole match onto the screen when 'nowrap' is set.
7214 */
7215 t->lnum += search_match_lines;
7216 t->col = search_match_endcol;
7217 if (t->lnum > curbuf->b_ml.ml_line_count)
7218 {
7219 t->lnum = curbuf->b_ml.ml_line_count;
7220 coladvance((colnr_T)MAXCOL);
7221 }
7222}
7223#endif