blob: 3fb9c8623351d6f8a593ff19c4006a6830764bba [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
144
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200146 static void
147trigger_cmd_autocmd(int typechar, int evt)
148{
149 char_u typestr[2];
150
151 typestr[0] = typechar;
152 typestr[1] = NUL;
153 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
154}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200157 * Abandon the command line.
158 */
159 static void
160abandon_cmdline(void)
161{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100162 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200163 if (msg_scrolled == 0)
164 compute_cmdrow();
165 MSG("");
166 redraw_cmdline = TRUE;
167}
168
Bram Moolenaaree219b02017-12-17 14:55:01 +0100169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200170/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100171 * Guess that the pattern matches everything. Only finds specific cases, such
172 * as a trailing \|, which can happen while typing a pattern.
173 */
174 static int
175empty_pattern(char_u *p)
176{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100177 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100178
179 /* remove trailing \v and the like */
180 while (n >= 2 && p[n - 2] == '\\'
181 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
182 n -= 2;
183 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
184}
185
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200187typedef struct {
188 colnr_T vs_curswant;
189 colnr_T vs_leftcol;
190 linenr_T vs_topline;
191# ifdef FEAT_DIFF
192 int vs_topfill;
193# endif
194 linenr_T vs_botline;
195 linenr_T vs_empty_rows;
196} viewstate_T;
197
198 static void
199save_viewstate(viewstate_T *vs)
200{
201 vs->vs_curswant = curwin->w_curswant;
202 vs->vs_leftcol = curwin->w_leftcol;
203 vs->vs_topline = curwin->w_topline;
204# ifdef FEAT_DIFF
205 vs->vs_topfill = curwin->w_topfill;
206# endif
207 vs->vs_botline = curwin->w_botline;
208 vs->vs_empty_rows = curwin->w_empty_rows;
209}
210
211 static void
212restore_viewstate(viewstate_T *vs)
213{
214 curwin->w_curswant = vs->vs_curswant;
215 curwin->w_leftcol = vs->vs_leftcol;
216 curwin->w_topline = vs->vs_topline;
217# ifdef FEAT_DIFF
218 curwin->w_topfill = vs->vs_topfill;
219# endif
220 curwin->w_botline = vs->vs_botline;
221 curwin->w_empty_rows = vs->vs_empty_rows;
222}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223
224// Struct to store the state of 'incsearch' highlighting.
225typedef struct {
226 pos_T search_start; // where 'incsearch' starts searching
227 pos_T save_cursor;
228 viewstate_T init_viewstate;
229 viewstate_T old_viewstate;
230 pos_T match_start;
231 pos_T match_end;
232 int did_incsearch;
233 int incsearch_postponed;
234} incsearch_state_T;
235
236 static void
237init_incsearch_state(incsearch_state_T *is_state)
238{
239 is_state->match_start = curwin->w_cursor;
240 is_state->did_incsearch = FALSE;
241 is_state->incsearch_postponed = FALSE;
242 CLEAR_POS(&is_state->match_end);
243 is_state->save_cursor = curwin->w_cursor; // may be restored later
244 is_state->search_start = curwin->w_cursor;
245 save_viewstate(&is_state->init_viewstate);
246 save_viewstate(&is_state->old_viewstate);
247}
248
249/*
250 * First move cursor to end of match, then to the start. This
251 * moves the whole match onto the screen when 'nowrap' is set.
252 */
253 static void
254set_search_match(pos_T *t)
255{
256 t->lnum += search_match_lines;
257 t->col = search_match_endcol;
258 if (t->lnum > curbuf->b_ml.ml_line_count)
259 {
260 t->lnum = curbuf->b_ml.ml_line_count;
261 coladvance((colnr_T)MAXCOL);
262 }
263}
264
265/*
266 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200267 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200268 */
269 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200270do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
271 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200272{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
276 if (p_is && !cmd_silent)
277 {
278 // by default search all lines
279 search_first_line = 0;
280 search_last_line = MAXLNUM;
281
282 if (firstc == '/' || firstc == '?')
283 return TRUE;
284 if (firstc == ':')
285 {
286 char_u *cmd = skip_range(ccline.cmdbuff, NULL);
287 char_u *p;
288 int delim;
289 char_u *end;
290
291 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
292 {
293 // Skip over "substitute" to find the pattern separator.
294 for (p = cmd; ASCII_ISALPHA(*p); ++p)
295 ;
296 if (*p != NUL)
297 {
298 delim = *p++;
299 end = skip_regexp(p, delim, p_magic, NULL);
300 if (end > p)
301 {
302 char_u *dummy;
303 exarg_T ea;
304 pos_T save_cursor = curwin->w_cursor;
305
306 // found a non-empty pattern
307 *skiplen = (int)(p - ccline.cmdbuff);
308 *patlen = (int)(end - p);
309
310 // parse the address range
311 vim_memset(&ea, 0, sizeof(ea));
312 ea.line1 = 1;
313 ea.line2 = 1;
314 ea.cmd = ccline.cmdbuff;
315 ea.addr_type = ADDR_LINES;
316 parse_cmd_address(&ea, &dummy);
317 curwin->w_cursor = is_state->search_start;
318 if (ea.addr_count > 0)
319 {
320 search_first_line = ea.line1;
321 search_last_line = ea.line2;
322 }
323 else if (*cmd == 's')
324 {
325 // :s defaults to the current line
326 search_first_line = curwin->w_cursor.lnum;
327 search_last_line = curwin->w_cursor.lnum;
328 }
329
330 curwin->w_cursor = save_cursor;
331 return TRUE;
332 }
333 }
334 }
335 }
336 }
337
338 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200339}
340
341/*
342 * Do 'incsearch' highlighting if desired.
343 */
344 static void
345may_do_incsearch_highlighting(
346 int firstc,
347 long count,
348 incsearch_state_T *is_state)
349{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200351 int i;
352 pos_T end_pos;
353 struct cmdline_info save_ccline;
354#ifdef FEAT_RELTIME
355 proftime_T tm;
356#endif
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200357 int c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200358
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200359 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200360 return;
361
362 // If there is a character waiting, search and redraw later.
363 if (char_avail())
364 {
365 is_state->incsearch_postponed = TRUE;
366 return;
367 }
368 is_state->incsearch_postponed = FALSE;
369
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200370 if (search_first_line == 0)
371 // start at the original cursor position
372 curwin->w_cursor = is_state->search_start;
373 else
374 {
375 // start at the first line in the range
376 curwin->w_cursor.lnum = search_first_line;
377 curwin->w_cursor.col = 0;
378 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200379 save_last_search_pattern();
380
381 // If there is no command line, don't do anything.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200382 if (patlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200383 {
384 i = 0;
385 set_no_hlsearch(TRUE); // turn off previous highlight
386 redraw_all_later(SOME_VALID);
387 }
388 else
389 {
390 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
391
392 cursor_off(); // so the user knows we're busy
393 out_flush();
394 ++emsg_off; // so it doesn't beep if bad expr
395#ifdef FEAT_RELTIME
396 // Set the time limit to half a second.
397 profile_setlimit(500L, &tm);
398#endif
399 if (!p_hls)
400 search_flags += SEARCH_KEEP;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200401 c = ccline.cmdbuff[skiplen + patlen];
402 ccline.cmdbuff[skiplen + patlen] = NUL;
403 i = do_search(NULL, firstc == ':' ? '/' : firstc,
404 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200405#ifdef FEAT_RELTIME
406 &tm, NULL
407#else
408 NULL, NULL
409#endif
410 );
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200411 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200412 --emsg_off;
413
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200414 if (curwin->w_cursor.lnum < search_first_line
415 || curwin->w_cursor.lnum > search_last_line)
416 // match outside of address range
417 i = 0;
418
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200419 // if interrupted while searching, behave like it failed
420 if (got_int)
421 {
422 (void)vpeekc(); // remove <C-C> from input stream
423 got_int = FALSE; // don't abandon the command line
424 i = 0;
425 }
426 else if (char_avail())
427 // cancelled searching because a char was typed
428 is_state->incsearch_postponed = TRUE;
429 }
430 if (i != 0)
431 highlight_match = TRUE; // highlight position
432 else
433 highlight_match = FALSE; // remove highlight
434
435 // First restore the old curwin values, so the screen is positioned in the
436 // same way as the actual search command.
437 restore_viewstate(&is_state->old_viewstate);
438 changed_cline_bef_curs();
439 update_topline();
440
441 if (i != 0)
442 {
443 pos_T save_pos = curwin->w_cursor;
444
445 is_state->match_start = curwin->w_cursor;
446 set_search_match(&curwin->w_cursor);
447 validate_cursor();
448 end_pos = curwin->w_cursor;
449 is_state->match_end = end_pos;
450 curwin->w_cursor = save_pos;
451 }
452 else
453 end_pos = curwin->w_cursor; // shutup gcc 4
454
455 // Disable 'hlsearch' highlighting if the pattern matches everything.
456 // Avoids a flash when typing "foo\|".
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200457 c = ccline.cmdbuff[skiplen + patlen];
458 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200459 if (empty_pattern(ccline.cmdbuff))
460 set_no_hlsearch(TRUE);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200461 ccline.cmdbuff[skiplen + patlen] = c;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462
463 validate_cursor();
464 // May redraw the status line to show the cursor position.
465 if (p_ru && curwin->w_status_height > 0)
466 curwin->w_redr_status = TRUE;
467
468 save_cmdline(&save_ccline);
469 update_screen(SOME_VALID);
470 restore_cmdline(&save_ccline);
471 restore_last_search_pattern();
472
473 // Leave it at the end to make CTRL-R CTRL-W work.
474 if (i != 0)
475 curwin->w_cursor = end_pos;
476
477 msg_starthere();
478 redrawcmdline();
479 is_state->did_incsearch = TRUE;
480}
481
482/*
483 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
484 * or previous match.
485 * Returns FAIL when jumping to cmdline_not_changed;
486 */
487 static int
488may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200489 int firstc,
490 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200491 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200492 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200493{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200494 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200495 pos_T t;
496 char_u *pat;
497 int search_flags = SEARCH_NOOF;
498 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200499 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200500
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200501 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200503 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200504 return FAIL;
505
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200507 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200508 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200509 skiplen = 0;
510 patlen = STRLEN(pat);
511 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200512 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200513 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200514
515 save_last_search_pattern();
516 cursor_off();
517 out_flush();
518 if (c == Ctrl_G)
519 {
520 t = is_state->match_end;
521 if (LT_POS(is_state->match_start, is_state->match_end))
522 // Start searching at the end of the match not at the beginning of
523 // the next column.
524 (void)decl(&t);
525 search_flags += SEARCH_COL;
526 }
527 else
528 t = is_state->match_start;
529 if (!p_hls)
530 search_flags += SEARCH_KEEP;
531 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200532 save = pat[patlen];
533 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200534 i = searchit(curwin, curbuf, &t,
535 c == Ctrl_G ? FORWARD : BACKWARD,
536 pat, count, search_flags,
537 RE_SEARCH, 0, NULL, NULL);
538 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200539 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540 if (i)
541 {
542 is_state->search_start = is_state->match_start;
543 is_state->match_end = t;
544 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200545 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200546 {
547 // Move just before the current match, so that when nv_search
548 // finishes the cursor will be put back on the match.
549 is_state->search_start = t;
550 (void)decl(&is_state->search_start);
551 }
552 else if (c == Ctrl_G && firstc == '?')
553 {
554 // Move just after the current match, so that when nv_search
555 // finishes the cursor will be put back on the match.
556 is_state->search_start = t;
557 (void)incl(&is_state->search_start);
558 }
559 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
560 {
561 // wrap around
562 is_state->search_start = t;
563 if (firstc == '?')
564 (void)incl(&is_state->search_start);
565 else
566 (void)decl(&is_state->search_start);
567 }
568
569 set_search_match(&is_state->match_end);
570 curwin->w_cursor = is_state->match_start;
571 changed_cline_bef_curs();
572 update_topline();
573 validate_cursor();
574 highlight_match = TRUE;
575 save_viewstate(&is_state->old_viewstate);
576 update_screen(NOT_VALID);
577 redrawcmdline();
578 }
579 else
580 vim_beep(BO_ERROR);
581 restore_last_search_pattern();
582 return FAIL;
583}
584
585/*
586 * When CTRL-L typed: add character from the match to the pattern.
587 * May set "*c" to the added character.
588 * Return OK when jumping to cmdline_not_changed.
589 */
590 static int
591may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
592{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200593 int skiplen, patlen;
594
595 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 return FAIL;
597
598 // Add a character from under the cursor for 'incsearch'.
599 if (is_state->did_incsearch)
600 {
601 curwin->w_cursor = is_state->match_end;
602 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
603 {
604 *c = gchar_cursor();
605
606 // If 'ignorecase' and 'smartcase' are set and the
607 // command line has no uppercase characters, convert
608 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200609 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610 *c = MB_TOLOWER(*c);
611 if (*c != NUL)
612 {
613 if (*c == firstc || vim_strchr((char_u *)(
614 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
615 {
616 // put a backslash before special characters
617 stuffcharReadbuff(*c);
618 *c = '\\';
619 }
620 return FAIL;
621 }
622 }
623 }
624 return OK;
625}
626
627 static void
628finish_incsearch_highlighting(int gotesc, incsearch_state_T *is_state)
629{
630 if (is_state->did_incsearch)
631 {
632 if (gotesc)
633 curwin->w_cursor = is_state->save_cursor;
634 else
635 {
636 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
637 {
638 // put the '" mark at the original position
639 curwin->w_cursor = is_state->save_cursor;
640 setpcmark();
641 }
642 curwin->w_cursor = is_state->search_start;
643 }
644 restore_viewstate(&is_state->old_viewstate);
645 highlight_match = FALSE;
646 validate_cursor(); /* needed for TAB */
647 redraw_all_later(SOME_VALID);
648 }
649}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200650#endif
651
Bram Moolenaar66216052017-12-16 16:33:44 +0100652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 * getcmdline() - accept a command line starting with firstc.
654 *
655 * firstc == ':' get ":" command line.
656 * firstc == '/' or '?' get search pattern
657 * firstc == '=' get expression
658 * firstc == '@' get text for input() function
659 * firstc == '>' get text for debug mode
660 * firstc == NUL get text for :insert command
661 * firstc == -1 like NUL, and break on CTRL-C
662 *
663 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
664 * command line.
665 *
666 * Careful: getcmdline() can be called recursively!
667 *
668 * Return pointer to allocated string if there is a commandline, NULL
669 * otherwise.
670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100672getcmdline(
673 int firstc,
674 long count UNUSED, /* only used for incremental search */
675 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676{
677 int c;
678 int i;
679 int j;
680 int gotesc = FALSE; /* TRUE when <ESC> just typed */
681 int do_abbr; /* when TRUE check for abbr. */
682#ifdef FEAT_CMDHIST
683 char_u *lookfor = NULL; /* string to match */
684 int hiscnt; /* current history line in use */
685 int histype; /* history type to be used */
686#endif
687#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200688 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689#endif
690 int did_wild_list = FALSE; /* did wild_list() recently */
691 int wim_index = 0; /* index in wim_flags[] */
692 int res;
693 int save_msg_scroll = msg_scroll;
694 int save_State = State; /* remember State when called */
695 int some_key_typed = FALSE; /* one of the keys was typed */
696#ifdef FEAT_MOUSE
697 /* mouse drag and release events are ignored, unless they are
698 * preceded with a mouse down event */
699 int ignore_drag_release = TRUE;
700#endif
701#ifdef FEAT_EVAL
702 int break_ctrl_c = FALSE;
703#endif
704 expand_T xpc;
705 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200706#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000707 /* Everything that may work recursively should save and restore the
708 * current command line in save_ccline. That includes update_screen(), a
709 * custom status line may invoke ":normal". */
710 struct cmdline_info save_ccline;
711#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200712 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#ifdef FEAT_EVAL
715 if (firstc == -1)
716 {
717 firstc = NUL;
718 break_ctrl_c = TRUE;
719 }
720#endif
721#ifdef FEAT_RIGHTLEFT
722 /* start without Hebrew mapping for a command line */
723 if (firstc == ':' || firstc == '=' || firstc == '>')
724 cmd_hkmap = 0;
725#endif
726
727 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200730 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731#endif
732
733 /*
734 * set some variables for redrawcmd()
735 */
736 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000737 ccline.cmdindent = (firstc > 0 ? indent : 0);
738
739 /* alloc initial ccline.cmdbuff */
740 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 if (ccline.cmdbuff == NULL)
742 return NULL; /* out of memory */
743 ccline.cmdlen = ccline.cmdpos = 0;
744 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100745 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000747 /* autoindent for :insert and :append */
748 if (firstc <= 0)
749 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200750 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000751 ccline.cmdbuff[indent] = NUL;
752 ccline.cmdpos = indent;
753 ccline.cmdspos = indent;
754 ccline.cmdlen = indent;
755 }
756
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000758 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759
760#ifdef FEAT_RIGHTLEFT
761 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
762 && (firstc == '/' || firstc == '?'))
763 cmdmsg_rl = TRUE;
764 else
765 cmdmsg_rl = FALSE;
766#endif
767
768 redir_off = TRUE; /* don't redirect the typed command */
769 if (!cmd_silent)
770 {
771 i = msg_scrolled;
772 msg_scrolled = 0; /* avoid wait_return message */
773 gotocmdline(TRUE);
774 msg_scrolled += i;
775 redrawcmdprompt(); /* draw prompt or indent */
776 set_cmdspos();
777 }
778 xpc.xp_context = EXPAND_NOTHING;
779 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000780#ifndef BACKSLASH_IN_FILENAME
781 xpc.xp_shell = FALSE;
782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000784#if defined(FEAT_EVAL)
785 if (ccline.input_fn)
786 {
787 xpc.xp_context = ccline.xp_context;
788 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000789# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000790 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000791# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000792 }
793#endif
794
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 /*
796 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
797 * doing ":@0" when register 0 doesn't contain a CR.
798 */
799 msg_scroll = FALSE;
800
801 State = CMDLINE;
802
803 if (firstc == '/' || firstc == '?' || firstc == '@')
804 {
805 /* Use ":lmap" mappings for search pattern and input(). */
806 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
807 b_im_ptr = &curbuf->b_p_iminsert;
808 else
809 b_im_ptr = &curbuf->b_p_imsearch;
810 if (*b_im_ptr == B_IMODE_LMAP)
811 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100812#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 im_set_active(*b_im_ptr == B_IMODE_IM);
814#endif
815 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100816#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 else if (p_imcmdline)
818 im_set_active(TRUE);
819#endif
820
821#ifdef FEAT_MOUSE
822 setmouse();
823#endif
824#ifdef CURSOR_SHAPE
825 ui_cursor_shape(); /* may show different cursor shape */
826#endif
827
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000828 /* When inside an autocommand for writing "exiting" may be set and
829 * terminal mode set to cooked. Need to set raw mode here then. */
830 settmode(TMODE_RAW);
831
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200832 /* Trigger CmdlineEnter autocommands. */
833 cmdline_type = firstc == NUL ? '-' : firstc;
834 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200835
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_CMDHIST
837 init_history();
838 hiscnt = hislen; /* set hiscnt to impossible history value */
839 histype = hist_char2type(firstc);
840#endif
841
842#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000843 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844#endif
845
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200846 /* If something above caused an error, reset the flags, we do want to type
847 * and execute commands. Display may be messed up a bit. */
848 if (did_emsg)
849 redrawcmd();
850 did_emsg = FALSE;
851 got_int = FALSE;
852
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 /*
854 * Collect the command string, handling editing keys.
855 */
856 for (;;)
857 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000858 redir_off = TRUE; /* Don't redirect the typed command.
859 Repeated, because a ":redir" inside
860 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861#ifdef USE_ON_FLY_SCROLL
862 dont_scroll = FALSE; /* allow scrolling here */
863#endif
864 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
865
Bram Moolenaar72532d32018-04-07 19:09:09 +0200866 did_emsg = FALSE; /* There can't really be a reason why an error
867 that occurs while typing a command should
868 cause the command not to be executed. */
869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000871
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100872 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
873 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000874 do
875 {
876 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100877 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 if (KeyTyped)
880 {
881 some_key_typed = TRUE;
882#ifdef FEAT_RIGHTLEFT
883 if (cmd_hkmap)
884 c = hkmap(c);
885# ifdef FEAT_FKMAP
886 if (cmd_fkmap)
887 c = cmdl_fkmap(c);
888# endif
889 if (cmdmsg_rl && !KeyStuffed)
890 {
891 /* Invert horizontal movements and operations. Only when
892 * typed by the user directly, not when the result of a
893 * mapping. */
894 switch (c)
895 {
896 case K_RIGHT: c = K_LEFT; break;
897 case K_S_RIGHT: c = K_S_LEFT; break;
898 case K_C_RIGHT: c = K_C_LEFT; break;
899 case K_LEFT: c = K_RIGHT; break;
900 case K_S_LEFT: c = K_S_RIGHT; break;
901 case K_C_LEFT: c = K_C_RIGHT; break;
902 }
903 }
904#endif
905 }
906
907 /*
908 * Ignore got_int when CTRL-C was typed here.
909 * Don't ignore it in :global, we really need to break then, e.g., for
910 * ":g/pat/normal /pat" (without the <CR>).
911 * Don't ignore it for the input() function.
912 */
913 if ((c == Ctrl_C
914#ifdef UNIX
915 || c == intr_char
916#endif
917 )
918#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
919 && firstc != '@'
920#endif
921#ifdef FEAT_EVAL
922 && !break_ctrl_c
923#endif
924 && !global_busy)
925 got_int = FALSE;
926
927#ifdef FEAT_CMDHIST
928 /* free old command line when finished moving around in the history
929 * list */
930 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000931 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000932 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 && c != K_PAGEDOWN && c != K_PAGEUP
934 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000935 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100937 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938#endif
939
940 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000941 * When there are matching completions to select <S-Tab> works like
942 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000944 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 c = Ctrl_P;
946
947#ifdef FEAT_WILDMENU
948 /* Special translations for 'wildmenu' */
949 if (did_wild_list && p_wmnu)
950 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000951 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000953 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 c = Ctrl_N;
955 }
956 /* Hitting CR after "emenu Name.": complete submenu */
957 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
958 && ccline.cmdpos > 1
959 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
960 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
961 && (c == '\n' || c == '\r' || c == K_KENTER))
962 c = K_DOWN;
963#endif
964
965 /* free expanded names when finished walking through matches */
966 if (xpc.xp_numfiles != -1
967 && !(c == p_wc && KeyTyped) && c != p_wcm
968 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
969 && c != Ctrl_L)
970 {
971 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
972 did_wild_list = FALSE;
973#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000974 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975#endif
976 xpc.xp_context = EXPAND_NOTHING;
977 wim_index = 0;
978#ifdef FEAT_WILDMENU
979 if (p_wmnu && wild_menu_showing != 0)
980 {
981 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000982 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000983
984 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000985 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
987 if (wild_menu_showing == WM_SCROLLED)
988 {
989 /* Entered command line, move it up */
990 cmdline_row--;
991 redrawcmd();
992 }
993 else if (save_p_ls != -1)
994 {
995 /* restore 'laststatus' and 'winminheight' */
996 p_ls = save_p_ls;
997 p_wmh = save_p_wmh;
998 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000999 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001001 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 redrawcmd();
1003 save_p_ls = -1;
1004 }
1005 else
1006 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 redraw_statuslines();
1009 }
1010 KeyTyped = skt;
1011 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001012 if (ccline.input_fn)
1013 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
1015#endif
1016 }
1017
1018#ifdef FEAT_WILDMENU
1019 /* Special translations for 'wildmenu' */
1020 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1021 {
1022 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001023 if (c == K_DOWN && ccline.cmdpos > 0
1024 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001026 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 {
1028 /* Hitting <Up>: Remove one submenu name in front of the
1029 * cursor */
1030 int found = FALSE;
1031
1032 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1033 i = 0;
1034 while (--j > 0)
1035 {
1036 /* check for start of menu name */
1037 if (ccline.cmdbuff[j] == ' '
1038 && ccline.cmdbuff[j - 1] != '\\')
1039 {
1040 i = j + 1;
1041 break;
1042 }
1043 /* check for start of submenu name */
1044 if (ccline.cmdbuff[j] == '.'
1045 && ccline.cmdbuff[j - 1] != '\\')
1046 {
1047 if (found)
1048 {
1049 i = j + 1;
1050 break;
1051 }
1052 else
1053 found = TRUE;
1054 }
1055 }
1056 if (i > 0)
1057 cmdline_del(i);
1058 c = p_wc;
1059 xpc.xp_context = EXPAND_NOTHING;
1060 }
1061 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001062 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001063 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001064 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 {
1066 char_u upseg[5];
1067
1068 upseg[0] = PATHSEP;
1069 upseg[1] = '.';
1070 upseg[2] = '.';
1071 upseg[3] = PATHSEP;
1072 upseg[4] = NUL;
1073
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001074 if (c == K_DOWN
1075 && ccline.cmdpos > 0
1076 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1077 && (ccline.cmdpos < 3
1078 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1080 {
1081 /* go down a directory */
1082 c = p_wc;
1083 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001084 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 {
1086 /* If in a direct ancestor, strip off one ../ to go down */
1087 int found = FALSE;
1088
1089 j = ccline.cmdpos;
1090 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1091 while (--j > i)
1092 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001093#ifdef FEAT_MBYTE
1094 if (has_mbyte)
1095 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (vim_ispathsep(ccline.cmdbuff[j]))
1098 {
1099 found = TRUE;
1100 break;
1101 }
1102 }
1103 if (found
1104 && ccline.cmdbuff[j - 1] == '.'
1105 && ccline.cmdbuff[j - 2] == '.'
1106 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1107 {
1108 cmdline_del(j - 2);
1109 c = p_wc;
1110 }
1111 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001112 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 {
1114 /* go up a directory */
1115 int found = FALSE;
1116
1117 j = ccline.cmdpos - 1;
1118 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1119 while (--j > i)
1120 {
1121#ifdef FEAT_MBYTE
1122 if (has_mbyte)
1123 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1124#endif
1125 if (vim_ispathsep(ccline.cmdbuff[j])
1126#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001127 && vim_strchr((char_u *)" *?[{`$%#",
1128 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129#endif
1130 )
1131 {
1132 if (found)
1133 {
1134 i = j + 1;
1135 break;
1136 }
1137 else
1138 found = TRUE;
1139 }
1140 }
1141
1142 if (!found)
1143 j = i;
1144 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1145 j += 4;
1146 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1147 && j == i)
1148 j += 3;
1149 else
1150 j = 0;
1151 if (j > 0)
1152 {
1153 /* TODO this is only for DOS/UNIX systems - need to put in
1154 * machine-specific stuff here and in upseg init */
1155 cmdline_del(j);
1156 put_on_cmdline(upseg + 1, 3, FALSE);
1157 }
1158 else if (ccline.cmdpos > i)
1159 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001160
1161 /* Now complete in the new directory. Set KeyTyped in case the
1162 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001164 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167
1168#endif /* FEAT_WILDMENU */
1169
1170 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1171 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1172 if (c == Ctrl_BSL)
1173 {
1174 ++no_mapping;
1175 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001176 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 --no_mapping;
1178 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001179 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1180 * is in a mapping. */
1181 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1182 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 {
1184 vungetc(c);
1185 c = Ctrl_BSL;
1186 }
1187#ifdef FEAT_EVAL
1188 else if (c == 'e')
1189 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001190 char_u *p = NULL;
1191 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 /*
1194 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001195 * Need to save and restore the current command line, to be
1196 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 */
1198 if (ccline.cmdpos == ccline.cmdlen)
1199 new_cmdpos = 99999; /* keep it at the end */
1200 else
1201 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001202
1203 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001205 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (c == '=')
1207 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001208 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001209 * to avoid nasty things like going to another buffer when
1210 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001211 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001212 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001214 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001215 restore_cmdline(&save_ccline);
1216
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001217 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001219 len = (int)STRLEN(p);
1220 if (realloc_cmdbuff(len + 1) == OK)
1221 {
1222 ccline.cmdlen = len;
1223 STRCPY(ccline.cmdbuff, p);
1224 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001226 /* Restore the cursor or use the position set with
1227 * set_cmdline_pos(). */
1228 if (new_cmdpos > ccline.cmdlen)
1229 ccline.cmdpos = ccline.cmdlen;
1230 else
1231 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001233 KeyTyped = FALSE; /* Don't do p_wc completion. */
1234 redrawcmd();
1235 goto cmdline_changed;
1236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 }
1238 }
1239 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001240 got_int = FALSE; /* don't abandon the command line */
1241 did_emsg = FALSE;
1242 emsg_on_display = FALSE;
1243 redrawcmd();
1244 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 }
1246#endif
1247 else
1248 {
1249 if (c == Ctrl_G && p_im && restart_edit == 0)
1250 restart_edit = 'a';
1251 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1252 in history */
1253 goto returncmd; /* back to Normal mode */
1254 }
1255 }
1256
1257#ifdef FEAT_CMDWIN
1258 if (c == cedit_key || c == K_CMDWIN)
1259 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001260 if (ex_normal_busy == 0 && got_int == FALSE)
1261 {
1262 /*
1263 * Open a window to edit the command line (and history).
1264 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001265 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001266 some_key_typed = TRUE;
1267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269# ifdef FEAT_DIGRAPHS
1270 else
1271# endif
1272#endif
1273#ifdef FEAT_DIGRAPHS
1274 c = do_digraph(c);
1275#endif
1276
1277 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1278 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1279 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001280 /* In Ex mode a backslash escapes a newline. */
1281 if (exmode_active
1282 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001283 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001284 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001285 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001287 if (c == K_KENTER)
1288 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001290 else
1291 {
1292 gotesc = FALSE; /* Might have typed ESC previously, don't
1293 truncate the cmdline now. */
1294 if (ccheck_abbr(c + ABBR_OFF))
1295 goto cmdline_changed;
1296 if (!cmd_silent)
1297 {
1298 windgoto(msg_row, 0);
1299 out_flush();
1300 }
1301 break;
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304
1305 /*
1306 * Completion for 'wildchar' or 'wildcharm' key.
1307 * - hitting <ESC> twice means: abandon command line.
1308 * - wildcard expansion is only done when the 'wildchar' key is really
1309 * typed, not when it comes from a macro
1310 */
1311 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1312 {
1313 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1314 {
1315 /* if 'wildmode' contains "list" may still need to list */
1316 if (xpc.xp_numfiles > 1
1317 && !did_wild_list
1318 && (wim_flags[wim_index] & WIM_LIST))
1319 {
1320 (void)showmatches(&xpc, FALSE);
1321 redrawcmd();
1322 did_wild_list = TRUE;
1323 }
1324 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001325 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1326 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001328 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1329 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 else
1331 res = OK; /* don't insert 'wildchar' now */
1332 }
1333 else /* typed p_wc first time */
1334 {
1335 wim_index = 0;
1336 j = ccline.cmdpos;
1337 /* if 'wildmode' first contains "longest", get longest
1338 * common part */
1339 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001340 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1341 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001343 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1344 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345
1346 /* if interrupted while completing, behave like it failed */
1347 if (got_int)
1348 {
1349 (void)vpeekc(); /* remove <C-C> from input stream */
1350 got_int = FALSE; /* don't abandon the command line */
1351 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1352#ifdef FEAT_WILDMENU
1353 xpc.xp_context = EXPAND_NOTHING;
1354#endif
1355 goto cmdline_changed;
1356 }
1357
1358 /* when more than one match, and 'wildmode' first contains
1359 * "list", or no change and 'wildmode' contains "longest,list",
1360 * list all matches */
1361 if (res == OK && xpc.xp_numfiles > 1)
1362 {
1363 /* a "longest" that didn't do anything is skipped (but not
1364 * "list:longest") */
1365 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1366 wim_index = 1;
1367 if ((wim_flags[wim_index] & WIM_LIST)
1368#ifdef FEAT_WILDMENU
1369 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1370#endif
1371 )
1372 {
1373 if (!(wim_flags[0] & WIM_LONGEST))
1374 {
1375#ifdef FEAT_WILDMENU
1376 int p_wmnu_save = p_wmnu;
1377 p_wmnu = 0;
1378#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001379 /* remove match */
1380 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381#ifdef FEAT_WILDMENU
1382 p_wmnu = p_wmnu_save;
1383#endif
1384 }
1385#ifdef FEAT_WILDMENU
1386 (void)showmatches(&xpc, p_wmnu
1387 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1388#else
1389 (void)showmatches(&xpc, FALSE);
1390#endif
1391 redrawcmd();
1392 did_wild_list = TRUE;
1393 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001394 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1395 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001397 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1398 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
1400 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001401 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403#ifdef FEAT_WILDMENU
1404 else if (xpc.xp_numfiles == -1)
1405 xpc.xp_context = EXPAND_NOTHING;
1406#endif
1407 }
1408 if (wim_index < 3)
1409 ++wim_index;
1410 if (c == ESC)
1411 gotesc = TRUE;
1412 if (res == OK)
1413 goto cmdline_changed;
1414 }
1415
1416 gotesc = FALSE;
1417
1418 /* <S-Tab> goes to last match, in a clumsy way */
1419 if (c == K_S_TAB && KeyTyped)
1420 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001421 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1422 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1423 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001424 goto cmdline_changed;
1425 }
1426
1427 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1428 c = NL;
1429
1430 do_abbr = TRUE; /* default: check for abbreviation */
1431
1432 /*
1433 * Big switch for a typed command line character.
1434 */
1435 switch (c)
1436 {
1437 case K_BS:
1438 case Ctrl_H:
1439 case K_DEL:
1440 case K_KDEL:
1441 case Ctrl_W:
1442#ifdef FEAT_FKMAP
1443 if (cmd_fkmap && c == K_BS)
1444 c = K_DEL;
1445#endif
1446 if (c == K_KDEL)
1447 c = K_DEL;
1448
1449 /*
1450 * delete current character is the same as backspace on next
1451 * character, except at end of line
1452 */
1453 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1454 ++ccline.cmdpos;
1455#ifdef FEAT_MBYTE
1456 if (has_mbyte && c == K_DEL)
1457 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1458 ccline.cmdbuff + ccline.cmdpos);
1459#endif
1460 if (ccline.cmdpos > 0)
1461 {
1462 char_u *p;
1463
1464 j = ccline.cmdpos;
1465 p = ccline.cmdbuff + j;
1466#ifdef FEAT_MBYTE
1467 if (has_mbyte)
1468 {
1469 p = mb_prevptr(ccline.cmdbuff, p);
1470 if (c == Ctrl_W)
1471 {
1472 while (p > ccline.cmdbuff && vim_isspace(*p))
1473 p = mb_prevptr(ccline.cmdbuff, p);
1474 i = mb_get_class(p);
1475 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1476 p = mb_prevptr(ccline.cmdbuff, p);
1477 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001478 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 }
1480 }
1481 else
1482#endif
1483 if (c == Ctrl_W)
1484 {
1485 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1486 --p;
1487 i = vim_iswordc(p[-1]);
1488 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1489 && vim_iswordc(p[-1]) == i)
1490 --p;
1491 }
1492 else
1493 --p;
1494 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1495 ccline.cmdlen -= j - ccline.cmdpos;
1496 i = ccline.cmdpos;
1497 while (i < ccline.cmdlen)
1498 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1499
1500 /* Truncate at the end, required for multi-byte chars. */
1501 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001502#ifdef FEAT_SEARCH_EXTRA
1503 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001504 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001505 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001506 /* save view settings, so that the screen
1507 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001508 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001509 }
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 redrawcmd();
1512 }
1513 else if (ccline.cmdlen == 0 && c != Ctrl_W
1514 && ccline.cmdprompt == NULL && indent == 0)
1515 {
1516 /* In ex and debug mode it doesn't make sense to return. */
1517 if (exmode_active
1518#ifdef FEAT_EVAL
1519 || ccline.cmdfirstc == '>'
1520#endif
1521 )
1522 goto cmdline_not_changed;
1523
Bram Moolenaard23a8232018-02-10 18:45:26 +01001524 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (!cmd_silent)
1526 {
1527#ifdef FEAT_RIGHTLEFT
1528 if (cmdmsg_rl)
1529 msg_col = Columns;
1530 else
1531#endif
1532 msg_col = 0;
1533 msg_putchar(' '); /* delete ':' */
1534 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001535#ifdef FEAT_SEARCH_EXTRA
1536 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001537 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 redraw_cmdline = TRUE;
1540 goto returncmd; /* back to cmd mode */
1541 }
1542 goto cmdline_changed;
1543
1544 case K_INS:
1545 case K_KINS:
1546#ifdef FEAT_FKMAP
1547 /* if Farsi mode set, we are in reverse insert mode -
1548 Do not change the mode */
1549 if (cmd_fkmap)
1550 beep_flush();
1551 else
1552#endif
1553 ccline.overstrike = !ccline.overstrike;
1554#ifdef CURSOR_SHAPE
1555 ui_cursor_shape(); /* may show different cursor shape */
1556#endif
1557 goto cmdline_not_changed;
1558
1559 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001560 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 {
1562 /* ":lmap" mappings exists, toggle use of mappings. */
1563 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001564#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 im_set_active(FALSE); /* Disable input method */
1566#endif
1567 if (b_im_ptr != NULL)
1568 {
1569 if (State & LANGMAP)
1570 *b_im_ptr = B_IMODE_LMAP;
1571 else
1572 *b_im_ptr = B_IMODE_NONE;
1573 }
1574 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001575#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 else
1577 {
1578 /* There are no ":lmap" mappings, toggle IM. When
1579 * 'imdisable' is set don't try getting the status, it's
1580 * always off. */
1581 if ((p_imdisable && b_im_ptr != NULL)
1582 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1583 {
1584 im_set_active(FALSE); /* Disable input method */
1585 if (b_im_ptr != NULL)
1586 *b_im_ptr = B_IMODE_NONE;
1587 }
1588 else
1589 {
1590 im_set_active(TRUE); /* Enable input method */
1591 if (b_im_ptr != NULL)
1592 *b_im_ptr = B_IMODE_IM;
1593 }
1594 }
1595#endif
1596 if (b_im_ptr != NULL)
1597 {
1598 if (b_im_ptr == &curbuf->b_p_iminsert)
1599 set_iminsert_global();
1600 else
1601 set_imsearch_global();
1602 }
1603#ifdef CURSOR_SHAPE
1604 ui_cursor_shape(); /* may show different cursor shape */
1605#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001606#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /* Show/unshow value of 'keymap' in status lines later. */
1608 status_redraw_curbuf();
1609#endif
1610 goto cmdline_not_changed;
1611
1612/* case '@': only in very old vi */
1613 case Ctrl_U:
1614 /* delete all characters left of the cursor */
1615 j = ccline.cmdpos;
1616 ccline.cmdlen -= j;
1617 i = ccline.cmdpos = 0;
1618 while (i < ccline.cmdlen)
1619 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1620 /* Truncate at the end, required for multi-byte chars. */
1621 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001622#ifdef FEAT_SEARCH_EXTRA
1623 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001624 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 redrawcmd();
1627 goto cmdline_changed;
1628
1629#ifdef FEAT_CLIPBOARD
1630 case Ctrl_Y:
1631 /* Copy the modeless selection, if there is one. */
1632 if (clip_star.state != SELECT_CLEARED)
1633 {
1634 if (clip_star.state == SELECT_DONE)
1635 clip_copy_modeless_selection(TRUE);
1636 goto cmdline_not_changed;
1637 }
1638 break;
1639#endif
1640
1641 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1642 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001643 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001644 * ":normal" runs out of characters. */
1645 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001646 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 goto cmdline_not_changed;
1648
1649 gotesc = TRUE; /* will free ccline.cmdbuff after
1650 putting it in history */
1651 goto returncmd; /* back to cmd mode */
1652
1653 case Ctrl_R: /* insert register */
1654#ifdef USE_ON_FLY_SCROLL
1655 dont_scroll = TRUE; /* disallow scrolling here */
1656#endif
1657 putcmdline('"', TRUE);
1658 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001659 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 if (i == Ctrl_O)
1661 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1662 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001663 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001664 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 --no_mapping;
1666#ifdef FEAT_EVAL
1667 /*
1668 * Insert the result of an expression.
1669 * Need to save the current command line, to be able to enter
1670 * a new one...
1671 */
1672 new_cmdpos = -1;
1673 if (c == '=')
1674 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1676 {
1677 beep_flush();
1678 c = ESC;
1679 }
1680 else
1681 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001682 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001684 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 }
1686 }
1687#endif
1688 if (c != ESC) /* use ESC to cancel inserting register */
1689 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001690 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001691
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001692#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001693 /* When there was a serious error abort getting the
1694 * command line. */
1695 if (aborting())
1696 {
1697 gotesc = TRUE; /* will free ccline.cmdbuff after
1698 putting it in history */
1699 goto returncmd; /* back to cmd mode */
1700 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 KeyTyped = FALSE; /* Don't do p_wc completion. */
1703#ifdef FEAT_EVAL
1704 if (new_cmdpos >= 0)
1705 {
1706 /* set_cmdline_pos() was used */
1707 if (new_cmdpos > ccline.cmdlen)
1708 ccline.cmdpos = ccline.cmdlen;
1709 else
1710 ccline.cmdpos = new_cmdpos;
1711 }
1712#endif
1713 }
1714 redrawcmd();
1715 goto cmdline_changed;
1716
1717 case Ctrl_D:
1718 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1719 break; /* Use ^D as normal char instead */
1720
1721 redrawcmd();
1722 continue; /* don't do incremental search now */
1723
1724 case K_RIGHT:
1725 case K_S_RIGHT:
1726 case K_C_RIGHT:
1727 do
1728 {
1729 if (ccline.cmdpos >= ccline.cmdlen)
1730 break;
1731 i = cmdline_charsize(ccline.cmdpos);
1732 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1733 break;
1734 ccline.cmdspos += i;
1735#ifdef FEAT_MBYTE
1736 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001737 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 + ccline.cmdpos);
1739 else
1740#endif
1741 ++ccline.cmdpos;
1742 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001743 while ((c == K_S_RIGHT || c == K_C_RIGHT
1744 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1746#ifdef FEAT_MBYTE
1747 if (has_mbyte)
1748 set_cmdspos_cursor();
1749#endif
1750 goto cmdline_not_changed;
1751
1752 case K_LEFT:
1753 case K_S_LEFT:
1754 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001755 if (ccline.cmdpos == 0)
1756 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 do
1758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 --ccline.cmdpos;
1760#ifdef FEAT_MBYTE
1761 if (has_mbyte) /* move to first byte of char */
1762 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1763 ccline.cmdbuff + ccline.cmdpos);
1764#endif
1765 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1766 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001767 while (ccline.cmdpos > 0
1768 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001769 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1771#ifdef FEAT_MBYTE
1772 if (has_mbyte)
1773 set_cmdspos_cursor();
1774#endif
1775 goto cmdline_not_changed;
1776
1777 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001778 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001779 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780
Bram Moolenaar4770d092006-01-12 23:22:24 +00001781#ifdef FEAT_GUI_W32
1782 /* On Win32 ignore <M-F4>, we get it when closing the window was
1783 * cancelled. */
1784 case K_F4:
1785 if (mod_mask == MOD_MASK_ALT)
1786 {
1787 redrawcmd(); /* somehow the cmdline is cleared */
1788 goto cmdline_not_changed;
1789 }
1790 break;
1791#endif
1792
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_MOUSE
1794 case K_MIDDLEDRAG:
1795 case K_MIDDLERELEASE:
1796 goto cmdline_not_changed; /* Ignore mouse */
1797
1798 case K_MIDDLEMOUSE:
1799# ifdef FEAT_GUI
1800 /* When GUI is active, also paste when 'mouse' is empty */
1801 if (!gui.in_use)
1802# endif
1803 if (!mouse_has(MOUSE_COMMAND))
1804 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001805# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001807 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001809# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001810 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 redrawcmd();
1812 goto cmdline_changed;
1813
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001814# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001816 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 redrawcmd();
1818 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001819# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820
1821 case K_LEFTDRAG:
1822 case K_LEFTRELEASE:
1823 case K_RIGHTDRAG:
1824 case K_RIGHTRELEASE:
1825 /* Ignore drag and release events when the button-down wasn't
1826 * seen before. */
1827 if (ignore_drag_release)
1828 goto cmdline_not_changed;
1829 /* FALLTHROUGH */
1830 case K_LEFTMOUSE:
1831 case K_RIGHTMOUSE:
1832 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1833 ignore_drag_release = TRUE;
1834 else
1835 ignore_drag_release = FALSE;
1836# ifdef FEAT_GUI
1837 /* When GUI is active, also move when 'mouse' is empty */
1838 if (!gui.in_use)
1839# endif
1840 if (!mouse_has(MOUSE_COMMAND))
1841 goto cmdline_not_changed; /* Ignore mouse */
1842# ifdef FEAT_CLIPBOARD
1843 if (mouse_row < cmdline_row && clip_star.available)
1844 {
1845 int button, is_click, is_drag;
1846
1847 /*
1848 * Handle modeless selection.
1849 */
1850 button = get_mouse_button(KEY2TERMCAP1(c),
1851 &is_click, &is_drag);
1852 if (mouse_model_popup() && button == MOUSE_LEFT
1853 && (mod_mask & MOD_MASK_SHIFT))
1854 {
1855 /* Translate shift-left to right button. */
1856 button = MOUSE_RIGHT;
1857 mod_mask &= ~MOD_MASK_SHIFT;
1858 }
1859 clip_modeless(button, is_click, is_drag);
1860 goto cmdline_not_changed;
1861 }
1862# endif
1863
1864 set_cmdspos();
1865 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1866 ++ccline.cmdpos)
1867 {
1868 i = cmdline_charsize(ccline.cmdpos);
1869 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1870 && mouse_col < ccline.cmdspos % Columns + i)
1871 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001872# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (has_mbyte)
1874 {
1875 /* Count ">" for double-wide char that doesn't fit. */
1876 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001877 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 + ccline.cmdpos) - 1;
1879 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 ccline.cmdspos += i;
1882 }
1883 goto cmdline_not_changed;
1884
1885 /* Mouse scroll wheel: ignored here */
1886 case K_MOUSEDOWN:
1887 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001888 case K_MOUSELEFT:
1889 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 /* Alternate buttons ignored here */
1891 case K_X1MOUSE:
1892 case K_X1DRAG:
1893 case K_X1RELEASE:
1894 case K_X2MOUSE:
1895 case K_X2DRAG:
1896 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001897 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 goto cmdline_not_changed;
1899
1900#endif /* FEAT_MOUSE */
1901
1902#ifdef FEAT_GUI
1903 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1904 case K_LEFTRELEASE_NM:
1905 goto cmdline_not_changed;
1906
1907 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001908 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 gui_do_scroll();
1911 redrawcmd();
1912 }
1913 goto cmdline_not_changed;
1914
1915 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001916 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001918 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 redrawcmd();
1920 }
1921 goto cmdline_not_changed;
1922#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001923#ifdef FEAT_GUI_TABLINE
1924 case K_TABLINE:
1925 case K_TABMENU:
1926 /* Don't want to change any tabs here. Make sure the same tab
1927 * is still selected. */
1928 if (gui_use_tabline())
1929 gui_mch_set_curtab(tabpage_index(curtab));
1930 goto cmdline_not_changed;
1931#endif
1932
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 case K_SELECT: /* end of Select mode mapping - ignore */
1934 goto cmdline_not_changed;
1935
1936 case Ctrl_B: /* begin of command line */
1937 case K_HOME:
1938 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 case K_S_HOME:
1940 case K_C_HOME:
1941 ccline.cmdpos = 0;
1942 set_cmdspos();
1943 goto cmdline_not_changed;
1944
1945 case Ctrl_E: /* end of command line */
1946 case K_END:
1947 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 case K_S_END:
1949 case K_C_END:
1950 ccline.cmdpos = ccline.cmdlen;
1951 set_cmdspos_cursor();
1952 goto cmdline_not_changed;
1953
1954 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001955 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 break;
1957 goto cmdline_changed;
1958
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001959 case Ctrl_L:
1960#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001961 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001962 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001963#endif
1964
1965 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001966 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 break;
1968 goto cmdline_changed;
1969
1970 case Ctrl_N: /* next match */
1971 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001972 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001974 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1975 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001977 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001980 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 case K_UP:
1982 case K_DOWN:
1983 case K_S_UP:
1984 case K_S_DOWN:
1985 case K_PAGEUP:
1986 case K_KPAGEUP:
1987 case K_PAGEDOWN:
1988 case K_KPAGEDOWN:
1989 if (hislen == 0 || firstc == NUL) /* no history */
1990 goto cmdline_not_changed;
1991
1992 i = hiscnt;
1993
1994 /* save current command string so it can be restored later */
1995 if (lookfor == NULL)
1996 {
1997 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1998 goto cmdline_not_changed;
1999 lookfor[ccline.cmdpos] = NUL;
2000 }
2001
2002 j = (int)STRLEN(lookfor);
2003 for (;;)
2004 {
2005 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002006 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002007 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
2009 if (hiscnt == hislen) /* first time */
2010 hiscnt = hisidx[histype];
2011 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2012 hiscnt = hislen - 1;
2013 else if (hiscnt != hisidx[histype] + 1)
2014 --hiscnt;
2015 else /* at top of list */
2016 {
2017 hiscnt = i;
2018 break;
2019 }
2020 }
2021 else /* one step forwards */
2022 {
2023 /* on last entry, clear the line */
2024 if (hiscnt == hisidx[histype])
2025 {
2026 hiscnt = hislen;
2027 break;
2028 }
2029
2030 /* not on a history line, nothing to do */
2031 if (hiscnt == hislen)
2032 break;
2033 if (hiscnt == hislen - 1) /* wrap around */
2034 hiscnt = 0;
2035 else
2036 ++hiscnt;
2037 }
2038 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2039 {
2040 hiscnt = i;
2041 break;
2042 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002043 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002044 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 || STRNCMP(history[histype][hiscnt].hisstr,
2046 lookfor, (size_t)j) == 0)
2047 break;
2048 }
2049
2050 if (hiscnt != i) /* jumped to other entry */
2051 {
2052 char_u *p;
2053 int len;
2054 int old_firstc;
2055
2056 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002057 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 if (hiscnt == hislen)
2059 p = lookfor; /* back to the old one */
2060 else
2061 p = history[histype][hiscnt].hisstr;
2062
2063 if (histype == HIST_SEARCH
2064 && p != lookfor
2065 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2066 {
2067 /* Correct for the separator character used when
2068 * adding the history entry vs the one used now.
2069 * First loop: count length.
2070 * Second loop: copy the characters. */
2071 for (i = 0; i <= 1; ++i)
2072 {
2073 len = 0;
2074 for (j = 0; p[j] != NUL; ++j)
2075 {
2076 /* Replace old sep with new sep, unless it is
2077 * escaped. */
2078 if (p[j] == old_firstc
2079 && (j == 0 || p[j - 1] != '\\'))
2080 {
2081 if (i > 0)
2082 ccline.cmdbuff[len] = firstc;
2083 }
2084 else
2085 {
2086 /* Escape new sep, unless it is already
2087 * escaped. */
2088 if (p[j] == firstc
2089 && (j == 0 || p[j - 1] != '\\'))
2090 {
2091 if (i > 0)
2092 ccline.cmdbuff[len] = '\\';
2093 ++len;
2094 }
2095 if (i > 0)
2096 ccline.cmdbuff[len] = p[j];
2097 }
2098 ++len;
2099 }
2100 if (i == 0)
2101 {
2102 alloc_cmdbuff(len);
2103 if (ccline.cmdbuff == NULL)
2104 goto returncmd;
2105 }
2106 }
2107 ccline.cmdbuff[len] = NUL;
2108 }
2109 else
2110 {
2111 alloc_cmdbuff((int)STRLEN(p));
2112 if (ccline.cmdbuff == NULL)
2113 goto returncmd;
2114 STRCPY(ccline.cmdbuff, p);
2115 }
2116
2117 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2118 redrawcmd();
2119 goto cmdline_changed;
2120 }
2121 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002122#endif
2123 goto cmdline_not_changed;
2124
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002125#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002126 case Ctrl_G: /* next match */
2127 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002128 if (may_adjust_incsearch_highlighting(
2129 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002130 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002131 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132#endif
2133
2134 case Ctrl_V:
2135 case Ctrl_Q:
2136#ifdef FEAT_MOUSE
2137 ignore_drag_release = TRUE;
2138#endif
2139 putcmdline('^', TRUE);
2140 c = get_literal(); /* get next (two) character(s) */
2141 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002142 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#ifdef FEAT_MBYTE
2144 /* may need to remove ^ when composing char was typed */
2145 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2146 {
2147 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2148 msg_putchar(' ');
2149 cursorcmd();
2150 }
2151#endif
2152 break;
2153
2154#ifdef FEAT_DIGRAPHS
2155 case Ctrl_K:
2156#ifdef FEAT_MOUSE
2157 ignore_drag_release = TRUE;
2158#endif
2159 putcmdline('?', TRUE);
2160#ifdef USE_ON_FLY_SCROLL
2161 dont_scroll = TRUE; /* disallow scrolling here */
2162#endif
2163 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002164 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 if (c != NUL)
2166 break;
2167
2168 redrawcmd();
2169 goto cmdline_not_changed;
2170#endif /* FEAT_DIGRAPHS */
2171
2172#ifdef FEAT_RIGHTLEFT
2173 case Ctrl__: /* CTRL-_: switch language mode */
2174 if (!p_ari)
2175 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002176# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (p_altkeymap)
2178 {
2179 cmd_fkmap = !cmd_fkmap;
2180 if (cmd_fkmap) /* in Farsi always in Insert mode */
2181 ccline.overstrike = FALSE;
2182 }
2183 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 cmd_hkmap = !cmd_hkmap;
2186 goto cmdline_not_changed;
2187#endif
2188
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002189 case K_PS:
2190 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2191 goto cmdline_changed;
2192
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 default:
2194#ifdef UNIX
2195 if (c == intr_char)
2196 {
2197 gotesc = TRUE; /* will free ccline.cmdbuff after
2198 putting it in history */
2199 goto returncmd; /* back to Normal mode */
2200 }
2201#endif
2202 /*
2203 * Normal character with no special meaning. Just set mod_mask
2204 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2205 * the string <S-Space>. This should only happen after ^V.
2206 */
2207 if (!IS_SPECIAL(c))
2208 mod_mask = 0x0;
2209 break;
2210 }
2211 /*
2212 * End of switch on command line character.
2213 * We come here if we have a normal character.
2214 */
2215
Bram Moolenaarede3e632013-06-23 16:16:19 +02002216 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#ifdef FEAT_MBYTE
2218 /* Add ABBR_OFF for characters above 0x100, this is
2219 * what check_abbr() expects. */
2220 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2221#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002222 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 goto cmdline_changed;
2224
2225 /*
2226 * put the character in the command line
2227 */
2228 if (IS_SPECIAL(c) || mod_mask != 0)
2229 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2230 else
2231 {
2232#ifdef FEAT_MBYTE
2233 if (has_mbyte)
2234 {
2235 j = (*mb_char2bytes)(c, IObuff);
2236 IObuff[j] = NUL; /* exclude composing chars */
2237 put_on_cmdline(IObuff, j, TRUE);
2238 }
2239 else
2240#endif
2241 {
2242 IObuff[0] = c;
2243 put_on_cmdline(IObuff, 1, TRUE);
2244 }
2245 }
2246 goto cmdline_changed;
2247
2248/*
2249 * This part implements incremental searches for "/" and "?"
2250 * Jump to cmdline_not_changed when a character has been read but the command
2251 * line did not change. Then we only search and redraw if something changed in
2252 * the past.
2253 * Jump to cmdline_changed when the command line did change.
2254 * (Sorry for the goto's, I know it is ugly).
2255 */
2256cmdline_not_changed:
2257#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002258 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 continue;
2260#endif
2261
2262cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002263 /* Trigger CmdlineChanged autocommands. */
2264 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002265
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002267 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268#endif
2269
2270#ifdef FEAT_RIGHTLEFT
2271 if (cmdmsg_rl
2272# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002273 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274# endif
2275 )
2276 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002277 * right-left typing. Not efficient, but it works.
2278 * Do it only when there are no characters left to read
2279 * to avoid useless intermediate redraws. */
2280 if (vpeekc() == NUL)
2281 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#endif
2283 }
2284
2285returncmd:
2286
2287#ifdef FEAT_RIGHTLEFT
2288 cmdmsg_rl = FALSE;
2289#endif
2290
2291#ifdef FEAT_FKMAP
2292 cmd_fkmap = 0;
2293#endif
2294
2295 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002296 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002299 finish_incsearch_highlighting(gotesc, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
2301
2302 if (ccline.cmdbuff != NULL)
2303 {
2304 /*
2305 * Put line in history buffer (":" and "=" only when it was typed).
2306 */
2307#ifdef FEAT_CMDHIST
2308 if (ccline.cmdlen && firstc != NUL
2309 && (some_key_typed || histype == HIST_SEARCH))
2310 {
2311 add_to_history(histype, ccline.cmdbuff, TRUE,
2312 histype == HIST_SEARCH ? firstc : NUL);
2313 if (firstc == ':')
2314 {
2315 vim_free(new_last_cmdline);
2316 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2317 }
2318 }
2319#endif
2320
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002321 if (gotesc)
2322 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 }
2324
2325 /*
2326 * If the screen was shifted up, redraw the whole screen (later).
2327 * If the line is too long, clear it, so ruler and shown command do
2328 * not get printed in the middle of it.
2329 */
2330 msg_check();
2331 msg_scroll = save_msg_scroll;
2332 redir_off = FALSE;
2333
2334 /* When the command line was typed, no need for a wait-return prompt. */
2335 if (some_key_typed)
2336 need_wait_return = FALSE;
2337
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002338 /* Trigger CmdlineLeave autocommands. */
2339 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002340
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002342#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2344 im_save_status(b_im_ptr);
2345 im_set_active(FALSE);
2346#endif
2347#ifdef FEAT_MOUSE
2348 setmouse();
2349#endif
2350#ifdef CURSOR_SHAPE
2351 ui_cursor_shape(); /* may show different cursor shape */
2352#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002353 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002355 {
2356 char_u *p = ccline.cmdbuff;
2357
2358 /* Make ccline empty, getcmdline() may try to use it. */
2359 ccline.cmdbuff = NULL;
2360 return p;
2361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362}
2363
2364#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2365/*
2366 * Get a command line with a prompt.
2367 * This is prepared to be called recursively from getcmdline() (e.g. by
2368 * f_input() when evaluating an expression from CTRL-R =).
2369 * Returns the command line in allocated memory, or NULL.
2370 */
2371 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002372getcmdline_prompt(
2373 int firstc,
2374 char_u *prompt, /* command line prompt */
2375 int attr, /* attributes for prompt */
2376 int xp_context, /* type of expansion */
2377 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378{
2379 char_u *s;
2380 struct cmdline_info save_ccline;
2381 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002382 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002384 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 ccline.cmdprompt = prompt;
2386 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002387# ifdef FEAT_EVAL
2388 ccline.xp_context = xp_context;
2389 ccline.xp_arg = xp_arg;
2390 ccline.input_fn = (firstc == '@');
2391# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002392 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002394 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002395 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002396 /* Restore msg_col, the prompt from input() may have changed it.
2397 * But only if called recursively and the commandline is therefore being
2398 * restored to an old one; if not, the input() prompt stays on the screen,
2399 * so we need its modified msg_col left intact. */
2400 if (ccline.cmdbuff != NULL)
2401 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402
2403 return s;
2404}
2405#endif
2406
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002407/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002408 * Return TRUE when the text must not be changed and we can't switch to
2409 * another window or buffer. Used when editing the command line, evaluating
2410 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002411 */
2412 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002413text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002414{
2415#ifdef FEAT_CMDWIN
2416 if (cmdwin_type != 0)
2417 return TRUE;
2418#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002419 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002420}
2421
2422/*
2423 * Give an error message for a command that isn't allowed while the cmdline
2424 * window is open or editing the cmdline in another way.
2425 */
2426 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002427text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002428{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002429 EMSG(_(get_text_locked_msg()));
2430}
2431
2432 char_u *
2433get_text_locked_msg(void)
2434{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002435#ifdef FEAT_CMDWIN
2436 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002437 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002438#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002439 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002440}
2441
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002442/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002443 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2444 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002445 */
2446 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002447curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002448{
2449 if (curbuf_lock > 0)
2450 {
2451 EMSG(_("E788: Not allowed to edit another buffer now"));
2452 return TRUE;
2453 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002454 return allbuf_locked();
2455}
2456
2457/*
2458 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2459 * message.
2460 */
2461 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002462allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002463{
2464 if (allbuf_lock > 0)
2465 {
2466 EMSG(_("E811: Not allowed to change buffer information now"));
2467 return TRUE;
2468 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002469 return FALSE;
2470}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002471
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002473cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474{
2475#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2476 if (cmdline_star > 0) /* showing '*', always 1 position */
2477 return 1;
2478#endif
2479 return ptr2cells(ccline.cmdbuff + idx);
2480}
2481
2482/*
2483 * Compute the offset of the cursor on the command line for the prompt and
2484 * indent.
2485 */
2486 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002487set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002489 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 ccline.cmdspos = 1 + ccline.cmdindent;
2491 else
2492 ccline.cmdspos = 0 + ccline.cmdindent;
2493}
2494
2495/*
2496 * Compute the screen position for the cursor on the command line.
2497 */
2498 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002499set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500{
2501 int i, m, c;
2502
2503 set_cmdspos();
2504 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002507 if (m < 0) /* overflow, Columns or Rows at weird value */
2508 m = MAXCOL;
2509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 else
2511 m = MAXCOL;
2512 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2513 {
2514 c = cmdline_charsize(i);
2515#ifdef FEAT_MBYTE
2516 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2517 if (has_mbyte)
2518 correct_cmdspos(i, c);
2519#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002520 /* If the cmdline doesn't fit, show cursor on last visible char.
2521 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if ((ccline.cmdspos += c) >= m)
2523 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 ccline.cmdspos -= c;
2525 break;
2526 }
2527#ifdef FEAT_MBYTE
2528 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002529 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530#endif
2531 }
2532}
2533
2534#ifdef FEAT_MBYTE
2535/*
2536 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2537 * character that doesn't fit, so that a ">" must be displayed.
2538 */
2539 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002540correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002542 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2544 && ccline.cmdspos % Columns + cells > Columns)
2545 ccline.cmdspos++;
2546}
2547#endif
2548
2549/*
2550 * Get an Ex command line for the ":" command.
2551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002553getexline(
2554 int c, /* normally ':', NUL for ":append" */
2555 void *cookie UNUSED,
2556 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 /* When executing a register, remove ':' that's in front of each line. */
2559 if (exec_from_reg && vpeekc() == ':')
2560 (void)vgetc();
2561 return getcmdline(c, 1L, indent);
2562}
2563
2564/*
2565 * Get an Ex command line for Ex mode.
2566 * In Ex mode we only use the OS supplied line editing features and no
2567 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002568 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002571getexmodeline(
2572 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002573 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002574 void *cookie UNUSED,
2575 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002577 garray_T line_ga;
2578 char_u *pend;
2579 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002580 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002581 int escaped = FALSE; /* CTRL-V typed */
2582 int vcol = 0;
2583 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002584 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002585 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 /* Switch cursor on now. This avoids that it happens after the "\n", which
2588 * confuses the system function that computes tabstops. */
2589 cursor_on();
2590
2591 /* always start in column 0; write a newline if necessary */
2592 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002593 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002595 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002597 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002598 if (p_prompt)
2599 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 while (indent-- > 0)
2601 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 }
2604
2605 ga_init2(&line_ga, 1, 30);
2606
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002607 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002608 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002609 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002610 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002611 while (indent >= 8)
2612 {
2613 ga_append(&line_ga, TAB);
2614 msg_puts((char_u *)" ");
2615 indent -= 8;
2616 }
2617 while (indent-- > 0)
2618 {
2619 ga_append(&line_ga, ' ');
2620 msg_putchar(' ');
2621 }
2622 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002623 ++no_mapping;
2624 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002625
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 /*
2627 * Get the line, one character at a time.
2628 */
2629 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002630 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002632 long sw;
2633 char_u *s;
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (ga_grow(&line_ga, 40) == FAIL)
2636 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaarba748c82017-02-26 14:00:07 +01002638 /*
2639 * Get one character at a time.
2640 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002641 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002642
2643 /* Check for a ":normal" command and no more characters left. */
2644 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2645 c1 = '\n';
2646 else
2647 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002650 * Handle line editing.
2651 * Previously this was left to the system, putting the terminal in
2652 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002654 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002656 msg_putchar('\n');
2657 break;
2658 }
2659
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002660 if (c1 == K_PS)
2661 {
2662 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2663 goto redraw;
2664 }
2665
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002666 if (!escaped)
2667 {
2668 /* CR typed means "enter", which is NL */
2669 if (c1 == '\r')
2670 c1 = '\n';
2671
2672 if (c1 == BS || c1 == K_BS
2673 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002675 if (line_ga.ga_len > 0)
2676 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002677#ifdef FEAT_MBYTE
2678 if (has_mbyte)
2679 {
2680 p = (char_u *)line_ga.ga_data;
2681 p[line_ga.ga_len] = NUL;
2682 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2683 line_ga.ga_len -= len;
2684 }
2685 else
2686#endif
2687 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002688 goto redraw;
2689 }
2690 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002695 msg_col = startcol;
2696 msg_clr_eos();
2697 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002698 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002699 }
2700
2701 if (c1 == Ctrl_T)
2702 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002703 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 p = (char_u *)line_ga.ga_data;
2705 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002706 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002707 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002709 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002711 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002712 p = (char_u *)line_ga.ga_data;
2713 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002714 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2715 *s = ' ';
2716 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718redraw:
2719 /* redraw the line */
2720 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002721 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002722 p = (char_u *)line_ga.ga_data;
2723 p[line_ga.ga_len] = NUL;
2724 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002726 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 msg_putchar(' ');
2731 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002732 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002734 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002736 len = MB_PTR2LEN(p);
2737 msg_outtrans_len(p, len);
2738 vcol += ptr2cells(p);
2739 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002743 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 continue;
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002747 if (c1 == Ctrl_D)
2748 {
2749 /* Delete one shiftwidth. */
2750 p = (char_u *)line_ga.ga_data;
2751 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 if (prev_char == '^')
2754 ex_keep_indent = TRUE;
2755 indent = 0;
2756 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 else
2759 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002761 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002762 if (indent > 0)
2763 {
2764 --indent;
2765 indent -= indent % get_sw_value(curbuf);
2766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002768 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002769 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002770 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002771 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2772 --line_ga.ga_len;
2773 }
2774 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776
2777 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2778 {
2779 escaped = TRUE;
2780 continue;
2781 }
2782
2783 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2784 if (IS_SPECIAL(c1))
2785 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787
2788 if (IS_SPECIAL(c1))
2789 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002790#ifdef FEAT_MBYTE
2791 if (has_mbyte)
2792 len = (*mb_char2bytes)(c1,
2793 (char_u *)line_ga.ga_data + line_ga.ga_len);
2794 else
2795#endif
2796 {
2797 len = 1;
2798 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2799 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 if (c1 == '\n')
2801 msg_putchar('\n');
2802 else if (c1 == TAB)
2803 {
2804 /* Don't use chartabsize(), 'ts' can be different */
2805 do
2806 {
2807 msg_putchar(' ');
2808 } while (++vcol % 8);
2809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002813 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002814 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002816 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 escaped = FALSE;
2818
2819 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002820 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002821
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002822 /* We are done when a NL is entered, but not when it comes after an
2823 * odd number of backslashes, that results in a NUL. */
2824 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002826 int bcount = 0;
2827
2828 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2829 ++bcount;
2830
2831 if (bcount > 0)
2832 {
2833 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2834 * "\NL", etc. */
2835 line_ga.ga_len -= (bcount + 1) / 2;
2836 pend -= (bcount + 1) / 2;
2837 pend[-1] = '\n';
2838 }
2839
2840 if ((bcount & 1) == 0)
2841 {
2842 --line_ga.ga_len;
2843 --pend;
2844 *pend = NUL;
2845 break;
2846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 }
2849
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 --no_mapping;
2851 --allow_keys;
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 /* make following messages go to the next line */
2854 msg_didout = FALSE;
2855 msg_col = 0;
2856 if (msg_row < Rows - 1)
2857 ++msg_row;
2858 emsg_on_display = FALSE; /* don't want ui_delay() */
2859
2860 if (got_int)
2861 ga_clear(&line_ga);
2862
2863 return (char_u *)line_ga.ga_data;
2864}
2865
Bram Moolenaare344bea2005-09-01 20:46:49 +00002866# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2867 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868/*
2869 * Return TRUE if ccline.overstrike is on.
2870 */
2871 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002872cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873{
2874 return ccline.overstrike;
2875}
2876
2877/*
2878 * Return TRUE if the cursor is at the end of the cmdline.
2879 */
2880 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002881cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 return (ccline.cmdpos >= ccline.cmdlen);
2884}
2885#endif
2886
Bram Moolenaar9372a112005-12-06 19:59:18 +00002887#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888/*
2889 * Return the virtual column number at the current cursor position.
2890 * This is used by the IM code to obtain the start of the preedit string.
2891 */
2892 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002893cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894{
2895 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2896 return MAXCOL;
2897
2898# ifdef FEAT_MBYTE
2899 if (has_mbyte)
2900 {
2901 colnr_T col;
2902 int i = 0;
2903
2904 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002905 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
2907 return col;
2908 }
2909 else
2910# endif
2911 return ccline.cmdpos;
2912}
2913#endif
2914
2915#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2916/*
2917 * If part of the command line is an IM preedit string, redraw it with
2918 * IM feedback attributes. The cursor position is restored after drawing.
2919 */
2920 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002921redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
2923 if ((State & CMDLINE)
2924 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002925 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 && !p_imdisable
2927 && im_is_preediting())
2928 {
2929 int cmdpos = 0;
2930 int cmdspos;
2931 int old_row;
2932 int old_col;
2933 colnr_T col;
2934
2935 old_row = msg_row;
2936 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002937 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939# ifdef FEAT_MBYTE
2940 if (has_mbyte)
2941 {
2942 for (col = 0; col < preedit_start_col
2943 && cmdpos < ccline.cmdlen; ++col)
2944 {
2945 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002946 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
2948 }
2949 else
2950# endif
2951 {
2952 cmdspos += preedit_start_col;
2953 cmdpos += preedit_start_col;
2954 }
2955
2956 msg_row = cmdline_row + (cmdspos / (int)Columns);
2957 msg_col = cmdspos % (int)Columns;
2958 if (msg_row >= Rows)
2959 msg_row = Rows - 1;
2960
2961 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2962 {
2963 int char_len;
2964 int char_attr;
2965
2966 char_attr = im_get_feedback_attr(col);
2967 if (char_attr < 0)
2968 break; /* end of preedit string */
2969
2970# ifdef FEAT_MBYTE
2971 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002972 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 else
2974# endif
2975 char_len = 1;
2976
2977 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2978 cmdpos += char_len;
2979 }
2980
2981 msg_row = old_row;
2982 msg_col = old_col;
2983 }
2984}
2985#endif /* FEAT_XIM && FEAT_GUI_GTK */
2986
2987/*
2988 * Allocate a new command line buffer.
2989 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2990 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2991 */
2992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002993alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994{
2995 /*
2996 * give some extra space to avoid having to allocate all the time
2997 */
2998 if (len < 80)
2999 len = 100;
3000 else
3001 len += 20;
3002
3003 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3004 ccline.cmdbufflen = len;
3005}
3006
3007/*
3008 * Re-allocate the command line to length len + something extra.
3009 * return FAIL for failure, OK otherwise
3010 */
3011 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003012realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 char_u *p;
3015
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003016 if (len < ccline.cmdbufflen)
3017 return OK; /* no need to resize */
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 p = ccline.cmdbuff;
3020 alloc_cmdbuff(len); /* will get some more */
3021 if (ccline.cmdbuff == NULL) /* out of memory */
3022 {
3023 ccline.cmdbuff = p; /* keep the old one */
3024 return FAIL;
3025 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003026 /* There isn't always a NUL after the command, but it may need to be
3027 * there, thus copy up to the NUL and add a NUL. */
3028 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3029 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003031
3032 if (ccline.xpc != NULL
3033 && ccline.xpc->xp_pattern != NULL
3034 && ccline.xpc->xp_context != EXPAND_NOTHING
3035 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3036 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003037 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003038
3039 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3040 * to point into the newly allocated memory. */
3041 if (i >= 0 && i <= ccline.cmdlen)
3042 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3043 }
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 return OK;
3046}
3047
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003048#if defined(FEAT_ARABIC) || defined(PROTO)
3049static char_u *arshape_buf = NULL;
3050
3051# if defined(EXITFREE) || defined(PROTO)
3052 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003054{
3055 vim_free(arshape_buf);
3056}
3057# endif
3058#endif
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060/*
3061 * Draw part of the cmdline at the current cursor position. But draw stars
3062 * when cmdline_star is TRUE.
3063 */
3064 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003065draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066{
3067#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3068 int i;
3069
3070 if (cmdline_star > 0)
3071 for (i = 0; i < len; ++i)
3072 {
3073 msg_putchar('*');
3074# ifdef FEAT_MBYTE
3075 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003076 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077# endif
3078 }
3079 else
3080#endif
3081#ifdef FEAT_ARABIC
3082 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3083 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 static int buflen = 0;
3085 char_u *p;
3086 int j;
3087 int newlen = 0;
3088 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003089 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 int prev_c = 0;
3091 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003092 int u8c;
3093 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095
3096 /*
3097 * Do arabic shaping into a temporary buffer. This is very
3098 * inefficient!
3099 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003100 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 {
3102 /* Re-allocate the buffer. We keep it around to avoid a lot of
3103 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003104 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003105 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003106 arshape_buf = alloc(buflen);
3107 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 return; /* out of memory */
3109 }
3110
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003111 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3112 {
3113 /* Prepend a space to draw the leading composing char on. */
3114 arshape_buf[0] = ' ';
3115 newlen = 1;
3116 }
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 for (j = start; j < start + len; j += mb_l)
3119 {
3120 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003121 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003122 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 if (ARABIC_CHAR(u8c))
3124 {
3125 /* Do Arabic shaping. */
3126 if (cmdmsg_rl)
3127 {
3128 /* displaying from right to left */
3129 pc = prev_c;
3130 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003131 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 if (j + mb_l >= start + len)
3133 nc = NUL;
3134 else
3135 nc = utf_ptr2char(p + mb_l);
3136 }
3137 else
3138 {
3139 /* displaying from left to right */
3140 if (j + mb_l >= start + len)
3141 pc = NUL;
3142 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003143 {
3144 int pcc[MAX_MCO];
3145
3146 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003148 pc1 = pcc[0];
3149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 nc = prev_c;
3151 }
3152 prev_c = u8c;
3153
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003154 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003156 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003157 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003159 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3160 if (u8cc[1] != 0)
3161 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003162 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 }
3165 else
3166 {
3167 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003168 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 newlen += mb_l;
3170 }
3171 }
3172
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003173 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 else
3176#endif
3177 msg_outtrans_len(ccline.cmdbuff + start, len);
3178}
3179
3180/*
3181 * Put a character on the command line. Shifts the following text to the
3182 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3183 * "c" must be printable (fit in one display cell)!
3184 */
3185 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003186putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187{
3188 if (cmd_silent)
3189 return;
3190 msg_no_more = TRUE;
3191 msg_putchar(c);
3192 if (shift)
3193 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3194 msg_no_more = FALSE;
3195 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003196 extra_char = c;
3197 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198}
3199
3200/*
3201 * Undo a putcmdline(c, FALSE).
3202 */
3203 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003204unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205{
3206 if (cmd_silent)
3207 return;
3208 msg_no_more = TRUE;
3209 if (ccline.cmdlen == ccline.cmdpos)
3210 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003211#ifdef FEAT_MBYTE
3212 else if (has_mbyte)
3213 draw_cmdline(ccline.cmdpos,
3214 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 else
3217 draw_cmdline(ccline.cmdpos, 1);
3218 msg_no_more = FALSE;
3219 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003220 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221}
3222
3223/*
3224 * Put the given string, of the given length, onto the command line.
3225 * If len is -1, then STRLEN() is used to calculate the length.
3226 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3227 * part will be redrawn, otherwise it will not. If this function is called
3228 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3229 * called afterwards.
3230 */
3231 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003232put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233{
3234 int retval;
3235 int i;
3236 int m;
3237 int c;
3238
3239 if (len < 0)
3240 len = (int)STRLEN(str);
3241
3242 /* Check if ccline.cmdbuff needs to be longer */
3243 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003244 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 else
3246 retval = OK;
3247 if (retval == OK)
3248 {
3249 if (!ccline.overstrike)
3250 {
3251 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3252 ccline.cmdbuff + ccline.cmdpos,
3253 (size_t)(ccline.cmdlen - ccline.cmdpos));
3254 ccline.cmdlen += len;
3255 }
3256 else
3257 {
3258#ifdef FEAT_MBYTE
3259 if (has_mbyte)
3260 {
3261 /* Count nr of characters in the new string. */
3262 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003263 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 ++m;
3265 /* Count nr of bytes in cmdline that are overwritten by these
3266 * characters. */
3267 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003268 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 --m;
3270 if (i < ccline.cmdlen)
3271 {
3272 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3273 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3274 ccline.cmdlen += ccline.cmdpos + len - i;
3275 }
3276 else
3277 ccline.cmdlen = ccline.cmdpos + len;
3278 }
3279 else
3280#endif
3281 if (ccline.cmdpos + len > ccline.cmdlen)
3282 ccline.cmdlen = ccline.cmdpos + len;
3283 }
3284 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3285 ccline.cmdbuff[ccline.cmdlen] = NUL;
3286
3287#ifdef FEAT_MBYTE
3288 if (enc_utf8)
3289 {
3290 /* When the inserted text starts with a composing character,
3291 * backup to the character before it. There could be two of them.
3292 */
3293 i = 0;
3294 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3295 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3296 {
3297 i = (*mb_head_off)(ccline.cmdbuff,
3298 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3299 ccline.cmdpos -= i;
3300 len += i;
3301 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3302 }
3303# ifdef FEAT_ARABIC
3304 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3305 {
3306 /* Check the previous character for Arabic combining pair. */
3307 i = (*mb_head_off)(ccline.cmdbuff,
3308 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3309 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3310 + ccline.cmdpos - i), c))
3311 {
3312 ccline.cmdpos -= i;
3313 len += i;
3314 }
3315 else
3316 i = 0;
3317 }
3318# endif
3319 if (i != 0)
3320 {
3321 /* Also backup the cursor position. */
3322 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3323 ccline.cmdspos -= i;
3324 msg_col -= i;
3325 if (msg_col < 0)
3326 {
3327 msg_col += Columns;
3328 --msg_row;
3329 }
3330 }
3331 }
3332#endif
3333
3334 if (redraw && !cmd_silent)
3335 {
3336 msg_no_more = TRUE;
3337 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003338 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3340 /* Avoid clearing the rest of the line too often. */
3341 if (cmdline_row != i || ccline.overstrike)
3342 msg_clr_eos();
3343 msg_no_more = FALSE;
3344 }
3345#ifdef FEAT_FKMAP
3346 /*
3347 * If we are in Farsi command mode, the character input must be in
3348 * Insert mode. So do not advance the cmdpos.
3349 */
3350 if (!cmd_fkmap)
3351#endif
3352 {
3353 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003354 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003356 if (m < 0) /* overflow, Columns or Rows at weird value */
3357 m = MAXCOL;
3358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
3360 m = MAXCOL;
3361 for (i = 0; i < len; ++i)
3362 {
3363 c = cmdline_charsize(ccline.cmdpos);
3364#ifdef FEAT_MBYTE
3365 /* count ">" for a double-wide char that doesn't fit. */
3366 if (has_mbyte)
3367 correct_cmdspos(ccline.cmdpos, c);
3368#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003369 /* Stop cursor at the end of the screen, but do increment the
3370 * insert position, so that entering a very long command
3371 * works, even though you can't see it. */
3372 if (ccline.cmdspos + c < m)
3373 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374#ifdef FEAT_MBYTE
3375 if (has_mbyte)
3376 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003377 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (c > len - i - 1)
3379 c = len - i - 1;
3380 ccline.cmdpos += c;
3381 i += c;
3382 }
3383#endif
3384 ++ccline.cmdpos;
3385 }
3386 }
3387 }
3388 if (redraw)
3389 msg_check();
3390 return retval;
3391}
3392
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003393static struct cmdline_info prev_ccline;
3394static int prev_ccline_used = FALSE;
3395
3396/*
3397 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3398 * and overwrite it. But get_cmdline_str() may need it, thus make it
3399 * available globally in prev_ccline.
3400 */
3401 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003402save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003403{
3404 if (!prev_ccline_used)
3405 {
3406 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3407 prev_ccline_used = TRUE;
3408 }
3409 *ccp = prev_ccline;
3410 prev_ccline = ccline;
3411 ccline.cmdbuff = NULL;
3412 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003413 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003414}
3415
3416/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003417 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003418 */
3419 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003420restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003421{
3422 ccline = prev_ccline;
3423 prev_ccline = *ccp;
3424}
3425
Bram Moolenaar5a305422006-04-28 22:38:25 +00003426#if defined(FEAT_EVAL) || defined(PROTO)
3427/*
3428 * Save the command line into allocated memory. Returns a pointer to be
3429 * passed to restore_cmdline_alloc() later.
3430 * Returns NULL when failed.
3431 */
3432 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003433save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003434{
3435 struct cmdline_info *p;
3436
3437 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3438 if (p != NULL)
3439 save_cmdline(p);
3440 return (char_u *)p;
3441}
3442
3443/*
3444 * Restore the command line from the return value of save_cmdline_alloc().
3445 */
3446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003447restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003448{
3449 if (p != NULL)
3450 {
3451 restore_cmdline((struct cmdline_info *)p);
3452 vim_free(p);
3453 }
3454}
3455#endif
3456
Bram Moolenaar8299df92004-07-10 09:47:34 +00003457/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003458 * Paste a yank register into the command line.
3459 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003460 * insert_reg() can't be used here, because special characters from the
3461 * register contents will be interpreted as commands.
3462 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003463 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003464 */
3465 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003466cmdline_paste(
3467 int regname,
3468 int literally, /* Insert text literally instead of "as typed" */
3469 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003470{
3471 long i;
3472 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003473 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003474 int allocated;
3475 struct cmdline_info save_ccline;
3476
3477 /* check for valid regname; also accept special characters for CTRL-R in
3478 * the command line */
3479 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003480 && regname != Ctrl_A && regname != Ctrl_L
3481 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003482 return FAIL;
3483
3484 /* A register containing CTRL-R can cause an endless loop. Allow using
3485 * CTRL-C to break the loop. */
3486 line_breakcheck();
3487 if (got_int)
3488 return FAIL;
3489
3490#ifdef FEAT_CLIPBOARD
3491 regname = may_get_selection(regname);
3492#endif
3493
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003494 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003495 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003496 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003497 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003498 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003499 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003500 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003501
3502 if (i)
3503 {
3504 /* Got the value of a special register in "arg". */
3505 if (arg == NULL)
3506 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003507
3508 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3509 * part of the word. */
3510 p = arg;
3511 if (p_is && regname == Ctrl_W)
3512 {
3513 char_u *w;
3514 int len;
3515
3516 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003517 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003518 {
3519#ifdef FEAT_MBYTE
3520 if (has_mbyte)
3521 {
3522 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3523 if (!vim_iswordc(mb_ptr2char(w - len)))
3524 break;
3525 w -= len;
3526 }
3527 else
3528#endif
3529 {
3530 if (!vim_iswordc(w[-1]))
3531 break;
3532 --w;
3533 }
3534 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003535 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003536 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3537 p += len;
3538 }
3539
3540 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541 if (allocated)
3542 vim_free(arg);
3543 return OK;
3544 }
3545
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003546 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547}
3548
3549/*
3550 * Put a string on the command line.
3551 * When "literally" is TRUE, insert literally.
3552 * When "literally" is FALSE, insert as typed, but don't leave the command
3553 * line.
3554 */
3555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003556cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003557{
3558 int c, cv;
3559
3560 if (literally)
3561 put_on_cmdline(s, -1, TRUE);
3562 else
3563 while (*s != NUL)
3564 {
3565 cv = *s;
3566 if (cv == Ctrl_V && s[1])
3567 ++s;
3568#ifdef FEAT_MBYTE
3569 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003570 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003571 else
3572#endif
3573 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003574 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3575 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003576#ifdef UNIX
3577 || c == intr_char
3578#endif
3579 || (c == Ctrl_BSL && *s == Ctrl_N))
3580 stuffcharReadbuff(Ctrl_V);
3581 stuffcharReadbuff(c);
3582 }
3583}
3584
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585#ifdef FEAT_WILDMENU
3586/*
3587 * Delete characters on the command line, from "from" to the current
3588 * position.
3589 */
3590 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003591cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
3593 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3594 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3595 ccline.cmdlen -= ccline.cmdpos - from;
3596 ccline.cmdpos = from;
3597}
3598#endif
3599
3600/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003601 * This function is called when the screen size changes and with incremental
3602 * search and in other situations where the command line may have been
3603 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 */
3605 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003606redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003608 redrawcmdline_ex(TRUE);
3609}
3610
3611 void
3612redrawcmdline_ex(int do_compute_cmdrow)
3613{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 if (cmd_silent)
3615 return;
3616 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003617 if (do_compute_cmdrow)
3618 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 redrawcmd();
3620 cursorcmd();
3621}
3622
3623 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003624redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625{
3626 int i;
3627
3628 if (cmd_silent)
3629 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003630 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 msg_putchar(ccline.cmdfirstc);
3632 if (ccline.cmdprompt != NULL)
3633 {
3634 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3635 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3636 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003637 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 --ccline.cmdindent;
3639 }
3640 else
3641 for (i = ccline.cmdindent; i > 0; --i)
3642 msg_putchar(' ');
3643}
3644
3645/*
3646 * Redraw what is currently on the command line.
3647 */
3648 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003649redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 if (cmd_silent)
3652 return;
3653
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003654 /* when 'incsearch' is set there may be no command line while redrawing */
3655 if (ccline.cmdbuff == NULL)
3656 {
3657 windgoto(cmdline_row, 0);
3658 msg_clr_eos();
3659 return;
3660 }
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 msg_start();
3663 redrawcmdprompt();
3664
3665 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3666 msg_no_more = TRUE;
3667 draw_cmdline(0, ccline.cmdlen);
3668 msg_clr_eos();
3669 msg_no_more = FALSE;
3670
3671 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003672 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003673 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
3675 /*
3676 * An emsg() before may have set msg_scroll. This is used in normal mode,
3677 * in cmdline mode we can reset them now.
3678 */
3679 msg_scroll = FALSE; /* next message overwrites cmdline */
3680
3681 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3682 * in cmdline mode */
3683 skip_redraw = FALSE;
3684}
3685
3686 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003687compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003689 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 cmdline_row = Rows - 1;
3691 else
3692 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003693 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694}
3695
3696 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003697cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698{
3699 if (cmd_silent)
3700 return;
3701
3702#ifdef FEAT_RIGHTLEFT
3703 if (cmdmsg_rl)
3704 {
3705 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3706 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3707 if (msg_row <= 0)
3708 msg_row = Rows - 1;
3709 }
3710 else
3711#endif
3712 {
3713 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3714 msg_col = ccline.cmdspos % (int)Columns;
3715 if (msg_row >= Rows)
3716 msg_row = Rows - 1;
3717 }
3718
3719 windgoto(msg_row, msg_col);
3720#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003721 if (p_imst == IM_ON_THE_SPOT)
3722 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723#endif
3724#ifdef MCH_CURSOR_SHAPE
3725 mch_update_cursor();
3726#endif
3727}
3728
3729 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003730gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731{
3732 msg_start();
3733#ifdef FEAT_RIGHTLEFT
3734 if (cmdmsg_rl)
3735 msg_col = Columns - 1;
3736 else
3737#endif
3738 msg_col = 0; /* always start in column 0 */
3739 if (clr) /* clear the bottom line(s) */
3740 msg_clr_eos(); /* will reset clear_cmdline */
3741 windgoto(cmdline_row, 0);
3742}
3743
3744/*
3745 * Check the word in front of the cursor for an abbreviation.
3746 * Called when the non-id character "c" has been entered.
3747 * When an abbreviation is recognized it is removed from the text with
3748 * backspaces and the replacement string is inserted, followed by "c".
3749 */
3750 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003751ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003753 int spos = 0;
3754
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3756 return FALSE;
3757
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003758 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3759 * Actually accepts any mark. */
3760 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3761 spos++;
3762 if (ccline.cmdlen - spos > 5
3763 && ccline.cmdbuff[spos] == '\''
3764 && ccline.cmdbuff[spos + 2] == ','
3765 && ccline.cmdbuff[spos + 3] == '\'')
3766 spos += 5;
3767 else
3768 /* check abbreviation from the beginning of the commandline */
3769 spos = 0;
3770
3771 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772}
3773
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003774#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3775 static int
3776#ifdef __BORLANDC__
3777_RTLENTRYF
3778#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003779sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003780{
3781 char_u *p1 = *(char_u **)s1;
3782 char_u *p2 = *(char_u **)s2;
3783
3784 if (*p1 != '<' && *p2 == '<') return -1;
3785 if (*p1 == '<' && *p2 != '<') return 1;
3786 return STRCMP(p1, p2);
3787}
3788#endif
3789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790/*
3791 * Return FAIL if this is not an appropriate context in which to do
3792 * completion of anything, return OK if it is (even if there are no matches).
3793 * For the caller, this means that the character is just passed through like a
3794 * normal character (instead of being expanded). This allows :s/^I^D etc.
3795 */
3796 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003797nextwild(
3798 expand_T *xp,
3799 int type,
3800 int options, /* extra options for ExpandOne() */
3801 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802{
3803 int i, j;
3804 char_u *p1;
3805 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 int difflen;
3807 int v;
3808
3809 if (xp->xp_numfiles == -1)
3810 {
3811 set_expand_context(xp);
3812 cmd_showtail = expand_showtail(xp);
3813 }
3814
3815 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3816 {
3817 beep_flush();
3818 return OK; /* Something illegal on command line */
3819 }
3820 if (xp->xp_context == EXPAND_NOTHING)
3821 {
3822 /* Caller can use the character as a normal char instead */
3823 return FAIL;
3824 }
3825
3826 MSG_PUTS("..."); /* show that we are busy */
3827 out_flush();
3828
3829 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003830 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 if (type == WILD_NEXT || type == WILD_PREV)
3833 {
3834 /*
3835 * Get next/previous match for a previous expanded pattern.
3836 */
3837 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3838 }
3839 else
3840 {
3841 /*
3842 * Translate string into pattern and expand it.
3843 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003844 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3845 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 p2 = NULL;
3847 else
3848 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003849 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003850 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3851 if (escape)
3852 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003853
3854 if (p_wic)
3855 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003856 p2 = ExpandOne(xp, p1,
3857 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003858 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003860 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (p2 != NULL && type == WILD_LONGEST)
3862 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003863 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (ccline.cmdbuff[i + j] == '*'
3865 || ccline.cmdbuff[i + j] == '?')
3866 break;
3867 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003868 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 }
3870 }
3871 }
3872
3873 if (p2 != NULL && !got_int)
3874 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003875 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003876 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003878 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 xp->xp_pattern = ccline.cmdbuff + i;
3880 }
3881 else
3882 v = OK;
3883 if (v == OK)
3884 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003885 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3886 &ccline.cmdbuff[ccline.cmdpos],
3887 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3888 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 ccline.cmdlen += difflen;
3890 ccline.cmdpos += difflen;
3891 }
3892 }
3893 vim_free(p2);
3894
3895 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003896 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897
3898 /* When expanding a ":map" command and no matches are found, assume that
3899 * the key is supposed to be inserted literally */
3900 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3901 return FAIL;
3902
3903 if (xp->xp_numfiles <= 0 && p2 == NULL)
3904 beep_flush();
3905 else if (xp->xp_numfiles == 1)
3906 /* free expanded pattern */
3907 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3908
3909 return OK;
3910}
3911
3912/*
3913 * Do wildcard expansion on the string 'str'.
3914 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003915 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 * Return NULL for failure.
3917 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003918 * "orig" is the originally expanded string, copied to allocated memory. It
3919 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3920 * WILD_PREV "orig" should be NULL.
3921 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003922 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3923 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 *
3925 * mode = WILD_FREE: just free previously expanded matches
3926 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3927 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3928 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3929 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3930 * mode = WILD_ALL: return all matches concatenated
3931 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003932 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 *
3934 * options = WILD_LIST_NOTFOUND: list entries without a match
3935 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3936 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3937 * options = WILD_NO_BEEP: Don't beep for multiple matches
3938 * options = WILD_ADD_SLASH: add a slash after directory names
3939 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3940 * options = WILD_SILENT: don't print warning messages
3941 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003942 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 *
3944 * The variables xp->xp_context and xp->xp_backslash must have been set!
3945 */
3946 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003947ExpandOne(
3948 expand_T *xp,
3949 char_u *str,
3950 char_u *orig, /* allocated copy of original of expanded string */
3951 int options,
3952 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953{
3954 char_u *ss = NULL;
3955 static int findex;
3956 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003957 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 int i;
3959 long_u len;
3960 int non_suf_match; /* number without matching suffix */
3961
3962 /*
3963 * first handle the case of using an old match
3964 */
3965 if (mode == WILD_NEXT || mode == WILD_PREV)
3966 {
3967 if (xp->xp_numfiles > 0)
3968 {
3969 if (mode == WILD_PREV)
3970 {
3971 if (findex == -1)
3972 findex = xp->xp_numfiles;
3973 --findex;
3974 }
3975 else /* mode == WILD_NEXT */
3976 ++findex;
3977
3978 /*
3979 * When wrapping around, return the original string, set findex to
3980 * -1.
3981 */
3982 if (findex < 0)
3983 {
3984 if (orig_save == NULL)
3985 findex = xp->xp_numfiles - 1;
3986 else
3987 findex = -1;
3988 }
3989 if (findex >= xp->xp_numfiles)
3990 {
3991 if (orig_save == NULL)
3992 findex = 0;
3993 else
3994 findex = -1;
3995 }
3996#ifdef FEAT_WILDMENU
3997 if (p_wmnu)
3998 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3999 findex, cmd_showtail);
4000#endif
4001 if (findex == -1)
4002 return vim_strsave(orig_save);
4003 return vim_strsave(xp->xp_files[findex]);
4004 }
4005 else
4006 return NULL;
4007 }
4008
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004009 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4011 {
4012 FreeWild(xp->xp_numfiles, xp->xp_files);
4013 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004014 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016 findex = 0;
4017
4018 if (mode == WILD_FREE) /* only release file name */
4019 return NULL;
4020
4021 if (xp->xp_numfiles == -1)
4022 {
4023 vim_free(orig_save);
4024 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004025 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 /*
4028 * Do the expansion.
4029 */
4030 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4031 options) == FAIL)
4032 {
4033#ifdef FNAME_ILLEGAL
4034 /* Illegal file name has been silently skipped. But when there
4035 * are wildcards, the real problem is that there was no match,
4036 * causing the pattern to be added, which has illegal characters.
4037 */
4038 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4039 EMSG2(_(e_nomatch2), str);
4040#endif
4041 }
4042 else if (xp->xp_numfiles == 0)
4043 {
4044 if (!(options & WILD_SILENT))
4045 EMSG2(_(e_nomatch2), str);
4046 }
4047 else
4048 {
4049 /* Escape the matches for use on the command line. */
4050 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4051
4052 /*
4053 * Check for matching suffixes in file names.
4054 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004055 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4056 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
4058 if (xp->xp_numfiles)
4059 non_suf_match = xp->xp_numfiles;
4060 else
4061 non_suf_match = 1;
4062 if ((xp->xp_context == EXPAND_FILES
4063 || xp->xp_context == EXPAND_DIRECTORIES)
4064 && xp->xp_numfiles > 1)
4065 {
4066 /*
4067 * More than one match; check suffix.
4068 * The files will have been sorted on matching suffix in
4069 * expand_wildcards, only need to check the first two.
4070 */
4071 non_suf_match = 0;
4072 for (i = 0; i < 2; ++i)
4073 if (match_suffix(xp->xp_files[i]))
4074 ++non_suf_match;
4075 }
4076 if (non_suf_match != 1)
4077 {
4078 /* Can we ever get here unless it's while expanding
4079 * interactively? If not, we can get rid of this all
4080 * together. Don't really want to wait for this message
4081 * (and possibly have to hit return to continue!).
4082 */
4083 if (!(options & WILD_SILENT))
4084 EMSG(_(e_toomany));
4085 else if (!(options & WILD_NO_BEEP))
4086 beep_flush();
4087 }
4088 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4089 ss = vim_strsave(xp->xp_files[0]);
4090 }
4091 }
4092 }
4093
4094 /* Find longest common part */
4095 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4096 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004097 int mb_len = 1;
4098 int c0, ci;
4099
4100 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004102#ifdef FEAT_MBYTE
4103 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004105 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4106 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4107 }
4108 else
4109#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004110 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004111 for (i = 1; i < xp->xp_numfiles; ++i)
4112 {
4113#ifdef FEAT_MBYTE
4114 if (has_mbyte)
4115 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4116 else
4117#endif
4118 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004119 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004121 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004122 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004124 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 break;
4126 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004127 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 break;
4129 }
4130 if (i < xp->xp_numfiles)
4131 {
4132 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004133 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 break;
4135 }
4136 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004137
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 ss = alloc((unsigned)len + 1);
4139 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004140 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 findex = -1; /* next p_wc gets first one */
4142 }
4143
4144 /* Concatenate all matching names */
4145 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4146 {
4147 len = 0;
4148 for (i = 0; i < xp->xp_numfiles; ++i)
4149 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4150 ss = lalloc(len, TRUE);
4151 if (ss != NULL)
4152 {
4153 *ss = NUL;
4154 for (i = 0; i < xp->xp_numfiles; ++i)
4155 {
4156 STRCAT(ss, xp->xp_files[i]);
4157 if (i != xp->xp_numfiles - 1)
4158 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4159 }
4160 }
4161 }
4162
4163 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4164 ExpandCleanup(xp);
4165
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004166 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004167 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004168 vim_free(orig);
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 return ss;
4171}
4172
4173/*
4174 * Prepare an expand structure for use.
4175 */
4176 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004177ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004179 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004180 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004182#ifndef BACKSLASH_IN_FILENAME
4183 xp->xp_shell = FALSE;
4184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 xp->xp_numfiles = -1;
4186 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004187#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4188 xp->xp_arg = NULL;
4189#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004190 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191}
4192
4193/*
4194 * Cleanup an expand structure after use.
4195 */
4196 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004197ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198{
4199 if (xp->xp_numfiles >= 0)
4200 {
4201 FreeWild(xp->xp_numfiles, xp->xp_files);
4202 xp->xp_numfiles = -1;
4203 }
4204}
4205
4206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004207ExpandEscape(
4208 expand_T *xp,
4209 char_u *str,
4210 int numfiles,
4211 char_u **files,
4212 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int i;
4215 char_u *p;
4216
4217 /*
4218 * May change home directory back to "~"
4219 */
4220 if (options & WILD_HOME_REPLACE)
4221 tilde_replace(str, numfiles, files);
4222
4223 if (options & WILD_ESCAPE)
4224 {
4225 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004226 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004227 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 || xp->xp_context == EXPAND_BUFFERS
4229 || xp->xp_context == EXPAND_DIRECTORIES)
4230 {
4231 /*
4232 * Insert a backslash into a file name before a space, \, %, #
4233 * and wildmatch characters, except '~'.
4234 */
4235 for (i = 0; i < numfiles; ++i)
4236 {
4237 /* for ":set path=" we need to escape spaces twice */
4238 if (xp->xp_backslash == XP_BS_THREE)
4239 {
4240 p = vim_strsave_escaped(files[i], (char_u *)" ");
4241 if (p != NULL)
4242 {
4243 vim_free(files[i]);
4244 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004245#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 p = vim_strsave_escaped(files[i], (char_u *)" ");
4247 if (p != NULL)
4248 {
4249 vim_free(files[i]);
4250 files[i] = p;
4251 }
4252#endif
4253 }
4254 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004255#ifdef BACKSLASH_IN_FILENAME
4256 p = vim_strsave_fnameescape(files[i], FALSE);
4257#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004258 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004259#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 if (p != NULL)
4261 {
4262 vim_free(files[i]);
4263 files[i] = p;
4264 }
4265
4266 /* If 'str' starts with "\~", replace "~" at start of
4267 * files[i] with "\~". */
4268 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004269 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 }
4271 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004272
4273 /* If the first file starts with a '+' escape it. Otherwise it
4274 * could be seen as "+cmd". */
4275 if (*files[0] == '+')
4276 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 }
4278 else if (xp->xp_context == EXPAND_TAGS)
4279 {
4280 /*
4281 * Insert a backslash before characters in a tag name that
4282 * would terminate the ":tag" command.
4283 */
4284 for (i = 0; i < numfiles; ++i)
4285 {
4286 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4287 if (p != NULL)
4288 {
4289 vim_free(files[i]);
4290 files[i] = p;
4291 }
4292 }
4293 }
4294 }
4295}
4296
4297/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004298 * Escape special characters in "fname" for when used as a file name argument
4299 * after a Vim command, or, when "shell" is non-zero, a shell command.
4300 * Returns the result in allocated memory.
4301 */
4302 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004303vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004304{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004305 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004306#ifdef BACKSLASH_IN_FILENAME
4307 char_u buf[20];
4308 int j = 0;
4309
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004310 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004311 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004312 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004313 buf[j++] = *p;
4314 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004315 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004316#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004317 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4318 if (shell && csh_like_shell() && p != NULL)
4319 {
4320 char_u *s;
4321
4322 /* For csh and similar shells need to put two backslashes before '!'.
4323 * One is taken by Vim, one by the shell. */
4324 s = vim_strsave_escaped(p, (char_u *)"!");
4325 vim_free(p);
4326 p = s;
4327 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004328#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004329
4330 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4331 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004332 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004333 escape_fname(&p);
4334
4335 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004336}
4337
4338/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004339 * Put a backslash before the file name in "pp", which is in allocated memory.
4340 */
4341 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004342escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004343{
4344 char_u *p;
4345
4346 p = alloc((unsigned)(STRLEN(*pp) + 2));
4347 if (p != NULL)
4348 {
4349 p[0] = '\\';
4350 STRCPY(p + 1, *pp);
4351 vim_free(*pp);
4352 *pp = p;
4353 }
4354}
4355
4356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 * For each file name in files[num_files]:
4358 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4359 */
4360 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004361tilde_replace(
4362 char_u *orig_pat,
4363 int num_files,
4364 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365{
4366 int i;
4367 char_u *p;
4368
4369 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4370 {
4371 for (i = 0; i < num_files; ++i)
4372 {
4373 p = home_replace_save(NULL, files[i]);
4374 if (p != NULL)
4375 {
4376 vim_free(files[i]);
4377 files[i] = p;
4378 }
4379 }
4380 }
4381}
4382
4383/*
4384 * Show all matches for completion on the command line.
4385 * Returns EXPAND_NOTHING when the character that triggered expansion should
4386 * be inserted like a normal character.
4387 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004389showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
4391#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4392 int num_files;
4393 char_u **files_found;
4394 int i, j, k;
4395 int maxlen;
4396 int lines;
4397 int columns;
4398 char_u *p;
4399 int lastlen;
4400 int attr;
4401 int showtail;
4402
4403 if (xp->xp_numfiles == -1)
4404 {
4405 set_expand_context(xp);
4406 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4407 &num_files, &files_found);
4408 showtail = expand_showtail(xp);
4409 if (i != EXPAND_OK)
4410 return i;
4411
4412 }
4413 else
4414 {
4415 num_files = xp->xp_numfiles;
4416 files_found = xp->xp_files;
4417 showtail = cmd_showtail;
4418 }
4419
4420#ifdef FEAT_WILDMENU
4421 if (!wildmenu)
4422 {
4423#endif
4424 msg_didany = FALSE; /* lines_left will be set */
4425 msg_start(); /* prepare for paging */
4426 msg_putchar('\n');
4427 out_flush();
4428 cmdline_row = msg_row;
4429 msg_didany = FALSE; /* lines_left will be set again */
4430 msg_start(); /* prepare for paging */
4431#ifdef FEAT_WILDMENU
4432 }
4433#endif
4434
4435 if (got_int)
4436 got_int = FALSE; /* only int. the completion, not the cmd line */
4437#ifdef FEAT_WILDMENU
4438 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004439 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#endif
4441 else
4442 {
4443 /* find the length of the longest file name */
4444 maxlen = 0;
4445 for (i = 0; i < num_files; ++i)
4446 {
4447 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004448 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 || xp->xp_context == EXPAND_BUFFERS))
4450 {
4451 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4452 j = vim_strsize(NameBuff);
4453 }
4454 else
4455 j = vim_strsize(L_SHOWFILE(i));
4456 if (j > maxlen)
4457 maxlen = j;
4458 }
4459
4460 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4461 lines = num_files;
4462 else
4463 {
4464 /* compute the number of columns and lines for the listing */
4465 maxlen += 2; /* two spaces between file names */
4466 columns = ((int)Columns + 2) / maxlen;
4467 if (columns < 1)
4468 columns = 1;
4469 lines = (num_files + columns - 1) / columns;
4470 }
4471
Bram Moolenaar8820b482017-03-16 17:23:31 +01004472 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4475 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004476 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 msg_clr_eos();
4478 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004479 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481
4482 /* list the files line by line */
4483 for (i = 0; i < lines; ++i)
4484 {
4485 lastlen = 999;
4486 for (k = i; k < num_files; k += lines)
4487 {
4488 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4489 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004490 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 p = files_found[k] + STRLEN(files_found[k]) + 1;
4492 msg_advance(maxlen + 1);
4493 msg_puts(p);
4494 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 break;
4497 }
4498 for (j = maxlen - lastlen; --j >= 0; )
4499 msg_putchar(' ');
4500 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004501 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 || xp->xp_context == EXPAND_BUFFERS)
4503 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004504 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004505 if (xp->xp_numfiles != -1)
4506 {
4507 char_u *halved_slash;
4508 char_u *exp_path;
4509
4510 /* Expansion was done before and special characters
4511 * were escaped, need to halve backslashes. Also
4512 * $HOME has been replaced with ~/. */
4513 exp_path = expand_env_save_opt(files_found[k], TRUE);
4514 halved_slash = backslash_halve_save(
4515 exp_path != NULL ? exp_path : files_found[k]);
4516 j = mch_isdir(halved_slash != NULL ? halved_slash
4517 : files_found[k]);
4518 vim_free(exp_path);
4519 vim_free(halved_slash);
4520 }
4521 else
4522 /* Expansion was done here, file names are literal. */
4523 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (showtail)
4525 p = L_SHOWFILE(k);
4526 else
4527 {
4528 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4529 TRUE);
4530 p = NameBuff;
4531 }
4532 }
4533 else
4534 {
4535 j = FALSE;
4536 p = L_SHOWFILE(k);
4537 }
4538 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4539 }
4540 if (msg_col > 0) /* when not wrapped around */
4541 {
4542 msg_clr_eos();
4543 msg_putchar('\n');
4544 }
4545 out_flush(); /* show one line at a time */
4546 if (got_int)
4547 {
4548 got_int = FALSE;
4549 break;
4550 }
4551 }
4552
4553 /*
4554 * we redraw the command below the lines that we have just listed
4555 * This is a bit tricky, but it saves a lot of screen updating.
4556 */
4557 cmdline_row = msg_row; /* will put it back later */
4558 }
4559
4560 if (xp->xp_numfiles == -1)
4561 FreeWild(num_files, files_found);
4562
4563 return EXPAND_OK;
4564}
4565
4566/*
4567 * Private gettail for showmatches() (and win_redr_status_matches()):
4568 * Find tail of file name path, but ignore trailing "/".
4569 */
4570 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004571sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572{
4573 char_u *p;
4574 char_u *t = s;
4575 int had_sep = FALSE;
4576
4577 for (p = s; *p != NUL; )
4578 {
4579 if (vim_ispathsep(*p)
4580#ifdef BACKSLASH_IN_FILENAME
4581 && !rem_backslash(p)
4582#endif
4583 )
4584 had_sep = TRUE;
4585 else if (had_sep)
4586 {
4587 t = p;
4588 had_sep = FALSE;
4589 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004590 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 return t;
4593}
4594
4595/*
4596 * Return TRUE if we only need to show the tail of completion matches.
4597 * When not completing file names or there is a wildcard in the path FALSE is
4598 * returned.
4599 */
4600 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004601expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 char_u *s;
4604 char_u *end;
4605
4606 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004607 if (xp->xp_context != EXPAND_FILES
4608 && xp->xp_context != EXPAND_SHELLCMD
4609 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 return FALSE;
4611
4612 end = gettail(xp->xp_pattern);
4613 if (end == xp->xp_pattern) /* there is no path separator */
4614 return FALSE;
4615
4616 for (s = xp->xp_pattern; s < end; s++)
4617 {
4618 /* Skip escaped wildcards. Only when the backslash is not a path
4619 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4620 if (rem_backslash(s))
4621 ++s;
4622 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4623 return FALSE;
4624 }
4625 return TRUE;
4626}
4627
4628/*
4629 * Prepare a string for expansion.
4630 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004631 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 * When expanding other names: The string will be used with regcomp(). Copy
4633 * the name into allocated memory and prepend "^".
4634 */
4635 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004636addstar(
4637 char_u *fname,
4638 int len,
4639 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640{
4641 char_u *retval;
4642 int i, j;
4643 int new_len;
4644 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004645 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004647 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004648 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004649 && context != EXPAND_SHELLCMD
4650 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 {
4652 /*
4653 * Matching will be done internally (on something other than files).
4654 * So we convert the file-matching-type wildcards into our kind for
4655 * use with vim_regcomp(). First work out how long it will be:
4656 */
4657
4658 /* For help tags the translation is done in find_help_tags().
4659 * For a tag pattern starting with "/" no translation is needed. */
4660 if (context == EXPAND_HELP
4661 || context == EXPAND_COLORS
4662 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004663 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004664 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004665 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004666 || ((context == EXPAND_TAGS_LISTFILES
4667 || context == EXPAND_TAGS)
4668 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 retval = vim_strnsave(fname, len);
4670 else
4671 {
4672 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4673 for (i = 0; i < len; i++)
4674 {
4675 if (fname[i] == '*' || fname[i] == '~')
4676 new_len++; /* '*' needs to be replaced by ".*"
4677 '~' needs to be replaced by "\~" */
4678
4679 /* Buffer names are like file names. "." should be literal */
4680 if (context == EXPAND_BUFFERS && fname[i] == '.')
4681 new_len++; /* "." becomes "\." */
4682
4683 /* Custom expansion takes care of special things, match
4684 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004685 if ((context == EXPAND_USER_DEFINED
4686 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 new_len++; /* '\' becomes "\\" */
4688 }
4689 retval = alloc(new_len);
4690 if (retval != NULL)
4691 {
4692 retval[0] = '^';
4693 j = 1;
4694 for (i = 0; i < len; i++, j++)
4695 {
4696 /* Skip backslash. But why? At least keep it for custom
4697 * expansion. */
4698 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004699 && context != EXPAND_USER_LIST
4700 && fname[i] == '\\'
4701 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 break;
4703
4704 switch (fname[i])
4705 {
4706 case '*': retval[j++] = '.';
4707 break;
4708 case '~': retval[j++] = '\\';
4709 break;
4710 case '?': retval[j] = '.';
4711 continue;
4712 case '.': if (context == EXPAND_BUFFERS)
4713 retval[j++] = '\\';
4714 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004715 case '\\': if (context == EXPAND_USER_DEFINED
4716 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 retval[j++] = '\\';
4718 break;
4719 }
4720 retval[j] = fname[i];
4721 }
4722 retval[j] = NUL;
4723 }
4724 }
4725 }
4726 else
4727 {
4728 retval = alloc(len + 4);
4729 if (retval != NULL)
4730 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004731 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732
4733 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004734 * Don't add a star to *, ~, ~user, $var or `cmd`.
4735 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 * ~ would be at the start of the file name, but not the tail.
4737 * $ could be anywhere in the tail.
4738 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004739 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 */
4741 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004742 ends_in_star = (len > 0 && retval[len - 1] == '*');
4743#ifndef BACKSLASH_IN_FILENAME
4744 for (i = len - 2; i >= 0; --i)
4745 {
4746 if (retval[i] != '\\')
4747 break;
4748 ends_in_star = !ends_in_star;
4749 }
4750#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004752 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 && vim_strchr(tail, '$') == NULL
4754 && vim_strchr(retval, '`') == NULL)
4755 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004756 else if (len > 0 && retval[len - 1] == '$')
4757 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 retval[len] = NUL;
4759 }
4760 }
4761 return retval;
4762}
4763
4764/*
4765 * Must parse the command line so far to work out what context we are in.
4766 * Completion can then be done based on that context.
4767 * This routine sets the variables:
4768 * xp->xp_pattern The start of the pattern to be expanded within
4769 * the command line (ends at the cursor).
4770 * xp->xp_context The type of thing to expand. Will be one of:
4771 *
4772 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4773 * the command line, like an unknown command. Caller
4774 * should beep.
4775 * EXPAND_NOTHING Unrecognised context for completion, use char like
4776 * a normal char, rather than for completion. eg
4777 * :s/^I/
4778 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4779 * it.
4780 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4781 * EXPAND_FILES After command with XFILE set, or after setting
4782 * with P_EXPAND set. eg :e ^I, :w>>^I
4783 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4784 * when we know only directories are of interest. eg
4785 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004786 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4788 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4789 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4790 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4791 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4792 * EXPAND_EVENTS Complete event names
4793 * EXPAND_SYNTAX Complete :syntax command arguments
4794 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4795 * EXPAND_AUGROUP Complete autocommand group names
4796 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4797 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4798 * eg :unmap a^I , :cunab x^I
4799 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4800 * eg :call sub^I
4801 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4802 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4803 * names in expressions, eg :while s^I
4804 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004805 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004808set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004810 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 if (ccline.cmdfirstc != ':'
4812#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004813 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004814 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815#endif
4816 )
4817 {
4818 xp->xp_context = EXPAND_NOTHING;
4819 return;
4820 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004821 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822}
4823
4824 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004825set_cmd_context(
4826 expand_T *xp,
4827 char_u *str, /* start of command line */
4828 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004829 int col, /* position of cursor */
4830 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831{
4832 int old_char = NUL;
4833 char_u *nextcomm;
4834
4835 /*
4836 * Avoid a UMR warning from Purify, only save the character if it has been
4837 * written before.
4838 */
4839 if (col < len)
4840 old_char = str[col];
4841 str[col] = NUL;
4842 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004843
4844#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004845 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004846 {
4847# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004848 /* pass CMD_SIZE because there is no real command */
4849 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004850# endif
4851 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004852 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004853 {
4854 xp->xp_context = ccline.xp_context;
4855 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004856# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004857 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004858# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004859 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004860 else
4861#endif
4862 while (nextcomm != NULL)
4863 nextcomm = set_one_cmd_context(xp, nextcomm);
4864
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004865 /* Store the string here so that call_user_expand_func() can get to them
4866 * easily. */
4867 xp->xp_line = str;
4868 xp->xp_col = col;
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 str[col] = old_char;
4871}
4872
4873/*
4874 * Expand the command line "str" from context "xp".
4875 * "xp" must have been set by set_cmd_context().
4876 * xp->xp_pattern points into "str", to where the text that is to be expanded
4877 * starts.
4878 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4879 * cursor.
4880 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4881 * key that triggered expansion literally.
4882 * Returns EXPAND_OK otherwise.
4883 */
4884 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004885expand_cmdline(
4886 expand_T *xp,
4887 char_u *str, /* start of command line */
4888 int col, /* position of cursor */
4889 int *matchcount, /* return: nr of matches */
4890 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891{
4892 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004893 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4896 {
4897 beep_flush();
4898 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4899 }
4900 if (xp->xp_context == EXPAND_NOTHING)
4901 {
4902 /* Caller can use the character as a normal char instead */
4903 return EXPAND_NOTHING;
4904 }
4905
4906 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004907 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4908 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 if (file_str == NULL)
4910 return EXPAND_UNSUCCESSFUL;
4911
Bram Moolenaar94950a92010-12-02 16:01:29 +01004912 if (p_wic)
4913 options += WILD_ICASE;
4914
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004916 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 *matchcount = 0;
4919 *matches = NULL;
4920 }
4921 vim_free(file_str);
4922
4923 return EXPAND_OK;
4924}
4925
4926#ifdef FEAT_MULTI_LANG
4927/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004928 * Cleanup matches for help tags:
4929 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4930 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004932static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
4937 int i, j;
4938 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004939 char_u buf[4];
4940 char_u *p = buf;
4941
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004942 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004943 {
4944 *p++ = '@';
4945 *p++ = p_hlg[0];
4946 *p++ = p_hlg[1];
4947 }
4948 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949
4950 for (i = 0; i < num_file; ++i)
4951 {
4952 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004953 if (len <= 0)
4954 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004955 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 {
4957 /* Sorting on priority means the same item in another language may
4958 * be anywhere. Search all items for a match up to the "@en". */
4959 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004960 if (j != i && (int)STRLEN(file[j]) == len + 3
4961 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 break;
4963 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004964 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 file[i][len] = NUL;
4966 }
4967 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004968
4969 if (*buf != NUL)
4970 for (i = 0; i < num_file; ++i)
4971 {
4972 len = (int)STRLEN(file[i]) - 3;
4973 if (len <= 0)
4974 continue;
4975 if (STRCMP(file[i] + len, buf) == 0)
4976 {
4977 /* remove the default language */
4978 file[i][len] = NUL;
4979 }
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981}
4982#endif
4983
4984/*
4985 * Do the expansion based on xp->xp_context and "pat".
4986 */
4987 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004988ExpandFromContext(
4989 expand_T *xp,
4990 char_u *pat,
4991 int *num_file,
4992 char_u ***file,
4993 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994{
4995#ifdef FEAT_CMDL_COMPL
4996 regmatch_T regmatch;
4997#endif
4998 int ret;
4999 int flags;
5000
5001 flags = EW_DIR; /* include directories */
5002 if (options & WILD_LIST_NOTFOUND)
5003 flags |= EW_NOTFOUND;
5004 if (options & WILD_ADD_SLASH)
5005 flags |= EW_ADDSLASH;
5006 if (options & WILD_KEEP_ALL)
5007 flags |= EW_KEEPALL;
5008 if (options & WILD_SILENT)
5009 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005010 if (options & WILD_ALLLINKS)
5011 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005013 if (xp->xp_context == EXPAND_FILES
5014 || xp->xp_context == EXPAND_DIRECTORIES
5015 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 /*
5018 * Expand file or directory names.
5019 */
5020 int free_pat = FALSE;
5021 int i;
5022
5023 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5024 * space */
5025 if (xp->xp_backslash != XP_BS_NONE)
5026 {
5027 free_pat = TRUE;
5028 pat = vim_strsave(pat);
5029 for (i = 0; pat[i]; ++i)
5030 if (pat[i] == '\\')
5031 {
5032 if (xp->xp_backslash == XP_BS_THREE
5033 && pat[i + 1] == '\\'
5034 && pat[i + 2] == '\\'
5035 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005036 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 if (xp->xp_backslash == XP_BS_ONE
5038 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005039 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 }
5042
5043 if (xp->xp_context == EXPAND_FILES)
5044 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005045 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5046 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 else
5048 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005049 if (options & WILD_ICASE)
5050 flags |= EW_ICASE;
5051
Bram Moolenaard7834d32009-12-02 16:14:36 +00005052 /* Expand wildcards, supporting %:h and the like. */
5053 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (free_pat)
5055 vim_free(pat);
5056 return ret;
5057 }
5058
5059 *file = (char_u **)"";
5060 *num_file = 0;
5061 if (xp->xp_context == EXPAND_HELP)
5062 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005063 /* With an empty argument we would get all the help tags, which is
5064 * very slow. Get matches for "help" instead. */
5065 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5066 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 {
5068#ifdef FEAT_MULTI_LANG
5069 cleanup_help_tags(*num_file, *file);
5070#endif
5071 return OK;
5072 }
5073 return FAIL;
5074 }
5075
5076#ifndef FEAT_CMDL_COMPL
5077 return FAIL;
5078#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005079 if (xp->xp_context == EXPAND_SHELLCMD)
5080 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 if (xp->xp_context == EXPAND_OLD_SETTING)
5082 return ExpandOldSetting(num_file, file);
5083 if (xp->xp_context == EXPAND_BUFFERS)
5084 return ExpandBufnames(pat, num_file, file, options);
5085 if (xp->xp_context == EXPAND_TAGS
5086 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5087 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5088 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005089 {
5090 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005091 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5092 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005095 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005096 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005097 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005098 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005099 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005100 {
5101 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005102 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005103 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005104 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005105 {
5106 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005107 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005108 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005109# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5110 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005111 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005112# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005113 if (xp->xp_context == EXPAND_PACKADD)
5114 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5117 if (regmatch.regprog == NULL)
5118 return FAIL;
5119
5120 /* set ignore-case according to p_ic, p_scs and pat */
5121 regmatch.rm_ic = ignorecase(pat);
5122
5123 if (xp->xp_context == EXPAND_SETTINGS
5124 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5125 ret = ExpandSettings(xp, &regmatch, num_file, file);
5126 else if (xp->xp_context == EXPAND_MAPPINGS)
5127 ret = ExpandMappings(&regmatch, num_file, file);
5128# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5129 else if (xp->xp_context == EXPAND_USER_DEFINED)
5130 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5131# endif
5132 else
5133 {
5134 static struct expgen
5135 {
5136 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005137 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005139 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 } tab[] =
5141 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005142 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5143 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005144 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005145 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005146#ifdef FEAT_CMDHIST
5147 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005150 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005151 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005152 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5153 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5154 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155#endif
5156#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005157 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5158 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5159 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5160 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161#endif
5162#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005163 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5164 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165#endif
5166#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005167 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005169#ifdef FEAT_PROFILE
5170 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5171#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005172 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005173 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5174 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005175#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005176 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005177#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005178#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005179 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005180#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005181#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005182 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5185 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005186 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5187 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005189 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005190 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005191 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 };
5193 int i;
5194
5195 /*
5196 * Find a context in the table and call the ExpandGeneric() with the
5197 * right function to do the expansion.
5198 */
5199 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005200 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 if (xp->xp_context == tab[i].context)
5202 {
5203 if (tab[i].ic)
5204 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005205 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005206 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208 }
5209 }
5210
Bram Moolenaar473de612013-06-08 18:19:48 +02005211 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212
5213 return ret;
5214#endif /* FEAT_CMDL_COMPL */
5215}
5216
5217#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5218/*
5219 * Expand a list of names.
5220 *
5221 * Generic function for command line completion. It calls a function to
5222 * obtain strings, one by one. The strings are matched against a regexp
5223 * program. Matching strings are copied into an array, which is returned.
5224 *
5225 * Returns OK when no problems encountered, FAIL for error (out of memory).
5226 */
5227 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005228ExpandGeneric(
5229 expand_T *xp,
5230 regmatch_T *regmatch,
5231 int *num_file,
5232 char_u ***file,
5233 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005235 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236{
5237 int i;
5238 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005239 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 char_u *str;
5241
5242 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005243 * round == 0: count the number of matching names
5244 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005246 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 {
5248 for (i = 0; ; ++i)
5249 {
5250 str = (*func)(xp, i);
5251 if (str == NULL) /* end of list */
5252 break;
5253 if (*str == NUL) /* skip empty strings */
5254 continue;
5255
5256 if (vim_regexec(regmatch, str, (colnr_T)0))
5257 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005258 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005260 if (escaped)
5261 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5262 else
5263 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 (*file)[count] = str;
5265#ifdef FEAT_MENU
5266 if (func == get_menu_names && str != NULL)
5267 {
5268 /* test for separator added by get_menu_names() */
5269 str += STRLEN(str) - 1;
5270 if (*str == '\001')
5271 *str = '.';
5272 }
5273#endif
5274 }
5275 ++count;
5276 }
5277 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005278 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 if (count == 0)
5281 return OK;
5282 *num_file = count;
5283 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5284 if (*file == NULL)
5285 {
5286 *file = (char_u **)"";
5287 return FAIL;
5288 }
5289 count = 0;
5290 }
5291 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005292
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005293 /* Sort the results. Keep menu's in the specified order. */
5294 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005295 {
5296 if (xp->xp_context == EXPAND_EXPRESSION
5297 || xp->xp_context == EXPAND_FUNCTIONS
5298 || xp->xp_context == EXPAND_USER_FUNC)
5299 /* <SNR> functions should be sorted to the end. */
5300 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5301 sort_func_compare);
5302 else
5303 sort_strings(*file, *num_file);
5304 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005305
Bram Moolenaar4f688582007-07-24 12:34:30 +00005306#ifdef FEAT_CMDL_COMPL
5307 /* Reset the variables used for special highlight names expansion, so that
5308 * they don't show up when getting normal highlight names by ID. */
5309 reset_expand_highlight();
5310#endif
5311
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 return OK;
5313}
5314
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005315/*
5316 * Complete a shell command.
5317 * Returns FAIL or OK;
5318 */
5319 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005320expand_shellcmd(
5321 char_u *filepat, /* pattern to match with command names */
5322 int *num_file, /* return: number of matches */
5323 char_u ***file, /* return: array with matches */
5324 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005325{
5326 char_u *pat;
5327 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005328 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005329 int mustfree = FALSE;
5330 garray_T ga;
5331 char_u *buf = alloc(MAXPATHL);
5332 size_t l;
5333 char_u *s, *e;
5334 int flags = flagsarg;
5335 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005336 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005337 hashtab_T found_ht;
5338 hashitem_T *hi;
5339 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005340
5341 if (buf == NULL)
5342 return FAIL;
5343
5344 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5345 * space */
5346 pat = vim_strsave(filepat);
5347 for (i = 0; pat[i]; ++i)
5348 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005349 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005350
Bram Moolenaarb5971142015-03-21 17:32:19 +01005351 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005352
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005353 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5354 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005355 path = (char_u *)".";
5356 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005357 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005358 /* For an absolute name we don't use $PATH. */
5359 if (!mch_isFullName(pat))
5360 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005361 if (path == NULL)
5362 path = (char_u *)"";
5363 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005364
5365 /*
5366 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005367 * collect them in "ga". When "." is not in $PATH also expand for the
5368 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005369 */
5370 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005371 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005372 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005373 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005374#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005375 e = vim_strchr(s, ';');
5376#else
5377 e = vim_strchr(s, ':');
5378#endif
5379 if (e == NULL)
5380 e = s + STRLEN(s);
5381
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005382 if (*s == NUL)
5383 {
5384 if (did_curdir)
5385 break;
5386 // Find directories in the current directory, path is empty.
5387 did_curdir = TRUE;
5388 flags |= EW_DIR;
5389 }
5390 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5391 {
5392 did_curdir = TRUE;
5393 flags |= EW_DIR;
5394 }
5395 else
5396 // Do not match directories inside a $PATH item.
5397 flags &= ~EW_DIR;
5398
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005399 l = e - s;
5400 if (l > MAXPATHL - 5)
5401 break;
5402 vim_strncpy(buf, s, l);
5403 add_pathsep(buf);
5404 l = STRLEN(buf);
5405 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5406
5407 /* Expand matches in one directory of $PATH. */
5408 ret = expand_wildcards(1, &buf, num_file, file, flags);
5409 if (ret == OK)
5410 {
5411 if (ga_grow(&ga, *num_file) == FAIL)
5412 FreeWild(*num_file, *file);
5413 else
5414 {
5415 for (i = 0; i < *num_file; ++i)
5416 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005417 char_u *name = (*file)[i];
5418
5419 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005420 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005421 // Check if this name was already found.
5422 hash = hash_hash(name + l);
5423 hi = hash_lookup(&found_ht, name + l, hash);
5424 if (HASHITEM_EMPTY(hi))
5425 {
5426 // Remove the path that was prepended.
5427 STRMOVE(name, name + l);
5428 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5429 hash_add_item(&found_ht, hi, name, hash);
5430 name = NULL;
5431 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005432 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005433 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005434 }
5435 vim_free(*file);
5436 }
5437 }
5438 if (*e != NUL)
5439 ++e;
5440 }
5441 *file = ga.ga_data;
5442 *num_file = ga.ga_len;
5443
5444 vim_free(buf);
5445 vim_free(pat);
5446 if (mustfree)
5447 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005448 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005449 return OK;
5450}
5451
5452
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5454/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005455 * Call "user_expand_func()" to invoke a user defined Vim script function and
5456 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005458 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005459call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005460 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005461 expand_T *xp,
5462 int *num_file,
5463 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005465 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005466 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005468 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005469 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005470 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
Bram Moolenaarb7515462013-06-29 12:58:33 +02005472 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005473 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 *num_file = 0;
5475 *file = NULL;
5476
Bram Moolenaarb7515462013-06-29 12:58:33 +02005477 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005478 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005479 keep = ccline.cmdbuff[ccline.cmdlen];
5480 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005481 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005482
Bram Moolenaarffa96842018-06-12 22:05:14 +02005483 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5484
5485 args[0].v_type = VAR_STRING;
5486 args[0].vval.v_string = pat;
5487 args[1].v_type = VAR_STRING;
5488 args[1].vval.v_string = xp->xp_line;
5489 args[2].v_type = VAR_NUMBER;
5490 args[2].vval.v_number = xp->xp_col;
5491 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005493 /* Save the cmdline, we don't know what the function may do. */
5494 save_ccline = ccline;
5495 ccline.cmdbuff = NULL;
5496 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005498
Bram Moolenaarded27a12018-08-01 19:06:03 +02005499 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005500
5501 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005503 if (ccline.cmdbuff != NULL)
5504 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005505
Bram Moolenaarffa96842018-06-12 22:05:14 +02005506 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005507 return ret;
5508}
5509
5510/*
5511 * Expand names with a function defined by the user.
5512 */
5513 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005514ExpandUserDefined(
5515 expand_T *xp,
5516 regmatch_T *regmatch,
5517 int *num_file,
5518 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005519{
5520 char_u *retstr;
5521 char_u *s;
5522 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005523 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005524 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005525 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005526
5527 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5528 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 return FAIL;
5530
5531 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005532 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 {
5534 e = vim_strchr(s, '\n');
5535 if (e == NULL)
5536 e = s + STRLEN(s);
5537 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005538 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005540 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5541 *e = keep;
5542
5543 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005545 if (ga_grow(&ga, 1) == FAIL)
5546 break;
5547 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5548 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 }
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 if (*e != NUL)
5552 ++e;
5553 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005554 vim_free(retstr);
5555 *file = ga.ga_data;
5556 *num_file = ga.ga_len;
5557 return OK;
5558}
5559
5560/*
5561 * Expand names with a list returned by a function defined by the user.
5562 */
5563 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005564ExpandUserList(
5565 expand_T *xp,
5566 int *num_file,
5567 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005568{
5569 list_T *retlist;
5570 listitem_T *li;
5571 garray_T ga;
5572
5573 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5574 if (retlist == NULL)
5575 return FAIL;
5576
5577 ga_init2(&ga, (int)sizeof(char *), 3);
5578 /* Loop over the items in the list. */
5579 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5580 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005581 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5582 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005583
5584 if (ga_grow(&ga, 1) == FAIL)
5585 break;
5586
5587 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005588 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005589 ++ga.ga_len;
5590 }
5591 list_unref(retlist);
5592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 *file = ga.ga_data;
5594 *num_file = ga.ga_len;
5595 return OK;
5596}
5597#endif
5598
5599/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005600 * Expand color scheme, compiler or filetype names.
5601 * Search from 'runtimepath':
5602 * 'runtimepath'/{dirnames}/{pat}.vim
5603 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5604 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5605 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5606 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005607 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 */
5609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ExpandRTDir(
5611 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005612 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613 int *num_file,
5614 char_u ***file,
5615 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 char_u *s;
5618 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005619 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005621 int i;
5622 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623
5624 *num_file = 0;
5625 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005626 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005627 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005629 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005631 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5632 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005634 ga_clear_strings(&ga);
5635 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005637 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005638 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005639 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005641
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005642 if (flags & DIP_START) {
5643 for (i = 0; dirnames[i] != NULL; ++i)
5644 {
5645 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5646 if (s == NULL)
5647 {
5648 ga_clear_strings(&ga);
5649 return FAIL;
5650 }
5651 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5652 globpath(p_pp, s, &ga, 0);
5653 vim_free(s);
5654 }
5655 }
5656
5657 if (flags & DIP_OPT) {
5658 for (i = 0; dirnames[i] != NULL; ++i)
5659 {
5660 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5661 if (s == NULL)
5662 {
5663 ga_clear_strings(&ga);
5664 return FAIL;
5665 }
5666 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5667 globpath(p_pp, s, &ga, 0);
5668 vim_free(s);
5669 }
5670 }
5671
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005672 for (i = 0; i < ga.ga_len; ++i)
5673 {
5674 match = ((char_u **)ga.ga_data)[i];
5675 s = match;
5676 e = s + STRLEN(s);
5677 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5678 {
5679 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005680 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005681 if (s < match || vim_ispathsep(*s))
5682 break;
5683 ++s;
5684 *e = NUL;
5685 mch_memmove(match, s, e - s + 1);
5686 }
5687 }
5688
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005689 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005690 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005691
5692 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005693 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005694 remove_duplicates(&ga);
5695
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 *file = ga.ga_data;
5697 *num_file = ga.ga_len;
5698 return OK;
5699}
5700
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005701/*
5702 * Expand loadplugin names:
5703 * 'packpath'/pack/ * /opt/{pat}
5704 */
5705 static int
5706ExpandPackAddDir(
5707 char_u *pat,
5708 int *num_file,
5709 char_u ***file)
5710{
5711 char_u *s;
5712 char_u *e;
5713 char_u *match;
5714 garray_T ga;
5715 int i;
5716 int pat_len;
5717
5718 *num_file = 0;
5719 *file = NULL;
5720 pat_len = (int)STRLEN(pat);
5721 ga_init2(&ga, (int)sizeof(char *), 10);
5722
5723 s = alloc((unsigned)(pat_len + 26));
5724 if (s == NULL)
5725 {
5726 ga_clear_strings(&ga);
5727 return FAIL;
5728 }
5729 sprintf((char *)s, "pack/*/opt/%s*", pat);
5730 globpath(p_pp, s, &ga, 0);
5731 vim_free(s);
5732
5733 for (i = 0; i < ga.ga_len; ++i)
5734 {
5735 match = ((char_u **)ga.ga_data)[i];
5736 s = gettail(match);
5737 e = s + STRLEN(s);
5738 mch_memmove(match, s, e - s + 1);
5739 }
5740
5741 if (ga.ga_len == 0)
5742 return FAIL;
5743
5744 /* Sort and remove duplicates which can happen when specifying multiple
5745 * directories in dirnames. */
5746 remove_duplicates(&ga);
5747
5748 *file = ga.ga_data;
5749 *num_file = ga.ga_len;
5750 return OK;
5751}
5752
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#endif
5754
5755#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5756/*
5757 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005758 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005760 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005761globpath(
5762 char_u *path,
5763 char_u *file,
5764 garray_T *ga,
5765 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766{
5767 expand_T xpc;
5768 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 int num_p;
5771 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772
5773 buf = alloc(MAXPATHL);
5774 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005775 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005777 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005779
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 /* Loop over all entries in {path}. */
5781 while (*path != NUL)
5782 {
5783 /* Copy one item of the path to buf[] and concatenate the file name. */
5784 copy_option_part(&path, buf, MAXPATHL, ",");
5785 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5786 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005787# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005788 /* Using the platform's path separator (\) makes vim incorrectly
5789 * treat it as an escape character, use '/' instead. */
5790 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5791 STRCAT(buf, "/");
5792# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005794# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005796 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5797 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005799 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005801 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 for (i = 0; i < num_p; ++i)
5804 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005805 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005806 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005807 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 FreeWild(num_p, p);
5812 }
5813 }
5814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815
5816 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817}
5818
5819#endif
5820
5821#if defined(FEAT_CMDHIST) || defined(PROTO)
5822
5823/*********************************
5824 * Command line history stuff *
5825 *********************************/
5826
5827/*
5828 * Translate a history character to the associated type number.
5829 */
5830 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005831hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832{
5833 if (c == ':')
5834 return HIST_CMD;
5835 if (c == '=')
5836 return HIST_EXPR;
5837 if (c == '@')
5838 return HIST_INPUT;
5839 if (c == '>')
5840 return HIST_DEBUG;
5841 return HIST_SEARCH; /* must be '?' or '/' */
5842}
5843
5844/*
5845 * Table of history names.
5846 * These names are used in :history and various hist...() functions.
5847 * It is sufficient to give the significant prefix of a history name.
5848 */
5849
5850static char *(history_names[]) =
5851{
5852 "cmd",
5853 "search",
5854 "expr",
5855 "input",
5856 "debug",
5857 NULL
5858};
5859
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005860#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5861/*
5862 * Function given to ExpandGeneric() to obtain the possible first
5863 * arguments of the ":history command.
5864 */
5865 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005866get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005867{
5868 static char_u compl[2] = { NUL, NUL };
5869 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005870 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005871 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5872
5873 if (idx < short_names_count)
5874 {
5875 compl[0] = (char_u)short_names[idx];
5876 return compl;
5877 }
5878 if (idx < short_names_count + history_name_count)
5879 return (char_u *)history_names[idx - short_names_count];
5880 if (idx == short_names_count + history_name_count)
5881 return (char_u *)"all";
5882 return NULL;
5883}
5884#endif
5885
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886/*
5887 * init_history() - Initialize the command line history.
5888 * Also used to re-allocate the history when the size changes.
5889 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005890 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005891init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892{
5893 int newlen; /* new length of history table */
5894 histentry_T *temp;
5895 int i;
5896 int j;
5897 int type;
5898
5899 /*
5900 * If size of history table changed, reallocate it
5901 */
5902 newlen = (int)p_hi;
5903 if (newlen != hislen) /* history length changed */
5904 {
5905 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5906 {
5907 if (newlen)
5908 {
5909 temp = (histentry_T *)lalloc(
5910 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5911 if (temp == NULL) /* out of memory! */
5912 {
5913 if (type == 0) /* first one: just keep the old length */
5914 {
5915 newlen = hislen;
5916 break;
5917 }
5918 /* Already changed one table, now we can only have zero
5919 * length for all tables. */
5920 newlen = 0;
5921 type = -1;
5922 continue;
5923 }
5924 }
5925 else
5926 temp = NULL;
5927 if (newlen == 0 || temp != NULL)
5928 {
5929 if (hisidx[type] < 0) /* there are no entries yet */
5930 {
5931 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005932 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 }
5934 else if (newlen > hislen) /* array becomes bigger */
5935 {
5936 for (i = 0; i <= hisidx[type]; ++i)
5937 temp[i] = history[type][i];
5938 j = i;
5939 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005940 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 for ( ; j < hislen; ++i, ++j)
5942 temp[i] = history[type][j];
5943 }
5944 else /* array becomes smaller or 0 */
5945 {
5946 j = hisidx[type];
5947 for (i = newlen - 1; ; --i)
5948 {
5949 if (i >= 0) /* copy newest entries */
5950 temp[i] = history[type][j];
5951 else /* remove older entries */
5952 vim_free(history[type][j].hisstr);
5953 if (--j < 0)
5954 j = hislen - 1;
5955 if (j == hisidx[type])
5956 break;
5957 }
5958 hisidx[type] = newlen - 1;
5959 }
5960 vim_free(history[type]);
5961 history[type] = temp;
5962 }
5963 }
5964 hislen = newlen;
5965 }
5966}
5967
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005968 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005969clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005970{
5971 hisptr->hisnum = 0;
5972 hisptr->viminfo = FALSE;
5973 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005974 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005975}
5976
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977/*
5978 * Check if command line 'str' is already in history.
5979 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5980 */
5981 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005982in_history(
5983 int type,
5984 char_u *str,
5985 int move_to_front, /* Move the entry to the front if it exists */
5986 int sep,
5987 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988{
5989 int i;
5990 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005991 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
5993 if (hisidx[type] < 0)
5994 return FALSE;
5995 i = hisidx[type];
5996 do
5997 {
5998 if (history[type][i].hisstr == NULL)
5999 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006000
6001 /* For search history, check that the separator character matches as
6002 * well. */
6003 p = history[type][i].hisstr;
6004 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006005 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006006 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 {
6008 if (!move_to_front)
6009 return TRUE;
6010 last_i = i;
6011 break;
6012 }
6013 if (--i < 0)
6014 i = hislen - 1;
6015 } while (i != hisidx[type]);
6016
6017 if (last_i >= 0)
6018 {
6019 str = history[type][i].hisstr;
6020 while (i != hisidx[type])
6021 {
6022 if (++i >= hislen)
6023 i = 0;
6024 history[type][last_i] = history[type][i];
6025 last_i = i;
6026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006028 history[type][i].viminfo = FALSE;
6029 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006030 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 return TRUE;
6032 }
6033 return FALSE;
6034}
6035
6036/*
6037 * Convert history name (from table above) to its HIST_ equivalent.
6038 * When "name" is empty, return "cmd" history.
6039 * Returns -1 for unknown history name.
6040 */
6041 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006042get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043{
6044 int i;
6045 int len = (int)STRLEN(name);
6046
6047 /* No argument: use current history. */
6048 if (len == 0)
6049 return hist_char2type(ccline.cmdfirstc);
6050
6051 for (i = 0; history_names[i] != NULL; ++i)
6052 if (STRNICMP(name, history_names[i], len) == 0)
6053 return i;
6054
6055 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6056 return hist_char2type(name[0]);
6057
6058 return -1;
6059}
6060
6061static int last_maptick = -1; /* last seen maptick */
6062
6063/*
6064 * Add the given string to the given history. If the string is already in the
6065 * history then it is moved to the front. "histype" may be one of he HIST_
6066 * values.
6067 */
6068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006069add_to_history(
6070 int histype,
6071 char_u *new_entry,
6072 int in_map, /* consider maptick when inside a mapping */
6073 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074{
6075 histentry_T *hisptr;
6076 int len;
6077
6078 if (hislen == 0) /* no history */
6079 return;
6080
Bram Moolenaara939e432013-11-09 05:30:26 +01006081 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6082 return;
6083
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084 /*
6085 * Searches inside the same mapping overwrite each other, so that only
6086 * the last line is kept. Be careful not to remove a line that was moved
6087 * down, only lines that were added.
6088 */
6089 if (histype == HIST_SEARCH && in_map)
6090 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006091 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 {
6093 /* Current line is from the same mapping, remove it */
6094 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6095 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006096 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 --hisnum[histype];
6098 if (--hisidx[HIST_SEARCH] < 0)
6099 hisidx[HIST_SEARCH] = hislen - 1;
6100 }
6101 last_maptick = -1;
6102 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006103 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104 {
6105 if (++hisidx[histype] == hislen)
6106 hisidx[histype] = 0;
6107 hisptr = &history[histype][hisidx[histype]];
6108 vim_free(hisptr->hisstr);
6109
6110 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006111 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6113 if (hisptr->hisstr != NULL)
6114 hisptr->hisstr[len + 1] = sep;
6115
6116 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006117 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006118 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 if (histype == HIST_SEARCH && in_map)
6120 last_maptick = maptick;
6121 }
6122}
6123
6124#if defined(FEAT_EVAL) || defined(PROTO)
6125
6126/*
6127 * Get identifier of newest history entry.
6128 * "histype" may be one of the HIST_ values.
6129 */
6130 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006131get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6134 || hisidx[histype] < 0)
6135 return -1;
6136
6137 return history[histype][hisidx[histype]].hisnum;
6138}
6139
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 * Calculate history index from a number:
6142 * num > 0: seen as identifying number of a history entry
6143 * num < 0: relative position in history wrt newest entry
6144 * "histype" may be one of the HIST_ values.
6145 */
6146 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006147calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 int i;
6150 histentry_T *hist;
6151 int wrapped = FALSE;
6152
6153 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6154 || (i = hisidx[histype]) < 0 || num == 0)
6155 return -1;
6156
6157 hist = history[histype];
6158 if (num > 0)
6159 {
6160 while (hist[i].hisnum > num)
6161 if (--i < 0)
6162 {
6163 if (wrapped)
6164 break;
6165 i += hislen;
6166 wrapped = TRUE;
6167 }
6168 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6169 return i;
6170 }
6171 else if (-num <= hislen)
6172 {
6173 i += num + 1;
6174 if (i < 0)
6175 i += hislen;
6176 if (hist[i].hisstr != NULL)
6177 return i;
6178 }
6179 return -1;
6180}
6181
6182/*
6183 * Get a history entry by its index.
6184 * "histype" may be one of the HIST_ values.
6185 */
6186 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006187get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188{
6189 idx = calc_hist_idx(histype, idx);
6190 if (idx >= 0)
6191 return history[histype][idx].hisstr;
6192 else
6193 return (char_u *)"";
6194}
6195
6196/*
6197 * Clear all entries of a history.
6198 * "histype" may be one of the HIST_ values.
6199 */
6200 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006201clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202{
6203 int i;
6204 histentry_T *hisptr;
6205
6206 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6207 {
6208 hisptr = history[histype];
6209 for (i = hislen; i--;)
6210 {
6211 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006212 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006213 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 }
6215 hisidx[histype] = -1; /* mark history as cleared */
6216 hisnum[histype] = 0; /* reset identifier counter */
6217 return OK;
6218 }
6219 return FAIL;
6220}
6221
6222/*
6223 * Remove all entries matching {str} from a history.
6224 * "histype" may be one of the HIST_ values.
6225 */
6226 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006227del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228{
6229 regmatch_T regmatch;
6230 histentry_T *hisptr;
6231 int idx;
6232 int i;
6233 int last;
6234 int found = FALSE;
6235
6236 regmatch.regprog = NULL;
6237 regmatch.rm_ic = FALSE; /* always match case */
6238 if (hislen != 0
6239 && histype >= 0
6240 && histype < HIST_COUNT
6241 && *str != NUL
6242 && (idx = hisidx[histype]) >= 0
6243 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6244 != NULL)
6245 {
6246 i = last = idx;
6247 do
6248 {
6249 hisptr = &history[histype][i];
6250 if (hisptr->hisstr == NULL)
6251 break;
6252 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6253 {
6254 found = TRUE;
6255 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006256 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257 }
6258 else
6259 {
6260 if (i != last)
6261 {
6262 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006263 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 }
6265 if (--last < 0)
6266 last += hislen;
6267 }
6268 if (--i < 0)
6269 i += hislen;
6270 } while (i != idx);
6271 if (history[histype][idx].hisstr == NULL)
6272 hisidx[histype] = -1;
6273 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006274 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275 return found;
6276}
6277
6278/*
6279 * Remove an indexed entry from a history.
6280 * "histype" may be one of the HIST_ values.
6281 */
6282 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006283del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006284{
6285 int i, j;
6286
6287 i = calc_hist_idx(histype, idx);
6288 if (i < 0)
6289 return FALSE;
6290 idx = hisidx[histype];
6291 vim_free(history[histype][i].hisstr);
6292
6293 /* When deleting the last added search string in a mapping, reset
6294 * last_maptick, so that the last added search string isn't deleted again.
6295 */
6296 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6297 last_maptick = -1;
6298
6299 while (i != idx)
6300 {
6301 j = (i + 1) % hislen;
6302 history[histype][i] = history[histype][j];
6303 i = j;
6304 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006305 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 if (--i < 0)
6307 i += hislen;
6308 hisidx[histype] = i;
6309 return TRUE;
6310}
6311
6312#endif /* FEAT_EVAL */
6313
6314#if defined(FEAT_CRYPT) || defined(PROTO)
6315/*
6316 * Very specific function to remove the value in ":set key=val" from the
6317 * history.
6318 */
6319 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006320remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321{
6322 char_u *p;
6323 int i;
6324
6325 i = hisidx[HIST_CMD];
6326 if (i < 0)
6327 return;
6328 p = history[HIST_CMD][i].hisstr;
6329 if (p != NULL)
6330 for ( ; *p; ++p)
6331 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6332 {
6333 p = vim_strchr(p + 3, '=');
6334 if (p == NULL)
6335 break;
6336 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006337 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (p[i] == '\\' && p[i + 1])
6339 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006340 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 --p;
6342 }
6343}
6344#endif
6345
6346#endif /* FEAT_CMDHIST */
6347
Bram Moolenaar064154c2016-03-19 22:50:43 +01006348#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006349/*
6350 * Get pointer to the command line info to use. cmdline_paste() may clear
6351 * ccline and put the previous value in prev_ccline.
6352 */
6353 static struct cmdline_info *
6354get_ccline_ptr(void)
6355{
6356 if ((State & CMDLINE) == 0)
6357 return NULL;
6358 if (ccline.cmdbuff != NULL)
6359 return &ccline;
6360 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6361 return &prev_ccline;
6362 return NULL;
6363}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006364#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006365
Bram Moolenaar064154c2016-03-19 22:50:43 +01006366#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006367/*
6368 * Get the current command line in allocated memory.
6369 * Only works when the command line is being edited.
6370 * Returns NULL when something is wrong.
6371 */
6372 char_u *
6373get_cmdline_str(void)
6374{
6375 struct cmdline_info *p = get_ccline_ptr();
6376
6377 if (p == NULL)
6378 return NULL;
6379 return vim_strnsave(p->cmdbuff, p->cmdlen);
6380}
6381
6382/*
6383 * Get the current command line position, counted in bytes.
6384 * Zero is the first position.
6385 * Only works when the command line is being edited.
6386 * Returns -1 when something is wrong.
6387 */
6388 int
6389get_cmdline_pos(void)
6390{
6391 struct cmdline_info *p = get_ccline_ptr();
6392
6393 if (p == NULL)
6394 return -1;
6395 return p->cmdpos;
6396}
6397
6398/*
6399 * Set the command line byte position to "pos". Zero is the first position.
6400 * Only works when the command line is being edited.
6401 * Returns 1 when failed, 0 when OK.
6402 */
6403 int
6404set_cmdline_pos(
6405 int pos)
6406{
6407 struct cmdline_info *p = get_ccline_ptr();
6408
6409 if (p == NULL)
6410 return 1;
6411
6412 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6413 * changed the command line. */
6414 if (pos < 0)
6415 new_cmdpos = 0;
6416 else
6417 new_cmdpos = pos;
6418 return 0;
6419}
6420#endif
6421
6422#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6423/*
6424 * Get the current command-line type.
6425 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6426 * Only works when the command line is being edited.
6427 * Returns NUL when something is wrong.
6428 */
6429 int
6430get_cmdline_type(void)
6431{
6432 struct cmdline_info *p = get_ccline_ptr();
6433
6434 if (p == NULL)
6435 return NUL;
6436 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006437 return
6438# ifdef FEAT_EVAL
6439 (p->input_fn) ? '@' :
6440# endif
6441 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006442 return p->cmdfirstc;
6443}
6444#endif
6445
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6447/*
6448 * Get indices "num1,num2" that specify a range within a list (not a range of
6449 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6450 * Returns OK if parsed successfully, otherwise FAIL.
6451 */
6452 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006453get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454{
6455 int len;
6456 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006457 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458
6459 *str = skipwhite(*str);
6460 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6461 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006462 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 *str += len;
6464 *num1 = (int)num;
6465 first = TRUE;
6466 }
6467 *str = skipwhite(*str);
6468 if (**str == ',') /* parse "to" part of range */
6469 {
6470 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006471 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (len > 0)
6473 {
6474 *num2 = (int)num;
6475 *str = skipwhite(*str + len);
6476 }
6477 else if (!first) /* no number given at all */
6478 return FAIL;
6479 }
6480 else if (first) /* only one number given */
6481 *num2 = *num1;
6482 return OK;
6483}
6484#endif
6485
6486#if defined(FEAT_CMDHIST) || defined(PROTO)
6487/*
6488 * :history command - print a history
6489 */
6490 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006491ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 histentry_T *hist;
6494 int histype1 = HIST_CMD;
6495 int histype2 = HIST_CMD;
6496 int hisidx1 = 1;
6497 int hisidx2 = -1;
6498 int idx;
6499 int i, j, k;
6500 char_u *end;
6501 char_u *arg = eap->arg;
6502
6503 if (hislen == 0)
6504 {
6505 MSG(_("'history' option is zero"));
6506 return;
6507 }
6508
6509 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6510 {
6511 end = arg;
6512 while (ASCII_ISALPHA(*end)
6513 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6514 end++;
6515 i = *end;
6516 *end = NUL;
6517 histype1 = get_histtype(arg);
6518 if (histype1 == -1)
6519 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006520 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 {
6522 histype1 = 0;
6523 histype2 = HIST_COUNT-1;
6524 }
6525 else
6526 {
6527 *end = i;
6528 EMSG(_(e_trailing));
6529 return;
6530 }
6531 }
6532 else
6533 histype2 = histype1;
6534 *end = i;
6535 }
6536 else
6537 end = arg;
6538 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6539 {
6540 EMSG(_(e_trailing));
6541 return;
6542 }
6543
6544 for (; !got_int && histype1 <= histype2; ++histype1)
6545 {
6546 STRCPY(IObuff, "\n # ");
6547 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6548 MSG_PUTS_TITLE(IObuff);
6549 idx = hisidx[histype1];
6550 hist = history[histype1];
6551 j = hisidx1;
6552 k = hisidx2;
6553 if (j < 0)
6554 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6555 if (k < 0)
6556 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6557 if (idx >= 0 && j <= k)
6558 for (i = idx + 1; !got_int; ++i)
6559 {
6560 if (i == hislen)
6561 i = 0;
6562 if (hist[i].hisstr != NULL
6563 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6564 {
6565 msg_putchar('\n');
6566 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6567 hist[i].hisnum);
6568 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6569 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006570 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 else
6572 STRCAT(IObuff, hist[i].hisstr);
6573 msg_outtrans(IObuff);
6574 out_flush();
6575 }
6576 if (i == idx)
6577 break;
6578 }
6579 }
6580}
6581#endif
6582
6583#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006584/*
6585 * Buffers for history read from a viminfo file. Only valid while reading.
6586 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006587static histentry_T *viminfo_history[HIST_COUNT] =
6588 {NULL, NULL, NULL, NULL, NULL};
6589static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6590static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591static int viminfo_add_at_front = FALSE;
6592
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006593static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595/*
6596 * Translate a history type number to the associated character.
6597 */
6598 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006599hist_type2char(
6600 int type,
6601 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602{
6603 if (type == HIST_CMD)
6604 return ':';
6605 if (type == HIST_SEARCH)
6606 {
6607 if (use_question)
6608 return '?';
6609 else
6610 return '/';
6611 }
6612 if (type == HIST_EXPR)
6613 return '=';
6614 return '@';
6615}
6616
6617/*
6618 * Prepare for reading the history from the viminfo file.
6619 * This allocates history arrays to store the read history lines.
6620 */
6621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006622prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623{
6624 int i;
6625 int num;
6626 int type;
6627 int len;
6628
6629 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006630 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 if (asklen > hislen)
6632 asklen = hislen;
6633
6634 for (type = 0; type < HIST_COUNT; ++type)
6635 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006636 /* Count the number of empty spaces in the history list. Entries read
6637 * from viminfo previously are also considered empty. If there are
6638 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006640 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 num++;
6642 len = asklen;
6643 if (num > len)
6644 len = num;
6645 if (len <= 0)
6646 viminfo_history[type] = NULL;
6647 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006648 viminfo_history[type] = (histentry_T *)lalloc(
6649 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 if (viminfo_history[type] == NULL)
6651 len = 0;
6652 viminfo_hislen[type] = len;
6653 viminfo_hisidx[type] = 0;
6654 }
6655}
6656
6657/*
6658 * Accept a line from the viminfo, store it in the history array when it's
6659 * new.
6660 */
6661 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006662read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663{
6664 int type;
6665 long_u len;
6666 char_u *val;
6667 char_u *p;
6668
6669 type = hist_char2type(virp->vir_line[0]);
6670 if (viminfo_hisidx[type] < viminfo_hislen[type])
6671 {
6672 val = viminfo_readstring(virp, 1, TRUE);
6673 if (val != NULL && *val != NUL)
6674 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006675 int sep = (*val == ' ' ? NUL : *val);
6676
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006678 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
6680 /* Need to re-allocate to append the separator byte. */
6681 len = STRLEN(val);
6682 p = lalloc(len + 2, TRUE);
6683 if (p != NULL)
6684 {
6685 if (type == HIST_SEARCH)
6686 {
6687 /* Search entry: Move the separator from the first
6688 * column to after the NUL. */
6689 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006690 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 }
6692 else
6693 {
6694 /* Not a search entry: No separator in the viminfo
6695 * file, add a NUL separator. */
6696 mch_memmove(p, val, (size_t)len + 1);
6697 p[len + 1] = NUL;
6698 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006699 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6700 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006701 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6702 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006703 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 }
6705 }
6706 }
6707 vim_free(val);
6708 }
6709 return viminfo_readline(virp);
6710}
6711
Bram Moolenaar07219f92013-04-14 23:19:36 +02006712/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006713 * Accept a new style history line from the viminfo, store it in the history
6714 * array when it's new.
6715 */
6716 void
6717handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006718 garray_T *values,
6719 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006720{
6721 int type;
6722 long_u len;
6723 char_u *val;
6724 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006725 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006726
6727 /* Check the format:
6728 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006729 if (values->ga_len < 4
6730 || vp[0].bv_type != BVAL_NR
6731 || vp[1].bv_type != BVAL_NR
6732 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6733 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006734 return;
6735
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006736 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006737 if (type >= HIST_COUNT)
6738 return;
6739 if (viminfo_hisidx[type] < viminfo_hislen[type])
6740 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006741 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006742 if (val != NULL && *val != NUL)
6743 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006744 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6745 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006746 int idx;
6747 int overwrite = FALSE;
6748
6749 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6750 {
6751 /* If lines were written by an older Vim we need to avoid
6752 * getting duplicates. See if the entry already exists. */
6753 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6754 {
6755 p = viminfo_history[type][idx].hisstr;
6756 if (STRCMP(val, p) == 0
6757 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6758 {
6759 overwrite = TRUE;
6760 break;
6761 }
6762 }
6763
6764 if (!overwrite)
6765 {
6766 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006767 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006768 p = lalloc(len + 2, TRUE);
6769 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006770 else
6771 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006772 if (p != NULL)
6773 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006774 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006775 if (!overwrite)
6776 {
6777 mch_memmove(p, val, (size_t)len + 1);
6778 /* Put the separator after the NUL. */
6779 p[len + 1] = sep;
6780 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006781 viminfo_history[type][idx].hisnum = 0;
6782 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006783 viminfo_hisidx[type]++;
6784 }
6785 }
6786 }
6787 }
6788 }
6789}
6790
6791/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006792 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006793 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006794 static void
6795concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 int idx;
6798 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006799
6800 idx = hisidx[type] + viminfo_hisidx[type];
6801 if (idx >= hislen)
6802 idx -= hislen;
6803 else if (idx < 0)
6804 idx = hislen - 1;
6805 if (viminfo_add_at_front)
6806 hisidx[type] = idx;
6807 else
6808 {
6809 if (hisidx[type] == -1)
6810 hisidx[type] = hislen - 1;
6811 do
6812 {
6813 if (history[type][idx].hisstr != NULL
6814 || history[type][idx].viminfo)
6815 break;
6816 if (++idx == hislen)
6817 idx = 0;
6818 } while (idx != hisidx[type]);
6819 if (idx != hisidx[type] && --idx < 0)
6820 idx = hislen - 1;
6821 }
6822 for (i = 0; i < viminfo_hisidx[type]; i++)
6823 {
6824 vim_free(history[type][idx].hisstr);
6825 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6826 history[type][idx].viminfo = TRUE;
6827 history[type][idx].time_set = viminfo_history[type][i].time_set;
6828 if (--idx < 0)
6829 idx = hislen - 1;
6830 }
6831 idx += 1;
6832 idx %= hislen;
6833 for (i = 0; i < viminfo_hisidx[type]; i++)
6834 {
6835 history[type][idx++].hisnum = ++hisnum[type];
6836 idx %= hislen;
6837 }
6838}
6839
6840#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6841 static int
6842#ifdef __BORLANDC__
6843_RTLENTRYF
6844#endif
6845sort_hist(const void *s1, const void *s2)
6846{
6847 histentry_T *p1 = *(histentry_T **)s1;
6848 histentry_T *p2 = *(histentry_T **)s2;
6849
6850 if (p1->time_set < p2->time_set) return -1;
6851 if (p1->time_set > p2->time_set) return 1;
6852 return 0;
6853}
6854#endif
6855
6856/*
6857 * Merge history lines from viminfo and lines typed in this Vim based on the
6858 * timestamp;
6859 */
6860 static void
6861merge_history(int type)
6862{
6863 int max_len;
6864 histentry_T **tot_hist;
6865 histentry_T *new_hist;
6866 int i;
6867 int len;
6868
6869 /* Make one long list with all entries. */
6870 max_len = hislen + viminfo_hisidx[type];
6871 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6872 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6873 if (tot_hist == NULL || new_hist == NULL)
6874 {
6875 vim_free(tot_hist);
6876 vim_free(new_hist);
6877 return;
6878 }
6879 for (i = 0; i < viminfo_hisidx[type]; i++)
6880 tot_hist[i] = &viminfo_history[type][i];
6881 len = i;
6882 for (i = 0; i < hislen; i++)
6883 if (history[type][i].hisstr != NULL)
6884 tot_hist[len++] = &history[type][i];
6885
6886 /* Sort the list on timestamp. */
6887 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6888
6889 /* Keep the newest ones. */
6890 for (i = 0; i < hislen; i++)
6891 {
6892 if (i < len)
6893 {
6894 new_hist[i] = *tot_hist[i];
6895 tot_hist[i]->hisstr = NULL;
6896 if (new_hist[i].hisnum == 0)
6897 new_hist[i].hisnum = ++hisnum[type];
6898 }
6899 else
6900 clear_hist_entry(&new_hist[i]);
6901 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006902 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006903
6904 /* Free what is not kept. */
6905 for (i = 0; i < viminfo_hisidx[type]; i++)
6906 vim_free(viminfo_history[type][i].hisstr);
6907 for (i = 0; i < hislen; i++)
6908 vim_free(history[type][i].hisstr);
6909 vim_free(history[type]);
6910 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006911 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006912}
6913
6914/*
6915 * Finish reading history lines from viminfo. Not used when writing viminfo.
6916 */
6917 void
6918finish_viminfo_history(vir_T *virp)
6919{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006921 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006922
6923 for (type = 0; type < HIST_COUNT; ++type)
6924 {
6925 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006926 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006927
6928 if (merge)
6929 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006931 concat_history(type);
6932
Bram Moolenaard23a8232018-02-10 18:45:26 +01006933 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006934 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935 }
6936}
6937
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006938/*
6939 * Write history to viminfo file in "fp".
6940 * When "merge" is TRUE merge history lines with a previously read viminfo
6941 * file, data is in viminfo_history[].
6942 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6943 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006945write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946{
6947 int i;
6948 int type;
6949 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006950 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
6952 init_history();
6953 if (hislen == 0)
6954 return;
6955 for (type = 0; type < HIST_COUNT; ++type)
6956 {
6957 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6958 if (num_saved == 0)
6959 continue;
6960 if (num_saved < 0) /* Use default */
6961 num_saved = hislen;
6962 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6963 type == HIST_CMD ? _("Command Line") :
6964 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006965 type == HIST_EXPR ? _("Expression") :
6966 type == HIST_INPUT ? _("Input Line") :
6967 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 if (num_saved > hislen)
6969 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006970
6971 /*
6972 * Merge typed and viminfo history:
6973 * round 1: history of typed commands.
6974 * round 2: history from recently read viminfo.
6975 */
6976 for (round = 1; round <= 2; ++round)
6977 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006978 if (round == 1)
6979 /* start at newest entry, somewhere in the list */
6980 i = hisidx[type];
6981 else if (viminfo_hisidx[type] > 0)
6982 /* start at newest entry, first in the list */
6983 i = 0;
6984 else
6985 /* empty list */
6986 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006987 if (i >= 0)
6988 while (num_saved > 0
6989 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006991 char_u *p;
6992 time_t timestamp;
6993 int c = NUL;
6994
6995 if (round == 1)
6996 {
6997 p = history[type][i].hisstr;
6998 timestamp = history[type][i].time_set;
6999 }
7000 else
7001 {
7002 p = viminfo_history[type] == NULL ? NULL
7003 : viminfo_history[type][i].hisstr;
7004 timestamp = viminfo_history[type] == NULL ? 0
7005 : viminfo_history[type][i].time_set;
7006 }
7007
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007008 if (p != NULL && (round == 2
7009 || !merge
7010 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007012 --num_saved;
7013 fputc(hist_type2char(type, TRUE), fp);
7014 /* For the search history: put the separator in the
7015 * second column; use a space if there isn't one. */
7016 if (type == HIST_SEARCH)
7017 {
7018 c = p[STRLEN(p) + 1];
7019 putc(c == NUL ? ' ' : c, fp);
7020 }
7021 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007022
7023 {
7024 char cbuf[NUMBUFLEN];
7025
7026 /* New style history with a bar line. Format:
7027 * |{bartype},{histtype},{timestamp},{separator},"text" */
7028 if (c == NUL)
7029 cbuf[0] = NUL;
7030 else
7031 sprintf(cbuf, "%d", c);
7032 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7033 type, (long)timestamp, cbuf);
7034 barline_writestring(fp, p, LSIZE - 20);
7035 putc('\n', fp);
7036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007038 if (round == 1)
7039 {
7040 /* Decrement index, loop around and stop when back at
7041 * the start. */
7042 if (--i < 0)
7043 i = hislen - 1;
7044 if (i == hisidx[type])
7045 break;
7046 }
7047 else
7048 {
7049 /* Increment index. Stop at the end in the while. */
7050 ++i;
7051 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007053 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007054 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007055 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007056 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007057 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007058 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
7060}
7061#endif /* FEAT_VIMINFO */
7062
7063#if defined(FEAT_FKMAP) || defined(PROTO)
7064/*
7065 * Write a character at the current cursor+offset position.
7066 * It is directly written into the command buffer block.
7067 */
7068 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007069cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7072 {
7073 EMSG(_("E198: cmd_pchar beyond the command length"));
7074 return;
7075 }
7076 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7077 ccline.cmdbuff[ccline.cmdlen] = NUL;
7078}
7079
7080 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007081cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082{
7083 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7084 {
7085 /* EMSG(_("cmd_gchar beyond the command length")); */
7086 return NUL;
7087 }
7088 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7089}
7090#endif
7091
7092#if defined(FEAT_CMDWIN) || defined(PROTO)
7093/*
7094 * Open a window on the current command line and history. Allow editing in
7095 * the window. Returns when the window is closed.
7096 * Returns:
7097 * CR if the command is to be executed
7098 * Ctrl_C if it is to be abandoned
7099 * K_IGNORE if editing continues
7100 */
7101 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007102open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007105 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007107 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 win_T *wp;
7109 int i;
7110 linenr_T lnum;
7111 int histtype;
7112 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113 int save_restart_edit = restart_edit;
7114 int save_State = State;
7115 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007116#ifdef FEAT_RIGHTLEFT
7117 int save_cmdmsg_rl = cmdmsg_rl;
7118#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007119#ifdef FEAT_FOLDING
7120 int save_KeyTyped;
7121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007122
7123 /* Can't do this recursively. Can't do it when typing a password. */
7124 if (cmdwin_type != 0
7125# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7126 || cmdline_star > 0
7127# endif
7128 )
7129 {
7130 beep_flush();
7131 return K_IGNORE;
7132 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007133 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
7135 /* Save current window sizes. */
7136 win_size_save(&winsizes);
7137
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007139 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007140
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007141 /* don't use a new tab page */
7142 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007143 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007144
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 /* Create a window for the command-line buffer. */
7146 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7147 {
7148 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007149 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 return K_IGNORE;
7151 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007152 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007155 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007156 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007159#ifdef FEAT_FOLDING
7160 curwin->w_p_fen = FALSE;
7161#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007163 curwin->w_p_rl = cmdmsg_rl;
7164 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007166 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007169 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007170 /* But don't allow switching to another buffer. */
7171 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007172
Bram Moolenaar46152342005-09-07 21:18:43 +00007173 /* Showing the prompt may have set need_wait_return, reset it. */
7174 need_wait_return = FALSE;
7175
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007176 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7178 {
7179 if (p_wc == TAB)
7180 {
7181 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7182 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7183 }
7184 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7185 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007186 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007188 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7189 * sets 'textwidth' to 78). */
7190 curbuf->b_p_tw = 0;
7191
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 /* Fill the buffer with the history. */
7193 init_history();
7194 if (hislen > 0)
7195 {
7196 i = hisidx[histtype];
7197 if (i >= 0)
7198 {
7199 lnum = 0;
7200 do
7201 {
7202 if (++i == hislen)
7203 i = 0;
7204 if (history[histtype][i].hisstr != NULL)
7205 ml_append(lnum++, history[histtype][i].hisstr,
7206 (colnr_T)0, FALSE);
7207 }
7208 while (i != hisidx[histtype]);
7209 }
7210 }
7211
7212 /* Replace the empty last line with the current command-line and put the
7213 * cursor there. */
7214 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7215 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7216 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007217 changed_line_abv_curs();
7218 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007219 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220
7221 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007222 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223
7224 /* No Ex mode here! */
7225 exmode_active = 0;
7226
7227 State = NORMAL;
7228# ifdef FEAT_MOUSE
7229 setmouse();
7230# endif
7231
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007233 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007234 if (restart_edit != 0) /* autocmd with ":startinsert" */
7235 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237 i = RedrawingDisabled;
7238 RedrawingDisabled = 0;
7239
7240 /*
7241 * Call the main loop until <CR> or CTRL-C is typed.
7242 */
7243 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007244 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245
7246 RedrawingDisabled = i;
7247
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007248# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007249 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007250# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007251
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007253 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007254
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007255# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007256 /* Restore KeyTyped in case it is modified by autocommands */
7257 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258# endif
7259
Bram Moolenaarccc18222007-05-10 18:25:20 +00007260 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007261 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 cmdwin_type = 0;
7263
7264 exmode_active = save_exmode;
7265
Bram Moolenaarf9821062008-06-20 16:31:07 +00007266 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007268 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
7270 cmdwin_result = Ctrl_C;
7271 EMSG(_("E199: Active window or buffer deleted"));
7272 }
7273 else
7274 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007275# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 /* autocmds may abort script processing */
7277 if (aborting() && cmdwin_result != K_IGNORE)
7278 cmdwin_result = Ctrl_C;
7279# endif
7280 /* Set the new command line from the cmdline buffer. */
7281 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007282 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007284 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7285
7286 if (histtype == HIST_CMD)
7287 {
7288 /* Execute the command directly. */
7289 ccline.cmdbuff = vim_strsave((char_u *)p);
7290 cmdwin_result = CAR;
7291 }
7292 else
7293 {
7294 /* First need to cancel what we were doing. */
7295 ccline.cmdbuff = NULL;
7296 stuffcharReadbuff(':');
7297 stuffReadbuff((char_u *)p);
7298 stuffcharReadbuff(CAR);
7299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 }
7301 else if (cmdwin_result == K_XF2) /* :qa typed */
7302 {
7303 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7304 cmdwin_result = CAR;
7305 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007306 else if (cmdwin_result == Ctrl_C)
7307 {
7308 /* :q or :close, don't execute any command
7309 * and don't modify the cmd window. */
7310 ccline.cmdbuff = NULL;
7311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 else
7313 ccline.cmdbuff = vim_strsave(ml_get_curline());
7314 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007315 {
7316 ccline.cmdbuff = vim_strsave((char_u *)"");
7317 ccline.cmdlen = 0;
7318 ccline.cmdbufflen = 1;
7319 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 else
7323 {
7324 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7325 ccline.cmdbufflen = ccline.cmdlen + 1;
7326 ccline.cmdpos = curwin->w_cursor.col;
7327 if (ccline.cmdpos > ccline.cmdlen)
7328 ccline.cmdpos = ccline.cmdlen;
7329 if (cmdwin_result == K_IGNORE)
7330 {
7331 set_cmdspos_cursor();
7332 redrawcmd();
7333 }
7334 }
7335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007337 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007338# ifdef FEAT_CONCEAL
7339 /* Avoid command-line window first character being concealed. */
7340 curwin->w_p_cole = 0;
7341# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007343 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 win_goto(old_curwin);
7345 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007346
7347 /* win_close() may have already wiped the buffer when 'bh' is
7348 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007349 if (bufref_valid(&bufref))
7350 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351
7352 /* Restore window sizes. */
7353 win_size_restore(&winsizes);
7354
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007355 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 }
7357
7358 ga_clear(&winsizes);
7359 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007360# ifdef FEAT_RIGHTLEFT
7361 cmdmsg_rl = save_cmdmsg_rl;
7362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 State = save_State;
7365# ifdef FEAT_MOUSE
7366 setmouse();
7367# endif
7368
7369 return cmdwin_result;
7370}
7371#endif /* FEAT_CMDWIN */
7372
7373/*
7374 * Used for commands that either take a simple command string argument, or:
7375 * cmd << endmarker
7376 * {script}
7377 * endmarker
7378 * Returns a pointer to allocated memory with {script} or NULL.
7379 */
7380 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007381script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
7383 char_u *theline;
7384 char *end_pattern = NULL;
7385 char dot[] = ".";
7386 garray_T ga;
7387
7388 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7389 return NULL;
7390
7391 ga_init2(&ga, 1, 0x400);
7392
7393 if (cmd[2] != NUL)
7394 end_pattern = (char *)skipwhite(cmd + 2);
7395 else
7396 end_pattern = dot;
7397
7398 for (;;)
7399 {
7400 theline = eap->getline(
7401#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007402 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403#endif
7404 NUL, eap->cookie, 0);
7405
7406 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007407 {
7408 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411
7412 ga_concat(&ga, theline);
7413 ga_append(&ga, '\n');
7414 vim_free(theline);
7415 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007416 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 return (char_u *)ga.ga_data;
7419}