blob: 07032bb542d7c444a991f7e8d90fdf4137659ed5 [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
144
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200146 static void
147trigger_cmd_autocmd(int typechar, int evt)
148{
149 char_u typestr[2];
150
151 typestr[0] = typechar;
152 typestr[1] = NUL;
153 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
154}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200157 * Abandon the command line.
158 */
159 static void
160abandon_cmdline(void)
161{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100162 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200163 if (msg_scrolled == 0)
164 compute_cmdrow();
165 MSG("");
166 redraw_cmdline = TRUE;
167}
168
Bram Moolenaaree219b02017-12-17 14:55:01 +0100169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200170/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100171 * Guess that the pattern matches everything. Only finds specific cases, such
172 * as a trailing \|, which can happen while typing a pattern.
173 */
174 static int
175empty_pattern(char_u *p)
176{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100177 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100178
179 /* remove trailing \v and the like */
180 while (n >= 2 && p[n - 2] == '\\'
181 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
182 n -= 2;
183 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
184}
185
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200187typedef struct {
188 colnr_T vs_curswant;
189 colnr_T vs_leftcol;
190 linenr_T vs_topline;
191# ifdef FEAT_DIFF
192 int vs_topfill;
193# endif
194 linenr_T vs_botline;
195 linenr_T vs_empty_rows;
196} viewstate_T;
197
198 static void
199save_viewstate(viewstate_T *vs)
200{
201 vs->vs_curswant = curwin->w_curswant;
202 vs->vs_leftcol = curwin->w_leftcol;
203 vs->vs_topline = curwin->w_topline;
204# ifdef FEAT_DIFF
205 vs->vs_topfill = curwin->w_topfill;
206# endif
207 vs->vs_botline = curwin->w_botline;
208 vs->vs_empty_rows = curwin->w_empty_rows;
209}
210
211 static void
212restore_viewstate(viewstate_T *vs)
213{
214 curwin->w_curswant = vs->vs_curswant;
215 curwin->w_leftcol = vs->vs_leftcol;
216 curwin->w_topline = vs->vs_topline;
217# ifdef FEAT_DIFF
218 curwin->w_topfill = vs->vs_topfill;
219# endif
220 curwin->w_botline = vs->vs_botline;
221 curwin->w_empty_rows = vs->vs_empty_rows;
222}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223
224// Struct to store the state of 'incsearch' highlighting.
225typedef struct {
226 pos_T search_start; // where 'incsearch' starts searching
227 pos_T save_cursor;
228 viewstate_T init_viewstate;
229 viewstate_T old_viewstate;
230 pos_T match_start;
231 pos_T match_end;
232 int did_incsearch;
233 int incsearch_postponed;
234} incsearch_state_T;
235
236 static void
237init_incsearch_state(incsearch_state_T *is_state)
238{
239 is_state->match_start = curwin->w_cursor;
240 is_state->did_incsearch = FALSE;
241 is_state->incsearch_postponed = FALSE;
242 CLEAR_POS(&is_state->match_end);
243 is_state->save_cursor = curwin->w_cursor; // may be restored later
244 is_state->search_start = curwin->w_cursor;
245 save_viewstate(&is_state->init_viewstate);
246 save_viewstate(&is_state->old_viewstate);
247}
248
249/*
250 * First move cursor to end of match, then to the start. This
251 * moves the whole match onto the screen when 'nowrap' is set.
252 */
253 static void
254set_search_match(pos_T *t)
255{
256 t->lnum += search_match_lines;
257 t->col = search_match_endcol;
258 if (t->lnum > curbuf->b_ml.ml_line_count)
259 {
260 t->lnum = curbuf->b_ml.ml_line_count;
261 coladvance((colnr_T)MAXCOL);
262 }
263}
264
265/*
266 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200267 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200268 */
269 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200270do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
271 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200272{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
276 if (p_is && !cmd_silent)
277 {
278 // by default search all lines
279 search_first_line = 0;
280 search_last_line = MAXLNUM;
281
282 if (firstc == '/' || firstc == '?')
283 return TRUE;
284 if (firstc == ':')
285 {
286 char_u *cmd = skip_range(ccline.cmdbuff, NULL);
287 char_u *p;
288 int delim;
289 char_u *end;
290
291 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
292 {
293 // Skip over "substitute" to find the pattern separator.
294 for (p = cmd; ASCII_ISALPHA(*p); ++p)
295 ;
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200296 if (*p != NUL
297 && (STRNCMP(cmd, "substitute", p - cmd) == 0
298 || STRNCMP(cmd, "global", p - cmd) == 0
299 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200300 {
301 delim = *p++;
302 end = skip_regexp(p, delim, p_magic, NULL);
303 if (end > p)
304 {
305 char_u *dummy;
306 exarg_T ea;
307 pos_T save_cursor = curwin->w_cursor;
308
309 // found a non-empty pattern
310 *skiplen = (int)(p - ccline.cmdbuff);
311 *patlen = (int)(end - p);
312
313 // parse the address range
314 vim_memset(&ea, 0, sizeof(ea));
315 ea.line1 = 1;
316 ea.line2 = 1;
317 ea.cmd = ccline.cmdbuff;
318 ea.addr_type = ADDR_LINES;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200319 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200320 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200321 if (ea.addr_count > 0)
322 {
323 search_first_line = ea.line1;
324 search_last_line = ea.line2;
325 }
326 else if (*cmd == 's')
327 {
328 // :s defaults to the current line
329 search_first_line = curwin->w_cursor.lnum;
330 search_last_line = curwin->w_cursor.lnum;
331 }
332
333 curwin->w_cursor = save_cursor;
334 return TRUE;
335 }
336 }
337 }
338 }
339 }
340
341 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200342}
343
344/*
345 * Do 'incsearch' highlighting if desired.
346 */
347 static void
348may_do_incsearch_highlighting(
349 int firstc,
350 long count,
351 incsearch_state_T *is_state)
352{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200353 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200354 int i;
355 pos_T end_pos;
356 struct cmdline_info save_ccline;
357#ifdef FEAT_RELTIME
358 proftime_T tm;
359#endif
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200360 int c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200361
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200362 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200363 return;
364
365 // If there is a character waiting, search and redraw later.
366 if (char_avail())
367 {
368 is_state->incsearch_postponed = TRUE;
369 return;
370 }
371 is_state->incsearch_postponed = FALSE;
372
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200373 if (search_first_line == 0)
374 // start at the original cursor position
375 curwin->w_cursor = is_state->search_start;
376 else
377 {
378 // start at the first line in the range
379 curwin->w_cursor.lnum = search_first_line;
380 curwin->w_cursor.col = 0;
381 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200382 save_last_search_pattern();
383
384 // If there is no command line, don't do anything.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200385 if (patlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200386 {
387 i = 0;
388 set_no_hlsearch(TRUE); // turn off previous highlight
389 redraw_all_later(SOME_VALID);
390 }
391 else
392 {
393 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
394
395 cursor_off(); // so the user knows we're busy
396 out_flush();
397 ++emsg_off; // so it doesn't beep if bad expr
398#ifdef FEAT_RELTIME
399 // Set the time limit to half a second.
400 profile_setlimit(500L, &tm);
401#endif
402 if (!p_hls)
403 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200404 if (search_first_line != 0)
405 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200406 c = ccline.cmdbuff[skiplen + patlen];
407 ccline.cmdbuff[skiplen + patlen] = NUL;
408 i = do_search(NULL, firstc == ':' ? '/' : firstc,
409 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200410#ifdef FEAT_RELTIME
411 &tm, NULL
412#else
413 NULL, NULL
414#endif
415 );
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200416 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200417 --emsg_off;
418
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200419 if (curwin->w_cursor.lnum < search_first_line
420 || curwin->w_cursor.lnum > search_last_line)
421 // match outside of address range
422 i = 0;
423
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200424 // if interrupted while searching, behave like it failed
425 if (got_int)
426 {
427 (void)vpeekc(); // remove <C-C> from input stream
428 got_int = FALSE; // don't abandon the command line
429 i = 0;
430 }
431 else if (char_avail())
432 // cancelled searching because a char was typed
433 is_state->incsearch_postponed = TRUE;
434 }
435 if (i != 0)
436 highlight_match = TRUE; // highlight position
437 else
438 highlight_match = FALSE; // remove highlight
439
440 // First restore the old curwin values, so the screen is positioned in the
441 // same way as the actual search command.
442 restore_viewstate(&is_state->old_viewstate);
443 changed_cline_bef_curs();
444 update_topline();
445
446 if (i != 0)
447 {
448 pos_T save_pos = curwin->w_cursor;
449
450 is_state->match_start = curwin->w_cursor;
451 set_search_match(&curwin->w_cursor);
452 validate_cursor();
453 end_pos = curwin->w_cursor;
454 is_state->match_end = end_pos;
455 curwin->w_cursor = save_pos;
456 }
457 else
458 end_pos = curwin->w_cursor; // shutup gcc 4
459
460 // Disable 'hlsearch' highlighting if the pattern matches everything.
461 // Avoids a flash when typing "foo\|".
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200462 c = ccline.cmdbuff[skiplen + patlen];
463 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464 if (empty_pattern(ccline.cmdbuff))
465 set_no_hlsearch(TRUE);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200466 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200467
468 validate_cursor();
469 // May redraw the status line to show the cursor position.
470 if (p_ru && curwin->w_status_height > 0)
471 curwin->w_redr_status = TRUE;
472
473 save_cmdline(&save_ccline);
474 update_screen(SOME_VALID);
475 restore_cmdline(&save_ccline);
476 restore_last_search_pattern();
477
478 // Leave it at the end to make CTRL-R CTRL-W work.
479 if (i != 0)
480 curwin->w_cursor = end_pos;
481
482 msg_starthere();
483 redrawcmdline();
484 is_state->did_incsearch = TRUE;
485}
486
487/*
488 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
489 * or previous match.
490 * Returns FAIL when jumping to cmdline_not_changed;
491 */
492 static int
493may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200494 int firstc,
495 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200496 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200497 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200498{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200499 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500 pos_T t;
501 char_u *pat;
502 int search_flags = SEARCH_NOOF;
503 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200504 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200505
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200507 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200508 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200509 return FAIL;
510
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200511 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200512 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200513 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200514 skiplen = 0;
515 patlen = STRLEN(pat);
516 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200517 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200518 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200519
520 save_last_search_pattern();
521 cursor_off();
522 out_flush();
523 if (c == Ctrl_G)
524 {
525 t = is_state->match_end;
526 if (LT_POS(is_state->match_start, is_state->match_end))
527 // Start searching at the end of the match not at the beginning of
528 // the next column.
529 (void)decl(&t);
530 search_flags += SEARCH_COL;
531 }
532 else
533 t = is_state->match_start;
534 if (!p_hls)
535 search_flags += SEARCH_KEEP;
536 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200537 save = pat[patlen];
538 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 i = searchit(curwin, curbuf, &t,
540 c == Ctrl_G ? FORWARD : BACKWARD,
541 pat, count, search_flags,
542 RE_SEARCH, 0, NULL, NULL);
543 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200544 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200545 if (i)
546 {
547 is_state->search_start = is_state->match_start;
548 is_state->match_end = t;
549 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200550 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200551 {
552 // Move just before the current match, so that when nv_search
553 // finishes the cursor will be put back on the match.
554 is_state->search_start = t;
555 (void)decl(&is_state->search_start);
556 }
557 else if (c == Ctrl_G && firstc == '?')
558 {
559 // Move just after the current match, so that when nv_search
560 // finishes the cursor will be put back on the match.
561 is_state->search_start = t;
562 (void)incl(&is_state->search_start);
563 }
564 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
565 {
566 // wrap around
567 is_state->search_start = t;
568 if (firstc == '?')
569 (void)incl(&is_state->search_start);
570 else
571 (void)decl(&is_state->search_start);
572 }
573
574 set_search_match(&is_state->match_end);
575 curwin->w_cursor = is_state->match_start;
576 changed_cline_bef_curs();
577 update_topline();
578 validate_cursor();
579 highlight_match = TRUE;
580 save_viewstate(&is_state->old_viewstate);
581 update_screen(NOT_VALID);
582 redrawcmdline();
583 }
584 else
585 vim_beep(BO_ERROR);
586 restore_last_search_pattern();
587 return FAIL;
588}
589
590/*
591 * When CTRL-L typed: add character from the match to the pattern.
592 * May set "*c" to the added character.
593 * Return OK when jumping to cmdline_not_changed.
594 */
595 static int
596may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
597{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200598 int skiplen, patlen;
599
600 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200601 return FAIL;
602
603 // Add a character from under the cursor for 'incsearch'.
604 if (is_state->did_incsearch)
605 {
606 curwin->w_cursor = is_state->match_end;
607 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
608 {
609 *c = gchar_cursor();
610
611 // If 'ignorecase' and 'smartcase' are set and the
612 // command line has no uppercase characters, convert
613 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200614 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200615 *c = MB_TOLOWER(*c);
616 if (*c != NUL)
617 {
618 if (*c == firstc || vim_strchr((char_u *)(
619 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
620 {
621 // put a backslash before special characters
622 stuffcharReadbuff(*c);
623 *c = '\\';
624 }
625 return FAIL;
626 }
627 }
628 }
629 return OK;
630}
631
632 static void
633finish_incsearch_highlighting(int gotesc, incsearch_state_T *is_state)
634{
635 if (is_state->did_incsearch)
636 {
637 if (gotesc)
638 curwin->w_cursor = is_state->save_cursor;
639 else
640 {
641 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
642 {
643 // put the '" mark at the original position
644 curwin->w_cursor = is_state->save_cursor;
645 setpcmark();
646 }
647 curwin->w_cursor = is_state->search_start;
648 }
649 restore_viewstate(&is_state->old_viewstate);
650 highlight_match = FALSE;
651 validate_cursor(); /* needed for TAB */
652 redraw_all_later(SOME_VALID);
653 }
654}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200655#endif
656
Bram Moolenaar66216052017-12-16 16:33:44 +0100657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 * getcmdline() - accept a command line starting with firstc.
659 *
660 * firstc == ':' get ":" command line.
661 * firstc == '/' or '?' get search pattern
662 * firstc == '=' get expression
663 * firstc == '@' get text for input() function
664 * firstc == '>' get text for debug mode
665 * firstc == NUL get text for :insert command
666 * firstc == -1 like NUL, and break on CTRL-C
667 *
668 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
669 * command line.
670 *
671 * Careful: getcmdline() can be called recursively!
672 *
673 * Return pointer to allocated string if there is a commandline, NULL
674 * otherwise.
675 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100677getcmdline(
678 int firstc,
679 long count UNUSED, /* only used for incremental search */
680 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681{
682 int c;
683 int i;
684 int j;
685 int gotesc = FALSE; /* TRUE when <ESC> just typed */
686 int do_abbr; /* when TRUE check for abbr. */
687#ifdef FEAT_CMDHIST
688 char_u *lookfor = NULL; /* string to match */
689 int hiscnt; /* current history line in use */
690 int histype; /* history type to be used */
691#endif
692#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200693 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694#endif
695 int did_wild_list = FALSE; /* did wild_list() recently */
696 int wim_index = 0; /* index in wim_flags[] */
697 int res;
698 int save_msg_scroll = msg_scroll;
699 int save_State = State; /* remember State when called */
700 int some_key_typed = FALSE; /* one of the keys was typed */
701#ifdef FEAT_MOUSE
702 /* mouse drag and release events are ignored, unless they are
703 * preceded with a mouse down event */
704 int ignore_drag_release = TRUE;
705#endif
706#ifdef FEAT_EVAL
707 int break_ctrl_c = FALSE;
708#endif
709 expand_T xpc;
710 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200711#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000712 /* Everything that may work recursively should save and restore the
713 * current command line in save_ccline. That includes update_screen(), a
714 * custom status line may invoke ":normal". */
715 struct cmdline_info save_ccline;
716#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200717 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719#ifdef FEAT_EVAL
720 if (firstc == -1)
721 {
722 firstc = NUL;
723 break_ctrl_c = TRUE;
724 }
725#endif
726#ifdef FEAT_RIGHTLEFT
727 /* start without Hebrew mapping for a command line */
728 if (firstc == ':' || firstc == '=' || firstc == '>')
729 cmd_hkmap = 0;
730#endif
731
732 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200733
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200735 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#endif
737
738 /*
739 * set some variables for redrawcmd()
740 */
741 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000742 ccline.cmdindent = (firstc > 0 ? indent : 0);
743
744 /* alloc initial ccline.cmdbuff */
745 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 if (ccline.cmdbuff == NULL)
747 return NULL; /* out of memory */
748 ccline.cmdlen = ccline.cmdpos = 0;
749 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100750 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000752 /* autoindent for :insert and :append */
753 if (firstc <= 0)
754 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200755 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000756 ccline.cmdbuff[indent] = NUL;
757 ccline.cmdpos = indent;
758 ccline.cmdspos = indent;
759 ccline.cmdlen = indent;
760 }
761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000763 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764
765#ifdef FEAT_RIGHTLEFT
766 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
767 && (firstc == '/' || firstc == '?'))
768 cmdmsg_rl = TRUE;
769 else
770 cmdmsg_rl = FALSE;
771#endif
772
773 redir_off = TRUE; /* don't redirect the typed command */
774 if (!cmd_silent)
775 {
776 i = msg_scrolled;
777 msg_scrolled = 0; /* avoid wait_return message */
778 gotocmdline(TRUE);
779 msg_scrolled += i;
780 redrawcmdprompt(); /* draw prompt or indent */
781 set_cmdspos();
782 }
783 xpc.xp_context = EXPAND_NOTHING;
784 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000785#ifndef BACKSLASH_IN_FILENAME
786 xpc.xp_shell = FALSE;
787#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000789#if defined(FEAT_EVAL)
790 if (ccline.input_fn)
791 {
792 xpc.xp_context = ccline.xp_context;
793 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000794# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000795 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000796# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000797 }
798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 /*
801 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
802 * doing ":@0" when register 0 doesn't contain a CR.
803 */
804 msg_scroll = FALSE;
805
806 State = CMDLINE;
807
808 if (firstc == '/' || firstc == '?' || firstc == '@')
809 {
810 /* Use ":lmap" mappings for search pattern and input(). */
811 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
812 b_im_ptr = &curbuf->b_p_iminsert;
813 else
814 b_im_ptr = &curbuf->b_p_imsearch;
815 if (*b_im_ptr == B_IMODE_LMAP)
816 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100817#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 im_set_active(*b_im_ptr == B_IMODE_IM);
819#endif
820 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100821#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 else if (p_imcmdline)
823 im_set_active(TRUE);
824#endif
825
826#ifdef FEAT_MOUSE
827 setmouse();
828#endif
829#ifdef CURSOR_SHAPE
830 ui_cursor_shape(); /* may show different cursor shape */
831#endif
832
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000833 /* When inside an autocommand for writing "exiting" may be set and
834 * terminal mode set to cooked. Need to set raw mode here then. */
835 settmode(TMODE_RAW);
836
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200837 /* Trigger CmdlineEnter autocommands. */
838 cmdline_type = firstc == NUL ? '-' : firstc;
839 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#ifdef FEAT_CMDHIST
842 init_history();
843 hiscnt = hislen; /* set hiscnt to impossible history value */
844 histype = hist_char2type(firstc);
845#endif
846
847#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000848 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849#endif
850
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200851 /* If something above caused an error, reset the flags, we do want to type
852 * and execute commands. Display may be messed up a bit. */
853 if (did_emsg)
854 redrawcmd();
855 did_emsg = FALSE;
856 got_int = FALSE;
857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 /*
859 * Collect the command string, handling editing keys.
860 */
861 for (;;)
862 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000863 redir_off = TRUE; /* Don't redirect the typed command.
864 Repeated, because a ":redir" inside
865 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866#ifdef USE_ON_FLY_SCROLL
867 dont_scroll = FALSE; /* allow scrolling here */
868#endif
869 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
870
Bram Moolenaar72532d32018-04-07 19:09:09 +0200871 did_emsg = FALSE; /* There can't really be a reason why an error
872 that occurs while typing a command should
873 cause the command not to be executed. */
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000876
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100877 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
878 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000879 do
880 {
881 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100882 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 if (KeyTyped)
885 {
886 some_key_typed = TRUE;
887#ifdef FEAT_RIGHTLEFT
888 if (cmd_hkmap)
889 c = hkmap(c);
890# ifdef FEAT_FKMAP
891 if (cmd_fkmap)
892 c = cmdl_fkmap(c);
893# endif
894 if (cmdmsg_rl && !KeyStuffed)
895 {
896 /* Invert horizontal movements and operations. Only when
897 * typed by the user directly, not when the result of a
898 * mapping. */
899 switch (c)
900 {
901 case K_RIGHT: c = K_LEFT; break;
902 case K_S_RIGHT: c = K_S_LEFT; break;
903 case K_C_RIGHT: c = K_C_LEFT; break;
904 case K_LEFT: c = K_RIGHT; break;
905 case K_S_LEFT: c = K_S_RIGHT; break;
906 case K_C_LEFT: c = K_C_RIGHT; break;
907 }
908 }
909#endif
910 }
911
912 /*
913 * Ignore got_int when CTRL-C was typed here.
914 * Don't ignore it in :global, we really need to break then, e.g., for
915 * ":g/pat/normal /pat" (without the <CR>).
916 * Don't ignore it for the input() function.
917 */
918 if ((c == Ctrl_C
919#ifdef UNIX
920 || c == intr_char
921#endif
922 )
923#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
924 && firstc != '@'
925#endif
926#ifdef FEAT_EVAL
927 && !break_ctrl_c
928#endif
929 && !global_busy)
930 got_int = FALSE;
931
932#ifdef FEAT_CMDHIST
933 /* free old command line when finished moving around in the history
934 * list */
935 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000936 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000937 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 && c != K_PAGEDOWN && c != K_PAGEUP
939 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000940 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100942 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944
945 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000946 * When there are matching completions to select <S-Tab> works like
947 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000949 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 c = Ctrl_P;
951
952#ifdef FEAT_WILDMENU
953 /* Special translations for 'wildmenu' */
954 if (did_wild_list && p_wmnu)
955 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000956 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000958 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 c = Ctrl_N;
960 }
961 /* Hitting CR after "emenu Name.": complete submenu */
962 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
963 && ccline.cmdpos > 1
964 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
965 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
966 && (c == '\n' || c == '\r' || c == K_KENTER))
967 c = K_DOWN;
968#endif
969
970 /* free expanded names when finished walking through matches */
971 if (xpc.xp_numfiles != -1
972 && !(c == p_wc && KeyTyped) && c != p_wcm
973 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
974 && c != Ctrl_L)
975 {
976 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
977 did_wild_list = FALSE;
978#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000979 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980#endif
981 xpc.xp_context = EXPAND_NOTHING;
982 wim_index = 0;
983#ifdef FEAT_WILDMENU
984 if (p_wmnu && wild_menu_showing != 0)
985 {
986 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000987 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000988
989 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000990 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991
992 if (wild_menu_showing == WM_SCROLLED)
993 {
994 /* Entered command line, move it up */
995 cmdline_row--;
996 redrawcmd();
997 }
998 else if (save_p_ls != -1)
999 {
1000 /* restore 'laststatus' and 'winminheight' */
1001 p_ls = save_p_ls;
1002 p_wmh = save_p_wmh;
1003 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001004 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001006 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 redrawcmd();
1008 save_p_ls = -1;
1009 }
1010 else
1011 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 redraw_statuslines();
1014 }
1015 KeyTyped = skt;
1016 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001017 if (ccline.input_fn)
1018 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 }
1020#endif
1021 }
1022
1023#ifdef FEAT_WILDMENU
1024 /* Special translations for 'wildmenu' */
1025 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1026 {
1027 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001028 if (c == K_DOWN && ccline.cmdpos > 0
1029 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001031 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 {
1033 /* Hitting <Up>: Remove one submenu name in front of the
1034 * cursor */
1035 int found = FALSE;
1036
1037 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1038 i = 0;
1039 while (--j > 0)
1040 {
1041 /* check for start of menu name */
1042 if (ccline.cmdbuff[j] == ' '
1043 && ccline.cmdbuff[j - 1] != '\\')
1044 {
1045 i = j + 1;
1046 break;
1047 }
1048 /* check for start of submenu name */
1049 if (ccline.cmdbuff[j] == '.'
1050 && ccline.cmdbuff[j - 1] != '\\')
1051 {
1052 if (found)
1053 {
1054 i = j + 1;
1055 break;
1056 }
1057 else
1058 found = TRUE;
1059 }
1060 }
1061 if (i > 0)
1062 cmdline_del(i);
1063 c = p_wc;
1064 xpc.xp_context = EXPAND_NOTHING;
1065 }
1066 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001067 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001068 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001069 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 {
1071 char_u upseg[5];
1072
1073 upseg[0] = PATHSEP;
1074 upseg[1] = '.';
1075 upseg[2] = '.';
1076 upseg[3] = PATHSEP;
1077 upseg[4] = NUL;
1078
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001079 if (c == K_DOWN
1080 && ccline.cmdpos > 0
1081 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1082 && (ccline.cmdpos < 3
1083 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1085 {
1086 /* go down a directory */
1087 c = p_wc;
1088 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001089 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 {
1091 /* If in a direct ancestor, strip off one ../ to go down */
1092 int found = FALSE;
1093
1094 j = ccline.cmdpos;
1095 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1096 while (--j > i)
1097 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001098#ifdef FEAT_MBYTE
1099 if (has_mbyte)
1100 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 if (vim_ispathsep(ccline.cmdbuff[j]))
1103 {
1104 found = TRUE;
1105 break;
1106 }
1107 }
1108 if (found
1109 && ccline.cmdbuff[j - 1] == '.'
1110 && ccline.cmdbuff[j - 2] == '.'
1111 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1112 {
1113 cmdline_del(j - 2);
1114 c = p_wc;
1115 }
1116 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001117 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 {
1119 /* go up a directory */
1120 int found = FALSE;
1121
1122 j = ccline.cmdpos - 1;
1123 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1124 while (--j > i)
1125 {
1126#ifdef FEAT_MBYTE
1127 if (has_mbyte)
1128 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1129#endif
1130 if (vim_ispathsep(ccline.cmdbuff[j])
1131#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001132 && vim_strchr((char_u *)" *?[{`$%#",
1133 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135 )
1136 {
1137 if (found)
1138 {
1139 i = j + 1;
1140 break;
1141 }
1142 else
1143 found = TRUE;
1144 }
1145 }
1146
1147 if (!found)
1148 j = i;
1149 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1150 j += 4;
1151 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1152 && j == i)
1153 j += 3;
1154 else
1155 j = 0;
1156 if (j > 0)
1157 {
1158 /* TODO this is only for DOS/UNIX systems - need to put in
1159 * machine-specific stuff here and in upseg init */
1160 cmdline_del(j);
1161 put_on_cmdline(upseg + 1, 3, FALSE);
1162 }
1163 else if (ccline.cmdpos > i)
1164 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001165
1166 /* Now complete in the new directory. Set KeyTyped in case the
1167 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001169 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 }
1171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
1173#endif /* FEAT_WILDMENU */
1174
1175 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1176 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1177 if (c == Ctrl_BSL)
1178 {
1179 ++no_mapping;
1180 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001181 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 --no_mapping;
1183 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001184 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1185 * is in a mapping. */
1186 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1187 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 {
1189 vungetc(c);
1190 c = Ctrl_BSL;
1191 }
1192#ifdef FEAT_EVAL
1193 else if (c == 'e')
1194 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001195 char_u *p = NULL;
1196 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
1198 /*
1199 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001200 * Need to save and restore the current command line, to be
1201 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 */
1203 if (ccline.cmdpos == ccline.cmdlen)
1204 new_cmdpos = 99999; /* keep it at the end */
1205 else
1206 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001207
1208 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001210 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 if (c == '=')
1212 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001213 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001214 * to avoid nasty things like going to another buffer when
1215 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001216 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001217 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001219 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001220 restore_cmdline(&save_ccline);
1221
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001222 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001224 len = (int)STRLEN(p);
1225 if (realloc_cmdbuff(len + 1) == OK)
1226 {
1227 ccline.cmdlen = len;
1228 STRCPY(ccline.cmdbuff, p);
1229 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001231 /* Restore the cursor or use the position set with
1232 * set_cmdline_pos(). */
1233 if (new_cmdpos > ccline.cmdlen)
1234 ccline.cmdpos = ccline.cmdlen;
1235 else
1236 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001238 KeyTyped = FALSE; /* Don't do p_wc completion. */
1239 redrawcmd();
1240 goto cmdline_changed;
1241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 }
1243 }
1244 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001245 got_int = FALSE; /* don't abandon the command line */
1246 did_emsg = FALSE;
1247 emsg_on_display = FALSE;
1248 redrawcmd();
1249 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 }
1251#endif
1252 else
1253 {
1254 if (c == Ctrl_G && p_im && restart_edit == 0)
1255 restart_edit = 'a';
1256 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1257 in history */
1258 goto returncmd; /* back to Normal mode */
1259 }
1260 }
1261
1262#ifdef FEAT_CMDWIN
1263 if (c == cedit_key || c == K_CMDWIN)
1264 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001265 if (ex_normal_busy == 0 && got_int == FALSE)
1266 {
1267 /*
1268 * Open a window to edit the command line (and history).
1269 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001270 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001271 some_key_typed = TRUE;
1272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 }
1274# ifdef FEAT_DIGRAPHS
1275 else
1276# endif
1277#endif
1278#ifdef FEAT_DIGRAPHS
1279 c = do_digraph(c);
1280#endif
1281
1282 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1283 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1284 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001285 /* In Ex mode a backslash escapes a newline. */
1286 if (exmode_active
1287 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001288 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001289 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001290 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001292 if (c == K_KENTER)
1293 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001295 else
1296 {
1297 gotesc = FALSE; /* Might have typed ESC previously, don't
1298 truncate the cmdline now. */
1299 if (ccheck_abbr(c + ABBR_OFF))
1300 goto cmdline_changed;
1301 if (!cmd_silent)
1302 {
1303 windgoto(msg_row, 0);
1304 out_flush();
1305 }
1306 break;
1307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309
1310 /*
1311 * Completion for 'wildchar' or 'wildcharm' key.
1312 * - hitting <ESC> twice means: abandon command line.
1313 * - wildcard expansion is only done when the 'wildchar' key is really
1314 * typed, not when it comes from a macro
1315 */
1316 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1317 {
1318 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1319 {
1320 /* if 'wildmode' contains "list" may still need to list */
1321 if (xpc.xp_numfiles > 1
1322 && !did_wild_list
1323 && (wim_flags[wim_index] & WIM_LIST))
1324 {
1325 (void)showmatches(&xpc, FALSE);
1326 redrawcmd();
1327 did_wild_list = TRUE;
1328 }
1329 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001330 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1331 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001333 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1334 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 else
1336 res = OK; /* don't insert 'wildchar' now */
1337 }
1338 else /* typed p_wc first time */
1339 {
1340 wim_index = 0;
1341 j = ccline.cmdpos;
1342 /* if 'wildmode' first contains "longest", get longest
1343 * common part */
1344 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001345 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1346 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001348 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1349 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 /* if interrupted while completing, behave like it failed */
1352 if (got_int)
1353 {
1354 (void)vpeekc(); /* remove <C-C> from input stream */
1355 got_int = FALSE; /* don't abandon the command line */
1356 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1357#ifdef FEAT_WILDMENU
1358 xpc.xp_context = EXPAND_NOTHING;
1359#endif
1360 goto cmdline_changed;
1361 }
1362
1363 /* when more than one match, and 'wildmode' first contains
1364 * "list", or no change and 'wildmode' contains "longest,list",
1365 * list all matches */
1366 if (res == OK && xpc.xp_numfiles > 1)
1367 {
1368 /* a "longest" that didn't do anything is skipped (but not
1369 * "list:longest") */
1370 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1371 wim_index = 1;
1372 if ((wim_flags[wim_index] & WIM_LIST)
1373#ifdef FEAT_WILDMENU
1374 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1375#endif
1376 )
1377 {
1378 if (!(wim_flags[0] & WIM_LONGEST))
1379 {
1380#ifdef FEAT_WILDMENU
1381 int p_wmnu_save = p_wmnu;
1382 p_wmnu = 0;
1383#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001384 /* remove match */
1385 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#ifdef FEAT_WILDMENU
1387 p_wmnu = p_wmnu_save;
1388#endif
1389 }
1390#ifdef FEAT_WILDMENU
1391 (void)showmatches(&xpc, p_wmnu
1392 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1393#else
1394 (void)showmatches(&xpc, FALSE);
1395#endif
1396 redrawcmd();
1397 did_wild_list = TRUE;
1398 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001399 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1400 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001402 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1403 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 }
1405 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001406 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408#ifdef FEAT_WILDMENU
1409 else if (xpc.xp_numfiles == -1)
1410 xpc.xp_context = EXPAND_NOTHING;
1411#endif
1412 }
1413 if (wim_index < 3)
1414 ++wim_index;
1415 if (c == ESC)
1416 gotesc = TRUE;
1417 if (res == OK)
1418 goto cmdline_changed;
1419 }
1420
1421 gotesc = FALSE;
1422
1423 /* <S-Tab> goes to last match, in a clumsy way */
1424 if (c == K_S_TAB && KeyTyped)
1425 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001426 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1427 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1428 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 goto cmdline_changed;
1430 }
1431
1432 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1433 c = NL;
1434
1435 do_abbr = TRUE; /* default: check for abbreviation */
1436
1437 /*
1438 * Big switch for a typed command line character.
1439 */
1440 switch (c)
1441 {
1442 case K_BS:
1443 case Ctrl_H:
1444 case K_DEL:
1445 case K_KDEL:
1446 case Ctrl_W:
1447#ifdef FEAT_FKMAP
1448 if (cmd_fkmap && c == K_BS)
1449 c = K_DEL;
1450#endif
1451 if (c == K_KDEL)
1452 c = K_DEL;
1453
1454 /*
1455 * delete current character is the same as backspace on next
1456 * character, except at end of line
1457 */
1458 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1459 ++ccline.cmdpos;
1460#ifdef FEAT_MBYTE
1461 if (has_mbyte && c == K_DEL)
1462 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1463 ccline.cmdbuff + ccline.cmdpos);
1464#endif
1465 if (ccline.cmdpos > 0)
1466 {
1467 char_u *p;
1468
1469 j = ccline.cmdpos;
1470 p = ccline.cmdbuff + j;
1471#ifdef FEAT_MBYTE
1472 if (has_mbyte)
1473 {
1474 p = mb_prevptr(ccline.cmdbuff, p);
1475 if (c == Ctrl_W)
1476 {
1477 while (p > ccline.cmdbuff && vim_isspace(*p))
1478 p = mb_prevptr(ccline.cmdbuff, p);
1479 i = mb_get_class(p);
1480 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1481 p = mb_prevptr(ccline.cmdbuff, p);
1482 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001483 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 }
1485 }
1486 else
1487#endif
1488 if (c == Ctrl_W)
1489 {
1490 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1491 --p;
1492 i = vim_iswordc(p[-1]);
1493 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1494 && vim_iswordc(p[-1]) == i)
1495 --p;
1496 }
1497 else
1498 --p;
1499 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1500 ccline.cmdlen -= j - ccline.cmdpos;
1501 i = ccline.cmdpos;
1502 while (i < ccline.cmdlen)
1503 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1504
1505 /* Truncate at the end, required for multi-byte chars. */
1506 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001507#ifdef FEAT_SEARCH_EXTRA
1508 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001509 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001510 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001511 /* save view settings, so that the screen
1512 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001513 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001514 }
1515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 redrawcmd();
1517 }
1518 else if (ccline.cmdlen == 0 && c != Ctrl_W
1519 && ccline.cmdprompt == NULL && indent == 0)
1520 {
1521 /* In ex and debug mode it doesn't make sense to return. */
1522 if (exmode_active
1523#ifdef FEAT_EVAL
1524 || ccline.cmdfirstc == '>'
1525#endif
1526 )
1527 goto cmdline_not_changed;
1528
Bram Moolenaard23a8232018-02-10 18:45:26 +01001529 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530 if (!cmd_silent)
1531 {
1532#ifdef FEAT_RIGHTLEFT
1533 if (cmdmsg_rl)
1534 msg_col = Columns;
1535 else
1536#endif
1537 msg_col = 0;
1538 msg_putchar(' '); /* delete ':' */
1539 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001540#ifdef FEAT_SEARCH_EXTRA
1541 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001542 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 redraw_cmdline = TRUE;
1545 goto returncmd; /* back to cmd mode */
1546 }
1547 goto cmdline_changed;
1548
1549 case K_INS:
1550 case K_KINS:
1551#ifdef FEAT_FKMAP
1552 /* if Farsi mode set, we are in reverse insert mode -
1553 Do not change the mode */
1554 if (cmd_fkmap)
1555 beep_flush();
1556 else
1557#endif
1558 ccline.overstrike = !ccline.overstrike;
1559#ifdef CURSOR_SHAPE
1560 ui_cursor_shape(); /* may show different cursor shape */
1561#endif
1562 goto cmdline_not_changed;
1563
1564 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001565 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 {
1567 /* ":lmap" mappings exists, toggle use of mappings. */
1568 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001569#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 im_set_active(FALSE); /* Disable input method */
1571#endif
1572 if (b_im_ptr != NULL)
1573 {
1574 if (State & LANGMAP)
1575 *b_im_ptr = B_IMODE_LMAP;
1576 else
1577 *b_im_ptr = B_IMODE_NONE;
1578 }
1579 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001580#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 else
1582 {
1583 /* There are no ":lmap" mappings, toggle IM. When
1584 * 'imdisable' is set don't try getting the status, it's
1585 * always off. */
1586 if ((p_imdisable && b_im_ptr != NULL)
1587 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1588 {
1589 im_set_active(FALSE); /* Disable input method */
1590 if (b_im_ptr != NULL)
1591 *b_im_ptr = B_IMODE_NONE;
1592 }
1593 else
1594 {
1595 im_set_active(TRUE); /* Enable input method */
1596 if (b_im_ptr != NULL)
1597 *b_im_ptr = B_IMODE_IM;
1598 }
1599 }
1600#endif
1601 if (b_im_ptr != NULL)
1602 {
1603 if (b_im_ptr == &curbuf->b_p_iminsert)
1604 set_iminsert_global();
1605 else
1606 set_imsearch_global();
1607 }
1608#ifdef CURSOR_SHAPE
1609 ui_cursor_shape(); /* may show different cursor shape */
1610#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001611#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 /* Show/unshow value of 'keymap' in status lines later. */
1613 status_redraw_curbuf();
1614#endif
1615 goto cmdline_not_changed;
1616
1617/* case '@': only in very old vi */
1618 case Ctrl_U:
1619 /* delete all characters left of the cursor */
1620 j = ccline.cmdpos;
1621 ccline.cmdlen -= j;
1622 i = ccline.cmdpos = 0;
1623 while (i < ccline.cmdlen)
1624 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1625 /* Truncate at the end, required for multi-byte chars. */
1626 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001627#ifdef FEAT_SEARCH_EXTRA
1628 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001629 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 redrawcmd();
1632 goto cmdline_changed;
1633
1634#ifdef FEAT_CLIPBOARD
1635 case Ctrl_Y:
1636 /* Copy the modeless selection, if there is one. */
1637 if (clip_star.state != SELECT_CLEARED)
1638 {
1639 if (clip_star.state == SELECT_DONE)
1640 clip_copy_modeless_selection(TRUE);
1641 goto cmdline_not_changed;
1642 }
1643 break;
1644#endif
1645
1646 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1647 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001648 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001649 * ":normal" runs out of characters. */
1650 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001651 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 goto cmdline_not_changed;
1653
1654 gotesc = TRUE; /* will free ccline.cmdbuff after
1655 putting it in history */
1656 goto returncmd; /* back to cmd mode */
1657
1658 case Ctrl_R: /* insert register */
1659#ifdef USE_ON_FLY_SCROLL
1660 dont_scroll = TRUE; /* disallow scrolling here */
1661#endif
1662 putcmdline('"', TRUE);
1663 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001664 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (i == Ctrl_O)
1666 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1667 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001668 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001669 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 --no_mapping;
1671#ifdef FEAT_EVAL
1672 /*
1673 * Insert the result of an expression.
1674 * Need to save the current command line, to be able to enter
1675 * a new one...
1676 */
1677 new_cmdpos = -1;
1678 if (c == '=')
1679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1681 {
1682 beep_flush();
1683 c = ESC;
1684 }
1685 else
1686 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001687 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001689 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 }
1691 }
1692#endif
1693 if (c != ESC) /* use ESC to cancel inserting register */
1694 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001695 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001696
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001697#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001698 /* When there was a serious error abort getting the
1699 * command line. */
1700 if (aborting())
1701 {
1702 gotesc = TRUE; /* will free ccline.cmdbuff after
1703 putting it in history */
1704 goto returncmd; /* back to cmd mode */
1705 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 KeyTyped = FALSE; /* Don't do p_wc completion. */
1708#ifdef FEAT_EVAL
1709 if (new_cmdpos >= 0)
1710 {
1711 /* set_cmdline_pos() was used */
1712 if (new_cmdpos > ccline.cmdlen)
1713 ccline.cmdpos = ccline.cmdlen;
1714 else
1715 ccline.cmdpos = new_cmdpos;
1716 }
1717#endif
1718 }
1719 redrawcmd();
1720 goto cmdline_changed;
1721
1722 case Ctrl_D:
1723 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1724 break; /* Use ^D as normal char instead */
1725
1726 redrawcmd();
1727 continue; /* don't do incremental search now */
1728
1729 case K_RIGHT:
1730 case K_S_RIGHT:
1731 case K_C_RIGHT:
1732 do
1733 {
1734 if (ccline.cmdpos >= ccline.cmdlen)
1735 break;
1736 i = cmdline_charsize(ccline.cmdpos);
1737 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1738 break;
1739 ccline.cmdspos += i;
1740#ifdef FEAT_MBYTE
1741 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001742 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 + ccline.cmdpos);
1744 else
1745#endif
1746 ++ccline.cmdpos;
1747 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001748 while ((c == K_S_RIGHT || c == K_C_RIGHT
1749 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1751#ifdef FEAT_MBYTE
1752 if (has_mbyte)
1753 set_cmdspos_cursor();
1754#endif
1755 goto cmdline_not_changed;
1756
1757 case K_LEFT:
1758 case K_S_LEFT:
1759 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001760 if (ccline.cmdpos == 0)
1761 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 do
1763 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 --ccline.cmdpos;
1765#ifdef FEAT_MBYTE
1766 if (has_mbyte) /* move to first byte of char */
1767 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1768 ccline.cmdbuff + ccline.cmdpos);
1769#endif
1770 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1771 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001772 while (ccline.cmdpos > 0
1773 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001774 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1776#ifdef FEAT_MBYTE
1777 if (has_mbyte)
1778 set_cmdspos_cursor();
1779#endif
1780 goto cmdline_not_changed;
1781
1782 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001783 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001784 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar4770d092006-01-12 23:22:24 +00001786#ifdef FEAT_GUI_W32
1787 /* On Win32 ignore <M-F4>, we get it when closing the window was
1788 * cancelled. */
1789 case K_F4:
1790 if (mod_mask == MOD_MASK_ALT)
1791 {
1792 redrawcmd(); /* somehow the cmdline is cleared */
1793 goto cmdline_not_changed;
1794 }
1795 break;
1796#endif
1797
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#ifdef FEAT_MOUSE
1799 case K_MIDDLEDRAG:
1800 case K_MIDDLERELEASE:
1801 goto cmdline_not_changed; /* Ignore mouse */
1802
1803 case K_MIDDLEMOUSE:
1804# ifdef FEAT_GUI
1805 /* When GUI is active, also paste when 'mouse' is empty */
1806 if (!gui.in_use)
1807# endif
1808 if (!mouse_has(MOUSE_COMMAND))
1809 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001810# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001812 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001814# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001815 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 redrawcmd();
1817 goto cmdline_changed;
1818
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001819# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001821 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 redrawcmd();
1823 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001824# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
1826 case K_LEFTDRAG:
1827 case K_LEFTRELEASE:
1828 case K_RIGHTDRAG:
1829 case K_RIGHTRELEASE:
1830 /* Ignore drag and release events when the button-down wasn't
1831 * seen before. */
1832 if (ignore_drag_release)
1833 goto cmdline_not_changed;
1834 /* FALLTHROUGH */
1835 case K_LEFTMOUSE:
1836 case K_RIGHTMOUSE:
1837 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1838 ignore_drag_release = TRUE;
1839 else
1840 ignore_drag_release = FALSE;
1841# ifdef FEAT_GUI
1842 /* When GUI is active, also move when 'mouse' is empty */
1843 if (!gui.in_use)
1844# endif
1845 if (!mouse_has(MOUSE_COMMAND))
1846 goto cmdline_not_changed; /* Ignore mouse */
1847# ifdef FEAT_CLIPBOARD
1848 if (mouse_row < cmdline_row && clip_star.available)
1849 {
1850 int button, is_click, is_drag;
1851
1852 /*
1853 * Handle modeless selection.
1854 */
1855 button = get_mouse_button(KEY2TERMCAP1(c),
1856 &is_click, &is_drag);
1857 if (mouse_model_popup() && button == MOUSE_LEFT
1858 && (mod_mask & MOD_MASK_SHIFT))
1859 {
1860 /* Translate shift-left to right button. */
1861 button = MOUSE_RIGHT;
1862 mod_mask &= ~MOD_MASK_SHIFT;
1863 }
1864 clip_modeless(button, is_click, is_drag);
1865 goto cmdline_not_changed;
1866 }
1867# endif
1868
1869 set_cmdspos();
1870 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1871 ++ccline.cmdpos)
1872 {
1873 i = cmdline_charsize(ccline.cmdpos);
1874 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1875 && mouse_col < ccline.cmdspos % Columns + i)
1876 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001877# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (has_mbyte)
1879 {
1880 /* Count ">" for double-wide char that doesn't fit. */
1881 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001882 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 + ccline.cmdpos) - 1;
1884 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001885# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 ccline.cmdspos += i;
1887 }
1888 goto cmdline_not_changed;
1889
1890 /* Mouse scroll wheel: ignored here */
1891 case K_MOUSEDOWN:
1892 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001893 case K_MOUSELEFT:
1894 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 /* Alternate buttons ignored here */
1896 case K_X1MOUSE:
1897 case K_X1DRAG:
1898 case K_X1RELEASE:
1899 case K_X2MOUSE:
1900 case K_X2DRAG:
1901 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001902 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 goto cmdline_not_changed;
1904
1905#endif /* FEAT_MOUSE */
1906
1907#ifdef FEAT_GUI
1908 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1909 case K_LEFTRELEASE_NM:
1910 goto cmdline_not_changed;
1911
1912 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001913 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 {
1915 gui_do_scroll();
1916 redrawcmd();
1917 }
1918 goto cmdline_not_changed;
1919
1920 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001921 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001923 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 redrawcmd();
1925 }
1926 goto cmdline_not_changed;
1927#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001928#ifdef FEAT_GUI_TABLINE
1929 case K_TABLINE:
1930 case K_TABMENU:
1931 /* Don't want to change any tabs here. Make sure the same tab
1932 * is still selected. */
1933 if (gui_use_tabline())
1934 gui_mch_set_curtab(tabpage_index(curtab));
1935 goto cmdline_not_changed;
1936#endif
1937
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 case K_SELECT: /* end of Select mode mapping - ignore */
1939 goto cmdline_not_changed;
1940
1941 case Ctrl_B: /* begin of command line */
1942 case K_HOME:
1943 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 case K_S_HOME:
1945 case K_C_HOME:
1946 ccline.cmdpos = 0;
1947 set_cmdspos();
1948 goto cmdline_not_changed;
1949
1950 case Ctrl_E: /* end of command line */
1951 case K_END:
1952 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 case K_S_END:
1954 case K_C_END:
1955 ccline.cmdpos = ccline.cmdlen;
1956 set_cmdspos_cursor();
1957 goto cmdline_not_changed;
1958
1959 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001960 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 break;
1962 goto cmdline_changed;
1963
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001964 case Ctrl_L:
1965#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001966 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001967 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001968#endif
1969
1970 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001971 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 break;
1973 goto cmdline_changed;
1974
1975 case Ctrl_N: /* next match */
1976 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001977 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001979 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1980 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001982 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001985 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 case K_UP:
1987 case K_DOWN:
1988 case K_S_UP:
1989 case K_S_DOWN:
1990 case K_PAGEUP:
1991 case K_KPAGEUP:
1992 case K_PAGEDOWN:
1993 case K_KPAGEDOWN:
1994 if (hislen == 0 || firstc == NUL) /* no history */
1995 goto cmdline_not_changed;
1996
1997 i = hiscnt;
1998
1999 /* save current command string so it can be restored later */
2000 if (lookfor == NULL)
2001 {
2002 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2003 goto cmdline_not_changed;
2004 lookfor[ccline.cmdpos] = NUL;
2005 }
2006
2007 j = (int)STRLEN(lookfor);
2008 for (;;)
2009 {
2010 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002011 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002012 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 {
2014 if (hiscnt == hislen) /* first time */
2015 hiscnt = hisidx[histype];
2016 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2017 hiscnt = hislen - 1;
2018 else if (hiscnt != hisidx[histype] + 1)
2019 --hiscnt;
2020 else /* at top of list */
2021 {
2022 hiscnt = i;
2023 break;
2024 }
2025 }
2026 else /* one step forwards */
2027 {
2028 /* on last entry, clear the line */
2029 if (hiscnt == hisidx[histype])
2030 {
2031 hiscnt = hislen;
2032 break;
2033 }
2034
2035 /* not on a history line, nothing to do */
2036 if (hiscnt == hislen)
2037 break;
2038 if (hiscnt == hislen - 1) /* wrap around */
2039 hiscnt = 0;
2040 else
2041 ++hiscnt;
2042 }
2043 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2044 {
2045 hiscnt = i;
2046 break;
2047 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002048 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002049 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 || STRNCMP(history[histype][hiscnt].hisstr,
2051 lookfor, (size_t)j) == 0)
2052 break;
2053 }
2054
2055 if (hiscnt != i) /* jumped to other entry */
2056 {
2057 char_u *p;
2058 int len;
2059 int old_firstc;
2060
2061 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002062 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 if (hiscnt == hislen)
2064 p = lookfor; /* back to the old one */
2065 else
2066 p = history[histype][hiscnt].hisstr;
2067
2068 if (histype == HIST_SEARCH
2069 && p != lookfor
2070 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2071 {
2072 /* Correct for the separator character used when
2073 * adding the history entry vs the one used now.
2074 * First loop: count length.
2075 * Second loop: copy the characters. */
2076 for (i = 0; i <= 1; ++i)
2077 {
2078 len = 0;
2079 for (j = 0; p[j] != NUL; ++j)
2080 {
2081 /* Replace old sep with new sep, unless it is
2082 * escaped. */
2083 if (p[j] == old_firstc
2084 && (j == 0 || p[j - 1] != '\\'))
2085 {
2086 if (i > 0)
2087 ccline.cmdbuff[len] = firstc;
2088 }
2089 else
2090 {
2091 /* Escape new sep, unless it is already
2092 * escaped. */
2093 if (p[j] == firstc
2094 && (j == 0 || p[j - 1] != '\\'))
2095 {
2096 if (i > 0)
2097 ccline.cmdbuff[len] = '\\';
2098 ++len;
2099 }
2100 if (i > 0)
2101 ccline.cmdbuff[len] = p[j];
2102 }
2103 ++len;
2104 }
2105 if (i == 0)
2106 {
2107 alloc_cmdbuff(len);
2108 if (ccline.cmdbuff == NULL)
2109 goto returncmd;
2110 }
2111 }
2112 ccline.cmdbuff[len] = NUL;
2113 }
2114 else
2115 {
2116 alloc_cmdbuff((int)STRLEN(p));
2117 if (ccline.cmdbuff == NULL)
2118 goto returncmd;
2119 STRCPY(ccline.cmdbuff, p);
2120 }
2121
2122 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2123 redrawcmd();
2124 goto cmdline_changed;
2125 }
2126 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002127#endif
2128 goto cmdline_not_changed;
2129
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002130#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002131 case Ctrl_G: /* next match */
2132 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002133 if (may_adjust_incsearch_highlighting(
2134 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002135 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137#endif
2138
2139 case Ctrl_V:
2140 case Ctrl_Q:
2141#ifdef FEAT_MOUSE
2142 ignore_drag_release = TRUE;
2143#endif
2144 putcmdline('^', TRUE);
2145 c = get_literal(); /* get next (two) character(s) */
2146 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002147 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148#ifdef FEAT_MBYTE
2149 /* may need to remove ^ when composing char was typed */
2150 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2151 {
2152 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2153 msg_putchar(' ');
2154 cursorcmd();
2155 }
2156#endif
2157 break;
2158
2159#ifdef FEAT_DIGRAPHS
2160 case Ctrl_K:
2161#ifdef FEAT_MOUSE
2162 ignore_drag_release = TRUE;
2163#endif
2164 putcmdline('?', TRUE);
2165#ifdef USE_ON_FLY_SCROLL
2166 dont_scroll = TRUE; /* disallow scrolling here */
2167#endif
2168 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002169 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 if (c != NUL)
2171 break;
2172
2173 redrawcmd();
2174 goto cmdline_not_changed;
2175#endif /* FEAT_DIGRAPHS */
2176
2177#ifdef FEAT_RIGHTLEFT
2178 case Ctrl__: /* CTRL-_: switch language mode */
2179 if (!p_ari)
2180 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002181# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 if (p_altkeymap)
2183 {
2184 cmd_fkmap = !cmd_fkmap;
2185 if (cmd_fkmap) /* in Farsi always in Insert mode */
2186 ccline.overstrike = FALSE;
2187 }
2188 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002189# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 cmd_hkmap = !cmd_hkmap;
2191 goto cmdline_not_changed;
2192#endif
2193
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002194 case K_PS:
2195 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2196 goto cmdline_changed;
2197
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 default:
2199#ifdef UNIX
2200 if (c == intr_char)
2201 {
2202 gotesc = TRUE; /* will free ccline.cmdbuff after
2203 putting it in history */
2204 goto returncmd; /* back to Normal mode */
2205 }
2206#endif
2207 /*
2208 * Normal character with no special meaning. Just set mod_mask
2209 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2210 * the string <S-Space>. This should only happen after ^V.
2211 */
2212 if (!IS_SPECIAL(c))
2213 mod_mask = 0x0;
2214 break;
2215 }
2216 /*
2217 * End of switch on command line character.
2218 * We come here if we have a normal character.
2219 */
2220
Bram Moolenaarede3e632013-06-23 16:16:19 +02002221 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222#ifdef FEAT_MBYTE
2223 /* Add ABBR_OFF for characters above 0x100, this is
2224 * what check_abbr() expects. */
2225 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2226#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002227 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 goto cmdline_changed;
2229
2230 /*
2231 * put the character in the command line
2232 */
2233 if (IS_SPECIAL(c) || mod_mask != 0)
2234 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2235 else
2236 {
2237#ifdef FEAT_MBYTE
2238 if (has_mbyte)
2239 {
2240 j = (*mb_char2bytes)(c, IObuff);
2241 IObuff[j] = NUL; /* exclude composing chars */
2242 put_on_cmdline(IObuff, j, TRUE);
2243 }
2244 else
2245#endif
2246 {
2247 IObuff[0] = c;
2248 put_on_cmdline(IObuff, 1, TRUE);
2249 }
2250 }
2251 goto cmdline_changed;
2252
2253/*
2254 * This part implements incremental searches for "/" and "?"
2255 * Jump to cmdline_not_changed when a character has been read but the command
2256 * line did not change. Then we only search and redraw if something changed in
2257 * the past.
2258 * Jump to cmdline_changed when the command line did change.
2259 * (Sorry for the goto's, I know it is ugly).
2260 */
2261cmdline_not_changed:
2262#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002263 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 continue;
2265#endif
2266
2267cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002268 /* Trigger CmdlineChanged autocommands. */
2269 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002272 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273#endif
2274
2275#ifdef FEAT_RIGHTLEFT
2276 if (cmdmsg_rl
2277# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002278 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279# endif
2280 )
2281 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002282 * right-left typing. Not efficient, but it works.
2283 * Do it only when there are no characters left to read
2284 * to avoid useless intermediate redraws. */
2285 if (vpeekc() == NUL)
2286 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287#endif
2288 }
2289
2290returncmd:
2291
2292#ifdef FEAT_RIGHTLEFT
2293 cmdmsg_rl = FALSE;
2294#endif
2295
2296#ifdef FEAT_FKMAP
2297 cmd_fkmap = 0;
2298#endif
2299
2300 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002301 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302
2303#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002304 finish_incsearch_highlighting(gotesc, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
2306
2307 if (ccline.cmdbuff != NULL)
2308 {
2309 /*
2310 * Put line in history buffer (":" and "=" only when it was typed).
2311 */
2312#ifdef FEAT_CMDHIST
2313 if (ccline.cmdlen && firstc != NUL
2314 && (some_key_typed || histype == HIST_SEARCH))
2315 {
2316 add_to_history(histype, ccline.cmdbuff, TRUE,
2317 histype == HIST_SEARCH ? firstc : NUL);
2318 if (firstc == ':')
2319 {
2320 vim_free(new_last_cmdline);
2321 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2322 }
2323 }
2324#endif
2325
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002326 if (gotesc)
2327 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 }
2329
2330 /*
2331 * If the screen was shifted up, redraw the whole screen (later).
2332 * If the line is too long, clear it, so ruler and shown command do
2333 * not get printed in the middle of it.
2334 */
2335 msg_check();
2336 msg_scroll = save_msg_scroll;
2337 redir_off = FALSE;
2338
2339 /* When the command line was typed, no need for a wait-return prompt. */
2340 if (some_key_typed)
2341 need_wait_return = FALSE;
2342
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002343 /* Trigger CmdlineLeave autocommands. */
2344 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002347#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2349 im_save_status(b_im_ptr);
2350 im_set_active(FALSE);
2351#endif
2352#ifdef FEAT_MOUSE
2353 setmouse();
2354#endif
2355#ifdef CURSOR_SHAPE
2356 ui_cursor_shape(); /* may show different cursor shape */
2357#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002358 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002360 {
2361 char_u *p = ccline.cmdbuff;
2362
2363 /* Make ccline empty, getcmdline() may try to use it. */
2364 ccline.cmdbuff = NULL;
2365 return p;
2366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367}
2368
2369#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2370/*
2371 * Get a command line with a prompt.
2372 * This is prepared to be called recursively from getcmdline() (e.g. by
2373 * f_input() when evaluating an expression from CTRL-R =).
2374 * Returns the command line in allocated memory, or NULL.
2375 */
2376 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002377getcmdline_prompt(
2378 int firstc,
2379 char_u *prompt, /* command line prompt */
2380 int attr, /* attributes for prompt */
2381 int xp_context, /* type of expansion */
2382 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383{
2384 char_u *s;
2385 struct cmdline_info save_ccline;
2386 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002387 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002389 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 ccline.cmdprompt = prompt;
2391 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002392# ifdef FEAT_EVAL
2393 ccline.xp_context = xp_context;
2394 ccline.xp_arg = xp_arg;
2395 ccline.input_fn = (firstc == '@');
2396# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002397 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002399 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002400 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002401 /* Restore msg_col, the prompt from input() may have changed it.
2402 * But only if called recursively and the commandline is therefore being
2403 * restored to an old one; if not, the input() prompt stays on the screen,
2404 * so we need its modified msg_col left intact. */
2405 if (ccline.cmdbuff != NULL)
2406 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407
2408 return s;
2409}
2410#endif
2411
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002412/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002413 * Return TRUE when the text must not be changed and we can't switch to
2414 * another window or buffer. Used when editing the command line, evaluating
2415 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002416 */
2417 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002418text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002419{
2420#ifdef FEAT_CMDWIN
2421 if (cmdwin_type != 0)
2422 return TRUE;
2423#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002424 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002425}
2426
2427/*
2428 * Give an error message for a command that isn't allowed while the cmdline
2429 * window is open or editing the cmdline in another way.
2430 */
2431 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002432text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002433{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002434 EMSG(_(get_text_locked_msg()));
2435}
2436
2437 char_u *
2438get_text_locked_msg(void)
2439{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002440#ifdef FEAT_CMDWIN
2441 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002442 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002443#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002444 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002445}
2446
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002447/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002448 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2449 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002450 */
2451 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002452curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002453{
2454 if (curbuf_lock > 0)
2455 {
2456 EMSG(_("E788: Not allowed to edit another buffer now"));
2457 return TRUE;
2458 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002459 return allbuf_locked();
2460}
2461
2462/*
2463 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2464 * message.
2465 */
2466 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002467allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002468{
2469 if (allbuf_lock > 0)
2470 {
2471 EMSG(_("E811: Not allowed to change buffer information now"));
2472 return TRUE;
2473 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474 return FALSE;
2475}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002478cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479{
2480#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2481 if (cmdline_star > 0) /* showing '*', always 1 position */
2482 return 1;
2483#endif
2484 return ptr2cells(ccline.cmdbuff + idx);
2485}
2486
2487/*
2488 * Compute the offset of the cursor on the command line for the prompt and
2489 * indent.
2490 */
2491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002492set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002494 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 ccline.cmdspos = 1 + ccline.cmdindent;
2496 else
2497 ccline.cmdspos = 0 + ccline.cmdindent;
2498}
2499
2500/*
2501 * Compute the screen position for the cursor on the command line.
2502 */
2503 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002504set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
2506 int i, m, c;
2507
2508 set_cmdspos();
2509 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002510 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002512 if (m < 0) /* overflow, Columns or Rows at weird value */
2513 m = MAXCOL;
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 else
2516 m = MAXCOL;
2517 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2518 {
2519 c = cmdline_charsize(i);
2520#ifdef FEAT_MBYTE
2521 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2522 if (has_mbyte)
2523 correct_cmdspos(i, c);
2524#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002525 /* If the cmdline doesn't fit, show cursor on last visible char.
2526 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if ((ccline.cmdspos += c) >= m)
2528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 ccline.cmdspos -= c;
2530 break;
2531 }
2532#ifdef FEAT_MBYTE
2533 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002534 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535#endif
2536 }
2537}
2538
2539#ifdef FEAT_MBYTE
2540/*
2541 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2542 * character that doesn't fit, so that a ">" must be displayed.
2543 */
2544 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002547 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2549 && ccline.cmdspos % Columns + cells > Columns)
2550 ccline.cmdspos++;
2551}
2552#endif
2553
2554/*
2555 * Get an Ex command line for the ":" command.
2556 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002558getexline(
2559 int c, /* normally ':', NUL for ":append" */
2560 void *cookie UNUSED,
2561 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 /* When executing a register, remove ':' that's in front of each line. */
2564 if (exec_from_reg && vpeekc() == ':')
2565 (void)vgetc();
2566 return getcmdline(c, 1L, indent);
2567}
2568
2569/*
2570 * Get an Ex command line for Ex mode.
2571 * In Ex mode we only use the OS supplied line editing features and no
2572 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002576getexmodeline(
2577 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002578 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002579 void *cookie UNUSED,
2580 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002582 garray_T line_ga;
2583 char_u *pend;
2584 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002585 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002586 int escaped = FALSE; /* CTRL-V typed */
2587 int vcol = 0;
2588 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002589 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002590 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 /* Switch cursor on now. This avoids that it happens after the "\n", which
2593 * confuses the system function that computes tabstops. */
2594 cursor_on();
2595
2596 /* always start in column 0; write a newline if necessary */
2597 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002600 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002602 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002603 if (p_prompt)
2604 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 while (indent-- > 0)
2606 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 }
2609
2610 ga_init2(&line_ga, 1, 30);
2611
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002612 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002613 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002614 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002615 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002616 while (indent >= 8)
2617 {
2618 ga_append(&line_ga, TAB);
2619 msg_puts((char_u *)" ");
2620 indent -= 8;
2621 }
2622 while (indent-- > 0)
2623 {
2624 ga_append(&line_ga, ' ');
2625 msg_putchar(' ');
2626 }
2627 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002628 ++no_mapping;
2629 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 /*
2632 * Get the line, one character at a time.
2633 */
2634 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002637 long sw;
2638 char_u *s;
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (ga_grow(&line_ga, 40) == FAIL)
2641 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642
Bram Moolenaarba748c82017-02-26 14:00:07 +01002643 /*
2644 * Get one character at a time.
2645 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002646 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002647
2648 /* Check for a ":normal" command and no more characters left. */
2649 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2650 c1 = '\n';
2651 else
2652 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002655 * Handle line editing.
2656 * Previously this was left to the system, putting the terminal in
2657 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002659 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002661 msg_putchar('\n');
2662 break;
2663 }
2664
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002665 if (c1 == K_PS)
2666 {
2667 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2668 goto redraw;
2669 }
2670
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002671 if (!escaped)
2672 {
2673 /* CR typed means "enter", which is NL */
2674 if (c1 == '\r')
2675 c1 = '\n';
2676
2677 if (c1 == BS || c1 == K_BS
2678 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002680 if (line_ga.ga_len > 0)
2681 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002682#ifdef FEAT_MBYTE
2683 if (has_mbyte)
2684 {
2685 p = (char_u *)line_ga.ga_data;
2686 p[line_ga.ga_len] = NUL;
2687 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2688 line_ga.ga_len -= len;
2689 }
2690 else
2691#endif
2692 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693 goto redraw;
2694 }
2695 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 msg_col = startcol;
2701 msg_clr_eos();
2702 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002703 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 }
2705
2706 if (c1 == Ctrl_T)
2707 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002708 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002709 p = (char_u *)line_ga.ga_data;
2710 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002711 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002712 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002714 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002716 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002717 p = (char_u *)line_ga.ga_data;
2718 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002719 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2720 *s = ' ';
2721 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002723redraw:
2724 /* redraw the line */
2725 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002727 p = (char_u *)line_ga.ga_data;
2728 p[line_ga.ga_len] = NUL;
2729 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 msg_putchar(' ');
2736 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002737 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002739 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002741 len = MB_PTR2LEN(p);
2742 msg_outtrans_len(p, len);
2743 vcol += ptr2cells(p);
2744 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 }
2746 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002748 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 continue;
2750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 if (c1 == Ctrl_D)
2753 {
2754 /* Delete one shiftwidth. */
2755 p = (char_u *)line_ga.ga_data;
2756 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 if (prev_char == '^')
2759 ex_keep_indent = TRUE;
2760 indent = 0;
2761 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 else
2764 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002766 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002767 if (indent > 0)
2768 {
2769 --indent;
2770 indent -= indent % get_sw_value(curbuf);
2771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002773 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002775 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2777 --line_ga.ga_len;
2778 }
2779 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781
2782 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2783 {
2784 escaped = TRUE;
2785 continue;
2786 }
2787
2788 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2789 if (IS_SPECIAL(c1))
2790 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792
2793 if (IS_SPECIAL(c1))
2794 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002795#ifdef FEAT_MBYTE
2796 if (has_mbyte)
2797 len = (*mb_char2bytes)(c1,
2798 (char_u *)line_ga.ga_data + line_ga.ga_len);
2799 else
2800#endif
2801 {
2802 len = 1;
2803 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2804 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 if (c1 == '\n')
2806 msg_putchar('\n');
2807 else if (c1 == TAB)
2808 {
2809 /* Don't use chartabsize(), 'ts' can be different */
2810 do
2811 {
2812 msg_putchar(' ');
2813 } while (++vcol % 8);
2814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002818 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002821 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 escaped = FALSE;
2823
2824 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002825 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002827 /* We are done when a NL is entered, but not when it comes after an
2828 * odd number of backslashes, that results in a NUL. */
2829 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002831 int bcount = 0;
2832
2833 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2834 ++bcount;
2835
2836 if (bcount > 0)
2837 {
2838 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2839 * "\NL", etc. */
2840 line_ga.ga_len -= (bcount + 1) / 2;
2841 pend -= (bcount + 1) / 2;
2842 pend[-1] = '\n';
2843 }
2844
2845 if ((bcount & 1) == 0)
2846 {
2847 --line_ga.ga_len;
2848 --pend;
2849 *pend = NUL;
2850 break;
2851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 }
2853 }
2854
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 --no_mapping;
2856 --allow_keys;
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 /* make following messages go to the next line */
2859 msg_didout = FALSE;
2860 msg_col = 0;
2861 if (msg_row < Rows - 1)
2862 ++msg_row;
2863 emsg_on_display = FALSE; /* don't want ui_delay() */
2864
2865 if (got_int)
2866 ga_clear(&line_ga);
2867
2868 return (char_u *)line_ga.ga_data;
2869}
2870
Bram Moolenaare344bea2005-09-01 20:46:49 +00002871# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2872 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873/*
2874 * Return TRUE if ccline.overstrike is on.
2875 */
2876 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002877cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 return ccline.overstrike;
2880}
2881
2882/*
2883 * Return TRUE if the cursor is at the end of the cmdline.
2884 */
2885 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002886cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
2888 return (ccline.cmdpos >= ccline.cmdlen);
2889}
2890#endif
2891
Bram Moolenaar9372a112005-12-06 19:59:18 +00002892#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893/*
2894 * Return the virtual column number at the current cursor position.
2895 * This is used by the IM code to obtain the start of the preedit string.
2896 */
2897 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002898cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2901 return MAXCOL;
2902
2903# ifdef FEAT_MBYTE
2904 if (has_mbyte)
2905 {
2906 colnr_T col;
2907 int i = 0;
2908
2909 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002910 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911
2912 return col;
2913 }
2914 else
2915# endif
2916 return ccline.cmdpos;
2917}
2918#endif
2919
2920#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2921/*
2922 * If part of the command line is an IM preedit string, redraw it with
2923 * IM feedback attributes. The cursor position is restored after drawing.
2924 */
2925 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002926redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
2928 if ((State & CMDLINE)
2929 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002930 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 && !p_imdisable
2932 && im_is_preediting())
2933 {
2934 int cmdpos = 0;
2935 int cmdspos;
2936 int old_row;
2937 int old_col;
2938 colnr_T col;
2939
2940 old_row = msg_row;
2941 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002942 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943
2944# ifdef FEAT_MBYTE
2945 if (has_mbyte)
2946 {
2947 for (col = 0; col < preedit_start_col
2948 && cmdpos < ccline.cmdlen; ++col)
2949 {
2950 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002951 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 }
2954 else
2955# endif
2956 {
2957 cmdspos += preedit_start_col;
2958 cmdpos += preedit_start_col;
2959 }
2960
2961 msg_row = cmdline_row + (cmdspos / (int)Columns);
2962 msg_col = cmdspos % (int)Columns;
2963 if (msg_row >= Rows)
2964 msg_row = Rows - 1;
2965
2966 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2967 {
2968 int char_len;
2969 int char_attr;
2970
2971 char_attr = im_get_feedback_attr(col);
2972 if (char_attr < 0)
2973 break; /* end of preedit string */
2974
2975# ifdef FEAT_MBYTE
2976 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002977 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 else
2979# endif
2980 char_len = 1;
2981
2982 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2983 cmdpos += char_len;
2984 }
2985
2986 msg_row = old_row;
2987 msg_col = old_col;
2988 }
2989}
2990#endif /* FEAT_XIM && FEAT_GUI_GTK */
2991
2992/*
2993 * Allocate a new command line buffer.
2994 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2995 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2996 */
2997 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002998alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 /*
3001 * give some extra space to avoid having to allocate all the time
3002 */
3003 if (len < 80)
3004 len = 100;
3005 else
3006 len += 20;
3007
3008 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3009 ccline.cmdbufflen = len;
3010}
3011
3012/*
3013 * Re-allocate the command line to length len + something extra.
3014 * return FAIL for failure, OK otherwise
3015 */
3016 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003017realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 char_u *p;
3020
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003021 if (len < ccline.cmdbufflen)
3022 return OK; /* no need to resize */
3023
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 p = ccline.cmdbuff;
3025 alloc_cmdbuff(len); /* will get some more */
3026 if (ccline.cmdbuff == NULL) /* out of memory */
3027 {
3028 ccline.cmdbuff = p; /* keep the old one */
3029 return FAIL;
3030 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003031 /* There isn't always a NUL after the command, but it may need to be
3032 * there, thus copy up to the NUL and add a NUL. */
3033 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3034 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003036
3037 if (ccline.xpc != NULL
3038 && ccline.xpc->xp_pattern != NULL
3039 && ccline.xpc->xp_context != EXPAND_NOTHING
3040 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3041 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003042 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003043
3044 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3045 * to point into the newly allocated memory. */
3046 if (i >= 0 && i <= ccline.cmdlen)
3047 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3048 }
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 return OK;
3051}
3052
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003053#if defined(FEAT_ARABIC) || defined(PROTO)
3054static char_u *arshape_buf = NULL;
3055
3056# if defined(EXITFREE) || defined(PROTO)
3057 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003058free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003059{
3060 vim_free(arshape_buf);
3061}
3062# endif
3063#endif
3064
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065/*
3066 * Draw part of the cmdline at the current cursor position. But draw stars
3067 * when cmdline_star is TRUE.
3068 */
3069 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003070draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
3072#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3073 int i;
3074
3075 if (cmdline_star > 0)
3076 for (i = 0; i < len; ++i)
3077 {
3078 msg_putchar('*');
3079# ifdef FEAT_MBYTE
3080 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003081 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082# endif
3083 }
3084 else
3085#endif
3086#ifdef FEAT_ARABIC
3087 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 static int buflen = 0;
3090 char_u *p;
3091 int j;
3092 int newlen = 0;
3093 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003094 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 int prev_c = 0;
3096 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003097 int u8c;
3098 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100
3101 /*
3102 * Do arabic shaping into a temporary buffer. This is very
3103 * inefficient!
3104 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003105 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 {
3107 /* Re-allocate the buffer. We keep it around to avoid a lot of
3108 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003109 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003110 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003111 arshape_buf = alloc(buflen);
3112 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 return; /* out of memory */
3114 }
3115
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003116 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3117 {
3118 /* Prepend a space to draw the leading composing char on. */
3119 arshape_buf[0] = ' ';
3120 newlen = 1;
3121 }
3122
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 for (j = start; j < start + len; j += mb_l)
3124 {
3125 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003126 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003127 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (ARABIC_CHAR(u8c))
3129 {
3130 /* Do Arabic shaping. */
3131 if (cmdmsg_rl)
3132 {
3133 /* displaying from right to left */
3134 pc = prev_c;
3135 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003136 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 if (j + mb_l >= start + len)
3138 nc = NUL;
3139 else
3140 nc = utf_ptr2char(p + mb_l);
3141 }
3142 else
3143 {
3144 /* displaying from left to right */
3145 if (j + mb_l >= start + len)
3146 pc = NUL;
3147 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003148 {
3149 int pcc[MAX_MCO];
3150
3151 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003153 pc1 = pcc[0];
3154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 nc = prev_c;
3156 }
3157 prev_c = u8c;
3158
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003159 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003161 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003162 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003164 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3165 if (u8cc[1] != 0)
3166 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003167 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 }
3169 }
3170 else
3171 {
3172 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003173 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 newlen += mb_l;
3175 }
3176 }
3177
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003178 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 }
3180 else
3181#endif
3182 msg_outtrans_len(ccline.cmdbuff + start, len);
3183}
3184
3185/*
3186 * Put a character on the command line. Shifts the following text to the
3187 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3188 * "c" must be printable (fit in one display cell)!
3189 */
3190 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003191putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192{
3193 if (cmd_silent)
3194 return;
3195 msg_no_more = TRUE;
3196 msg_putchar(c);
3197 if (shift)
3198 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3199 msg_no_more = FALSE;
3200 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003201 extra_char = c;
3202 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203}
3204
3205/*
3206 * Undo a putcmdline(c, FALSE).
3207 */
3208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003209unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 if (cmd_silent)
3212 return;
3213 msg_no_more = TRUE;
3214 if (ccline.cmdlen == ccline.cmdpos)
3215 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003216#ifdef FEAT_MBYTE
3217 else if (has_mbyte)
3218 draw_cmdline(ccline.cmdpos,
3219 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 else
3222 draw_cmdline(ccline.cmdpos, 1);
3223 msg_no_more = FALSE;
3224 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003225 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226}
3227
3228/*
3229 * Put the given string, of the given length, onto the command line.
3230 * If len is -1, then STRLEN() is used to calculate the length.
3231 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3232 * part will be redrawn, otherwise it will not. If this function is called
3233 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3234 * called afterwards.
3235 */
3236 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003237put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 int retval;
3240 int i;
3241 int m;
3242 int c;
3243
3244 if (len < 0)
3245 len = (int)STRLEN(str);
3246
3247 /* Check if ccline.cmdbuff needs to be longer */
3248 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003249 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 else
3251 retval = OK;
3252 if (retval == OK)
3253 {
3254 if (!ccline.overstrike)
3255 {
3256 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3257 ccline.cmdbuff + ccline.cmdpos,
3258 (size_t)(ccline.cmdlen - ccline.cmdpos));
3259 ccline.cmdlen += len;
3260 }
3261 else
3262 {
3263#ifdef FEAT_MBYTE
3264 if (has_mbyte)
3265 {
3266 /* Count nr of characters in the new string. */
3267 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003268 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 ++m;
3270 /* Count nr of bytes in cmdline that are overwritten by these
3271 * characters. */
3272 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003273 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 --m;
3275 if (i < ccline.cmdlen)
3276 {
3277 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3278 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3279 ccline.cmdlen += ccline.cmdpos + len - i;
3280 }
3281 else
3282 ccline.cmdlen = ccline.cmdpos + len;
3283 }
3284 else
3285#endif
3286 if (ccline.cmdpos + len > ccline.cmdlen)
3287 ccline.cmdlen = ccline.cmdpos + len;
3288 }
3289 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3290 ccline.cmdbuff[ccline.cmdlen] = NUL;
3291
3292#ifdef FEAT_MBYTE
3293 if (enc_utf8)
3294 {
3295 /* When the inserted text starts with a composing character,
3296 * backup to the character before it. There could be two of them.
3297 */
3298 i = 0;
3299 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3300 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3301 {
3302 i = (*mb_head_off)(ccline.cmdbuff,
3303 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3304 ccline.cmdpos -= i;
3305 len += i;
3306 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3307 }
3308# ifdef FEAT_ARABIC
3309 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3310 {
3311 /* Check the previous character for Arabic combining pair. */
3312 i = (*mb_head_off)(ccline.cmdbuff,
3313 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3314 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3315 + ccline.cmdpos - i), c))
3316 {
3317 ccline.cmdpos -= i;
3318 len += i;
3319 }
3320 else
3321 i = 0;
3322 }
3323# endif
3324 if (i != 0)
3325 {
3326 /* Also backup the cursor position. */
3327 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3328 ccline.cmdspos -= i;
3329 msg_col -= i;
3330 if (msg_col < 0)
3331 {
3332 msg_col += Columns;
3333 --msg_row;
3334 }
3335 }
3336 }
3337#endif
3338
3339 if (redraw && !cmd_silent)
3340 {
3341 msg_no_more = TRUE;
3342 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003343 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3345 /* Avoid clearing the rest of the line too often. */
3346 if (cmdline_row != i || ccline.overstrike)
3347 msg_clr_eos();
3348 msg_no_more = FALSE;
3349 }
3350#ifdef FEAT_FKMAP
3351 /*
3352 * If we are in Farsi command mode, the character input must be in
3353 * Insert mode. So do not advance the cmdpos.
3354 */
3355 if (!cmd_fkmap)
3356#endif
3357 {
3358 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003361 if (m < 0) /* overflow, Columns or Rows at weird value */
3362 m = MAXCOL;
3363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 else
3365 m = MAXCOL;
3366 for (i = 0; i < len; ++i)
3367 {
3368 c = cmdline_charsize(ccline.cmdpos);
3369#ifdef FEAT_MBYTE
3370 /* count ">" for a double-wide char that doesn't fit. */
3371 if (has_mbyte)
3372 correct_cmdspos(ccline.cmdpos, c);
3373#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003374 /* Stop cursor at the end of the screen, but do increment the
3375 * insert position, so that entering a very long command
3376 * works, even though you can't see it. */
3377 if (ccline.cmdspos + c < m)
3378 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379#ifdef FEAT_MBYTE
3380 if (has_mbyte)
3381 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003382 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (c > len - i - 1)
3384 c = len - i - 1;
3385 ccline.cmdpos += c;
3386 i += c;
3387 }
3388#endif
3389 ++ccline.cmdpos;
3390 }
3391 }
3392 }
3393 if (redraw)
3394 msg_check();
3395 return retval;
3396}
3397
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003398static struct cmdline_info prev_ccline;
3399static int prev_ccline_used = FALSE;
3400
3401/*
3402 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3403 * and overwrite it. But get_cmdline_str() may need it, thus make it
3404 * available globally in prev_ccline.
3405 */
3406 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003407save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003408{
3409 if (!prev_ccline_used)
3410 {
3411 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3412 prev_ccline_used = TRUE;
3413 }
3414 *ccp = prev_ccline;
3415 prev_ccline = ccline;
3416 ccline.cmdbuff = NULL;
3417 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003418 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003419}
3420
3421/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003422 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003423 */
3424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003425restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003426{
3427 ccline = prev_ccline;
3428 prev_ccline = *ccp;
3429}
3430
Bram Moolenaar5a305422006-04-28 22:38:25 +00003431#if defined(FEAT_EVAL) || defined(PROTO)
3432/*
3433 * Save the command line into allocated memory. Returns a pointer to be
3434 * passed to restore_cmdline_alloc() later.
3435 * Returns NULL when failed.
3436 */
3437 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003438save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003439{
3440 struct cmdline_info *p;
3441
3442 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3443 if (p != NULL)
3444 save_cmdline(p);
3445 return (char_u *)p;
3446}
3447
3448/*
3449 * Restore the command line from the return value of save_cmdline_alloc().
3450 */
3451 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003453{
3454 if (p != NULL)
3455 {
3456 restore_cmdline((struct cmdline_info *)p);
3457 vim_free(p);
3458 }
3459}
3460#endif
3461
Bram Moolenaar8299df92004-07-10 09:47:34 +00003462/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003463 * Paste a yank register into the command line.
3464 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003465 * insert_reg() can't be used here, because special characters from the
3466 * register contents will be interpreted as commands.
3467 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003468 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003469 */
3470 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003471cmdline_paste(
3472 int regname,
3473 int literally, /* Insert text literally instead of "as typed" */
3474 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003475{
3476 long i;
3477 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003478 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003479 int allocated;
3480 struct cmdline_info save_ccline;
3481
3482 /* check for valid regname; also accept special characters for CTRL-R in
3483 * the command line */
3484 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003485 && regname != Ctrl_A && regname != Ctrl_L
3486 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003487 return FAIL;
3488
3489 /* A register containing CTRL-R can cause an endless loop. Allow using
3490 * CTRL-C to break the loop. */
3491 line_breakcheck();
3492 if (got_int)
3493 return FAIL;
3494
3495#ifdef FEAT_CLIPBOARD
3496 regname = may_get_selection(regname);
3497#endif
3498
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003499 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003500 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003501 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003502 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003503 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003504 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003505 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003506
3507 if (i)
3508 {
3509 /* Got the value of a special register in "arg". */
3510 if (arg == NULL)
3511 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003512
3513 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3514 * part of the word. */
3515 p = arg;
3516 if (p_is && regname == Ctrl_W)
3517 {
3518 char_u *w;
3519 int len;
3520
3521 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003522 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003523 {
3524#ifdef FEAT_MBYTE
3525 if (has_mbyte)
3526 {
3527 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3528 if (!vim_iswordc(mb_ptr2char(w - len)))
3529 break;
3530 w -= len;
3531 }
3532 else
3533#endif
3534 {
3535 if (!vim_iswordc(w[-1]))
3536 break;
3537 --w;
3538 }
3539 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003540 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003541 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3542 p += len;
3543 }
3544
3545 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003546 if (allocated)
3547 vim_free(arg);
3548 return OK;
3549 }
3550
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003551 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003552}
3553
3554/*
3555 * Put a string on the command line.
3556 * When "literally" is TRUE, insert literally.
3557 * When "literally" is FALSE, insert as typed, but don't leave the command
3558 * line.
3559 */
3560 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003561cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003562{
3563 int c, cv;
3564
3565 if (literally)
3566 put_on_cmdline(s, -1, TRUE);
3567 else
3568 while (*s != NUL)
3569 {
3570 cv = *s;
3571 if (cv == Ctrl_V && s[1])
3572 ++s;
3573#ifdef FEAT_MBYTE
3574 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003575 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003576 else
3577#endif
3578 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003579 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3580 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581#ifdef UNIX
3582 || c == intr_char
3583#endif
3584 || (c == Ctrl_BSL && *s == Ctrl_N))
3585 stuffcharReadbuff(Ctrl_V);
3586 stuffcharReadbuff(c);
3587 }
3588}
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590#ifdef FEAT_WILDMENU
3591/*
3592 * Delete characters on the command line, from "from" to the current
3593 * position.
3594 */
3595 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003596cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597{
3598 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3599 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3600 ccline.cmdlen -= ccline.cmdpos - from;
3601 ccline.cmdpos = from;
3602}
3603#endif
3604
3605/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003606 * This function is called when the screen size changes and with incremental
3607 * search and in other situations where the command line may have been
3608 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 */
3610 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003611redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003613 redrawcmdline_ex(TRUE);
3614}
3615
3616 void
3617redrawcmdline_ex(int do_compute_cmdrow)
3618{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (cmd_silent)
3620 return;
3621 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003622 if (do_compute_cmdrow)
3623 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 redrawcmd();
3625 cursorcmd();
3626}
3627
3628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003629redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
3631 int i;
3632
3633 if (cmd_silent)
3634 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003635 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 msg_putchar(ccline.cmdfirstc);
3637 if (ccline.cmdprompt != NULL)
3638 {
3639 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3640 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3641 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003642 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 --ccline.cmdindent;
3644 }
3645 else
3646 for (i = ccline.cmdindent; i > 0; --i)
3647 msg_putchar(' ');
3648}
3649
3650/*
3651 * Redraw what is currently on the command line.
3652 */
3653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003654redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 if (cmd_silent)
3657 return;
3658
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003659 /* when 'incsearch' is set there may be no command line while redrawing */
3660 if (ccline.cmdbuff == NULL)
3661 {
3662 windgoto(cmdline_row, 0);
3663 msg_clr_eos();
3664 return;
3665 }
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 msg_start();
3668 redrawcmdprompt();
3669
3670 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3671 msg_no_more = TRUE;
3672 draw_cmdline(0, ccline.cmdlen);
3673 msg_clr_eos();
3674 msg_no_more = FALSE;
3675
3676 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003677 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003678 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679
3680 /*
3681 * An emsg() before may have set msg_scroll. This is used in normal mode,
3682 * in cmdline mode we can reset them now.
3683 */
3684 msg_scroll = FALSE; /* next message overwrites cmdline */
3685
3686 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3687 * in cmdline mode */
3688 skip_redraw = FALSE;
3689}
3690
3691 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003692compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003694 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 cmdline_row = Rows - 1;
3696 else
3697 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003698 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699}
3700
3701 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003702cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 if (cmd_silent)
3705 return;
3706
3707#ifdef FEAT_RIGHTLEFT
3708 if (cmdmsg_rl)
3709 {
3710 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3711 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3712 if (msg_row <= 0)
3713 msg_row = Rows - 1;
3714 }
3715 else
3716#endif
3717 {
3718 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3719 msg_col = ccline.cmdspos % (int)Columns;
3720 if (msg_row >= Rows)
3721 msg_row = Rows - 1;
3722 }
3723
3724 windgoto(msg_row, msg_col);
3725#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003726 if (p_imst == IM_ON_THE_SPOT)
3727 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#endif
3729#ifdef MCH_CURSOR_SHAPE
3730 mch_update_cursor();
3731#endif
3732}
3733
3734 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003735gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
3737 msg_start();
3738#ifdef FEAT_RIGHTLEFT
3739 if (cmdmsg_rl)
3740 msg_col = Columns - 1;
3741 else
3742#endif
3743 msg_col = 0; /* always start in column 0 */
3744 if (clr) /* clear the bottom line(s) */
3745 msg_clr_eos(); /* will reset clear_cmdline */
3746 windgoto(cmdline_row, 0);
3747}
3748
3749/*
3750 * Check the word in front of the cursor for an abbreviation.
3751 * Called when the non-id character "c" has been entered.
3752 * When an abbreviation is recognized it is removed from the text with
3753 * backspaces and the replacement string is inserted, followed by "c".
3754 */
3755 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003756ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003758 int spos = 0;
3759
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3761 return FALSE;
3762
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003763 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3764 * Actually accepts any mark. */
3765 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3766 spos++;
3767 if (ccline.cmdlen - spos > 5
3768 && ccline.cmdbuff[spos] == '\''
3769 && ccline.cmdbuff[spos + 2] == ','
3770 && ccline.cmdbuff[spos + 3] == '\'')
3771 spos += 5;
3772 else
3773 /* check abbreviation from the beginning of the commandline */
3774 spos = 0;
3775
3776 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777}
3778
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003779#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3780 static int
3781#ifdef __BORLANDC__
3782_RTLENTRYF
3783#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003784sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003785{
3786 char_u *p1 = *(char_u **)s1;
3787 char_u *p2 = *(char_u **)s2;
3788
3789 if (*p1 != '<' && *p2 == '<') return -1;
3790 if (*p1 == '<' && *p2 != '<') return 1;
3791 return STRCMP(p1, p2);
3792}
3793#endif
3794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795/*
3796 * Return FAIL if this is not an appropriate context in which to do
3797 * completion of anything, return OK if it is (even if there are no matches).
3798 * For the caller, this means that the character is just passed through like a
3799 * normal character (instead of being expanded). This allows :s/^I^D etc.
3800 */
3801 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003802nextwild(
3803 expand_T *xp,
3804 int type,
3805 int options, /* extra options for ExpandOne() */
3806 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807{
3808 int i, j;
3809 char_u *p1;
3810 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 int difflen;
3812 int v;
3813
3814 if (xp->xp_numfiles == -1)
3815 {
3816 set_expand_context(xp);
3817 cmd_showtail = expand_showtail(xp);
3818 }
3819
3820 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3821 {
3822 beep_flush();
3823 return OK; /* Something illegal on command line */
3824 }
3825 if (xp->xp_context == EXPAND_NOTHING)
3826 {
3827 /* Caller can use the character as a normal char instead */
3828 return FAIL;
3829 }
3830
3831 MSG_PUTS("..."); /* show that we are busy */
3832 out_flush();
3833
3834 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003835 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836
3837 if (type == WILD_NEXT || type == WILD_PREV)
3838 {
3839 /*
3840 * Get next/previous match for a previous expanded pattern.
3841 */
3842 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3843 }
3844 else
3845 {
3846 /*
3847 * Translate string into pattern and expand it.
3848 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003849 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3850 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 p2 = NULL;
3852 else
3853 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003854 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003855 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3856 if (escape)
3857 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003858
3859 if (p_wic)
3860 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003861 p2 = ExpandOne(xp, p1,
3862 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003863 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003865 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (p2 != NULL && type == WILD_LONGEST)
3867 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003868 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (ccline.cmdbuff[i + j] == '*'
3870 || ccline.cmdbuff[i + j] == '?')
3871 break;
3872 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003873 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875 }
3876 }
3877
3878 if (p2 != NULL && !got_int)
3879 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003880 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003881 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003883 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 xp->xp_pattern = ccline.cmdbuff + i;
3885 }
3886 else
3887 v = OK;
3888 if (v == OK)
3889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003890 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3891 &ccline.cmdbuff[ccline.cmdpos],
3892 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3893 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 ccline.cmdlen += difflen;
3895 ccline.cmdpos += difflen;
3896 }
3897 }
3898 vim_free(p2);
3899
3900 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003901 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902
3903 /* When expanding a ":map" command and no matches are found, assume that
3904 * the key is supposed to be inserted literally */
3905 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3906 return FAIL;
3907
3908 if (xp->xp_numfiles <= 0 && p2 == NULL)
3909 beep_flush();
3910 else if (xp->xp_numfiles == 1)
3911 /* free expanded pattern */
3912 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3913
3914 return OK;
3915}
3916
3917/*
3918 * Do wildcard expansion on the string 'str'.
3919 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003920 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 * Return NULL for failure.
3922 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003923 * "orig" is the originally expanded string, copied to allocated memory. It
3924 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3925 * WILD_PREV "orig" should be NULL.
3926 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003927 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3928 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 *
3930 * mode = WILD_FREE: just free previously expanded matches
3931 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3932 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3933 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3934 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3935 * mode = WILD_ALL: return all matches concatenated
3936 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003937 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 *
3939 * options = WILD_LIST_NOTFOUND: list entries without a match
3940 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3941 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3942 * options = WILD_NO_BEEP: Don't beep for multiple matches
3943 * options = WILD_ADD_SLASH: add a slash after directory names
3944 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3945 * options = WILD_SILENT: don't print warning messages
3946 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003947 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 *
3949 * The variables xp->xp_context and xp->xp_backslash must have been set!
3950 */
3951 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003952ExpandOne(
3953 expand_T *xp,
3954 char_u *str,
3955 char_u *orig, /* allocated copy of original of expanded string */
3956 int options,
3957 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958{
3959 char_u *ss = NULL;
3960 static int findex;
3961 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003962 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 int i;
3964 long_u len;
3965 int non_suf_match; /* number without matching suffix */
3966
3967 /*
3968 * first handle the case of using an old match
3969 */
3970 if (mode == WILD_NEXT || mode == WILD_PREV)
3971 {
3972 if (xp->xp_numfiles > 0)
3973 {
3974 if (mode == WILD_PREV)
3975 {
3976 if (findex == -1)
3977 findex = xp->xp_numfiles;
3978 --findex;
3979 }
3980 else /* mode == WILD_NEXT */
3981 ++findex;
3982
3983 /*
3984 * When wrapping around, return the original string, set findex to
3985 * -1.
3986 */
3987 if (findex < 0)
3988 {
3989 if (orig_save == NULL)
3990 findex = xp->xp_numfiles - 1;
3991 else
3992 findex = -1;
3993 }
3994 if (findex >= xp->xp_numfiles)
3995 {
3996 if (orig_save == NULL)
3997 findex = 0;
3998 else
3999 findex = -1;
4000 }
4001#ifdef FEAT_WILDMENU
4002 if (p_wmnu)
4003 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4004 findex, cmd_showtail);
4005#endif
4006 if (findex == -1)
4007 return vim_strsave(orig_save);
4008 return vim_strsave(xp->xp_files[findex]);
4009 }
4010 else
4011 return NULL;
4012 }
4013
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004014 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4016 {
4017 FreeWild(xp->xp_numfiles, xp->xp_files);
4018 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004019 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 }
4021 findex = 0;
4022
4023 if (mode == WILD_FREE) /* only release file name */
4024 return NULL;
4025
4026 if (xp->xp_numfiles == -1)
4027 {
4028 vim_free(orig_save);
4029 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004030 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031
4032 /*
4033 * Do the expansion.
4034 */
4035 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4036 options) == FAIL)
4037 {
4038#ifdef FNAME_ILLEGAL
4039 /* Illegal file name has been silently skipped. But when there
4040 * are wildcards, the real problem is that there was no match,
4041 * causing the pattern to be added, which has illegal characters.
4042 */
4043 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4044 EMSG2(_(e_nomatch2), str);
4045#endif
4046 }
4047 else if (xp->xp_numfiles == 0)
4048 {
4049 if (!(options & WILD_SILENT))
4050 EMSG2(_(e_nomatch2), str);
4051 }
4052 else
4053 {
4054 /* Escape the matches for use on the command line. */
4055 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4056
4057 /*
4058 * Check for matching suffixes in file names.
4059 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004060 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4061 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 {
4063 if (xp->xp_numfiles)
4064 non_suf_match = xp->xp_numfiles;
4065 else
4066 non_suf_match = 1;
4067 if ((xp->xp_context == EXPAND_FILES
4068 || xp->xp_context == EXPAND_DIRECTORIES)
4069 && xp->xp_numfiles > 1)
4070 {
4071 /*
4072 * More than one match; check suffix.
4073 * The files will have been sorted on matching suffix in
4074 * expand_wildcards, only need to check the first two.
4075 */
4076 non_suf_match = 0;
4077 for (i = 0; i < 2; ++i)
4078 if (match_suffix(xp->xp_files[i]))
4079 ++non_suf_match;
4080 }
4081 if (non_suf_match != 1)
4082 {
4083 /* Can we ever get here unless it's while expanding
4084 * interactively? If not, we can get rid of this all
4085 * together. Don't really want to wait for this message
4086 * (and possibly have to hit return to continue!).
4087 */
4088 if (!(options & WILD_SILENT))
4089 EMSG(_(e_toomany));
4090 else if (!(options & WILD_NO_BEEP))
4091 beep_flush();
4092 }
4093 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4094 ss = vim_strsave(xp->xp_files[0]);
4095 }
4096 }
4097 }
4098
4099 /* Find longest common part */
4100 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4101 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004102 int mb_len = 1;
4103 int c0, ci;
4104
4105 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004107#ifdef FEAT_MBYTE
4108 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004110 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4111 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4112 }
4113 else
4114#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004115 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004116 for (i = 1; i < xp->xp_numfiles; ++i)
4117 {
4118#ifdef FEAT_MBYTE
4119 if (has_mbyte)
4120 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4121 else
4122#endif
4123 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004124 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004126 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004127 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004129 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 break;
4131 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004132 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134 }
4135 if (i < xp->xp_numfiles)
4136 {
4137 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004138 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 break;
4140 }
4141 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004142
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 ss = alloc((unsigned)len + 1);
4144 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004145 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 findex = -1; /* next p_wc gets first one */
4147 }
4148
4149 /* Concatenate all matching names */
4150 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4151 {
4152 len = 0;
4153 for (i = 0; i < xp->xp_numfiles; ++i)
4154 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4155 ss = lalloc(len, TRUE);
4156 if (ss != NULL)
4157 {
4158 *ss = NUL;
4159 for (i = 0; i < xp->xp_numfiles; ++i)
4160 {
4161 STRCAT(ss, xp->xp_files[i]);
4162 if (i != xp->xp_numfiles - 1)
4163 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4164 }
4165 }
4166 }
4167
4168 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4169 ExpandCleanup(xp);
4170
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004171 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004172 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004173 vim_free(orig);
4174
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 return ss;
4176}
4177
4178/*
4179 * Prepare an expand structure for use.
4180 */
4181 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004182ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004184 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004185 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004187#ifndef BACKSLASH_IN_FILENAME
4188 xp->xp_shell = FALSE;
4189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 xp->xp_numfiles = -1;
4191 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004192#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4193 xp->xp_arg = NULL;
4194#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004195 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196}
4197
4198/*
4199 * Cleanup an expand structure after use.
4200 */
4201 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004202ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204 if (xp->xp_numfiles >= 0)
4205 {
4206 FreeWild(xp->xp_numfiles, xp->xp_files);
4207 xp->xp_numfiles = -1;
4208 }
4209}
4210
4211 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004212ExpandEscape(
4213 expand_T *xp,
4214 char_u *str,
4215 int numfiles,
4216 char_u **files,
4217 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218{
4219 int i;
4220 char_u *p;
4221
4222 /*
4223 * May change home directory back to "~"
4224 */
4225 if (options & WILD_HOME_REPLACE)
4226 tilde_replace(str, numfiles, files);
4227
4228 if (options & WILD_ESCAPE)
4229 {
4230 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004231 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004232 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 || xp->xp_context == EXPAND_BUFFERS
4234 || xp->xp_context == EXPAND_DIRECTORIES)
4235 {
4236 /*
4237 * Insert a backslash into a file name before a space, \, %, #
4238 * and wildmatch characters, except '~'.
4239 */
4240 for (i = 0; i < numfiles; ++i)
4241 {
4242 /* for ":set path=" we need to escape spaces twice */
4243 if (xp->xp_backslash == XP_BS_THREE)
4244 {
4245 p = vim_strsave_escaped(files[i], (char_u *)" ");
4246 if (p != NULL)
4247 {
4248 vim_free(files[i]);
4249 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004250#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 p = vim_strsave_escaped(files[i], (char_u *)" ");
4252 if (p != NULL)
4253 {
4254 vim_free(files[i]);
4255 files[i] = p;
4256 }
4257#endif
4258 }
4259 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004260#ifdef BACKSLASH_IN_FILENAME
4261 p = vim_strsave_fnameescape(files[i], FALSE);
4262#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004263 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 if (p != NULL)
4266 {
4267 vim_free(files[i]);
4268 files[i] = p;
4269 }
4270
4271 /* If 'str' starts with "\~", replace "~" at start of
4272 * files[i] with "\~". */
4273 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004274 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 }
4276 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004277
4278 /* If the first file starts with a '+' escape it. Otherwise it
4279 * could be seen as "+cmd". */
4280 if (*files[0] == '+')
4281 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 else if (xp->xp_context == EXPAND_TAGS)
4284 {
4285 /*
4286 * Insert a backslash before characters in a tag name that
4287 * would terminate the ":tag" command.
4288 */
4289 for (i = 0; i < numfiles; ++i)
4290 {
4291 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4292 if (p != NULL)
4293 {
4294 vim_free(files[i]);
4295 files[i] = p;
4296 }
4297 }
4298 }
4299 }
4300}
4301
4302/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004303 * Escape special characters in "fname" for when used as a file name argument
4304 * after a Vim command, or, when "shell" is non-zero, a shell command.
4305 * Returns the result in allocated memory.
4306 */
4307 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004308vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004309{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004310 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004311#ifdef BACKSLASH_IN_FILENAME
4312 char_u buf[20];
4313 int j = 0;
4314
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004315 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004316 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004317 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004318 buf[j++] = *p;
4319 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004320 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004321#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004322 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4323 if (shell && csh_like_shell() && p != NULL)
4324 {
4325 char_u *s;
4326
4327 /* For csh and similar shells need to put two backslashes before '!'.
4328 * One is taken by Vim, one by the shell. */
4329 s = vim_strsave_escaped(p, (char_u *)"!");
4330 vim_free(p);
4331 p = s;
4332 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004333#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004334
4335 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4336 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004337 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004338 escape_fname(&p);
4339
4340 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004341}
4342
4343/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004344 * Put a backslash before the file name in "pp", which is in allocated memory.
4345 */
4346 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004347escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004348{
4349 char_u *p;
4350
4351 p = alloc((unsigned)(STRLEN(*pp) + 2));
4352 if (p != NULL)
4353 {
4354 p[0] = '\\';
4355 STRCPY(p + 1, *pp);
4356 vim_free(*pp);
4357 *pp = p;
4358 }
4359}
4360
4361/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 * For each file name in files[num_files]:
4363 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4364 */
4365 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004366tilde_replace(
4367 char_u *orig_pat,
4368 int num_files,
4369 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370{
4371 int i;
4372 char_u *p;
4373
4374 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4375 {
4376 for (i = 0; i < num_files; ++i)
4377 {
4378 p = home_replace_save(NULL, files[i]);
4379 if (p != NULL)
4380 {
4381 vim_free(files[i]);
4382 files[i] = p;
4383 }
4384 }
4385 }
4386}
4387
4388/*
4389 * Show all matches for completion on the command line.
4390 * Returns EXPAND_NOTHING when the character that triggered expansion should
4391 * be inserted like a normal character.
4392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4397 int num_files;
4398 char_u **files_found;
4399 int i, j, k;
4400 int maxlen;
4401 int lines;
4402 int columns;
4403 char_u *p;
4404 int lastlen;
4405 int attr;
4406 int showtail;
4407
4408 if (xp->xp_numfiles == -1)
4409 {
4410 set_expand_context(xp);
4411 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4412 &num_files, &files_found);
4413 showtail = expand_showtail(xp);
4414 if (i != EXPAND_OK)
4415 return i;
4416
4417 }
4418 else
4419 {
4420 num_files = xp->xp_numfiles;
4421 files_found = xp->xp_files;
4422 showtail = cmd_showtail;
4423 }
4424
4425#ifdef FEAT_WILDMENU
4426 if (!wildmenu)
4427 {
4428#endif
4429 msg_didany = FALSE; /* lines_left will be set */
4430 msg_start(); /* prepare for paging */
4431 msg_putchar('\n');
4432 out_flush();
4433 cmdline_row = msg_row;
4434 msg_didany = FALSE; /* lines_left will be set again */
4435 msg_start(); /* prepare for paging */
4436#ifdef FEAT_WILDMENU
4437 }
4438#endif
4439
4440 if (got_int)
4441 got_int = FALSE; /* only int. the completion, not the cmd line */
4442#ifdef FEAT_WILDMENU
4443 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004444 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445#endif
4446 else
4447 {
4448 /* find the length of the longest file name */
4449 maxlen = 0;
4450 for (i = 0; i < num_files; ++i)
4451 {
4452 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004453 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 || xp->xp_context == EXPAND_BUFFERS))
4455 {
4456 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4457 j = vim_strsize(NameBuff);
4458 }
4459 else
4460 j = vim_strsize(L_SHOWFILE(i));
4461 if (j > maxlen)
4462 maxlen = j;
4463 }
4464
4465 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4466 lines = num_files;
4467 else
4468 {
4469 /* compute the number of columns and lines for the listing */
4470 maxlen += 2; /* two spaces between file names */
4471 columns = ((int)Columns + 2) / maxlen;
4472 if (columns < 1)
4473 columns = 1;
4474 lines = (num_files + columns - 1) / columns;
4475 }
4476
Bram Moolenaar8820b482017-03-16 17:23:31 +01004477 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478
4479 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4480 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004481 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 msg_clr_eos();
4483 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004484 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486
4487 /* list the files line by line */
4488 for (i = 0; i < lines; ++i)
4489 {
4490 lastlen = 999;
4491 for (k = i; k < num_files; k += lines)
4492 {
4493 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4494 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 p = files_found[k] + STRLEN(files_found[k]) + 1;
4497 msg_advance(maxlen + 1);
4498 msg_puts(p);
4499 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004500 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 break;
4502 }
4503 for (j = maxlen - lastlen; --j >= 0; )
4504 msg_putchar(' ');
4505 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004506 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 || xp->xp_context == EXPAND_BUFFERS)
4508 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004509 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004510 if (xp->xp_numfiles != -1)
4511 {
4512 char_u *halved_slash;
4513 char_u *exp_path;
4514
4515 /* Expansion was done before and special characters
4516 * were escaped, need to halve backslashes. Also
4517 * $HOME has been replaced with ~/. */
4518 exp_path = expand_env_save_opt(files_found[k], TRUE);
4519 halved_slash = backslash_halve_save(
4520 exp_path != NULL ? exp_path : files_found[k]);
4521 j = mch_isdir(halved_slash != NULL ? halved_slash
4522 : files_found[k]);
4523 vim_free(exp_path);
4524 vim_free(halved_slash);
4525 }
4526 else
4527 /* Expansion was done here, file names are literal. */
4528 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 if (showtail)
4530 p = L_SHOWFILE(k);
4531 else
4532 {
4533 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4534 TRUE);
4535 p = NameBuff;
4536 }
4537 }
4538 else
4539 {
4540 j = FALSE;
4541 p = L_SHOWFILE(k);
4542 }
4543 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4544 }
4545 if (msg_col > 0) /* when not wrapped around */
4546 {
4547 msg_clr_eos();
4548 msg_putchar('\n');
4549 }
4550 out_flush(); /* show one line at a time */
4551 if (got_int)
4552 {
4553 got_int = FALSE;
4554 break;
4555 }
4556 }
4557
4558 /*
4559 * we redraw the command below the lines that we have just listed
4560 * This is a bit tricky, but it saves a lot of screen updating.
4561 */
4562 cmdline_row = msg_row; /* will put it back later */
4563 }
4564
4565 if (xp->xp_numfiles == -1)
4566 FreeWild(num_files, files_found);
4567
4568 return EXPAND_OK;
4569}
4570
4571/*
4572 * Private gettail for showmatches() (and win_redr_status_matches()):
4573 * Find tail of file name path, but ignore trailing "/".
4574 */
4575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004576sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577{
4578 char_u *p;
4579 char_u *t = s;
4580 int had_sep = FALSE;
4581
4582 for (p = s; *p != NUL; )
4583 {
4584 if (vim_ispathsep(*p)
4585#ifdef BACKSLASH_IN_FILENAME
4586 && !rem_backslash(p)
4587#endif
4588 )
4589 had_sep = TRUE;
4590 else if (had_sep)
4591 {
4592 t = p;
4593 had_sep = FALSE;
4594 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004595 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
4597 return t;
4598}
4599
4600/*
4601 * Return TRUE if we only need to show the tail of completion matches.
4602 * When not completing file names or there is a wildcard in the path FALSE is
4603 * returned.
4604 */
4605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004606expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607{
4608 char_u *s;
4609 char_u *end;
4610
4611 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004612 if (xp->xp_context != EXPAND_FILES
4613 && xp->xp_context != EXPAND_SHELLCMD
4614 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 return FALSE;
4616
4617 end = gettail(xp->xp_pattern);
4618 if (end == xp->xp_pattern) /* there is no path separator */
4619 return FALSE;
4620
4621 for (s = xp->xp_pattern; s < end; s++)
4622 {
4623 /* Skip escaped wildcards. Only when the backslash is not a path
4624 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4625 if (rem_backslash(s))
4626 ++s;
4627 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4628 return FALSE;
4629 }
4630 return TRUE;
4631}
4632
4633/*
4634 * Prepare a string for expansion.
4635 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004636 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 * When expanding other names: The string will be used with regcomp(). Copy
4638 * the name into allocated memory and prepend "^".
4639 */
4640 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004641addstar(
4642 char_u *fname,
4643 int len,
4644 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
4646 char_u *retval;
4647 int i, j;
4648 int new_len;
4649 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004650 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004652 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004653 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004654 && context != EXPAND_SHELLCMD
4655 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 {
4657 /*
4658 * Matching will be done internally (on something other than files).
4659 * So we convert the file-matching-type wildcards into our kind for
4660 * use with vim_regcomp(). First work out how long it will be:
4661 */
4662
4663 /* For help tags the translation is done in find_help_tags().
4664 * For a tag pattern starting with "/" no translation is needed. */
4665 if (context == EXPAND_HELP
4666 || context == EXPAND_COLORS
4667 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004668 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004669 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004670 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004671 || ((context == EXPAND_TAGS_LISTFILES
4672 || context == EXPAND_TAGS)
4673 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 retval = vim_strnsave(fname, len);
4675 else
4676 {
4677 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4678 for (i = 0; i < len; i++)
4679 {
4680 if (fname[i] == '*' || fname[i] == '~')
4681 new_len++; /* '*' needs to be replaced by ".*"
4682 '~' needs to be replaced by "\~" */
4683
4684 /* Buffer names are like file names. "." should be literal */
4685 if (context == EXPAND_BUFFERS && fname[i] == '.')
4686 new_len++; /* "." becomes "\." */
4687
4688 /* Custom expansion takes care of special things, match
4689 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004690 if ((context == EXPAND_USER_DEFINED
4691 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 new_len++; /* '\' becomes "\\" */
4693 }
4694 retval = alloc(new_len);
4695 if (retval != NULL)
4696 {
4697 retval[0] = '^';
4698 j = 1;
4699 for (i = 0; i < len; i++, j++)
4700 {
4701 /* Skip backslash. But why? At least keep it for custom
4702 * expansion. */
4703 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004704 && context != EXPAND_USER_LIST
4705 && fname[i] == '\\'
4706 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 break;
4708
4709 switch (fname[i])
4710 {
4711 case '*': retval[j++] = '.';
4712 break;
4713 case '~': retval[j++] = '\\';
4714 break;
4715 case '?': retval[j] = '.';
4716 continue;
4717 case '.': if (context == EXPAND_BUFFERS)
4718 retval[j++] = '\\';
4719 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004720 case '\\': if (context == EXPAND_USER_DEFINED
4721 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 retval[j++] = '\\';
4723 break;
4724 }
4725 retval[j] = fname[i];
4726 }
4727 retval[j] = NUL;
4728 }
4729 }
4730 }
4731 else
4732 {
4733 retval = alloc(len + 4);
4734 if (retval != NULL)
4735 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004736 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737
4738 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004739 * Don't add a star to *, ~, ~user, $var or `cmd`.
4740 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 * ~ would be at the start of the file name, but not the tail.
4742 * $ could be anywhere in the tail.
4743 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004744 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 */
4746 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004747 ends_in_star = (len > 0 && retval[len - 1] == '*');
4748#ifndef BACKSLASH_IN_FILENAME
4749 for (i = len - 2; i >= 0; --i)
4750 {
4751 if (retval[i] != '\\')
4752 break;
4753 ends_in_star = !ends_in_star;
4754 }
4755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004757 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 && vim_strchr(tail, '$') == NULL
4759 && vim_strchr(retval, '`') == NULL)
4760 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004761 else if (len > 0 && retval[len - 1] == '$')
4762 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 retval[len] = NUL;
4764 }
4765 }
4766 return retval;
4767}
4768
4769/*
4770 * Must parse the command line so far to work out what context we are in.
4771 * Completion can then be done based on that context.
4772 * This routine sets the variables:
4773 * xp->xp_pattern The start of the pattern to be expanded within
4774 * the command line (ends at the cursor).
4775 * xp->xp_context The type of thing to expand. Will be one of:
4776 *
4777 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4778 * the command line, like an unknown command. Caller
4779 * should beep.
4780 * EXPAND_NOTHING Unrecognised context for completion, use char like
4781 * a normal char, rather than for completion. eg
4782 * :s/^I/
4783 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4784 * it.
4785 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4786 * EXPAND_FILES After command with XFILE set, or after setting
4787 * with P_EXPAND set. eg :e ^I, :w>>^I
4788 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4789 * when we know only directories are of interest. eg
4790 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004791 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4793 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4794 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4795 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4796 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4797 * EXPAND_EVENTS Complete event names
4798 * EXPAND_SYNTAX Complete :syntax command arguments
4799 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4800 * EXPAND_AUGROUP Complete autocommand group names
4801 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4802 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4803 * eg :unmap a^I , :cunab x^I
4804 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4805 * eg :call sub^I
4806 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4807 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4808 * names in expressions, eg :while s^I
4809 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004810 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 */
4812 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004813set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004815 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 if (ccline.cmdfirstc != ':'
4817#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004818 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004819 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820#endif
4821 )
4822 {
4823 xp->xp_context = EXPAND_NOTHING;
4824 return;
4825 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004826 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827}
4828
4829 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004830set_cmd_context(
4831 expand_T *xp,
4832 char_u *str, /* start of command line */
4833 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004834 int col, /* position of cursor */
4835 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
4837 int old_char = NUL;
4838 char_u *nextcomm;
4839
4840 /*
4841 * Avoid a UMR warning from Purify, only save the character if it has been
4842 * written before.
4843 */
4844 if (col < len)
4845 old_char = str[col];
4846 str[col] = NUL;
4847 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004848
4849#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004850 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004851 {
4852# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004853 /* pass CMD_SIZE because there is no real command */
4854 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004855# endif
4856 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004857 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004858 {
4859 xp->xp_context = ccline.xp_context;
4860 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004861# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004862 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004863# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004864 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004865 else
4866#endif
4867 while (nextcomm != NULL)
4868 nextcomm = set_one_cmd_context(xp, nextcomm);
4869
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004870 /* Store the string here so that call_user_expand_func() can get to them
4871 * easily. */
4872 xp->xp_line = str;
4873 xp->xp_col = col;
4874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 str[col] = old_char;
4876}
4877
4878/*
4879 * Expand the command line "str" from context "xp".
4880 * "xp" must have been set by set_cmd_context().
4881 * xp->xp_pattern points into "str", to where the text that is to be expanded
4882 * starts.
4883 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4884 * cursor.
4885 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4886 * key that triggered expansion literally.
4887 * Returns EXPAND_OK otherwise.
4888 */
4889 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004890expand_cmdline(
4891 expand_T *xp,
4892 char_u *str, /* start of command line */
4893 int col, /* position of cursor */
4894 int *matchcount, /* return: nr of matches */
4895 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
4897 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004898 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899
4900 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4901 {
4902 beep_flush();
4903 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4904 }
4905 if (xp->xp_context == EXPAND_NOTHING)
4906 {
4907 /* Caller can use the character as a normal char instead */
4908 return EXPAND_NOTHING;
4909 }
4910
4911 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004912 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4913 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 if (file_str == NULL)
4915 return EXPAND_UNSUCCESSFUL;
4916
Bram Moolenaar94950a92010-12-02 16:01:29 +01004917 if (p_wic)
4918 options += WILD_ICASE;
4919
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004921 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
4923 *matchcount = 0;
4924 *matches = NULL;
4925 }
4926 vim_free(file_str);
4927
4928 return EXPAND_OK;
4929}
4930
4931#ifdef FEAT_MULTI_LANG
4932/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004933 * Cleanup matches for help tags:
4934 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4935 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004937static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004940cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
4942 int i, j;
4943 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004944 char_u buf[4];
4945 char_u *p = buf;
4946
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004947 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004948 {
4949 *p++ = '@';
4950 *p++ = p_hlg[0];
4951 *p++ = p_hlg[1];
4952 }
4953 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954
4955 for (i = 0; i < num_file; ++i)
4956 {
4957 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004958 if (len <= 0)
4959 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004960 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
4962 /* Sorting on priority means the same item in another language may
4963 * be anywhere. Search all items for a match up to the "@en". */
4964 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004965 if (j != i && (int)STRLEN(file[j]) == len + 3
4966 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 break;
4968 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004969 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 file[i][len] = NUL;
4971 }
4972 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004973
4974 if (*buf != NUL)
4975 for (i = 0; i < num_file; ++i)
4976 {
4977 len = (int)STRLEN(file[i]) - 3;
4978 if (len <= 0)
4979 continue;
4980 if (STRCMP(file[i] + len, buf) == 0)
4981 {
4982 /* remove the default language */
4983 file[i][len] = NUL;
4984 }
4985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986}
4987#endif
4988
4989/*
4990 * Do the expansion based on xp->xp_context and "pat".
4991 */
4992 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004993ExpandFromContext(
4994 expand_T *xp,
4995 char_u *pat,
4996 int *num_file,
4997 char_u ***file,
4998 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999{
5000#ifdef FEAT_CMDL_COMPL
5001 regmatch_T regmatch;
5002#endif
5003 int ret;
5004 int flags;
5005
5006 flags = EW_DIR; /* include directories */
5007 if (options & WILD_LIST_NOTFOUND)
5008 flags |= EW_NOTFOUND;
5009 if (options & WILD_ADD_SLASH)
5010 flags |= EW_ADDSLASH;
5011 if (options & WILD_KEEP_ALL)
5012 flags |= EW_KEEPALL;
5013 if (options & WILD_SILENT)
5014 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005015 if (options & WILD_ALLLINKS)
5016 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005018 if (xp->xp_context == EXPAND_FILES
5019 || xp->xp_context == EXPAND_DIRECTORIES
5020 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 {
5022 /*
5023 * Expand file or directory names.
5024 */
5025 int free_pat = FALSE;
5026 int i;
5027
5028 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5029 * space */
5030 if (xp->xp_backslash != XP_BS_NONE)
5031 {
5032 free_pat = TRUE;
5033 pat = vim_strsave(pat);
5034 for (i = 0; pat[i]; ++i)
5035 if (pat[i] == '\\')
5036 {
5037 if (xp->xp_backslash == XP_BS_THREE
5038 && pat[i + 1] == '\\'
5039 && pat[i + 2] == '\\'
5040 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005041 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 if (xp->xp_backslash == XP_BS_ONE
5043 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005044 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
5046 }
5047
5048 if (xp->xp_context == EXPAND_FILES)
5049 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005050 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5051 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 else
5053 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005054 if (options & WILD_ICASE)
5055 flags |= EW_ICASE;
5056
Bram Moolenaard7834d32009-12-02 16:14:36 +00005057 /* Expand wildcards, supporting %:h and the like. */
5058 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (free_pat)
5060 vim_free(pat);
5061 return ret;
5062 }
5063
5064 *file = (char_u **)"";
5065 *num_file = 0;
5066 if (xp->xp_context == EXPAND_HELP)
5067 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005068 /* With an empty argument we would get all the help tags, which is
5069 * very slow. Get matches for "help" instead. */
5070 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5071 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073#ifdef FEAT_MULTI_LANG
5074 cleanup_help_tags(*num_file, *file);
5075#endif
5076 return OK;
5077 }
5078 return FAIL;
5079 }
5080
5081#ifndef FEAT_CMDL_COMPL
5082 return FAIL;
5083#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005084 if (xp->xp_context == EXPAND_SHELLCMD)
5085 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (xp->xp_context == EXPAND_OLD_SETTING)
5087 return ExpandOldSetting(num_file, file);
5088 if (xp->xp_context == EXPAND_BUFFERS)
5089 return ExpandBufnames(pat, num_file, file, options);
5090 if (xp->xp_context == EXPAND_TAGS
5091 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5092 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5093 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 {
5095 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005096 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5097 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005100 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005101 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005102 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005103 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005104 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005105 {
5106 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005107 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005108 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005109 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005110 {
5111 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005112 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005113 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5115 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005116 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005117# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005118 if (xp->xp_context == EXPAND_PACKADD)
5119 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120
5121 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5122 if (regmatch.regprog == NULL)
5123 return FAIL;
5124
5125 /* set ignore-case according to p_ic, p_scs and pat */
5126 regmatch.rm_ic = ignorecase(pat);
5127
5128 if (xp->xp_context == EXPAND_SETTINGS
5129 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5130 ret = ExpandSettings(xp, &regmatch, num_file, file);
5131 else if (xp->xp_context == EXPAND_MAPPINGS)
5132 ret = ExpandMappings(&regmatch, num_file, file);
5133# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5134 else if (xp->xp_context == EXPAND_USER_DEFINED)
5135 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5136# endif
5137 else
5138 {
5139 static struct expgen
5140 {
5141 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005142 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005144 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 } tab[] =
5146 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005147 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5148 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005149 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005150 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005151#ifdef FEAT_CMDHIST
5152 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005155 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005156 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005157 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5158 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5159 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160#endif
5161#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005162 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5163 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5164 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5165 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166#endif
5167#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005168 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5169 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#endif
5171#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005172 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005174#ifdef FEAT_PROFILE
5175 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5176#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005177 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005178 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5179 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005180#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005181 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005182#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005183#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005184 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005185#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005186#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005187 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5190 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005191 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5192 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005194 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005195 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005196 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 };
5198 int i;
5199
5200 /*
5201 * Find a context in the table and call the ExpandGeneric() with the
5202 * right function to do the expansion.
5203 */
5204 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005205 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 if (xp->xp_context == tab[i].context)
5207 {
5208 if (tab[i].ic)
5209 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005210 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005211 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
5213 }
5214 }
5215
Bram Moolenaar473de612013-06-08 18:19:48 +02005216 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217
5218 return ret;
5219#endif /* FEAT_CMDL_COMPL */
5220}
5221
5222#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5223/*
5224 * Expand a list of names.
5225 *
5226 * Generic function for command line completion. It calls a function to
5227 * obtain strings, one by one. The strings are matched against a regexp
5228 * program. Matching strings are copied into an array, which is returned.
5229 *
5230 * Returns OK when no problems encountered, FAIL for error (out of memory).
5231 */
5232 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005233ExpandGeneric(
5234 expand_T *xp,
5235 regmatch_T *regmatch,
5236 int *num_file,
5237 char_u ***file,
5238 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005240 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241{
5242 int i;
5243 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005244 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 char_u *str;
5246
5247 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005248 * round == 0: count the number of matching names
5249 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005251 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
5253 for (i = 0; ; ++i)
5254 {
5255 str = (*func)(xp, i);
5256 if (str == NULL) /* end of list */
5257 break;
5258 if (*str == NUL) /* skip empty strings */
5259 continue;
5260
5261 if (vim_regexec(regmatch, str, (colnr_T)0))
5262 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005263 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005265 if (escaped)
5266 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5267 else
5268 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 (*file)[count] = str;
5270#ifdef FEAT_MENU
5271 if (func == get_menu_names && str != NULL)
5272 {
5273 /* test for separator added by get_menu_names() */
5274 str += STRLEN(str) - 1;
5275 if (*str == '\001')
5276 *str = '.';
5277 }
5278#endif
5279 }
5280 ++count;
5281 }
5282 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005283 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
5285 if (count == 0)
5286 return OK;
5287 *num_file = count;
5288 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5289 if (*file == NULL)
5290 {
5291 *file = (char_u **)"";
5292 return FAIL;
5293 }
5294 count = 0;
5295 }
5296 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005297
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005298 /* Sort the results. Keep menu's in the specified order. */
5299 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005300 {
5301 if (xp->xp_context == EXPAND_EXPRESSION
5302 || xp->xp_context == EXPAND_FUNCTIONS
5303 || xp->xp_context == EXPAND_USER_FUNC)
5304 /* <SNR> functions should be sorted to the end. */
5305 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5306 sort_func_compare);
5307 else
5308 sort_strings(*file, *num_file);
5309 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005310
Bram Moolenaar4f688582007-07-24 12:34:30 +00005311#ifdef FEAT_CMDL_COMPL
5312 /* Reset the variables used for special highlight names expansion, so that
5313 * they don't show up when getting normal highlight names by ID. */
5314 reset_expand_highlight();
5315#endif
5316
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 return OK;
5318}
5319
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005320/*
5321 * Complete a shell command.
5322 * Returns FAIL or OK;
5323 */
5324 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005325expand_shellcmd(
5326 char_u *filepat, /* pattern to match with command names */
5327 int *num_file, /* return: number of matches */
5328 char_u ***file, /* return: array with matches */
5329 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005330{
5331 char_u *pat;
5332 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005333 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005334 int mustfree = FALSE;
5335 garray_T ga;
5336 char_u *buf = alloc(MAXPATHL);
5337 size_t l;
5338 char_u *s, *e;
5339 int flags = flagsarg;
5340 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005341 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005342 hashtab_T found_ht;
5343 hashitem_T *hi;
5344 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005345
5346 if (buf == NULL)
5347 return FAIL;
5348
5349 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5350 * space */
5351 pat = vim_strsave(filepat);
5352 for (i = 0; pat[i]; ++i)
5353 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005354 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005355
Bram Moolenaarb5971142015-03-21 17:32:19 +01005356 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005357
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005358 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5359 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005360 path = (char_u *)".";
5361 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005362 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005363 /* For an absolute name we don't use $PATH. */
5364 if (!mch_isFullName(pat))
5365 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005366 if (path == NULL)
5367 path = (char_u *)"";
5368 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005369
5370 /*
5371 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005372 * collect them in "ga". When "." is not in $PATH also expand for the
5373 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005374 */
5375 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005376 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005377 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005378 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005379#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005380 e = vim_strchr(s, ';');
5381#else
5382 e = vim_strchr(s, ':');
5383#endif
5384 if (e == NULL)
5385 e = s + STRLEN(s);
5386
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005387 if (*s == NUL)
5388 {
5389 if (did_curdir)
5390 break;
5391 // Find directories in the current directory, path is empty.
5392 did_curdir = TRUE;
5393 flags |= EW_DIR;
5394 }
5395 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5396 {
5397 did_curdir = TRUE;
5398 flags |= EW_DIR;
5399 }
5400 else
5401 // Do not match directories inside a $PATH item.
5402 flags &= ~EW_DIR;
5403
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005404 l = e - s;
5405 if (l > MAXPATHL - 5)
5406 break;
5407 vim_strncpy(buf, s, l);
5408 add_pathsep(buf);
5409 l = STRLEN(buf);
5410 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5411
5412 /* Expand matches in one directory of $PATH. */
5413 ret = expand_wildcards(1, &buf, num_file, file, flags);
5414 if (ret == OK)
5415 {
5416 if (ga_grow(&ga, *num_file) == FAIL)
5417 FreeWild(*num_file, *file);
5418 else
5419 {
5420 for (i = 0; i < *num_file; ++i)
5421 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005422 char_u *name = (*file)[i];
5423
5424 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005425 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005426 // Check if this name was already found.
5427 hash = hash_hash(name + l);
5428 hi = hash_lookup(&found_ht, name + l, hash);
5429 if (HASHITEM_EMPTY(hi))
5430 {
5431 // Remove the path that was prepended.
5432 STRMOVE(name, name + l);
5433 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5434 hash_add_item(&found_ht, hi, name, hash);
5435 name = NULL;
5436 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005437 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005438 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005439 }
5440 vim_free(*file);
5441 }
5442 }
5443 if (*e != NUL)
5444 ++e;
5445 }
5446 *file = ga.ga_data;
5447 *num_file = ga.ga_len;
5448
5449 vim_free(buf);
5450 vim_free(pat);
5451 if (mustfree)
5452 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005453 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005454 return OK;
5455}
5456
5457
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5459/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005460 * Call "user_expand_func()" to invoke a user defined Vim script function and
5461 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005463 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005464call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005465 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005466 expand_T *xp,
5467 int *num_file,
5468 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005470 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005471 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005473 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005474 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005475 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
Bram Moolenaarb7515462013-06-29 12:58:33 +02005477 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005478 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 *num_file = 0;
5480 *file = NULL;
5481
Bram Moolenaarb7515462013-06-29 12:58:33 +02005482 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005483 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005484 keep = ccline.cmdbuff[ccline.cmdlen];
5485 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005486 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005487
Bram Moolenaarffa96842018-06-12 22:05:14 +02005488 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5489
5490 args[0].v_type = VAR_STRING;
5491 args[0].vval.v_string = pat;
5492 args[1].v_type = VAR_STRING;
5493 args[1].vval.v_string = xp->xp_line;
5494 args[2].v_type = VAR_NUMBER;
5495 args[2].vval.v_number = xp->xp_col;
5496 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005498 /* Save the cmdline, we don't know what the function may do. */
5499 save_ccline = ccline;
5500 ccline.cmdbuff = NULL;
5501 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005503
Bram Moolenaarded27a12018-08-01 19:06:03 +02005504 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005505
5506 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005508 if (ccline.cmdbuff != NULL)
5509 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005510
Bram Moolenaarffa96842018-06-12 22:05:14 +02005511 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005512 return ret;
5513}
5514
5515/*
5516 * Expand names with a function defined by the user.
5517 */
5518 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005519ExpandUserDefined(
5520 expand_T *xp,
5521 regmatch_T *regmatch,
5522 int *num_file,
5523 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005524{
5525 char_u *retstr;
5526 char_u *s;
5527 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005528 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005529 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005530 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005531
5532 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5533 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 return FAIL;
5535
5536 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005537 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
5539 e = vim_strchr(s, '\n');
5540 if (e == NULL)
5541 e = s + STRLEN(s);
5542 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005543 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005545 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5546 *e = keep;
5547
5548 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005550 if (ga_grow(&ga, 1) == FAIL)
5551 break;
5552 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5553 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 if (*e != NUL)
5557 ++e;
5558 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005559 vim_free(retstr);
5560 *file = ga.ga_data;
5561 *num_file = ga.ga_len;
5562 return OK;
5563}
5564
5565/*
5566 * Expand names with a list returned by a function defined by the user.
5567 */
5568 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005569ExpandUserList(
5570 expand_T *xp,
5571 int *num_file,
5572 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005573{
5574 list_T *retlist;
5575 listitem_T *li;
5576 garray_T ga;
5577
5578 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5579 if (retlist == NULL)
5580 return FAIL;
5581
5582 ga_init2(&ga, (int)sizeof(char *), 3);
5583 /* Loop over the items in the list. */
5584 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5585 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005586 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5587 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005588
5589 if (ga_grow(&ga, 1) == FAIL)
5590 break;
5591
5592 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005593 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005594 ++ga.ga_len;
5595 }
5596 list_unref(retlist);
5597
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 *file = ga.ga_data;
5599 *num_file = ga.ga_len;
5600 return OK;
5601}
5602#endif
5603
5604/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005605 * Expand color scheme, compiler or filetype names.
5606 * Search from 'runtimepath':
5607 * 'runtimepath'/{dirnames}/{pat}.vim
5608 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5609 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5610 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5611 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005612 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 */
5614 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005615ExpandRTDir(
5616 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005617 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618 int *num_file,
5619 char_u ***file,
5620 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 char_u *s;
5623 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005624 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005626 int i;
5627 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
5629 *num_file = 0;
5630 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005631 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005632 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005634 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005636 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5637 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005639 ga_clear_strings(&ga);
5640 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005642 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005643 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005644 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005646
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005647 if (flags & DIP_START) {
5648 for (i = 0; dirnames[i] != NULL; ++i)
5649 {
5650 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5651 if (s == NULL)
5652 {
5653 ga_clear_strings(&ga);
5654 return FAIL;
5655 }
5656 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5657 globpath(p_pp, s, &ga, 0);
5658 vim_free(s);
5659 }
5660 }
5661
5662 if (flags & DIP_OPT) {
5663 for (i = 0; dirnames[i] != NULL; ++i)
5664 {
5665 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5666 if (s == NULL)
5667 {
5668 ga_clear_strings(&ga);
5669 return FAIL;
5670 }
5671 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5672 globpath(p_pp, s, &ga, 0);
5673 vim_free(s);
5674 }
5675 }
5676
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005677 for (i = 0; i < ga.ga_len; ++i)
5678 {
5679 match = ((char_u **)ga.ga_data)[i];
5680 s = match;
5681 e = s + STRLEN(s);
5682 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5683 {
5684 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005685 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005686 if (s < match || vim_ispathsep(*s))
5687 break;
5688 ++s;
5689 *e = NUL;
5690 mch_memmove(match, s, e - s + 1);
5691 }
5692 }
5693
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005694 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005695 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005696
5697 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005698 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005699 remove_duplicates(&ga);
5700
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 *file = ga.ga_data;
5702 *num_file = ga.ga_len;
5703 return OK;
5704}
5705
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005706/*
5707 * Expand loadplugin names:
5708 * 'packpath'/pack/ * /opt/{pat}
5709 */
5710 static int
5711ExpandPackAddDir(
5712 char_u *pat,
5713 int *num_file,
5714 char_u ***file)
5715{
5716 char_u *s;
5717 char_u *e;
5718 char_u *match;
5719 garray_T ga;
5720 int i;
5721 int pat_len;
5722
5723 *num_file = 0;
5724 *file = NULL;
5725 pat_len = (int)STRLEN(pat);
5726 ga_init2(&ga, (int)sizeof(char *), 10);
5727
5728 s = alloc((unsigned)(pat_len + 26));
5729 if (s == NULL)
5730 {
5731 ga_clear_strings(&ga);
5732 return FAIL;
5733 }
5734 sprintf((char *)s, "pack/*/opt/%s*", pat);
5735 globpath(p_pp, s, &ga, 0);
5736 vim_free(s);
5737
5738 for (i = 0; i < ga.ga_len; ++i)
5739 {
5740 match = ((char_u **)ga.ga_data)[i];
5741 s = gettail(match);
5742 e = s + STRLEN(s);
5743 mch_memmove(match, s, e - s + 1);
5744 }
5745
5746 if (ga.ga_len == 0)
5747 return FAIL;
5748
5749 /* Sort and remove duplicates which can happen when specifying multiple
5750 * directories in dirnames. */
5751 remove_duplicates(&ga);
5752
5753 *file = ga.ga_data;
5754 *num_file = ga.ga_len;
5755 return OK;
5756}
5757
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758#endif
5759
5760#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5761/*
5762 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005763 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005765 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005766globpath(
5767 char_u *path,
5768 char_u *file,
5769 garray_T *ga,
5770 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771{
5772 expand_T xpc;
5773 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 int num_p;
5776 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 buf = alloc(MAXPATHL);
5779 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005780 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005782 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 /* Loop over all entries in {path}. */
5786 while (*path != NUL)
5787 {
5788 /* Copy one item of the path to buf[] and concatenate the file name. */
5789 copy_option_part(&path, buf, MAXPATHL, ",");
5790 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5791 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005792# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005793 /* Using the platform's path separator (\) makes vim incorrectly
5794 * treat it as an escape character, use '/' instead. */
5795 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5796 STRCAT(buf, "/");
5797# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005799# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005801 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5802 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005804 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005806 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 for (i = 0; i < num_p; ++i)
5809 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005810 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005811 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005812 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005815
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 FreeWild(num_p, p);
5817 }
5818 }
5819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820
5821 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822}
5823
5824#endif
5825
5826#if defined(FEAT_CMDHIST) || defined(PROTO)
5827
5828/*********************************
5829 * Command line history stuff *
5830 *********************************/
5831
5832/*
5833 * Translate a history character to the associated type number.
5834 */
5835 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005836hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 if (c == ':')
5839 return HIST_CMD;
5840 if (c == '=')
5841 return HIST_EXPR;
5842 if (c == '@')
5843 return HIST_INPUT;
5844 if (c == '>')
5845 return HIST_DEBUG;
5846 return HIST_SEARCH; /* must be '?' or '/' */
5847}
5848
5849/*
5850 * Table of history names.
5851 * These names are used in :history and various hist...() functions.
5852 * It is sufficient to give the significant prefix of a history name.
5853 */
5854
5855static char *(history_names[]) =
5856{
5857 "cmd",
5858 "search",
5859 "expr",
5860 "input",
5861 "debug",
5862 NULL
5863};
5864
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005865#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5866/*
5867 * Function given to ExpandGeneric() to obtain the possible first
5868 * arguments of the ":history command.
5869 */
5870 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005871get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005872{
5873 static char_u compl[2] = { NUL, NUL };
5874 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005875 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005876 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5877
5878 if (idx < short_names_count)
5879 {
5880 compl[0] = (char_u)short_names[idx];
5881 return compl;
5882 }
5883 if (idx < short_names_count + history_name_count)
5884 return (char_u *)history_names[idx - short_names_count];
5885 if (idx == short_names_count + history_name_count)
5886 return (char_u *)"all";
5887 return NULL;
5888}
5889#endif
5890
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891/*
5892 * init_history() - Initialize the command line history.
5893 * Also used to re-allocate the history when the size changes.
5894 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005895 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005896init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 int newlen; /* new length of history table */
5899 histentry_T *temp;
5900 int i;
5901 int j;
5902 int type;
5903
5904 /*
5905 * If size of history table changed, reallocate it
5906 */
5907 newlen = (int)p_hi;
5908 if (newlen != hislen) /* history length changed */
5909 {
5910 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5911 {
5912 if (newlen)
5913 {
5914 temp = (histentry_T *)lalloc(
5915 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5916 if (temp == NULL) /* out of memory! */
5917 {
5918 if (type == 0) /* first one: just keep the old length */
5919 {
5920 newlen = hislen;
5921 break;
5922 }
5923 /* Already changed one table, now we can only have zero
5924 * length for all tables. */
5925 newlen = 0;
5926 type = -1;
5927 continue;
5928 }
5929 }
5930 else
5931 temp = NULL;
5932 if (newlen == 0 || temp != NULL)
5933 {
5934 if (hisidx[type] < 0) /* there are no entries yet */
5935 {
5936 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005937 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 }
5939 else if (newlen > hislen) /* array becomes bigger */
5940 {
5941 for (i = 0; i <= hisidx[type]; ++i)
5942 temp[i] = history[type][i];
5943 j = i;
5944 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005945 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 for ( ; j < hislen; ++i, ++j)
5947 temp[i] = history[type][j];
5948 }
5949 else /* array becomes smaller or 0 */
5950 {
5951 j = hisidx[type];
5952 for (i = newlen - 1; ; --i)
5953 {
5954 if (i >= 0) /* copy newest entries */
5955 temp[i] = history[type][j];
5956 else /* remove older entries */
5957 vim_free(history[type][j].hisstr);
5958 if (--j < 0)
5959 j = hislen - 1;
5960 if (j == hisidx[type])
5961 break;
5962 }
5963 hisidx[type] = newlen - 1;
5964 }
5965 vim_free(history[type]);
5966 history[type] = temp;
5967 }
5968 }
5969 hislen = newlen;
5970 }
5971}
5972
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005973 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005975{
5976 hisptr->hisnum = 0;
5977 hisptr->viminfo = FALSE;
5978 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005979 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005980}
5981
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982/*
5983 * Check if command line 'str' is already in history.
5984 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5985 */
5986 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005987in_history(
5988 int type,
5989 char_u *str,
5990 int move_to_front, /* Move the entry to the front if it exists */
5991 int sep,
5992 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993{
5994 int i;
5995 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005996 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997
5998 if (hisidx[type] < 0)
5999 return FALSE;
6000 i = hisidx[type];
6001 do
6002 {
6003 if (history[type][i].hisstr == NULL)
6004 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006005
6006 /* For search history, check that the separator character matches as
6007 * well. */
6008 p = history[type][i].hisstr;
6009 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006010 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006011 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012 {
6013 if (!move_to_front)
6014 return TRUE;
6015 last_i = i;
6016 break;
6017 }
6018 if (--i < 0)
6019 i = hislen - 1;
6020 } while (i != hisidx[type]);
6021
6022 if (last_i >= 0)
6023 {
6024 str = history[type][i].hisstr;
6025 while (i != hisidx[type])
6026 {
6027 if (++i >= hislen)
6028 i = 0;
6029 history[type][last_i] = history[type][i];
6030 last_i = i;
6031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006033 history[type][i].viminfo = FALSE;
6034 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006035 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 return TRUE;
6037 }
6038 return FALSE;
6039}
6040
6041/*
6042 * Convert history name (from table above) to its HIST_ equivalent.
6043 * When "name" is empty, return "cmd" history.
6044 * Returns -1 for unknown history name.
6045 */
6046 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006047get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048{
6049 int i;
6050 int len = (int)STRLEN(name);
6051
6052 /* No argument: use current history. */
6053 if (len == 0)
6054 return hist_char2type(ccline.cmdfirstc);
6055
6056 for (i = 0; history_names[i] != NULL; ++i)
6057 if (STRNICMP(name, history_names[i], len) == 0)
6058 return i;
6059
6060 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6061 return hist_char2type(name[0]);
6062
6063 return -1;
6064}
6065
6066static int last_maptick = -1; /* last seen maptick */
6067
6068/*
6069 * Add the given string to the given history. If the string is already in the
6070 * history then it is moved to the front. "histype" may be one of he HIST_
6071 * values.
6072 */
6073 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006074add_to_history(
6075 int histype,
6076 char_u *new_entry,
6077 int in_map, /* consider maptick when inside a mapping */
6078 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079{
6080 histentry_T *hisptr;
6081 int len;
6082
6083 if (hislen == 0) /* no history */
6084 return;
6085
Bram Moolenaara939e432013-11-09 05:30:26 +01006086 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6087 return;
6088
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 /*
6090 * Searches inside the same mapping overwrite each other, so that only
6091 * the last line is kept. Be careful not to remove a line that was moved
6092 * down, only lines that were added.
6093 */
6094 if (histype == HIST_SEARCH && in_map)
6095 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006096 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 {
6098 /* Current line is from the same mapping, remove it */
6099 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6100 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006101 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 --hisnum[histype];
6103 if (--hisidx[HIST_SEARCH] < 0)
6104 hisidx[HIST_SEARCH] = hislen - 1;
6105 }
6106 last_maptick = -1;
6107 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006108 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 {
6110 if (++hisidx[histype] == hislen)
6111 hisidx[histype] = 0;
6112 hisptr = &history[histype][hisidx[histype]];
6113 vim_free(hisptr->hisstr);
6114
6115 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006116 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6118 if (hisptr->hisstr != NULL)
6119 hisptr->hisstr[len + 1] = sep;
6120
6121 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006122 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006123 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 if (histype == HIST_SEARCH && in_map)
6125 last_maptick = maptick;
6126 }
6127}
6128
6129#if defined(FEAT_EVAL) || defined(PROTO)
6130
6131/*
6132 * Get identifier of newest history entry.
6133 * "histype" may be one of the HIST_ values.
6134 */
6135 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006136get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137{
6138 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6139 || hisidx[histype] < 0)
6140 return -1;
6141
6142 return history[histype][hisidx[histype]].hisnum;
6143}
6144
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 * Calculate history index from a number:
6147 * num > 0: seen as identifying number of a history entry
6148 * num < 0: relative position in history wrt newest entry
6149 * "histype" may be one of the HIST_ values.
6150 */
6151 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 int i;
6155 histentry_T *hist;
6156 int wrapped = FALSE;
6157
6158 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6159 || (i = hisidx[histype]) < 0 || num == 0)
6160 return -1;
6161
6162 hist = history[histype];
6163 if (num > 0)
6164 {
6165 while (hist[i].hisnum > num)
6166 if (--i < 0)
6167 {
6168 if (wrapped)
6169 break;
6170 i += hislen;
6171 wrapped = TRUE;
6172 }
6173 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6174 return i;
6175 }
6176 else if (-num <= hislen)
6177 {
6178 i += num + 1;
6179 if (i < 0)
6180 i += hislen;
6181 if (hist[i].hisstr != NULL)
6182 return i;
6183 }
6184 return -1;
6185}
6186
6187/*
6188 * Get a history entry by its index.
6189 * "histype" may be one of the HIST_ values.
6190 */
6191 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 idx = calc_hist_idx(histype, idx);
6195 if (idx >= 0)
6196 return history[histype][idx].hisstr;
6197 else
6198 return (char_u *)"";
6199}
6200
6201/*
6202 * Clear all entries of a history.
6203 * "histype" may be one of the HIST_ values.
6204 */
6205 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006206clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207{
6208 int i;
6209 histentry_T *hisptr;
6210
6211 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6212 {
6213 hisptr = history[histype];
6214 for (i = hislen; i--;)
6215 {
6216 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006217 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006218 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220 hisidx[histype] = -1; /* mark history as cleared */
6221 hisnum[histype] = 0; /* reset identifier counter */
6222 return OK;
6223 }
6224 return FAIL;
6225}
6226
6227/*
6228 * Remove all entries matching {str} from a history.
6229 * "histype" may be one of the HIST_ values.
6230 */
6231 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006232del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 regmatch_T regmatch;
6235 histentry_T *hisptr;
6236 int idx;
6237 int i;
6238 int last;
6239 int found = FALSE;
6240
6241 regmatch.regprog = NULL;
6242 regmatch.rm_ic = FALSE; /* always match case */
6243 if (hislen != 0
6244 && histype >= 0
6245 && histype < HIST_COUNT
6246 && *str != NUL
6247 && (idx = hisidx[histype]) >= 0
6248 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6249 != NULL)
6250 {
6251 i = last = idx;
6252 do
6253 {
6254 hisptr = &history[histype][i];
6255 if (hisptr->hisstr == NULL)
6256 break;
6257 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6258 {
6259 found = TRUE;
6260 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006261 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262 }
6263 else
6264 {
6265 if (i != last)
6266 {
6267 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006268 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 }
6270 if (--last < 0)
6271 last += hislen;
6272 }
6273 if (--i < 0)
6274 i += hislen;
6275 } while (i != idx);
6276 if (history[histype][idx].hisstr == NULL)
6277 hisidx[histype] = -1;
6278 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006279 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 return found;
6281}
6282
6283/*
6284 * Remove an indexed entry from a history.
6285 * "histype" may be one of the HIST_ values.
6286 */
6287 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006288del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 int i, j;
6291
6292 i = calc_hist_idx(histype, idx);
6293 if (i < 0)
6294 return FALSE;
6295 idx = hisidx[histype];
6296 vim_free(history[histype][i].hisstr);
6297
6298 /* When deleting the last added search string in a mapping, reset
6299 * last_maptick, so that the last added search string isn't deleted again.
6300 */
6301 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6302 last_maptick = -1;
6303
6304 while (i != idx)
6305 {
6306 j = (i + 1) % hislen;
6307 history[histype][i] = history[histype][j];
6308 i = j;
6309 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006310 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 if (--i < 0)
6312 i += hislen;
6313 hisidx[histype] = i;
6314 return TRUE;
6315}
6316
6317#endif /* FEAT_EVAL */
6318
6319#if defined(FEAT_CRYPT) || defined(PROTO)
6320/*
6321 * Very specific function to remove the value in ":set key=val" from the
6322 * history.
6323 */
6324 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006325remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 char_u *p;
6328 int i;
6329
6330 i = hisidx[HIST_CMD];
6331 if (i < 0)
6332 return;
6333 p = history[HIST_CMD][i].hisstr;
6334 if (p != NULL)
6335 for ( ; *p; ++p)
6336 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6337 {
6338 p = vim_strchr(p + 3, '=');
6339 if (p == NULL)
6340 break;
6341 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006342 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 if (p[i] == '\\' && p[i + 1])
6344 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006345 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 --p;
6347 }
6348}
6349#endif
6350
6351#endif /* FEAT_CMDHIST */
6352
Bram Moolenaar064154c2016-03-19 22:50:43 +01006353#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006354/*
6355 * Get pointer to the command line info to use. cmdline_paste() may clear
6356 * ccline and put the previous value in prev_ccline.
6357 */
6358 static struct cmdline_info *
6359get_ccline_ptr(void)
6360{
6361 if ((State & CMDLINE) == 0)
6362 return NULL;
6363 if (ccline.cmdbuff != NULL)
6364 return &ccline;
6365 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6366 return &prev_ccline;
6367 return NULL;
6368}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006369#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006370
Bram Moolenaar064154c2016-03-19 22:50:43 +01006371#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006372/*
6373 * Get the current command line in allocated memory.
6374 * Only works when the command line is being edited.
6375 * Returns NULL when something is wrong.
6376 */
6377 char_u *
6378get_cmdline_str(void)
6379{
6380 struct cmdline_info *p = get_ccline_ptr();
6381
6382 if (p == NULL)
6383 return NULL;
6384 return vim_strnsave(p->cmdbuff, p->cmdlen);
6385}
6386
6387/*
6388 * Get the current command line position, counted in bytes.
6389 * Zero is the first position.
6390 * Only works when the command line is being edited.
6391 * Returns -1 when something is wrong.
6392 */
6393 int
6394get_cmdline_pos(void)
6395{
6396 struct cmdline_info *p = get_ccline_ptr();
6397
6398 if (p == NULL)
6399 return -1;
6400 return p->cmdpos;
6401}
6402
6403/*
6404 * Set the command line byte position to "pos". Zero is the first position.
6405 * Only works when the command line is being edited.
6406 * Returns 1 when failed, 0 when OK.
6407 */
6408 int
6409set_cmdline_pos(
6410 int pos)
6411{
6412 struct cmdline_info *p = get_ccline_ptr();
6413
6414 if (p == NULL)
6415 return 1;
6416
6417 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6418 * changed the command line. */
6419 if (pos < 0)
6420 new_cmdpos = 0;
6421 else
6422 new_cmdpos = pos;
6423 return 0;
6424}
6425#endif
6426
6427#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6428/*
6429 * Get the current command-line type.
6430 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6431 * Only works when the command line is being edited.
6432 * Returns NUL when something is wrong.
6433 */
6434 int
6435get_cmdline_type(void)
6436{
6437 struct cmdline_info *p = get_ccline_ptr();
6438
6439 if (p == NULL)
6440 return NUL;
6441 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006442 return
6443# ifdef FEAT_EVAL
6444 (p->input_fn) ? '@' :
6445# endif
6446 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006447 return p->cmdfirstc;
6448}
6449#endif
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6452/*
6453 * Get indices "num1,num2" that specify a range within a list (not a range of
6454 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6455 * Returns OK if parsed successfully, otherwise FAIL.
6456 */
6457 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006458get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459{
6460 int len;
6461 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006462 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 *str = skipwhite(*str);
6465 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6466 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006467 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 *str += len;
6469 *num1 = (int)num;
6470 first = TRUE;
6471 }
6472 *str = skipwhite(*str);
6473 if (**str == ',') /* parse "to" part of range */
6474 {
6475 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006476 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 if (len > 0)
6478 {
6479 *num2 = (int)num;
6480 *str = skipwhite(*str + len);
6481 }
6482 else if (!first) /* no number given at all */
6483 return FAIL;
6484 }
6485 else if (first) /* only one number given */
6486 *num2 = *num1;
6487 return OK;
6488}
6489#endif
6490
6491#if defined(FEAT_CMDHIST) || defined(PROTO)
6492/*
6493 * :history command - print a history
6494 */
6495 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006496ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497{
6498 histentry_T *hist;
6499 int histype1 = HIST_CMD;
6500 int histype2 = HIST_CMD;
6501 int hisidx1 = 1;
6502 int hisidx2 = -1;
6503 int idx;
6504 int i, j, k;
6505 char_u *end;
6506 char_u *arg = eap->arg;
6507
6508 if (hislen == 0)
6509 {
6510 MSG(_("'history' option is zero"));
6511 return;
6512 }
6513
6514 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6515 {
6516 end = arg;
6517 while (ASCII_ISALPHA(*end)
6518 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6519 end++;
6520 i = *end;
6521 *end = NUL;
6522 histype1 = get_histtype(arg);
6523 if (histype1 == -1)
6524 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006525 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
6527 histype1 = 0;
6528 histype2 = HIST_COUNT-1;
6529 }
6530 else
6531 {
6532 *end = i;
6533 EMSG(_(e_trailing));
6534 return;
6535 }
6536 }
6537 else
6538 histype2 = histype1;
6539 *end = i;
6540 }
6541 else
6542 end = arg;
6543 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6544 {
6545 EMSG(_(e_trailing));
6546 return;
6547 }
6548
6549 for (; !got_int && histype1 <= histype2; ++histype1)
6550 {
6551 STRCPY(IObuff, "\n # ");
6552 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6553 MSG_PUTS_TITLE(IObuff);
6554 idx = hisidx[histype1];
6555 hist = history[histype1];
6556 j = hisidx1;
6557 k = hisidx2;
6558 if (j < 0)
6559 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6560 if (k < 0)
6561 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6562 if (idx >= 0 && j <= k)
6563 for (i = idx + 1; !got_int; ++i)
6564 {
6565 if (i == hislen)
6566 i = 0;
6567 if (hist[i].hisstr != NULL
6568 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6569 {
6570 msg_putchar('\n');
6571 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6572 hist[i].hisnum);
6573 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6574 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006575 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 else
6577 STRCAT(IObuff, hist[i].hisstr);
6578 msg_outtrans(IObuff);
6579 out_flush();
6580 }
6581 if (i == idx)
6582 break;
6583 }
6584 }
6585}
6586#endif
6587
6588#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006589/*
6590 * Buffers for history read from a viminfo file. Only valid while reading.
6591 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006592static histentry_T *viminfo_history[HIST_COUNT] =
6593 {NULL, NULL, NULL, NULL, NULL};
6594static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6595static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006596static int viminfo_add_at_front = FALSE;
6597
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006598static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600/*
6601 * Translate a history type number to the associated character.
6602 */
6603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006604hist_type2char(
6605 int type,
6606 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607{
6608 if (type == HIST_CMD)
6609 return ':';
6610 if (type == HIST_SEARCH)
6611 {
6612 if (use_question)
6613 return '?';
6614 else
6615 return '/';
6616 }
6617 if (type == HIST_EXPR)
6618 return '=';
6619 return '@';
6620}
6621
6622/*
6623 * Prepare for reading the history from the viminfo file.
6624 * This allocates history arrays to store the read history lines.
6625 */
6626 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006627prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628{
6629 int i;
6630 int num;
6631 int type;
6632 int len;
6633
6634 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006635 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 if (asklen > hislen)
6637 asklen = hislen;
6638
6639 for (type = 0; type < HIST_COUNT; ++type)
6640 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006641 /* Count the number of empty spaces in the history list. Entries read
6642 * from viminfo previously are also considered empty. If there are
6643 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006645 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 num++;
6647 len = asklen;
6648 if (num > len)
6649 len = num;
6650 if (len <= 0)
6651 viminfo_history[type] = NULL;
6652 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006653 viminfo_history[type] = (histentry_T *)lalloc(
6654 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 if (viminfo_history[type] == NULL)
6656 len = 0;
6657 viminfo_hislen[type] = len;
6658 viminfo_hisidx[type] = 0;
6659 }
6660}
6661
6662/*
6663 * Accept a line from the viminfo, store it in the history array when it's
6664 * new.
6665 */
6666 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006667read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 int type;
6670 long_u len;
6671 char_u *val;
6672 char_u *p;
6673
6674 type = hist_char2type(virp->vir_line[0]);
6675 if (viminfo_hisidx[type] < viminfo_hislen[type])
6676 {
6677 val = viminfo_readstring(virp, 1, TRUE);
6678 if (val != NULL && *val != NUL)
6679 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006680 int sep = (*val == ' ' ? NUL : *val);
6681
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006683 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006684 {
6685 /* Need to re-allocate to append the separator byte. */
6686 len = STRLEN(val);
6687 p = lalloc(len + 2, TRUE);
6688 if (p != NULL)
6689 {
6690 if (type == HIST_SEARCH)
6691 {
6692 /* Search entry: Move the separator from the first
6693 * column to after the NUL. */
6694 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006695 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 }
6697 else
6698 {
6699 /* Not a search entry: No separator in the viminfo
6700 * file, add a NUL separator. */
6701 mch_memmove(p, val, (size_t)len + 1);
6702 p[len + 1] = NUL;
6703 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006704 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6705 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006706 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6707 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006708 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 }
6710 }
6711 }
6712 vim_free(val);
6713 }
6714 return viminfo_readline(virp);
6715}
6716
Bram Moolenaar07219f92013-04-14 23:19:36 +02006717/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006718 * Accept a new style history line from the viminfo, store it in the history
6719 * array when it's new.
6720 */
6721 void
6722handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006723 garray_T *values,
6724 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006725{
6726 int type;
6727 long_u len;
6728 char_u *val;
6729 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006730 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006731
6732 /* Check the format:
6733 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006734 if (values->ga_len < 4
6735 || vp[0].bv_type != BVAL_NR
6736 || vp[1].bv_type != BVAL_NR
6737 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6738 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006739 return;
6740
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006741 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006742 if (type >= HIST_COUNT)
6743 return;
6744 if (viminfo_hisidx[type] < viminfo_hislen[type])
6745 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006746 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006747 if (val != NULL && *val != NUL)
6748 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006749 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6750 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006751 int idx;
6752 int overwrite = FALSE;
6753
6754 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6755 {
6756 /* If lines were written by an older Vim we need to avoid
6757 * getting duplicates. See if the entry already exists. */
6758 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6759 {
6760 p = viminfo_history[type][idx].hisstr;
6761 if (STRCMP(val, p) == 0
6762 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6763 {
6764 overwrite = TRUE;
6765 break;
6766 }
6767 }
6768
6769 if (!overwrite)
6770 {
6771 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006772 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006773 p = lalloc(len + 2, TRUE);
6774 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006775 else
6776 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 if (p != NULL)
6778 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006779 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006780 if (!overwrite)
6781 {
6782 mch_memmove(p, val, (size_t)len + 1);
6783 /* Put the separator after the NUL. */
6784 p[len + 1] = sep;
6785 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006786 viminfo_history[type][idx].hisnum = 0;
6787 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006788 viminfo_hisidx[type]++;
6789 }
6790 }
6791 }
6792 }
6793 }
6794}
6795
6796/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006797 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006798 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006799 static void
6800concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
6802 int idx;
6803 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006804
6805 idx = hisidx[type] + viminfo_hisidx[type];
6806 if (idx >= hislen)
6807 idx -= hislen;
6808 else if (idx < 0)
6809 idx = hislen - 1;
6810 if (viminfo_add_at_front)
6811 hisidx[type] = idx;
6812 else
6813 {
6814 if (hisidx[type] == -1)
6815 hisidx[type] = hislen - 1;
6816 do
6817 {
6818 if (history[type][idx].hisstr != NULL
6819 || history[type][idx].viminfo)
6820 break;
6821 if (++idx == hislen)
6822 idx = 0;
6823 } while (idx != hisidx[type]);
6824 if (idx != hisidx[type] && --idx < 0)
6825 idx = hislen - 1;
6826 }
6827 for (i = 0; i < viminfo_hisidx[type]; i++)
6828 {
6829 vim_free(history[type][idx].hisstr);
6830 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6831 history[type][idx].viminfo = TRUE;
6832 history[type][idx].time_set = viminfo_history[type][i].time_set;
6833 if (--idx < 0)
6834 idx = hislen - 1;
6835 }
6836 idx += 1;
6837 idx %= hislen;
6838 for (i = 0; i < viminfo_hisidx[type]; i++)
6839 {
6840 history[type][idx++].hisnum = ++hisnum[type];
6841 idx %= hislen;
6842 }
6843}
6844
6845#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6846 static int
6847#ifdef __BORLANDC__
6848_RTLENTRYF
6849#endif
6850sort_hist(const void *s1, const void *s2)
6851{
6852 histentry_T *p1 = *(histentry_T **)s1;
6853 histentry_T *p2 = *(histentry_T **)s2;
6854
6855 if (p1->time_set < p2->time_set) return -1;
6856 if (p1->time_set > p2->time_set) return 1;
6857 return 0;
6858}
6859#endif
6860
6861/*
6862 * Merge history lines from viminfo and lines typed in this Vim based on the
6863 * timestamp;
6864 */
6865 static void
6866merge_history(int type)
6867{
6868 int max_len;
6869 histentry_T **tot_hist;
6870 histentry_T *new_hist;
6871 int i;
6872 int len;
6873
6874 /* Make one long list with all entries. */
6875 max_len = hislen + viminfo_hisidx[type];
6876 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6877 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6878 if (tot_hist == NULL || new_hist == NULL)
6879 {
6880 vim_free(tot_hist);
6881 vim_free(new_hist);
6882 return;
6883 }
6884 for (i = 0; i < viminfo_hisidx[type]; i++)
6885 tot_hist[i] = &viminfo_history[type][i];
6886 len = i;
6887 for (i = 0; i < hislen; i++)
6888 if (history[type][i].hisstr != NULL)
6889 tot_hist[len++] = &history[type][i];
6890
6891 /* Sort the list on timestamp. */
6892 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6893
6894 /* Keep the newest ones. */
6895 for (i = 0; i < hislen; i++)
6896 {
6897 if (i < len)
6898 {
6899 new_hist[i] = *tot_hist[i];
6900 tot_hist[i]->hisstr = NULL;
6901 if (new_hist[i].hisnum == 0)
6902 new_hist[i].hisnum = ++hisnum[type];
6903 }
6904 else
6905 clear_hist_entry(&new_hist[i]);
6906 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006907 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006908
6909 /* Free what is not kept. */
6910 for (i = 0; i < viminfo_hisidx[type]; i++)
6911 vim_free(viminfo_history[type][i].hisstr);
6912 for (i = 0; i < hislen; i++)
6913 vim_free(history[type][i].hisstr);
6914 vim_free(history[type]);
6915 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006916 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006917}
6918
6919/*
6920 * Finish reading history lines from viminfo. Not used when writing viminfo.
6921 */
6922 void
6923finish_viminfo_history(vir_T *virp)
6924{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006926 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927
6928 for (type = 0; type < HIST_COUNT; ++type)
6929 {
6930 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006931 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006932
6933 if (merge)
6934 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006936 concat_history(type);
6937
Bram Moolenaard23a8232018-02-10 18:45:26 +01006938 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006939 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 }
6941}
6942
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006943/*
6944 * Write history to viminfo file in "fp".
6945 * When "merge" is TRUE merge history lines with a previously read viminfo
6946 * file, data is in viminfo_history[].
6947 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006950write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951{
6952 int i;
6953 int type;
6954 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006955 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956
6957 init_history();
6958 if (hislen == 0)
6959 return;
6960 for (type = 0; type < HIST_COUNT; ++type)
6961 {
6962 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6963 if (num_saved == 0)
6964 continue;
6965 if (num_saved < 0) /* Use default */
6966 num_saved = hislen;
6967 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6968 type == HIST_CMD ? _("Command Line") :
6969 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006970 type == HIST_EXPR ? _("Expression") :
6971 type == HIST_INPUT ? _("Input Line") :
6972 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 if (num_saved > hislen)
6974 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006975
6976 /*
6977 * Merge typed and viminfo history:
6978 * round 1: history of typed commands.
6979 * round 2: history from recently read viminfo.
6980 */
6981 for (round = 1; round <= 2; ++round)
6982 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006983 if (round == 1)
6984 /* start at newest entry, somewhere in the list */
6985 i = hisidx[type];
6986 else if (viminfo_hisidx[type] > 0)
6987 /* start at newest entry, first in the list */
6988 i = 0;
6989 else
6990 /* empty list */
6991 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006992 if (i >= 0)
6993 while (num_saved > 0
6994 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006996 char_u *p;
6997 time_t timestamp;
6998 int c = NUL;
6999
7000 if (round == 1)
7001 {
7002 p = history[type][i].hisstr;
7003 timestamp = history[type][i].time_set;
7004 }
7005 else
7006 {
7007 p = viminfo_history[type] == NULL ? NULL
7008 : viminfo_history[type][i].hisstr;
7009 timestamp = viminfo_history[type] == NULL ? 0
7010 : viminfo_history[type][i].time_set;
7011 }
7012
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007013 if (p != NULL && (round == 2
7014 || !merge
7015 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007017 --num_saved;
7018 fputc(hist_type2char(type, TRUE), fp);
7019 /* For the search history: put the separator in the
7020 * second column; use a space if there isn't one. */
7021 if (type == HIST_SEARCH)
7022 {
7023 c = p[STRLEN(p) + 1];
7024 putc(c == NUL ? ' ' : c, fp);
7025 }
7026 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007027
7028 {
7029 char cbuf[NUMBUFLEN];
7030
7031 /* New style history with a bar line. Format:
7032 * |{bartype},{histtype},{timestamp},{separator},"text" */
7033 if (c == NUL)
7034 cbuf[0] = NUL;
7035 else
7036 sprintf(cbuf, "%d", c);
7037 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7038 type, (long)timestamp, cbuf);
7039 barline_writestring(fp, p, LSIZE - 20);
7040 putc('\n', fp);
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007043 if (round == 1)
7044 {
7045 /* Decrement index, loop around and stop when back at
7046 * the start. */
7047 if (--i < 0)
7048 i = hislen - 1;
7049 if (i == hisidx[type])
7050 break;
7051 }
7052 else
7053 {
7054 /* Increment index. Stop at the end in the while. */
7055 ++i;
7056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007058 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007059 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007060 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007061 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007062 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007063 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065}
7066#endif /* FEAT_VIMINFO */
7067
7068#if defined(FEAT_FKMAP) || defined(PROTO)
7069/*
7070 * Write a character at the current cursor+offset position.
7071 * It is directly written into the command buffer block.
7072 */
7073 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007074cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7077 {
7078 EMSG(_("E198: cmd_pchar beyond the command length"));
7079 return;
7080 }
7081 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7082 ccline.cmdbuff[ccline.cmdlen] = NUL;
7083}
7084
7085 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007086cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087{
7088 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7089 {
7090 /* EMSG(_("cmd_gchar beyond the command length")); */
7091 return NUL;
7092 }
7093 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7094}
7095#endif
7096
7097#if defined(FEAT_CMDWIN) || defined(PROTO)
7098/*
7099 * Open a window on the current command line and history. Allow editing in
7100 * the window. Returns when the window is closed.
7101 * Returns:
7102 * CR if the command is to be executed
7103 * Ctrl_C if it is to be abandoned
7104 * K_IGNORE if editing continues
7105 */
7106 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007107open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007110 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007112 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 win_T *wp;
7114 int i;
7115 linenr_T lnum;
7116 int histtype;
7117 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 int save_restart_edit = restart_edit;
7119 int save_State = State;
7120 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007121#ifdef FEAT_RIGHTLEFT
7122 int save_cmdmsg_rl = cmdmsg_rl;
7123#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007124#ifdef FEAT_FOLDING
7125 int save_KeyTyped;
7126#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127
7128 /* Can't do this recursively. Can't do it when typing a password. */
7129 if (cmdwin_type != 0
7130# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7131 || cmdline_star > 0
7132# endif
7133 )
7134 {
7135 beep_flush();
7136 return K_IGNORE;
7137 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007138 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139
7140 /* Save current window sizes. */
7141 win_size_save(&winsizes);
7142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007144 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007145
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007146 /* don't use a new tab page */
7147 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007148 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 /* Create a window for the command-line buffer. */
7151 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7152 {
7153 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007154 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 return K_IGNORE;
7156 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007157 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158
7159 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007160 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007161 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007164#ifdef FEAT_FOLDING
7165 curwin->w_p_fen = FALSE;
7166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007168 curwin->w_p_rl = cmdmsg_rl;
7169 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007171 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007174 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007175 /* But don't allow switching to another buffer. */
7176 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177
Bram Moolenaar46152342005-09-07 21:18:43 +00007178 /* Showing the prompt may have set need_wait_return, reset it. */
7179 need_wait_return = FALSE;
7180
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007181 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7183 {
7184 if (p_wc == TAB)
7185 {
7186 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7187 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7188 }
7189 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7190 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007191 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007193 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7194 * sets 'textwidth' to 78). */
7195 curbuf->b_p_tw = 0;
7196
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 /* Fill the buffer with the history. */
7198 init_history();
7199 if (hislen > 0)
7200 {
7201 i = hisidx[histtype];
7202 if (i >= 0)
7203 {
7204 lnum = 0;
7205 do
7206 {
7207 if (++i == hislen)
7208 i = 0;
7209 if (history[histtype][i].hisstr != NULL)
7210 ml_append(lnum++, history[histtype][i].hisstr,
7211 (colnr_T)0, FALSE);
7212 }
7213 while (i != hisidx[histtype]);
7214 }
7215 }
7216
7217 /* Replace the empty last line with the current command-line and put the
7218 * cursor there. */
7219 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7220 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7221 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007222 changed_line_abv_curs();
7223 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007224 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007227 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228
7229 /* No Ex mode here! */
7230 exmode_active = 0;
7231
7232 State = NORMAL;
7233# ifdef FEAT_MOUSE
7234 setmouse();
7235# endif
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007238 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007239 if (restart_edit != 0) /* autocmd with ":startinsert" */
7240 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
7242 i = RedrawingDisabled;
7243 RedrawingDisabled = 0;
7244
7245 /*
7246 * Call the main loop until <CR> or CTRL-C is typed.
7247 */
7248 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007249 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
7251 RedrawingDisabled = i;
7252
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007253# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007254 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007255# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007256
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007258 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007259
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007260# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007261 /* Restore KeyTyped in case it is modified by autocommands */
7262 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263# endif
7264
Bram Moolenaarccc18222007-05-10 18:25:20 +00007265 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007266 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 cmdwin_type = 0;
7268
7269 exmode_active = save_exmode;
7270
Bram Moolenaarf9821062008-06-20 16:31:07 +00007271 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007273 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 {
7275 cmdwin_result = Ctrl_C;
7276 EMSG(_("E199: Active window or buffer deleted"));
7277 }
7278 else
7279 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007280# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 /* autocmds may abort script processing */
7282 if (aborting() && cmdwin_result != K_IGNORE)
7283 cmdwin_result = Ctrl_C;
7284# endif
7285 /* Set the new command line from the cmdline buffer. */
7286 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007287 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007289 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7290
7291 if (histtype == HIST_CMD)
7292 {
7293 /* Execute the command directly. */
7294 ccline.cmdbuff = vim_strsave((char_u *)p);
7295 cmdwin_result = CAR;
7296 }
7297 else
7298 {
7299 /* First need to cancel what we were doing. */
7300 ccline.cmdbuff = NULL;
7301 stuffcharReadbuff(':');
7302 stuffReadbuff((char_u *)p);
7303 stuffcharReadbuff(CAR);
7304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007305 }
7306 else if (cmdwin_result == K_XF2) /* :qa typed */
7307 {
7308 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7309 cmdwin_result = CAR;
7310 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007311 else if (cmdwin_result == Ctrl_C)
7312 {
7313 /* :q or :close, don't execute any command
7314 * and don't modify the cmd window. */
7315 ccline.cmdbuff = NULL;
7316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 else
7318 ccline.cmdbuff = vim_strsave(ml_get_curline());
7319 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007320 {
7321 ccline.cmdbuff = vim_strsave((char_u *)"");
7322 ccline.cmdlen = 0;
7323 ccline.cmdbufflen = 1;
7324 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 else
7328 {
7329 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7330 ccline.cmdbufflen = ccline.cmdlen + 1;
7331 ccline.cmdpos = curwin->w_cursor.col;
7332 if (ccline.cmdpos > ccline.cmdlen)
7333 ccline.cmdpos = ccline.cmdlen;
7334 if (cmdwin_result == K_IGNORE)
7335 {
7336 set_cmdspos_cursor();
7337 redrawcmd();
7338 }
7339 }
7340
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007342 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007343# ifdef FEAT_CONCEAL
7344 /* Avoid command-line window first character being concealed. */
7345 curwin->w_p_cole = 0;
7346# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007348 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349 win_goto(old_curwin);
7350 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007351
7352 /* win_close() may have already wiped the buffer when 'bh' is
7353 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007354 if (bufref_valid(&bufref))
7355 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356
7357 /* Restore window sizes. */
7358 win_size_restore(&winsizes);
7359
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007360 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 }
7362
7363 ga_clear(&winsizes);
7364 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007365# ifdef FEAT_RIGHTLEFT
7366 cmdmsg_rl = save_cmdmsg_rl;
7367# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368
7369 State = save_State;
7370# ifdef FEAT_MOUSE
7371 setmouse();
7372# endif
7373
7374 return cmdwin_result;
7375}
7376#endif /* FEAT_CMDWIN */
7377
7378/*
7379 * Used for commands that either take a simple command string argument, or:
7380 * cmd << endmarker
7381 * {script}
7382 * endmarker
7383 * Returns a pointer to allocated memory with {script} or NULL.
7384 */
7385 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007386script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387{
7388 char_u *theline;
7389 char *end_pattern = NULL;
7390 char dot[] = ".";
7391 garray_T ga;
7392
7393 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7394 return NULL;
7395
7396 ga_init2(&ga, 1, 0x400);
7397
7398 if (cmd[2] != NUL)
7399 end_pattern = (char *)skipwhite(cmd + 2);
7400 else
7401 end_pattern = dot;
7402
7403 for (;;)
7404 {
7405 theline = eap->getline(
7406#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007407 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408#endif
7409 NUL, eap->cookie, 0);
7410
7411 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007412 {
7413 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416
7417 ga_concat(&ga, theline);
7418 ga_append(&ga, '\n');
7419 vim_free(theline);
7420 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007421 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007422
7423 return (char_u *)ga.ga_data;
7424}