blob: 804013bb3875986b9493881166d62da71f82e2fd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
144
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200146 static void
147trigger_cmd_autocmd(int typechar, int evt)
148{
149 char_u typestr[2];
150
151 typestr[0] = typechar;
152 typestr[1] = NUL;
153 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
154}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200157 * Abandon the command line.
158 */
159 static void
160abandon_cmdline(void)
161{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100162 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200163 if (msg_scrolled == 0)
164 compute_cmdrow();
165 MSG("");
166 redraw_cmdline = TRUE;
167}
168
Bram Moolenaaree219b02017-12-17 14:55:01 +0100169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200170/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100171 * Guess that the pattern matches everything. Only finds specific cases, such
172 * as a trailing \|, which can happen while typing a pattern.
173 */
174 static int
175empty_pattern(char_u *p)
176{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100177 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100178
179 /* remove trailing \v and the like */
180 while (n >= 2 && p[n - 2] == '\\'
181 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
182 n -= 2;
183 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
184}
185
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200187typedef struct {
188 colnr_T vs_curswant;
189 colnr_T vs_leftcol;
190 linenr_T vs_topline;
191# ifdef FEAT_DIFF
192 int vs_topfill;
193# endif
194 linenr_T vs_botline;
195 linenr_T vs_empty_rows;
196} viewstate_T;
197
198 static void
199save_viewstate(viewstate_T *vs)
200{
201 vs->vs_curswant = curwin->w_curswant;
202 vs->vs_leftcol = curwin->w_leftcol;
203 vs->vs_topline = curwin->w_topline;
204# ifdef FEAT_DIFF
205 vs->vs_topfill = curwin->w_topfill;
206# endif
207 vs->vs_botline = curwin->w_botline;
208 vs->vs_empty_rows = curwin->w_empty_rows;
209}
210
211 static void
212restore_viewstate(viewstate_T *vs)
213{
214 curwin->w_curswant = vs->vs_curswant;
215 curwin->w_leftcol = vs->vs_leftcol;
216 curwin->w_topline = vs->vs_topline;
217# ifdef FEAT_DIFF
218 curwin->w_topfill = vs->vs_topfill;
219# endif
220 curwin->w_botline = vs->vs_botline;
221 curwin->w_empty_rows = vs->vs_empty_rows;
222}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223
224// Struct to store the state of 'incsearch' highlighting.
225typedef struct {
226 pos_T search_start; // where 'incsearch' starts searching
227 pos_T save_cursor;
228 viewstate_T init_viewstate;
229 viewstate_T old_viewstate;
230 pos_T match_start;
231 pos_T match_end;
232 int did_incsearch;
233 int incsearch_postponed;
234} incsearch_state_T;
235
236 static void
237init_incsearch_state(incsearch_state_T *is_state)
238{
239 is_state->match_start = curwin->w_cursor;
240 is_state->did_incsearch = FALSE;
241 is_state->incsearch_postponed = FALSE;
242 CLEAR_POS(&is_state->match_end);
243 is_state->save_cursor = curwin->w_cursor; // may be restored later
244 is_state->search_start = curwin->w_cursor;
245 save_viewstate(&is_state->init_viewstate);
246 save_viewstate(&is_state->old_viewstate);
247}
248
249/*
250 * First move cursor to end of match, then to the start. This
251 * moves the whole match onto the screen when 'nowrap' is set.
252 */
253 static void
254set_search_match(pos_T *t)
255{
256 t->lnum += search_match_lines;
257 t->col = search_match_endcol;
258 if (t->lnum > curbuf->b_ml.ml_line_count)
259 {
260 t->lnum = curbuf->b_ml.ml_line_count;
261 coladvance((colnr_T)MAXCOL);
262 }
263}
264
265/*
266 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200267 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200268 */
269 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200270do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
271 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200272{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
276 if (p_is && !cmd_silent)
277 {
278 // by default search all lines
279 search_first_line = 0;
280 search_last_line = MAXLNUM;
281
282 if (firstc == '/' || firstc == '?')
283 return TRUE;
284 if (firstc == ':')
285 {
286 char_u *cmd = skip_range(ccline.cmdbuff, NULL);
287 char_u *p;
288 int delim;
289 char_u *end;
290
291 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
292 {
293 // Skip over "substitute" to find the pattern separator.
294 for (p = cmd; ASCII_ISALPHA(*p); ++p)
295 ;
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200296 if (*p != NUL
297 && (STRNCMP(cmd, "substitute", p - cmd) == 0
298 || STRNCMP(cmd, "global", p - cmd) == 0
299 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200300 {
301 delim = *p++;
302 end = skip_regexp(p, delim, p_magic, NULL);
303 if (end > p)
304 {
305 char_u *dummy;
306 exarg_T ea;
307 pos_T save_cursor = curwin->w_cursor;
308
309 // found a non-empty pattern
310 *skiplen = (int)(p - ccline.cmdbuff);
311 *patlen = (int)(end - p);
312
313 // parse the address range
314 vim_memset(&ea, 0, sizeof(ea));
315 ea.line1 = 1;
316 ea.line2 = 1;
317 ea.cmd = ccline.cmdbuff;
318 ea.addr_type = ADDR_LINES;
319 parse_cmd_address(&ea, &dummy);
320 curwin->w_cursor = is_state->search_start;
321 if (ea.addr_count > 0)
322 {
323 search_first_line = ea.line1;
324 search_last_line = ea.line2;
325 }
326 else if (*cmd == 's')
327 {
328 // :s defaults to the current line
329 search_first_line = curwin->w_cursor.lnum;
330 search_last_line = curwin->w_cursor.lnum;
331 }
332
333 curwin->w_cursor = save_cursor;
334 return TRUE;
335 }
336 }
337 }
338 }
339 }
340
341 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200342}
343
344/*
345 * Do 'incsearch' highlighting if desired.
346 */
347 static void
348may_do_incsearch_highlighting(
349 int firstc,
350 long count,
351 incsearch_state_T *is_state)
352{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200353 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200354 int i;
355 pos_T end_pos;
356 struct cmdline_info save_ccline;
357#ifdef FEAT_RELTIME
358 proftime_T tm;
359#endif
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200360 int c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200361
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200362 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200363 return;
364
365 // If there is a character waiting, search and redraw later.
366 if (char_avail())
367 {
368 is_state->incsearch_postponed = TRUE;
369 return;
370 }
371 is_state->incsearch_postponed = FALSE;
372
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200373 if (search_first_line == 0)
374 // start at the original cursor position
375 curwin->w_cursor = is_state->search_start;
376 else
377 {
378 // start at the first line in the range
379 curwin->w_cursor.lnum = search_first_line;
380 curwin->w_cursor.col = 0;
381 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200382 save_last_search_pattern();
383
384 // If there is no command line, don't do anything.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200385 if (patlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200386 {
387 i = 0;
388 set_no_hlsearch(TRUE); // turn off previous highlight
389 redraw_all_later(SOME_VALID);
390 }
391 else
392 {
393 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
394
395 cursor_off(); // so the user knows we're busy
396 out_flush();
397 ++emsg_off; // so it doesn't beep if bad expr
398#ifdef FEAT_RELTIME
399 // Set the time limit to half a second.
400 profile_setlimit(500L, &tm);
401#endif
402 if (!p_hls)
403 search_flags += SEARCH_KEEP;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404 c = ccline.cmdbuff[skiplen + patlen];
405 ccline.cmdbuff[skiplen + patlen] = NUL;
406 i = do_search(NULL, firstc == ':' ? '/' : firstc,
407 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200408#ifdef FEAT_RELTIME
409 &tm, NULL
410#else
411 NULL, NULL
412#endif
413 );
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200414 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200415 --emsg_off;
416
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 if (curwin->w_cursor.lnum < search_first_line
418 || curwin->w_cursor.lnum > search_last_line)
419 // match outside of address range
420 i = 0;
421
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200422 // if interrupted while searching, behave like it failed
423 if (got_int)
424 {
425 (void)vpeekc(); // remove <C-C> from input stream
426 got_int = FALSE; // don't abandon the command line
427 i = 0;
428 }
429 else if (char_avail())
430 // cancelled searching because a char was typed
431 is_state->incsearch_postponed = TRUE;
432 }
433 if (i != 0)
434 highlight_match = TRUE; // highlight position
435 else
436 highlight_match = FALSE; // remove highlight
437
438 // First restore the old curwin values, so the screen is positioned in the
439 // same way as the actual search command.
440 restore_viewstate(&is_state->old_viewstate);
441 changed_cline_bef_curs();
442 update_topline();
443
444 if (i != 0)
445 {
446 pos_T save_pos = curwin->w_cursor;
447
448 is_state->match_start = curwin->w_cursor;
449 set_search_match(&curwin->w_cursor);
450 validate_cursor();
451 end_pos = curwin->w_cursor;
452 is_state->match_end = end_pos;
453 curwin->w_cursor = save_pos;
454 }
455 else
456 end_pos = curwin->w_cursor; // shutup gcc 4
457
458 // Disable 'hlsearch' highlighting if the pattern matches everything.
459 // Avoids a flash when typing "foo\|".
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200460 c = ccline.cmdbuff[skiplen + patlen];
461 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462 if (empty_pattern(ccline.cmdbuff))
463 set_no_hlsearch(TRUE);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200464 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465
466 validate_cursor();
467 // May redraw the status line to show the cursor position.
468 if (p_ru && curwin->w_status_height > 0)
469 curwin->w_redr_status = TRUE;
470
471 save_cmdline(&save_ccline);
472 update_screen(SOME_VALID);
473 restore_cmdline(&save_ccline);
474 restore_last_search_pattern();
475
476 // Leave it at the end to make CTRL-R CTRL-W work.
477 if (i != 0)
478 curwin->w_cursor = end_pos;
479
480 msg_starthere();
481 redrawcmdline();
482 is_state->did_incsearch = TRUE;
483}
484
485/*
486 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
487 * or previous match.
488 * Returns FAIL when jumping to cmdline_not_changed;
489 */
490 static int
491may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200492 int firstc,
493 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200494 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200495 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200496{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200497 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200498 pos_T t;
499 char_u *pat;
500 int search_flags = SEARCH_NOOF;
501 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200502 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200503
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200504 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200505 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200507 return FAIL;
508
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200509 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200510 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200511 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200512 skiplen = 0;
513 patlen = STRLEN(pat);
514 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200515 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200516 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200517
518 save_last_search_pattern();
519 cursor_off();
520 out_flush();
521 if (c == Ctrl_G)
522 {
523 t = is_state->match_end;
524 if (LT_POS(is_state->match_start, is_state->match_end))
525 // Start searching at the end of the match not at the beginning of
526 // the next column.
527 (void)decl(&t);
528 search_flags += SEARCH_COL;
529 }
530 else
531 t = is_state->match_start;
532 if (!p_hls)
533 search_flags += SEARCH_KEEP;
534 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200535 save = pat[patlen];
536 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200537 i = searchit(curwin, curbuf, &t,
538 c == Ctrl_G ? FORWARD : BACKWARD,
539 pat, count, search_flags,
540 RE_SEARCH, 0, NULL, NULL);
541 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200542 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200543 if (i)
544 {
545 is_state->search_start = is_state->match_start;
546 is_state->match_end = t;
547 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200548 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 {
550 // Move just before 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)decl(&is_state->search_start);
554 }
555 else if (c == Ctrl_G && firstc == '?')
556 {
557 // Move just after the current match, so that when nv_search
558 // finishes the cursor will be put back on the match.
559 is_state->search_start = t;
560 (void)incl(&is_state->search_start);
561 }
562 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
563 {
564 // wrap around
565 is_state->search_start = t;
566 if (firstc == '?')
567 (void)incl(&is_state->search_start);
568 else
569 (void)decl(&is_state->search_start);
570 }
571
572 set_search_match(&is_state->match_end);
573 curwin->w_cursor = is_state->match_start;
574 changed_cline_bef_curs();
575 update_topline();
576 validate_cursor();
577 highlight_match = TRUE;
578 save_viewstate(&is_state->old_viewstate);
579 update_screen(NOT_VALID);
580 redrawcmdline();
581 }
582 else
583 vim_beep(BO_ERROR);
584 restore_last_search_pattern();
585 return FAIL;
586}
587
588/*
589 * When CTRL-L typed: add character from the match to the pattern.
590 * May set "*c" to the added character.
591 * Return OK when jumping to cmdline_not_changed.
592 */
593 static int
594may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
595{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200596 int skiplen, patlen;
597
598 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 return FAIL;
600
601 // Add a character from under the cursor for 'incsearch'.
602 if (is_state->did_incsearch)
603 {
604 curwin->w_cursor = is_state->match_end;
605 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
606 {
607 *c = gchar_cursor();
608
609 // If 'ignorecase' and 'smartcase' are set and the
610 // command line has no uppercase characters, convert
611 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200612 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200613 *c = MB_TOLOWER(*c);
614 if (*c != NUL)
615 {
616 if (*c == firstc || vim_strchr((char_u *)(
617 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
618 {
619 // put a backslash before special characters
620 stuffcharReadbuff(*c);
621 *c = '\\';
622 }
623 return FAIL;
624 }
625 }
626 }
627 return OK;
628}
629
630 static void
631finish_incsearch_highlighting(int gotesc, incsearch_state_T *is_state)
632{
633 if (is_state->did_incsearch)
634 {
635 if (gotesc)
636 curwin->w_cursor = is_state->save_cursor;
637 else
638 {
639 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
640 {
641 // put the '" mark at the original position
642 curwin->w_cursor = is_state->save_cursor;
643 setpcmark();
644 }
645 curwin->w_cursor = is_state->search_start;
646 }
647 restore_viewstate(&is_state->old_viewstate);
648 highlight_match = FALSE;
649 validate_cursor(); /* needed for TAB */
650 redraw_all_later(SOME_VALID);
651 }
652}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200653#endif
654
Bram Moolenaar66216052017-12-16 16:33:44 +0100655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 * getcmdline() - accept a command line starting with firstc.
657 *
658 * firstc == ':' get ":" command line.
659 * firstc == '/' or '?' get search pattern
660 * firstc == '=' get expression
661 * firstc == '@' get text for input() function
662 * firstc == '>' get text for debug mode
663 * firstc == NUL get text for :insert command
664 * firstc == -1 like NUL, and break on CTRL-C
665 *
666 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
667 * command line.
668 *
669 * Careful: getcmdline() can be called recursively!
670 *
671 * Return pointer to allocated string if there is a commandline, NULL
672 * otherwise.
673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100675getcmdline(
676 int firstc,
677 long count UNUSED, /* only used for incremental search */
678 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679{
680 int c;
681 int i;
682 int j;
683 int gotesc = FALSE; /* TRUE when <ESC> just typed */
684 int do_abbr; /* when TRUE check for abbr. */
685#ifdef FEAT_CMDHIST
686 char_u *lookfor = NULL; /* string to match */
687 int hiscnt; /* current history line in use */
688 int histype; /* history type to be used */
689#endif
690#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200691 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#endif
693 int did_wild_list = FALSE; /* did wild_list() recently */
694 int wim_index = 0; /* index in wim_flags[] */
695 int res;
696 int save_msg_scroll = msg_scroll;
697 int save_State = State; /* remember State when called */
698 int some_key_typed = FALSE; /* one of the keys was typed */
699#ifdef FEAT_MOUSE
700 /* mouse drag and release events are ignored, unless they are
701 * preceded with a mouse down event */
702 int ignore_drag_release = TRUE;
703#endif
704#ifdef FEAT_EVAL
705 int break_ctrl_c = FALSE;
706#endif
707 expand_T xpc;
708 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200709#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000710 /* Everything that may work recursively should save and restore the
711 * current command line in save_ccline. That includes update_screen(), a
712 * custom status line may invoke ":normal". */
713 struct cmdline_info save_ccline;
714#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200715 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#ifdef FEAT_EVAL
718 if (firstc == -1)
719 {
720 firstc = NUL;
721 break_ctrl_c = TRUE;
722 }
723#endif
724#ifdef FEAT_RIGHTLEFT
725 /* start without Hebrew mapping for a command line */
726 if (firstc == ':' || firstc == '=' || firstc == '>')
727 cmd_hkmap = 0;
728#endif
729
730 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200731
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200733 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734#endif
735
736 /*
737 * set some variables for redrawcmd()
738 */
739 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000740 ccline.cmdindent = (firstc > 0 ? indent : 0);
741
742 /* alloc initial ccline.cmdbuff */
743 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 if (ccline.cmdbuff == NULL)
745 return NULL; /* out of memory */
746 ccline.cmdlen = ccline.cmdpos = 0;
747 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100748 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000750 /* autoindent for :insert and :append */
751 if (firstc <= 0)
752 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200753 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000754 ccline.cmdbuff[indent] = NUL;
755 ccline.cmdpos = indent;
756 ccline.cmdspos = indent;
757 ccline.cmdlen = indent;
758 }
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000761 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762
763#ifdef FEAT_RIGHTLEFT
764 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
765 && (firstc == '/' || firstc == '?'))
766 cmdmsg_rl = TRUE;
767 else
768 cmdmsg_rl = FALSE;
769#endif
770
771 redir_off = TRUE; /* don't redirect the typed command */
772 if (!cmd_silent)
773 {
774 i = msg_scrolled;
775 msg_scrolled = 0; /* avoid wait_return message */
776 gotocmdline(TRUE);
777 msg_scrolled += i;
778 redrawcmdprompt(); /* draw prompt or indent */
779 set_cmdspos();
780 }
781 xpc.xp_context = EXPAND_NOTHING;
782 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000783#ifndef BACKSLASH_IN_FILENAME
784 xpc.xp_shell = FALSE;
785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000787#if defined(FEAT_EVAL)
788 if (ccline.input_fn)
789 {
790 xpc.xp_context = ccline.xp_context;
791 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000792# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000793 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000794# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000795 }
796#endif
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /*
799 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
800 * doing ":@0" when register 0 doesn't contain a CR.
801 */
802 msg_scroll = FALSE;
803
804 State = CMDLINE;
805
806 if (firstc == '/' || firstc == '?' || firstc == '@')
807 {
808 /* Use ":lmap" mappings for search pattern and input(). */
809 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
810 b_im_ptr = &curbuf->b_p_iminsert;
811 else
812 b_im_ptr = &curbuf->b_p_imsearch;
813 if (*b_im_ptr == B_IMODE_LMAP)
814 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100815#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 im_set_active(*b_im_ptr == B_IMODE_IM);
817#endif
818 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100819#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 else if (p_imcmdline)
821 im_set_active(TRUE);
822#endif
823
824#ifdef FEAT_MOUSE
825 setmouse();
826#endif
827#ifdef CURSOR_SHAPE
828 ui_cursor_shape(); /* may show different cursor shape */
829#endif
830
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000831 /* When inside an autocommand for writing "exiting" may be set and
832 * terminal mode set to cooked. Need to set raw mode here then. */
833 settmode(TMODE_RAW);
834
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200835 /* Trigger CmdlineEnter autocommands. */
836 cmdline_type = firstc == NUL ? '-' : firstc;
837 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200838
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_CMDHIST
840 init_history();
841 hiscnt = hislen; /* set hiscnt to impossible history value */
842 histype = hist_char2type(firstc);
843#endif
844
845#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000846 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#endif
848
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200849 /* If something above caused an error, reset the flags, we do want to type
850 * and execute commands. Display may be messed up a bit. */
851 if (did_emsg)
852 redrawcmd();
853 did_emsg = FALSE;
854 got_int = FALSE;
855
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 /*
857 * Collect the command string, handling editing keys.
858 */
859 for (;;)
860 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000861 redir_off = TRUE; /* Don't redirect the typed command.
862 Repeated, because a ":redir" inside
863 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef USE_ON_FLY_SCROLL
865 dont_scroll = FALSE; /* allow scrolling here */
866#endif
867 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
868
Bram Moolenaar72532d32018-04-07 19:09:09 +0200869 did_emsg = FALSE; /* There can't really be a reason why an error
870 that occurs while typing a command should
871 cause the command not to be executed. */
872
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000874
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100875 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
876 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000877 do
878 {
879 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100880 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 if (KeyTyped)
883 {
884 some_key_typed = TRUE;
885#ifdef FEAT_RIGHTLEFT
886 if (cmd_hkmap)
887 c = hkmap(c);
888# ifdef FEAT_FKMAP
889 if (cmd_fkmap)
890 c = cmdl_fkmap(c);
891# endif
892 if (cmdmsg_rl && !KeyStuffed)
893 {
894 /* Invert horizontal movements and operations. Only when
895 * typed by the user directly, not when the result of a
896 * mapping. */
897 switch (c)
898 {
899 case K_RIGHT: c = K_LEFT; break;
900 case K_S_RIGHT: c = K_S_LEFT; break;
901 case K_C_RIGHT: c = K_C_LEFT; break;
902 case K_LEFT: c = K_RIGHT; break;
903 case K_S_LEFT: c = K_S_RIGHT; break;
904 case K_C_LEFT: c = K_C_RIGHT; break;
905 }
906 }
907#endif
908 }
909
910 /*
911 * Ignore got_int when CTRL-C was typed here.
912 * Don't ignore it in :global, we really need to break then, e.g., for
913 * ":g/pat/normal /pat" (without the <CR>).
914 * Don't ignore it for the input() function.
915 */
916 if ((c == Ctrl_C
917#ifdef UNIX
918 || c == intr_char
919#endif
920 )
921#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
922 && firstc != '@'
923#endif
924#ifdef FEAT_EVAL
925 && !break_ctrl_c
926#endif
927 && !global_busy)
928 got_int = FALSE;
929
930#ifdef FEAT_CMDHIST
931 /* free old command line when finished moving around in the history
932 * list */
933 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000934 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000935 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 && c != K_PAGEDOWN && c != K_PAGEUP
937 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000938 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100940 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#endif
942
943 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000944 * When there are matching completions to select <S-Tab> works like
945 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000947 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 c = Ctrl_P;
949
950#ifdef FEAT_WILDMENU
951 /* Special translations for 'wildmenu' */
952 if (did_wild_list && p_wmnu)
953 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000954 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000955 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000956 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 c = Ctrl_N;
958 }
959 /* Hitting CR after "emenu Name.": complete submenu */
960 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
961 && ccline.cmdpos > 1
962 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
963 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
964 && (c == '\n' || c == '\r' || c == K_KENTER))
965 c = K_DOWN;
966#endif
967
968 /* free expanded names when finished walking through matches */
969 if (xpc.xp_numfiles != -1
970 && !(c == p_wc && KeyTyped) && c != p_wcm
971 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
972 && c != Ctrl_L)
973 {
974 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
975 did_wild_list = FALSE;
976#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000977 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978#endif
979 xpc.xp_context = EXPAND_NOTHING;
980 wim_index = 0;
981#ifdef FEAT_WILDMENU
982 if (p_wmnu && wild_menu_showing != 0)
983 {
984 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000985 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000986
987 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000988 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 if (wild_menu_showing == WM_SCROLLED)
991 {
992 /* Entered command line, move it up */
993 cmdline_row--;
994 redrawcmd();
995 }
996 else if (save_p_ls != -1)
997 {
998 /* restore 'laststatus' and 'winminheight' */
999 p_ls = save_p_ls;
1000 p_wmh = save_p_wmh;
1001 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001002 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001004 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 redrawcmd();
1006 save_p_ls = -1;
1007 }
1008 else
1009 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 redraw_statuslines();
1012 }
1013 KeyTyped = skt;
1014 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001015 if (ccline.input_fn)
1016 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 }
1018#endif
1019 }
1020
1021#ifdef FEAT_WILDMENU
1022 /* Special translations for 'wildmenu' */
1023 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1024 {
1025 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001026 if (c == K_DOWN && ccline.cmdpos > 0
1027 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001029 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 {
1031 /* Hitting <Up>: Remove one submenu name in front of the
1032 * cursor */
1033 int found = FALSE;
1034
1035 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1036 i = 0;
1037 while (--j > 0)
1038 {
1039 /* check for start of menu name */
1040 if (ccline.cmdbuff[j] == ' '
1041 && ccline.cmdbuff[j - 1] != '\\')
1042 {
1043 i = j + 1;
1044 break;
1045 }
1046 /* check for start of submenu name */
1047 if (ccline.cmdbuff[j] == '.'
1048 && ccline.cmdbuff[j - 1] != '\\')
1049 {
1050 if (found)
1051 {
1052 i = j + 1;
1053 break;
1054 }
1055 else
1056 found = TRUE;
1057 }
1058 }
1059 if (i > 0)
1060 cmdline_del(i);
1061 c = p_wc;
1062 xpc.xp_context = EXPAND_NOTHING;
1063 }
1064 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001065 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001066 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001067 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 {
1069 char_u upseg[5];
1070
1071 upseg[0] = PATHSEP;
1072 upseg[1] = '.';
1073 upseg[2] = '.';
1074 upseg[3] = PATHSEP;
1075 upseg[4] = NUL;
1076
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001077 if (c == K_DOWN
1078 && ccline.cmdpos > 0
1079 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1080 && (ccline.cmdpos < 3
1081 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1083 {
1084 /* go down a directory */
1085 c = p_wc;
1086 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001087 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
1089 /* If in a direct ancestor, strip off one ../ to go down */
1090 int found = FALSE;
1091
1092 j = ccline.cmdpos;
1093 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1094 while (--j > i)
1095 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001096#ifdef FEAT_MBYTE
1097 if (has_mbyte)
1098 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 if (vim_ispathsep(ccline.cmdbuff[j]))
1101 {
1102 found = TRUE;
1103 break;
1104 }
1105 }
1106 if (found
1107 && ccline.cmdbuff[j - 1] == '.'
1108 && ccline.cmdbuff[j - 2] == '.'
1109 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1110 {
1111 cmdline_del(j - 2);
1112 c = p_wc;
1113 }
1114 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001115 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 {
1117 /* go up a directory */
1118 int found = FALSE;
1119
1120 j = ccline.cmdpos - 1;
1121 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1122 while (--j > i)
1123 {
1124#ifdef FEAT_MBYTE
1125 if (has_mbyte)
1126 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1127#endif
1128 if (vim_ispathsep(ccline.cmdbuff[j])
1129#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001130 && vim_strchr((char_u *)" *?[{`$%#",
1131 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132#endif
1133 )
1134 {
1135 if (found)
1136 {
1137 i = j + 1;
1138 break;
1139 }
1140 else
1141 found = TRUE;
1142 }
1143 }
1144
1145 if (!found)
1146 j = i;
1147 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1148 j += 4;
1149 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1150 && j == i)
1151 j += 3;
1152 else
1153 j = 0;
1154 if (j > 0)
1155 {
1156 /* TODO this is only for DOS/UNIX systems - need to put in
1157 * machine-specific stuff here and in upseg init */
1158 cmdline_del(j);
1159 put_on_cmdline(upseg + 1, 3, FALSE);
1160 }
1161 else if (ccline.cmdpos > i)
1162 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001163
1164 /* Now complete in the new directory. Set KeyTyped in case the
1165 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001167 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 }
1169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170
1171#endif /* FEAT_WILDMENU */
1172
1173 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1174 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1175 if (c == Ctrl_BSL)
1176 {
1177 ++no_mapping;
1178 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001179 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 --no_mapping;
1181 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001182 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1183 * is in a mapping. */
1184 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1185 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 {
1187 vungetc(c);
1188 c = Ctrl_BSL;
1189 }
1190#ifdef FEAT_EVAL
1191 else if (c == 'e')
1192 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001193 char_u *p = NULL;
1194 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195
1196 /*
1197 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001198 * Need to save and restore the current command line, to be
1199 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 */
1201 if (ccline.cmdpos == ccline.cmdlen)
1202 new_cmdpos = 99999; /* keep it at the end */
1203 else
1204 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001205
1206 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001208 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 if (c == '=')
1210 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001211 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001212 * to avoid nasty things like going to another buffer when
1213 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001214 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001215 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001217 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001218 restore_cmdline(&save_ccline);
1219
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001220 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001222 len = (int)STRLEN(p);
1223 if (realloc_cmdbuff(len + 1) == OK)
1224 {
1225 ccline.cmdlen = len;
1226 STRCPY(ccline.cmdbuff, p);
1227 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001229 /* Restore the cursor or use the position set with
1230 * set_cmdline_pos(). */
1231 if (new_cmdpos > ccline.cmdlen)
1232 ccline.cmdpos = ccline.cmdlen;
1233 else
1234 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001236 KeyTyped = FALSE; /* Don't do p_wc completion. */
1237 redrawcmd();
1238 goto cmdline_changed;
1239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 }
1241 }
1242 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001243 got_int = FALSE; /* don't abandon the command line */
1244 did_emsg = FALSE;
1245 emsg_on_display = FALSE;
1246 redrawcmd();
1247 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249#endif
1250 else
1251 {
1252 if (c == Ctrl_G && p_im && restart_edit == 0)
1253 restart_edit = 'a';
1254 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1255 in history */
1256 goto returncmd; /* back to Normal mode */
1257 }
1258 }
1259
1260#ifdef FEAT_CMDWIN
1261 if (c == cedit_key || c == K_CMDWIN)
1262 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001263 if (ex_normal_busy == 0 && got_int == FALSE)
1264 {
1265 /*
1266 * Open a window to edit the command line (and history).
1267 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001268 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001269 some_key_typed = TRUE;
1270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 }
1272# ifdef FEAT_DIGRAPHS
1273 else
1274# endif
1275#endif
1276#ifdef FEAT_DIGRAPHS
1277 c = do_digraph(c);
1278#endif
1279
1280 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1281 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1282 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001283 /* In Ex mode a backslash escapes a newline. */
1284 if (exmode_active
1285 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001286 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001287 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001288 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001290 if (c == K_KENTER)
1291 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001293 else
1294 {
1295 gotesc = FALSE; /* Might have typed ESC previously, don't
1296 truncate the cmdline now. */
1297 if (ccheck_abbr(c + ABBR_OFF))
1298 goto cmdline_changed;
1299 if (!cmd_silent)
1300 {
1301 windgoto(msg_row, 0);
1302 out_flush();
1303 }
1304 break;
1305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307
1308 /*
1309 * Completion for 'wildchar' or 'wildcharm' key.
1310 * - hitting <ESC> twice means: abandon command line.
1311 * - wildcard expansion is only done when the 'wildchar' key is really
1312 * typed, not when it comes from a macro
1313 */
1314 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1315 {
1316 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1317 {
1318 /* if 'wildmode' contains "list" may still need to list */
1319 if (xpc.xp_numfiles > 1
1320 && !did_wild_list
1321 && (wim_flags[wim_index] & WIM_LIST))
1322 {
1323 (void)showmatches(&xpc, FALSE);
1324 redrawcmd();
1325 did_wild_list = TRUE;
1326 }
1327 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001328 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1329 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001331 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1332 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 else
1334 res = OK; /* don't insert 'wildchar' now */
1335 }
1336 else /* typed p_wc first time */
1337 {
1338 wim_index = 0;
1339 j = ccline.cmdpos;
1340 /* if 'wildmode' first contains "longest", get longest
1341 * common part */
1342 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001343 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1344 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001346 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1347 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
1349 /* if interrupted while completing, behave like it failed */
1350 if (got_int)
1351 {
1352 (void)vpeekc(); /* remove <C-C> from input stream */
1353 got_int = FALSE; /* don't abandon the command line */
1354 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1355#ifdef FEAT_WILDMENU
1356 xpc.xp_context = EXPAND_NOTHING;
1357#endif
1358 goto cmdline_changed;
1359 }
1360
1361 /* when more than one match, and 'wildmode' first contains
1362 * "list", or no change and 'wildmode' contains "longest,list",
1363 * list all matches */
1364 if (res == OK && xpc.xp_numfiles > 1)
1365 {
1366 /* a "longest" that didn't do anything is skipped (but not
1367 * "list:longest") */
1368 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1369 wim_index = 1;
1370 if ((wim_flags[wim_index] & WIM_LIST)
1371#ifdef FEAT_WILDMENU
1372 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1373#endif
1374 )
1375 {
1376 if (!(wim_flags[0] & WIM_LONGEST))
1377 {
1378#ifdef FEAT_WILDMENU
1379 int p_wmnu_save = p_wmnu;
1380 p_wmnu = 0;
1381#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001382 /* remove match */
1383 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384#ifdef FEAT_WILDMENU
1385 p_wmnu = p_wmnu_save;
1386#endif
1387 }
1388#ifdef FEAT_WILDMENU
1389 (void)showmatches(&xpc, p_wmnu
1390 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1391#else
1392 (void)showmatches(&xpc, FALSE);
1393#endif
1394 redrawcmd();
1395 did_wild_list = TRUE;
1396 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001397 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1398 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001400 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1401 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001404 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 }
1406#ifdef FEAT_WILDMENU
1407 else if (xpc.xp_numfiles == -1)
1408 xpc.xp_context = EXPAND_NOTHING;
1409#endif
1410 }
1411 if (wim_index < 3)
1412 ++wim_index;
1413 if (c == ESC)
1414 gotesc = TRUE;
1415 if (res == OK)
1416 goto cmdline_changed;
1417 }
1418
1419 gotesc = FALSE;
1420
1421 /* <S-Tab> goes to last match, in a clumsy way */
1422 if (c == K_S_TAB && KeyTyped)
1423 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001424 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1425 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1426 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 goto cmdline_changed;
1428 }
1429
1430 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1431 c = NL;
1432
1433 do_abbr = TRUE; /* default: check for abbreviation */
1434
1435 /*
1436 * Big switch for a typed command line character.
1437 */
1438 switch (c)
1439 {
1440 case K_BS:
1441 case Ctrl_H:
1442 case K_DEL:
1443 case K_KDEL:
1444 case Ctrl_W:
1445#ifdef FEAT_FKMAP
1446 if (cmd_fkmap && c == K_BS)
1447 c = K_DEL;
1448#endif
1449 if (c == K_KDEL)
1450 c = K_DEL;
1451
1452 /*
1453 * delete current character is the same as backspace on next
1454 * character, except at end of line
1455 */
1456 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1457 ++ccline.cmdpos;
1458#ifdef FEAT_MBYTE
1459 if (has_mbyte && c == K_DEL)
1460 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1461 ccline.cmdbuff + ccline.cmdpos);
1462#endif
1463 if (ccline.cmdpos > 0)
1464 {
1465 char_u *p;
1466
1467 j = ccline.cmdpos;
1468 p = ccline.cmdbuff + j;
1469#ifdef FEAT_MBYTE
1470 if (has_mbyte)
1471 {
1472 p = mb_prevptr(ccline.cmdbuff, p);
1473 if (c == Ctrl_W)
1474 {
1475 while (p > ccline.cmdbuff && vim_isspace(*p))
1476 p = mb_prevptr(ccline.cmdbuff, p);
1477 i = mb_get_class(p);
1478 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1479 p = mb_prevptr(ccline.cmdbuff, p);
1480 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001481 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 }
1484 else
1485#endif
1486 if (c == Ctrl_W)
1487 {
1488 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1489 --p;
1490 i = vim_iswordc(p[-1]);
1491 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1492 && vim_iswordc(p[-1]) == i)
1493 --p;
1494 }
1495 else
1496 --p;
1497 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1498 ccline.cmdlen -= j - ccline.cmdpos;
1499 i = ccline.cmdpos;
1500 while (i < ccline.cmdlen)
1501 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1502
1503 /* Truncate at the end, required for multi-byte chars. */
1504 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001505#ifdef FEAT_SEARCH_EXTRA
1506 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001507 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001508 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001509 /* save view settings, so that the screen
1510 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001511 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001512 }
1513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 redrawcmd();
1515 }
1516 else if (ccline.cmdlen == 0 && c != Ctrl_W
1517 && ccline.cmdprompt == NULL && indent == 0)
1518 {
1519 /* In ex and debug mode it doesn't make sense to return. */
1520 if (exmode_active
1521#ifdef FEAT_EVAL
1522 || ccline.cmdfirstc == '>'
1523#endif
1524 )
1525 goto cmdline_not_changed;
1526
Bram Moolenaard23a8232018-02-10 18:45:26 +01001527 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 if (!cmd_silent)
1529 {
1530#ifdef FEAT_RIGHTLEFT
1531 if (cmdmsg_rl)
1532 msg_col = Columns;
1533 else
1534#endif
1535 msg_col = 0;
1536 msg_putchar(' '); /* delete ':' */
1537 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001538#ifdef FEAT_SEARCH_EXTRA
1539 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001540 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001541#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 redraw_cmdline = TRUE;
1543 goto returncmd; /* back to cmd mode */
1544 }
1545 goto cmdline_changed;
1546
1547 case K_INS:
1548 case K_KINS:
1549#ifdef FEAT_FKMAP
1550 /* if Farsi mode set, we are in reverse insert mode -
1551 Do not change the mode */
1552 if (cmd_fkmap)
1553 beep_flush();
1554 else
1555#endif
1556 ccline.overstrike = !ccline.overstrike;
1557#ifdef CURSOR_SHAPE
1558 ui_cursor_shape(); /* may show different cursor shape */
1559#endif
1560 goto cmdline_not_changed;
1561
1562 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001563 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 {
1565 /* ":lmap" mappings exists, toggle use of mappings. */
1566 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001567#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 im_set_active(FALSE); /* Disable input method */
1569#endif
1570 if (b_im_ptr != NULL)
1571 {
1572 if (State & LANGMAP)
1573 *b_im_ptr = B_IMODE_LMAP;
1574 else
1575 *b_im_ptr = B_IMODE_NONE;
1576 }
1577 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001578#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 else
1580 {
1581 /* There are no ":lmap" mappings, toggle IM. When
1582 * 'imdisable' is set don't try getting the status, it's
1583 * always off. */
1584 if ((p_imdisable && b_im_ptr != NULL)
1585 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1586 {
1587 im_set_active(FALSE); /* Disable input method */
1588 if (b_im_ptr != NULL)
1589 *b_im_ptr = B_IMODE_NONE;
1590 }
1591 else
1592 {
1593 im_set_active(TRUE); /* Enable input method */
1594 if (b_im_ptr != NULL)
1595 *b_im_ptr = B_IMODE_IM;
1596 }
1597 }
1598#endif
1599 if (b_im_ptr != NULL)
1600 {
1601 if (b_im_ptr == &curbuf->b_p_iminsert)
1602 set_iminsert_global();
1603 else
1604 set_imsearch_global();
1605 }
1606#ifdef CURSOR_SHAPE
1607 ui_cursor_shape(); /* may show different cursor shape */
1608#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001609#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 /* Show/unshow value of 'keymap' in status lines later. */
1611 status_redraw_curbuf();
1612#endif
1613 goto cmdline_not_changed;
1614
1615/* case '@': only in very old vi */
1616 case Ctrl_U:
1617 /* delete all characters left of the cursor */
1618 j = ccline.cmdpos;
1619 ccline.cmdlen -= j;
1620 i = ccline.cmdpos = 0;
1621 while (i < ccline.cmdlen)
1622 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1623 /* Truncate at the end, required for multi-byte chars. */
1624 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001625#ifdef FEAT_SEARCH_EXTRA
1626 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001627 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 redrawcmd();
1630 goto cmdline_changed;
1631
1632#ifdef FEAT_CLIPBOARD
1633 case Ctrl_Y:
1634 /* Copy the modeless selection, if there is one. */
1635 if (clip_star.state != SELECT_CLEARED)
1636 {
1637 if (clip_star.state == SELECT_DONE)
1638 clip_copy_modeless_selection(TRUE);
1639 goto cmdline_not_changed;
1640 }
1641 break;
1642#endif
1643
1644 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1645 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001646 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001647 * ":normal" runs out of characters. */
1648 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001649 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 goto cmdline_not_changed;
1651
1652 gotesc = TRUE; /* will free ccline.cmdbuff after
1653 putting it in history */
1654 goto returncmd; /* back to cmd mode */
1655
1656 case Ctrl_R: /* insert register */
1657#ifdef USE_ON_FLY_SCROLL
1658 dont_scroll = TRUE; /* disallow scrolling here */
1659#endif
1660 putcmdline('"', TRUE);
1661 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001662 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (i == Ctrl_O)
1664 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1665 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001666 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001667 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 --no_mapping;
1669#ifdef FEAT_EVAL
1670 /*
1671 * Insert the result of an expression.
1672 * Need to save the current command line, to be able to enter
1673 * a new one...
1674 */
1675 new_cmdpos = -1;
1676 if (c == '=')
1677 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1679 {
1680 beep_flush();
1681 c = ESC;
1682 }
1683 else
1684 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001685 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001687 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 }
1689 }
1690#endif
1691 if (c != ESC) /* use ESC to cancel inserting register */
1692 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001693 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001694
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001695#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001696 /* When there was a serious error abort getting the
1697 * command line. */
1698 if (aborting())
1699 {
1700 gotesc = TRUE; /* will free ccline.cmdbuff after
1701 putting it in history */
1702 goto returncmd; /* back to cmd mode */
1703 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 KeyTyped = FALSE; /* Don't do p_wc completion. */
1706#ifdef FEAT_EVAL
1707 if (new_cmdpos >= 0)
1708 {
1709 /* set_cmdline_pos() was used */
1710 if (new_cmdpos > ccline.cmdlen)
1711 ccline.cmdpos = ccline.cmdlen;
1712 else
1713 ccline.cmdpos = new_cmdpos;
1714 }
1715#endif
1716 }
1717 redrawcmd();
1718 goto cmdline_changed;
1719
1720 case Ctrl_D:
1721 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1722 break; /* Use ^D as normal char instead */
1723
1724 redrawcmd();
1725 continue; /* don't do incremental search now */
1726
1727 case K_RIGHT:
1728 case K_S_RIGHT:
1729 case K_C_RIGHT:
1730 do
1731 {
1732 if (ccline.cmdpos >= ccline.cmdlen)
1733 break;
1734 i = cmdline_charsize(ccline.cmdpos);
1735 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1736 break;
1737 ccline.cmdspos += i;
1738#ifdef FEAT_MBYTE
1739 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001740 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 + ccline.cmdpos);
1742 else
1743#endif
1744 ++ccline.cmdpos;
1745 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001746 while ((c == K_S_RIGHT || c == K_C_RIGHT
1747 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1749#ifdef FEAT_MBYTE
1750 if (has_mbyte)
1751 set_cmdspos_cursor();
1752#endif
1753 goto cmdline_not_changed;
1754
1755 case K_LEFT:
1756 case K_S_LEFT:
1757 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001758 if (ccline.cmdpos == 0)
1759 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 do
1761 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 --ccline.cmdpos;
1763#ifdef FEAT_MBYTE
1764 if (has_mbyte) /* move to first byte of char */
1765 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1766 ccline.cmdbuff + ccline.cmdpos);
1767#endif
1768 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1769 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001770 while (ccline.cmdpos > 0
1771 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001772 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1774#ifdef FEAT_MBYTE
1775 if (has_mbyte)
1776 set_cmdspos_cursor();
1777#endif
1778 goto cmdline_not_changed;
1779
1780 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001781 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001782 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783
Bram Moolenaar4770d092006-01-12 23:22:24 +00001784#ifdef FEAT_GUI_W32
1785 /* On Win32 ignore <M-F4>, we get it when closing the window was
1786 * cancelled. */
1787 case K_F4:
1788 if (mod_mask == MOD_MASK_ALT)
1789 {
1790 redrawcmd(); /* somehow the cmdline is cleared */
1791 goto cmdline_not_changed;
1792 }
1793 break;
1794#endif
1795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796#ifdef FEAT_MOUSE
1797 case K_MIDDLEDRAG:
1798 case K_MIDDLERELEASE:
1799 goto cmdline_not_changed; /* Ignore mouse */
1800
1801 case K_MIDDLEMOUSE:
1802# ifdef FEAT_GUI
1803 /* When GUI is active, also paste when 'mouse' is empty */
1804 if (!gui.in_use)
1805# endif
1806 if (!mouse_has(MOUSE_COMMAND))
1807 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001808# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001810 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001812# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001813 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 redrawcmd();
1815 goto cmdline_changed;
1816
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001817# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001819 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 redrawcmd();
1821 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001822# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823
1824 case K_LEFTDRAG:
1825 case K_LEFTRELEASE:
1826 case K_RIGHTDRAG:
1827 case K_RIGHTRELEASE:
1828 /* Ignore drag and release events when the button-down wasn't
1829 * seen before. */
1830 if (ignore_drag_release)
1831 goto cmdline_not_changed;
1832 /* FALLTHROUGH */
1833 case K_LEFTMOUSE:
1834 case K_RIGHTMOUSE:
1835 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1836 ignore_drag_release = TRUE;
1837 else
1838 ignore_drag_release = FALSE;
1839# ifdef FEAT_GUI
1840 /* When GUI is active, also move when 'mouse' is empty */
1841 if (!gui.in_use)
1842# endif
1843 if (!mouse_has(MOUSE_COMMAND))
1844 goto cmdline_not_changed; /* Ignore mouse */
1845# ifdef FEAT_CLIPBOARD
1846 if (mouse_row < cmdline_row && clip_star.available)
1847 {
1848 int button, is_click, is_drag;
1849
1850 /*
1851 * Handle modeless selection.
1852 */
1853 button = get_mouse_button(KEY2TERMCAP1(c),
1854 &is_click, &is_drag);
1855 if (mouse_model_popup() && button == MOUSE_LEFT
1856 && (mod_mask & MOD_MASK_SHIFT))
1857 {
1858 /* Translate shift-left to right button. */
1859 button = MOUSE_RIGHT;
1860 mod_mask &= ~MOD_MASK_SHIFT;
1861 }
1862 clip_modeless(button, is_click, is_drag);
1863 goto cmdline_not_changed;
1864 }
1865# endif
1866
1867 set_cmdspos();
1868 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1869 ++ccline.cmdpos)
1870 {
1871 i = cmdline_charsize(ccline.cmdpos);
1872 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1873 && mouse_col < ccline.cmdspos % Columns + i)
1874 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001875# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (has_mbyte)
1877 {
1878 /* Count ">" for double-wide char that doesn't fit. */
1879 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001880 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 + ccline.cmdpos) - 1;
1882 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001883# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 ccline.cmdspos += i;
1885 }
1886 goto cmdline_not_changed;
1887
1888 /* Mouse scroll wheel: ignored here */
1889 case K_MOUSEDOWN:
1890 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001891 case K_MOUSELEFT:
1892 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 /* Alternate buttons ignored here */
1894 case K_X1MOUSE:
1895 case K_X1DRAG:
1896 case K_X1RELEASE:
1897 case K_X2MOUSE:
1898 case K_X2DRAG:
1899 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001900 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 goto cmdline_not_changed;
1902
1903#endif /* FEAT_MOUSE */
1904
1905#ifdef FEAT_GUI
1906 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1907 case K_LEFTRELEASE_NM:
1908 goto cmdline_not_changed;
1909
1910 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001911 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
1913 gui_do_scroll();
1914 redrawcmd();
1915 }
1916 goto cmdline_not_changed;
1917
1918 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001919 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001921 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 redrawcmd();
1923 }
1924 goto cmdline_not_changed;
1925#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001926#ifdef FEAT_GUI_TABLINE
1927 case K_TABLINE:
1928 case K_TABMENU:
1929 /* Don't want to change any tabs here. Make sure the same tab
1930 * is still selected. */
1931 if (gui_use_tabline())
1932 gui_mch_set_curtab(tabpage_index(curtab));
1933 goto cmdline_not_changed;
1934#endif
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 case K_SELECT: /* end of Select mode mapping - ignore */
1937 goto cmdline_not_changed;
1938
1939 case Ctrl_B: /* begin of command line */
1940 case K_HOME:
1941 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 case K_S_HOME:
1943 case K_C_HOME:
1944 ccline.cmdpos = 0;
1945 set_cmdspos();
1946 goto cmdline_not_changed;
1947
1948 case Ctrl_E: /* end of command line */
1949 case K_END:
1950 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 case K_S_END:
1952 case K_C_END:
1953 ccline.cmdpos = ccline.cmdlen;
1954 set_cmdspos_cursor();
1955 goto cmdline_not_changed;
1956
1957 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001958 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 break;
1960 goto cmdline_changed;
1961
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001962 case Ctrl_L:
1963#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001964 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001965 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001966#endif
1967
1968 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001969 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 break;
1971 goto cmdline_changed;
1972
1973 case Ctrl_N: /* next match */
1974 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001975 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001977 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1978 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001980 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001983 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 case K_UP:
1985 case K_DOWN:
1986 case K_S_UP:
1987 case K_S_DOWN:
1988 case K_PAGEUP:
1989 case K_KPAGEUP:
1990 case K_PAGEDOWN:
1991 case K_KPAGEDOWN:
1992 if (hislen == 0 || firstc == NUL) /* no history */
1993 goto cmdline_not_changed;
1994
1995 i = hiscnt;
1996
1997 /* save current command string so it can be restored later */
1998 if (lookfor == NULL)
1999 {
2000 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2001 goto cmdline_not_changed;
2002 lookfor[ccline.cmdpos] = NUL;
2003 }
2004
2005 j = (int)STRLEN(lookfor);
2006 for (;;)
2007 {
2008 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002009 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002010 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 {
2012 if (hiscnt == hislen) /* first time */
2013 hiscnt = hisidx[histype];
2014 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2015 hiscnt = hislen - 1;
2016 else if (hiscnt != hisidx[histype] + 1)
2017 --hiscnt;
2018 else /* at top of list */
2019 {
2020 hiscnt = i;
2021 break;
2022 }
2023 }
2024 else /* one step forwards */
2025 {
2026 /* on last entry, clear the line */
2027 if (hiscnt == hisidx[histype])
2028 {
2029 hiscnt = hislen;
2030 break;
2031 }
2032
2033 /* not on a history line, nothing to do */
2034 if (hiscnt == hislen)
2035 break;
2036 if (hiscnt == hislen - 1) /* wrap around */
2037 hiscnt = 0;
2038 else
2039 ++hiscnt;
2040 }
2041 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2042 {
2043 hiscnt = i;
2044 break;
2045 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002046 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002047 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 || STRNCMP(history[histype][hiscnt].hisstr,
2049 lookfor, (size_t)j) == 0)
2050 break;
2051 }
2052
2053 if (hiscnt != i) /* jumped to other entry */
2054 {
2055 char_u *p;
2056 int len;
2057 int old_firstc;
2058
2059 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002060 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 if (hiscnt == hislen)
2062 p = lookfor; /* back to the old one */
2063 else
2064 p = history[histype][hiscnt].hisstr;
2065
2066 if (histype == HIST_SEARCH
2067 && p != lookfor
2068 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2069 {
2070 /* Correct for the separator character used when
2071 * adding the history entry vs the one used now.
2072 * First loop: count length.
2073 * Second loop: copy the characters. */
2074 for (i = 0; i <= 1; ++i)
2075 {
2076 len = 0;
2077 for (j = 0; p[j] != NUL; ++j)
2078 {
2079 /* Replace old sep with new sep, unless it is
2080 * escaped. */
2081 if (p[j] == old_firstc
2082 && (j == 0 || p[j - 1] != '\\'))
2083 {
2084 if (i > 0)
2085 ccline.cmdbuff[len] = firstc;
2086 }
2087 else
2088 {
2089 /* Escape new sep, unless it is already
2090 * escaped. */
2091 if (p[j] == firstc
2092 && (j == 0 || p[j - 1] != '\\'))
2093 {
2094 if (i > 0)
2095 ccline.cmdbuff[len] = '\\';
2096 ++len;
2097 }
2098 if (i > 0)
2099 ccline.cmdbuff[len] = p[j];
2100 }
2101 ++len;
2102 }
2103 if (i == 0)
2104 {
2105 alloc_cmdbuff(len);
2106 if (ccline.cmdbuff == NULL)
2107 goto returncmd;
2108 }
2109 }
2110 ccline.cmdbuff[len] = NUL;
2111 }
2112 else
2113 {
2114 alloc_cmdbuff((int)STRLEN(p));
2115 if (ccline.cmdbuff == NULL)
2116 goto returncmd;
2117 STRCPY(ccline.cmdbuff, p);
2118 }
2119
2120 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2121 redrawcmd();
2122 goto cmdline_changed;
2123 }
2124 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002125#endif
2126 goto cmdline_not_changed;
2127
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002128#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002129 case Ctrl_G: /* next match */
2130 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002131 if (may_adjust_incsearch_highlighting(
2132 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002133 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135#endif
2136
2137 case Ctrl_V:
2138 case Ctrl_Q:
2139#ifdef FEAT_MOUSE
2140 ignore_drag_release = TRUE;
2141#endif
2142 putcmdline('^', TRUE);
2143 c = get_literal(); /* get next (two) character(s) */
2144 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002145 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#ifdef FEAT_MBYTE
2147 /* may need to remove ^ when composing char was typed */
2148 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2149 {
2150 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2151 msg_putchar(' ');
2152 cursorcmd();
2153 }
2154#endif
2155 break;
2156
2157#ifdef FEAT_DIGRAPHS
2158 case Ctrl_K:
2159#ifdef FEAT_MOUSE
2160 ignore_drag_release = TRUE;
2161#endif
2162 putcmdline('?', TRUE);
2163#ifdef USE_ON_FLY_SCROLL
2164 dont_scroll = TRUE; /* disallow scrolling here */
2165#endif
2166 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002167 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (c != NUL)
2169 break;
2170
2171 redrawcmd();
2172 goto cmdline_not_changed;
2173#endif /* FEAT_DIGRAPHS */
2174
2175#ifdef FEAT_RIGHTLEFT
2176 case Ctrl__: /* CTRL-_: switch language mode */
2177 if (!p_ari)
2178 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002179# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 if (p_altkeymap)
2181 {
2182 cmd_fkmap = !cmd_fkmap;
2183 if (cmd_fkmap) /* in Farsi always in Insert mode */
2184 ccline.overstrike = FALSE;
2185 }
2186 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002187# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 cmd_hkmap = !cmd_hkmap;
2189 goto cmdline_not_changed;
2190#endif
2191
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002192 case K_PS:
2193 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2194 goto cmdline_changed;
2195
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 default:
2197#ifdef UNIX
2198 if (c == intr_char)
2199 {
2200 gotesc = TRUE; /* will free ccline.cmdbuff after
2201 putting it in history */
2202 goto returncmd; /* back to Normal mode */
2203 }
2204#endif
2205 /*
2206 * Normal character with no special meaning. Just set mod_mask
2207 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2208 * the string <S-Space>. This should only happen after ^V.
2209 */
2210 if (!IS_SPECIAL(c))
2211 mod_mask = 0x0;
2212 break;
2213 }
2214 /*
2215 * End of switch on command line character.
2216 * We come here if we have a normal character.
2217 */
2218
Bram Moolenaarede3e632013-06-23 16:16:19 +02002219 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#ifdef FEAT_MBYTE
2221 /* Add ABBR_OFF for characters above 0x100, this is
2222 * what check_abbr() expects. */
2223 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2224#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002225 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 goto cmdline_changed;
2227
2228 /*
2229 * put the character in the command line
2230 */
2231 if (IS_SPECIAL(c) || mod_mask != 0)
2232 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2233 else
2234 {
2235#ifdef FEAT_MBYTE
2236 if (has_mbyte)
2237 {
2238 j = (*mb_char2bytes)(c, IObuff);
2239 IObuff[j] = NUL; /* exclude composing chars */
2240 put_on_cmdline(IObuff, j, TRUE);
2241 }
2242 else
2243#endif
2244 {
2245 IObuff[0] = c;
2246 put_on_cmdline(IObuff, 1, TRUE);
2247 }
2248 }
2249 goto cmdline_changed;
2250
2251/*
2252 * This part implements incremental searches for "/" and "?"
2253 * Jump to cmdline_not_changed when a character has been read but the command
2254 * line did not change. Then we only search and redraw if something changed in
2255 * the past.
2256 * Jump to cmdline_changed when the command line did change.
2257 * (Sorry for the goto's, I know it is ugly).
2258 */
2259cmdline_not_changed:
2260#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002261 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 continue;
2263#endif
2264
2265cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002266 /* Trigger CmdlineChanged autocommands. */
2267 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002268
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002270 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271#endif
2272
2273#ifdef FEAT_RIGHTLEFT
2274 if (cmdmsg_rl
2275# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002276 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277# endif
2278 )
2279 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002280 * right-left typing. Not efficient, but it works.
2281 * Do it only when there are no characters left to read
2282 * to avoid useless intermediate redraws. */
2283 if (vpeekc() == NUL)
2284 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285#endif
2286 }
2287
2288returncmd:
2289
2290#ifdef FEAT_RIGHTLEFT
2291 cmdmsg_rl = FALSE;
2292#endif
2293
2294#ifdef FEAT_FKMAP
2295 cmd_fkmap = 0;
2296#endif
2297
2298 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002299 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
2301#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002302 finish_incsearch_highlighting(gotesc, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303#endif
2304
2305 if (ccline.cmdbuff != NULL)
2306 {
2307 /*
2308 * Put line in history buffer (":" and "=" only when it was typed).
2309 */
2310#ifdef FEAT_CMDHIST
2311 if (ccline.cmdlen && firstc != NUL
2312 && (some_key_typed || histype == HIST_SEARCH))
2313 {
2314 add_to_history(histype, ccline.cmdbuff, TRUE,
2315 histype == HIST_SEARCH ? firstc : NUL);
2316 if (firstc == ':')
2317 {
2318 vim_free(new_last_cmdline);
2319 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2320 }
2321 }
2322#endif
2323
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002324 if (gotesc)
2325 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 }
2327
2328 /*
2329 * If the screen was shifted up, redraw the whole screen (later).
2330 * If the line is too long, clear it, so ruler and shown command do
2331 * not get printed in the middle of it.
2332 */
2333 msg_check();
2334 msg_scroll = save_msg_scroll;
2335 redir_off = FALSE;
2336
2337 /* When the command line was typed, no need for a wait-return prompt. */
2338 if (some_key_typed)
2339 need_wait_return = FALSE;
2340
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002341 /* Trigger CmdlineLeave autocommands. */
2342 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002343
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002345#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2347 im_save_status(b_im_ptr);
2348 im_set_active(FALSE);
2349#endif
2350#ifdef FEAT_MOUSE
2351 setmouse();
2352#endif
2353#ifdef CURSOR_SHAPE
2354 ui_cursor_shape(); /* may show different cursor shape */
2355#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002356 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002358 {
2359 char_u *p = ccline.cmdbuff;
2360
2361 /* Make ccline empty, getcmdline() may try to use it. */
2362 ccline.cmdbuff = NULL;
2363 return p;
2364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365}
2366
2367#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2368/*
2369 * Get a command line with a prompt.
2370 * This is prepared to be called recursively from getcmdline() (e.g. by
2371 * f_input() when evaluating an expression from CTRL-R =).
2372 * Returns the command line in allocated memory, or NULL.
2373 */
2374 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002375getcmdline_prompt(
2376 int firstc,
2377 char_u *prompt, /* command line prompt */
2378 int attr, /* attributes for prompt */
2379 int xp_context, /* type of expansion */
2380 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 char_u *s;
2383 struct cmdline_info save_ccline;
2384 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002385 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002387 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 ccline.cmdprompt = prompt;
2389 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002390# ifdef FEAT_EVAL
2391 ccline.xp_context = xp_context;
2392 ccline.xp_arg = xp_arg;
2393 ccline.input_fn = (firstc == '@');
2394# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002395 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002397 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002398 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002399 /* Restore msg_col, the prompt from input() may have changed it.
2400 * But only if called recursively and the commandline is therefore being
2401 * restored to an old one; if not, the input() prompt stays on the screen,
2402 * so we need its modified msg_col left intact. */
2403 if (ccline.cmdbuff != NULL)
2404 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 return s;
2407}
2408#endif
2409
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002410/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002411 * Return TRUE when the text must not be changed and we can't switch to
2412 * another window or buffer. Used when editing the command line, evaluating
2413 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002414 */
2415 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002416text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002417{
2418#ifdef FEAT_CMDWIN
2419 if (cmdwin_type != 0)
2420 return TRUE;
2421#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002422 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002423}
2424
2425/*
2426 * Give an error message for a command that isn't allowed while the cmdline
2427 * window is open or editing the cmdline in another way.
2428 */
2429 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002430text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002431{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002432 EMSG(_(get_text_locked_msg()));
2433}
2434
2435 char_u *
2436get_text_locked_msg(void)
2437{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002438#ifdef FEAT_CMDWIN
2439 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002440 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002441#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002442 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002443}
2444
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002445/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002446 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2447 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002448 */
2449 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002450curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002451{
2452 if (curbuf_lock > 0)
2453 {
2454 EMSG(_("E788: Not allowed to edit another buffer now"));
2455 return TRUE;
2456 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002457 return allbuf_locked();
2458}
2459
2460/*
2461 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2462 * message.
2463 */
2464 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002465allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002466{
2467 if (allbuf_lock > 0)
2468 {
2469 EMSG(_("E811: Not allowed to change buffer information now"));
2470 return TRUE;
2471 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002472 return FALSE;
2473}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002476cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2479 if (cmdline_star > 0) /* showing '*', always 1 position */
2480 return 1;
2481#endif
2482 return ptr2cells(ccline.cmdbuff + idx);
2483}
2484
2485/*
2486 * Compute the offset of the cursor on the command line for the prompt and
2487 * indent.
2488 */
2489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002490set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002492 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 ccline.cmdspos = 1 + ccline.cmdindent;
2494 else
2495 ccline.cmdspos = 0 + ccline.cmdindent;
2496}
2497
2498/*
2499 * Compute the screen position for the cursor on the command line.
2500 */
2501 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002502set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 int i, m, c;
2505
2506 set_cmdspos();
2507 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002508 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002510 if (m < 0) /* overflow, Columns or Rows at weird value */
2511 m = MAXCOL;
2512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 else
2514 m = MAXCOL;
2515 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2516 {
2517 c = cmdline_charsize(i);
2518#ifdef FEAT_MBYTE
2519 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2520 if (has_mbyte)
2521 correct_cmdspos(i, c);
2522#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002523 /* If the cmdline doesn't fit, show cursor on last visible char.
2524 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if ((ccline.cmdspos += c) >= m)
2526 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 ccline.cmdspos -= c;
2528 break;
2529 }
2530#ifdef FEAT_MBYTE
2531 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002532 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533#endif
2534 }
2535}
2536
2537#ifdef FEAT_MBYTE
2538/*
2539 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2540 * character that doesn't fit, so that a ">" must be displayed.
2541 */
2542 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002543correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002545 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2547 && ccline.cmdspos % Columns + cells > Columns)
2548 ccline.cmdspos++;
2549}
2550#endif
2551
2552/*
2553 * Get an Ex command line for the ":" command.
2554 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002556getexline(
2557 int c, /* normally ':', NUL for ":append" */
2558 void *cookie UNUSED,
2559 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560{
2561 /* When executing a register, remove ':' that's in front of each line. */
2562 if (exec_from_reg && vpeekc() == ':')
2563 (void)vgetc();
2564 return getcmdline(c, 1L, indent);
2565}
2566
2567/*
2568 * Get an Ex command line for Ex mode.
2569 * In Ex mode we only use the OS supplied line editing features and no
2570 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002571 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002574getexmodeline(
2575 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002577 void *cookie UNUSED,
2578 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002580 garray_T line_ga;
2581 char_u *pend;
2582 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002583 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002584 int escaped = FALSE; /* CTRL-V typed */
2585 int vcol = 0;
2586 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002587 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002588 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
2590 /* Switch cursor on now. This avoids that it happens after the "\n", which
2591 * confuses the system function that computes tabstops. */
2592 cursor_on();
2593
2594 /* always start in column 0; write a newline if necessary */
2595 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002596 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002600 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002601 if (p_prompt)
2602 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 while (indent-- > 0)
2604 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
2607
2608 ga_init2(&line_ga, 1, 30);
2609
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002610 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002611 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002612 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002613 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002614 while (indent >= 8)
2615 {
2616 ga_append(&line_ga, TAB);
2617 msg_puts((char_u *)" ");
2618 indent -= 8;
2619 }
2620 while (indent-- > 0)
2621 {
2622 ga_append(&line_ga, ' ');
2623 msg_putchar(' ');
2624 }
2625 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002626 ++no_mapping;
2627 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002628
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /*
2630 * Get the line, one character at a time.
2631 */
2632 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002633 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002635 long sw;
2636 char_u *s;
2637
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 if (ga_grow(&line_ga, 40) == FAIL)
2639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640
Bram Moolenaarba748c82017-02-26 14:00:07 +01002641 /*
2642 * Get one character at a time.
2643 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002644 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002645
2646 /* Check for a ":normal" command and no more characters left. */
2647 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2648 c1 = '\n';
2649 else
2650 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651
2652 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002653 * Handle line editing.
2654 * Previously this was left to the system, putting the terminal in
2655 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002657 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002659 msg_putchar('\n');
2660 break;
2661 }
2662
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002663 if (c1 == K_PS)
2664 {
2665 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2666 goto redraw;
2667 }
2668
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002669 if (!escaped)
2670 {
2671 /* CR typed means "enter", which is NL */
2672 if (c1 == '\r')
2673 c1 = '\n';
2674
2675 if (c1 == BS || c1 == K_BS
2676 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002678 if (line_ga.ga_len > 0)
2679 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002680#ifdef FEAT_MBYTE
2681 if (has_mbyte)
2682 {
2683 p = (char_u *)line_ga.ga_data;
2684 p[line_ga.ga_len] = NUL;
2685 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2686 line_ga.ga_len -= len;
2687 }
2688 else
2689#endif
2690 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002691 goto redraw;
2692 }
2693 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 }
2695
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 msg_col = startcol;
2699 msg_clr_eos();
2700 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002701 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 }
2703
2704 if (c1 == Ctrl_T)
2705 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002706 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 p = (char_u *)line_ga.ga_data;
2708 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002709 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002710 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002712 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002714 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002715 p = (char_u *)line_ga.ga_data;
2716 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2718 *s = ' ';
2719 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002721redraw:
2722 /* redraw the line */
2723 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002725 p = (char_u *)line_ga.ga_data;
2726 p[line_ga.ga_len] = NUL;
2727 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 msg_putchar(' ');
2734 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002735 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002739 len = MB_PTR2LEN(p);
2740 msg_outtrans_len(p, len);
2741 vcol += ptr2cells(p);
2742 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002745 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002746 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 continue;
2748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 if (c1 == Ctrl_D)
2751 {
2752 /* Delete one shiftwidth. */
2753 p = (char_u *)line_ga.ga_data;
2754 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 if (prev_char == '^')
2757 ex_keep_indent = TRUE;
2758 indent = 0;
2759 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 }
2761 else
2762 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002764 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002765 if (indent > 0)
2766 {
2767 --indent;
2768 indent -= indent % get_sw_value(curbuf);
2769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002771 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002772 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002773 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2775 --line_ga.ga_len;
2776 }
2777 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779
2780 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2781 {
2782 escaped = TRUE;
2783 continue;
2784 }
2785
2786 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2787 if (IS_SPECIAL(c1))
2788 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790
2791 if (IS_SPECIAL(c1))
2792 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002793#ifdef FEAT_MBYTE
2794 if (has_mbyte)
2795 len = (*mb_char2bytes)(c1,
2796 (char_u *)line_ga.ga_data + line_ga.ga_len);
2797 else
2798#endif
2799 {
2800 len = 1;
2801 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2802 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 if (c1 == '\n')
2804 msg_putchar('\n');
2805 else if (c1 == TAB)
2806 {
2807 /* Don't use chartabsize(), 'ts' can be different */
2808 do
2809 {
2810 msg_putchar(' ');
2811 } while (++vcol % 8);
2812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002816 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002819 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 escaped = FALSE;
2821
2822 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002823 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002825 /* We are done when a NL is entered, but not when it comes after an
2826 * odd number of backslashes, that results in a NUL. */
2827 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002829 int bcount = 0;
2830
2831 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2832 ++bcount;
2833
2834 if (bcount > 0)
2835 {
2836 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2837 * "\NL", etc. */
2838 line_ga.ga_len -= (bcount + 1) / 2;
2839 pend -= (bcount + 1) / 2;
2840 pend[-1] = '\n';
2841 }
2842
2843 if ((bcount & 1) == 0)
2844 {
2845 --line_ga.ga_len;
2846 --pend;
2847 *pend = NUL;
2848 break;
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
2851 }
2852
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 --no_mapping;
2854 --allow_keys;
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 /* make following messages go to the next line */
2857 msg_didout = FALSE;
2858 msg_col = 0;
2859 if (msg_row < Rows - 1)
2860 ++msg_row;
2861 emsg_on_display = FALSE; /* don't want ui_delay() */
2862
2863 if (got_int)
2864 ga_clear(&line_ga);
2865
2866 return (char_u *)line_ga.ga_data;
2867}
2868
Bram Moolenaare344bea2005-09-01 20:46:49 +00002869# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2870 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871/*
2872 * Return TRUE if ccline.overstrike is on.
2873 */
2874 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002875cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876{
2877 return ccline.overstrike;
2878}
2879
2880/*
2881 * Return TRUE if the cursor is at the end of the cmdline.
2882 */
2883 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002884cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 return (ccline.cmdpos >= ccline.cmdlen);
2887}
2888#endif
2889
Bram Moolenaar9372a112005-12-06 19:59:18 +00002890#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891/*
2892 * Return the virtual column number at the current cursor position.
2893 * This is used by the IM code to obtain the start of the preedit string.
2894 */
2895 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002896cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2899 return MAXCOL;
2900
2901# ifdef FEAT_MBYTE
2902 if (has_mbyte)
2903 {
2904 colnr_T col;
2905 int i = 0;
2906
2907 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002908 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909
2910 return col;
2911 }
2912 else
2913# endif
2914 return ccline.cmdpos;
2915}
2916#endif
2917
2918#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2919/*
2920 * If part of the command line is an IM preedit string, redraw it with
2921 * IM feedback attributes. The cursor position is restored after drawing.
2922 */
2923 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002924redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925{
2926 if ((State & CMDLINE)
2927 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002928 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 && !p_imdisable
2930 && im_is_preediting())
2931 {
2932 int cmdpos = 0;
2933 int cmdspos;
2934 int old_row;
2935 int old_col;
2936 colnr_T col;
2937
2938 old_row = msg_row;
2939 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002940 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941
2942# ifdef FEAT_MBYTE
2943 if (has_mbyte)
2944 {
2945 for (col = 0; col < preedit_start_col
2946 && cmdpos < ccline.cmdlen; ++col)
2947 {
2948 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002949 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 }
2951 }
2952 else
2953# endif
2954 {
2955 cmdspos += preedit_start_col;
2956 cmdpos += preedit_start_col;
2957 }
2958
2959 msg_row = cmdline_row + (cmdspos / (int)Columns);
2960 msg_col = cmdspos % (int)Columns;
2961 if (msg_row >= Rows)
2962 msg_row = Rows - 1;
2963
2964 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2965 {
2966 int char_len;
2967 int char_attr;
2968
2969 char_attr = im_get_feedback_attr(col);
2970 if (char_attr < 0)
2971 break; /* end of preedit string */
2972
2973# ifdef FEAT_MBYTE
2974 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002975 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 else
2977# endif
2978 char_len = 1;
2979
2980 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2981 cmdpos += char_len;
2982 }
2983
2984 msg_row = old_row;
2985 msg_col = old_col;
2986 }
2987}
2988#endif /* FEAT_XIM && FEAT_GUI_GTK */
2989
2990/*
2991 * Allocate a new command line buffer.
2992 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2993 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2994 */
2995 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002996alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
2998 /*
2999 * give some extra space to avoid having to allocate all the time
3000 */
3001 if (len < 80)
3002 len = 100;
3003 else
3004 len += 20;
3005
3006 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3007 ccline.cmdbufflen = len;
3008}
3009
3010/*
3011 * Re-allocate the command line to length len + something extra.
3012 * return FAIL for failure, OK otherwise
3013 */
3014 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003015realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 char_u *p;
3018
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003019 if (len < ccline.cmdbufflen)
3020 return OK; /* no need to resize */
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 p = ccline.cmdbuff;
3023 alloc_cmdbuff(len); /* will get some more */
3024 if (ccline.cmdbuff == NULL) /* out of memory */
3025 {
3026 ccline.cmdbuff = p; /* keep the old one */
3027 return FAIL;
3028 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003029 /* There isn't always a NUL after the command, but it may need to be
3030 * there, thus copy up to the NUL and add a NUL. */
3031 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3032 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003034
3035 if (ccline.xpc != NULL
3036 && ccline.xpc->xp_pattern != NULL
3037 && ccline.xpc->xp_context != EXPAND_NOTHING
3038 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3039 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003040 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003041
3042 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3043 * to point into the newly allocated memory. */
3044 if (i >= 0 && i <= ccline.cmdlen)
3045 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3046 }
3047
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 return OK;
3049}
3050
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003051#if defined(FEAT_ARABIC) || defined(PROTO)
3052static char_u *arshape_buf = NULL;
3053
3054# if defined(EXITFREE) || defined(PROTO)
3055 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003056free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003057{
3058 vim_free(arshape_buf);
3059}
3060# endif
3061#endif
3062
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063/*
3064 * Draw part of the cmdline at the current cursor position. But draw stars
3065 * when cmdline_star is TRUE.
3066 */
3067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003068draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069{
3070#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3071 int i;
3072
3073 if (cmdline_star > 0)
3074 for (i = 0; i < len; ++i)
3075 {
3076 msg_putchar('*');
3077# ifdef FEAT_MBYTE
3078 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003079 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080# endif
3081 }
3082 else
3083#endif
3084#ifdef FEAT_ARABIC
3085 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3086 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 static int buflen = 0;
3088 char_u *p;
3089 int j;
3090 int newlen = 0;
3091 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003092 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 int prev_c = 0;
3094 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003095 int u8c;
3096 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099 /*
3100 * Do arabic shaping into a temporary buffer. This is very
3101 * inefficient!
3102 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003103 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 {
3105 /* Re-allocate the buffer. We keep it around to avoid a lot of
3106 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003107 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003108 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003109 arshape_buf = alloc(buflen);
3110 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 return; /* out of memory */
3112 }
3113
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003114 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3115 {
3116 /* Prepend a space to draw the leading composing char on. */
3117 arshape_buf[0] = ' ';
3118 newlen = 1;
3119 }
3120
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 for (j = start; j < start + len; j += mb_l)
3122 {
3123 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003124 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003125 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (ARABIC_CHAR(u8c))
3127 {
3128 /* Do Arabic shaping. */
3129 if (cmdmsg_rl)
3130 {
3131 /* displaying from right to left */
3132 pc = prev_c;
3133 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003134 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 if (j + mb_l >= start + len)
3136 nc = NUL;
3137 else
3138 nc = utf_ptr2char(p + mb_l);
3139 }
3140 else
3141 {
3142 /* displaying from left to right */
3143 if (j + mb_l >= start + len)
3144 pc = NUL;
3145 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003146 {
3147 int pcc[MAX_MCO];
3148
3149 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003151 pc1 = pcc[0];
3152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 nc = prev_c;
3154 }
3155 prev_c = u8c;
3156
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003157 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003159 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003160 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003162 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3163 if (u8cc[1] != 0)
3164 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003165 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 }
3167 }
3168 else
3169 {
3170 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003171 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 newlen += mb_l;
3173 }
3174 }
3175
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003176 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
3178 else
3179#endif
3180 msg_outtrans_len(ccline.cmdbuff + start, len);
3181}
3182
3183/*
3184 * Put a character on the command line. Shifts the following text to the
3185 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3186 * "c" must be printable (fit in one display cell)!
3187 */
3188 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003189putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 if (cmd_silent)
3192 return;
3193 msg_no_more = TRUE;
3194 msg_putchar(c);
3195 if (shift)
3196 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3197 msg_no_more = FALSE;
3198 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003199 extra_char = c;
3200 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201}
3202
3203/*
3204 * Undo a putcmdline(c, FALSE).
3205 */
3206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003207unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208{
3209 if (cmd_silent)
3210 return;
3211 msg_no_more = TRUE;
3212 if (ccline.cmdlen == ccline.cmdpos)
3213 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003214#ifdef FEAT_MBYTE
3215 else if (has_mbyte)
3216 draw_cmdline(ccline.cmdpos,
3217 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3218#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 else
3220 draw_cmdline(ccline.cmdpos, 1);
3221 msg_no_more = FALSE;
3222 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003223 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224}
3225
3226/*
3227 * Put the given string, of the given length, onto the command line.
3228 * If len is -1, then STRLEN() is used to calculate the length.
3229 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3230 * part will be redrawn, otherwise it will not. If this function is called
3231 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3232 * called afterwards.
3233 */
3234 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003235put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 int retval;
3238 int i;
3239 int m;
3240 int c;
3241
3242 if (len < 0)
3243 len = (int)STRLEN(str);
3244
3245 /* Check if ccline.cmdbuff needs to be longer */
3246 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003247 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 else
3249 retval = OK;
3250 if (retval == OK)
3251 {
3252 if (!ccline.overstrike)
3253 {
3254 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3255 ccline.cmdbuff + ccline.cmdpos,
3256 (size_t)(ccline.cmdlen - ccline.cmdpos));
3257 ccline.cmdlen += len;
3258 }
3259 else
3260 {
3261#ifdef FEAT_MBYTE
3262 if (has_mbyte)
3263 {
3264 /* Count nr of characters in the new string. */
3265 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003266 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 ++m;
3268 /* Count nr of bytes in cmdline that are overwritten by these
3269 * characters. */
3270 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003271 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 --m;
3273 if (i < ccline.cmdlen)
3274 {
3275 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3276 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3277 ccline.cmdlen += ccline.cmdpos + len - i;
3278 }
3279 else
3280 ccline.cmdlen = ccline.cmdpos + len;
3281 }
3282 else
3283#endif
3284 if (ccline.cmdpos + len > ccline.cmdlen)
3285 ccline.cmdlen = ccline.cmdpos + len;
3286 }
3287 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3288 ccline.cmdbuff[ccline.cmdlen] = NUL;
3289
3290#ifdef FEAT_MBYTE
3291 if (enc_utf8)
3292 {
3293 /* When the inserted text starts with a composing character,
3294 * backup to the character before it. There could be two of them.
3295 */
3296 i = 0;
3297 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3298 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3299 {
3300 i = (*mb_head_off)(ccline.cmdbuff,
3301 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3302 ccline.cmdpos -= i;
3303 len += i;
3304 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3305 }
3306# ifdef FEAT_ARABIC
3307 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3308 {
3309 /* Check the previous character for Arabic combining pair. */
3310 i = (*mb_head_off)(ccline.cmdbuff,
3311 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3312 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3313 + ccline.cmdpos - i), c))
3314 {
3315 ccline.cmdpos -= i;
3316 len += i;
3317 }
3318 else
3319 i = 0;
3320 }
3321# endif
3322 if (i != 0)
3323 {
3324 /* Also backup the cursor position. */
3325 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3326 ccline.cmdspos -= i;
3327 msg_col -= i;
3328 if (msg_col < 0)
3329 {
3330 msg_col += Columns;
3331 --msg_row;
3332 }
3333 }
3334 }
3335#endif
3336
3337 if (redraw && !cmd_silent)
3338 {
3339 msg_no_more = TRUE;
3340 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003341 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3343 /* Avoid clearing the rest of the line too often. */
3344 if (cmdline_row != i || ccline.overstrike)
3345 msg_clr_eos();
3346 msg_no_more = FALSE;
3347 }
3348#ifdef FEAT_FKMAP
3349 /*
3350 * If we are in Farsi command mode, the character input must be in
3351 * Insert mode. So do not advance the cmdpos.
3352 */
3353 if (!cmd_fkmap)
3354#endif
3355 {
3356 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003357 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003359 if (m < 0) /* overflow, Columns or Rows at weird value */
3360 m = MAXCOL;
3361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 else
3363 m = MAXCOL;
3364 for (i = 0; i < len; ++i)
3365 {
3366 c = cmdline_charsize(ccline.cmdpos);
3367#ifdef FEAT_MBYTE
3368 /* count ">" for a double-wide char that doesn't fit. */
3369 if (has_mbyte)
3370 correct_cmdspos(ccline.cmdpos, c);
3371#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003372 /* Stop cursor at the end of the screen, but do increment the
3373 * insert position, so that entering a very long command
3374 * works, even though you can't see it. */
3375 if (ccline.cmdspos + c < m)
3376 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377#ifdef FEAT_MBYTE
3378 if (has_mbyte)
3379 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003380 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 if (c > len - i - 1)
3382 c = len - i - 1;
3383 ccline.cmdpos += c;
3384 i += c;
3385 }
3386#endif
3387 ++ccline.cmdpos;
3388 }
3389 }
3390 }
3391 if (redraw)
3392 msg_check();
3393 return retval;
3394}
3395
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003396static struct cmdline_info prev_ccline;
3397static int prev_ccline_used = FALSE;
3398
3399/*
3400 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3401 * and overwrite it. But get_cmdline_str() may need it, thus make it
3402 * available globally in prev_ccline.
3403 */
3404 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003405save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003406{
3407 if (!prev_ccline_used)
3408 {
3409 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3410 prev_ccline_used = TRUE;
3411 }
3412 *ccp = prev_ccline;
3413 prev_ccline = ccline;
3414 ccline.cmdbuff = NULL;
3415 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003416 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003417}
3418
3419/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003420 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003421 */
3422 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003423restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003424{
3425 ccline = prev_ccline;
3426 prev_ccline = *ccp;
3427}
3428
Bram Moolenaar5a305422006-04-28 22:38:25 +00003429#if defined(FEAT_EVAL) || defined(PROTO)
3430/*
3431 * Save the command line into allocated memory. Returns a pointer to be
3432 * passed to restore_cmdline_alloc() later.
3433 * Returns NULL when failed.
3434 */
3435 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003436save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003437{
3438 struct cmdline_info *p;
3439
3440 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3441 if (p != NULL)
3442 save_cmdline(p);
3443 return (char_u *)p;
3444}
3445
3446/*
3447 * Restore the command line from the return value of save_cmdline_alloc().
3448 */
3449 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003450restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003451{
3452 if (p != NULL)
3453 {
3454 restore_cmdline((struct cmdline_info *)p);
3455 vim_free(p);
3456 }
3457}
3458#endif
3459
Bram Moolenaar8299df92004-07-10 09:47:34 +00003460/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003461 * Paste a yank register into the command line.
3462 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003463 * insert_reg() can't be used here, because special characters from the
3464 * register contents will be interpreted as commands.
3465 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003466 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003467 */
3468 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003469cmdline_paste(
3470 int regname,
3471 int literally, /* Insert text literally instead of "as typed" */
3472 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003473{
3474 long i;
3475 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003476 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003477 int allocated;
3478 struct cmdline_info save_ccline;
3479
3480 /* check for valid regname; also accept special characters for CTRL-R in
3481 * the command line */
3482 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003483 && regname != Ctrl_A && regname != Ctrl_L
3484 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003485 return FAIL;
3486
3487 /* A register containing CTRL-R can cause an endless loop. Allow using
3488 * CTRL-C to break the loop. */
3489 line_breakcheck();
3490 if (got_int)
3491 return FAIL;
3492
3493#ifdef FEAT_CLIPBOARD
3494 regname = may_get_selection(regname);
3495#endif
3496
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003497 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003498 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003499 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003500 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003501 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003502 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003503 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003504
3505 if (i)
3506 {
3507 /* Got the value of a special register in "arg". */
3508 if (arg == NULL)
3509 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003510
3511 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3512 * part of the word. */
3513 p = arg;
3514 if (p_is && regname == Ctrl_W)
3515 {
3516 char_u *w;
3517 int len;
3518
3519 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003520 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003521 {
3522#ifdef FEAT_MBYTE
3523 if (has_mbyte)
3524 {
3525 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3526 if (!vim_iswordc(mb_ptr2char(w - len)))
3527 break;
3528 w -= len;
3529 }
3530 else
3531#endif
3532 {
3533 if (!vim_iswordc(w[-1]))
3534 break;
3535 --w;
3536 }
3537 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003538 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003539 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3540 p += len;
3541 }
3542
3543 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003544 if (allocated)
3545 vim_free(arg);
3546 return OK;
3547 }
3548
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003549 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003550}
3551
3552/*
3553 * Put a string on the command line.
3554 * When "literally" is TRUE, insert literally.
3555 * When "literally" is FALSE, insert as typed, but don't leave the command
3556 * line.
3557 */
3558 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003559cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003560{
3561 int c, cv;
3562
3563 if (literally)
3564 put_on_cmdline(s, -1, TRUE);
3565 else
3566 while (*s != NUL)
3567 {
3568 cv = *s;
3569 if (cv == Ctrl_V && s[1])
3570 ++s;
3571#ifdef FEAT_MBYTE
3572 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003573 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003574 else
3575#endif
3576 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003577 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3578 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003579#ifdef UNIX
3580 || c == intr_char
3581#endif
3582 || (c == Ctrl_BSL && *s == Ctrl_N))
3583 stuffcharReadbuff(Ctrl_V);
3584 stuffcharReadbuff(c);
3585 }
3586}
3587
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#ifdef FEAT_WILDMENU
3589/*
3590 * Delete characters on the command line, from "from" to the current
3591 * position.
3592 */
3593 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003594cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3597 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3598 ccline.cmdlen -= ccline.cmdpos - from;
3599 ccline.cmdpos = from;
3600}
3601#endif
3602
3603/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003604 * This function is called when the screen size changes and with incremental
3605 * search and in other situations where the command line may have been
3606 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 */
3608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003609redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003611 redrawcmdline_ex(TRUE);
3612}
3613
3614 void
3615redrawcmdline_ex(int do_compute_cmdrow)
3616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 if (cmd_silent)
3618 return;
3619 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003620 if (do_compute_cmdrow)
3621 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 redrawcmd();
3623 cursorcmd();
3624}
3625
3626 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003627redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628{
3629 int i;
3630
3631 if (cmd_silent)
3632 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003633 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 msg_putchar(ccline.cmdfirstc);
3635 if (ccline.cmdprompt != NULL)
3636 {
3637 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3638 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3639 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003640 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 --ccline.cmdindent;
3642 }
3643 else
3644 for (i = ccline.cmdindent; i > 0; --i)
3645 msg_putchar(' ');
3646}
3647
3648/*
3649 * Redraw what is currently on the command line.
3650 */
3651 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003652redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 if (cmd_silent)
3655 return;
3656
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003657 /* when 'incsearch' is set there may be no command line while redrawing */
3658 if (ccline.cmdbuff == NULL)
3659 {
3660 windgoto(cmdline_row, 0);
3661 msg_clr_eos();
3662 return;
3663 }
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 msg_start();
3666 redrawcmdprompt();
3667
3668 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3669 msg_no_more = TRUE;
3670 draw_cmdline(0, ccline.cmdlen);
3671 msg_clr_eos();
3672 msg_no_more = FALSE;
3673
3674 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003675 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003676 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677
3678 /*
3679 * An emsg() before may have set msg_scroll. This is used in normal mode,
3680 * in cmdline mode we can reset them now.
3681 */
3682 msg_scroll = FALSE; /* next message overwrites cmdline */
3683
3684 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3685 * in cmdline mode */
3686 skip_redraw = FALSE;
3687}
3688
3689 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003690compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003692 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 cmdline_row = Rows - 1;
3694 else
3695 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003696 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697}
3698
3699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003700cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701{
3702 if (cmd_silent)
3703 return;
3704
3705#ifdef FEAT_RIGHTLEFT
3706 if (cmdmsg_rl)
3707 {
3708 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3709 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3710 if (msg_row <= 0)
3711 msg_row = Rows - 1;
3712 }
3713 else
3714#endif
3715 {
3716 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3717 msg_col = ccline.cmdspos % (int)Columns;
3718 if (msg_row >= Rows)
3719 msg_row = Rows - 1;
3720 }
3721
3722 windgoto(msg_row, msg_col);
3723#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003724 if (p_imst == IM_ON_THE_SPOT)
3725 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726#endif
3727#ifdef MCH_CURSOR_SHAPE
3728 mch_update_cursor();
3729#endif
3730}
3731
3732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003733gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
3735 msg_start();
3736#ifdef FEAT_RIGHTLEFT
3737 if (cmdmsg_rl)
3738 msg_col = Columns - 1;
3739 else
3740#endif
3741 msg_col = 0; /* always start in column 0 */
3742 if (clr) /* clear the bottom line(s) */
3743 msg_clr_eos(); /* will reset clear_cmdline */
3744 windgoto(cmdline_row, 0);
3745}
3746
3747/*
3748 * Check the word in front of the cursor for an abbreviation.
3749 * Called when the non-id character "c" has been entered.
3750 * When an abbreviation is recognized it is removed from the text with
3751 * backspaces and the replacement string is inserted, followed by "c".
3752 */
3753 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003754ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003756 int spos = 0;
3757
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3759 return FALSE;
3760
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003761 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3762 * Actually accepts any mark. */
3763 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3764 spos++;
3765 if (ccline.cmdlen - spos > 5
3766 && ccline.cmdbuff[spos] == '\''
3767 && ccline.cmdbuff[spos + 2] == ','
3768 && ccline.cmdbuff[spos + 3] == '\'')
3769 spos += 5;
3770 else
3771 /* check abbreviation from the beginning of the commandline */
3772 spos = 0;
3773
3774 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775}
3776
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003777#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3778 static int
3779#ifdef __BORLANDC__
3780_RTLENTRYF
3781#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003782sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003783{
3784 char_u *p1 = *(char_u **)s1;
3785 char_u *p2 = *(char_u **)s2;
3786
3787 if (*p1 != '<' && *p2 == '<') return -1;
3788 if (*p1 == '<' && *p2 != '<') return 1;
3789 return STRCMP(p1, p2);
3790}
3791#endif
3792
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793/*
3794 * Return FAIL if this is not an appropriate context in which to do
3795 * completion of anything, return OK if it is (even if there are no matches).
3796 * For the caller, this means that the character is just passed through like a
3797 * normal character (instead of being expanded). This allows :s/^I^D etc.
3798 */
3799 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003800nextwild(
3801 expand_T *xp,
3802 int type,
3803 int options, /* extra options for ExpandOne() */
3804 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 int i, j;
3807 char_u *p1;
3808 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 int difflen;
3810 int v;
3811
3812 if (xp->xp_numfiles == -1)
3813 {
3814 set_expand_context(xp);
3815 cmd_showtail = expand_showtail(xp);
3816 }
3817
3818 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3819 {
3820 beep_flush();
3821 return OK; /* Something illegal on command line */
3822 }
3823 if (xp->xp_context == EXPAND_NOTHING)
3824 {
3825 /* Caller can use the character as a normal char instead */
3826 return FAIL;
3827 }
3828
3829 MSG_PUTS("..."); /* show that we are busy */
3830 out_flush();
3831
3832 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003833 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834
3835 if (type == WILD_NEXT || type == WILD_PREV)
3836 {
3837 /*
3838 * Get next/previous match for a previous expanded pattern.
3839 */
3840 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3841 }
3842 else
3843 {
3844 /*
3845 * Translate string into pattern and expand it.
3846 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003847 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3848 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 p2 = NULL;
3850 else
3851 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003852 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003853 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3854 if (escape)
3855 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003856
3857 if (p_wic)
3858 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003859 p2 = ExpandOne(xp, p1,
3860 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003861 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003863 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (p2 != NULL && type == WILD_LONGEST)
3865 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003866 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 if (ccline.cmdbuff[i + j] == '*'
3868 || ccline.cmdbuff[i + j] == '?')
3869 break;
3870 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003871 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 }
3873 }
3874 }
3875
3876 if (p2 != NULL && !got_int)
3877 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003878 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003879 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003881 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 xp->xp_pattern = ccline.cmdbuff + i;
3883 }
3884 else
3885 v = OK;
3886 if (v == OK)
3887 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003888 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3889 &ccline.cmdbuff[ccline.cmdpos],
3890 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3891 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 ccline.cmdlen += difflen;
3893 ccline.cmdpos += difflen;
3894 }
3895 }
3896 vim_free(p2);
3897
3898 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003899 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900
3901 /* When expanding a ":map" command and no matches are found, assume that
3902 * the key is supposed to be inserted literally */
3903 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3904 return FAIL;
3905
3906 if (xp->xp_numfiles <= 0 && p2 == NULL)
3907 beep_flush();
3908 else if (xp->xp_numfiles == 1)
3909 /* free expanded pattern */
3910 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3911
3912 return OK;
3913}
3914
3915/*
3916 * Do wildcard expansion on the string 'str'.
3917 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003918 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 * Return NULL for failure.
3920 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003921 * "orig" is the originally expanded string, copied to allocated memory. It
3922 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3923 * WILD_PREV "orig" should be NULL.
3924 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003925 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3926 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 *
3928 * mode = WILD_FREE: just free previously expanded matches
3929 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3930 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3931 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3932 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3933 * mode = WILD_ALL: return all matches concatenated
3934 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003935 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 *
3937 * options = WILD_LIST_NOTFOUND: list entries without a match
3938 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3939 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3940 * options = WILD_NO_BEEP: Don't beep for multiple matches
3941 * options = WILD_ADD_SLASH: add a slash after directory names
3942 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3943 * options = WILD_SILENT: don't print warning messages
3944 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003945 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 *
3947 * The variables xp->xp_context and xp->xp_backslash must have been set!
3948 */
3949 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003950ExpandOne(
3951 expand_T *xp,
3952 char_u *str,
3953 char_u *orig, /* allocated copy of original of expanded string */
3954 int options,
3955 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956{
3957 char_u *ss = NULL;
3958 static int findex;
3959 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003960 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 int i;
3962 long_u len;
3963 int non_suf_match; /* number without matching suffix */
3964
3965 /*
3966 * first handle the case of using an old match
3967 */
3968 if (mode == WILD_NEXT || mode == WILD_PREV)
3969 {
3970 if (xp->xp_numfiles > 0)
3971 {
3972 if (mode == WILD_PREV)
3973 {
3974 if (findex == -1)
3975 findex = xp->xp_numfiles;
3976 --findex;
3977 }
3978 else /* mode == WILD_NEXT */
3979 ++findex;
3980
3981 /*
3982 * When wrapping around, return the original string, set findex to
3983 * -1.
3984 */
3985 if (findex < 0)
3986 {
3987 if (orig_save == NULL)
3988 findex = xp->xp_numfiles - 1;
3989 else
3990 findex = -1;
3991 }
3992 if (findex >= xp->xp_numfiles)
3993 {
3994 if (orig_save == NULL)
3995 findex = 0;
3996 else
3997 findex = -1;
3998 }
3999#ifdef FEAT_WILDMENU
4000 if (p_wmnu)
4001 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4002 findex, cmd_showtail);
4003#endif
4004 if (findex == -1)
4005 return vim_strsave(orig_save);
4006 return vim_strsave(xp->xp_files[findex]);
4007 }
4008 else
4009 return NULL;
4010 }
4011
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004012 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4014 {
4015 FreeWild(xp->xp_numfiles, xp->xp_files);
4016 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004017 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 findex = 0;
4020
4021 if (mode == WILD_FREE) /* only release file name */
4022 return NULL;
4023
4024 if (xp->xp_numfiles == -1)
4025 {
4026 vim_free(orig_save);
4027 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004028 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029
4030 /*
4031 * Do the expansion.
4032 */
4033 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4034 options) == FAIL)
4035 {
4036#ifdef FNAME_ILLEGAL
4037 /* Illegal file name has been silently skipped. But when there
4038 * are wildcards, the real problem is that there was no match,
4039 * causing the pattern to be added, which has illegal characters.
4040 */
4041 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4042 EMSG2(_(e_nomatch2), str);
4043#endif
4044 }
4045 else if (xp->xp_numfiles == 0)
4046 {
4047 if (!(options & WILD_SILENT))
4048 EMSG2(_(e_nomatch2), str);
4049 }
4050 else
4051 {
4052 /* Escape the matches for use on the command line. */
4053 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4054
4055 /*
4056 * Check for matching suffixes in file names.
4057 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004058 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4059 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
4061 if (xp->xp_numfiles)
4062 non_suf_match = xp->xp_numfiles;
4063 else
4064 non_suf_match = 1;
4065 if ((xp->xp_context == EXPAND_FILES
4066 || xp->xp_context == EXPAND_DIRECTORIES)
4067 && xp->xp_numfiles > 1)
4068 {
4069 /*
4070 * More than one match; check suffix.
4071 * The files will have been sorted on matching suffix in
4072 * expand_wildcards, only need to check the first two.
4073 */
4074 non_suf_match = 0;
4075 for (i = 0; i < 2; ++i)
4076 if (match_suffix(xp->xp_files[i]))
4077 ++non_suf_match;
4078 }
4079 if (non_suf_match != 1)
4080 {
4081 /* Can we ever get here unless it's while expanding
4082 * interactively? If not, we can get rid of this all
4083 * together. Don't really want to wait for this message
4084 * (and possibly have to hit return to continue!).
4085 */
4086 if (!(options & WILD_SILENT))
4087 EMSG(_(e_toomany));
4088 else if (!(options & WILD_NO_BEEP))
4089 beep_flush();
4090 }
4091 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4092 ss = vim_strsave(xp->xp_files[0]);
4093 }
4094 }
4095 }
4096
4097 /* Find longest common part */
4098 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4099 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004100 int mb_len = 1;
4101 int c0, ci;
4102
4103 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004105#ifdef FEAT_MBYTE
4106 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004108 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4109 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4110 }
4111 else
4112#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004113 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004114 for (i = 1; i < xp->xp_numfiles; ++i)
4115 {
4116#ifdef FEAT_MBYTE
4117 if (has_mbyte)
4118 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4119 else
4120#endif
4121 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004122 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004124 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004125 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004127 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 break;
4129 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004130 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 break;
4132 }
4133 if (i < xp->xp_numfiles)
4134 {
4135 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004136 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 break;
4138 }
4139 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004140
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 ss = alloc((unsigned)len + 1);
4142 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004143 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 findex = -1; /* next p_wc gets first one */
4145 }
4146
4147 /* Concatenate all matching names */
4148 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4149 {
4150 len = 0;
4151 for (i = 0; i < xp->xp_numfiles; ++i)
4152 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4153 ss = lalloc(len, TRUE);
4154 if (ss != NULL)
4155 {
4156 *ss = NUL;
4157 for (i = 0; i < xp->xp_numfiles; ++i)
4158 {
4159 STRCAT(ss, xp->xp_files[i]);
4160 if (i != xp->xp_numfiles - 1)
4161 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4162 }
4163 }
4164 }
4165
4166 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4167 ExpandCleanup(xp);
4168
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004169 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004170 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004171 vim_free(orig);
4172
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 return ss;
4174}
4175
4176/*
4177 * Prepare an expand structure for use.
4178 */
4179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004180ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004182 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004183 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004185#ifndef BACKSLASH_IN_FILENAME
4186 xp->xp_shell = FALSE;
4187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 xp->xp_numfiles = -1;
4189 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004190#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4191 xp->xp_arg = NULL;
4192#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004193 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194}
4195
4196/*
4197 * Cleanup an expand structure after use.
4198 */
4199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004200ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
4202 if (xp->xp_numfiles >= 0)
4203 {
4204 FreeWild(xp->xp_numfiles, xp->xp_files);
4205 xp->xp_numfiles = -1;
4206 }
4207}
4208
4209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004210ExpandEscape(
4211 expand_T *xp,
4212 char_u *str,
4213 int numfiles,
4214 char_u **files,
4215 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 int i;
4218 char_u *p;
4219
4220 /*
4221 * May change home directory back to "~"
4222 */
4223 if (options & WILD_HOME_REPLACE)
4224 tilde_replace(str, numfiles, files);
4225
4226 if (options & WILD_ESCAPE)
4227 {
4228 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004229 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004230 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 || xp->xp_context == EXPAND_BUFFERS
4232 || xp->xp_context == EXPAND_DIRECTORIES)
4233 {
4234 /*
4235 * Insert a backslash into a file name before a space, \, %, #
4236 * and wildmatch characters, except '~'.
4237 */
4238 for (i = 0; i < numfiles; ++i)
4239 {
4240 /* for ":set path=" we need to escape spaces twice */
4241 if (xp->xp_backslash == XP_BS_THREE)
4242 {
4243 p = vim_strsave_escaped(files[i], (char_u *)" ");
4244 if (p != NULL)
4245 {
4246 vim_free(files[i]);
4247 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004248#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 p = vim_strsave_escaped(files[i], (char_u *)" ");
4250 if (p != NULL)
4251 {
4252 vim_free(files[i]);
4253 files[i] = p;
4254 }
4255#endif
4256 }
4257 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004258#ifdef BACKSLASH_IN_FILENAME
4259 p = vim_strsave_fnameescape(files[i], FALSE);
4260#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004261 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 if (p != NULL)
4264 {
4265 vim_free(files[i]);
4266 files[i] = p;
4267 }
4268
4269 /* If 'str' starts with "\~", replace "~" at start of
4270 * files[i] with "\~". */
4271 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004272 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004275
4276 /* If the first file starts with a '+' escape it. Otherwise it
4277 * could be seen as "+cmd". */
4278 if (*files[0] == '+')
4279 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 }
4281 else if (xp->xp_context == EXPAND_TAGS)
4282 {
4283 /*
4284 * Insert a backslash before characters in a tag name that
4285 * would terminate the ":tag" command.
4286 */
4287 for (i = 0; i < numfiles; ++i)
4288 {
4289 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4290 if (p != NULL)
4291 {
4292 vim_free(files[i]);
4293 files[i] = p;
4294 }
4295 }
4296 }
4297 }
4298}
4299
4300/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004301 * Escape special characters in "fname" for when used as a file name argument
4302 * after a Vim command, or, when "shell" is non-zero, a shell command.
4303 * Returns the result in allocated memory.
4304 */
4305 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004306vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004307{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004308 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004309#ifdef BACKSLASH_IN_FILENAME
4310 char_u buf[20];
4311 int j = 0;
4312
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004313 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004314 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004315 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004316 buf[j++] = *p;
4317 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004318 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004319#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004320 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4321 if (shell && csh_like_shell() && p != NULL)
4322 {
4323 char_u *s;
4324
4325 /* For csh and similar shells need to put two backslashes before '!'.
4326 * One is taken by Vim, one by the shell. */
4327 s = vim_strsave_escaped(p, (char_u *)"!");
4328 vim_free(p);
4329 p = s;
4330 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004331#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004332
4333 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4334 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004335 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004336 escape_fname(&p);
4337
4338 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004339}
4340
4341/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004342 * Put a backslash before the file name in "pp", which is in allocated memory.
4343 */
4344 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004345escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004346{
4347 char_u *p;
4348
4349 p = alloc((unsigned)(STRLEN(*pp) + 2));
4350 if (p != NULL)
4351 {
4352 p[0] = '\\';
4353 STRCPY(p + 1, *pp);
4354 vim_free(*pp);
4355 *pp = p;
4356 }
4357}
4358
4359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 * For each file name in files[num_files]:
4361 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4362 */
4363 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004364tilde_replace(
4365 char_u *orig_pat,
4366 int num_files,
4367 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368{
4369 int i;
4370 char_u *p;
4371
4372 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4373 {
4374 for (i = 0; i < num_files; ++i)
4375 {
4376 p = home_replace_save(NULL, files[i]);
4377 if (p != NULL)
4378 {
4379 vim_free(files[i]);
4380 files[i] = p;
4381 }
4382 }
4383 }
4384}
4385
4386/*
4387 * Show all matches for completion on the command line.
4388 * Returns EXPAND_NOTHING when the character that triggered expansion should
4389 * be inserted like a normal character.
4390 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004392showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4395 int num_files;
4396 char_u **files_found;
4397 int i, j, k;
4398 int maxlen;
4399 int lines;
4400 int columns;
4401 char_u *p;
4402 int lastlen;
4403 int attr;
4404 int showtail;
4405
4406 if (xp->xp_numfiles == -1)
4407 {
4408 set_expand_context(xp);
4409 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4410 &num_files, &files_found);
4411 showtail = expand_showtail(xp);
4412 if (i != EXPAND_OK)
4413 return i;
4414
4415 }
4416 else
4417 {
4418 num_files = xp->xp_numfiles;
4419 files_found = xp->xp_files;
4420 showtail = cmd_showtail;
4421 }
4422
4423#ifdef FEAT_WILDMENU
4424 if (!wildmenu)
4425 {
4426#endif
4427 msg_didany = FALSE; /* lines_left will be set */
4428 msg_start(); /* prepare for paging */
4429 msg_putchar('\n');
4430 out_flush();
4431 cmdline_row = msg_row;
4432 msg_didany = FALSE; /* lines_left will be set again */
4433 msg_start(); /* prepare for paging */
4434#ifdef FEAT_WILDMENU
4435 }
4436#endif
4437
4438 if (got_int)
4439 got_int = FALSE; /* only int. the completion, not the cmd line */
4440#ifdef FEAT_WILDMENU
4441 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004442 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443#endif
4444 else
4445 {
4446 /* find the length of the longest file name */
4447 maxlen = 0;
4448 for (i = 0; i < num_files; ++i)
4449 {
4450 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004451 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 || xp->xp_context == EXPAND_BUFFERS))
4453 {
4454 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4455 j = vim_strsize(NameBuff);
4456 }
4457 else
4458 j = vim_strsize(L_SHOWFILE(i));
4459 if (j > maxlen)
4460 maxlen = j;
4461 }
4462
4463 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4464 lines = num_files;
4465 else
4466 {
4467 /* compute the number of columns and lines for the listing */
4468 maxlen += 2; /* two spaces between file names */
4469 columns = ((int)Columns + 2) / maxlen;
4470 if (columns < 1)
4471 columns = 1;
4472 lines = (num_files + columns - 1) / columns;
4473 }
4474
Bram Moolenaar8820b482017-03-16 17:23:31 +01004475 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476
4477 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4478 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004479 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 msg_clr_eos();
4481 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004482 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 }
4484
4485 /* list the files line by line */
4486 for (i = 0; i < lines; ++i)
4487 {
4488 lastlen = 999;
4489 for (k = i; k < num_files; k += lines)
4490 {
4491 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4492 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004493 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 p = files_found[k] + STRLEN(files_found[k]) + 1;
4495 msg_advance(maxlen + 1);
4496 msg_puts(p);
4497 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004498 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 break;
4500 }
4501 for (j = maxlen - lastlen; --j >= 0; )
4502 msg_putchar(' ');
4503 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004504 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 || xp->xp_context == EXPAND_BUFFERS)
4506 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004507 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004508 if (xp->xp_numfiles != -1)
4509 {
4510 char_u *halved_slash;
4511 char_u *exp_path;
4512
4513 /* Expansion was done before and special characters
4514 * were escaped, need to halve backslashes. Also
4515 * $HOME has been replaced with ~/. */
4516 exp_path = expand_env_save_opt(files_found[k], TRUE);
4517 halved_slash = backslash_halve_save(
4518 exp_path != NULL ? exp_path : files_found[k]);
4519 j = mch_isdir(halved_slash != NULL ? halved_slash
4520 : files_found[k]);
4521 vim_free(exp_path);
4522 vim_free(halved_slash);
4523 }
4524 else
4525 /* Expansion was done here, file names are literal. */
4526 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (showtail)
4528 p = L_SHOWFILE(k);
4529 else
4530 {
4531 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4532 TRUE);
4533 p = NameBuff;
4534 }
4535 }
4536 else
4537 {
4538 j = FALSE;
4539 p = L_SHOWFILE(k);
4540 }
4541 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4542 }
4543 if (msg_col > 0) /* when not wrapped around */
4544 {
4545 msg_clr_eos();
4546 msg_putchar('\n');
4547 }
4548 out_flush(); /* show one line at a time */
4549 if (got_int)
4550 {
4551 got_int = FALSE;
4552 break;
4553 }
4554 }
4555
4556 /*
4557 * we redraw the command below the lines that we have just listed
4558 * This is a bit tricky, but it saves a lot of screen updating.
4559 */
4560 cmdline_row = msg_row; /* will put it back later */
4561 }
4562
4563 if (xp->xp_numfiles == -1)
4564 FreeWild(num_files, files_found);
4565
4566 return EXPAND_OK;
4567}
4568
4569/*
4570 * Private gettail for showmatches() (and win_redr_status_matches()):
4571 * Find tail of file name path, but ignore trailing "/".
4572 */
4573 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004574sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575{
4576 char_u *p;
4577 char_u *t = s;
4578 int had_sep = FALSE;
4579
4580 for (p = s; *p != NUL; )
4581 {
4582 if (vim_ispathsep(*p)
4583#ifdef BACKSLASH_IN_FILENAME
4584 && !rem_backslash(p)
4585#endif
4586 )
4587 had_sep = TRUE;
4588 else if (had_sep)
4589 {
4590 t = p;
4591 had_sep = FALSE;
4592 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004593 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 }
4595 return t;
4596}
4597
4598/*
4599 * Return TRUE if we only need to show the tail of completion matches.
4600 * When not completing file names or there is a wildcard in the path FALSE is
4601 * returned.
4602 */
4603 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 char_u *s;
4607 char_u *end;
4608
4609 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004610 if (xp->xp_context != EXPAND_FILES
4611 && xp->xp_context != EXPAND_SHELLCMD
4612 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 return FALSE;
4614
4615 end = gettail(xp->xp_pattern);
4616 if (end == xp->xp_pattern) /* there is no path separator */
4617 return FALSE;
4618
4619 for (s = xp->xp_pattern; s < end; s++)
4620 {
4621 /* Skip escaped wildcards. Only when the backslash is not a path
4622 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4623 if (rem_backslash(s))
4624 ++s;
4625 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4626 return FALSE;
4627 }
4628 return TRUE;
4629}
4630
4631/*
4632 * Prepare a string for expansion.
4633 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004634 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 * When expanding other names: The string will be used with regcomp(). Copy
4636 * the name into allocated memory and prepend "^".
4637 */
4638 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004639addstar(
4640 char_u *fname,
4641 int len,
4642 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643{
4644 char_u *retval;
4645 int i, j;
4646 int new_len;
4647 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004648 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004650 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004651 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004652 && context != EXPAND_SHELLCMD
4653 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 {
4655 /*
4656 * Matching will be done internally (on something other than files).
4657 * So we convert the file-matching-type wildcards into our kind for
4658 * use with vim_regcomp(). First work out how long it will be:
4659 */
4660
4661 /* For help tags the translation is done in find_help_tags().
4662 * For a tag pattern starting with "/" no translation is needed. */
4663 if (context == EXPAND_HELP
4664 || context == EXPAND_COLORS
4665 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004666 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004667 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004668 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004669 || ((context == EXPAND_TAGS_LISTFILES
4670 || context == EXPAND_TAGS)
4671 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 retval = vim_strnsave(fname, len);
4673 else
4674 {
4675 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4676 for (i = 0; i < len; i++)
4677 {
4678 if (fname[i] == '*' || fname[i] == '~')
4679 new_len++; /* '*' needs to be replaced by ".*"
4680 '~' needs to be replaced by "\~" */
4681
4682 /* Buffer names are like file names. "." should be literal */
4683 if (context == EXPAND_BUFFERS && fname[i] == '.')
4684 new_len++; /* "." becomes "\." */
4685
4686 /* Custom expansion takes care of special things, match
4687 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004688 if ((context == EXPAND_USER_DEFINED
4689 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 new_len++; /* '\' becomes "\\" */
4691 }
4692 retval = alloc(new_len);
4693 if (retval != NULL)
4694 {
4695 retval[0] = '^';
4696 j = 1;
4697 for (i = 0; i < len; i++, j++)
4698 {
4699 /* Skip backslash. But why? At least keep it for custom
4700 * expansion. */
4701 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004702 && context != EXPAND_USER_LIST
4703 && fname[i] == '\\'
4704 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 break;
4706
4707 switch (fname[i])
4708 {
4709 case '*': retval[j++] = '.';
4710 break;
4711 case '~': retval[j++] = '\\';
4712 break;
4713 case '?': retval[j] = '.';
4714 continue;
4715 case '.': if (context == EXPAND_BUFFERS)
4716 retval[j++] = '\\';
4717 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004718 case '\\': if (context == EXPAND_USER_DEFINED
4719 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 retval[j++] = '\\';
4721 break;
4722 }
4723 retval[j] = fname[i];
4724 }
4725 retval[j] = NUL;
4726 }
4727 }
4728 }
4729 else
4730 {
4731 retval = alloc(len + 4);
4732 if (retval != NULL)
4733 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004734 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004737 * Don't add a star to *, ~, ~user, $var or `cmd`.
4738 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 * ~ would be at the start of the file name, but not the tail.
4740 * $ could be anywhere in the tail.
4741 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004742 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 */
4744 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004745 ends_in_star = (len > 0 && retval[len - 1] == '*');
4746#ifndef BACKSLASH_IN_FILENAME
4747 for (i = len - 2; i >= 0; --i)
4748 {
4749 if (retval[i] != '\\')
4750 break;
4751 ends_in_star = !ends_in_star;
4752 }
4753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004755 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 && vim_strchr(tail, '$') == NULL
4757 && vim_strchr(retval, '`') == NULL)
4758 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004759 else if (len > 0 && retval[len - 1] == '$')
4760 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 retval[len] = NUL;
4762 }
4763 }
4764 return retval;
4765}
4766
4767/*
4768 * Must parse the command line so far to work out what context we are in.
4769 * Completion can then be done based on that context.
4770 * This routine sets the variables:
4771 * xp->xp_pattern The start of the pattern to be expanded within
4772 * the command line (ends at the cursor).
4773 * xp->xp_context The type of thing to expand. Will be one of:
4774 *
4775 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4776 * the command line, like an unknown command. Caller
4777 * should beep.
4778 * EXPAND_NOTHING Unrecognised context for completion, use char like
4779 * a normal char, rather than for completion. eg
4780 * :s/^I/
4781 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4782 * it.
4783 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4784 * EXPAND_FILES After command with XFILE set, or after setting
4785 * with P_EXPAND set. eg :e ^I, :w>>^I
4786 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4787 * when we know only directories are of interest. eg
4788 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004789 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4791 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4792 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4793 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4794 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4795 * EXPAND_EVENTS Complete event names
4796 * EXPAND_SYNTAX Complete :syntax command arguments
4797 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4798 * EXPAND_AUGROUP Complete autocommand group names
4799 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4800 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4801 * eg :unmap a^I , :cunab x^I
4802 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4803 * eg :call sub^I
4804 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4805 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4806 * names in expressions, eg :while s^I
4807 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004808 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 */
4810 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004811set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004813 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (ccline.cmdfirstc != ':'
4815#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004816 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004817 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818#endif
4819 )
4820 {
4821 xp->xp_context = EXPAND_NOTHING;
4822 return;
4823 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004824 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825}
4826
4827 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004828set_cmd_context(
4829 expand_T *xp,
4830 char_u *str, /* start of command line */
4831 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004832 int col, /* position of cursor */
4833 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 int old_char = NUL;
4836 char_u *nextcomm;
4837
4838 /*
4839 * Avoid a UMR warning from Purify, only save the character if it has been
4840 * written before.
4841 */
4842 if (col < len)
4843 old_char = str[col];
4844 str[col] = NUL;
4845 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004846
4847#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004848 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004849 {
4850# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004851 /* pass CMD_SIZE because there is no real command */
4852 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004853# endif
4854 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004855 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004856 {
4857 xp->xp_context = ccline.xp_context;
4858 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004859# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004860 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004861# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004862 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004863 else
4864#endif
4865 while (nextcomm != NULL)
4866 nextcomm = set_one_cmd_context(xp, nextcomm);
4867
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004868 /* Store the string here so that call_user_expand_func() can get to them
4869 * easily. */
4870 xp->xp_line = str;
4871 xp->xp_col = col;
4872
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 str[col] = old_char;
4874}
4875
4876/*
4877 * Expand the command line "str" from context "xp".
4878 * "xp" must have been set by set_cmd_context().
4879 * xp->xp_pattern points into "str", to where the text that is to be expanded
4880 * starts.
4881 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4882 * cursor.
4883 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4884 * key that triggered expansion literally.
4885 * Returns EXPAND_OK otherwise.
4886 */
4887 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004888expand_cmdline(
4889 expand_T *xp,
4890 char_u *str, /* start of command line */
4891 int col, /* position of cursor */
4892 int *matchcount, /* return: nr of matches */
4893 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894{
4895 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004896 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
4898 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4899 {
4900 beep_flush();
4901 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4902 }
4903 if (xp->xp_context == EXPAND_NOTHING)
4904 {
4905 /* Caller can use the character as a normal char instead */
4906 return EXPAND_NOTHING;
4907 }
4908
4909 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004910 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4911 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 if (file_str == NULL)
4913 return EXPAND_UNSUCCESSFUL;
4914
Bram Moolenaar94950a92010-12-02 16:01:29 +01004915 if (p_wic)
4916 options += WILD_ICASE;
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004919 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 *matchcount = 0;
4922 *matches = NULL;
4923 }
4924 vim_free(file_str);
4925
4926 return EXPAND_OK;
4927}
4928
4929#ifdef FEAT_MULTI_LANG
4930/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004931 * Cleanup matches for help tags:
4932 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4933 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004935static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936
4937 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004938cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939{
4940 int i, j;
4941 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004942 char_u buf[4];
4943 char_u *p = buf;
4944
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004945 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004946 {
4947 *p++ = '@';
4948 *p++ = p_hlg[0];
4949 *p++ = p_hlg[1];
4950 }
4951 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952
4953 for (i = 0; i < num_file; ++i)
4954 {
4955 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004956 if (len <= 0)
4957 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004958 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 /* Sorting on priority means the same item in another language may
4961 * be anywhere. Search all items for a match up to the "@en". */
4962 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004963 if (j != i && (int)STRLEN(file[j]) == len + 3
4964 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 break;
4966 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004967 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 file[i][len] = NUL;
4969 }
4970 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004971
4972 if (*buf != NUL)
4973 for (i = 0; i < num_file; ++i)
4974 {
4975 len = (int)STRLEN(file[i]) - 3;
4976 if (len <= 0)
4977 continue;
4978 if (STRCMP(file[i] + len, buf) == 0)
4979 {
4980 /* remove the default language */
4981 file[i][len] = NUL;
4982 }
4983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984}
4985#endif
4986
4987/*
4988 * Do the expansion based on xp->xp_context and "pat".
4989 */
4990 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004991ExpandFromContext(
4992 expand_T *xp,
4993 char_u *pat,
4994 int *num_file,
4995 char_u ***file,
4996 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997{
4998#ifdef FEAT_CMDL_COMPL
4999 regmatch_T regmatch;
5000#endif
5001 int ret;
5002 int flags;
5003
5004 flags = EW_DIR; /* include directories */
5005 if (options & WILD_LIST_NOTFOUND)
5006 flags |= EW_NOTFOUND;
5007 if (options & WILD_ADD_SLASH)
5008 flags |= EW_ADDSLASH;
5009 if (options & WILD_KEEP_ALL)
5010 flags |= EW_KEEPALL;
5011 if (options & WILD_SILENT)
5012 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005013 if (options & WILD_ALLLINKS)
5014 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005016 if (xp->xp_context == EXPAND_FILES
5017 || xp->xp_context == EXPAND_DIRECTORIES
5018 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
5020 /*
5021 * Expand file or directory names.
5022 */
5023 int free_pat = FALSE;
5024 int i;
5025
5026 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5027 * space */
5028 if (xp->xp_backslash != XP_BS_NONE)
5029 {
5030 free_pat = TRUE;
5031 pat = vim_strsave(pat);
5032 for (i = 0; pat[i]; ++i)
5033 if (pat[i] == '\\')
5034 {
5035 if (xp->xp_backslash == XP_BS_THREE
5036 && pat[i + 1] == '\\'
5037 && pat[i + 2] == '\\'
5038 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005039 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 if (xp->xp_backslash == XP_BS_ONE
5041 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005042 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 }
5045
5046 if (xp->xp_context == EXPAND_FILES)
5047 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005048 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5049 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 else
5051 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005052 if (options & WILD_ICASE)
5053 flags |= EW_ICASE;
5054
Bram Moolenaard7834d32009-12-02 16:14:36 +00005055 /* Expand wildcards, supporting %:h and the like. */
5056 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (free_pat)
5058 vim_free(pat);
5059 return ret;
5060 }
5061
5062 *file = (char_u **)"";
5063 *num_file = 0;
5064 if (xp->xp_context == EXPAND_HELP)
5065 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005066 /* With an empty argument we would get all the help tags, which is
5067 * very slow. Get matches for "help" instead. */
5068 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5069 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 {
5071#ifdef FEAT_MULTI_LANG
5072 cleanup_help_tags(*num_file, *file);
5073#endif
5074 return OK;
5075 }
5076 return FAIL;
5077 }
5078
5079#ifndef FEAT_CMDL_COMPL
5080 return FAIL;
5081#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005082 if (xp->xp_context == EXPAND_SHELLCMD)
5083 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (xp->xp_context == EXPAND_OLD_SETTING)
5085 return ExpandOldSetting(num_file, file);
5086 if (xp->xp_context == EXPAND_BUFFERS)
5087 return ExpandBufnames(pat, num_file, file, options);
5088 if (xp->xp_context == EXPAND_TAGS
5089 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5090 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5091 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005092 {
5093 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005094 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5095 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005098 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005099 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005100 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005101 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005102 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005103 {
5104 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005105 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005106 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005107 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005108 {
5109 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005110 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005111 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005112# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5113 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005114 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005115# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005116 if (xp->xp_context == EXPAND_PACKADD)
5117 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
5119 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5120 if (regmatch.regprog == NULL)
5121 return FAIL;
5122
5123 /* set ignore-case according to p_ic, p_scs and pat */
5124 regmatch.rm_ic = ignorecase(pat);
5125
5126 if (xp->xp_context == EXPAND_SETTINGS
5127 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5128 ret = ExpandSettings(xp, &regmatch, num_file, file);
5129 else if (xp->xp_context == EXPAND_MAPPINGS)
5130 ret = ExpandMappings(&regmatch, num_file, file);
5131# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5132 else if (xp->xp_context == EXPAND_USER_DEFINED)
5133 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5134# endif
5135 else
5136 {
5137 static struct expgen
5138 {
5139 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005140 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005142 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 } tab[] =
5144 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005145 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5146 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005147 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005148 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005149#ifdef FEAT_CMDHIST
5150 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5151#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005153 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005154 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005155 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5156 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5157 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158#endif
5159#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005160 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5161 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5162 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5163 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164#endif
5165#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005166 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5167 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#endif
5169#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005170 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005172#ifdef FEAT_PROFILE
5173 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5174#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005175 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005176 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5177 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005178#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005179 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005180#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005181#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005182 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005183#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005184#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005185 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5188 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005189 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5190 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005192 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005193 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005194 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 };
5196 int i;
5197
5198 /*
5199 * Find a context in the table and call the ExpandGeneric() with the
5200 * right function to do the expansion.
5201 */
5202 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005203 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (xp->xp_context == tab[i].context)
5205 {
5206 if (tab[i].ic)
5207 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005208 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005209 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 break;
5211 }
5212 }
5213
Bram Moolenaar473de612013-06-08 18:19:48 +02005214 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215
5216 return ret;
5217#endif /* FEAT_CMDL_COMPL */
5218}
5219
5220#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5221/*
5222 * Expand a list of names.
5223 *
5224 * Generic function for command line completion. It calls a function to
5225 * obtain strings, one by one. The strings are matched against a regexp
5226 * program. Matching strings are copied into an array, which is returned.
5227 *
5228 * Returns OK when no problems encountered, FAIL for error (out of memory).
5229 */
5230 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005231ExpandGeneric(
5232 expand_T *xp,
5233 regmatch_T *regmatch,
5234 int *num_file,
5235 char_u ***file,
5236 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005238 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239{
5240 int i;
5241 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005242 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 char_u *str;
5244
5245 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005246 * round == 0: count the number of matching names
5247 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005249 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
5251 for (i = 0; ; ++i)
5252 {
5253 str = (*func)(xp, i);
5254 if (str == NULL) /* end of list */
5255 break;
5256 if (*str == NUL) /* skip empty strings */
5257 continue;
5258
5259 if (vim_regexec(regmatch, str, (colnr_T)0))
5260 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005261 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005263 if (escaped)
5264 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5265 else
5266 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 (*file)[count] = str;
5268#ifdef FEAT_MENU
5269 if (func == get_menu_names && str != NULL)
5270 {
5271 /* test for separator added by get_menu_names() */
5272 str += STRLEN(str) - 1;
5273 if (*str == '\001')
5274 *str = '.';
5275 }
5276#endif
5277 }
5278 ++count;
5279 }
5280 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005281 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
5283 if (count == 0)
5284 return OK;
5285 *num_file = count;
5286 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5287 if (*file == NULL)
5288 {
5289 *file = (char_u **)"";
5290 return FAIL;
5291 }
5292 count = 0;
5293 }
5294 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005295
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005296 /* Sort the results. Keep menu's in the specified order. */
5297 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005298 {
5299 if (xp->xp_context == EXPAND_EXPRESSION
5300 || xp->xp_context == EXPAND_FUNCTIONS
5301 || xp->xp_context == EXPAND_USER_FUNC)
5302 /* <SNR> functions should be sorted to the end. */
5303 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5304 sort_func_compare);
5305 else
5306 sort_strings(*file, *num_file);
5307 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005308
Bram Moolenaar4f688582007-07-24 12:34:30 +00005309#ifdef FEAT_CMDL_COMPL
5310 /* Reset the variables used for special highlight names expansion, so that
5311 * they don't show up when getting normal highlight names by ID. */
5312 reset_expand_highlight();
5313#endif
5314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 return OK;
5316}
5317
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005318/*
5319 * Complete a shell command.
5320 * Returns FAIL or OK;
5321 */
5322 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005323expand_shellcmd(
5324 char_u *filepat, /* pattern to match with command names */
5325 int *num_file, /* return: number of matches */
5326 char_u ***file, /* return: array with matches */
5327 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005328{
5329 char_u *pat;
5330 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005331 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005332 int mustfree = FALSE;
5333 garray_T ga;
5334 char_u *buf = alloc(MAXPATHL);
5335 size_t l;
5336 char_u *s, *e;
5337 int flags = flagsarg;
5338 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005339 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005340 hashtab_T found_ht;
5341 hashitem_T *hi;
5342 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005343
5344 if (buf == NULL)
5345 return FAIL;
5346
5347 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5348 * space */
5349 pat = vim_strsave(filepat);
5350 for (i = 0; pat[i]; ++i)
5351 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005352 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005353
Bram Moolenaarb5971142015-03-21 17:32:19 +01005354 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005355
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005356 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5357 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005358 path = (char_u *)".";
5359 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005360 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005361 /* For an absolute name we don't use $PATH. */
5362 if (!mch_isFullName(pat))
5363 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005364 if (path == NULL)
5365 path = (char_u *)"";
5366 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005367
5368 /*
5369 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005370 * collect them in "ga". When "." is not in $PATH also expand for the
5371 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005372 */
5373 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005374 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005375 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005376 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005377#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005378 e = vim_strchr(s, ';');
5379#else
5380 e = vim_strchr(s, ':');
5381#endif
5382 if (e == NULL)
5383 e = s + STRLEN(s);
5384
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005385 if (*s == NUL)
5386 {
5387 if (did_curdir)
5388 break;
5389 // Find directories in the current directory, path is empty.
5390 did_curdir = TRUE;
5391 flags |= EW_DIR;
5392 }
5393 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5394 {
5395 did_curdir = TRUE;
5396 flags |= EW_DIR;
5397 }
5398 else
5399 // Do not match directories inside a $PATH item.
5400 flags &= ~EW_DIR;
5401
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005402 l = e - s;
5403 if (l > MAXPATHL - 5)
5404 break;
5405 vim_strncpy(buf, s, l);
5406 add_pathsep(buf);
5407 l = STRLEN(buf);
5408 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5409
5410 /* Expand matches in one directory of $PATH. */
5411 ret = expand_wildcards(1, &buf, num_file, file, flags);
5412 if (ret == OK)
5413 {
5414 if (ga_grow(&ga, *num_file) == FAIL)
5415 FreeWild(*num_file, *file);
5416 else
5417 {
5418 for (i = 0; i < *num_file; ++i)
5419 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005420 char_u *name = (*file)[i];
5421
5422 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005423 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005424 // Check if this name was already found.
5425 hash = hash_hash(name + l);
5426 hi = hash_lookup(&found_ht, name + l, hash);
5427 if (HASHITEM_EMPTY(hi))
5428 {
5429 // Remove the path that was prepended.
5430 STRMOVE(name, name + l);
5431 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5432 hash_add_item(&found_ht, hi, name, hash);
5433 name = NULL;
5434 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005435 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005436 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005437 }
5438 vim_free(*file);
5439 }
5440 }
5441 if (*e != NUL)
5442 ++e;
5443 }
5444 *file = ga.ga_data;
5445 *num_file = ga.ga_len;
5446
5447 vim_free(buf);
5448 vim_free(pat);
5449 if (mustfree)
5450 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005451 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005452 return OK;
5453}
5454
5455
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5457/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005458 * Call "user_expand_func()" to invoke a user defined Vim script function and
5459 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005461 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005462call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005463 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005464 expand_T *xp,
5465 int *num_file,
5466 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005468 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005469 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005471 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005472 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005473 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
Bram Moolenaarb7515462013-06-29 12:58:33 +02005475 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005476 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 *num_file = 0;
5478 *file = NULL;
5479
Bram Moolenaarb7515462013-06-29 12:58:33 +02005480 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005481 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005482 keep = ccline.cmdbuff[ccline.cmdlen];
5483 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005484 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005485
Bram Moolenaarffa96842018-06-12 22:05:14 +02005486 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5487
5488 args[0].v_type = VAR_STRING;
5489 args[0].vval.v_string = pat;
5490 args[1].v_type = VAR_STRING;
5491 args[1].vval.v_string = xp->xp_line;
5492 args[2].v_type = VAR_NUMBER;
5493 args[2].vval.v_number = xp->xp_col;
5494 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005496 /* Save the cmdline, we don't know what the function may do. */
5497 save_ccline = ccline;
5498 ccline.cmdbuff = NULL;
5499 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005501
Bram Moolenaarded27a12018-08-01 19:06:03 +02005502 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005503
5504 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005506 if (ccline.cmdbuff != NULL)
5507 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005508
Bram Moolenaarffa96842018-06-12 22:05:14 +02005509 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005510 return ret;
5511}
5512
5513/*
5514 * Expand names with a function defined by the user.
5515 */
5516 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005517ExpandUserDefined(
5518 expand_T *xp,
5519 regmatch_T *regmatch,
5520 int *num_file,
5521 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005522{
5523 char_u *retstr;
5524 char_u *s;
5525 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005526 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005527 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005528 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005529
5530 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5531 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 return FAIL;
5533
5534 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005535 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 e = vim_strchr(s, '\n');
5538 if (e == NULL)
5539 e = s + STRLEN(s);
5540 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005541 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005543 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5544 *e = keep;
5545
5546 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005548 if (ga_grow(&ga, 1) == FAIL)
5549 break;
5550 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5551 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 }
5553
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (*e != NUL)
5555 ++e;
5556 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005557 vim_free(retstr);
5558 *file = ga.ga_data;
5559 *num_file = ga.ga_len;
5560 return OK;
5561}
5562
5563/*
5564 * Expand names with a list returned by a function defined by the user.
5565 */
5566 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005567ExpandUserList(
5568 expand_T *xp,
5569 int *num_file,
5570 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005571{
5572 list_T *retlist;
5573 listitem_T *li;
5574 garray_T ga;
5575
5576 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5577 if (retlist == NULL)
5578 return FAIL;
5579
5580 ga_init2(&ga, (int)sizeof(char *), 3);
5581 /* Loop over the items in the list. */
5582 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5583 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005584 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5585 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005586
5587 if (ga_grow(&ga, 1) == FAIL)
5588 break;
5589
5590 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005591 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005592 ++ga.ga_len;
5593 }
5594 list_unref(retlist);
5595
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 *file = ga.ga_data;
5597 *num_file = ga.ga_len;
5598 return OK;
5599}
5600#endif
5601
5602/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005603 * Expand color scheme, compiler or filetype names.
5604 * Search from 'runtimepath':
5605 * 'runtimepath'/{dirnames}/{pat}.vim
5606 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5607 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5608 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5609 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005610 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 */
5612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ExpandRTDir(
5614 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005615 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005616 int *num_file,
5617 char_u ***file,
5618 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 char_u *s;
5621 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005622 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005624 int i;
5625 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626
5627 *num_file = 0;
5628 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005629 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005630 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005632 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005634 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5635 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005637 ga_clear_strings(&ga);
5638 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005640 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005641 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005642 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005644
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005645 if (flags & DIP_START) {
5646 for (i = 0; dirnames[i] != NULL; ++i)
5647 {
5648 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5649 if (s == NULL)
5650 {
5651 ga_clear_strings(&ga);
5652 return FAIL;
5653 }
5654 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5655 globpath(p_pp, s, &ga, 0);
5656 vim_free(s);
5657 }
5658 }
5659
5660 if (flags & DIP_OPT) {
5661 for (i = 0; dirnames[i] != NULL; ++i)
5662 {
5663 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5664 if (s == NULL)
5665 {
5666 ga_clear_strings(&ga);
5667 return FAIL;
5668 }
5669 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5670 globpath(p_pp, s, &ga, 0);
5671 vim_free(s);
5672 }
5673 }
5674
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005675 for (i = 0; i < ga.ga_len; ++i)
5676 {
5677 match = ((char_u **)ga.ga_data)[i];
5678 s = match;
5679 e = s + STRLEN(s);
5680 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5681 {
5682 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005683 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005684 if (s < match || vim_ispathsep(*s))
5685 break;
5686 ++s;
5687 *e = NUL;
5688 mch_memmove(match, s, e - s + 1);
5689 }
5690 }
5691
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005692 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005693 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005694
5695 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005696 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005697 remove_duplicates(&ga);
5698
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 *file = ga.ga_data;
5700 *num_file = ga.ga_len;
5701 return OK;
5702}
5703
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005704/*
5705 * Expand loadplugin names:
5706 * 'packpath'/pack/ * /opt/{pat}
5707 */
5708 static int
5709ExpandPackAddDir(
5710 char_u *pat,
5711 int *num_file,
5712 char_u ***file)
5713{
5714 char_u *s;
5715 char_u *e;
5716 char_u *match;
5717 garray_T ga;
5718 int i;
5719 int pat_len;
5720
5721 *num_file = 0;
5722 *file = NULL;
5723 pat_len = (int)STRLEN(pat);
5724 ga_init2(&ga, (int)sizeof(char *), 10);
5725
5726 s = alloc((unsigned)(pat_len + 26));
5727 if (s == NULL)
5728 {
5729 ga_clear_strings(&ga);
5730 return FAIL;
5731 }
5732 sprintf((char *)s, "pack/*/opt/%s*", pat);
5733 globpath(p_pp, s, &ga, 0);
5734 vim_free(s);
5735
5736 for (i = 0; i < ga.ga_len; ++i)
5737 {
5738 match = ((char_u **)ga.ga_data)[i];
5739 s = gettail(match);
5740 e = s + STRLEN(s);
5741 mch_memmove(match, s, e - s + 1);
5742 }
5743
5744 if (ga.ga_len == 0)
5745 return FAIL;
5746
5747 /* Sort and remove duplicates which can happen when specifying multiple
5748 * directories in dirnames. */
5749 remove_duplicates(&ga);
5750
5751 *file = ga.ga_data;
5752 *num_file = ga.ga_len;
5753 return OK;
5754}
5755
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756#endif
5757
5758#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5759/*
5760 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005761 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005763 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005764globpath(
5765 char_u *path,
5766 char_u *file,
5767 garray_T *ga,
5768 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 expand_T xpc;
5771 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 int num_p;
5774 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776 buf = alloc(MAXPATHL);
5777 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005780 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /* Loop over all entries in {path}. */
5784 while (*path != NUL)
5785 {
5786 /* Copy one item of the path to buf[] and concatenate the file name. */
5787 copy_option_part(&path, buf, MAXPATHL, ",");
5788 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5789 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005790# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005791 /* Using the platform's path separator (\) makes vim incorrectly
5792 * treat it as an escape character, use '/' instead. */
5793 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5794 STRCAT(buf, "/");
5795# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005799 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5800 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005802 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005804 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 for (i = 0; i < num_p; ++i)
5807 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005808 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005809 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005810 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 FreeWild(num_p, p);
5815 }
5816 }
5817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820}
5821
5822#endif
5823
5824#if defined(FEAT_CMDHIST) || defined(PROTO)
5825
5826/*********************************
5827 * Command line history stuff *
5828 *********************************/
5829
5830/*
5831 * Translate a history character to the associated type number.
5832 */
5833 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005834hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835{
5836 if (c == ':')
5837 return HIST_CMD;
5838 if (c == '=')
5839 return HIST_EXPR;
5840 if (c == '@')
5841 return HIST_INPUT;
5842 if (c == '>')
5843 return HIST_DEBUG;
5844 return HIST_SEARCH; /* must be '?' or '/' */
5845}
5846
5847/*
5848 * Table of history names.
5849 * These names are used in :history and various hist...() functions.
5850 * It is sufficient to give the significant prefix of a history name.
5851 */
5852
5853static char *(history_names[]) =
5854{
5855 "cmd",
5856 "search",
5857 "expr",
5858 "input",
5859 "debug",
5860 NULL
5861};
5862
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005863#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5864/*
5865 * Function given to ExpandGeneric() to obtain the possible first
5866 * arguments of the ":history command.
5867 */
5868 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005869get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005870{
5871 static char_u compl[2] = { NUL, NUL };
5872 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005873 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005874 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5875
5876 if (idx < short_names_count)
5877 {
5878 compl[0] = (char_u)short_names[idx];
5879 return compl;
5880 }
5881 if (idx < short_names_count + history_name_count)
5882 return (char_u *)history_names[idx - short_names_count];
5883 if (idx == short_names_count + history_name_count)
5884 return (char_u *)"all";
5885 return NULL;
5886}
5887#endif
5888
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889/*
5890 * init_history() - Initialize the command line history.
5891 * Also used to re-allocate the history when the size changes.
5892 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005893 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005894init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 int newlen; /* new length of history table */
5897 histentry_T *temp;
5898 int i;
5899 int j;
5900 int type;
5901
5902 /*
5903 * If size of history table changed, reallocate it
5904 */
5905 newlen = (int)p_hi;
5906 if (newlen != hislen) /* history length changed */
5907 {
5908 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5909 {
5910 if (newlen)
5911 {
5912 temp = (histentry_T *)lalloc(
5913 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5914 if (temp == NULL) /* out of memory! */
5915 {
5916 if (type == 0) /* first one: just keep the old length */
5917 {
5918 newlen = hislen;
5919 break;
5920 }
5921 /* Already changed one table, now we can only have zero
5922 * length for all tables. */
5923 newlen = 0;
5924 type = -1;
5925 continue;
5926 }
5927 }
5928 else
5929 temp = NULL;
5930 if (newlen == 0 || temp != NULL)
5931 {
5932 if (hisidx[type] < 0) /* there are no entries yet */
5933 {
5934 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005935 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
5937 else if (newlen > hislen) /* array becomes bigger */
5938 {
5939 for (i = 0; i <= hisidx[type]; ++i)
5940 temp[i] = history[type][i];
5941 j = i;
5942 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005943 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 for ( ; j < hislen; ++i, ++j)
5945 temp[i] = history[type][j];
5946 }
5947 else /* array becomes smaller or 0 */
5948 {
5949 j = hisidx[type];
5950 for (i = newlen - 1; ; --i)
5951 {
5952 if (i >= 0) /* copy newest entries */
5953 temp[i] = history[type][j];
5954 else /* remove older entries */
5955 vim_free(history[type][j].hisstr);
5956 if (--j < 0)
5957 j = hislen - 1;
5958 if (j == hisidx[type])
5959 break;
5960 }
5961 hisidx[type] = newlen - 1;
5962 }
5963 vim_free(history[type]);
5964 history[type] = temp;
5965 }
5966 }
5967 hislen = newlen;
5968 }
5969}
5970
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005971 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005972clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005973{
5974 hisptr->hisnum = 0;
5975 hisptr->viminfo = FALSE;
5976 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005977 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005978}
5979
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980/*
5981 * Check if command line 'str' is already in history.
5982 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5983 */
5984 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005985in_history(
5986 int type,
5987 char_u *str,
5988 int move_to_front, /* Move the entry to the front if it exists */
5989 int sep,
5990 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 int i;
5993 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005994 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 if (hisidx[type] < 0)
5997 return FALSE;
5998 i = hisidx[type];
5999 do
6000 {
6001 if (history[type][i].hisstr == NULL)
6002 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006003
6004 /* For search history, check that the separator character matches as
6005 * well. */
6006 p = history[type][i].hisstr;
6007 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006008 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006009 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 {
6011 if (!move_to_front)
6012 return TRUE;
6013 last_i = i;
6014 break;
6015 }
6016 if (--i < 0)
6017 i = hislen - 1;
6018 } while (i != hisidx[type]);
6019
6020 if (last_i >= 0)
6021 {
6022 str = history[type][i].hisstr;
6023 while (i != hisidx[type])
6024 {
6025 if (++i >= hislen)
6026 i = 0;
6027 history[type][last_i] = history[type][i];
6028 last_i = i;
6029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006031 history[type][i].viminfo = FALSE;
6032 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006033 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034 return TRUE;
6035 }
6036 return FALSE;
6037}
6038
6039/*
6040 * Convert history name (from table above) to its HIST_ equivalent.
6041 * When "name" is empty, return "cmd" history.
6042 * Returns -1 for unknown history name.
6043 */
6044 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006045get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
6047 int i;
6048 int len = (int)STRLEN(name);
6049
6050 /* No argument: use current history. */
6051 if (len == 0)
6052 return hist_char2type(ccline.cmdfirstc);
6053
6054 for (i = 0; history_names[i] != NULL; ++i)
6055 if (STRNICMP(name, history_names[i], len) == 0)
6056 return i;
6057
6058 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6059 return hist_char2type(name[0]);
6060
6061 return -1;
6062}
6063
6064static int last_maptick = -1; /* last seen maptick */
6065
6066/*
6067 * Add the given string to the given history. If the string is already in the
6068 * history then it is moved to the front. "histype" may be one of he HIST_
6069 * values.
6070 */
6071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006072add_to_history(
6073 int histype,
6074 char_u *new_entry,
6075 int in_map, /* consider maptick when inside a mapping */
6076 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 histentry_T *hisptr;
6079 int len;
6080
6081 if (hislen == 0) /* no history */
6082 return;
6083
Bram Moolenaara939e432013-11-09 05:30:26 +01006084 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6085 return;
6086
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087 /*
6088 * Searches inside the same mapping overwrite each other, so that only
6089 * the last line is kept. Be careful not to remove a line that was moved
6090 * down, only lines that were added.
6091 */
6092 if (histype == HIST_SEARCH && in_map)
6093 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006094 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
6096 /* Current line is from the same mapping, remove it */
6097 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6098 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006099 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 --hisnum[histype];
6101 if (--hisidx[HIST_SEARCH] < 0)
6102 hisidx[HIST_SEARCH] = hislen - 1;
6103 }
6104 last_maptick = -1;
6105 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006106 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 if (++hisidx[histype] == hislen)
6109 hisidx[histype] = 0;
6110 hisptr = &history[histype][hisidx[histype]];
6111 vim_free(hisptr->hisstr);
6112
6113 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006114 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6116 if (hisptr->hisstr != NULL)
6117 hisptr->hisstr[len + 1] = sep;
6118
6119 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006120 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006121 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 if (histype == HIST_SEARCH && in_map)
6123 last_maptick = maptick;
6124 }
6125}
6126
6127#if defined(FEAT_EVAL) || defined(PROTO)
6128
6129/*
6130 * Get identifier of newest history entry.
6131 * "histype" may be one of the HIST_ values.
6132 */
6133 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006134get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135{
6136 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6137 || hisidx[histype] < 0)
6138 return -1;
6139
6140 return history[histype][hisidx[histype]].hisnum;
6141}
6142
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 * Calculate history index from a number:
6145 * num > 0: seen as identifying number of a history entry
6146 * num < 0: relative position in history wrt newest entry
6147 * "histype" may be one of the HIST_ values.
6148 */
6149 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006150calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151{
6152 int i;
6153 histentry_T *hist;
6154 int wrapped = FALSE;
6155
6156 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6157 || (i = hisidx[histype]) < 0 || num == 0)
6158 return -1;
6159
6160 hist = history[histype];
6161 if (num > 0)
6162 {
6163 while (hist[i].hisnum > num)
6164 if (--i < 0)
6165 {
6166 if (wrapped)
6167 break;
6168 i += hislen;
6169 wrapped = TRUE;
6170 }
6171 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6172 return i;
6173 }
6174 else if (-num <= hislen)
6175 {
6176 i += num + 1;
6177 if (i < 0)
6178 i += hislen;
6179 if (hist[i].hisstr != NULL)
6180 return i;
6181 }
6182 return -1;
6183}
6184
6185/*
6186 * Get a history entry by its index.
6187 * "histype" may be one of the HIST_ values.
6188 */
6189 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006190get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191{
6192 idx = calc_hist_idx(histype, idx);
6193 if (idx >= 0)
6194 return history[histype][idx].hisstr;
6195 else
6196 return (char_u *)"";
6197}
6198
6199/*
6200 * Clear all entries of a history.
6201 * "histype" may be one of the HIST_ values.
6202 */
6203 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006204clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006205{
6206 int i;
6207 histentry_T *hisptr;
6208
6209 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6210 {
6211 hisptr = history[histype];
6212 for (i = hislen; i--;)
6213 {
6214 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006215 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006216 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218 hisidx[histype] = -1; /* mark history as cleared */
6219 hisnum[histype] = 0; /* reset identifier counter */
6220 return OK;
6221 }
6222 return FAIL;
6223}
6224
6225/*
6226 * Remove all entries matching {str} from a history.
6227 * "histype" may be one of the HIST_ values.
6228 */
6229 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006230del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 regmatch_T regmatch;
6233 histentry_T *hisptr;
6234 int idx;
6235 int i;
6236 int last;
6237 int found = FALSE;
6238
6239 regmatch.regprog = NULL;
6240 regmatch.rm_ic = FALSE; /* always match case */
6241 if (hislen != 0
6242 && histype >= 0
6243 && histype < HIST_COUNT
6244 && *str != NUL
6245 && (idx = hisidx[histype]) >= 0
6246 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6247 != NULL)
6248 {
6249 i = last = idx;
6250 do
6251 {
6252 hisptr = &history[histype][i];
6253 if (hisptr->hisstr == NULL)
6254 break;
6255 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6256 {
6257 found = TRUE;
6258 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006259 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260 }
6261 else
6262 {
6263 if (i != last)
6264 {
6265 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006266 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 }
6268 if (--last < 0)
6269 last += hislen;
6270 }
6271 if (--i < 0)
6272 i += hislen;
6273 } while (i != idx);
6274 if (history[histype][idx].hisstr == NULL)
6275 hisidx[histype] = -1;
6276 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006277 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 return found;
6279}
6280
6281/*
6282 * Remove an indexed entry from a history.
6283 * "histype" may be one of the HIST_ values.
6284 */
6285 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006286del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
6288 int i, j;
6289
6290 i = calc_hist_idx(histype, idx);
6291 if (i < 0)
6292 return FALSE;
6293 idx = hisidx[histype];
6294 vim_free(history[histype][i].hisstr);
6295
6296 /* When deleting the last added search string in a mapping, reset
6297 * last_maptick, so that the last added search string isn't deleted again.
6298 */
6299 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6300 last_maptick = -1;
6301
6302 while (i != idx)
6303 {
6304 j = (i + 1) % hislen;
6305 history[histype][i] = history[histype][j];
6306 i = j;
6307 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006308 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 if (--i < 0)
6310 i += hislen;
6311 hisidx[histype] = i;
6312 return TRUE;
6313}
6314
6315#endif /* FEAT_EVAL */
6316
6317#if defined(FEAT_CRYPT) || defined(PROTO)
6318/*
6319 * Very specific function to remove the value in ":set key=val" from the
6320 * history.
6321 */
6322 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006323remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 char_u *p;
6326 int i;
6327
6328 i = hisidx[HIST_CMD];
6329 if (i < 0)
6330 return;
6331 p = history[HIST_CMD][i].hisstr;
6332 if (p != NULL)
6333 for ( ; *p; ++p)
6334 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6335 {
6336 p = vim_strchr(p + 3, '=');
6337 if (p == NULL)
6338 break;
6339 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006340 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 if (p[i] == '\\' && p[i + 1])
6342 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006343 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344 --p;
6345 }
6346}
6347#endif
6348
6349#endif /* FEAT_CMDHIST */
6350
Bram Moolenaar064154c2016-03-19 22:50:43 +01006351#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006352/*
6353 * Get pointer to the command line info to use. cmdline_paste() may clear
6354 * ccline and put the previous value in prev_ccline.
6355 */
6356 static struct cmdline_info *
6357get_ccline_ptr(void)
6358{
6359 if ((State & CMDLINE) == 0)
6360 return NULL;
6361 if (ccline.cmdbuff != NULL)
6362 return &ccline;
6363 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6364 return &prev_ccline;
6365 return NULL;
6366}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006367#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006368
Bram Moolenaar064154c2016-03-19 22:50:43 +01006369#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006370/*
6371 * Get the current command line in allocated memory.
6372 * Only works when the command line is being edited.
6373 * Returns NULL when something is wrong.
6374 */
6375 char_u *
6376get_cmdline_str(void)
6377{
6378 struct cmdline_info *p = get_ccline_ptr();
6379
6380 if (p == NULL)
6381 return NULL;
6382 return vim_strnsave(p->cmdbuff, p->cmdlen);
6383}
6384
6385/*
6386 * Get the current command line position, counted in bytes.
6387 * Zero is the first position.
6388 * Only works when the command line is being edited.
6389 * Returns -1 when something is wrong.
6390 */
6391 int
6392get_cmdline_pos(void)
6393{
6394 struct cmdline_info *p = get_ccline_ptr();
6395
6396 if (p == NULL)
6397 return -1;
6398 return p->cmdpos;
6399}
6400
6401/*
6402 * Set the command line byte position to "pos". Zero is the first position.
6403 * Only works when the command line is being edited.
6404 * Returns 1 when failed, 0 when OK.
6405 */
6406 int
6407set_cmdline_pos(
6408 int pos)
6409{
6410 struct cmdline_info *p = get_ccline_ptr();
6411
6412 if (p == NULL)
6413 return 1;
6414
6415 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6416 * changed the command line. */
6417 if (pos < 0)
6418 new_cmdpos = 0;
6419 else
6420 new_cmdpos = pos;
6421 return 0;
6422}
6423#endif
6424
6425#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6426/*
6427 * Get the current command-line type.
6428 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6429 * Only works when the command line is being edited.
6430 * Returns NUL when something is wrong.
6431 */
6432 int
6433get_cmdline_type(void)
6434{
6435 struct cmdline_info *p = get_ccline_ptr();
6436
6437 if (p == NULL)
6438 return NUL;
6439 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006440 return
6441# ifdef FEAT_EVAL
6442 (p->input_fn) ? '@' :
6443# endif
6444 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006445 return p->cmdfirstc;
6446}
6447#endif
6448
Bram Moolenaar071d4272004-06-13 20:20:40 +00006449#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6450/*
6451 * Get indices "num1,num2" that specify a range within a list (not a range of
6452 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6453 * Returns OK if parsed successfully, otherwise FAIL.
6454 */
6455 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006456get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457{
6458 int len;
6459 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006460 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 *str = skipwhite(*str);
6463 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6464 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006465 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006466 *str += len;
6467 *num1 = (int)num;
6468 first = TRUE;
6469 }
6470 *str = skipwhite(*str);
6471 if (**str == ',') /* parse "to" part of range */
6472 {
6473 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006474 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006475 if (len > 0)
6476 {
6477 *num2 = (int)num;
6478 *str = skipwhite(*str + len);
6479 }
6480 else if (!first) /* no number given at all */
6481 return FAIL;
6482 }
6483 else if (first) /* only one number given */
6484 *num2 = *num1;
6485 return OK;
6486}
6487#endif
6488
6489#if defined(FEAT_CMDHIST) || defined(PROTO)
6490/*
6491 * :history command - print a history
6492 */
6493 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006494ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495{
6496 histentry_T *hist;
6497 int histype1 = HIST_CMD;
6498 int histype2 = HIST_CMD;
6499 int hisidx1 = 1;
6500 int hisidx2 = -1;
6501 int idx;
6502 int i, j, k;
6503 char_u *end;
6504 char_u *arg = eap->arg;
6505
6506 if (hislen == 0)
6507 {
6508 MSG(_("'history' option is zero"));
6509 return;
6510 }
6511
6512 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6513 {
6514 end = arg;
6515 while (ASCII_ISALPHA(*end)
6516 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6517 end++;
6518 i = *end;
6519 *end = NUL;
6520 histype1 = get_histtype(arg);
6521 if (histype1 == -1)
6522 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006523 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 {
6525 histype1 = 0;
6526 histype2 = HIST_COUNT-1;
6527 }
6528 else
6529 {
6530 *end = i;
6531 EMSG(_(e_trailing));
6532 return;
6533 }
6534 }
6535 else
6536 histype2 = histype1;
6537 *end = i;
6538 }
6539 else
6540 end = arg;
6541 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6542 {
6543 EMSG(_(e_trailing));
6544 return;
6545 }
6546
6547 for (; !got_int && histype1 <= histype2; ++histype1)
6548 {
6549 STRCPY(IObuff, "\n # ");
6550 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6551 MSG_PUTS_TITLE(IObuff);
6552 idx = hisidx[histype1];
6553 hist = history[histype1];
6554 j = hisidx1;
6555 k = hisidx2;
6556 if (j < 0)
6557 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6558 if (k < 0)
6559 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6560 if (idx >= 0 && j <= k)
6561 for (i = idx + 1; !got_int; ++i)
6562 {
6563 if (i == hislen)
6564 i = 0;
6565 if (hist[i].hisstr != NULL
6566 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6567 {
6568 msg_putchar('\n');
6569 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6570 hist[i].hisnum);
6571 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6572 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006573 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 else
6575 STRCAT(IObuff, hist[i].hisstr);
6576 msg_outtrans(IObuff);
6577 out_flush();
6578 }
6579 if (i == idx)
6580 break;
6581 }
6582 }
6583}
6584#endif
6585
6586#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006587/*
6588 * Buffers for history read from a viminfo file. Only valid while reading.
6589 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006590static histentry_T *viminfo_history[HIST_COUNT] =
6591 {NULL, NULL, NULL, NULL, NULL};
6592static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6593static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594static int viminfo_add_at_front = FALSE;
6595
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006596static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597
6598/*
6599 * Translate a history type number to the associated character.
6600 */
6601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006602hist_type2char(
6603 int type,
6604 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605{
6606 if (type == HIST_CMD)
6607 return ':';
6608 if (type == HIST_SEARCH)
6609 {
6610 if (use_question)
6611 return '?';
6612 else
6613 return '/';
6614 }
6615 if (type == HIST_EXPR)
6616 return '=';
6617 return '@';
6618}
6619
6620/*
6621 * Prepare for reading the history from the viminfo file.
6622 * This allocates history arrays to store the read history lines.
6623 */
6624 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006625prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626{
6627 int i;
6628 int num;
6629 int type;
6630 int len;
6631
6632 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006633 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (asklen > hislen)
6635 asklen = hislen;
6636
6637 for (type = 0; type < HIST_COUNT; ++type)
6638 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006639 /* Count the number of empty spaces in the history list. Entries read
6640 * from viminfo previously are also considered empty. If there are
6641 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006643 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006644 num++;
6645 len = asklen;
6646 if (num > len)
6647 len = num;
6648 if (len <= 0)
6649 viminfo_history[type] = NULL;
6650 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006651 viminfo_history[type] = (histentry_T *)lalloc(
6652 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (viminfo_history[type] == NULL)
6654 len = 0;
6655 viminfo_hislen[type] = len;
6656 viminfo_hisidx[type] = 0;
6657 }
6658}
6659
6660/*
6661 * Accept a line from the viminfo, store it in the history array when it's
6662 * new.
6663 */
6664 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006665read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666{
6667 int type;
6668 long_u len;
6669 char_u *val;
6670 char_u *p;
6671
6672 type = hist_char2type(virp->vir_line[0]);
6673 if (viminfo_hisidx[type] < viminfo_hislen[type])
6674 {
6675 val = viminfo_readstring(virp, 1, TRUE);
6676 if (val != NULL && *val != NUL)
6677 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006678 int sep = (*val == ' ' ? NUL : *val);
6679
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006681 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 {
6683 /* Need to re-allocate to append the separator byte. */
6684 len = STRLEN(val);
6685 p = lalloc(len + 2, TRUE);
6686 if (p != NULL)
6687 {
6688 if (type == HIST_SEARCH)
6689 {
6690 /* Search entry: Move the separator from the first
6691 * column to after the NUL. */
6692 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006693 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 }
6695 else
6696 {
6697 /* Not a search entry: No separator in the viminfo
6698 * file, add a NUL separator. */
6699 mch_memmove(p, val, (size_t)len + 1);
6700 p[len + 1] = NUL;
6701 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006702 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6703 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006704 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6705 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006706 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708 }
6709 }
6710 vim_free(val);
6711 }
6712 return viminfo_readline(virp);
6713}
6714
Bram Moolenaar07219f92013-04-14 23:19:36 +02006715/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006716 * Accept a new style history line from the viminfo, store it in the history
6717 * array when it's new.
6718 */
6719 void
6720handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006721 garray_T *values,
6722 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006723{
6724 int type;
6725 long_u len;
6726 char_u *val;
6727 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006728 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006729
6730 /* Check the format:
6731 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006732 if (values->ga_len < 4
6733 || vp[0].bv_type != BVAL_NR
6734 || vp[1].bv_type != BVAL_NR
6735 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6736 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006737 return;
6738
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006739 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006740 if (type >= HIST_COUNT)
6741 return;
6742 if (viminfo_hisidx[type] < viminfo_hislen[type])
6743 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006744 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006745 if (val != NULL && *val != NUL)
6746 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006747 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6748 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006749 int idx;
6750 int overwrite = FALSE;
6751
6752 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6753 {
6754 /* If lines were written by an older Vim we need to avoid
6755 * getting duplicates. See if the entry already exists. */
6756 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6757 {
6758 p = viminfo_history[type][idx].hisstr;
6759 if (STRCMP(val, p) == 0
6760 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6761 {
6762 overwrite = TRUE;
6763 break;
6764 }
6765 }
6766
6767 if (!overwrite)
6768 {
6769 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006770 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006771 p = lalloc(len + 2, TRUE);
6772 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006773 else
6774 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006775 if (p != NULL)
6776 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006777 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006778 if (!overwrite)
6779 {
6780 mch_memmove(p, val, (size_t)len + 1);
6781 /* Put the separator after the NUL. */
6782 p[len + 1] = sep;
6783 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006784 viminfo_history[type][idx].hisnum = 0;
6785 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006786 viminfo_hisidx[type]++;
6787 }
6788 }
6789 }
6790 }
6791 }
6792}
6793
6794/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006795 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006796 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006797 static void
6798concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
6800 int idx;
6801 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006802
6803 idx = hisidx[type] + viminfo_hisidx[type];
6804 if (idx >= hislen)
6805 idx -= hislen;
6806 else if (idx < 0)
6807 idx = hislen - 1;
6808 if (viminfo_add_at_front)
6809 hisidx[type] = idx;
6810 else
6811 {
6812 if (hisidx[type] == -1)
6813 hisidx[type] = hislen - 1;
6814 do
6815 {
6816 if (history[type][idx].hisstr != NULL
6817 || history[type][idx].viminfo)
6818 break;
6819 if (++idx == hislen)
6820 idx = 0;
6821 } while (idx != hisidx[type]);
6822 if (idx != hisidx[type] && --idx < 0)
6823 idx = hislen - 1;
6824 }
6825 for (i = 0; i < viminfo_hisidx[type]; i++)
6826 {
6827 vim_free(history[type][idx].hisstr);
6828 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6829 history[type][idx].viminfo = TRUE;
6830 history[type][idx].time_set = viminfo_history[type][i].time_set;
6831 if (--idx < 0)
6832 idx = hislen - 1;
6833 }
6834 idx += 1;
6835 idx %= hislen;
6836 for (i = 0; i < viminfo_hisidx[type]; i++)
6837 {
6838 history[type][idx++].hisnum = ++hisnum[type];
6839 idx %= hislen;
6840 }
6841}
6842
6843#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6844 static int
6845#ifdef __BORLANDC__
6846_RTLENTRYF
6847#endif
6848sort_hist(const void *s1, const void *s2)
6849{
6850 histentry_T *p1 = *(histentry_T **)s1;
6851 histentry_T *p2 = *(histentry_T **)s2;
6852
6853 if (p1->time_set < p2->time_set) return -1;
6854 if (p1->time_set > p2->time_set) return 1;
6855 return 0;
6856}
6857#endif
6858
6859/*
6860 * Merge history lines from viminfo and lines typed in this Vim based on the
6861 * timestamp;
6862 */
6863 static void
6864merge_history(int type)
6865{
6866 int max_len;
6867 histentry_T **tot_hist;
6868 histentry_T *new_hist;
6869 int i;
6870 int len;
6871
6872 /* Make one long list with all entries. */
6873 max_len = hislen + viminfo_hisidx[type];
6874 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6875 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6876 if (tot_hist == NULL || new_hist == NULL)
6877 {
6878 vim_free(tot_hist);
6879 vim_free(new_hist);
6880 return;
6881 }
6882 for (i = 0; i < viminfo_hisidx[type]; i++)
6883 tot_hist[i] = &viminfo_history[type][i];
6884 len = i;
6885 for (i = 0; i < hislen; i++)
6886 if (history[type][i].hisstr != NULL)
6887 tot_hist[len++] = &history[type][i];
6888
6889 /* Sort the list on timestamp. */
6890 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6891
6892 /* Keep the newest ones. */
6893 for (i = 0; i < hislen; i++)
6894 {
6895 if (i < len)
6896 {
6897 new_hist[i] = *tot_hist[i];
6898 tot_hist[i]->hisstr = NULL;
6899 if (new_hist[i].hisnum == 0)
6900 new_hist[i].hisnum = ++hisnum[type];
6901 }
6902 else
6903 clear_hist_entry(&new_hist[i]);
6904 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006905 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006906
6907 /* Free what is not kept. */
6908 for (i = 0; i < viminfo_hisidx[type]; i++)
6909 vim_free(viminfo_history[type][i].hisstr);
6910 for (i = 0; i < hislen; i++)
6911 vim_free(history[type][i].hisstr);
6912 vim_free(history[type]);
6913 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006914 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006915}
6916
6917/*
6918 * Finish reading history lines from viminfo. Not used when writing viminfo.
6919 */
6920 void
6921finish_viminfo_history(vir_T *virp)
6922{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006924 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925
6926 for (type = 0; type < HIST_COUNT; ++type)
6927 {
6928 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006929 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006930
6931 if (merge)
6932 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006934 concat_history(type);
6935
Bram Moolenaard23a8232018-02-10 18:45:26 +01006936 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006937 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
6939}
6940
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006941/*
6942 * Write history to viminfo file in "fp".
6943 * When "merge" is TRUE merge history lines with a previously read viminfo
6944 * file, data is in viminfo_history[].
6945 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6946 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006948write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949{
6950 int i;
6951 int type;
6952 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006953 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 init_history();
6956 if (hislen == 0)
6957 return;
6958 for (type = 0; type < HIST_COUNT; ++type)
6959 {
6960 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6961 if (num_saved == 0)
6962 continue;
6963 if (num_saved < 0) /* Use default */
6964 num_saved = hislen;
6965 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6966 type == HIST_CMD ? _("Command Line") :
6967 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006968 type == HIST_EXPR ? _("Expression") :
6969 type == HIST_INPUT ? _("Input Line") :
6970 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 if (num_saved > hislen)
6972 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006973
6974 /*
6975 * Merge typed and viminfo history:
6976 * round 1: history of typed commands.
6977 * round 2: history from recently read viminfo.
6978 */
6979 for (round = 1; round <= 2; ++round)
6980 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006981 if (round == 1)
6982 /* start at newest entry, somewhere in the list */
6983 i = hisidx[type];
6984 else if (viminfo_hisidx[type] > 0)
6985 /* start at newest entry, first in the list */
6986 i = 0;
6987 else
6988 /* empty list */
6989 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006990 if (i >= 0)
6991 while (num_saved > 0
6992 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006994 char_u *p;
6995 time_t timestamp;
6996 int c = NUL;
6997
6998 if (round == 1)
6999 {
7000 p = history[type][i].hisstr;
7001 timestamp = history[type][i].time_set;
7002 }
7003 else
7004 {
7005 p = viminfo_history[type] == NULL ? NULL
7006 : viminfo_history[type][i].hisstr;
7007 timestamp = viminfo_history[type] == NULL ? 0
7008 : viminfo_history[type][i].time_set;
7009 }
7010
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007011 if (p != NULL && (round == 2
7012 || !merge
7013 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007015 --num_saved;
7016 fputc(hist_type2char(type, TRUE), fp);
7017 /* For the search history: put the separator in the
7018 * second column; use a space if there isn't one. */
7019 if (type == HIST_SEARCH)
7020 {
7021 c = p[STRLEN(p) + 1];
7022 putc(c == NUL ? ' ' : c, fp);
7023 }
7024 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007025
7026 {
7027 char cbuf[NUMBUFLEN];
7028
7029 /* New style history with a bar line. Format:
7030 * |{bartype},{histtype},{timestamp},{separator},"text" */
7031 if (c == NUL)
7032 cbuf[0] = NUL;
7033 else
7034 sprintf(cbuf, "%d", c);
7035 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7036 type, (long)timestamp, cbuf);
7037 barline_writestring(fp, p, LSIZE - 20);
7038 putc('\n', fp);
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007041 if (round == 1)
7042 {
7043 /* Decrement index, loop around and stop when back at
7044 * the start. */
7045 if (--i < 0)
7046 i = hislen - 1;
7047 if (i == hisidx[type])
7048 break;
7049 }
7050 else
7051 {
7052 /* Increment index. Stop at the end in the while. */
7053 ++i;
7054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007056 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007057 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007058 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007059 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007060 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007061 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063}
7064#endif /* FEAT_VIMINFO */
7065
7066#if defined(FEAT_FKMAP) || defined(PROTO)
7067/*
7068 * Write a character at the current cursor+offset position.
7069 * It is directly written into the command buffer block.
7070 */
7071 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007072cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7075 {
7076 EMSG(_("E198: cmd_pchar beyond the command length"));
7077 return;
7078 }
7079 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7080 ccline.cmdbuff[ccline.cmdlen] = NUL;
7081}
7082
7083 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007084cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085{
7086 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7087 {
7088 /* EMSG(_("cmd_gchar beyond the command length")); */
7089 return NUL;
7090 }
7091 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7092}
7093#endif
7094
7095#if defined(FEAT_CMDWIN) || defined(PROTO)
7096/*
7097 * Open a window on the current command line and history. Allow editing in
7098 * the window. Returns when the window is closed.
7099 * Returns:
7100 * CR if the command is to be executed
7101 * Ctrl_C if it is to be abandoned
7102 * K_IGNORE if editing continues
7103 */
7104 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007105open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106{
7107 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007108 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007110 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 win_T *wp;
7112 int i;
7113 linenr_T lnum;
7114 int histtype;
7115 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116 int save_restart_edit = restart_edit;
7117 int save_State = State;
7118 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007119#ifdef FEAT_RIGHTLEFT
7120 int save_cmdmsg_rl = cmdmsg_rl;
7121#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007122#ifdef FEAT_FOLDING
7123 int save_KeyTyped;
7124#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125
7126 /* Can't do this recursively. Can't do it when typing a password. */
7127 if (cmdwin_type != 0
7128# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7129 || cmdline_star > 0
7130# endif
7131 )
7132 {
7133 beep_flush();
7134 return K_IGNORE;
7135 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007136 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 /* Save current window sizes. */
7139 win_size_save(&winsizes);
7140
Bram Moolenaar071d4272004-06-13 20:20:40 +00007141 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007142 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007143
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007144 /* don't use a new tab page */
7145 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007146 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007147
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 /* Create a window for the command-line buffer. */
7149 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7150 {
7151 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007152 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 return K_IGNORE;
7154 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007155 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156
7157 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007158 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007159 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007162#ifdef FEAT_FOLDING
7163 curwin->w_p_fen = FALSE;
7164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007166 curwin->w_p_rl = cmdmsg_rl;
7167 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007169 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007172 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007173 /* But don't allow switching to another buffer. */
7174 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175
Bram Moolenaar46152342005-09-07 21:18:43 +00007176 /* Showing the prompt may have set need_wait_return, reset it. */
7177 need_wait_return = FALSE;
7178
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007179 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7181 {
7182 if (p_wc == TAB)
7183 {
7184 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7185 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7186 }
7187 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7188 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007189 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007191 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7192 * sets 'textwidth' to 78). */
7193 curbuf->b_p_tw = 0;
7194
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 /* Fill the buffer with the history. */
7196 init_history();
7197 if (hislen > 0)
7198 {
7199 i = hisidx[histtype];
7200 if (i >= 0)
7201 {
7202 lnum = 0;
7203 do
7204 {
7205 if (++i == hislen)
7206 i = 0;
7207 if (history[histtype][i].hisstr != NULL)
7208 ml_append(lnum++, history[histtype][i].hisstr,
7209 (colnr_T)0, FALSE);
7210 }
7211 while (i != hisidx[histtype]);
7212 }
7213 }
7214
7215 /* Replace the empty last line with the current command-line and put the
7216 * cursor there. */
7217 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7218 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7219 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007220 changed_line_abv_curs();
7221 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007222 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
7224 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007225 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227 /* No Ex mode here! */
7228 exmode_active = 0;
7229
7230 State = NORMAL;
7231# ifdef FEAT_MOUSE
7232 setmouse();
7233# endif
7234
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007236 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007237 if (restart_edit != 0) /* autocmd with ":startinsert" */
7238 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239
7240 i = RedrawingDisabled;
7241 RedrawingDisabled = 0;
7242
7243 /*
7244 * Call the main loop until <CR> or CTRL-C is typed.
7245 */
7246 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007247 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249 RedrawingDisabled = i;
7250
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007251# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007252 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007253# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007256 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007257
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007258# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007259 /* Restore KeyTyped in case it is modified by autocommands */
7260 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261# endif
7262
Bram Moolenaarccc18222007-05-10 18:25:20 +00007263 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007264 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 cmdwin_type = 0;
7266
7267 exmode_active = save_exmode;
7268
Bram Moolenaarf9821062008-06-20 16:31:07 +00007269 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007271 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 {
7273 cmdwin_result = Ctrl_C;
7274 EMSG(_("E199: Active window or buffer deleted"));
7275 }
7276 else
7277 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007278# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 /* autocmds may abort script processing */
7280 if (aborting() && cmdwin_result != K_IGNORE)
7281 cmdwin_result = Ctrl_C;
7282# endif
7283 /* Set the new command line from the cmdline buffer. */
7284 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007285 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007287 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7288
7289 if (histtype == HIST_CMD)
7290 {
7291 /* Execute the command directly. */
7292 ccline.cmdbuff = vim_strsave((char_u *)p);
7293 cmdwin_result = CAR;
7294 }
7295 else
7296 {
7297 /* First need to cancel what we were doing. */
7298 ccline.cmdbuff = NULL;
7299 stuffcharReadbuff(':');
7300 stuffReadbuff((char_u *)p);
7301 stuffcharReadbuff(CAR);
7302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303 }
7304 else if (cmdwin_result == K_XF2) /* :qa typed */
7305 {
7306 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7307 cmdwin_result = CAR;
7308 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007309 else if (cmdwin_result == Ctrl_C)
7310 {
7311 /* :q or :close, don't execute any command
7312 * and don't modify the cmd window. */
7313 ccline.cmdbuff = NULL;
7314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 else
7316 ccline.cmdbuff = vim_strsave(ml_get_curline());
7317 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007318 {
7319 ccline.cmdbuff = vim_strsave((char_u *)"");
7320 ccline.cmdlen = 0;
7321 ccline.cmdbufflen = 1;
7322 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007325 else
7326 {
7327 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7328 ccline.cmdbufflen = ccline.cmdlen + 1;
7329 ccline.cmdpos = curwin->w_cursor.col;
7330 if (ccline.cmdpos > ccline.cmdlen)
7331 ccline.cmdpos = ccline.cmdlen;
7332 if (cmdwin_result == K_IGNORE)
7333 {
7334 set_cmdspos_cursor();
7335 redrawcmd();
7336 }
7337 }
7338
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007340 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007341# ifdef FEAT_CONCEAL
7342 /* Avoid command-line window first character being concealed. */
7343 curwin->w_p_cole = 0;
7344# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007346 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 win_goto(old_curwin);
7348 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007349
7350 /* win_close() may have already wiped the buffer when 'bh' is
7351 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007352 if (bufref_valid(&bufref))
7353 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354
7355 /* Restore window sizes. */
7356 win_size_restore(&winsizes);
7357
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007358 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 }
7360
7361 ga_clear(&winsizes);
7362 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007363# ifdef FEAT_RIGHTLEFT
7364 cmdmsg_rl = save_cmdmsg_rl;
7365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366
7367 State = save_State;
7368# ifdef FEAT_MOUSE
7369 setmouse();
7370# endif
7371
7372 return cmdwin_result;
7373}
7374#endif /* FEAT_CMDWIN */
7375
7376/*
7377 * Used for commands that either take a simple command string argument, or:
7378 * cmd << endmarker
7379 * {script}
7380 * endmarker
7381 * Returns a pointer to allocated memory with {script} or NULL.
7382 */
7383 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007384script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385{
7386 char_u *theline;
7387 char *end_pattern = NULL;
7388 char dot[] = ".";
7389 garray_T ga;
7390
7391 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7392 return NULL;
7393
7394 ga_init2(&ga, 1, 0x400);
7395
7396 if (cmd[2] != NUL)
7397 end_pattern = (char *)skipwhite(cmd + 2);
7398 else
7399 end_pattern = dot;
7400
7401 for (;;)
7402 {
7403 theline = eap->getline(
7404#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007405 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406#endif
7407 NUL, eap->cookie, 0);
7408
7409 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007410 {
7411 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414
7415 ga_concat(&ga, theline);
7416 ga_append(&ga, '\n');
7417 vim_free(theline);
7418 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007419 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420
7421 return (char_u *)ga.ga_data;
7422}