blob: c2eb0a8dda18b07c00ccd221265d0f5cf02637f6 [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 ;
296 if (*p != NUL)
297 {
298 delim = *p++;
299 end = skip_regexp(p, delim, p_magic, NULL);
300 if (end > p)
301 {
302 char_u *dummy;
303 exarg_T ea;
304 pos_T save_cursor = curwin->w_cursor;
305
306 // found a non-empty pattern
307 *skiplen = (int)(p - ccline.cmdbuff);
308 *patlen = (int)(end - p);
309
310 // parse the address range
311 vim_memset(&ea, 0, sizeof(ea));
312 ea.line1 = 1;
313 ea.line2 = 1;
314 ea.cmd = ccline.cmdbuff;
315 ea.addr_type = ADDR_LINES;
316 parse_cmd_address(&ea, &dummy);
317 curwin->w_cursor = is_state->search_start;
318 if (ea.addr_count > 0)
319 {
320 search_first_line = ea.line1;
321 search_last_line = ea.line2;
322 }
323 else if (*cmd == 's')
324 {
325 // :s defaults to the current line
326 search_first_line = curwin->w_cursor.lnum;
327 search_last_line = curwin->w_cursor.lnum;
328 }
329
330 curwin->w_cursor = save_cursor;
331 return TRUE;
332 }
333 }
334 }
335 }
336 }
337
338 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200339}
340
341/*
342 * Do 'incsearch' highlighting if desired.
343 */
344 static void
345may_do_incsearch_highlighting(
346 int firstc,
347 long count,
348 incsearch_state_T *is_state)
349{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200351 int i;
352 pos_T end_pos;
353 struct cmdline_info save_ccline;
354#ifdef FEAT_RELTIME
355 proftime_T tm;
356#endif
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200357 int c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200358
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200359 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200360 return;
361
362 // If there is a character waiting, search and redraw later.
363 if (char_avail())
364 {
365 is_state->incsearch_postponed = TRUE;
366 return;
367 }
368 is_state->incsearch_postponed = FALSE;
369
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200370 if (search_first_line == 0)
371 // start at the original cursor position
372 curwin->w_cursor = is_state->search_start;
373 else
374 {
375 // start at the first line in the range
376 curwin->w_cursor.lnum = search_first_line;
377 curwin->w_cursor.col = 0;
378 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200379 save_last_search_pattern();
380
381 // If there is no command line, don't do anything.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200382 if (patlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200383 {
384 i = 0;
385 set_no_hlsearch(TRUE); // turn off previous highlight
386 redraw_all_later(SOME_VALID);
387 }
388 else
389 {
390 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
391
392 cursor_off(); // so the user knows we're busy
393 out_flush();
394 ++emsg_off; // so it doesn't beep if bad expr
395#ifdef FEAT_RELTIME
396 // Set the time limit to half a second.
397 profile_setlimit(500L, &tm);
398#endif
399 if (!p_hls)
400 search_flags += SEARCH_KEEP;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200401 c = ccline.cmdbuff[skiplen + patlen];
402 ccline.cmdbuff[skiplen + patlen] = NUL;
403 i = do_search(NULL, firstc == ':' ? '/' : firstc,
404 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200405#ifdef FEAT_RELTIME
406 &tm, NULL
407#else
408 NULL, NULL
409#endif
410 );
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200411 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200412 --emsg_off;
413
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200414 if (curwin->w_cursor.lnum < search_first_line
415 || curwin->w_cursor.lnum > search_last_line)
416 // match outside of address range
417 i = 0;
418
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200419 // if interrupted while searching, behave like it failed
420 if (got_int)
421 {
422 (void)vpeekc(); // remove <C-C> from input stream
423 got_int = FALSE; // don't abandon the command line
424 i = 0;
425 }
426 else if (char_avail())
427 // cancelled searching because a char was typed
428 is_state->incsearch_postponed = TRUE;
429 }
430 if (i != 0)
431 highlight_match = TRUE; // highlight position
432 else
433 highlight_match = FALSE; // remove highlight
434
435 // First restore the old curwin values, so the screen is positioned in the
436 // same way as the actual search command.
437 restore_viewstate(&is_state->old_viewstate);
438 changed_cline_bef_curs();
439 update_topline();
440
441 if (i != 0)
442 {
443 pos_T save_pos = curwin->w_cursor;
444
445 is_state->match_start = curwin->w_cursor;
446 set_search_match(&curwin->w_cursor);
447 validate_cursor();
448 end_pos = curwin->w_cursor;
449 is_state->match_end = end_pos;
450 curwin->w_cursor = save_pos;
451 }
452 else
453 end_pos = curwin->w_cursor; // shutup gcc 4
454
455 // Disable 'hlsearch' highlighting if the pattern matches everything.
456 // Avoids a flash when typing "foo\|".
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200457 c = ccline.cmdbuff[skiplen + patlen];
458 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200459 if (empty_pattern(ccline.cmdbuff))
460 set_no_hlsearch(TRUE);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462
463 validate_cursor();
464 // May redraw the status line to show the cursor position.
465 if (p_ru && curwin->w_status_height > 0)
466 curwin->w_redr_status = TRUE;
467
468 save_cmdline(&save_ccline);
469 update_screen(SOME_VALID);
470 restore_cmdline(&save_ccline);
471 restore_last_search_pattern();
472
473 // Leave it at the end to make CTRL-R CTRL-W work.
474 if (i != 0)
475 curwin->w_cursor = end_pos;
476
477 msg_starthere();
478 redrawcmdline();
479 is_state->did_incsearch = TRUE;
480}
481
482/*
483 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
484 * or previous match.
485 * Returns FAIL when jumping to cmdline_not_changed;
486 */
487 static int
488may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200489 int firstc,
490 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200491 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200492 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200493{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200494 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200495 pos_T t;
496 char_u *pat;
497 int search_flags = SEARCH_NOOF;
498 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200499 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200501 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200503 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200504 return FAIL;
505
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200507 pat = last_search_pattern();
508 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200509 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200510
511 save_last_search_pattern();
512 cursor_off();
513 out_flush();
514 if (c == Ctrl_G)
515 {
516 t = is_state->match_end;
517 if (LT_POS(is_state->match_start, is_state->match_end))
518 // Start searching at the end of the match not at the beginning of
519 // the next column.
520 (void)decl(&t);
521 search_flags += SEARCH_COL;
522 }
523 else
524 t = is_state->match_start;
525 if (!p_hls)
526 search_flags += SEARCH_KEEP;
527 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200528 save = pat[patlen];
529 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200530 i = searchit(curwin, curbuf, &t,
531 c == Ctrl_G ? FORWARD : BACKWARD,
532 pat, count, search_flags,
533 RE_SEARCH, 0, NULL, NULL);
534 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200535 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200536 if (i)
537 {
538 is_state->search_start = is_state->match_start;
539 is_state->match_end = t;
540 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200541 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200542 {
543 // Move just before the current match, so that when nv_search
544 // finishes the cursor will be put back on the match.
545 is_state->search_start = t;
546 (void)decl(&is_state->search_start);
547 }
548 else if (c == Ctrl_G && firstc == '?')
549 {
550 // Move just after the current match, so that when nv_search
551 // finishes the cursor will be put back on the match.
552 is_state->search_start = t;
553 (void)incl(&is_state->search_start);
554 }
555 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
556 {
557 // wrap around
558 is_state->search_start = t;
559 if (firstc == '?')
560 (void)incl(&is_state->search_start);
561 else
562 (void)decl(&is_state->search_start);
563 }
564
565 set_search_match(&is_state->match_end);
566 curwin->w_cursor = is_state->match_start;
567 changed_cline_bef_curs();
568 update_topline();
569 validate_cursor();
570 highlight_match = TRUE;
571 save_viewstate(&is_state->old_viewstate);
572 update_screen(NOT_VALID);
573 redrawcmdline();
574 }
575 else
576 vim_beep(BO_ERROR);
577 restore_last_search_pattern();
578 return FAIL;
579}
580
581/*
582 * When CTRL-L typed: add character from the match to the pattern.
583 * May set "*c" to the added character.
584 * Return OK when jumping to cmdline_not_changed.
585 */
586 static int
587may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
588{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200589 int skiplen, patlen;
590
591 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 return FAIL;
593
594 // Add a character from under the cursor for 'incsearch'.
595 if (is_state->did_incsearch)
596 {
597 curwin->w_cursor = is_state->match_end;
598 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
599 {
600 *c = gchar_cursor();
601
602 // If 'ignorecase' and 'smartcase' are set and the
603 // command line has no uppercase characters, convert
604 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200605 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200606 *c = MB_TOLOWER(*c);
607 if (*c != NUL)
608 {
609 if (*c == firstc || vim_strchr((char_u *)(
610 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
611 {
612 // put a backslash before special characters
613 stuffcharReadbuff(*c);
614 *c = '\\';
615 }
616 return FAIL;
617 }
618 }
619 }
620 return OK;
621}
622
623 static void
624finish_incsearch_highlighting(int gotesc, incsearch_state_T *is_state)
625{
626 if (is_state->did_incsearch)
627 {
628 if (gotesc)
629 curwin->w_cursor = is_state->save_cursor;
630 else
631 {
632 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
633 {
634 // put the '" mark at the original position
635 curwin->w_cursor = is_state->save_cursor;
636 setpcmark();
637 }
638 curwin->w_cursor = is_state->search_start;
639 }
640 restore_viewstate(&is_state->old_viewstate);
641 highlight_match = FALSE;
642 validate_cursor(); /* needed for TAB */
643 redraw_all_later(SOME_VALID);
644 }
645}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200646#endif
647
Bram Moolenaar66216052017-12-16 16:33:44 +0100648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649 * getcmdline() - accept a command line starting with firstc.
650 *
651 * firstc == ':' get ":" command line.
652 * firstc == '/' or '?' get search pattern
653 * firstc == '=' get expression
654 * firstc == '@' get text for input() function
655 * firstc == '>' get text for debug mode
656 * firstc == NUL get text for :insert command
657 * firstc == -1 like NUL, and break on CTRL-C
658 *
659 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
660 * command line.
661 *
662 * Careful: getcmdline() can be called recursively!
663 *
664 * Return pointer to allocated string if there is a commandline, NULL
665 * otherwise.
666 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100668getcmdline(
669 int firstc,
670 long count UNUSED, /* only used for incremental search */
671 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672{
673 int c;
674 int i;
675 int j;
676 int gotesc = FALSE; /* TRUE when <ESC> just typed */
677 int do_abbr; /* when TRUE check for abbr. */
678#ifdef FEAT_CMDHIST
679 char_u *lookfor = NULL; /* string to match */
680 int hiscnt; /* current history line in use */
681 int histype; /* history type to be used */
682#endif
683#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#endif
686 int did_wild_list = FALSE; /* did wild_list() recently */
687 int wim_index = 0; /* index in wim_flags[] */
688 int res;
689 int save_msg_scroll = msg_scroll;
690 int save_State = State; /* remember State when called */
691 int some_key_typed = FALSE; /* one of the keys was typed */
692#ifdef FEAT_MOUSE
693 /* mouse drag and release events are ignored, unless they are
694 * preceded with a mouse down event */
695 int ignore_drag_release = TRUE;
696#endif
697#ifdef FEAT_EVAL
698 int break_ctrl_c = FALSE;
699#endif
700 expand_T xpc;
701 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200702#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000703 /* Everything that may work recursively should save and restore the
704 * current command line in save_ccline. That includes update_screen(), a
705 * custom status line may invoke ":normal". */
706 struct cmdline_info save_ccline;
707#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200708 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710#ifdef FEAT_EVAL
711 if (firstc == -1)
712 {
713 firstc = NUL;
714 break_ctrl_c = TRUE;
715 }
716#endif
717#ifdef FEAT_RIGHTLEFT
718 /* start without Hebrew mapping for a command line */
719 if (firstc == ':' || firstc == '=' || firstc == '>')
720 cmd_hkmap = 0;
721#endif
722
723 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200726 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727#endif
728
729 /*
730 * set some variables for redrawcmd()
731 */
732 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000733 ccline.cmdindent = (firstc > 0 ? indent : 0);
734
735 /* alloc initial ccline.cmdbuff */
736 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 if (ccline.cmdbuff == NULL)
738 return NULL; /* out of memory */
739 ccline.cmdlen = ccline.cmdpos = 0;
740 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100741 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000743 /* autoindent for :insert and :append */
744 if (firstc <= 0)
745 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200746 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000747 ccline.cmdbuff[indent] = NUL;
748 ccline.cmdpos = indent;
749 ccline.cmdspos = indent;
750 ccline.cmdlen = indent;
751 }
752
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000754 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755
756#ifdef FEAT_RIGHTLEFT
757 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
758 && (firstc == '/' || firstc == '?'))
759 cmdmsg_rl = TRUE;
760 else
761 cmdmsg_rl = FALSE;
762#endif
763
764 redir_off = TRUE; /* don't redirect the typed command */
765 if (!cmd_silent)
766 {
767 i = msg_scrolled;
768 msg_scrolled = 0; /* avoid wait_return message */
769 gotocmdline(TRUE);
770 msg_scrolled += i;
771 redrawcmdprompt(); /* draw prompt or indent */
772 set_cmdspos();
773 }
774 xpc.xp_context = EXPAND_NOTHING;
775 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000776#ifndef BACKSLASH_IN_FILENAME
777 xpc.xp_shell = FALSE;
778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000780#if defined(FEAT_EVAL)
781 if (ccline.input_fn)
782 {
783 xpc.xp_context = ccline.xp_context;
784 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000785# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000786 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000787# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000788 }
789#endif
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 /*
792 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
793 * doing ":@0" when register 0 doesn't contain a CR.
794 */
795 msg_scroll = FALSE;
796
797 State = CMDLINE;
798
799 if (firstc == '/' || firstc == '?' || firstc == '@')
800 {
801 /* Use ":lmap" mappings for search pattern and input(). */
802 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
803 b_im_ptr = &curbuf->b_p_iminsert;
804 else
805 b_im_ptr = &curbuf->b_p_imsearch;
806 if (*b_im_ptr == B_IMODE_LMAP)
807 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100808#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809 im_set_active(*b_im_ptr == B_IMODE_IM);
810#endif
811 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100812#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 else if (p_imcmdline)
814 im_set_active(TRUE);
815#endif
816
817#ifdef FEAT_MOUSE
818 setmouse();
819#endif
820#ifdef CURSOR_SHAPE
821 ui_cursor_shape(); /* may show different cursor shape */
822#endif
823
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000824 /* When inside an autocommand for writing "exiting" may be set and
825 * terminal mode set to cooked. Need to set raw mode here then. */
826 settmode(TMODE_RAW);
827
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200828 /* Trigger CmdlineEnter autocommands. */
829 cmdline_type = firstc == NUL ? '-' : firstc;
830 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#ifdef FEAT_CMDHIST
833 init_history();
834 hiscnt = hislen; /* set hiscnt to impossible history value */
835 histype = hist_char2type(firstc);
836#endif
837
838#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000839 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#endif
841
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200842 /* If something above caused an error, reset the flags, we do want to type
843 * and execute commands. Display may be messed up a bit. */
844 if (did_emsg)
845 redrawcmd();
846 did_emsg = FALSE;
847 got_int = FALSE;
848
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 /*
850 * Collect the command string, handling editing keys.
851 */
852 for (;;)
853 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000854 redir_off = TRUE; /* Don't redirect the typed command.
855 Repeated, because a ":redir" inside
856 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857#ifdef USE_ON_FLY_SCROLL
858 dont_scroll = FALSE; /* allow scrolling here */
859#endif
860 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
861
Bram Moolenaar72532d32018-04-07 19:09:09 +0200862 did_emsg = FALSE; /* There can't really be a reason why an error
863 that occurs while typing a command should
864 cause the command not to be executed. */
865
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000867
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100868 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
869 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000870 do
871 {
872 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100873 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (KeyTyped)
876 {
877 some_key_typed = TRUE;
878#ifdef FEAT_RIGHTLEFT
879 if (cmd_hkmap)
880 c = hkmap(c);
881# ifdef FEAT_FKMAP
882 if (cmd_fkmap)
883 c = cmdl_fkmap(c);
884# endif
885 if (cmdmsg_rl && !KeyStuffed)
886 {
887 /* Invert horizontal movements and operations. Only when
888 * typed by the user directly, not when the result of a
889 * mapping. */
890 switch (c)
891 {
892 case K_RIGHT: c = K_LEFT; break;
893 case K_S_RIGHT: c = K_S_LEFT; break;
894 case K_C_RIGHT: c = K_C_LEFT; break;
895 case K_LEFT: c = K_RIGHT; break;
896 case K_S_LEFT: c = K_S_RIGHT; break;
897 case K_C_LEFT: c = K_C_RIGHT; break;
898 }
899 }
900#endif
901 }
902
903 /*
904 * Ignore got_int when CTRL-C was typed here.
905 * Don't ignore it in :global, we really need to break then, e.g., for
906 * ":g/pat/normal /pat" (without the <CR>).
907 * Don't ignore it for the input() function.
908 */
909 if ((c == Ctrl_C
910#ifdef UNIX
911 || c == intr_char
912#endif
913 )
914#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
915 && firstc != '@'
916#endif
917#ifdef FEAT_EVAL
918 && !break_ctrl_c
919#endif
920 && !global_busy)
921 got_int = FALSE;
922
923#ifdef FEAT_CMDHIST
924 /* free old command line when finished moving around in the history
925 * list */
926 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000927 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000928 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 && c != K_PAGEDOWN && c != K_PAGEUP
930 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000931 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100933 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#endif
935
936 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000937 * When there are matching completions to select <S-Tab> works like
938 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000940 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 c = Ctrl_P;
942
943#ifdef FEAT_WILDMENU
944 /* Special translations for 'wildmenu' */
945 if (did_wild_list && p_wmnu)
946 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000947 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000949 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 c = Ctrl_N;
951 }
952 /* Hitting CR after "emenu Name.": complete submenu */
953 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
954 && ccline.cmdpos > 1
955 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
956 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
957 && (c == '\n' || c == '\r' || c == K_KENTER))
958 c = K_DOWN;
959#endif
960
961 /* free expanded names when finished walking through matches */
962 if (xpc.xp_numfiles != -1
963 && !(c == p_wc && KeyTyped) && c != p_wcm
964 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
965 && c != Ctrl_L)
966 {
967 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
968 did_wild_list = FALSE;
969#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000970 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#endif
972 xpc.xp_context = EXPAND_NOTHING;
973 wim_index = 0;
974#ifdef FEAT_WILDMENU
975 if (p_wmnu && wild_menu_showing != 0)
976 {
977 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000978 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000979
980 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000981 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 if (wild_menu_showing == WM_SCROLLED)
984 {
985 /* Entered command line, move it up */
986 cmdline_row--;
987 redrawcmd();
988 }
989 else if (save_p_ls != -1)
990 {
991 /* restore 'laststatus' and 'winminheight' */
992 p_ls = save_p_ls;
993 p_wmh = save_p_wmh;
994 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000995 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000997 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 redrawcmd();
999 save_p_ls = -1;
1000 }
1001 else
1002 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 redraw_statuslines();
1005 }
1006 KeyTyped = skt;
1007 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001008 if (ccline.input_fn)
1009 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011#endif
1012 }
1013
1014#ifdef FEAT_WILDMENU
1015 /* Special translations for 'wildmenu' */
1016 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1017 {
1018 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001019 if (c == K_DOWN && ccline.cmdpos > 0
1020 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001022 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 {
1024 /* Hitting <Up>: Remove one submenu name in front of the
1025 * cursor */
1026 int found = FALSE;
1027
1028 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1029 i = 0;
1030 while (--j > 0)
1031 {
1032 /* check for start of menu name */
1033 if (ccline.cmdbuff[j] == ' '
1034 && ccline.cmdbuff[j - 1] != '\\')
1035 {
1036 i = j + 1;
1037 break;
1038 }
1039 /* check for start of submenu name */
1040 if (ccline.cmdbuff[j] == '.'
1041 && ccline.cmdbuff[j - 1] != '\\')
1042 {
1043 if (found)
1044 {
1045 i = j + 1;
1046 break;
1047 }
1048 else
1049 found = TRUE;
1050 }
1051 }
1052 if (i > 0)
1053 cmdline_del(i);
1054 c = p_wc;
1055 xpc.xp_context = EXPAND_NOTHING;
1056 }
1057 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001058 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001059 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001060 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 {
1062 char_u upseg[5];
1063
1064 upseg[0] = PATHSEP;
1065 upseg[1] = '.';
1066 upseg[2] = '.';
1067 upseg[3] = PATHSEP;
1068 upseg[4] = NUL;
1069
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001070 if (c == K_DOWN
1071 && ccline.cmdpos > 0
1072 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1073 && (ccline.cmdpos < 3
1074 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1076 {
1077 /* go down a directory */
1078 c = p_wc;
1079 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001080 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 {
1082 /* If in a direct ancestor, strip off one ../ to go down */
1083 int found = FALSE;
1084
1085 j = ccline.cmdpos;
1086 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1087 while (--j > i)
1088 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001089#ifdef FEAT_MBYTE
1090 if (has_mbyte)
1091 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 if (vim_ispathsep(ccline.cmdbuff[j]))
1094 {
1095 found = TRUE;
1096 break;
1097 }
1098 }
1099 if (found
1100 && ccline.cmdbuff[j - 1] == '.'
1101 && ccline.cmdbuff[j - 2] == '.'
1102 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1103 {
1104 cmdline_del(j - 2);
1105 c = p_wc;
1106 }
1107 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001108 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 {
1110 /* go up a directory */
1111 int found = FALSE;
1112
1113 j = ccline.cmdpos - 1;
1114 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1115 while (--j > i)
1116 {
1117#ifdef FEAT_MBYTE
1118 if (has_mbyte)
1119 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1120#endif
1121 if (vim_ispathsep(ccline.cmdbuff[j])
1122#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001123 && vim_strchr((char_u *)" *?[{`$%#",
1124 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#endif
1126 )
1127 {
1128 if (found)
1129 {
1130 i = j + 1;
1131 break;
1132 }
1133 else
1134 found = TRUE;
1135 }
1136 }
1137
1138 if (!found)
1139 j = i;
1140 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1141 j += 4;
1142 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1143 && j == i)
1144 j += 3;
1145 else
1146 j = 0;
1147 if (j > 0)
1148 {
1149 /* TODO this is only for DOS/UNIX systems - need to put in
1150 * machine-specific stuff here and in upseg init */
1151 cmdline_del(j);
1152 put_on_cmdline(upseg + 1, 3, FALSE);
1153 }
1154 else if (ccline.cmdpos > i)
1155 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001156
1157 /* Now complete in the new directory. Set KeyTyped in case the
1158 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001160 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 }
1162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163
1164#endif /* FEAT_WILDMENU */
1165
1166 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1167 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1168 if (c == Ctrl_BSL)
1169 {
1170 ++no_mapping;
1171 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001172 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 --no_mapping;
1174 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001175 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1176 * is in a mapping. */
1177 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1178 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 {
1180 vungetc(c);
1181 c = Ctrl_BSL;
1182 }
1183#ifdef FEAT_EVAL
1184 else if (c == 'e')
1185 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001186 char_u *p = NULL;
1187 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188
1189 /*
1190 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001191 * Need to save and restore the current command line, to be
1192 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001193 */
1194 if (ccline.cmdpos == ccline.cmdlen)
1195 new_cmdpos = 99999; /* keep it at the end */
1196 else
1197 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001198
1199 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001201 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 if (c == '=')
1203 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001204 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001205 * to avoid nasty things like going to another buffer when
1206 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001207 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001210 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001211 restore_cmdline(&save_ccline);
1212
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001213 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001215 len = (int)STRLEN(p);
1216 if (realloc_cmdbuff(len + 1) == OK)
1217 {
1218 ccline.cmdlen = len;
1219 STRCPY(ccline.cmdbuff, p);
1220 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001222 /* Restore the cursor or use the position set with
1223 * set_cmdline_pos(). */
1224 if (new_cmdpos > ccline.cmdlen)
1225 ccline.cmdpos = ccline.cmdlen;
1226 else
1227 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001229 KeyTyped = FALSE; /* Don't do p_wc completion. */
1230 redrawcmd();
1231 goto cmdline_changed;
1232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 }
1234 }
1235 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001236 got_int = FALSE; /* don't abandon the command line */
1237 did_emsg = FALSE;
1238 emsg_on_display = FALSE;
1239 redrawcmd();
1240 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
1242#endif
1243 else
1244 {
1245 if (c == Ctrl_G && p_im && restart_edit == 0)
1246 restart_edit = 'a';
1247 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1248 in history */
1249 goto returncmd; /* back to Normal mode */
1250 }
1251 }
1252
1253#ifdef FEAT_CMDWIN
1254 if (c == cedit_key || c == K_CMDWIN)
1255 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001256 if (ex_normal_busy == 0 && got_int == FALSE)
1257 {
1258 /*
1259 * Open a window to edit the command line (and history).
1260 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001261 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001262 some_key_typed = TRUE;
1263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265# ifdef FEAT_DIGRAPHS
1266 else
1267# endif
1268#endif
1269#ifdef FEAT_DIGRAPHS
1270 c = do_digraph(c);
1271#endif
1272
1273 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1274 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1275 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001276 /* In Ex mode a backslash escapes a newline. */
1277 if (exmode_active
1278 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001279 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001280 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001281 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001283 if (c == K_KENTER)
1284 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 else
1287 {
1288 gotesc = FALSE; /* Might have typed ESC previously, don't
1289 truncate the cmdline now. */
1290 if (ccheck_abbr(c + ABBR_OFF))
1291 goto cmdline_changed;
1292 if (!cmd_silent)
1293 {
1294 windgoto(msg_row, 0);
1295 out_flush();
1296 }
1297 break;
1298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 }
1300
1301 /*
1302 * Completion for 'wildchar' or 'wildcharm' key.
1303 * - hitting <ESC> twice means: abandon command line.
1304 * - wildcard expansion is only done when the 'wildchar' key is really
1305 * typed, not when it comes from a macro
1306 */
1307 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1308 {
1309 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1310 {
1311 /* if 'wildmode' contains "list" may still need to list */
1312 if (xpc.xp_numfiles > 1
1313 && !did_wild_list
1314 && (wim_flags[wim_index] & WIM_LIST))
1315 {
1316 (void)showmatches(&xpc, FALSE);
1317 redrawcmd();
1318 did_wild_list = TRUE;
1319 }
1320 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001321 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1322 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001324 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1325 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 else
1327 res = OK; /* don't insert 'wildchar' now */
1328 }
1329 else /* typed p_wc first time */
1330 {
1331 wim_index = 0;
1332 j = ccline.cmdpos;
1333 /* if 'wildmode' first contains "longest", get longest
1334 * common part */
1335 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001336 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1337 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001339 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1340 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342 /* if interrupted while completing, behave like it failed */
1343 if (got_int)
1344 {
1345 (void)vpeekc(); /* remove <C-C> from input stream */
1346 got_int = FALSE; /* don't abandon the command line */
1347 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1348#ifdef FEAT_WILDMENU
1349 xpc.xp_context = EXPAND_NOTHING;
1350#endif
1351 goto cmdline_changed;
1352 }
1353
1354 /* when more than one match, and 'wildmode' first contains
1355 * "list", or no change and 'wildmode' contains "longest,list",
1356 * list all matches */
1357 if (res == OK && xpc.xp_numfiles > 1)
1358 {
1359 /* a "longest" that didn't do anything is skipped (but not
1360 * "list:longest") */
1361 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1362 wim_index = 1;
1363 if ((wim_flags[wim_index] & WIM_LIST)
1364#ifdef FEAT_WILDMENU
1365 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1366#endif
1367 )
1368 {
1369 if (!(wim_flags[0] & WIM_LONGEST))
1370 {
1371#ifdef FEAT_WILDMENU
1372 int p_wmnu_save = p_wmnu;
1373 p_wmnu = 0;
1374#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001375 /* remove match */
1376 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377#ifdef FEAT_WILDMENU
1378 p_wmnu = p_wmnu_save;
1379#endif
1380 }
1381#ifdef FEAT_WILDMENU
1382 (void)showmatches(&xpc, p_wmnu
1383 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1384#else
1385 (void)showmatches(&xpc, FALSE);
1386#endif
1387 redrawcmd();
1388 did_wild_list = TRUE;
1389 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001390 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1391 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001393 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1394 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001397 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 }
1399#ifdef FEAT_WILDMENU
1400 else if (xpc.xp_numfiles == -1)
1401 xpc.xp_context = EXPAND_NOTHING;
1402#endif
1403 }
1404 if (wim_index < 3)
1405 ++wim_index;
1406 if (c == ESC)
1407 gotesc = TRUE;
1408 if (res == OK)
1409 goto cmdline_changed;
1410 }
1411
1412 gotesc = FALSE;
1413
1414 /* <S-Tab> goes to last match, in a clumsy way */
1415 if (c == K_S_TAB && KeyTyped)
1416 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001417 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1418 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1419 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 goto cmdline_changed;
1421 }
1422
1423 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1424 c = NL;
1425
1426 do_abbr = TRUE; /* default: check for abbreviation */
1427
1428 /*
1429 * Big switch for a typed command line character.
1430 */
1431 switch (c)
1432 {
1433 case K_BS:
1434 case Ctrl_H:
1435 case K_DEL:
1436 case K_KDEL:
1437 case Ctrl_W:
1438#ifdef FEAT_FKMAP
1439 if (cmd_fkmap && c == K_BS)
1440 c = K_DEL;
1441#endif
1442 if (c == K_KDEL)
1443 c = K_DEL;
1444
1445 /*
1446 * delete current character is the same as backspace on next
1447 * character, except at end of line
1448 */
1449 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1450 ++ccline.cmdpos;
1451#ifdef FEAT_MBYTE
1452 if (has_mbyte && c == K_DEL)
1453 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1454 ccline.cmdbuff + ccline.cmdpos);
1455#endif
1456 if (ccline.cmdpos > 0)
1457 {
1458 char_u *p;
1459
1460 j = ccline.cmdpos;
1461 p = ccline.cmdbuff + j;
1462#ifdef FEAT_MBYTE
1463 if (has_mbyte)
1464 {
1465 p = mb_prevptr(ccline.cmdbuff, p);
1466 if (c == Ctrl_W)
1467 {
1468 while (p > ccline.cmdbuff && vim_isspace(*p))
1469 p = mb_prevptr(ccline.cmdbuff, p);
1470 i = mb_get_class(p);
1471 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1472 p = mb_prevptr(ccline.cmdbuff, p);
1473 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001474 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 }
1476 }
1477 else
1478#endif
1479 if (c == Ctrl_W)
1480 {
1481 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1482 --p;
1483 i = vim_iswordc(p[-1]);
1484 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1485 && vim_iswordc(p[-1]) == i)
1486 --p;
1487 }
1488 else
1489 --p;
1490 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1491 ccline.cmdlen -= j - ccline.cmdpos;
1492 i = ccline.cmdpos;
1493 while (i < ccline.cmdlen)
1494 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1495
1496 /* Truncate at the end, required for multi-byte chars. */
1497 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001498#ifdef FEAT_SEARCH_EXTRA
1499 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001500 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001501 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001502 /* save view settings, so that the screen
1503 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001504 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001505 }
1506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 redrawcmd();
1508 }
1509 else if (ccline.cmdlen == 0 && c != Ctrl_W
1510 && ccline.cmdprompt == NULL && indent == 0)
1511 {
1512 /* In ex and debug mode it doesn't make sense to return. */
1513 if (exmode_active
1514#ifdef FEAT_EVAL
1515 || ccline.cmdfirstc == '>'
1516#endif
1517 )
1518 goto cmdline_not_changed;
1519
Bram Moolenaard23a8232018-02-10 18:45:26 +01001520 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 if (!cmd_silent)
1522 {
1523#ifdef FEAT_RIGHTLEFT
1524 if (cmdmsg_rl)
1525 msg_col = Columns;
1526 else
1527#endif
1528 msg_col = 0;
1529 msg_putchar(' '); /* delete ':' */
1530 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001531#ifdef FEAT_SEARCH_EXTRA
1532 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001533 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 redraw_cmdline = TRUE;
1536 goto returncmd; /* back to cmd mode */
1537 }
1538 goto cmdline_changed;
1539
1540 case K_INS:
1541 case K_KINS:
1542#ifdef FEAT_FKMAP
1543 /* if Farsi mode set, we are in reverse insert mode -
1544 Do not change the mode */
1545 if (cmd_fkmap)
1546 beep_flush();
1547 else
1548#endif
1549 ccline.overstrike = !ccline.overstrike;
1550#ifdef CURSOR_SHAPE
1551 ui_cursor_shape(); /* may show different cursor shape */
1552#endif
1553 goto cmdline_not_changed;
1554
1555 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001556 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 {
1558 /* ":lmap" mappings exists, toggle use of mappings. */
1559 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001560#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 im_set_active(FALSE); /* Disable input method */
1562#endif
1563 if (b_im_ptr != NULL)
1564 {
1565 if (State & LANGMAP)
1566 *b_im_ptr = B_IMODE_LMAP;
1567 else
1568 *b_im_ptr = B_IMODE_NONE;
1569 }
1570 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001571#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 else
1573 {
1574 /* There are no ":lmap" mappings, toggle IM. When
1575 * 'imdisable' is set don't try getting the status, it's
1576 * always off. */
1577 if ((p_imdisable && b_im_ptr != NULL)
1578 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1579 {
1580 im_set_active(FALSE); /* Disable input method */
1581 if (b_im_ptr != NULL)
1582 *b_im_ptr = B_IMODE_NONE;
1583 }
1584 else
1585 {
1586 im_set_active(TRUE); /* Enable input method */
1587 if (b_im_ptr != NULL)
1588 *b_im_ptr = B_IMODE_IM;
1589 }
1590 }
1591#endif
1592 if (b_im_ptr != NULL)
1593 {
1594 if (b_im_ptr == &curbuf->b_p_iminsert)
1595 set_iminsert_global();
1596 else
1597 set_imsearch_global();
1598 }
1599#ifdef CURSOR_SHAPE
1600 ui_cursor_shape(); /* may show different cursor shape */
1601#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001602#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 /* Show/unshow value of 'keymap' in status lines later. */
1604 status_redraw_curbuf();
1605#endif
1606 goto cmdline_not_changed;
1607
1608/* case '@': only in very old vi */
1609 case Ctrl_U:
1610 /* delete all characters left of the cursor */
1611 j = ccline.cmdpos;
1612 ccline.cmdlen -= j;
1613 i = ccline.cmdpos = 0;
1614 while (i < ccline.cmdlen)
1615 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1616 /* Truncate at the end, required for multi-byte chars. */
1617 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001618#ifdef FEAT_SEARCH_EXTRA
1619 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001620 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 redrawcmd();
1623 goto cmdline_changed;
1624
1625#ifdef FEAT_CLIPBOARD
1626 case Ctrl_Y:
1627 /* Copy the modeless selection, if there is one. */
1628 if (clip_star.state != SELECT_CLEARED)
1629 {
1630 if (clip_star.state == SELECT_DONE)
1631 clip_copy_modeless_selection(TRUE);
1632 goto cmdline_not_changed;
1633 }
1634 break;
1635#endif
1636
1637 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1638 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001639 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001640 * ":normal" runs out of characters. */
1641 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001642 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 goto cmdline_not_changed;
1644
1645 gotesc = TRUE; /* will free ccline.cmdbuff after
1646 putting it in history */
1647 goto returncmd; /* back to cmd mode */
1648
1649 case Ctrl_R: /* insert register */
1650#ifdef USE_ON_FLY_SCROLL
1651 dont_scroll = TRUE; /* disallow scrolling here */
1652#endif
1653 putcmdline('"', TRUE);
1654 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001655 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (i == Ctrl_O)
1657 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1658 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001659 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001660 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 --no_mapping;
1662#ifdef FEAT_EVAL
1663 /*
1664 * Insert the result of an expression.
1665 * Need to save the current command line, to be able to enter
1666 * a new one...
1667 */
1668 new_cmdpos = -1;
1669 if (c == '=')
1670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1672 {
1673 beep_flush();
1674 c = ESC;
1675 }
1676 else
1677 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001678 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001680 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 }
1682 }
1683#endif
1684 if (c != ESC) /* use ESC to cancel inserting register */
1685 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001686 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001687
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001688#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001689 /* When there was a serious error abort getting the
1690 * command line. */
1691 if (aborting())
1692 {
1693 gotesc = TRUE; /* will free ccline.cmdbuff after
1694 putting it in history */
1695 goto returncmd; /* back to cmd mode */
1696 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 KeyTyped = FALSE; /* Don't do p_wc completion. */
1699#ifdef FEAT_EVAL
1700 if (new_cmdpos >= 0)
1701 {
1702 /* set_cmdline_pos() was used */
1703 if (new_cmdpos > ccline.cmdlen)
1704 ccline.cmdpos = ccline.cmdlen;
1705 else
1706 ccline.cmdpos = new_cmdpos;
1707 }
1708#endif
1709 }
1710 redrawcmd();
1711 goto cmdline_changed;
1712
1713 case Ctrl_D:
1714 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1715 break; /* Use ^D as normal char instead */
1716
1717 redrawcmd();
1718 continue; /* don't do incremental search now */
1719
1720 case K_RIGHT:
1721 case K_S_RIGHT:
1722 case K_C_RIGHT:
1723 do
1724 {
1725 if (ccline.cmdpos >= ccline.cmdlen)
1726 break;
1727 i = cmdline_charsize(ccline.cmdpos);
1728 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1729 break;
1730 ccline.cmdspos += i;
1731#ifdef FEAT_MBYTE
1732 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001733 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 + ccline.cmdpos);
1735 else
1736#endif
1737 ++ccline.cmdpos;
1738 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001739 while ((c == K_S_RIGHT || c == K_C_RIGHT
1740 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1742#ifdef FEAT_MBYTE
1743 if (has_mbyte)
1744 set_cmdspos_cursor();
1745#endif
1746 goto cmdline_not_changed;
1747
1748 case K_LEFT:
1749 case K_S_LEFT:
1750 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001751 if (ccline.cmdpos == 0)
1752 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 do
1754 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 --ccline.cmdpos;
1756#ifdef FEAT_MBYTE
1757 if (has_mbyte) /* move to first byte of char */
1758 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1759 ccline.cmdbuff + ccline.cmdpos);
1760#endif
1761 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1762 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001763 while (ccline.cmdpos > 0
1764 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001765 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1767#ifdef FEAT_MBYTE
1768 if (has_mbyte)
1769 set_cmdspos_cursor();
1770#endif
1771 goto cmdline_not_changed;
1772
1773 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001774 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001775 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
Bram Moolenaar4770d092006-01-12 23:22:24 +00001777#ifdef FEAT_GUI_W32
1778 /* On Win32 ignore <M-F4>, we get it when closing the window was
1779 * cancelled. */
1780 case K_F4:
1781 if (mod_mask == MOD_MASK_ALT)
1782 {
1783 redrawcmd(); /* somehow the cmdline is cleared */
1784 goto cmdline_not_changed;
1785 }
1786 break;
1787#endif
1788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789#ifdef FEAT_MOUSE
1790 case K_MIDDLEDRAG:
1791 case K_MIDDLERELEASE:
1792 goto cmdline_not_changed; /* Ignore mouse */
1793
1794 case K_MIDDLEMOUSE:
1795# ifdef FEAT_GUI
1796 /* When GUI is active, also paste when 'mouse' is empty */
1797 if (!gui.in_use)
1798# endif
1799 if (!mouse_has(MOUSE_COMMAND))
1800 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001801# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001803 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001805# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001806 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 redrawcmd();
1808 goto cmdline_changed;
1809
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001810# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001812 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 redrawcmd();
1814 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816
1817 case K_LEFTDRAG:
1818 case K_LEFTRELEASE:
1819 case K_RIGHTDRAG:
1820 case K_RIGHTRELEASE:
1821 /* Ignore drag and release events when the button-down wasn't
1822 * seen before. */
1823 if (ignore_drag_release)
1824 goto cmdline_not_changed;
1825 /* FALLTHROUGH */
1826 case K_LEFTMOUSE:
1827 case K_RIGHTMOUSE:
1828 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1829 ignore_drag_release = TRUE;
1830 else
1831 ignore_drag_release = FALSE;
1832# ifdef FEAT_GUI
1833 /* When GUI is active, also move when 'mouse' is empty */
1834 if (!gui.in_use)
1835# endif
1836 if (!mouse_has(MOUSE_COMMAND))
1837 goto cmdline_not_changed; /* Ignore mouse */
1838# ifdef FEAT_CLIPBOARD
1839 if (mouse_row < cmdline_row && clip_star.available)
1840 {
1841 int button, is_click, is_drag;
1842
1843 /*
1844 * Handle modeless selection.
1845 */
1846 button = get_mouse_button(KEY2TERMCAP1(c),
1847 &is_click, &is_drag);
1848 if (mouse_model_popup() && button == MOUSE_LEFT
1849 && (mod_mask & MOD_MASK_SHIFT))
1850 {
1851 /* Translate shift-left to right button. */
1852 button = MOUSE_RIGHT;
1853 mod_mask &= ~MOD_MASK_SHIFT;
1854 }
1855 clip_modeless(button, is_click, is_drag);
1856 goto cmdline_not_changed;
1857 }
1858# endif
1859
1860 set_cmdspos();
1861 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1862 ++ccline.cmdpos)
1863 {
1864 i = cmdline_charsize(ccline.cmdpos);
1865 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1866 && mouse_col < ccline.cmdspos % Columns + i)
1867 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001868# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 if (has_mbyte)
1870 {
1871 /* Count ">" for double-wide char that doesn't fit. */
1872 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001873 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 + ccline.cmdpos) - 1;
1875 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001876# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 ccline.cmdspos += i;
1878 }
1879 goto cmdline_not_changed;
1880
1881 /* Mouse scroll wheel: ignored here */
1882 case K_MOUSEDOWN:
1883 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001884 case K_MOUSELEFT:
1885 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 /* Alternate buttons ignored here */
1887 case K_X1MOUSE:
1888 case K_X1DRAG:
1889 case K_X1RELEASE:
1890 case K_X2MOUSE:
1891 case K_X2DRAG:
1892 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001893 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 goto cmdline_not_changed;
1895
1896#endif /* FEAT_MOUSE */
1897
1898#ifdef FEAT_GUI
1899 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1900 case K_LEFTRELEASE_NM:
1901 goto cmdline_not_changed;
1902
1903 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001904 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 {
1906 gui_do_scroll();
1907 redrawcmd();
1908 }
1909 goto cmdline_not_changed;
1910
1911 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001912 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001914 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 redrawcmd();
1916 }
1917 goto cmdline_not_changed;
1918#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001919#ifdef FEAT_GUI_TABLINE
1920 case K_TABLINE:
1921 case K_TABMENU:
1922 /* Don't want to change any tabs here. Make sure the same tab
1923 * is still selected. */
1924 if (gui_use_tabline())
1925 gui_mch_set_curtab(tabpage_index(curtab));
1926 goto cmdline_not_changed;
1927#endif
1928
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 case K_SELECT: /* end of Select mode mapping - ignore */
1930 goto cmdline_not_changed;
1931
1932 case Ctrl_B: /* begin of command line */
1933 case K_HOME:
1934 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 case K_S_HOME:
1936 case K_C_HOME:
1937 ccline.cmdpos = 0;
1938 set_cmdspos();
1939 goto cmdline_not_changed;
1940
1941 case Ctrl_E: /* end of command line */
1942 case K_END:
1943 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 case K_S_END:
1945 case K_C_END:
1946 ccline.cmdpos = ccline.cmdlen;
1947 set_cmdspos_cursor();
1948 goto cmdline_not_changed;
1949
1950 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001951 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 break;
1953 goto cmdline_changed;
1954
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001955 case Ctrl_L:
1956#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001957 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001958 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001959#endif
1960
1961 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001962 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 break;
1964 goto cmdline_changed;
1965
1966 case Ctrl_N: /* next match */
1967 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001968 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001970 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1971 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001973 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001976 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 case K_UP:
1978 case K_DOWN:
1979 case K_S_UP:
1980 case K_S_DOWN:
1981 case K_PAGEUP:
1982 case K_KPAGEUP:
1983 case K_PAGEDOWN:
1984 case K_KPAGEDOWN:
1985 if (hislen == 0 || firstc == NUL) /* no history */
1986 goto cmdline_not_changed;
1987
1988 i = hiscnt;
1989
1990 /* save current command string so it can be restored later */
1991 if (lookfor == NULL)
1992 {
1993 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1994 goto cmdline_not_changed;
1995 lookfor[ccline.cmdpos] = NUL;
1996 }
1997
1998 j = (int)STRLEN(lookfor);
1999 for (;;)
2000 {
2001 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002002 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002003 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
2005 if (hiscnt == hislen) /* first time */
2006 hiscnt = hisidx[histype];
2007 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2008 hiscnt = hislen - 1;
2009 else if (hiscnt != hisidx[histype] + 1)
2010 --hiscnt;
2011 else /* at top of list */
2012 {
2013 hiscnt = i;
2014 break;
2015 }
2016 }
2017 else /* one step forwards */
2018 {
2019 /* on last entry, clear the line */
2020 if (hiscnt == hisidx[histype])
2021 {
2022 hiscnt = hislen;
2023 break;
2024 }
2025
2026 /* not on a history line, nothing to do */
2027 if (hiscnt == hislen)
2028 break;
2029 if (hiscnt == hislen - 1) /* wrap around */
2030 hiscnt = 0;
2031 else
2032 ++hiscnt;
2033 }
2034 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2035 {
2036 hiscnt = i;
2037 break;
2038 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002039 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002040 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 || STRNCMP(history[histype][hiscnt].hisstr,
2042 lookfor, (size_t)j) == 0)
2043 break;
2044 }
2045
2046 if (hiscnt != i) /* jumped to other entry */
2047 {
2048 char_u *p;
2049 int len;
2050 int old_firstc;
2051
2052 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002053 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 if (hiscnt == hislen)
2055 p = lookfor; /* back to the old one */
2056 else
2057 p = history[histype][hiscnt].hisstr;
2058
2059 if (histype == HIST_SEARCH
2060 && p != lookfor
2061 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2062 {
2063 /* Correct for the separator character used when
2064 * adding the history entry vs the one used now.
2065 * First loop: count length.
2066 * Second loop: copy the characters. */
2067 for (i = 0; i <= 1; ++i)
2068 {
2069 len = 0;
2070 for (j = 0; p[j] != NUL; ++j)
2071 {
2072 /* Replace old sep with new sep, unless it is
2073 * escaped. */
2074 if (p[j] == old_firstc
2075 && (j == 0 || p[j - 1] != '\\'))
2076 {
2077 if (i > 0)
2078 ccline.cmdbuff[len] = firstc;
2079 }
2080 else
2081 {
2082 /* Escape new sep, unless it is already
2083 * escaped. */
2084 if (p[j] == firstc
2085 && (j == 0 || p[j - 1] != '\\'))
2086 {
2087 if (i > 0)
2088 ccline.cmdbuff[len] = '\\';
2089 ++len;
2090 }
2091 if (i > 0)
2092 ccline.cmdbuff[len] = p[j];
2093 }
2094 ++len;
2095 }
2096 if (i == 0)
2097 {
2098 alloc_cmdbuff(len);
2099 if (ccline.cmdbuff == NULL)
2100 goto returncmd;
2101 }
2102 }
2103 ccline.cmdbuff[len] = NUL;
2104 }
2105 else
2106 {
2107 alloc_cmdbuff((int)STRLEN(p));
2108 if (ccline.cmdbuff == NULL)
2109 goto returncmd;
2110 STRCPY(ccline.cmdbuff, p);
2111 }
2112
2113 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2114 redrawcmd();
2115 goto cmdline_changed;
2116 }
2117 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002118#endif
2119 goto cmdline_not_changed;
2120
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002121#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002122 case Ctrl_G: /* next match */
2123 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002124 if (may_adjust_incsearch_highlighting(
2125 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002126 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002127 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128#endif
2129
2130 case Ctrl_V:
2131 case Ctrl_Q:
2132#ifdef FEAT_MOUSE
2133 ignore_drag_release = TRUE;
2134#endif
2135 putcmdline('^', TRUE);
2136 c = get_literal(); /* get next (two) character(s) */
2137 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002138 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139#ifdef FEAT_MBYTE
2140 /* may need to remove ^ when composing char was typed */
2141 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2142 {
2143 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2144 msg_putchar(' ');
2145 cursorcmd();
2146 }
2147#endif
2148 break;
2149
2150#ifdef FEAT_DIGRAPHS
2151 case Ctrl_K:
2152#ifdef FEAT_MOUSE
2153 ignore_drag_release = TRUE;
2154#endif
2155 putcmdline('?', TRUE);
2156#ifdef USE_ON_FLY_SCROLL
2157 dont_scroll = TRUE; /* disallow scrolling here */
2158#endif
2159 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002160 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 if (c != NUL)
2162 break;
2163
2164 redrawcmd();
2165 goto cmdline_not_changed;
2166#endif /* FEAT_DIGRAPHS */
2167
2168#ifdef FEAT_RIGHTLEFT
2169 case Ctrl__: /* CTRL-_: switch language mode */
2170 if (!p_ari)
2171 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002172# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 if (p_altkeymap)
2174 {
2175 cmd_fkmap = !cmd_fkmap;
2176 if (cmd_fkmap) /* in Farsi always in Insert mode */
2177 ccline.overstrike = FALSE;
2178 }
2179 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002180# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 cmd_hkmap = !cmd_hkmap;
2182 goto cmdline_not_changed;
2183#endif
2184
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002185 case K_PS:
2186 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2187 goto cmdline_changed;
2188
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 default:
2190#ifdef UNIX
2191 if (c == intr_char)
2192 {
2193 gotesc = TRUE; /* will free ccline.cmdbuff after
2194 putting it in history */
2195 goto returncmd; /* back to Normal mode */
2196 }
2197#endif
2198 /*
2199 * Normal character with no special meaning. Just set mod_mask
2200 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2201 * the string <S-Space>. This should only happen after ^V.
2202 */
2203 if (!IS_SPECIAL(c))
2204 mod_mask = 0x0;
2205 break;
2206 }
2207 /*
2208 * End of switch on command line character.
2209 * We come here if we have a normal character.
2210 */
2211
Bram Moolenaarede3e632013-06-23 16:16:19 +02002212 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213#ifdef FEAT_MBYTE
2214 /* Add ABBR_OFF for characters above 0x100, this is
2215 * what check_abbr() expects. */
2216 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2217#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002218 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 goto cmdline_changed;
2220
2221 /*
2222 * put the character in the command line
2223 */
2224 if (IS_SPECIAL(c) || mod_mask != 0)
2225 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2226 else
2227 {
2228#ifdef FEAT_MBYTE
2229 if (has_mbyte)
2230 {
2231 j = (*mb_char2bytes)(c, IObuff);
2232 IObuff[j] = NUL; /* exclude composing chars */
2233 put_on_cmdline(IObuff, j, TRUE);
2234 }
2235 else
2236#endif
2237 {
2238 IObuff[0] = c;
2239 put_on_cmdline(IObuff, 1, TRUE);
2240 }
2241 }
2242 goto cmdline_changed;
2243
2244/*
2245 * This part implements incremental searches for "/" and "?"
2246 * Jump to cmdline_not_changed when a character has been read but the command
2247 * line did not change. Then we only search and redraw if something changed in
2248 * the past.
2249 * Jump to cmdline_changed when the command line did change.
2250 * (Sorry for the goto's, I know it is ugly).
2251 */
2252cmdline_not_changed:
2253#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002254 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 continue;
2256#endif
2257
2258cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002259 /* Trigger CmdlineChanged autocommands. */
2260 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002261
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002263 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264#endif
2265
2266#ifdef FEAT_RIGHTLEFT
2267 if (cmdmsg_rl
2268# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002269 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270# endif
2271 )
2272 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002273 * right-left typing. Not efficient, but it works.
2274 * Do it only when there are no characters left to read
2275 * to avoid useless intermediate redraws. */
2276 if (vpeekc() == NUL)
2277 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279 }
2280
2281returncmd:
2282
2283#ifdef FEAT_RIGHTLEFT
2284 cmdmsg_rl = FALSE;
2285#endif
2286
2287#ifdef FEAT_FKMAP
2288 cmd_fkmap = 0;
2289#endif
2290
2291 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002292 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
2294#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002295 finish_incsearch_highlighting(gotesc, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296#endif
2297
2298 if (ccline.cmdbuff != NULL)
2299 {
2300 /*
2301 * Put line in history buffer (":" and "=" only when it was typed).
2302 */
2303#ifdef FEAT_CMDHIST
2304 if (ccline.cmdlen && firstc != NUL
2305 && (some_key_typed || histype == HIST_SEARCH))
2306 {
2307 add_to_history(histype, ccline.cmdbuff, TRUE,
2308 histype == HIST_SEARCH ? firstc : NUL);
2309 if (firstc == ':')
2310 {
2311 vim_free(new_last_cmdline);
2312 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2313 }
2314 }
2315#endif
2316
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002317 if (gotesc)
2318 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320
2321 /*
2322 * If the screen was shifted up, redraw the whole screen (later).
2323 * If the line is too long, clear it, so ruler and shown command do
2324 * not get printed in the middle of it.
2325 */
2326 msg_check();
2327 msg_scroll = save_msg_scroll;
2328 redir_off = FALSE;
2329
2330 /* When the command line was typed, no need for a wait-return prompt. */
2331 if (some_key_typed)
2332 need_wait_return = FALSE;
2333
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002334 /* Trigger CmdlineLeave autocommands. */
2335 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002336
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002338#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2340 im_save_status(b_im_ptr);
2341 im_set_active(FALSE);
2342#endif
2343#ifdef FEAT_MOUSE
2344 setmouse();
2345#endif
2346#ifdef CURSOR_SHAPE
2347 ui_cursor_shape(); /* may show different cursor shape */
2348#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002349 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002351 {
2352 char_u *p = ccline.cmdbuff;
2353
2354 /* Make ccline empty, getcmdline() may try to use it. */
2355 ccline.cmdbuff = NULL;
2356 return p;
2357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358}
2359
2360#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2361/*
2362 * Get a command line with a prompt.
2363 * This is prepared to be called recursively from getcmdline() (e.g. by
2364 * f_input() when evaluating an expression from CTRL-R =).
2365 * Returns the command line in allocated memory, or NULL.
2366 */
2367 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002368getcmdline_prompt(
2369 int firstc,
2370 char_u *prompt, /* command line prompt */
2371 int attr, /* attributes for prompt */
2372 int xp_context, /* type of expansion */
2373 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374{
2375 char_u *s;
2376 struct cmdline_info save_ccline;
2377 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002378 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002380 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 ccline.cmdprompt = prompt;
2382 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002383# ifdef FEAT_EVAL
2384 ccline.xp_context = xp_context;
2385 ccline.xp_arg = xp_arg;
2386 ccline.input_fn = (firstc == '@');
2387# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002388 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002390 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002391 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002392 /* Restore msg_col, the prompt from input() may have changed it.
2393 * But only if called recursively and the commandline is therefore being
2394 * restored to an old one; if not, the input() prompt stays on the screen,
2395 * so we need its modified msg_col left intact. */
2396 if (ccline.cmdbuff != NULL)
2397 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 return s;
2400}
2401#endif
2402
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002403/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002404 * Return TRUE when the text must not be changed and we can't switch to
2405 * another window or buffer. Used when editing the command line, evaluating
2406 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002407 */
2408 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002409text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002410{
2411#ifdef FEAT_CMDWIN
2412 if (cmdwin_type != 0)
2413 return TRUE;
2414#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002415 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002416}
2417
2418/*
2419 * Give an error message for a command that isn't allowed while the cmdline
2420 * window is open or editing the cmdline in another way.
2421 */
2422 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002423text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002424{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002425 EMSG(_(get_text_locked_msg()));
2426}
2427
2428 char_u *
2429get_text_locked_msg(void)
2430{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002431#ifdef FEAT_CMDWIN
2432 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002433 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002434#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002435 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002436}
2437
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002438/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002439 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2440 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002441 */
2442 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002443curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002444{
2445 if (curbuf_lock > 0)
2446 {
2447 EMSG(_("E788: Not allowed to edit another buffer now"));
2448 return TRUE;
2449 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002450 return allbuf_locked();
2451}
2452
2453/*
2454 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2455 * message.
2456 */
2457 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002458allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002459{
2460 if (allbuf_lock > 0)
2461 {
2462 EMSG(_("E811: Not allowed to change buffer information now"));
2463 return TRUE;
2464 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002465 return FALSE;
2466}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002469cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470{
2471#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2472 if (cmdline_star > 0) /* showing '*', always 1 position */
2473 return 1;
2474#endif
2475 return ptr2cells(ccline.cmdbuff + idx);
2476}
2477
2478/*
2479 * Compute the offset of the cursor on the command line for the prompt and
2480 * indent.
2481 */
2482 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002483set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002485 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 ccline.cmdspos = 1 + ccline.cmdindent;
2487 else
2488 ccline.cmdspos = 0 + ccline.cmdindent;
2489}
2490
2491/*
2492 * Compute the screen position for the cursor on the command line.
2493 */
2494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002495set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496{
2497 int i, m, c;
2498
2499 set_cmdspos();
2500 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002501 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002503 if (m < 0) /* overflow, Columns or Rows at weird value */
2504 m = MAXCOL;
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 else
2507 m = MAXCOL;
2508 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2509 {
2510 c = cmdline_charsize(i);
2511#ifdef FEAT_MBYTE
2512 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2513 if (has_mbyte)
2514 correct_cmdspos(i, c);
2515#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002516 /* If the cmdline doesn't fit, show cursor on last visible char.
2517 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 if ((ccline.cmdspos += c) >= m)
2519 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 ccline.cmdspos -= c;
2521 break;
2522 }
2523#ifdef FEAT_MBYTE
2524 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002525 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526#endif
2527 }
2528}
2529
2530#ifdef FEAT_MBYTE
2531/*
2532 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2533 * character that doesn't fit, so that a ">" must be displayed.
2534 */
2535 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002536correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002538 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2540 && ccline.cmdspos % Columns + cells > Columns)
2541 ccline.cmdspos++;
2542}
2543#endif
2544
2545/*
2546 * Get an Ex command line for the ":" command.
2547 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002549getexline(
2550 int c, /* normally ':', NUL for ":append" */
2551 void *cookie UNUSED,
2552 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 /* When executing a register, remove ':' that's in front of each line. */
2555 if (exec_from_reg && vpeekc() == ':')
2556 (void)vgetc();
2557 return getcmdline(c, 1L, indent);
2558}
2559
2560/*
2561 * Get an Ex command line for Ex mode.
2562 * In Ex mode we only use the OS supplied line editing features and no
2563 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002564 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002567getexmodeline(
2568 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002569 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002570 void *cookie UNUSED,
2571 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573 garray_T line_ga;
2574 char_u *pend;
2575 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002576 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 int escaped = FALSE; /* CTRL-V typed */
2578 int vcol = 0;
2579 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002580 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002581 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 /* Switch cursor on now. This avoids that it happens after the "\n", which
2584 * confuses the system function that computes tabstops. */
2585 cursor_on();
2586
2587 /* always start in column 0; write a newline if necessary */
2588 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002589 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002591 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002593 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002594 if (p_prompt)
2595 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 while (indent-- > 0)
2597 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 }
2600
2601 ga_init2(&line_ga, 1, 30);
2602
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002603 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002604 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002605 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002606 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002607 while (indent >= 8)
2608 {
2609 ga_append(&line_ga, TAB);
2610 msg_puts((char_u *)" ");
2611 indent -= 8;
2612 }
2613 while (indent-- > 0)
2614 {
2615 ga_append(&line_ga, ' ');
2616 msg_putchar(' ');
2617 }
2618 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002619 ++no_mapping;
2620 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002621
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 /*
2623 * Get the line, one character at a time.
2624 */
2625 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002628 long sw;
2629 char_u *s;
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (ga_grow(&line_ga, 40) == FAIL)
2632 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633
Bram Moolenaarba748c82017-02-26 14:00:07 +01002634 /*
2635 * Get one character at a time.
2636 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002637 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002638
2639 /* Check for a ":normal" command and no more characters left. */
2640 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2641 c1 = '\n';
2642 else
2643 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644
2645 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 * Handle line editing.
2647 * Previously this was left to the system, putting the terminal in
2648 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002652 msg_putchar('\n');
2653 break;
2654 }
2655
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002656 if (c1 == K_PS)
2657 {
2658 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2659 goto redraw;
2660 }
2661
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002662 if (!escaped)
2663 {
2664 /* CR typed means "enter", which is NL */
2665 if (c1 == '\r')
2666 c1 = '\n';
2667
2668 if (c1 == BS || c1 == K_BS
2669 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002671 if (line_ga.ga_len > 0)
2672 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002673#ifdef FEAT_MBYTE
2674 if (has_mbyte)
2675 {
2676 p = (char_u *)line_ga.ga_data;
2677 p[line_ga.ga_len] = NUL;
2678 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2679 line_ga.ga_len -= len;
2680 }
2681 else
2682#endif
2683 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002684 goto redraw;
2685 }
2686 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
2688
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002691 msg_col = startcol;
2692 msg_clr_eos();
2693 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002694 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002695 }
2696
2697 if (c1 == Ctrl_T)
2698 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002699 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 p = (char_u *)line_ga.ga_data;
2701 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002702 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002703 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002705 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002707 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002708 p = (char_u *)line_ga.ga_data;
2709 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2711 *s = ' ';
2712 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002714redraw:
2715 /* redraw the line */
2716 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002718 p = (char_u *)line_ga.ga_data;
2719 p[line_ga.ga_len] = NUL;
2720 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 msg_putchar(' ');
2727 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002728 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002732 len = MB_PTR2LEN(p);
2733 msg_outtrans_len(p, len);
2734 vcol += ptr2cells(p);
2735 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
2737 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002739 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002740 continue;
2741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002743 if (c1 == Ctrl_D)
2744 {
2745 /* Delete one shiftwidth. */
2746 p = (char_u *)line_ga.ga_data;
2747 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 if (prev_char == '^')
2750 ex_keep_indent = TRUE;
2751 indent = 0;
2752 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754 else
2755 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002757 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002758 if (indent > 0)
2759 {
2760 --indent;
2761 indent -= indent % get_sw_value(curbuf);
2762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002764 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002766 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2768 --line_ga.ga_len;
2769 }
2770 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002772
2773 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2774 {
2775 escaped = TRUE;
2776 continue;
2777 }
2778
2779 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2780 if (IS_SPECIAL(c1))
2781 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783
2784 if (IS_SPECIAL(c1))
2785 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002786#ifdef FEAT_MBYTE
2787 if (has_mbyte)
2788 len = (*mb_char2bytes)(c1,
2789 (char_u *)line_ga.ga_data + line_ga.ga_len);
2790 else
2791#endif
2792 {
2793 len = 1;
2794 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2795 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002796 if (c1 == '\n')
2797 msg_putchar('\n');
2798 else if (c1 == TAB)
2799 {
2800 /* Don't use chartabsize(), 'ts' can be different */
2801 do
2802 {
2803 msg_putchar(' ');
2804 } while (++vcol % 8);
2805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002808 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002809 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002812 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813 escaped = FALSE;
2814
2815 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002816 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002818 /* We are done when a NL is entered, but not when it comes after an
2819 * odd number of backslashes, that results in a NUL. */
2820 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002822 int bcount = 0;
2823
2824 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2825 ++bcount;
2826
2827 if (bcount > 0)
2828 {
2829 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2830 * "\NL", etc. */
2831 line_ga.ga_len -= (bcount + 1) / 2;
2832 pend -= (bcount + 1) / 2;
2833 pend[-1] = '\n';
2834 }
2835
2836 if ((bcount & 1) == 0)
2837 {
2838 --line_ga.ga_len;
2839 --pend;
2840 *pend = NUL;
2841 break;
2842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 }
2844 }
2845
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 --no_mapping;
2847 --allow_keys;
2848
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 /* make following messages go to the next line */
2850 msg_didout = FALSE;
2851 msg_col = 0;
2852 if (msg_row < Rows - 1)
2853 ++msg_row;
2854 emsg_on_display = FALSE; /* don't want ui_delay() */
2855
2856 if (got_int)
2857 ga_clear(&line_ga);
2858
2859 return (char_u *)line_ga.ga_data;
2860}
2861
Bram Moolenaare344bea2005-09-01 20:46:49 +00002862# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2863 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864/*
2865 * Return TRUE if ccline.overstrike is on.
2866 */
2867 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002868cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 return ccline.overstrike;
2871}
2872
2873/*
2874 * Return TRUE if the cursor is at the end of the cmdline.
2875 */
2876 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002877cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878{
2879 return (ccline.cmdpos >= ccline.cmdlen);
2880}
2881#endif
2882
Bram Moolenaar9372a112005-12-06 19:59:18 +00002883#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884/*
2885 * Return the virtual column number at the current cursor position.
2886 * This is used by the IM code to obtain the start of the preedit string.
2887 */
2888 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002889cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890{
2891 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2892 return MAXCOL;
2893
2894# ifdef FEAT_MBYTE
2895 if (has_mbyte)
2896 {
2897 colnr_T col;
2898 int i = 0;
2899
2900 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002901 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902
2903 return col;
2904 }
2905 else
2906# endif
2907 return ccline.cmdpos;
2908}
2909#endif
2910
2911#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2912/*
2913 * If part of the command line is an IM preedit string, redraw it with
2914 * IM feedback attributes. The cursor position is restored after drawing.
2915 */
2916 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002917redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
2919 if ((State & CMDLINE)
2920 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002921 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 && !p_imdisable
2923 && im_is_preediting())
2924 {
2925 int cmdpos = 0;
2926 int cmdspos;
2927 int old_row;
2928 int old_col;
2929 colnr_T col;
2930
2931 old_row = msg_row;
2932 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002933 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934
2935# ifdef FEAT_MBYTE
2936 if (has_mbyte)
2937 {
2938 for (col = 0; col < preedit_start_col
2939 && cmdpos < ccline.cmdlen; ++col)
2940 {
2941 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002942 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
2944 }
2945 else
2946# endif
2947 {
2948 cmdspos += preedit_start_col;
2949 cmdpos += preedit_start_col;
2950 }
2951
2952 msg_row = cmdline_row + (cmdspos / (int)Columns);
2953 msg_col = cmdspos % (int)Columns;
2954 if (msg_row >= Rows)
2955 msg_row = Rows - 1;
2956
2957 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2958 {
2959 int char_len;
2960 int char_attr;
2961
2962 char_attr = im_get_feedback_attr(col);
2963 if (char_attr < 0)
2964 break; /* end of preedit string */
2965
2966# ifdef FEAT_MBYTE
2967 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002968 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 else
2970# endif
2971 char_len = 1;
2972
2973 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2974 cmdpos += char_len;
2975 }
2976
2977 msg_row = old_row;
2978 msg_col = old_col;
2979 }
2980}
2981#endif /* FEAT_XIM && FEAT_GUI_GTK */
2982
2983/*
2984 * Allocate a new command line buffer.
2985 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2986 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2987 */
2988 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002989alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
2991 /*
2992 * give some extra space to avoid having to allocate all the time
2993 */
2994 if (len < 80)
2995 len = 100;
2996 else
2997 len += 20;
2998
2999 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3000 ccline.cmdbufflen = len;
3001}
3002
3003/*
3004 * Re-allocate the command line to length len + something extra.
3005 * return FAIL for failure, OK otherwise
3006 */
3007 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003008realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 char_u *p;
3011
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003012 if (len < ccline.cmdbufflen)
3013 return OK; /* no need to resize */
3014
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 p = ccline.cmdbuff;
3016 alloc_cmdbuff(len); /* will get some more */
3017 if (ccline.cmdbuff == NULL) /* out of memory */
3018 {
3019 ccline.cmdbuff = p; /* keep the old one */
3020 return FAIL;
3021 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003022 /* There isn't always a NUL after the command, but it may need to be
3023 * there, thus copy up to the NUL and add a NUL. */
3024 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3025 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003027
3028 if (ccline.xpc != NULL
3029 && ccline.xpc->xp_pattern != NULL
3030 && ccline.xpc->xp_context != EXPAND_NOTHING
3031 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3032 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003033 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003034
3035 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3036 * to point into the newly allocated memory. */
3037 if (i >= 0 && i <= ccline.cmdlen)
3038 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3039 }
3040
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 return OK;
3042}
3043
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003044#if defined(FEAT_ARABIC) || defined(PROTO)
3045static char_u *arshape_buf = NULL;
3046
3047# if defined(EXITFREE) || defined(PROTO)
3048 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003049free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003050{
3051 vim_free(arshape_buf);
3052}
3053# endif
3054#endif
3055
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056/*
3057 * Draw part of the cmdline at the current cursor position. But draw stars
3058 * when cmdline_star is TRUE.
3059 */
3060 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003061draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062{
3063#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3064 int i;
3065
3066 if (cmdline_star > 0)
3067 for (i = 0; i < len; ++i)
3068 {
3069 msg_putchar('*');
3070# ifdef FEAT_MBYTE
3071 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003072 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073# endif
3074 }
3075 else
3076#endif
3077#ifdef FEAT_ARABIC
3078 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3079 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 static int buflen = 0;
3081 char_u *p;
3082 int j;
3083 int newlen = 0;
3084 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003085 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int prev_c = 0;
3087 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003088 int u8c;
3089 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091
3092 /*
3093 * Do arabic shaping into a temporary buffer. This is very
3094 * inefficient!
3095 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003096 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 {
3098 /* Re-allocate the buffer. We keep it around to avoid a lot of
3099 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003100 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003101 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003102 arshape_buf = alloc(buflen);
3103 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return; /* out of memory */
3105 }
3106
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003107 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3108 {
3109 /* Prepend a space to draw the leading composing char on. */
3110 arshape_buf[0] = ' ';
3111 newlen = 1;
3112 }
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 for (j = start; j < start + len; j += mb_l)
3115 {
3116 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003117 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003118 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 if (ARABIC_CHAR(u8c))
3120 {
3121 /* Do Arabic shaping. */
3122 if (cmdmsg_rl)
3123 {
3124 /* displaying from right to left */
3125 pc = prev_c;
3126 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003127 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 if (j + mb_l >= start + len)
3129 nc = NUL;
3130 else
3131 nc = utf_ptr2char(p + mb_l);
3132 }
3133 else
3134 {
3135 /* displaying from left to right */
3136 if (j + mb_l >= start + len)
3137 pc = NUL;
3138 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003139 {
3140 int pcc[MAX_MCO];
3141
3142 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003144 pc1 = pcc[0];
3145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 nc = prev_c;
3147 }
3148 prev_c = u8c;
3149
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003150 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003152 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003153 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003155 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3156 if (u8cc[1] != 0)
3157 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003158 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 }
3160 }
3161 else
3162 {
3163 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003164 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 newlen += mb_l;
3166 }
3167 }
3168
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003169 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171 else
3172#endif
3173 msg_outtrans_len(ccline.cmdbuff + start, len);
3174}
3175
3176/*
3177 * Put a character on the command line. Shifts the following text to the
3178 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3179 * "c" must be printable (fit in one display cell)!
3180 */
3181 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003182putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 if (cmd_silent)
3185 return;
3186 msg_no_more = TRUE;
3187 msg_putchar(c);
3188 if (shift)
3189 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3190 msg_no_more = FALSE;
3191 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003192 extra_char = c;
3193 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194}
3195
3196/*
3197 * Undo a putcmdline(c, FALSE).
3198 */
3199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003200unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 if (cmd_silent)
3203 return;
3204 msg_no_more = TRUE;
3205 if (ccline.cmdlen == ccline.cmdpos)
3206 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003207#ifdef FEAT_MBYTE
3208 else if (has_mbyte)
3209 draw_cmdline(ccline.cmdpos,
3210 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 else
3213 draw_cmdline(ccline.cmdpos, 1);
3214 msg_no_more = FALSE;
3215 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003216 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217}
3218
3219/*
3220 * Put the given string, of the given length, onto the command line.
3221 * If len is -1, then STRLEN() is used to calculate the length.
3222 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3223 * part will be redrawn, otherwise it will not. If this function is called
3224 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3225 * called afterwards.
3226 */
3227 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003228put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
3230 int retval;
3231 int i;
3232 int m;
3233 int c;
3234
3235 if (len < 0)
3236 len = (int)STRLEN(str);
3237
3238 /* Check if ccline.cmdbuff needs to be longer */
3239 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003240 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 else
3242 retval = OK;
3243 if (retval == OK)
3244 {
3245 if (!ccline.overstrike)
3246 {
3247 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3248 ccline.cmdbuff + ccline.cmdpos,
3249 (size_t)(ccline.cmdlen - ccline.cmdpos));
3250 ccline.cmdlen += len;
3251 }
3252 else
3253 {
3254#ifdef FEAT_MBYTE
3255 if (has_mbyte)
3256 {
3257 /* Count nr of characters in the new string. */
3258 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003259 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 ++m;
3261 /* Count nr of bytes in cmdline that are overwritten by these
3262 * characters. */
3263 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003264 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 --m;
3266 if (i < ccline.cmdlen)
3267 {
3268 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3269 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3270 ccline.cmdlen += ccline.cmdpos + len - i;
3271 }
3272 else
3273 ccline.cmdlen = ccline.cmdpos + len;
3274 }
3275 else
3276#endif
3277 if (ccline.cmdpos + len > ccline.cmdlen)
3278 ccline.cmdlen = ccline.cmdpos + len;
3279 }
3280 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3281 ccline.cmdbuff[ccline.cmdlen] = NUL;
3282
3283#ifdef FEAT_MBYTE
3284 if (enc_utf8)
3285 {
3286 /* When the inserted text starts with a composing character,
3287 * backup to the character before it. There could be two of them.
3288 */
3289 i = 0;
3290 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3291 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3292 {
3293 i = (*mb_head_off)(ccline.cmdbuff,
3294 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3295 ccline.cmdpos -= i;
3296 len += i;
3297 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3298 }
3299# ifdef FEAT_ARABIC
3300 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3301 {
3302 /* Check the previous character for Arabic combining pair. */
3303 i = (*mb_head_off)(ccline.cmdbuff,
3304 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3305 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3306 + ccline.cmdpos - i), c))
3307 {
3308 ccline.cmdpos -= i;
3309 len += i;
3310 }
3311 else
3312 i = 0;
3313 }
3314# endif
3315 if (i != 0)
3316 {
3317 /* Also backup the cursor position. */
3318 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3319 ccline.cmdspos -= i;
3320 msg_col -= i;
3321 if (msg_col < 0)
3322 {
3323 msg_col += Columns;
3324 --msg_row;
3325 }
3326 }
3327 }
3328#endif
3329
3330 if (redraw && !cmd_silent)
3331 {
3332 msg_no_more = TRUE;
3333 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003334 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3336 /* Avoid clearing the rest of the line too often. */
3337 if (cmdline_row != i || ccline.overstrike)
3338 msg_clr_eos();
3339 msg_no_more = FALSE;
3340 }
3341#ifdef FEAT_FKMAP
3342 /*
3343 * If we are in Farsi command mode, the character input must be in
3344 * Insert mode. So do not advance the cmdpos.
3345 */
3346 if (!cmd_fkmap)
3347#endif
3348 {
3349 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003350 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003352 if (m < 0) /* overflow, Columns or Rows at weird value */
3353 m = MAXCOL;
3354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 else
3356 m = MAXCOL;
3357 for (i = 0; i < len; ++i)
3358 {
3359 c = cmdline_charsize(ccline.cmdpos);
3360#ifdef FEAT_MBYTE
3361 /* count ">" for a double-wide char that doesn't fit. */
3362 if (has_mbyte)
3363 correct_cmdspos(ccline.cmdpos, c);
3364#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003365 /* Stop cursor at the end of the screen, but do increment the
3366 * insert position, so that entering a very long command
3367 * works, even though you can't see it. */
3368 if (ccline.cmdspos + c < m)
3369 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370#ifdef FEAT_MBYTE
3371 if (has_mbyte)
3372 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003373 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 if (c > len - i - 1)
3375 c = len - i - 1;
3376 ccline.cmdpos += c;
3377 i += c;
3378 }
3379#endif
3380 ++ccline.cmdpos;
3381 }
3382 }
3383 }
3384 if (redraw)
3385 msg_check();
3386 return retval;
3387}
3388
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003389static struct cmdline_info prev_ccline;
3390static int prev_ccline_used = FALSE;
3391
3392/*
3393 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3394 * and overwrite it. But get_cmdline_str() may need it, thus make it
3395 * available globally in prev_ccline.
3396 */
3397 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003398save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003399{
3400 if (!prev_ccline_used)
3401 {
3402 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3403 prev_ccline_used = TRUE;
3404 }
3405 *ccp = prev_ccline;
3406 prev_ccline = ccline;
3407 ccline.cmdbuff = NULL;
3408 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003409 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003410}
3411
3412/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003413 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003414 */
3415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003416restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003417{
3418 ccline = prev_ccline;
3419 prev_ccline = *ccp;
3420}
3421
Bram Moolenaar5a305422006-04-28 22:38:25 +00003422#if defined(FEAT_EVAL) || defined(PROTO)
3423/*
3424 * Save the command line into allocated memory. Returns a pointer to be
3425 * passed to restore_cmdline_alloc() later.
3426 * Returns NULL when failed.
3427 */
3428 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003429save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003430{
3431 struct cmdline_info *p;
3432
3433 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3434 if (p != NULL)
3435 save_cmdline(p);
3436 return (char_u *)p;
3437}
3438
3439/*
3440 * Restore the command line from the return value of save_cmdline_alloc().
3441 */
3442 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003443restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003444{
3445 if (p != NULL)
3446 {
3447 restore_cmdline((struct cmdline_info *)p);
3448 vim_free(p);
3449 }
3450}
3451#endif
3452
Bram Moolenaar8299df92004-07-10 09:47:34 +00003453/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003454 * Paste a yank register into the command line.
3455 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003456 * insert_reg() can't be used here, because special characters from the
3457 * register contents will be interpreted as commands.
3458 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003459 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003460 */
3461 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003462cmdline_paste(
3463 int regname,
3464 int literally, /* Insert text literally instead of "as typed" */
3465 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003466{
3467 long i;
3468 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003469 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003470 int allocated;
3471 struct cmdline_info save_ccline;
3472
3473 /* check for valid regname; also accept special characters for CTRL-R in
3474 * the command line */
3475 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003476 && regname != Ctrl_A && regname != Ctrl_L
3477 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003478 return FAIL;
3479
3480 /* A register containing CTRL-R can cause an endless loop. Allow using
3481 * CTRL-C to break the loop. */
3482 line_breakcheck();
3483 if (got_int)
3484 return FAIL;
3485
3486#ifdef FEAT_CLIPBOARD
3487 regname = may_get_selection(regname);
3488#endif
3489
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003490 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003491 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003492 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003493 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003494 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003495 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003496 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497
3498 if (i)
3499 {
3500 /* Got the value of a special register in "arg". */
3501 if (arg == NULL)
3502 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003503
3504 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3505 * part of the word. */
3506 p = arg;
3507 if (p_is && regname == Ctrl_W)
3508 {
3509 char_u *w;
3510 int len;
3511
3512 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003513 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003514 {
3515#ifdef FEAT_MBYTE
3516 if (has_mbyte)
3517 {
3518 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3519 if (!vim_iswordc(mb_ptr2char(w - len)))
3520 break;
3521 w -= len;
3522 }
3523 else
3524#endif
3525 {
3526 if (!vim_iswordc(w[-1]))
3527 break;
3528 --w;
3529 }
3530 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003531 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003532 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3533 p += len;
3534 }
3535
3536 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003537 if (allocated)
3538 vim_free(arg);
3539 return OK;
3540 }
3541
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003542 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543}
3544
3545/*
3546 * Put a string on the command line.
3547 * When "literally" is TRUE, insert literally.
3548 * When "literally" is FALSE, insert as typed, but don't leave the command
3549 * line.
3550 */
3551 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003552cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003553{
3554 int c, cv;
3555
3556 if (literally)
3557 put_on_cmdline(s, -1, TRUE);
3558 else
3559 while (*s != NUL)
3560 {
3561 cv = *s;
3562 if (cv == Ctrl_V && s[1])
3563 ++s;
3564#ifdef FEAT_MBYTE
3565 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003566 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003567 else
3568#endif
3569 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003570 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3571 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003572#ifdef UNIX
3573 || c == intr_char
3574#endif
3575 || (c == Ctrl_BSL && *s == Ctrl_N))
3576 stuffcharReadbuff(Ctrl_V);
3577 stuffcharReadbuff(c);
3578 }
3579}
3580
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581#ifdef FEAT_WILDMENU
3582/*
3583 * Delete characters on the command line, from "from" to the current
3584 * position.
3585 */
3586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003587cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588{
3589 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3590 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3591 ccline.cmdlen -= ccline.cmdpos - from;
3592 ccline.cmdpos = from;
3593}
3594#endif
3595
3596/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003597 * This function is called when the screen size changes and with incremental
3598 * search and in other situations where the command line may have been
3599 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 */
3601 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003602redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003604 redrawcmdline_ex(TRUE);
3605}
3606
3607 void
3608redrawcmdline_ex(int do_compute_cmdrow)
3609{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 if (cmd_silent)
3611 return;
3612 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003613 if (do_compute_cmdrow)
3614 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 redrawcmd();
3616 cursorcmd();
3617}
3618
3619 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003620redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621{
3622 int i;
3623
3624 if (cmd_silent)
3625 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003626 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 msg_putchar(ccline.cmdfirstc);
3628 if (ccline.cmdprompt != NULL)
3629 {
3630 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3631 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3632 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003633 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 --ccline.cmdindent;
3635 }
3636 else
3637 for (i = ccline.cmdindent; i > 0; --i)
3638 msg_putchar(' ');
3639}
3640
3641/*
3642 * Redraw what is currently on the command line.
3643 */
3644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003645redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646{
3647 if (cmd_silent)
3648 return;
3649
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003650 /* when 'incsearch' is set there may be no command line while redrawing */
3651 if (ccline.cmdbuff == NULL)
3652 {
3653 windgoto(cmdline_row, 0);
3654 msg_clr_eos();
3655 return;
3656 }
3657
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 msg_start();
3659 redrawcmdprompt();
3660
3661 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3662 msg_no_more = TRUE;
3663 draw_cmdline(0, ccline.cmdlen);
3664 msg_clr_eos();
3665 msg_no_more = FALSE;
3666
3667 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003668 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003669 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
3671 /*
3672 * An emsg() before may have set msg_scroll. This is used in normal mode,
3673 * in cmdline mode we can reset them now.
3674 */
3675 msg_scroll = FALSE; /* next message overwrites cmdline */
3676
3677 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3678 * in cmdline mode */
3679 skip_redraw = FALSE;
3680}
3681
3682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003683compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003685 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 cmdline_row = Rows - 1;
3687 else
3688 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003689 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690}
3691
3692 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003693cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694{
3695 if (cmd_silent)
3696 return;
3697
3698#ifdef FEAT_RIGHTLEFT
3699 if (cmdmsg_rl)
3700 {
3701 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3702 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3703 if (msg_row <= 0)
3704 msg_row = Rows - 1;
3705 }
3706 else
3707#endif
3708 {
3709 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3710 msg_col = ccline.cmdspos % (int)Columns;
3711 if (msg_row >= Rows)
3712 msg_row = Rows - 1;
3713 }
3714
3715 windgoto(msg_row, msg_col);
3716#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003717 if (p_imst == IM_ON_THE_SPOT)
3718 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719#endif
3720#ifdef MCH_CURSOR_SHAPE
3721 mch_update_cursor();
3722#endif
3723}
3724
3725 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003726gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727{
3728 msg_start();
3729#ifdef FEAT_RIGHTLEFT
3730 if (cmdmsg_rl)
3731 msg_col = Columns - 1;
3732 else
3733#endif
3734 msg_col = 0; /* always start in column 0 */
3735 if (clr) /* clear the bottom line(s) */
3736 msg_clr_eos(); /* will reset clear_cmdline */
3737 windgoto(cmdline_row, 0);
3738}
3739
3740/*
3741 * Check the word in front of the cursor for an abbreviation.
3742 * Called when the non-id character "c" has been entered.
3743 * When an abbreviation is recognized it is removed from the text with
3744 * backspaces and the replacement string is inserted, followed by "c".
3745 */
3746 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003747ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003749 int spos = 0;
3750
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3752 return FALSE;
3753
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003754 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3755 * Actually accepts any mark. */
3756 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3757 spos++;
3758 if (ccline.cmdlen - spos > 5
3759 && ccline.cmdbuff[spos] == '\''
3760 && ccline.cmdbuff[spos + 2] == ','
3761 && ccline.cmdbuff[spos + 3] == '\'')
3762 spos += 5;
3763 else
3764 /* check abbreviation from the beginning of the commandline */
3765 spos = 0;
3766
3767 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768}
3769
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003770#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3771 static int
3772#ifdef __BORLANDC__
3773_RTLENTRYF
3774#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003775sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003776{
3777 char_u *p1 = *(char_u **)s1;
3778 char_u *p2 = *(char_u **)s2;
3779
3780 if (*p1 != '<' && *p2 == '<') return -1;
3781 if (*p1 == '<' && *p2 != '<') return 1;
3782 return STRCMP(p1, p2);
3783}
3784#endif
3785
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786/*
3787 * Return FAIL if this is not an appropriate context in which to do
3788 * completion of anything, return OK if it is (even if there are no matches).
3789 * For the caller, this means that the character is just passed through like a
3790 * normal character (instead of being expanded). This allows :s/^I^D etc.
3791 */
3792 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003793nextwild(
3794 expand_T *xp,
3795 int type,
3796 int options, /* extra options for ExpandOne() */
3797 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798{
3799 int i, j;
3800 char_u *p1;
3801 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 int difflen;
3803 int v;
3804
3805 if (xp->xp_numfiles == -1)
3806 {
3807 set_expand_context(xp);
3808 cmd_showtail = expand_showtail(xp);
3809 }
3810
3811 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3812 {
3813 beep_flush();
3814 return OK; /* Something illegal on command line */
3815 }
3816 if (xp->xp_context == EXPAND_NOTHING)
3817 {
3818 /* Caller can use the character as a normal char instead */
3819 return FAIL;
3820 }
3821
3822 MSG_PUTS("..."); /* show that we are busy */
3823 out_flush();
3824
3825 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003826 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827
3828 if (type == WILD_NEXT || type == WILD_PREV)
3829 {
3830 /*
3831 * Get next/previous match for a previous expanded pattern.
3832 */
3833 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3834 }
3835 else
3836 {
3837 /*
3838 * Translate string into pattern and expand it.
3839 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003840 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3841 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 p2 = NULL;
3843 else
3844 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003845 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003846 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3847 if (escape)
3848 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003849
3850 if (p_wic)
3851 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003852 p2 = ExpandOne(xp, p1,
3853 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003854 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003856 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (p2 != NULL && type == WILD_LONGEST)
3858 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003859 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (ccline.cmdbuff[i + j] == '*'
3861 || ccline.cmdbuff[i + j] == '?')
3862 break;
3863 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003864 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 }
3867 }
3868
3869 if (p2 != NULL && !got_int)
3870 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003871 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003872 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003874 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 xp->xp_pattern = ccline.cmdbuff + i;
3876 }
3877 else
3878 v = OK;
3879 if (v == OK)
3880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003881 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3882 &ccline.cmdbuff[ccline.cmdpos],
3883 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3884 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 ccline.cmdlen += difflen;
3886 ccline.cmdpos += difflen;
3887 }
3888 }
3889 vim_free(p2);
3890
3891 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003892 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893
3894 /* When expanding a ":map" command and no matches are found, assume that
3895 * the key is supposed to be inserted literally */
3896 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3897 return FAIL;
3898
3899 if (xp->xp_numfiles <= 0 && p2 == NULL)
3900 beep_flush();
3901 else if (xp->xp_numfiles == 1)
3902 /* free expanded pattern */
3903 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3904
3905 return OK;
3906}
3907
3908/*
3909 * Do wildcard expansion on the string 'str'.
3910 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003911 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 * Return NULL for failure.
3913 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003914 * "orig" is the originally expanded string, copied to allocated memory. It
3915 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3916 * WILD_PREV "orig" should be NULL.
3917 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003918 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3919 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 *
3921 * mode = WILD_FREE: just free previously expanded matches
3922 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3923 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3924 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3925 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3926 * mode = WILD_ALL: return all matches concatenated
3927 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003928 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 *
3930 * options = WILD_LIST_NOTFOUND: list entries without a match
3931 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3932 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3933 * options = WILD_NO_BEEP: Don't beep for multiple matches
3934 * options = WILD_ADD_SLASH: add a slash after directory names
3935 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3936 * options = WILD_SILENT: don't print warning messages
3937 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003938 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 *
3940 * The variables xp->xp_context and xp->xp_backslash must have been set!
3941 */
3942 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003943ExpandOne(
3944 expand_T *xp,
3945 char_u *str,
3946 char_u *orig, /* allocated copy of original of expanded string */
3947 int options,
3948 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
3950 char_u *ss = NULL;
3951 static int findex;
3952 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003953 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 int i;
3955 long_u len;
3956 int non_suf_match; /* number without matching suffix */
3957
3958 /*
3959 * first handle the case of using an old match
3960 */
3961 if (mode == WILD_NEXT || mode == WILD_PREV)
3962 {
3963 if (xp->xp_numfiles > 0)
3964 {
3965 if (mode == WILD_PREV)
3966 {
3967 if (findex == -1)
3968 findex = xp->xp_numfiles;
3969 --findex;
3970 }
3971 else /* mode == WILD_NEXT */
3972 ++findex;
3973
3974 /*
3975 * When wrapping around, return the original string, set findex to
3976 * -1.
3977 */
3978 if (findex < 0)
3979 {
3980 if (orig_save == NULL)
3981 findex = xp->xp_numfiles - 1;
3982 else
3983 findex = -1;
3984 }
3985 if (findex >= xp->xp_numfiles)
3986 {
3987 if (orig_save == NULL)
3988 findex = 0;
3989 else
3990 findex = -1;
3991 }
3992#ifdef FEAT_WILDMENU
3993 if (p_wmnu)
3994 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3995 findex, cmd_showtail);
3996#endif
3997 if (findex == -1)
3998 return vim_strsave(orig_save);
3999 return vim_strsave(xp->xp_files[findex]);
4000 }
4001 else
4002 return NULL;
4003 }
4004
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004005 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4007 {
4008 FreeWild(xp->xp_numfiles, xp->xp_files);
4009 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004010 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 }
4012 findex = 0;
4013
4014 if (mode == WILD_FREE) /* only release file name */
4015 return NULL;
4016
4017 if (xp->xp_numfiles == -1)
4018 {
4019 vim_free(orig_save);
4020 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004021 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023 /*
4024 * Do the expansion.
4025 */
4026 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4027 options) == FAIL)
4028 {
4029#ifdef FNAME_ILLEGAL
4030 /* Illegal file name has been silently skipped. But when there
4031 * are wildcards, the real problem is that there was no match,
4032 * causing the pattern to be added, which has illegal characters.
4033 */
4034 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4035 EMSG2(_(e_nomatch2), str);
4036#endif
4037 }
4038 else if (xp->xp_numfiles == 0)
4039 {
4040 if (!(options & WILD_SILENT))
4041 EMSG2(_(e_nomatch2), str);
4042 }
4043 else
4044 {
4045 /* Escape the matches for use on the command line. */
4046 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4047
4048 /*
4049 * Check for matching suffixes in file names.
4050 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004051 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4052 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
4054 if (xp->xp_numfiles)
4055 non_suf_match = xp->xp_numfiles;
4056 else
4057 non_suf_match = 1;
4058 if ((xp->xp_context == EXPAND_FILES
4059 || xp->xp_context == EXPAND_DIRECTORIES)
4060 && xp->xp_numfiles > 1)
4061 {
4062 /*
4063 * More than one match; check suffix.
4064 * The files will have been sorted on matching suffix in
4065 * expand_wildcards, only need to check the first two.
4066 */
4067 non_suf_match = 0;
4068 for (i = 0; i < 2; ++i)
4069 if (match_suffix(xp->xp_files[i]))
4070 ++non_suf_match;
4071 }
4072 if (non_suf_match != 1)
4073 {
4074 /* Can we ever get here unless it's while expanding
4075 * interactively? If not, we can get rid of this all
4076 * together. Don't really want to wait for this message
4077 * (and possibly have to hit return to continue!).
4078 */
4079 if (!(options & WILD_SILENT))
4080 EMSG(_(e_toomany));
4081 else if (!(options & WILD_NO_BEEP))
4082 beep_flush();
4083 }
4084 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4085 ss = vim_strsave(xp->xp_files[0]);
4086 }
4087 }
4088 }
4089
4090 /* Find longest common part */
4091 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4092 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004093 int mb_len = 1;
4094 int c0, ci;
4095
4096 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004098#ifdef FEAT_MBYTE
4099 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004101 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4102 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4103 }
4104 else
4105#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004106 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004107 for (i = 1; i < xp->xp_numfiles; ++i)
4108 {
4109#ifdef FEAT_MBYTE
4110 if (has_mbyte)
4111 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4112 else
4113#endif
4114 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004115 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004117 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004118 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004120 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 break;
4122 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004123 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 break;
4125 }
4126 if (i < xp->xp_numfiles)
4127 {
4128 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004129 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 break;
4131 }
4132 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004133
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 ss = alloc((unsigned)len + 1);
4135 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004136 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 findex = -1; /* next p_wc gets first one */
4138 }
4139
4140 /* Concatenate all matching names */
4141 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4142 {
4143 len = 0;
4144 for (i = 0; i < xp->xp_numfiles; ++i)
4145 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4146 ss = lalloc(len, TRUE);
4147 if (ss != NULL)
4148 {
4149 *ss = NUL;
4150 for (i = 0; i < xp->xp_numfiles; ++i)
4151 {
4152 STRCAT(ss, xp->xp_files[i]);
4153 if (i != xp->xp_numfiles - 1)
4154 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4155 }
4156 }
4157 }
4158
4159 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4160 ExpandCleanup(xp);
4161
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004162 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004163 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004164 vim_free(orig);
4165
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 return ss;
4167}
4168
4169/*
4170 * Prepare an expand structure for use.
4171 */
4172 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004173ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004175 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004176 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004178#ifndef BACKSLASH_IN_FILENAME
4179 xp->xp_shell = FALSE;
4180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 xp->xp_numfiles = -1;
4182 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004183#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4184 xp->xp_arg = NULL;
4185#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004186 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187}
4188
4189/*
4190 * Cleanup an expand structure after use.
4191 */
4192 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004193ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
4195 if (xp->xp_numfiles >= 0)
4196 {
4197 FreeWild(xp->xp_numfiles, xp->xp_files);
4198 xp->xp_numfiles = -1;
4199 }
4200}
4201
4202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004203ExpandEscape(
4204 expand_T *xp,
4205 char_u *str,
4206 int numfiles,
4207 char_u **files,
4208 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209{
4210 int i;
4211 char_u *p;
4212
4213 /*
4214 * May change home directory back to "~"
4215 */
4216 if (options & WILD_HOME_REPLACE)
4217 tilde_replace(str, numfiles, files);
4218
4219 if (options & WILD_ESCAPE)
4220 {
4221 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004222 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004223 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 || xp->xp_context == EXPAND_BUFFERS
4225 || xp->xp_context == EXPAND_DIRECTORIES)
4226 {
4227 /*
4228 * Insert a backslash into a file name before a space, \, %, #
4229 * and wildmatch characters, except '~'.
4230 */
4231 for (i = 0; i < numfiles; ++i)
4232 {
4233 /* for ":set path=" we need to escape spaces twice */
4234 if (xp->xp_backslash == XP_BS_THREE)
4235 {
4236 p = vim_strsave_escaped(files[i], (char_u *)" ");
4237 if (p != NULL)
4238 {
4239 vim_free(files[i]);
4240 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004241#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 p = vim_strsave_escaped(files[i], (char_u *)" ");
4243 if (p != NULL)
4244 {
4245 vim_free(files[i]);
4246 files[i] = p;
4247 }
4248#endif
4249 }
4250 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004251#ifdef BACKSLASH_IN_FILENAME
4252 p = vim_strsave_fnameescape(files[i], FALSE);
4253#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004254 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004255#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 if (p != NULL)
4257 {
4258 vim_free(files[i]);
4259 files[i] = p;
4260 }
4261
4262 /* If 'str' starts with "\~", replace "~" at start of
4263 * files[i] with "\~". */
4264 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004265 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004268
4269 /* If the first file starts with a '+' escape it. Otherwise it
4270 * could be seen as "+cmd". */
4271 if (*files[0] == '+')
4272 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 else if (xp->xp_context == EXPAND_TAGS)
4275 {
4276 /*
4277 * Insert a backslash before characters in a tag name that
4278 * would terminate the ":tag" command.
4279 */
4280 for (i = 0; i < numfiles; ++i)
4281 {
4282 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4283 if (p != NULL)
4284 {
4285 vim_free(files[i]);
4286 files[i] = p;
4287 }
4288 }
4289 }
4290 }
4291}
4292
4293/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004294 * Escape special characters in "fname" for when used as a file name argument
4295 * after a Vim command, or, when "shell" is non-zero, a shell command.
4296 * Returns the result in allocated memory.
4297 */
4298 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004299vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004300{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004301 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004302#ifdef BACKSLASH_IN_FILENAME
4303 char_u buf[20];
4304 int j = 0;
4305
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004306 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004307 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004308 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004309 buf[j++] = *p;
4310 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004311 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004312#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004313 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4314 if (shell && csh_like_shell() && p != NULL)
4315 {
4316 char_u *s;
4317
4318 /* For csh and similar shells need to put two backslashes before '!'.
4319 * One is taken by Vim, one by the shell. */
4320 s = vim_strsave_escaped(p, (char_u *)"!");
4321 vim_free(p);
4322 p = s;
4323 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004324#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004325
4326 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4327 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004328 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004329 escape_fname(&p);
4330
4331 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004332}
4333
4334/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004335 * Put a backslash before the file name in "pp", which is in allocated memory.
4336 */
4337 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004338escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004339{
4340 char_u *p;
4341
4342 p = alloc((unsigned)(STRLEN(*pp) + 2));
4343 if (p != NULL)
4344 {
4345 p[0] = '\\';
4346 STRCPY(p + 1, *pp);
4347 vim_free(*pp);
4348 *pp = p;
4349 }
4350}
4351
4352/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 * For each file name in files[num_files]:
4354 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4355 */
4356 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004357tilde_replace(
4358 char_u *orig_pat,
4359 int num_files,
4360 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361{
4362 int i;
4363 char_u *p;
4364
4365 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4366 {
4367 for (i = 0; i < num_files; ++i)
4368 {
4369 p = home_replace_save(NULL, files[i]);
4370 if (p != NULL)
4371 {
4372 vim_free(files[i]);
4373 files[i] = p;
4374 }
4375 }
4376 }
4377}
4378
4379/*
4380 * Show all matches for completion on the command line.
4381 * Returns EXPAND_NOTHING when the character that triggered expansion should
4382 * be inserted like a normal character.
4383 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004385showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386{
4387#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4388 int num_files;
4389 char_u **files_found;
4390 int i, j, k;
4391 int maxlen;
4392 int lines;
4393 int columns;
4394 char_u *p;
4395 int lastlen;
4396 int attr;
4397 int showtail;
4398
4399 if (xp->xp_numfiles == -1)
4400 {
4401 set_expand_context(xp);
4402 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4403 &num_files, &files_found);
4404 showtail = expand_showtail(xp);
4405 if (i != EXPAND_OK)
4406 return i;
4407
4408 }
4409 else
4410 {
4411 num_files = xp->xp_numfiles;
4412 files_found = xp->xp_files;
4413 showtail = cmd_showtail;
4414 }
4415
4416#ifdef FEAT_WILDMENU
4417 if (!wildmenu)
4418 {
4419#endif
4420 msg_didany = FALSE; /* lines_left will be set */
4421 msg_start(); /* prepare for paging */
4422 msg_putchar('\n');
4423 out_flush();
4424 cmdline_row = msg_row;
4425 msg_didany = FALSE; /* lines_left will be set again */
4426 msg_start(); /* prepare for paging */
4427#ifdef FEAT_WILDMENU
4428 }
4429#endif
4430
4431 if (got_int)
4432 got_int = FALSE; /* only int. the completion, not the cmd line */
4433#ifdef FEAT_WILDMENU
4434 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004435 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#endif
4437 else
4438 {
4439 /* find the length of the longest file name */
4440 maxlen = 0;
4441 for (i = 0; i < num_files; ++i)
4442 {
4443 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004444 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 || xp->xp_context == EXPAND_BUFFERS))
4446 {
4447 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4448 j = vim_strsize(NameBuff);
4449 }
4450 else
4451 j = vim_strsize(L_SHOWFILE(i));
4452 if (j > maxlen)
4453 maxlen = j;
4454 }
4455
4456 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4457 lines = num_files;
4458 else
4459 {
4460 /* compute the number of columns and lines for the listing */
4461 maxlen += 2; /* two spaces between file names */
4462 columns = ((int)Columns + 2) / maxlen;
4463 if (columns < 1)
4464 columns = 1;
4465 lines = (num_files + columns - 1) / columns;
4466 }
4467
Bram Moolenaar8820b482017-03-16 17:23:31 +01004468 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469
4470 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4471 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 msg_clr_eos();
4474 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004475 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 }
4477
4478 /* list the files line by line */
4479 for (i = 0; i < lines; ++i)
4480 {
4481 lastlen = 999;
4482 for (k = i; k < num_files; k += lines)
4483 {
4484 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4485 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004486 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 p = files_found[k] + STRLEN(files_found[k]) + 1;
4488 msg_advance(maxlen + 1);
4489 msg_puts(p);
4490 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004491 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 break;
4493 }
4494 for (j = maxlen - lastlen; --j >= 0; )
4495 msg_putchar(' ');
4496 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004497 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 || xp->xp_context == EXPAND_BUFFERS)
4499 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004500 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004501 if (xp->xp_numfiles != -1)
4502 {
4503 char_u *halved_slash;
4504 char_u *exp_path;
4505
4506 /* Expansion was done before and special characters
4507 * were escaped, need to halve backslashes. Also
4508 * $HOME has been replaced with ~/. */
4509 exp_path = expand_env_save_opt(files_found[k], TRUE);
4510 halved_slash = backslash_halve_save(
4511 exp_path != NULL ? exp_path : files_found[k]);
4512 j = mch_isdir(halved_slash != NULL ? halved_slash
4513 : files_found[k]);
4514 vim_free(exp_path);
4515 vim_free(halved_slash);
4516 }
4517 else
4518 /* Expansion was done here, file names are literal. */
4519 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 if (showtail)
4521 p = L_SHOWFILE(k);
4522 else
4523 {
4524 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4525 TRUE);
4526 p = NameBuff;
4527 }
4528 }
4529 else
4530 {
4531 j = FALSE;
4532 p = L_SHOWFILE(k);
4533 }
4534 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4535 }
4536 if (msg_col > 0) /* when not wrapped around */
4537 {
4538 msg_clr_eos();
4539 msg_putchar('\n');
4540 }
4541 out_flush(); /* show one line at a time */
4542 if (got_int)
4543 {
4544 got_int = FALSE;
4545 break;
4546 }
4547 }
4548
4549 /*
4550 * we redraw the command below the lines that we have just listed
4551 * This is a bit tricky, but it saves a lot of screen updating.
4552 */
4553 cmdline_row = msg_row; /* will put it back later */
4554 }
4555
4556 if (xp->xp_numfiles == -1)
4557 FreeWild(num_files, files_found);
4558
4559 return EXPAND_OK;
4560}
4561
4562/*
4563 * Private gettail for showmatches() (and win_redr_status_matches()):
4564 * Find tail of file name path, but ignore trailing "/".
4565 */
4566 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004567sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568{
4569 char_u *p;
4570 char_u *t = s;
4571 int had_sep = FALSE;
4572
4573 for (p = s; *p != NUL; )
4574 {
4575 if (vim_ispathsep(*p)
4576#ifdef BACKSLASH_IN_FILENAME
4577 && !rem_backslash(p)
4578#endif
4579 )
4580 had_sep = TRUE;
4581 else if (had_sep)
4582 {
4583 t = p;
4584 had_sep = FALSE;
4585 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004586 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 return t;
4589}
4590
4591/*
4592 * Return TRUE if we only need to show the tail of completion matches.
4593 * When not completing file names or there is a wildcard in the path FALSE is
4594 * returned.
4595 */
4596 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004597expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
4599 char_u *s;
4600 char_u *end;
4601
4602 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004603 if (xp->xp_context != EXPAND_FILES
4604 && xp->xp_context != EXPAND_SHELLCMD
4605 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 return FALSE;
4607
4608 end = gettail(xp->xp_pattern);
4609 if (end == xp->xp_pattern) /* there is no path separator */
4610 return FALSE;
4611
4612 for (s = xp->xp_pattern; s < end; s++)
4613 {
4614 /* Skip escaped wildcards. Only when the backslash is not a path
4615 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4616 if (rem_backslash(s))
4617 ++s;
4618 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4619 return FALSE;
4620 }
4621 return TRUE;
4622}
4623
4624/*
4625 * Prepare a string for expansion.
4626 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004627 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 * When expanding other names: The string will be used with regcomp(). Copy
4629 * the name into allocated memory and prepend "^".
4630 */
4631 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004632addstar(
4633 char_u *fname,
4634 int len,
4635 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636{
4637 char_u *retval;
4638 int i, j;
4639 int new_len;
4640 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004641 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004643 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004644 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004645 && context != EXPAND_SHELLCMD
4646 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
4648 /*
4649 * Matching will be done internally (on something other than files).
4650 * So we convert the file-matching-type wildcards into our kind for
4651 * use with vim_regcomp(). First work out how long it will be:
4652 */
4653
4654 /* For help tags the translation is done in find_help_tags().
4655 * For a tag pattern starting with "/" no translation is needed. */
4656 if (context == EXPAND_HELP
4657 || context == EXPAND_COLORS
4658 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004659 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004660 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004661 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004662 || ((context == EXPAND_TAGS_LISTFILES
4663 || context == EXPAND_TAGS)
4664 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 retval = vim_strnsave(fname, len);
4666 else
4667 {
4668 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4669 for (i = 0; i < len; i++)
4670 {
4671 if (fname[i] == '*' || fname[i] == '~')
4672 new_len++; /* '*' needs to be replaced by ".*"
4673 '~' needs to be replaced by "\~" */
4674
4675 /* Buffer names are like file names. "." should be literal */
4676 if (context == EXPAND_BUFFERS && fname[i] == '.')
4677 new_len++; /* "." becomes "\." */
4678
4679 /* Custom expansion takes care of special things, match
4680 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004681 if ((context == EXPAND_USER_DEFINED
4682 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 new_len++; /* '\' becomes "\\" */
4684 }
4685 retval = alloc(new_len);
4686 if (retval != NULL)
4687 {
4688 retval[0] = '^';
4689 j = 1;
4690 for (i = 0; i < len; i++, j++)
4691 {
4692 /* Skip backslash. But why? At least keep it for custom
4693 * expansion. */
4694 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004695 && context != EXPAND_USER_LIST
4696 && fname[i] == '\\'
4697 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 break;
4699
4700 switch (fname[i])
4701 {
4702 case '*': retval[j++] = '.';
4703 break;
4704 case '~': retval[j++] = '\\';
4705 break;
4706 case '?': retval[j] = '.';
4707 continue;
4708 case '.': if (context == EXPAND_BUFFERS)
4709 retval[j++] = '\\';
4710 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004711 case '\\': if (context == EXPAND_USER_DEFINED
4712 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 retval[j++] = '\\';
4714 break;
4715 }
4716 retval[j] = fname[i];
4717 }
4718 retval[j] = NUL;
4719 }
4720 }
4721 }
4722 else
4723 {
4724 retval = alloc(len + 4);
4725 if (retval != NULL)
4726 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004727 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728
4729 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004730 * Don't add a star to *, ~, ~user, $var or `cmd`.
4731 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 * ~ would be at the start of the file name, but not the tail.
4733 * $ could be anywhere in the tail.
4734 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004735 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 */
4737 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004738 ends_in_star = (len > 0 && retval[len - 1] == '*');
4739#ifndef BACKSLASH_IN_FILENAME
4740 for (i = len - 2; i >= 0; --i)
4741 {
4742 if (retval[i] != '\\')
4743 break;
4744 ends_in_star = !ends_in_star;
4745 }
4746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004748 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 && vim_strchr(tail, '$') == NULL
4750 && vim_strchr(retval, '`') == NULL)
4751 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004752 else if (len > 0 && retval[len - 1] == '$')
4753 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 retval[len] = NUL;
4755 }
4756 }
4757 return retval;
4758}
4759
4760/*
4761 * Must parse the command line so far to work out what context we are in.
4762 * Completion can then be done based on that context.
4763 * This routine sets the variables:
4764 * xp->xp_pattern The start of the pattern to be expanded within
4765 * the command line (ends at the cursor).
4766 * xp->xp_context The type of thing to expand. Will be one of:
4767 *
4768 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4769 * the command line, like an unknown command. Caller
4770 * should beep.
4771 * EXPAND_NOTHING Unrecognised context for completion, use char like
4772 * a normal char, rather than for completion. eg
4773 * :s/^I/
4774 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4775 * it.
4776 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4777 * EXPAND_FILES After command with XFILE set, or after setting
4778 * with P_EXPAND set. eg :e ^I, :w>>^I
4779 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4780 * when we know only directories are of interest. eg
4781 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004782 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4784 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4785 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4786 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4787 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4788 * EXPAND_EVENTS Complete event names
4789 * EXPAND_SYNTAX Complete :syntax command arguments
4790 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4791 * EXPAND_AUGROUP Complete autocommand group names
4792 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4793 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4794 * eg :unmap a^I , :cunab x^I
4795 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4796 * eg :call sub^I
4797 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4798 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4799 * names in expressions, eg :while s^I
4800 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004801 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 */
4803 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004804set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004806 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 if (ccline.cmdfirstc != ':'
4808#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004809 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004810 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811#endif
4812 )
4813 {
4814 xp->xp_context = EXPAND_NOTHING;
4815 return;
4816 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004817 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818}
4819
4820 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004821set_cmd_context(
4822 expand_T *xp,
4823 char_u *str, /* start of command line */
4824 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004825 int col, /* position of cursor */
4826 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827{
4828 int old_char = NUL;
4829 char_u *nextcomm;
4830
4831 /*
4832 * Avoid a UMR warning from Purify, only save the character if it has been
4833 * written before.
4834 */
4835 if (col < len)
4836 old_char = str[col];
4837 str[col] = NUL;
4838 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004839
4840#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004841 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004842 {
4843# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004844 /* pass CMD_SIZE because there is no real command */
4845 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004846# endif
4847 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004848 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004849 {
4850 xp->xp_context = ccline.xp_context;
4851 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004852# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004853 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004854# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004855 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004856 else
4857#endif
4858 while (nextcomm != NULL)
4859 nextcomm = set_one_cmd_context(xp, nextcomm);
4860
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004861 /* Store the string here so that call_user_expand_func() can get to them
4862 * easily. */
4863 xp->xp_line = str;
4864 xp->xp_col = col;
4865
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 str[col] = old_char;
4867}
4868
4869/*
4870 * Expand the command line "str" from context "xp".
4871 * "xp" must have been set by set_cmd_context().
4872 * xp->xp_pattern points into "str", to where the text that is to be expanded
4873 * starts.
4874 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4875 * cursor.
4876 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4877 * key that triggered expansion literally.
4878 * Returns EXPAND_OK otherwise.
4879 */
4880 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004881expand_cmdline(
4882 expand_T *xp,
4883 char_u *str, /* start of command line */
4884 int col, /* position of cursor */
4885 int *matchcount, /* return: nr of matches */
4886 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
4888 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004889 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4892 {
4893 beep_flush();
4894 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4895 }
4896 if (xp->xp_context == EXPAND_NOTHING)
4897 {
4898 /* Caller can use the character as a normal char instead */
4899 return EXPAND_NOTHING;
4900 }
4901
4902 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004903 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4904 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 if (file_str == NULL)
4906 return EXPAND_UNSUCCESSFUL;
4907
Bram Moolenaar94950a92010-12-02 16:01:29 +01004908 if (p_wic)
4909 options += WILD_ICASE;
4910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004912 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 {
4914 *matchcount = 0;
4915 *matches = NULL;
4916 }
4917 vim_free(file_str);
4918
4919 return EXPAND_OK;
4920}
4921
4922#ifdef FEAT_MULTI_LANG
4923/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004924 * Cleanup matches for help tags:
4925 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4926 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004928static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929
4930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004931cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
4933 int i, j;
4934 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004935 char_u buf[4];
4936 char_u *p = buf;
4937
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004938 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004939 {
4940 *p++ = '@';
4941 *p++ = p_hlg[0];
4942 *p++ = p_hlg[1];
4943 }
4944 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945
4946 for (i = 0; i < num_file; ++i)
4947 {
4948 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004949 if (len <= 0)
4950 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004951 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
4953 /* Sorting on priority means the same item in another language may
4954 * be anywhere. Search all items for a match up to the "@en". */
4955 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004956 if (j != i && (int)STRLEN(file[j]) == len + 3
4957 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 break;
4959 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004960 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 file[i][len] = NUL;
4962 }
4963 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004964
4965 if (*buf != NUL)
4966 for (i = 0; i < num_file; ++i)
4967 {
4968 len = (int)STRLEN(file[i]) - 3;
4969 if (len <= 0)
4970 continue;
4971 if (STRCMP(file[i] + len, buf) == 0)
4972 {
4973 /* remove the default language */
4974 file[i][len] = NUL;
4975 }
4976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977}
4978#endif
4979
4980/*
4981 * Do the expansion based on xp->xp_context and "pat".
4982 */
4983 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984ExpandFromContext(
4985 expand_T *xp,
4986 char_u *pat,
4987 int *num_file,
4988 char_u ***file,
4989 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
4991#ifdef FEAT_CMDL_COMPL
4992 regmatch_T regmatch;
4993#endif
4994 int ret;
4995 int flags;
4996
4997 flags = EW_DIR; /* include directories */
4998 if (options & WILD_LIST_NOTFOUND)
4999 flags |= EW_NOTFOUND;
5000 if (options & WILD_ADD_SLASH)
5001 flags |= EW_ADDSLASH;
5002 if (options & WILD_KEEP_ALL)
5003 flags |= EW_KEEPALL;
5004 if (options & WILD_SILENT)
5005 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005006 if (options & WILD_ALLLINKS)
5007 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005009 if (xp->xp_context == EXPAND_FILES
5010 || xp->xp_context == EXPAND_DIRECTORIES
5011 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 {
5013 /*
5014 * Expand file or directory names.
5015 */
5016 int free_pat = FALSE;
5017 int i;
5018
5019 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5020 * space */
5021 if (xp->xp_backslash != XP_BS_NONE)
5022 {
5023 free_pat = TRUE;
5024 pat = vim_strsave(pat);
5025 for (i = 0; pat[i]; ++i)
5026 if (pat[i] == '\\')
5027 {
5028 if (xp->xp_backslash == XP_BS_THREE
5029 && pat[i + 1] == '\\'
5030 && pat[i + 2] == '\\'
5031 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005032 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (xp->xp_backslash == XP_BS_ONE
5034 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005035 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 }
5038
5039 if (xp->xp_context == EXPAND_FILES)
5040 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005041 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5042 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 else
5044 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005045 if (options & WILD_ICASE)
5046 flags |= EW_ICASE;
5047
Bram Moolenaard7834d32009-12-02 16:14:36 +00005048 /* Expand wildcards, supporting %:h and the like. */
5049 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 if (free_pat)
5051 vim_free(pat);
5052 return ret;
5053 }
5054
5055 *file = (char_u **)"";
5056 *num_file = 0;
5057 if (xp->xp_context == EXPAND_HELP)
5058 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005059 /* With an empty argument we would get all the help tags, which is
5060 * very slow. Get matches for "help" instead. */
5061 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5062 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
5064#ifdef FEAT_MULTI_LANG
5065 cleanup_help_tags(*num_file, *file);
5066#endif
5067 return OK;
5068 }
5069 return FAIL;
5070 }
5071
5072#ifndef FEAT_CMDL_COMPL
5073 return FAIL;
5074#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005075 if (xp->xp_context == EXPAND_SHELLCMD)
5076 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (xp->xp_context == EXPAND_OLD_SETTING)
5078 return ExpandOldSetting(num_file, file);
5079 if (xp->xp_context == EXPAND_BUFFERS)
5080 return ExpandBufnames(pat, num_file, file, options);
5081 if (xp->xp_context == EXPAND_TAGS
5082 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5083 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5084 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005085 {
5086 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005087 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5088 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005091 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005092 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005093 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005094 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005095 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005096 {
5097 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005098 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005099 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005100 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005101 {
5102 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005103 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005104 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005105# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5106 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005107 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005108# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005109 if (xp->xp_context == EXPAND_PACKADD)
5110 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
5112 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5113 if (regmatch.regprog == NULL)
5114 return FAIL;
5115
5116 /* set ignore-case according to p_ic, p_scs and pat */
5117 regmatch.rm_ic = ignorecase(pat);
5118
5119 if (xp->xp_context == EXPAND_SETTINGS
5120 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5121 ret = ExpandSettings(xp, &regmatch, num_file, file);
5122 else if (xp->xp_context == EXPAND_MAPPINGS)
5123 ret = ExpandMappings(&regmatch, num_file, file);
5124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5125 else if (xp->xp_context == EXPAND_USER_DEFINED)
5126 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5127# endif
5128 else
5129 {
5130 static struct expgen
5131 {
5132 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005133 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005135 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 } tab[] =
5137 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005138 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5139 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005140 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005141 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005142#ifdef FEAT_CMDHIST
5143 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005146 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005147 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005148 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5149 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5150 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151#endif
5152#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005153 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5154 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5155 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5156 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157#endif
5158#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005159 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5160 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#endif
5162#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005163 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005165#ifdef FEAT_PROFILE
5166 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5167#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005168 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005169 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5170 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005171#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005172 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005173#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005174#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005175 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005176#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005177#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005178 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5181 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005182 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5183 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005185 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005186 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005187 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 };
5189 int i;
5190
5191 /*
5192 * Find a context in the table and call the ExpandGeneric() with the
5193 * right function to do the expansion.
5194 */
5195 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005196 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 if (xp->xp_context == tab[i].context)
5198 {
5199 if (tab[i].ic)
5200 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005201 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005202 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204 }
5205 }
5206
Bram Moolenaar473de612013-06-08 18:19:48 +02005207 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208
5209 return ret;
5210#endif /* FEAT_CMDL_COMPL */
5211}
5212
5213#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5214/*
5215 * Expand a list of names.
5216 *
5217 * Generic function for command line completion. It calls a function to
5218 * obtain strings, one by one. The strings are matched against a regexp
5219 * program. Matching strings are copied into an array, which is returned.
5220 *
5221 * Returns OK when no problems encountered, FAIL for error (out of memory).
5222 */
5223 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005224ExpandGeneric(
5225 expand_T *xp,
5226 regmatch_T *regmatch,
5227 int *num_file,
5228 char_u ***file,
5229 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005231 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232{
5233 int i;
5234 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005235 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 char_u *str;
5237
5238 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005239 * round == 0: count the number of matching names
5240 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005242 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
5244 for (i = 0; ; ++i)
5245 {
5246 str = (*func)(xp, i);
5247 if (str == NULL) /* end of list */
5248 break;
5249 if (*str == NUL) /* skip empty strings */
5250 continue;
5251
5252 if (vim_regexec(regmatch, str, (colnr_T)0))
5253 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005254 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005256 if (escaped)
5257 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5258 else
5259 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260 (*file)[count] = str;
5261#ifdef FEAT_MENU
5262 if (func == get_menu_names && str != NULL)
5263 {
5264 /* test for separator added by get_menu_names() */
5265 str += STRLEN(str) - 1;
5266 if (*str == '\001')
5267 *str = '.';
5268 }
5269#endif
5270 }
5271 ++count;
5272 }
5273 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005274 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 {
5276 if (count == 0)
5277 return OK;
5278 *num_file = count;
5279 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5280 if (*file == NULL)
5281 {
5282 *file = (char_u **)"";
5283 return FAIL;
5284 }
5285 count = 0;
5286 }
5287 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005288
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005289 /* Sort the results. Keep menu's in the specified order. */
5290 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005291 {
5292 if (xp->xp_context == EXPAND_EXPRESSION
5293 || xp->xp_context == EXPAND_FUNCTIONS
5294 || xp->xp_context == EXPAND_USER_FUNC)
5295 /* <SNR> functions should be sorted to the end. */
5296 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5297 sort_func_compare);
5298 else
5299 sort_strings(*file, *num_file);
5300 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005301
Bram Moolenaar4f688582007-07-24 12:34:30 +00005302#ifdef FEAT_CMDL_COMPL
5303 /* Reset the variables used for special highlight names expansion, so that
5304 * they don't show up when getting normal highlight names by ID. */
5305 reset_expand_highlight();
5306#endif
5307
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 return OK;
5309}
5310
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005311/*
5312 * Complete a shell command.
5313 * Returns FAIL or OK;
5314 */
5315 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316expand_shellcmd(
5317 char_u *filepat, /* pattern to match with command names */
5318 int *num_file, /* return: number of matches */
5319 char_u ***file, /* return: array with matches */
5320 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005321{
5322 char_u *pat;
5323 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005324 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005325 int mustfree = FALSE;
5326 garray_T ga;
5327 char_u *buf = alloc(MAXPATHL);
5328 size_t l;
5329 char_u *s, *e;
5330 int flags = flagsarg;
5331 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005332 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005333 hashtab_T found_ht;
5334 hashitem_T *hi;
5335 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005336
5337 if (buf == NULL)
5338 return FAIL;
5339
5340 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5341 * space */
5342 pat = vim_strsave(filepat);
5343 for (i = 0; pat[i]; ++i)
5344 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005345 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005346
Bram Moolenaarb5971142015-03-21 17:32:19 +01005347 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005348
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005349 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5350 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005351 path = (char_u *)".";
5352 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005353 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005354 /* For an absolute name we don't use $PATH. */
5355 if (!mch_isFullName(pat))
5356 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005357 if (path == NULL)
5358 path = (char_u *)"";
5359 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005360
5361 /*
5362 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005363 * collect them in "ga". When "." is not in $PATH also expand for the
5364 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005365 */
5366 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005367 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005368 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005369 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005370#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005371 e = vim_strchr(s, ';');
5372#else
5373 e = vim_strchr(s, ':');
5374#endif
5375 if (e == NULL)
5376 e = s + STRLEN(s);
5377
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005378 if (*s == NUL)
5379 {
5380 if (did_curdir)
5381 break;
5382 // Find directories in the current directory, path is empty.
5383 did_curdir = TRUE;
5384 flags |= EW_DIR;
5385 }
5386 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5387 {
5388 did_curdir = TRUE;
5389 flags |= EW_DIR;
5390 }
5391 else
5392 // Do not match directories inside a $PATH item.
5393 flags &= ~EW_DIR;
5394
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005395 l = e - s;
5396 if (l > MAXPATHL - 5)
5397 break;
5398 vim_strncpy(buf, s, l);
5399 add_pathsep(buf);
5400 l = STRLEN(buf);
5401 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5402
5403 /* Expand matches in one directory of $PATH. */
5404 ret = expand_wildcards(1, &buf, num_file, file, flags);
5405 if (ret == OK)
5406 {
5407 if (ga_grow(&ga, *num_file) == FAIL)
5408 FreeWild(*num_file, *file);
5409 else
5410 {
5411 for (i = 0; i < *num_file; ++i)
5412 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005413 char_u *name = (*file)[i];
5414
5415 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005416 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005417 // Check if this name was already found.
5418 hash = hash_hash(name + l);
5419 hi = hash_lookup(&found_ht, name + l, hash);
5420 if (HASHITEM_EMPTY(hi))
5421 {
5422 // Remove the path that was prepended.
5423 STRMOVE(name, name + l);
5424 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5425 hash_add_item(&found_ht, hi, name, hash);
5426 name = NULL;
5427 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005428 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005429 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005430 }
5431 vim_free(*file);
5432 }
5433 }
5434 if (*e != NUL)
5435 ++e;
5436 }
5437 *file = ga.ga_data;
5438 *num_file = ga.ga_len;
5439
5440 vim_free(buf);
5441 vim_free(pat);
5442 if (mustfree)
5443 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005444 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005445 return OK;
5446}
5447
5448
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5450/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005451 * Call "user_expand_func()" to invoke a user defined Vim script function and
5452 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005454 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005455call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005456 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005457 expand_T *xp,
5458 int *num_file,
5459 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005461 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005462 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005464 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005465 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005466 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467
Bram Moolenaarb7515462013-06-29 12:58:33 +02005468 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005469 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 *num_file = 0;
5471 *file = NULL;
5472
Bram Moolenaarb7515462013-06-29 12:58:33 +02005473 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005474 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005475 keep = ccline.cmdbuff[ccline.cmdlen];
5476 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005477 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005478
Bram Moolenaarffa96842018-06-12 22:05:14 +02005479 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5480
5481 args[0].v_type = VAR_STRING;
5482 args[0].vval.v_string = pat;
5483 args[1].v_type = VAR_STRING;
5484 args[1].vval.v_string = xp->xp_line;
5485 args[2].v_type = VAR_NUMBER;
5486 args[2].vval.v_number = xp->xp_col;
5487 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005489 /* Save the cmdline, we don't know what the function may do. */
5490 save_ccline = ccline;
5491 ccline.cmdbuff = NULL;
5492 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005494
Bram Moolenaarded27a12018-08-01 19:06:03 +02005495 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005496
5497 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005499 if (ccline.cmdbuff != NULL)
5500 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005501
Bram Moolenaarffa96842018-06-12 22:05:14 +02005502 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005503 return ret;
5504}
5505
5506/*
5507 * Expand names with a function defined by the user.
5508 */
5509 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005510ExpandUserDefined(
5511 expand_T *xp,
5512 regmatch_T *regmatch,
5513 int *num_file,
5514 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005515{
5516 char_u *retstr;
5517 char_u *s;
5518 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005519 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005520 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005521 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005522
5523 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5524 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 return FAIL;
5526
5527 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005528 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
5530 e = vim_strchr(s, '\n');
5531 if (e == NULL)
5532 e = s + STRLEN(s);
5533 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005534 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005536 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5537 *e = keep;
5538
5539 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005541 if (ga_grow(&ga, 1) == FAIL)
5542 break;
5543 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5544 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 }
5546
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 if (*e != NUL)
5548 ++e;
5549 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005550 vim_free(retstr);
5551 *file = ga.ga_data;
5552 *num_file = ga.ga_len;
5553 return OK;
5554}
5555
5556/*
5557 * Expand names with a list returned by a function defined by the user.
5558 */
5559 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ExpandUserList(
5561 expand_T *xp,
5562 int *num_file,
5563 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005564{
5565 list_T *retlist;
5566 listitem_T *li;
5567 garray_T ga;
5568
5569 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5570 if (retlist == NULL)
5571 return FAIL;
5572
5573 ga_init2(&ga, (int)sizeof(char *), 3);
5574 /* Loop over the items in the list. */
5575 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5576 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005577 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5578 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005579
5580 if (ga_grow(&ga, 1) == FAIL)
5581 break;
5582
5583 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005584 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005585 ++ga.ga_len;
5586 }
5587 list_unref(retlist);
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 *file = ga.ga_data;
5590 *num_file = ga.ga_len;
5591 return OK;
5592}
5593#endif
5594
5595/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005596 * Expand color scheme, compiler or filetype names.
5597 * Search from 'runtimepath':
5598 * 'runtimepath'/{dirnames}/{pat}.vim
5599 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5600 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5601 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5602 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005603 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 */
5605 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005606ExpandRTDir(
5607 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005608 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005609 int *num_file,
5610 char_u ***file,
5611 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 char_u *s;
5614 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005615 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005617 int i;
5618 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
5620 *num_file = 0;
5621 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005622 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005623 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005625 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005627 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5628 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005630 ga_clear_strings(&ga);
5631 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005633 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005634 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005635 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005637
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005638 if (flags & DIP_START) {
5639 for (i = 0; dirnames[i] != NULL; ++i)
5640 {
5641 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5642 if (s == NULL)
5643 {
5644 ga_clear_strings(&ga);
5645 return FAIL;
5646 }
5647 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5648 globpath(p_pp, s, &ga, 0);
5649 vim_free(s);
5650 }
5651 }
5652
5653 if (flags & DIP_OPT) {
5654 for (i = 0; dirnames[i] != NULL; ++i)
5655 {
5656 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5657 if (s == NULL)
5658 {
5659 ga_clear_strings(&ga);
5660 return FAIL;
5661 }
5662 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5663 globpath(p_pp, s, &ga, 0);
5664 vim_free(s);
5665 }
5666 }
5667
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005668 for (i = 0; i < ga.ga_len; ++i)
5669 {
5670 match = ((char_u **)ga.ga_data)[i];
5671 s = match;
5672 e = s + STRLEN(s);
5673 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5674 {
5675 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005676 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005677 if (s < match || vim_ispathsep(*s))
5678 break;
5679 ++s;
5680 *e = NUL;
5681 mch_memmove(match, s, e - s + 1);
5682 }
5683 }
5684
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005685 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005686 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005687
5688 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005689 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005690 remove_duplicates(&ga);
5691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 *file = ga.ga_data;
5693 *num_file = ga.ga_len;
5694 return OK;
5695}
5696
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005697/*
5698 * Expand loadplugin names:
5699 * 'packpath'/pack/ * /opt/{pat}
5700 */
5701 static int
5702ExpandPackAddDir(
5703 char_u *pat,
5704 int *num_file,
5705 char_u ***file)
5706{
5707 char_u *s;
5708 char_u *e;
5709 char_u *match;
5710 garray_T ga;
5711 int i;
5712 int pat_len;
5713
5714 *num_file = 0;
5715 *file = NULL;
5716 pat_len = (int)STRLEN(pat);
5717 ga_init2(&ga, (int)sizeof(char *), 10);
5718
5719 s = alloc((unsigned)(pat_len + 26));
5720 if (s == NULL)
5721 {
5722 ga_clear_strings(&ga);
5723 return FAIL;
5724 }
5725 sprintf((char *)s, "pack/*/opt/%s*", pat);
5726 globpath(p_pp, s, &ga, 0);
5727 vim_free(s);
5728
5729 for (i = 0; i < ga.ga_len; ++i)
5730 {
5731 match = ((char_u **)ga.ga_data)[i];
5732 s = gettail(match);
5733 e = s + STRLEN(s);
5734 mch_memmove(match, s, e - s + 1);
5735 }
5736
5737 if (ga.ga_len == 0)
5738 return FAIL;
5739
5740 /* Sort and remove duplicates which can happen when specifying multiple
5741 * directories in dirnames. */
5742 remove_duplicates(&ga);
5743
5744 *file = ga.ga_data;
5745 *num_file = ga.ga_len;
5746 return OK;
5747}
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749#endif
5750
5751#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5752/*
5753 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005754 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005756 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005757globpath(
5758 char_u *path,
5759 char_u *file,
5760 garray_T *ga,
5761 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762{
5763 expand_T xpc;
5764 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 int num_p;
5767 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768
5769 buf = alloc(MAXPATHL);
5770 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005771 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005773 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 /* Loop over all entries in {path}. */
5777 while (*path != NUL)
5778 {
5779 /* Copy one item of the path to buf[] and concatenate the file name. */
5780 copy_option_part(&path, buf, MAXPATHL, ",");
5781 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5782 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005783# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005784 /* Using the platform's path separator (\) makes vim incorrectly
5785 * treat it as an escape character, use '/' instead. */
5786 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5787 STRCAT(buf, "/");
5788# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005790# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005792 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5793 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005795 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005797 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 for (i = 0; i < num_p; ++i)
5800 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005801 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005802 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005803 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005806
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 FreeWild(num_p, p);
5808 }
5809 }
5810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811
5812 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
5815#endif
5816
5817#if defined(FEAT_CMDHIST) || defined(PROTO)
5818
5819/*********************************
5820 * Command line history stuff *
5821 *********************************/
5822
5823/*
5824 * Translate a history character to the associated type number.
5825 */
5826 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005827hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828{
5829 if (c == ':')
5830 return HIST_CMD;
5831 if (c == '=')
5832 return HIST_EXPR;
5833 if (c == '@')
5834 return HIST_INPUT;
5835 if (c == '>')
5836 return HIST_DEBUG;
5837 return HIST_SEARCH; /* must be '?' or '/' */
5838}
5839
5840/*
5841 * Table of history names.
5842 * These names are used in :history and various hist...() functions.
5843 * It is sufficient to give the significant prefix of a history name.
5844 */
5845
5846static char *(history_names[]) =
5847{
5848 "cmd",
5849 "search",
5850 "expr",
5851 "input",
5852 "debug",
5853 NULL
5854};
5855
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5857/*
5858 * Function given to ExpandGeneric() to obtain the possible first
5859 * arguments of the ":history command.
5860 */
5861 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005862get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005863{
5864 static char_u compl[2] = { NUL, NUL };
5865 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005866 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005867 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5868
5869 if (idx < short_names_count)
5870 {
5871 compl[0] = (char_u)short_names[idx];
5872 return compl;
5873 }
5874 if (idx < short_names_count + history_name_count)
5875 return (char_u *)history_names[idx - short_names_count];
5876 if (idx == short_names_count + history_name_count)
5877 return (char_u *)"all";
5878 return NULL;
5879}
5880#endif
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882/*
5883 * init_history() - Initialize the command line history.
5884 * Also used to re-allocate the history when the size changes.
5885 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005886 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005887init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888{
5889 int newlen; /* new length of history table */
5890 histentry_T *temp;
5891 int i;
5892 int j;
5893 int type;
5894
5895 /*
5896 * If size of history table changed, reallocate it
5897 */
5898 newlen = (int)p_hi;
5899 if (newlen != hislen) /* history length changed */
5900 {
5901 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5902 {
5903 if (newlen)
5904 {
5905 temp = (histentry_T *)lalloc(
5906 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5907 if (temp == NULL) /* out of memory! */
5908 {
5909 if (type == 0) /* first one: just keep the old length */
5910 {
5911 newlen = hislen;
5912 break;
5913 }
5914 /* Already changed one table, now we can only have zero
5915 * length for all tables. */
5916 newlen = 0;
5917 type = -1;
5918 continue;
5919 }
5920 }
5921 else
5922 temp = NULL;
5923 if (newlen == 0 || temp != NULL)
5924 {
5925 if (hisidx[type] < 0) /* there are no entries yet */
5926 {
5927 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005928 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 }
5930 else if (newlen > hislen) /* array becomes bigger */
5931 {
5932 for (i = 0; i <= hisidx[type]; ++i)
5933 temp[i] = history[type][i];
5934 j = i;
5935 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005936 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 for ( ; j < hislen; ++i, ++j)
5938 temp[i] = history[type][j];
5939 }
5940 else /* array becomes smaller or 0 */
5941 {
5942 j = hisidx[type];
5943 for (i = newlen - 1; ; --i)
5944 {
5945 if (i >= 0) /* copy newest entries */
5946 temp[i] = history[type][j];
5947 else /* remove older entries */
5948 vim_free(history[type][j].hisstr);
5949 if (--j < 0)
5950 j = hislen - 1;
5951 if (j == hisidx[type])
5952 break;
5953 }
5954 hisidx[type] = newlen - 1;
5955 }
5956 vim_free(history[type]);
5957 history[type] = temp;
5958 }
5959 }
5960 hislen = newlen;
5961 }
5962}
5963
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005964 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005966{
5967 hisptr->hisnum = 0;
5968 hisptr->viminfo = FALSE;
5969 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005970 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005971}
5972
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973/*
5974 * Check if command line 'str' is already in history.
5975 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5976 */
5977 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005978in_history(
5979 int type,
5980 char_u *str,
5981 int move_to_front, /* Move the entry to the front if it exists */
5982 int sep,
5983 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 int i;
5986 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005987 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988
5989 if (hisidx[type] < 0)
5990 return FALSE;
5991 i = hisidx[type];
5992 do
5993 {
5994 if (history[type][i].hisstr == NULL)
5995 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005996
5997 /* For search history, check that the separator character matches as
5998 * well. */
5999 p = history[type][i].hisstr;
6000 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006001 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006002 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 {
6004 if (!move_to_front)
6005 return TRUE;
6006 last_i = i;
6007 break;
6008 }
6009 if (--i < 0)
6010 i = hislen - 1;
6011 } while (i != hisidx[type]);
6012
6013 if (last_i >= 0)
6014 {
6015 str = history[type][i].hisstr;
6016 while (i != hisidx[type])
6017 {
6018 if (++i >= hislen)
6019 i = 0;
6020 history[type][last_i] = history[type][i];
6021 last_i = i;
6022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006023 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006024 history[type][i].viminfo = FALSE;
6025 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006026 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 return TRUE;
6028 }
6029 return FALSE;
6030}
6031
6032/*
6033 * Convert history name (from table above) to its HIST_ equivalent.
6034 * When "name" is empty, return "cmd" history.
6035 * Returns -1 for unknown history name.
6036 */
6037 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006038get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 int i;
6041 int len = (int)STRLEN(name);
6042
6043 /* No argument: use current history. */
6044 if (len == 0)
6045 return hist_char2type(ccline.cmdfirstc);
6046
6047 for (i = 0; history_names[i] != NULL; ++i)
6048 if (STRNICMP(name, history_names[i], len) == 0)
6049 return i;
6050
6051 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6052 return hist_char2type(name[0]);
6053
6054 return -1;
6055}
6056
6057static int last_maptick = -1; /* last seen maptick */
6058
6059/*
6060 * Add the given string to the given history. If the string is already in the
6061 * history then it is moved to the front. "histype" may be one of he HIST_
6062 * values.
6063 */
6064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006065add_to_history(
6066 int histype,
6067 char_u *new_entry,
6068 int in_map, /* consider maptick when inside a mapping */
6069 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
6071 histentry_T *hisptr;
6072 int len;
6073
6074 if (hislen == 0) /* no history */
6075 return;
6076
Bram Moolenaara939e432013-11-09 05:30:26 +01006077 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6078 return;
6079
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 /*
6081 * Searches inside the same mapping overwrite each other, so that only
6082 * the last line is kept. Be careful not to remove a line that was moved
6083 * down, only lines that were added.
6084 */
6085 if (histype == HIST_SEARCH && in_map)
6086 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006087 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 {
6089 /* Current line is from the same mapping, remove it */
6090 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6091 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006092 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 --hisnum[histype];
6094 if (--hisidx[HIST_SEARCH] < 0)
6095 hisidx[HIST_SEARCH] = hislen - 1;
6096 }
6097 last_maptick = -1;
6098 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006099 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 if (++hisidx[histype] == hislen)
6102 hisidx[histype] = 0;
6103 hisptr = &history[histype][hisidx[histype]];
6104 vim_free(hisptr->hisstr);
6105
6106 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006107 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6109 if (hisptr->hisstr != NULL)
6110 hisptr->hisstr[len + 1] = sep;
6111
6112 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006113 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006114 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 if (histype == HIST_SEARCH && in_map)
6116 last_maptick = maptick;
6117 }
6118}
6119
6120#if defined(FEAT_EVAL) || defined(PROTO)
6121
6122/*
6123 * Get identifier of newest history entry.
6124 * "histype" may be one of the HIST_ values.
6125 */
6126 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006127get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128{
6129 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6130 || hisidx[histype] < 0)
6131 return -1;
6132
6133 return history[histype][hisidx[histype]].hisnum;
6134}
6135
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006136/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 * Calculate history index from a number:
6138 * num > 0: seen as identifying number of a history entry
6139 * num < 0: relative position in history wrt newest entry
6140 * "histype" may be one of the HIST_ values.
6141 */
6142 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006143calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144{
6145 int i;
6146 histentry_T *hist;
6147 int wrapped = FALSE;
6148
6149 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6150 || (i = hisidx[histype]) < 0 || num == 0)
6151 return -1;
6152
6153 hist = history[histype];
6154 if (num > 0)
6155 {
6156 while (hist[i].hisnum > num)
6157 if (--i < 0)
6158 {
6159 if (wrapped)
6160 break;
6161 i += hislen;
6162 wrapped = TRUE;
6163 }
6164 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6165 return i;
6166 }
6167 else if (-num <= hislen)
6168 {
6169 i += num + 1;
6170 if (i < 0)
6171 i += hislen;
6172 if (hist[i].hisstr != NULL)
6173 return i;
6174 }
6175 return -1;
6176}
6177
6178/*
6179 * Get a history entry by its index.
6180 * "histype" may be one of the HIST_ values.
6181 */
6182 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006183get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184{
6185 idx = calc_hist_idx(histype, idx);
6186 if (idx >= 0)
6187 return history[histype][idx].hisstr;
6188 else
6189 return (char_u *)"";
6190}
6191
6192/*
6193 * Clear all entries of a history.
6194 * "histype" may be one of the HIST_ values.
6195 */
6196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006197clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int i;
6200 histentry_T *hisptr;
6201
6202 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6203 {
6204 hisptr = history[histype];
6205 for (i = hislen; i--;)
6206 {
6207 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006208 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006209 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210 }
6211 hisidx[histype] = -1; /* mark history as cleared */
6212 hisnum[histype] = 0; /* reset identifier counter */
6213 return OK;
6214 }
6215 return FAIL;
6216}
6217
6218/*
6219 * Remove all entries matching {str} from a history.
6220 * "histype" may be one of the HIST_ values.
6221 */
6222 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006223del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224{
6225 regmatch_T regmatch;
6226 histentry_T *hisptr;
6227 int idx;
6228 int i;
6229 int last;
6230 int found = FALSE;
6231
6232 regmatch.regprog = NULL;
6233 regmatch.rm_ic = FALSE; /* always match case */
6234 if (hislen != 0
6235 && histype >= 0
6236 && histype < HIST_COUNT
6237 && *str != NUL
6238 && (idx = hisidx[histype]) >= 0
6239 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6240 != NULL)
6241 {
6242 i = last = idx;
6243 do
6244 {
6245 hisptr = &history[histype][i];
6246 if (hisptr->hisstr == NULL)
6247 break;
6248 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6249 {
6250 found = TRUE;
6251 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006252 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 }
6254 else
6255 {
6256 if (i != last)
6257 {
6258 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006259 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
6261 if (--last < 0)
6262 last += hislen;
6263 }
6264 if (--i < 0)
6265 i += hislen;
6266 } while (i != idx);
6267 if (history[histype][idx].hisstr == NULL)
6268 hisidx[histype] = -1;
6269 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006270 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 return found;
6272}
6273
6274/*
6275 * Remove an indexed entry from a history.
6276 * "histype" may be one of the HIST_ values.
6277 */
6278 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006279del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280{
6281 int i, j;
6282
6283 i = calc_hist_idx(histype, idx);
6284 if (i < 0)
6285 return FALSE;
6286 idx = hisidx[histype];
6287 vim_free(history[histype][i].hisstr);
6288
6289 /* When deleting the last added search string in a mapping, reset
6290 * last_maptick, so that the last added search string isn't deleted again.
6291 */
6292 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6293 last_maptick = -1;
6294
6295 while (i != idx)
6296 {
6297 j = (i + 1) % hislen;
6298 history[histype][i] = history[histype][j];
6299 i = j;
6300 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006301 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 if (--i < 0)
6303 i += hislen;
6304 hisidx[histype] = i;
6305 return TRUE;
6306}
6307
6308#endif /* FEAT_EVAL */
6309
6310#if defined(FEAT_CRYPT) || defined(PROTO)
6311/*
6312 * Very specific function to remove the value in ":set key=val" from the
6313 * history.
6314 */
6315 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 char_u *p;
6319 int i;
6320
6321 i = hisidx[HIST_CMD];
6322 if (i < 0)
6323 return;
6324 p = history[HIST_CMD][i].hisstr;
6325 if (p != NULL)
6326 for ( ; *p; ++p)
6327 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6328 {
6329 p = vim_strchr(p + 3, '=');
6330 if (p == NULL)
6331 break;
6332 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006333 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 if (p[i] == '\\' && p[i + 1])
6335 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006336 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006337 --p;
6338 }
6339}
6340#endif
6341
6342#endif /* FEAT_CMDHIST */
6343
Bram Moolenaar064154c2016-03-19 22:50:43 +01006344#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006345/*
6346 * Get pointer to the command line info to use. cmdline_paste() may clear
6347 * ccline and put the previous value in prev_ccline.
6348 */
6349 static struct cmdline_info *
6350get_ccline_ptr(void)
6351{
6352 if ((State & CMDLINE) == 0)
6353 return NULL;
6354 if (ccline.cmdbuff != NULL)
6355 return &ccline;
6356 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6357 return &prev_ccline;
6358 return NULL;
6359}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006360#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006361
Bram Moolenaar064154c2016-03-19 22:50:43 +01006362#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006363/*
6364 * Get the current command line in allocated memory.
6365 * Only works when the command line is being edited.
6366 * Returns NULL when something is wrong.
6367 */
6368 char_u *
6369get_cmdline_str(void)
6370{
6371 struct cmdline_info *p = get_ccline_ptr();
6372
6373 if (p == NULL)
6374 return NULL;
6375 return vim_strnsave(p->cmdbuff, p->cmdlen);
6376}
6377
6378/*
6379 * Get the current command line position, counted in bytes.
6380 * Zero is the first position.
6381 * Only works when the command line is being edited.
6382 * Returns -1 when something is wrong.
6383 */
6384 int
6385get_cmdline_pos(void)
6386{
6387 struct cmdline_info *p = get_ccline_ptr();
6388
6389 if (p == NULL)
6390 return -1;
6391 return p->cmdpos;
6392}
6393
6394/*
6395 * Set the command line byte position to "pos". Zero is the first position.
6396 * Only works when the command line is being edited.
6397 * Returns 1 when failed, 0 when OK.
6398 */
6399 int
6400set_cmdline_pos(
6401 int pos)
6402{
6403 struct cmdline_info *p = get_ccline_ptr();
6404
6405 if (p == NULL)
6406 return 1;
6407
6408 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6409 * changed the command line. */
6410 if (pos < 0)
6411 new_cmdpos = 0;
6412 else
6413 new_cmdpos = pos;
6414 return 0;
6415}
6416#endif
6417
6418#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6419/*
6420 * Get the current command-line type.
6421 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6422 * Only works when the command line is being edited.
6423 * Returns NUL when something is wrong.
6424 */
6425 int
6426get_cmdline_type(void)
6427{
6428 struct cmdline_info *p = get_ccline_ptr();
6429
6430 if (p == NULL)
6431 return NUL;
6432 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006433 return
6434# ifdef FEAT_EVAL
6435 (p->input_fn) ? '@' :
6436# endif
6437 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006438 return p->cmdfirstc;
6439}
6440#endif
6441
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6443/*
6444 * Get indices "num1,num2" that specify a range within a list (not a range of
6445 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6446 * Returns OK if parsed successfully, otherwise FAIL.
6447 */
6448 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 int len;
6452 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006453 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 *str = skipwhite(*str);
6456 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6457 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006458 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006459 *str += len;
6460 *num1 = (int)num;
6461 first = TRUE;
6462 }
6463 *str = skipwhite(*str);
6464 if (**str == ',') /* parse "to" part of range */
6465 {
6466 *str = skipwhite(*str + 1);
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 if (len > 0)
6469 {
6470 *num2 = (int)num;
6471 *str = skipwhite(*str + len);
6472 }
6473 else if (!first) /* no number given at all */
6474 return FAIL;
6475 }
6476 else if (first) /* only one number given */
6477 *num2 = *num1;
6478 return OK;
6479}
6480#endif
6481
6482#if defined(FEAT_CMDHIST) || defined(PROTO)
6483/*
6484 * :history command - print a history
6485 */
6486 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006487ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488{
6489 histentry_T *hist;
6490 int histype1 = HIST_CMD;
6491 int histype2 = HIST_CMD;
6492 int hisidx1 = 1;
6493 int hisidx2 = -1;
6494 int idx;
6495 int i, j, k;
6496 char_u *end;
6497 char_u *arg = eap->arg;
6498
6499 if (hislen == 0)
6500 {
6501 MSG(_("'history' option is zero"));
6502 return;
6503 }
6504
6505 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6506 {
6507 end = arg;
6508 while (ASCII_ISALPHA(*end)
6509 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6510 end++;
6511 i = *end;
6512 *end = NUL;
6513 histype1 = get_histtype(arg);
6514 if (histype1 == -1)
6515 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006516 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 {
6518 histype1 = 0;
6519 histype2 = HIST_COUNT-1;
6520 }
6521 else
6522 {
6523 *end = i;
6524 EMSG(_(e_trailing));
6525 return;
6526 }
6527 }
6528 else
6529 histype2 = histype1;
6530 *end = i;
6531 }
6532 else
6533 end = arg;
6534 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6535 {
6536 EMSG(_(e_trailing));
6537 return;
6538 }
6539
6540 for (; !got_int && histype1 <= histype2; ++histype1)
6541 {
6542 STRCPY(IObuff, "\n # ");
6543 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6544 MSG_PUTS_TITLE(IObuff);
6545 idx = hisidx[histype1];
6546 hist = history[histype1];
6547 j = hisidx1;
6548 k = hisidx2;
6549 if (j < 0)
6550 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6551 if (k < 0)
6552 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6553 if (idx >= 0 && j <= k)
6554 for (i = idx + 1; !got_int; ++i)
6555 {
6556 if (i == hislen)
6557 i = 0;
6558 if (hist[i].hisstr != NULL
6559 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6560 {
6561 msg_putchar('\n');
6562 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6563 hist[i].hisnum);
6564 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6565 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006566 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 else
6568 STRCAT(IObuff, hist[i].hisstr);
6569 msg_outtrans(IObuff);
6570 out_flush();
6571 }
6572 if (i == idx)
6573 break;
6574 }
6575 }
6576}
6577#endif
6578
6579#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006580/*
6581 * Buffers for history read from a viminfo file. Only valid while reading.
6582 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006583static histentry_T *viminfo_history[HIST_COUNT] =
6584 {NULL, NULL, NULL, NULL, NULL};
6585static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6586static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587static int viminfo_add_at_front = FALSE;
6588
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006589static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591/*
6592 * Translate a history type number to the associated character.
6593 */
6594 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006595hist_type2char(
6596 int type,
6597 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598{
6599 if (type == HIST_CMD)
6600 return ':';
6601 if (type == HIST_SEARCH)
6602 {
6603 if (use_question)
6604 return '?';
6605 else
6606 return '/';
6607 }
6608 if (type == HIST_EXPR)
6609 return '=';
6610 return '@';
6611}
6612
6613/*
6614 * Prepare for reading the history from the viminfo file.
6615 * This allocates history arrays to store the read history lines.
6616 */
6617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006618prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 int i;
6621 int num;
6622 int type;
6623 int len;
6624
6625 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006626 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 if (asklen > hislen)
6628 asklen = hislen;
6629
6630 for (type = 0; type < HIST_COUNT; ++type)
6631 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006632 /* Count the number of empty spaces in the history list. Entries read
6633 * from viminfo previously are also considered empty. If there are
6634 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006635 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006636 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637 num++;
6638 len = asklen;
6639 if (num > len)
6640 len = num;
6641 if (len <= 0)
6642 viminfo_history[type] = NULL;
6643 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006644 viminfo_history[type] = (histentry_T *)lalloc(
6645 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 if (viminfo_history[type] == NULL)
6647 len = 0;
6648 viminfo_hislen[type] = len;
6649 viminfo_hisidx[type] = 0;
6650 }
6651}
6652
6653/*
6654 * Accept a line from the viminfo, store it in the history array when it's
6655 * new.
6656 */
6657 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006658read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659{
6660 int type;
6661 long_u len;
6662 char_u *val;
6663 char_u *p;
6664
6665 type = hist_char2type(virp->vir_line[0]);
6666 if (viminfo_hisidx[type] < viminfo_hislen[type])
6667 {
6668 val = viminfo_readstring(virp, 1, TRUE);
6669 if (val != NULL && *val != NUL)
6670 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006671 int sep = (*val == ' ' ? NUL : *val);
6672
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006674 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 {
6676 /* Need to re-allocate to append the separator byte. */
6677 len = STRLEN(val);
6678 p = lalloc(len + 2, TRUE);
6679 if (p != NULL)
6680 {
6681 if (type == HIST_SEARCH)
6682 {
6683 /* Search entry: Move the separator from the first
6684 * column to after the NUL. */
6685 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006686 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 }
6688 else
6689 {
6690 /* Not a search entry: No separator in the viminfo
6691 * file, add a NUL separator. */
6692 mch_memmove(p, val, (size_t)len + 1);
6693 p[len + 1] = NUL;
6694 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006695 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6696 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006697 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6698 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006699 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701 }
6702 }
6703 vim_free(val);
6704 }
6705 return viminfo_readline(virp);
6706}
6707
Bram Moolenaar07219f92013-04-14 23:19:36 +02006708/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006709 * Accept a new style history line from the viminfo, store it in the history
6710 * array when it's new.
6711 */
6712 void
6713handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006714 garray_T *values,
6715 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006716{
6717 int type;
6718 long_u len;
6719 char_u *val;
6720 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006721 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006722
6723 /* Check the format:
6724 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006725 if (values->ga_len < 4
6726 || vp[0].bv_type != BVAL_NR
6727 || vp[1].bv_type != BVAL_NR
6728 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6729 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006730 return;
6731
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006732 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006733 if (type >= HIST_COUNT)
6734 return;
6735 if (viminfo_hisidx[type] < viminfo_hislen[type])
6736 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006737 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006738 if (val != NULL && *val != NUL)
6739 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006740 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6741 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006742 int idx;
6743 int overwrite = FALSE;
6744
6745 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6746 {
6747 /* If lines were written by an older Vim we need to avoid
6748 * getting duplicates. See if the entry already exists. */
6749 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6750 {
6751 p = viminfo_history[type][idx].hisstr;
6752 if (STRCMP(val, p) == 0
6753 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6754 {
6755 overwrite = TRUE;
6756 break;
6757 }
6758 }
6759
6760 if (!overwrite)
6761 {
6762 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006763 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006764 p = lalloc(len + 2, TRUE);
6765 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006766 else
6767 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006768 if (p != NULL)
6769 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006770 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006771 if (!overwrite)
6772 {
6773 mch_memmove(p, val, (size_t)len + 1);
6774 /* Put the separator after the NUL. */
6775 p[len + 1] = sep;
6776 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006777 viminfo_history[type][idx].hisnum = 0;
6778 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779 viminfo_hisidx[type]++;
6780 }
6781 }
6782 }
6783 }
6784 }
6785}
6786
6787/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006788 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006789 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006790 static void
6791concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 int idx;
6794 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006795
6796 idx = hisidx[type] + viminfo_hisidx[type];
6797 if (idx >= hislen)
6798 idx -= hislen;
6799 else if (idx < 0)
6800 idx = hislen - 1;
6801 if (viminfo_add_at_front)
6802 hisidx[type] = idx;
6803 else
6804 {
6805 if (hisidx[type] == -1)
6806 hisidx[type] = hislen - 1;
6807 do
6808 {
6809 if (history[type][idx].hisstr != NULL
6810 || history[type][idx].viminfo)
6811 break;
6812 if (++idx == hislen)
6813 idx = 0;
6814 } while (idx != hisidx[type]);
6815 if (idx != hisidx[type] && --idx < 0)
6816 idx = hislen - 1;
6817 }
6818 for (i = 0; i < viminfo_hisidx[type]; i++)
6819 {
6820 vim_free(history[type][idx].hisstr);
6821 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6822 history[type][idx].viminfo = TRUE;
6823 history[type][idx].time_set = viminfo_history[type][i].time_set;
6824 if (--idx < 0)
6825 idx = hislen - 1;
6826 }
6827 idx += 1;
6828 idx %= hislen;
6829 for (i = 0; i < viminfo_hisidx[type]; i++)
6830 {
6831 history[type][idx++].hisnum = ++hisnum[type];
6832 idx %= hislen;
6833 }
6834}
6835
6836#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6837 static int
6838#ifdef __BORLANDC__
6839_RTLENTRYF
6840#endif
6841sort_hist(const void *s1, const void *s2)
6842{
6843 histentry_T *p1 = *(histentry_T **)s1;
6844 histentry_T *p2 = *(histentry_T **)s2;
6845
6846 if (p1->time_set < p2->time_set) return -1;
6847 if (p1->time_set > p2->time_set) return 1;
6848 return 0;
6849}
6850#endif
6851
6852/*
6853 * Merge history lines from viminfo and lines typed in this Vim based on the
6854 * timestamp;
6855 */
6856 static void
6857merge_history(int type)
6858{
6859 int max_len;
6860 histentry_T **tot_hist;
6861 histentry_T *new_hist;
6862 int i;
6863 int len;
6864
6865 /* Make one long list with all entries. */
6866 max_len = hislen + viminfo_hisidx[type];
6867 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6868 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6869 if (tot_hist == NULL || new_hist == NULL)
6870 {
6871 vim_free(tot_hist);
6872 vim_free(new_hist);
6873 return;
6874 }
6875 for (i = 0; i < viminfo_hisidx[type]; i++)
6876 tot_hist[i] = &viminfo_history[type][i];
6877 len = i;
6878 for (i = 0; i < hislen; i++)
6879 if (history[type][i].hisstr != NULL)
6880 tot_hist[len++] = &history[type][i];
6881
6882 /* Sort the list on timestamp. */
6883 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6884
6885 /* Keep the newest ones. */
6886 for (i = 0; i < hislen; i++)
6887 {
6888 if (i < len)
6889 {
6890 new_hist[i] = *tot_hist[i];
6891 tot_hist[i]->hisstr = NULL;
6892 if (new_hist[i].hisnum == 0)
6893 new_hist[i].hisnum = ++hisnum[type];
6894 }
6895 else
6896 clear_hist_entry(&new_hist[i]);
6897 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006898 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006899
6900 /* Free what is not kept. */
6901 for (i = 0; i < viminfo_hisidx[type]; i++)
6902 vim_free(viminfo_history[type][i].hisstr);
6903 for (i = 0; i < hislen; i++)
6904 vim_free(history[type][i].hisstr);
6905 vim_free(history[type]);
6906 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006907 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006908}
6909
6910/*
6911 * Finish reading history lines from viminfo. Not used when writing viminfo.
6912 */
6913 void
6914finish_viminfo_history(vir_T *virp)
6915{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006917 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918
6919 for (type = 0; type < HIST_COUNT; ++type)
6920 {
6921 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006922 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006923
6924 if (merge)
6925 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006927 concat_history(type);
6928
Bram Moolenaard23a8232018-02-10 18:45:26 +01006929 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006930 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 }
6932}
6933
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006934/*
6935 * Write history to viminfo file in "fp".
6936 * When "merge" is TRUE merge history lines with a previously read viminfo
6937 * file, data is in viminfo_history[].
6938 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006941write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942{
6943 int i;
6944 int type;
6945 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006946 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 init_history();
6949 if (hislen == 0)
6950 return;
6951 for (type = 0; type < HIST_COUNT; ++type)
6952 {
6953 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6954 if (num_saved == 0)
6955 continue;
6956 if (num_saved < 0) /* Use default */
6957 num_saved = hislen;
6958 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6959 type == HIST_CMD ? _("Command Line") :
6960 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006961 type == HIST_EXPR ? _("Expression") :
6962 type == HIST_INPUT ? _("Input Line") :
6963 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 if (num_saved > hislen)
6965 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006966
6967 /*
6968 * Merge typed and viminfo history:
6969 * round 1: history of typed commands.
6970 * round 2: history from recently read viminfo.
6971 */
6972 for (round = 1; round <= 2; ++round)
6973 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006974 if (round == 1)
6975 /* start at newest entry, somewhere in the list */
6976 i = hisidx[type];
6977 else if (viminfo_hisidx[type] > 0)
6978 /* start at newest entry, first in the list */
6979 i = 0;
6980 else
6981 /* empty list */
6982 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006983 if (i >= 0)
6984 while (num_saved > 0
6985 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006987 char_u *p;
6988 time_t timestamp;
6989 int c = NUL;
6990
6991 if (round == 1)
6992 {
6993 p = history[type][i].hisstr;
6994 timestamp = history[type][i].time_set;
6995 }
6996 else
6997 {
6998 p = viminfo_history[type] == NULL ? NULL
6999 : viminfo_history[type][i].hisstr;
7000 timestamp = viminfo_history[type] == NULL ? 0
7001 : viminfo_history[type][i].time_set;
7002 }
7003
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007004 if (p != NULL && (round == 2
7005 || !merge
7006 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007008 --num_saved;
7009 fputc(hist_type2char(type, TRUE), fp);
7010 /* For the search history: put the separator in the
7011 * second column; use a space if there isn't one. */
7012 if (type == HIST_SEARCH)
7013 {
7014 c = p[STRLEN(p) + 1];
7015 putc(c == NUL ? ' ' : c, fp);
7016 }
7017 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007018
7019 {
7020 char cbuf[NUMBUFLEN];
7021
7022 /* New style history with a bar line. Format:
7023 * |{bartype},{histtype},{timestamp},{separator},"text" */
7024 if (c == NUL)
7025 cbuf[0] = NUL;
7026 else
7027 sprintf(cbuf, "%d", c);
7028 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7029 type, (long)timestamp, cbuf);
7030 barline_writestring(fp, p, LSIZE - 20);
7031 putc('\n', fp);
7032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007034 if (round == 1)
7035 {
7036 /* Decrement index, loop around and stop when back at
7037 * the start. */
7038 if (--i < 0)
7039 i = hislen - 1;
7040 if (i == hisidx[type])
7041 break;
7042 }
7043 else
7044 {
7045 /* Increment index. Stop at the end in the while. */
7046 ++i;
7047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007049 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007050 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007051 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007052 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007053 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007054 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
7056}
7057#endif /* FEAT_VIMINFO */
7058
7059#if defined(FEAT_FKMAP) || defined(PROTO)
7060/*
7061 * Write a character at the current cursor+offset position.
7062 * It is directly written into the command buffer block.
7063 */
7064 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007065cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066{
7067 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7068 {
7069 EMSG(_("E198: cmd_pchar beyond the command length"));
7070 return;
7071 }
7072 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7073 ccline.cmdbuff[ccline.cmdlen] = NUL;
7074}
7075
7076 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007077cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078{
7079 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7080 {
7081 /* EMSG(_("cmd_gchar beyond the command length")); */
7082 return NUL;
7083 }
7084 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7085}
7086#endif
7087
7088#if defined(FEAT_CMDWIN) || defined(PROTO)
7089/*
7090 * Open a window on the current command line and history. Allow editing in
7091 * the window. Returns when the window is closed.
7092 * Returns:
7093 * CR if the command is to be executed
7094 * Ctrl_C if it is to be abandoned
7095 * K_IGNORE if editing continues
7096 */
7097 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007098open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
7100 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007101 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007103 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 win_T *wp;
7105 int i;
7106 linenr_T lnum;
7107 int histtype;
7108 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 int save_restart_edit = restart_edit;
7110 int save_State = State;
7111 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007112#ifdef FEAT_RIGHTLEFT
7113 int save_cmdmsg_rl = cmdmsg_rl;
7114#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007115#ifdef FEAT_FOLDING
7116 int save_KeyTyped;
7117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118
7119 /* Can't do this recursively. Can't do it when typing a password. */
7120 if (cmdwin_type != 0
7121# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7122 || cmdline_star > 0
7123# endif
7124 )
7125 {
7126 beep_flush();
7127 return K_IGNORE;
7128 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007129 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130
7131 /* Save current window sizes. */
7132 win_size_save(&winsizes);
7133
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007135 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007136
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007137 /* don't use a new tab page */
7138 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007139 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 /* Create a window for the command-line buffer. */
7142 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7143 {
7144 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007145 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 return K_IGNORE;
7147 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007148 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
7150 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007151 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007152 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007155#ifdef FEAT_FOLDING
7156 curwin->w_p_fen = FALSE;
7157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007159 curwin->w_p_rl = cmdmsg_rl;
7160 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007162 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007165 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007166 /* But don't allow switching to another buffer. */
7167 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaar46152342005-09-07 21:18:43 +00007169 /* Showing the prompt may have set need_wait_return, reset it. */
7170 need_wait_return = FALSE;
7171
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007172 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7174 {
7175 if (p_wc == TAB)
7176 {
7177 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7178 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7179 }
7180 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7181 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007182 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007184 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7185 * sets 'textwidth' to 78). */
7186 curbuf->b_p_tw = 0;
7187
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 /* Fill the buffer with the history. */
7189 init_history();
7190 if (hislen > 0)
7191 {
7192 i = hisidx[histtype];
7193 if (i >= 0)
7194 {
7195 lnum = 0;
7196 do
7197 {
7198 if (++i == hislen)
7199 i = 0;
7200 if (history[histtype][i].hisstr != NULL)
7201 ml_append(lnum++, history[histtype][i].hisstr,
7202 (colnr_T)0, FALSE);
7203 }
7204 while (i != hisidx[histtype]);
7205 }
7206 }
7207
7208 /* Replace the empty last line with the current command-line and put the
7209 * cursor there. */
7210 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7211 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7212 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007213 changed_line_abv_curs();
7214 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007215 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216
7217 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007218 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
7220 /* No Ex mode here! */
7221 exmode_active = 0;
7222
7223 State = NORMAL;
7224# ifdef FEAT_MOUSE
7225 setmouse();
7226# endif
7227
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007229 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007230 if (restart_edit != 0) /* autocmd with ":startinsert" */
7231 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232
7233 i = RedrawingDisabled;
7234 RedrawingDisabled = 0;
7235
7236 /*
7237 * Call the main loop until <CR> or CTRL-C is typed.
7238 */
7239 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007240 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
7242 RedrawingDisabled = i;
7243
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007244# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007245 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007246# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007247
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007249 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007250
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007251# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007252 /* Restore KeyTyped in case it is modified by autocommands */
7253 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007254# endif
7255
Bram Moolenaarccc18222007-05-10 18:25:20 +00007256 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007257 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 cmdwin_type = 0;
7259
7260 exmode_active = save_exmode;
7261
Bram Moolenaarf9821062008-06-20 16:31:07 +00007262 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007264 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 {
7266 cmdwin_result = Ctrl_C;
7267 EMSG(_("E199: Active window or buffer deleted"));
7268 }
7269 else
7270 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007271# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 /* autocmds may abort script processing */
7273 if (aborting() && cmdwin_result != K_IGNORE)
7274 cmdwin_result = Ctrl_C;
7275# endif
7276 /* Set the new command line from the cmdline buffer. */
7277 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007278 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007280 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7281
7282 if (histtype == HIST_CMD)
7283 {
7284 /* Execute the command directly. */
7285 ccline.cmdbuff = vim_strsave((char_u *)p);
7286 cmdwin_result = CAR;
7287 }
7288 else
7289 {
7290 /* First need to cancel what we were doing. */
7291 ccline.cmdbuff = NULL;
7292 stuffcharReadbuff(':');
7293 stuffReadbuff((char_u *)p);
7294 stuffcharReadbuff(CAR);
7295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 }
7297 else if (cmdwin_result == K_XF2) /* :qa typed */
7298 {
7299 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7300 cmdwin_result = CAR;
7301 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007302 else if (cmdwin_result == Ctrl_C)
7303 {
7304 /* :q or :close, don't execute any command
7305 * and don't modify the cmd window. */
7306 ccline.cmdbuff = NULL;
7307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 else
7309 ccline.cmdbuff = vim_strsave(ml_get_curline());
7310 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007311 {
7312 ccline.cmdbuff = vim_strsave((char_u *)"");
7313 ccline.cmdlen = 0;
7314 ccline.cmdbufflen = 1;
7315 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 else
7319 {
7320 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7321 ccline.cmdbufflen = ccline.cmdlen + 1;
7322 ccline.cmdpos = curwin->w_cursor.col;
7323 if (ccline.cmdpos > ccline.cmdlen)
7324 ccline.cmdpos = ccline.cmdlen;
7325 if (cmdwin_result == K_IGNORE)
7326 {
7327 set_cmdspos_cursor();
7328 redrawcmd();
7329 }
7330 }
7331
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007333 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007334# ifdef FEAT_CONCEAL
7335 /* Avoid command-line window first character being concealed. */
7336 curwin->w_p_cole = 0;
7337# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007338 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007339 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 win_goto(old_curwin);
7341 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007342
7343 /* win_close() may have already wiped the buffer when 'bh' is
7344 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007345 if (bufref_valid(&bufref))
7346 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 /* Restore window sizes. */
7349 win_size_restore(&winsizes);
7350
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007351 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 }
7353
7354 ga_clear(&winsizes);
7355 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007356# ifdef FEAT_RIGHTLEFT
7357 cmdmsg_rl = save_cmdmsg_rl;
7358# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359
7360 State = save_State;
7361# ifdef FEAT_MOUSE
7362 setmouse();
7363# endif
7364
7365 return cmdwin_result;
7366}
7367#endif /* FEAT_CMDWIN */
7368
7369/*
7370 * Used for commands that either take a simple command string argument, or:
7371 * cmd << endmarker
7372 * {script}
7373 * endmarker
7374 * Returns a pointer to allocated memory with {script} or NULL.
7375 */
7376 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007377script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378{
7379 char_u *theline;
7380 char *end_pattern = NULL;
7381 char dot[] = ".";
7382 garray_T ga;
7383
7384 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7385 return NULL;
7386
7387 ga_init2(&ga, 1, 0x400);
7388
7389 if (cmd[2] != NUL)
7390 end_pattern = (char *)skipwhite(cmd + 2);
7391 else
7392 end_pattern = dot;
7393
7394 for (;;)
7395 {
7396 theline = eap->getline(
7397#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007398 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399#endif
7400 NUL, eap->cookie, 0);
7401
7402 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007403 {
7404 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007407
7408 ga_concat(&ga, theline);
7409 ga_append(&ga, '\n');
7410 vim_free(theline);
7411 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007412 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413
7414 return (char_u *)ga.ga_data;
7415}