blob: 75984806a42d690174e944d3291152e8ff037902 [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.
267 */
268 static int
269do_incsearch_highlighting(int firstc)
270{
271 return p_is && !cmd_silent && (firstc == '/' || firstc == '?');
272}
273
274/*
275 * Do 'incsearch' highlighting if desired.
276 */
277 static void
278may_do_incsearch_highlighting(
279 int firstc,
280 long count,
281 incsearch_state_T *is_state)
282{
283 int i;
284 pos_T end_pos;
285 struct cmdline_info save_ccline;
286#ifdef FEAT_RELTIME
287 proftime_T tm;
288#endif
289
290 if (!do_incsearch_highlighting(firstc))
291 return;
292
293 // If there is a character waiting, search and redraw later.
294 if (char_avail())
295 {
296 is_state->incsearch_postponed = TRUE;
297 return;
298 }
299 is_state->incsearch_postponed = FALSE;
300
301 // start at old position
302 curwin->w_cursor = is_state->search_start;
303 save_last_search_pattern();
304
305 // If there is no command line, don't do anything.
306 if (ccline.cmdlen == 0)
307 {
308 i = 0;
309 set_no_hlsearch(TRUE); // turn off previous highlight
310 redraw_all_later(SOME_VALID);
311 }
312 else
313 {
314 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
315
316 cursor_off(); // so the user knows we're busy
317 out_flush();
318 ++emsg_off; // so it doesn't beep if bad expr
319#ifdef FEAT_RELTIME
320 // Set the time limit to half a second.
321 profile_setlimit(500L, &tm);
322#endif
323 if (!p_hls)
324 search_flags += SEARCH_KEEP;
325 i = do_search(NULL, firstc, ccline.cmdbuff, count, search_flags,
326#ifdef FEAT_RELTIME
327 &tm, NULL
328#else
329 NULL, NULL
330#endif
331 );
332 --emsg_off;
333
334 // if interrupted while searching, behave like it failed
335 if (got_int)
336 {
337 (void)vpeekc(); // remove <C-C> from input stream
338 got_int = FALSE; // don't abandon the command line
339 i = 0;
340 }
341 else if (char_avail())
342 // cancelled searching because a char was typed
343 is_state->incsearch_postponed = TRUE;
344 }
345 if (i != 0)
346 highlight_match = TRUE; // highlight position
347 else
348 highlight_match = FALSE; // remove highlight
349
350 // First restore the old curwin values, so the screen is positioned in the
351 // same way as the actual search command.
352 restore_viewstate(&is_state->old_viewstate);
353 changed_cline_bef_curs();
354 update_topline();
355
356 if (i != 0)
357 {
358 pos_T save_pos = curwin->w_cursor;
359
360 is_state->match_start = curwin->w_cursor;
361 set_search_match(&curwin->w_cursor);
362 validate_cursor();
363 end_pos = curwin->w_cursor;
364 is_state->match_end = end_pos;
365 curwin->w_cursor = save_pos;
366 }
367 else
368 end_pos = curwin->w_cursor; // shutup gcc 4
369
370 // Disable 'hlsearch' highlighting if the pattern matches everything.
371 // Avoids a flash when typing "foo\|".
372 if (empty_pattern(ccline.cmdbuff))
373 set_no_hlsearch(TRUE);
374
375 validate_cursor();
376 // May redraw the status line to show the cursor position.
377 if (p_ru && curwin->w_status_height > 0)
378 curwin->w_redr_status = TRUE;
379
380 save_cmdline(&save_ccline);
381 update_screen(SOME_VALID);
382 restore_cmdline(&save_ccline);
383 restore_last_search_pattern();
384
385 // Leave it at the end to make CTRL-R CTRL-W work.
386 if (i != 0)
387 curwin->w_cursor = end_pos;
388
389 msg_starthere();
390 redrawcmdline();
391 is_state->did_incsearch = TRUE;
392}
393
394/*
395 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
396 * or previous match.
397 * Returns FAIL when jumping to cmdline_not_changed;
398 */
399 static int
400may_adjust_incsearch_highlighting(
401 int firstc,
402 long count,
403 incsearch_state_T *is_state,
404 int c)
405{
406 pos_T t;
407 char_u *pat;
408 int search_flags = SEARCH_NOOF;
409 int i;
410
411 if (!do_incsearch_highlighting(firstc))
412 return OK;
413 if (ccline.cmdlen == 0)
414 return FAIL;
415
416 if (firstc == ccline.cmdbuff[0])
417 pat = last_search_pattern();
418 else
419 pat = ccline.cmdbuff;
420
421 save_last_search_pattern();
422 cursor_off();
423 out_flush();
424 if (c == Ctrl_G)
425 {
426 t = is_state->match_end;
427 if (LT_POS(is_state->match_start, is_state->match_end))
428 // Start searching at the end of the match not at the beginning of
429 // the next column.
430 (void)decl(&t);
431 search_flags += SEARCH_COL;
432 }
433 else
434 t = is_state->match_start;
435 if (!p_hls)
436 search_flags += SEARCH_KEEP;
437 ++emsg_off;
438 i = searchit(curwin, curbuf, &t,
439 c == Ctrl_G ? FORWARD : BACKWARD,
440 pat, count, search_flags,
441 RE_SEARCH, 0, NULL, NULL);
442 --emsg_off;
443 if (i)
444 {
445 is_state->search_start = is_state->match_start;
446 is_state->match_end = t;
447 is_state->match_start = t;
448 if (c == Ctrl_T && firstc == '/')
449 {
450 // Move just before the current match, so that when nv_search
451 // finishes the cursor will be put back on the match.
452 is_state->search_start = t;
453 (void)decl(&is_state->search_start);
454 }
455 else if (c == Ctrl_G && firstc == '?')
456 {
457 // Move just after the current match, so that when nv_search
458 // finishes the cursor will be put back on the match.
459 is_state->search_start = t;
460 (void)incl(&is_state->search_start);
461 }
462 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
463 {
464 // wrap around
465 is_state->search_start = t;
466 if (firstc == '?')
467 (void)incl(&is_state->search_start);
468 else
469 (void)decl(&is_state->search_start);
470 }
471
472 set_search_match(&is_state->match_end);
473 curwin->w_cursor = is_state->match_start;
474 changed_cline_bef_curs();
475 update_topline();
476 validate_cursor();
477 highlight_match = TRUE;
478 save_viewstate(&is_state->old_viewstate);
479 update_screen(NOT_VALID);
480 redrawcmdline();
481 }
482 else
483 vim_beep(BO_ERROR);
484 restore_last_search_pattern();
485 return FAIL;
486}
487
488/*
489 * When CTRL-L typed: add character from the match to the pattern.
490 * May set "*c" to the added character.
491 * Return OK when jumping to cmdline_not_changed.
492 */
493 static int
494may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
495{
496 if (!do_incsearch_highlighting(firstc))
497 return FAIL;
498
499 // Add a character from under the cursor for 'incsearch'.
500 if (is_state->did_incsearch)
501 {
502 curwin->w_cursor = is_state->match_end;
503 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
504 {
505 *c = gchar_cursor();
506
507 // If 'ignorecase' and 'smartcase' are set and the
508 // command line has no uppercase characters, convert
509 // the character to lowercase.
510 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff))
511 *c = MB_TOLOWER(*c);
512 if (*c != NUL)
513 {
514 if (*c == firstc || vim_strchr((char_u *)(
515 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
516 {
517 // put a backslash before special characters
518 stuffcharReadbuff(*c);
519 *c = '\\';
520 }
521 return FAIL;
522 }
523 }
524 }
525 return OK;
526}
527
528 static void
529finish_incsearch_highlighting(int gotesc, incsearch_state_T *is_state)
530{
531 if (is_state->did_incsearch)
532 {
533 if (gotesc)
534 curwin->w_cursor = is_state->save_cursor;
535 else
536 {
537 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
538 {
539 // put the '" mark at the original position
540 curwin->w_cursor = is_state->save_cursor;
541 setpcmark();
542 }
543 curwin->w_cursor = is_state->search_start;
544 }
545 restore_viewstate(&is_state->old_viewstate);
546 highlight_match = FALSE;
547 validate_cursor(); /* needed for TAB */
548 redraw_all_later(SOME_VALID);
549 }
550}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200551#endif
552
Bram Moolenaar66216052017-12-16 16:33:44 +0100553/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 * getcmdline() - accept a command line starting with firstc.
555 *
556 * firstc == ':' get ":" command line.
557 * firstc == '/' or '?' get search pattern
558 * firstc == '=' get expression
559 * firstc == '@' get text for input() function
560 * firstc == '>' get text for debug mode
561 * firstc == NUL get text for :insert command
562 * firstc == -1 like NUL, and break on CTRL-C
563 *
564 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
565 * command line.
566 *
567 * Careful: getcmdline() can be called recursively!
568 *
569 * Return pointer to allocated string if there is a commandline, NULL
570 * otherwise.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100573getcmdline(
574 int firstc,
575 long count UNUSED, /* only used for incremental search */
576 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
578 int c;
579 int i;
580 int j;
581 int gotesc = FALSE; /* TRUE when <ESC> just typed */
582 int do_abbr; /* when TRUE check for abbr. */
583#ifdef FEAT_CMDHIST
584 char_u *lookfor = NULL; /* string to match */
585 int hiscnt; /* current history line in use */
586 int histype; /* history type to be used */
587#endif
588#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200589 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590#endif
591 int did_wild_list = FALSE; /* did wild_list() recently */
592 int wim_index = 0; /* index in wim_flags[] */
593 int res;
594 int save_msg_scroll = msg_scroll;
595 int save_State = State; /* remember State when called */
596 int some_key_typed = FALSE; /* one of the keys was typed */
597#ifdef FEAT_MOUSE
598 /* mouse drag and release events are ignored, unless they are
599 * preceded with a mouse down event */
600 int ignore_drag_release = TRUE;
601#endif
602#ifdef FEAT_EVAL
603 int break_ctrl_c = FALSE;
604#endif
605 expand_T xpc;
606 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000608 /* Everything that may work recursively should save and restore the
609 * current command line in save_ccline. That includes update_screen(), a
610 * custom status line may invoke ":normal". */
611 struct cmdline_info save_ccline;
612#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200613 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615#ifdef FEAT_EVAL
616 if (firstc == -1)
617 {
618 firstc = NUL;
619 break_ctrl_c = TRUE;
620 }
621#endif
622#ifdef FEAT_RIGHTLEFT
623 /* start without Hebrew mapping for a command line */
624 if (firstc == ':' || firstc == '=' || firstc == '>')
625 cmd_hkmap = 0;
626#endif
627
628 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200631 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
633
634 /*
635 * set some variables for redrawcmd()
636 */
637 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000638 ccline.cmdindent = (firstc > 0 ? indent : 0);
639
640 /* alloc initial ccline.cmdbuff */
641 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (ccline.cmdbuff == NULL)
643 return NULL; /* out of memory */
644 ccline.cmdlen = ccline.cmdpos = 0;
645 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100646 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000648 /* autoindent for :insert and :append */
649 if (firstc <= 0)
650 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200651 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000652 ccline.cmdbuff[indent] = NUL;
653 ccline.cmdpos = indent;
654 ccline.cmdspos = indent;
655 ccline.cmdlen = indent;
656 }
657
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000659 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660
661#ifdef FEAT_RIGHTLEFT
662 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
663 && (firstc == '/' || firstc == '?'))
664 cmdmsg_rl = TRUE;
665 else
666 cmdmsg_rl = FALSE;
667#endif
668
669 redir_off = TRUE; /* don't redirect the typed command */
670 if (!cmd_silent)
671 {
672 i = msg_scrolled;
673 msg_scrolled = 0; /* avoid wait_return message */
674 gotocmdline(TRUE);
675 msg_scrolled += i;
676 redrawcmdprompt(); /* draw prompt or indent */
677 set_cmdspos();
678 }
679 xpc.xp_context = EXPAND_NOTHING;
680 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000681#ifndef BACKSLASH_IN_FILENAME
682 xpc.xp_shell = FALSE;
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000685#if defined(FEAT_EVAL)
686 if (ccline.input_fn)
687 {
688 xpc.xp_context = ccline.xp_context;
689 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000690# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000691 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000692# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000693 }
694#endif
695
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 /*
697 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
698 * doing ":@0" when register 0 doesn't contain a CR.
699 */
700 msg_scroll = FALSE;
701
702 State = CMDLINE;
703
704 if (firstc == '/' || firstc == '?' || firstc == '@')
705 {
706 /* Use ":lmap" mappings for search pattern and input(). */
707 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
708 b_im_ptr = &curbuf->b_p_iminsert;
709 else
710 b_im_ptr = &curbuf->b_p_imsearch;
711 if (*b_im_ptr == B_IMODE_LMAP)
712 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100713#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 im_set_active(*b_im_ptr == B_IMODE_IM);
715#endif
716 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100717#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 else if (p_imcmdline)
719 im_set_active(TRUE);
720#endif
721
722#ifdef FEAT_MOUSE
723 setmouse();
724#endif
725#ifdef CURSOR_SHAPE
726 ui_cursor_shape(); /* may show different cursor shape */
727#endif
728
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000729 /* When inside an autocommand for writing "exiting" may be set and
730 * terminal mode set to cooked. Need to set raw mode here then. */
731 settmode(TMODE_RAW);
732
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200733 /* Trigger CmdlineEnter autocommands. */
734 cmdline_type = firstc == NUL ? '-' : firstc;
735 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#ifdef FEAT_CMDHIST
738 init_history();
739 hiscnt = hislen; /* set hiscnt to impossible history value */
740 histype = hist_char2type(firstc);
741#endif
742
743#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000744 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745#endif
746
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200747 /* If something above caused an error, reset the flags, we do want to type
748 * and execute commands. Display may be messed up a bit. */
749 if (did_emsg)
750 redrawcmd();
751 did_emsg = FALSE;
752 got_int = FALSE;
753
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 /*
755 * Collect the command string, handling editing keys.
756 */
757 for (;;)
758 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000759 redir_off = TRUE; /* Don't redirect the typed command.
760 Repeated, because a ":redir" inside
761 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762#ifdef USE_ON_FLY_SCROLL
763 dont_scroll = FALSE; /* allow scrolling here */
764#endif
765 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
766
Bram Moolenaar72532d32018-04-07 19:09:09 +0200767 did_emsg = FALSE; /* There can't really be a reason why an error
768 that occurs while typing a command should
769 cause the command not to be executed. */
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000772
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100773 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
774 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000775 do
776 {
777 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100778 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 if (KeyTyped)
781 {
782 some_key_typed = TRUE;
783#ifdef FEAT_RIGHTLEFT
784 if (cmd_hkmap)
785 c = hkmap(c);
786# ifdef FEAT_FKMAP
787 if (cmd_fkmap)
788 c = cmdl_fkmap(c);
789# endif
790 if (cmdmsg_rl && !KeyStuffed)
791 {
792 /* Invert horizontal movements and operations. Only when
793 * typed by the user directly, not when the result of a
794 * mapping. */
795 switch (c)
796 {
797 case K_RIGHT: c = K_LEFT; break;
798 case K_S_RIGHT: c = K_S_LEFT; break;
799 case K_C_RIGHT: c = K_C_LEFT; break;
800 case K_LEFT: c = K_RIGHT; break;
801 case K_S_LEFT: c = K_S_RIGHT; break;
802 case K_C_LEFT: c = K_C_RIGHT; break;
803 }
804 }
805#endif
806 }
807
808 /*
809 * Ignore got_int when CTRL-C was typed here.
810 * Don't ignore it in :global, we really need to break then, e.g., for
811 * ":g/pat/normal /pat" (without the <CR>).
812 * Don't ignore it for the input() function.
813 */
814 if ((c == Ctrl_C
815#ifdef UNIX
816 || c == intr_char
817#endif
818 )
819#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
820 && firstc != '@'
821#endif
822#ifdef FEAT_EVAL
823 && !break_ctrl_c
824#endif
825 && !global_busy)
826 got_int = FALSE;
827
828#ifdef FEAT_CMDHIST
829 /* free old command line when finished moving around in the history
830 * list */
831 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000832 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000833 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 && c != K_PAGEDOWN && c != K_PAGEUP
835 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000836 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100838 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#endif
840
841 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000842 * When there are matching completions to select <S-Tab> works like
843 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000845 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 c = Ctrl_P;
847
848#ifdef FEAT_WILDMENU
849 /* Special translations for 'wildmenu' */
850 if (did_wild_list && p_wmnu)
851 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000852 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000854 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 c = Ctrl_N;
856 }
857 /* Hitting CR after "emenu Name.": complete submenu */
858 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
859 && ccline.cmdpos > 1
860 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
861 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
862 && (c == '\n' || c == '\r' || c == K_KENTER))
863 c = K_DOWN;
864#endif
865
866 /* free expanded names when finished walking through matches */
867 if (xpc.xp_numfiles != -1
868 && !(c == p_wc && KeyTyped) && c != p_wcm
869 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
870 && c != Ctrl_L)
871 {
872 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
873 did_wild_list = FALSE;
874#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000875 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#endif
877 xpc.xp_context = EXPAND_NOTHING;
878 wim_index = 0;
879#ifdef FEAT_WILDMENU
880 if (p_wmnu && wild_menu_showing != 0)
881 {
882 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000883 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000884
885 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000886 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
888 if (wild_menu_showing == WM_SCROLLED)
889 {
890 /* Entered command line, move it up */
891 cmdline_row--;
892 redrawcmd();
893 }
894 else if (save_p_ls != -1)
895 {
896 /* restore 'laststatus' and 'winminheight' */
897 p_ls = save_p_ls;
898 p_wmh = save_p_wmh;
899 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000900 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000902 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 redrawcmd();
904 save_p_ls = -1;
905 }
906 else
907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 redraw_statuslines();
910 }
911 KeyTyped = skt;
912 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +0000913 if (ccline.input_fn)
914 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 }
916#endif
917 }
918
919#ifdef FEAT_WILDMENU
920 /* Special translations for 'wildmenu' */
921 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
922 {
923 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000924 if (c == K_DOWN && ccline.cmdpos > 0
925 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000927 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 {
929 /* Hitting <Up>: Remove one submenu name in front of the
930 * cursor */
931 int found = FALSE;
932
933 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
934 i = 0;
935 while (--j > 0)
936 {
937 /* check for start of menu name */
938 if (ccline.cmdbuff[j] == ' '
939 && ccline.cmdbuff[j - 1] != '\\')
940 {
941 i = j + 1;
942 break;
943 }
944 /* check for start of submenu name */
945 if (ccline.cmdbuff[j] == '.'
946 && ccline.cmdbuff[j - 1] != '\\')
947 {
948 if (found)
949 {
950 i = j + 1;
951 break;
952 }
953 else
954 found = TRUE;
955 }
956 }
957 if (i > 0)
958 cmdline_del(i);
959 c = p_wc;
960 xpc.xp_context = EXPAND_NOTHING;
961 }
962 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000963 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +0000964 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000965 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 {
967 char_u upseg[5];
968
969 upseg[0] = PATHSEP;
970 upseg[1] = '.';
971 upseg[2] = '.';
972 upseg[3] = PATHSEP;
973 upseg[4] = NUL;
974
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +0000975 if (c == K_DOWN
976 && ccline.cmdpos > 0
977 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
978 && (ccline.cmdpos < 3
979 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
981 {
982 /* go down a directory */
983 c = p_wc;
984 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000985 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 {
987 /* If in a direct ancestor, strip off one ../ to go down */
988 int found = FALSE;
989
990 j = ccline.cmdpos;
991 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
992 while (--j > i)
993 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000994#ifdef FEAT_MBYTE
995 if (has_mbyte)
996 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 if (vim_ispathsep(ccline.cmdbuff[j]))
999 {
1000 found = TRUE;
1001 break;
1002 }
1003 }
1004 if (found
1005 && ccline.cmdbuff[j - 1] == '.'
1006 && ccline.cmdbuff[j - 2] == '.'
1007 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1008 {
1009 cmdline_del(j - 2);
1010 c = p_wc;
1011 }
1012 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001013 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 {
1015 /* go up a directory */
1016 int found = FALSE;
1017
1018 j = ccline.cmdpos - 1;
1019 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1020 while (--j > i)
1021 {
1022#ifdef FEAT_MBYTE
1023 if (has_mbyte)
1024 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1025#endif
1026 if (vim_ispathsep(ccline.cmdbuff[j])
1027#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001028 && vim_strchr((char_u *)" *?[{`$%#",
1029 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030#endif
1031 )
1032 {
1033 if (found)
1034 {
1035 i = j + 1;
1036 break;
1037 }
1038 else
1039 found = TRUE;
1040 }
1041 }
1042
1043 if (!found)
1044 j = i;
1045 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1046 j += 4;
1047 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1048 && j == i)
1049 j += 3;
1050 else
1051 j = 0;
1052 if (j > 0)
1053 {
1054 /* TODO this is only for DOS/UNIX systems - need to put in
1055 * machine-specific stuff here and in upseg init */
1056 cmdline_del(j);
1057 put_on_cmdline(upseg + 1, 3, FALSE);
1058 }
1059 else if (ccline.cmdpos > i)
1060 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001061
1062 /* Now complete in the new directory. Set KeyTyped in case the
1063 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001065 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 }
1067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068
1069#endif /* FEAT_WILDMENU */
1070
1071 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1072 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1073 if (c == Ctrl_BSL)
1074 {
1075 ++no_mapping;
1076 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001077 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 --no_mapping;
1079 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001080 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1081 * is in a mapping. */
1082 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1083 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 {
1085 vungetc(c);
1086 c = Ctrl_BSL;
1087 }
1088#ifdef FEAT_EVAL
1089 else if (c == 'e')
1090 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001091 char_u *p = NULL;
1092 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093
1094 /*
1095 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001096 * Need to save and restore the current command line, to be
1097 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 */
1099 if (ccline.cmdpos == ccline.cmdlen)
1100 new_cmdpos = 99999; /* keep it at the end */
1101 else
1102 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001103
1104 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001106 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (c == '=')
1108 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001109 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001110 * to avoid nasty things like going to another buffer when
1111 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001112 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001113 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001115 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001116 restore_cmdline(&save_ccline);
1117
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001118 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001120 len = (int)STRLEN(p);
1121 if (realloc_cmdbuff(len + 1) == OK)
1122 {
1123 ccline.cmdlen = len;
1124 STRCPY(ccline.cmdbuff, p);
1125 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001127 /* Restore the cursor or use the position set with
1128 * set_cmdline_pos(). */
1129 if (new_cmdpos > ccline.cmdlen)
1130 ccline.cmdpos = ccline.cmdlen;
1131 else
1132 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001134 KeyTyped = FALSE; /* Don't do p_wc completion. */
1135 redrawcmd();
1136 goto cmdline_changed;
1137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 }
1139 }
1140 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001141 got_int = FALSE; /* don't abandon the command line */
1142 did_emsg = FALSE;
1143 emsg_on_display = FALSE;
1144 redrawcmd();
1145 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 }
1147#endif
1148 else
1149 {
1150 if (c == Ctrl_G && p_im && restart_edit == 0)
1151 restart_edit = 'a';
1152 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1153 in history */
1154 goto returncmd; /* back to Normal mode */
1155 }
1156 }
1157
1158#ifdef FEAT_CMDWIN
1159 if (c == cedit_key || c == K_CMDWIN)
1160 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001161 if (ex_normal_busy == 0 && got_int == FALSE)
1162 {
1163 /*
1164 * Open a window to edit the command line (and history).
1165 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001166 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001167 some_key_typed = TRUE;
1168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 }
1170# ifdef FEAT_DIGRAPHS
1171 else
1172# endif
1173#endif
1174#ifdef FEAT_DIGRAPHS
1175 c = do_digraph(c);
1176#endif
1177
1178 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1179 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1180 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001181 /* In Ex mode a backslash escapes a newline. */
1182 if (exmode_active
1183 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001184 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001185 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001186 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001188 if (c == K_KENTER)
1189 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001191 else
1192 {
1193 gotesc = FALSE; /* Might have typed ESC previously, don't
1194 truncate the cmdline now. */
1195 if (ccheck_abbr(c + ABBR_OFF))
1196 goto cmdline_changed;
1197 if (!cmd_silent)
1198 {
1199 windgoto(msg_row, 0);
1200 out_flush();
1201 }
1202 break;
1203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 }
1205
1206 /*
1207 * Completion for 'wildchar' or 'wildcharm' key.
1208 * - hitting <ESC> twice means: abandon command line.
1209 * - wildcard expansion is only done when the 'wildchar' key is really
1210 * typed, not when it comes from a macro
1211 */
1212 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1213 {
1214 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1215 {
1216 /* if 'wildmode' contains "list" may still need to list */
1217 if (xpc.xp_numfiles > 1
1218 && !did_wild_list
1219 && (wim_flags[wim_index] & WIM_LIST))
1220 {
1221 (void)showmatches(&xpc, FALSE);
1222 redrawcmd();
1223 did_wild_list = TRUE;
1224 }
1225 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001226 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1227 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001229 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1230 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 else
1232 res = OK; /* don't insert 'wildchar' now */
1233 }
1234 else /* typed p_wc first time */
1235 {
1236 wim_index = 0;
1237 j = ccline.cmdpos;
1238 /* if 'wildmode' first contains "longest", get longest
1239 * common part */
1240 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001241 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1242 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001244 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1245 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246
1247 /* if interrupted while completing, behave like it failed */
1248 if (got_int)
1249 {
1250 (void)vpeekc(); /* remove <C-C> from input stream */
1251 got_int = FALSE; /* don't abandon the command line */
1252 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1253#ifdef FEAT_WILDMENU
1254 xpc.xp_context = EXPAND_NOTHING;
1255#endif
1256 goto cmdline_changed;
1257 }
1258
1259 /* when more than one match, and 'wildmode' first contains
1260 * "list", or no change and 'wildmode' contains "longest,list",
1261 * list all matches */
1262 if (res == OK && xpc.xp_numfiles > 1)
1263 {
1264 /* a "longest" that didn't do anything is skipped (but not
1265 * "list:longest") */
1266 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1267 wim_index = 1;
1268 if ((wim_flags[wim_index] & WIM_LIST)
1269#ifdef FEAT_WILDMENU
1270 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1271#endif
1272 )
1273 {
1274 if (!(wim_flags[0] & WIM_LONGEST))
1275 {
1276#ifdef FEAT_WILDMENU
1277 int p_wmnu_save = p_wmnu;
1278 p_wmnu = 0;
1279#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001280 /* remove match */
1281 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#ifdef FEAT_WILDMENU
1283 p_wmnu = p_wmnu_save;
1284#endif
1285 }
1286#ifdef FEAT_WILDMENU
1287 (void)showmatches(&xpc, p_wmnu
1288 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1289#else
1290 (void)showmatches(&xpc, FALSE);
1291#endif
1292 redrawcmd();
1293 did_wild_list = TRUE;
1294 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001295 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1296 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001298 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1299 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001302 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304#ifdef FEAT_WILDMENU
1305 else if (xpc.xp_numfiles == -1)
1306 xpc.xp_context = EXPAND_NOTHING;
1307#endif
1308 }
1309 if (wim_index < 3)
1310 ++wim_index;
1311 if (c == ESC)
1312 gotesc = TRUE;
1313 if (res == OK)
1314 goto cmdline_changed;
1315 }
1316
1317 gotesc = FALSE;
1318
1319 /* <S-Tab> goes to last match, in a clumsy way */
1320 if (c == K_S_TAB && KeyTyped)
1321 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001322 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1323 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1324 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 goto cmdline_changed;
1326 }
1327
1328 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1329 c = NL;
1330
1331 do_abbr = TRUE; /* default: check for abbreviation */
1332
1333 /*
1334 * Big switch for a typed command line character.
1335 */
1336 switch (c)
1337 {
1338 case K_BS:
1339 case Ctrl_H:
1340 case K_DEL:
1341 case K_KDEL:
1342 case Ctrl_W:
1343#ifdef FEAT_FKMAP
1344 if (cmd_fkmap && c == K_BS)
1345 c = K_DEL;
1346#endif
1347 if (c == K_KDEL)
1348 c = K_DEL;
1349
1350 /*
1351 * delete current character is the same as backspace on next
1352 * character, except at end of line
1353 */
1354 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1355 ++ccline.cmdpos;
1356#ifdef FEAT_MBYTE
1357 if (has_mbyte && c == K_DEL)
1358 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1359 ccline.cmdbuff + ccline.cmdpos);
1360#endif
1361 if (ccline.cmdpos > 0)
1362 {
1363 char_u *p;
1364
1365 j = ccline.cmdpos;
1366 p = ccline.cmdbuff + j;
1367#ifdef FEAT_MBYTE
1368 if (has_mbyte)
1369 {
1370 p = mb_prevptr(ccline.cmdbuff, p);
1371 if (c == Ctrl_W)
1372 {
1373 while (p > ccline.cmdbuff && vim_isspace(*p))
1374 p = mb_prevptr(ccline.cmdbuff, p);
1375 i = mb_get_class(p);
1376 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1377 p = mb_prevptr(ccline.cmdbuff, p);
1378 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001379 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 }
1382 else
1383#endif
1384 if (c == Ctrl_W)
1385 {
1386 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1387 --p;
1388 i = vim_iswordc(p[-1]);
1389 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1390 && vim_iswordc(p[-1]) == i)
1391 --p;
1392 }
1393 else
1394 --p;
1395 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1396 ccline.cmdlen -= j - ccline.cmdpos;
1397 i = ccline.cmdpos;
1398 while (i < ccline.cmdlen)
1399 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1400
1401 /* Truncate at the end, required for multi-byte chars. */
1402 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001403#ifdef FEAT_SEARCH_EXTRA
1404 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001405 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001406 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001407 /* save view settings, so that the screen
1408 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001409 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001410 }
1411#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 redrawcmd();
1413 }
1414 else if (ccline.cmdlen == 0 && c != Ctrl_W
1415 && ccline.cmdprompt == NULL && indent == 0)
1416 {
1417 /* In ex and debug mode it doesn't make sense to return. */
1418 if (exmode_active
1419#ifdef FEAT_EVAL
1420 || ccline.cmdfirstc == '>'
1421#endif
1422 )
1423 goto cmdline_not_changed;
1424
Bram Moolenaard23a8232018-02-10 18:45:26 +01001425 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 if (!cmd_silent)
1427 {
1428#ifdef FEAT_RIGHTLEFT
1429 if (cmdmsg_rl)
1430 msg_col = Columns;
1431 else
1432#endif
1433 msg_col = 0;
1434 msg_putchar(' '); /* delete ':' */
1435 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001436#ifdef FEAT_SEARCH_EXTRA
1437 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001438 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 redraw_cmdline = TRUE;
1441 goto returncmd; /* back to cmd mode */
1442 }
1443 goto cmdline_changed;
1444
1445 case K_INS:
1446 case K_KINS:
1447#ifdef FEAT_FKMAP
1448 /* if Farsi mode set, we are in reverse insert mode -
1449 Do not change the mode */
1450 if (cmd_fkmap)
1451 beep_flush();
1452 else
1453#endif
1454 ccline.overstrike = !ccline.overstrike;
1455#ifdef CURSOR_SHAPE
1456 ui_cursor_shape(); /* may show different cursor shape */
1457#endif
1458 goto cmdline_not_changed;
1459
1460 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001461 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 {
1463 /* ":lmap" mappings exists, toggle use of mappings. */
1464 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001465#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 im_set_active(FALSE); /* Disable input method */
1467#endif
1468 if (b_im_ptr != NULL)
1469 {
1470 if (State & LANGMAP)
1471 *b_im_ptr = B_IMODE_LMAP;
1472 else
1473 *b_im_ptr = B_IMODE_NONE;
1474 }
1475 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001476#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else
1478 {
1479 /* There are no ":lmap" mappings, toggle IM. When
1480 * 'imdisable' is set don't try getting the status, it's
1481 * always off. */
1482 if ((p_imdisable && b_im_ptr != NULL)
1483 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1484 {
1485 im_set_active(FALSE); /* Disable input method */
1486 if (b_im_ptr != NULL)
1487 *b_im_ptr = B_IMODE_NONE;
1488 }
1489 else
1490 {
1491 im_set_active(TRUE); /* Enable input method */
1492 if (b_im_ptr != NULL)
1493 *b_im_ptr = B_IMODE_IM;
1494 }
1495 }
1496#endif
1497 if (b_im_ptr != NULL)
1498 {
1499 if (b_im_ptr == &curbuf->b_p_iminsert)
1500 set_iminsert_global();
1501 else
1502 set_imsearch_global();
1503 }
1504#ifdef CURSOR_SHAPE
1505 ui_cursor_shape(); /* may show different cursor shape */
1506#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001507#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508 /* Show/unshow value of 'keymap' in status lines later. */
1509 status_redraw_curbuf();
1510#endif
1511 goto cmdline_not_changed;
1512
1513/* case '@': only in very old vi */
1514 case Ctrl_U:
1515 /* delete all characters left of the cursor */
1516 j = ccline.cmdpos;
1517 ccline.cmdlen -= j;
1518 i = ccline.cmdpos = 0;
1519 while (i < ccline.cmdlen)
1520 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1521 /* Truncate at the end, required for multi-byte chars. */
1522 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001523#ifdef FEAT_SEARCH_EXTRA
1524 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001525 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 redrawcmd();
1528 goto cmdline_changed;
1529
1530#ifdef FEAT_CLIPBOARD
1531 case Ctrl_Y:
1532 /* Copy the modeless selection, if there is one. */
1533 if (clip_star.state != SELECT_CLEARED)
1534 {
1535 if (clip_star.state == SELECT_DONE)
1536 clip_copy_modeless_selection(TRUE);
1537 goto cmdline_not_changed;
1538 }
1539 break;
1540#endif
1541
1542 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1543 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001544 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001545 * ":normal" runs out of characters. */
1546 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001547 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 goto cmdline_not_changed;
1549
1550 gotesc = TRUE; /* will free ccline.cmdbuff after
1551 putting it in history */
1552 goto returncmd; /* back to cmd mode */
1553
1554 case Ctrl_R: /* insert register */
1555#ifdef USE_ON_FLY_SCROLL
1556 dont_scroll = TRUE; /* disallow scrolling here */
1557#endif
1558 putcmdline('"', TRUE);
1559 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001560 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (i == Ctrl_O)
1562 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1563 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001564 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001565 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 --no_mapping;
1567#ifdef FEAT_EVAL
1568 /*
1569 * Insert the result of an expression.
1570 * Need to save the current command line, to be able to enter
1571 * a new one...
1572 */
1573 new_cmdpos = -1;
1574 if (c == '=')
1575 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1577 {
1578 beep_flush();
1579 c = ESC;
1580 }
1581 else
1582 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001583 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001585 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 }
1587 }
1588#endif
1589 if (c != ESC) /* use ESC to cancel inserting register */
1590 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001591 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001592
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001593#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001594 /* When there was a serious error abort getting the
1595 * command line. */
1596 if (aborting())
1597 {
1598 gotesc = TRUE; /* will free ccline.cmdbuff after
1599 putting it in history */
1600 goto returncmd; /* back to cmd mode */
1601 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 KeyTyped = FALSE; /* Don't do p_wc completion. */
1604#ifdef FEAT_EVAL
1605 if (new_cmdpos >= 0)
1606 {
1607 /* set_cmdline_pos() was used */
1608 if (new_cmdpos > ccline.cmdlen)
1609 ccline.cmdpos = ccline.cmdlen;
1610 else
1611 ccline.cmdpos = new_cmdpos;
1612 }
1613#endif
1614 }
1615 redrawcmd();
1616 goto cmdline_changed;
1617
1618 case Ctrl_D:
1619 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1620 break; /* Use ^D as normal char instead */
1621
1622 redrawcmd();
1623 continue; /* don't do incremental search now */
1624
1625 case K_RIGHT:
1626 case K_S_RIGHT:
1627 case K_C_RIGHT:
1628 do
1629 {
1630 if (ccline.cmdpos >= ccline.cmdlen)
1631 break;
1632 i = cmdline_charsize(ccline.cmdpos);
1633 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1634 break;
1635 ccline.cmdspos += i;
1636#ifdef FEAT_MBYTE
1637 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001638 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 + ccline.cmdpos);
1640 else
1641#endif
1642 ++ccline.cmdpos;
1643 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001644 while ((c == K_S_RIGHT || c == K_C_RIGHT
1645 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1647#ifdef FEAT_MBYTE
1648 if (has_mbyte)
1649 set_cmdspos_cursor();
1650#endif
1651 goto cmdline_not_changed;
1652
1653 case K_LEFT:
1654 case K_S_LEFT:
1655 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001656 if (ccline.cmdpos == 0)
1657 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 do
1659 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 --ccline.cmdpos;
1661#ifdef FEAT_MBYTE
1662 if (has_mbyte) /* move to first byte of char */
1663 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1664 ccline.cmdbuff + ccline.cmdpos);
1665#endif
1666 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1667 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001668 while (ccline.cmdpos > 0
1669 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001670 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1672#ifdef FEAT_MBYTE
1673 if (has_mbyte)
1674 set_cmdspos_cursor();
1675#endif
1676 goto cmdline_not_changed;
1677
1678 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001679 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001680 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
Bram Moolenaar4770d092006-01-12 23:22:24 +00001682#ifdef FEAT_GUI_W32
1683 /* On Win32 ignore <M-F4>, we get it when closing the window was
1684 * cancelled. */
1685 case K_F4:
1686 if (mod_mask == MOD_MASK_ALT)
1687 {
1688 redrawcmd(); /* somehow the cmdline is cleared */
1689 goto cmdline_not_changed;
1690 }
1691 break;
1692#endif
1693
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_MOUSE
1695 case K_MIDDLEDRAG:
1696 case K_MIDDLERELEASE:
1697 goto cmdline_not_changed; /* Ignore mouse */
1698
1699 case K_MIDDLEMOUSE:
1700# ifdef FEAT_GUI
1701 /* When GUI is active, also paste when 'mouse' is empty */
1702 if (!gui.in_use)
1703# endif
1704 if (!mouse_has(MOUSE_COMMAND))
1705 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001706# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001708 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001710# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001711 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 redrawcmd();
1713 goto cmdline_changed;
1714
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001715# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001717 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 redrawcmd();
1719 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001720# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
1722 case K_LEFTDRAG:
1723 case K_LEFTRELEASE:
1724 case K_RIGHTDRAG:
1725 case K_RIGHTRELEASE:
1726 /* Ignore drag and release events when the button-down wasn't
1727 * seen before. */
1728 if (ignore_drag_release)
1729 goto cmdline_not_changed;
1730 /* FALLTHROUGH */
1731 case K_LEFTMOUSE:
1732 case K_RIGHTMOUSE:
1733 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1734 ignore_drag_release = TRUE;
1735 else
1736 ignore_drag_release = FALSE;
1737# ifdef FEAT_GUI
1738 /* When GUI is active, also move when 'mouse' is empty */
1739 if (!gui.in_use)
1740# endif
1741 if (!mouse_has(MOUSE_COMMAND))
1742 goto cmdline_not_changed; /* Ignore mouse */
1743# ifdef FEAT_CLIPBOARD
1744 if (mouse_row < cmdline_row && clip_star.available)
1745 {
1746 int button, is_click, is_drag;
1747
1748 /*
1749 * Handle modeless selection.
1750 */
1751 button = get_mouse_button(KEY2TERMCAP1(c),
1752 &is_click, &is_drag);
1753 if (mouse_model_popup() && button == MOUSE_LEFT
1754 && (mod_mask & MOD_MASK_SHIFT))
1755 {
1756 /* Translate shift-left to right button. */
1757 button = MOUSE_RIGHT;
1758 mod_mask &= ~MOD_MASK_SHIFT;
1759 }
1760 clip_modeless(button, is_click, is_drag);
1761 goto cmdline_not_changed;
1762 }
1763# endif
1764
1765 set_cmdspos();
1766 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1767 ++ccline.cmdpos)
1768 {
1769 i = cmdline_charsize(ccline.cmdpos);
1770 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1771 && mouse_col < ccline.cmdspos % Columns + i)
1772 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001773# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (has_mbyte)
1775 {
1776 /* Count ">" for double-wide char that doesn't fit. */
1777 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001778 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 + ccline.cmdpos) - 1;
1780 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001781# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 ccline.cmdspos += i;
1783 }
1784 goto cmdline_not_changed;
1785
1786 /* Mouse scroll wheel: ignored here */
1787 case K_MOUSEDOWN:
1788 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001789 case K_MOUSELEFT:
1790 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 /* Alternate buttons ignored here */
1792 case K_X1MOUSE:
1793 case K_X1DRAG:
1794 case K_X1RELEASE:
1795 case K_X2MOUSE:
1796 case K_X2DRAG:
1797 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001798 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 goto cmdline_not_changed;
1800
1801#endif /* FEAT_MOUSE */
1802
1803#ifdef FEAT_GUI
1804 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1805 case K_LEFTRELEASE_NM:
1806 goto cmdline_not_changed;
1807
1808 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001809 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 {
1811 gui_do_scroll();
1812 redrawcmd();
1813 }
1814 goto cmdline_not_changed;
1815
1816 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001817 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001819 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 redrawcmd();
1821 }
1822 goto cmdline_not_changed;
1823#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001824#ifdef FEAT_GUI_TABLINE
1825 case K_TABLINE:
1826 case K_TABMENU:
1827 /* Don't want to change any tabs here. Make sure the same tab
1828 * is still selected. */
1829 if (gui_use_tabline())
1830 gui_mch_set_curtab(tabpage_index(curtab));
1831 goto cmdline_not_changed;
1832#endif
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 case K_SELECT: /* end of Select mode mapping - ignore */
1835 goto cmdline_not_changed;
1836
1837 case Ctrl_B: /* begin of command line */
1838 case K_HOME:
1839 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 case K_S_HOME:
1841 case K_C_HOME:
1842 ccline.cmdpos = 0;
1843 set_cmdspos();
1844 goto cmdline_not_changed;
1845
1846 case Ctrl_E: /* end of command line */
1847 case K_END:
1848 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 case K_S_END:
1850 case K_C_END:
1851 ccline.cmdpos = ccline.cmdlen;
1852 set_cmdspos_cursor();
1853 goto cmdline_not_changed;
1854
1855 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001856 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 break;
1858 goto cmdline_changed;
1859
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001860 case Ctrl_L:
1861#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001862 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001863 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001864#endif
1865
1866 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001867 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 break;
1869 goto cmdline_changed;
1870
1871 case Ctrl_N: /* next match */
1872 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001873 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001875 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1876 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02001878 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02001881 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 case K_UP:
1883 case K_DOWN:
1884 case K_S_UP:
1885 case K_S_DOWN:
1886 case K_PAGEUP:
1887 case K_KPAGEUP:
1888 case K_PAGEDOWN:
1889 case K_KPAGEDOWN:
1890 if (hislen == 0 || firstc == NUL) /* no history */
1891 goto cmdline_not_changed;
1892
1893 i = hiscnt;
1894
1895 /* save current command string so it can be restored later */
1896 if (lookfor == NULL)
1897 {
1898 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
1899 goto cmdline_not_changed;
1900 lookfor[ccline.cmdpos] = NUL;
1901 }
1902
1903 j = (int)STRLEN(lookfor);
1904 for (;;)
1905 {
1906 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001907 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001908 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 {
1910 if (hiscnt == hislen) /* first time */
1911 hiscnt = hisidx[histype];
1912 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
1913 hiscnt = hislen - 1;
1914 else if (hiscnt != hisidx[histype] + 1)
1915 --hiscnt;
1916 else /* at top of list */
1917 {
1918 hiscnt = i;
1919 break;
1920 }
1921 }
1922 else /* one step forwards */
1923 {
1924 /* on last entry, clear the line */
1925 if (hiscnt == hisidx[histype])
1926 {
1927 hiscnt = hislen;
1928 break;
1929 }
1930
1931 /* not on a history line, nothing to do */
1932 if (hiscnt == hislen)
1933 break;
1934 if (hiscnt == hislen - 1) /* wrap around */
1935 hiscnt = 0;
1936 else
1937 ++hiscnt;
1938 }
1939 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
1940 {
1941 hiscnt = i;
1942 break;
1943 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001944 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001945 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 || STRNCMP(history[histype][hiscnt].hisstr,
1947 lookfor, (size_t)j) == 0)
1948 break;
1949 }
1950
1951 if (hiscnt != i) /* jumped to other entry */
1952 {
1953 char_u *p;
1954 int len;
1955 int old_firstc;
1956
1957 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001958 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 if (hiscnt == hislen)
1960 p = lookfor; /* back to the old one */
1961 else
1962 p = history[histype][hiscnt].hisstr;
1963
1964 if (histype == HIST_SEARCH
1965 && p != lookfor
1966 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
1967 {
1968 /* Correct for the separator character used when
1969 * adding the history entry vs the one used now.
1970 * First loop: count length.
1971 * Second loop: copy the characters. */
1972 for (i = 0; i <= 1; ++i)
1973 {
1974 len = 0;
1975 for (j = 0; p[j] != NUL; ++j)
1976 {
1977 /* Replace old sep with new sep, unless it is
1978 * escaped. */
1979 if (p[j] == old_firstc
1980 && (j == 0 || p[j - 1] != '\\'))
1981 {
1982 if (i > 0)
1983 ccline.cmdbuff[len] = firstc;
1984 }
1985 else
1986 {
1987 /* Escape new sep, unless it is already
1988 * escaped. */
1989 if (p[j] == firstc
1990 && (j == 0 || p[j - 1] != '\\'))
1991 {
1992 if (i > 0)
1993 ccline.cmdbuff[len] = '\\';
1994 ++len;
1995 }
1996 if (i > 0)
1997 ccline.cmdbuff[len] = p[j];
1998 }
1999 ++len;
2000 }
2001 if (i == 0)
2002 {
2003 alloc_cmdbuff(len);
2004 if (ccline.cmdbuff == NULL)
2005 goto returncmd;
2006 }
2007 }
2008 ccline.cmdbuff[len] = NUL;
2009 }
2010 else
2011 {
2012 alloc_cmdbuff((int)STRLEN(p));
2013 if (ccline.cmdbuff == NULL)
2014 goto returncmd;
2015 STRCPY(ccline.cmdbuff, p);
2016 }
2017
2018 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2019 redrawcmd();
2020 goto cmdline_changed;
2021 }
2022 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002023#endif
2024 goto cmdline_not_changed;
2025
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002026#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002027 case Ctrl_G: /* next match */
2028 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002029 if (may_adjust_incsearch_highlighting(
2030 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002031 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002032 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033#endif
2034
2035 case Ctrl_V:
2036 case Ctrl_Q:
2037#ifdef FEAT_MOUSE
2038 ignore_drag_release = TRUE;
2039#endif
2040 putcmdline('^', TRUE);
2041 c = get_literal(); /* get next (two) character(s) */
2042 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002043 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044#ifdef FEAT_MBYTE
2045 /* may need to remove ^ when composing char was typed */
2046 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2047 {
2048 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2049 msg_putchar(' ');
2050 cursorcmd();
2051 }
2052#endif
2053 break;
2054
2055#ifdef FEAT_DIGRAPHS
2056 case Ctrl_K:
2057#ifdef FEAT_MOUSE
2058 ignore_drag_release = TRUE;
2059#endif
2060 putcmdline('?', TRUE);
2061#ifdef USE_ON_FLY_SCROLL
2062 dont_scroll = TRUE; /* disallow scrolling here */
2063#endif
2064 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002065 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 if (c != NUL)
2067 break;
2068
2069 redrawcmd();
2070 goto cmdline_not_changed;
2071#endif /* FEAT_DIGRAPHS */
2072
2073#ifdef FEAT_RIGHTLEFT
2074 case Ctrl__: /* CTRL-_: switch language mode */
2075 if (!p_ari)
2076 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002077# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 if (p_altkeymap)
2079 {
2080 cmd_fkmap = !cmd_fkmap;
2081 if (cmd_fkmap) /* in Farsi always in Insert mode */
2082 ccline.overstrike = FALSE;
2083 }
2084 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 cmd_hkmap = !cmd_hkmap;
2087 goto cmdline_not_changed;
2088#endif
2089
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002090 case K_PS:
2091 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2092 goto cmdline_changed;
2093
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 default:
2095#ifdef UNIX
2096 if (c == intr_char)
2097 {
2098 gotesc = TRUE; /* will free ccline.cmdbuff after
2099 putting it in history */
2100 goto returncmd; /* back to Normal mode */
2101 }
2102#endif
2103 /*
2104 * Normal character with no special meaning. Just set mod_mask
2105 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2106 * the string <S-Space>. This should only happen after ^V.
2107 */
2108 if (!IS_SPECIAL(c))
2109 mod_mask = 0x0;
2110 break;
2111 }
2112 /*
2113 * End of switch on command line character.
2114 * We come here if we have a normal character.
2115 */
2116
Bram Moolenaarede3e632013-06-23 16:16:19 +02002117 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118#ifdef FEAT_MBYTE
2119 /* Add ABBR_OFF for characters above 0x100, this is
2120 * what check_abbr() expects. */
2121 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2122#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002123 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 goto cmdline_changed;
2125
2126 /*
2127 * put the character in the command line
2128 */
2129 if (IS_SPECIAL(c) || mod_mask != 0)
2130 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2131 else
2132 {
2133#ifdef FEAT_MBYTE
2134 if (has_mbyte)
2135 {
2136 j = (*mb_char2bytes)(c, IObuff);
2137 IObuff[j] = NUL; /* exclude composing chars */
2138 put_on_cmdline(IObuff, j, TRUE);
2139 }
2140 else
2141#endif
2142 {
2143 IObuff[0] = c;
2144 put_on_cmdline(IObuff, 1, TRUE);
2145 }
2146 }
2147 goto cmdline_changed;
2148
2149/*
2150 * This part implements incremental searches for "/" and "?"
2151 * Jump to cmdline_not_changed when a character has been read but the command
2152 * line did not change. Then we only search and redraw if something changed in
2153 * the past.
2154 * Jump to cmdline_changed when the command line did change.
2155 * (Sorry for the goto's, I know it is ugly).
2156 */
2157cmdline_not_changed:
2158#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002159 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 continue;
2161#endif
2162
2163cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002164 /* Trigger CmdlineChanged autocommands. */
2165 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002168 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169#endif
2170
2171#ifdef FEAT_RIGHTLEFT
2172 if (cmdmsg_rl
2173# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002174 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175# endif
2176 )
2177 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002178 * right-left typing. Not efficient, but it works.
2179 * Do it only when there are no characters left to read
2180 * to avoid useless intermediate redraws. */
2181 if (vpeekc() == NUL)
2182 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#endif
2184 }
2185
2186returncmd:
2187
2188#ifdef FEAT_RIGHTLEFT
2189 cmdmsg_rl = FALSE;
2190#endif
2191
2192#ifdef FEAT_FKMAP
2193 cmd_fkmap = 0;
2194#endif
2195
2196 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002197 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198
2199#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002200 finish_incsearch_highlighting(gotesc, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#endif
2202
2203 if (ccline.cmdbuff != NULL)
2204 {
2205 /*
2206 * Put line in history buffer (":" and "=" only when it was typed).
2207 */
2208#ifdef FEAT_CMDHIST
2209 if (ccline.cmdlen && firstc != NUL
2210 && (some_key_typed || histype == HIST_SEARCH))
2211 {
2212 add_to_history(histype, ccline.cmdbuff, TRUE,
2213 histype == HIST_SEARCH ? firstc : NUL);
2214 if (firstc == ':')
2215 {
2216 vim_free(new_last_cmdline);
2217 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2218 }
2219 }
2220#endif
2221
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002222 if (gotesc)
2223 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225
2226 /*
2227 * If the screen was shifted up, redraw the whole screen (later).
2228 * If the line is too long, clear it, so ruler and shown command do
2229 * not get printed in the middle of it.
2230 */
2231 msg_check();
2232 msg_scroll = save_msg_scroll;
2233 redir_off = FALSE;
2234
2235 /* When the command line was typed, no need for a wait-return prompt. */
2236 if (some_key_typed)
2237 need_wait_return = FALSE;
2238
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002239 /* Trigger CmdlineLeave autocommands. */
2240 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002241
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002243#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2245 im_save_status(b_im_ptr);
2246 im_set_active(FALSE);
2247#endif
2248#ifdef FEAT_MOUSE
2249 setmouse();
2250#endif
2251#ifdef CURSOR_SHAPE
2252 ui_cursor_shape(); /* may show different cursor shape */
2253#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002254 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002256 {
2257 char_u *p = ccline.cmdbuff;
2258
2259 /* Make ccline empty, getcmdline() may try to use it. */
2260 ccline.cmdbuff = NULL;
2261 return p;
2262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263}
2264
2265#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2266/*
2267 * Get a command line with a prompt.
2268 * This is prepared to be called recursively from getcmdline() (e.g. by
2269 * f_input() when evaluating an expression from CTRL-R =).
2270 * Returns the command line in allocated memory, or NULL.
2271 */
2272 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002273getcmdline_prompt(
2274 int firstc,
2275 char_u *prompt, /* command line prompt */
2276 int attr, /* attributes for prompt */
2277 int xp_context, /* type of expansion */
2278 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279{
2280 char_u *s;
2281 struct cmdline_info save_ccline;
2282 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002283 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002285 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 ccline.cmdprompt = prompt;
2287 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002288# ifdef FEAT_EVAL
2289 ccline.xp_context = xp_context;
2290 ccline.xp_arg = xp_arg;
2291 ccline.input_fn = (firstc == '@');
2292# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002293 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002295 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002296 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002297 /* Restore msg_col, the prompt from input() may have changed it.
2298 * But only if called recursively and the commandline is therefore being
2299 * restored to an old one; if not, the input() prompt stays on the screen,
2300 * so we need its modified msg_col left intact. */
2301 if (ccline.cmdbuff != NULL)
2302 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303
2304 return s;
2305}
2306#endif
2307
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002308/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002309 * Return TRUE when the text must not be changed and we can't switch to
2310 * another window or buffer. Used when editing the command line, evaluating
2311 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002312 */
2313 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002314text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002315{
2316#ifdef FEAT_CMDWIN
2317 if (cmdwin_type != 0)
2318 return TRUE;
2319#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002320 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002321}
2322
2323/*
2324 * Give an error message for a command that isn't allowed while the cmdline
2325 * window is open or editing the cmdline in another way.
2326 */
2327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002328text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002329{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002330 EMSG(_(get_text_locked_msg()));
2331}
2332
2333 char_u *
2334get_text_locked_msg(void)
2335{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002336#ifdef FEAT_CMDWIN
2337 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002338 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002339#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002340 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002341}
2342
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002343/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002344 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2345 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002346 */
2347 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002348curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002349{
2350 if (curbuf_lock > 0)
2351 {
2352 EMSG(_("E788: Not allowed to edit another buffer now"));
2353 return TRUE;
2354 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002355 return allbuf_locked();
2356}
2357
2358/*
2359 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2360 * message.
2361 */
2362 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002363allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002364{
2365 if (allbuf_lock > 0)
2366 {
2367 EMSG(_("E811: Not allowed to change buffer information now"));
2368 return TRUE;
2369 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002370 return FALSE;
2371}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002374cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
2376#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2377 if (cmdline_star > 0) /* showing '*', always 1 position */
2378 return 1;
2379#endif
2380 return ptr2cells(ccline.cmdbuff + idx);
2381}
2382
2383/*
2384 * Compute the offset of the cursor on the command line for the prompt and
2385 * indent.
2386 */
2387 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002388set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002390 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 ccline.cmdspos = 1 + ccline.cmdindent;
2392 else
2393 ccline.cmdspos = 0 + ccline.cmdindent;
2394}
2395
2396/*
2397 * Compute the screen position for the cursor on the command line.
2398 */
2399 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002400set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 int i, m, c;
2403
2404 set_cmdspos();
2405 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002406 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002408 if (m < 0) /* overflow, Columns or Rows at weird value */
2409 m = MAXCOL;
2410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 else
2412 m = MAXCOL;
2413 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2414 {
2415 c = cmdline_charsize(i);
2416#ifdef FEAT_MBYTE
2417 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2418 if (has_mbyte)
2419 correct_cmdspos(i, c);
2420#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002421 /* If the cmdline doesn't fit, show cursor on last visible char.
2422 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 if ((ccline.cmdspos += c) >= m)
2424 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 ccline.cmdspos -= c;
2426 break;
2427 }
2428#ifdef FEAT_MBYTE
2429 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002430 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431#endif
2432 }
2433}
2434
2435#ifdef FEAT_MBYTE
2436/*
2437 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2438 * character that doesn't fit, so that a ">" must be displayed.
2439 */
2440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002441correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002443 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2445 && ccline.cmdspos % Columns + cells > Columns)
2446 ccline.cmdspos++;
2447}
2448#endif
2449
2450/*
2451 * Get an Ex command line for the ":" command.
2452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002454getexline(
2455 int c, /* normally ':', NUL for ":append" */
2456 void *cookie UNUSED,
2457 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458{
2459 /* When executing a register, remove ':' that's in front of each line. */
2460 if (exec_from_reg && vpeekc() == ':')
2461 (void)vgetc();
2462 return getcmdline(c, 1L, indent);
2463}
2464
2465/*
2466 * Get an Ex command line for Ex mode.
2467 * In Ex mode we only use the OS supplied line editing features and no
2468 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002469 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002472getexmodeline(
2473 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002474 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002475 void *cookie UNUSED,
2476 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002478 garray_T line_ga;
2479 char_u *pend;
2480 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002481 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002482 int escaped = FALSE; /* CTRL-V typed */
2483 int vcol = 0;
2484 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002485 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002486 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
2488 /* Switch cursor on now. This avoids that it happens after the "\n", which
2489 * confuses the system function that computes tabstops. */
2490 cursor_on();
2491
2492 /* always start in column 0; write a newline if necessary */
2493 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002494 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002496 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002498 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002499 if (p_prompt)
2500 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 while (indent-- > 0)
2502 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 }
2505
2506 ga_init2(&line_ga, 1, 30);
2507
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002508 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002509 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002510 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002511 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002512 while (indent >= 8)
2513 {
2514 ga_append(&line_ga, TAB);
2515 msg_puts((char_u *)" ");
2516 indent -= 8;
2517 }
2518 while (indent-- > 0)
2519 {
2520 ga_append(&line_ga, ' ');
2521 msg_putchar(' ');
2522 }
2523 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002524 ++no_mapping;
2525 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /*
2528 * Get the line, one character at a time.
2529 */
2530 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002531 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002533 long sw;
2534 char_u *s;
2535
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (ga_grow(&line_ga, 40) == FAIL)
2537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
Bram Moolenaarba748c82017-02-26 14:00:07 +01002539 /*
2540 * Get one character at a time.
2541 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002542 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002543
2544 /* Check for a ":normal" command and no more characters left. */
2545 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2546 c1 = '\n';
2547 else
2548 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002551 * Handle line editing.
2552 * Previously this was left to the system, putting the terminal in
2553 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002555 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002557 msg_putchar('\n');
2558 break;
2559 }
2560
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002561 if (c1 == K_PS)
2562 {
2563 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2564 goto redraw;
2565 }
2566
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002567 if (!escaped)
2568 {
2569 /* CR typed means "enter", which is NL */
2570 if (c1 == '\r')
2571 c1 = '\n';
2572
2573 if (c1 == BS || c1 == K_BS
2574 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002576 if (line_ga.ga_len > 0)
2577 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002578#ifdef FEAT_MBYTE
2579 if (has_mbyte)
2580 {
2581 p = (char_u *)line_ga.ga_data;
2582 p[line_ga.ga_len] = NUL;
2583 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2584 line_ga.ga_len -= len;
2585 }
2586 else
2587#endif
2588 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002589 goto redraw;
2590 }
2591 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
2593
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002594 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002596 msg_col = startcol;
2597 msg_clr_eos();
2598 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002599 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002600 }
2601
2602 if (c1 == Ctrl_T)
2603 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002604 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002605 p = (char_u *)line_ga.ga_data;
2606 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002607 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002608 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002609add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002610 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002612 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002613 p = (char_u *)line_ga.ga_data;
2614 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002615 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2616 *s = ' ';
2617 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002619redraw:
2620 /* redraw the line */
2621 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002622 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002623 p = (char_u *)line_ga.ga_data;
2624 p[line_ga.ga_len] = NUL;
2625 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002627 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002629 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 msg_putchar(' ');
2632 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002633 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002635 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002637 len = MB_PTR2LEN(p);
2638 msg_outtrans_len(p, len);
2639 vcol += ptr2cells(p);
2640 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002643 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002644 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002645 continue;
2646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002648 if (c1 == Ctrl_D)
2649 {
2650 /* Delete one shiftwidth. */
2651 p = (char_u *)line_ga.ga_data;
2652 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002654 if (prev_char == '^')
2655 ex_keep_indent = TRUE;
2656 indent = 0;
2657 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 else
2660 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002661 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002662 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002663 if (indent > 0)
2664 {
2665 --indent;
2666 indent -= indent % get_sw_value(curbuf);
2667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002669 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002670 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002671 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2673 --line_ga.ga_len;
2674 }
2675 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677
2678 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2679 {
2680 escaped = TRUE;
2681 continue;
2682 }
2683
2684 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2685 if (IS_SPECIAL(c1))
2686 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002688
2689 if (IS_SPECIAL(c1))
2690 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002691#ifdef FEAT_MBYTE
2692 if (has_mbyte)
2693 len = (*mb_char2bytes)(c1,
2694 (char_u *)line_ga.ga_data + line_ga.ga_len);
2695 else
2696#endif
2697 {
2698 len = 1;
2699 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2700 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002701 if (c1 == '\n')
2702 msg_putchar('\n');
2703 else if (c1 == TAB)
2704 {
2705 /* Don't use chartabsize(), 'ts' can be different */
2706 do
2707 {
2708 msg_putchar(' ');
2709 } while (++vcol % 8);
2710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002714 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002717 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 escaped = FALSE;
2719
2720 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002721 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002723 /* We are done when a NL is entered, but not when it comes after an
2724 * odd number of backslashes, that results in a NUL. */
2725 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002727 int bcount = 0;
2728
2729 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2730 ++bcount;
2731
2732 if (bcount > 0)
2733 {
2734 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2735 * "\NL", etc. */
2736 line_ga.ga_len -= (bcount + 1) / 2;
2737 pend -= (bcount + 1) / 2;
2738 pend[-1] = '\n';
2739 }
2740
2741 if ((bcount & 1) == 0)
2742 {
2743 --line_ga.ga_len;
2744 --pend;
2745 *pend = NUL;
2746 break;
2747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 }
2749 }
2750
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002751 --no_mapping;
2752 --allow_keys;
2753
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 /* make following messages go to the next line */
2755 msg_didout = FALSE;
2756 msg_col = 0;
2757 if (msg_row < Rows - 1)
2758 ++msg_row;
2759 emsg_on_display = FALSE; /* don't want ui_delay() */
2760
2761 if (got_int)
2762 ga_clear(&line_ga);
2763
2764 return (char_u *)line_ga.ga_data;
2765}
2766
Bram Moolenaare344bea2005-09-01 20:46:49 +00002767# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2768 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769/*
2770 * Return TRUE if ccline.overstrike is on.
2771 */
2772 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002773cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 return ccline.overstrike;
2776}
2777
2778/*
2779 * Return TRUE if the cursor is at the end of the cmdline.
2780 */
2781 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002782cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783{
2784 return (ccline.cmdpos >= ccline.cmdlen);
2785}
2786#endif
2787
Bram Moolenaar9372a112005-12-06 19:59:18 +00002788#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789/*
2790 * Return the virtual column number at the current cursor position.
2791 * This is used by the IM code to obtain the start of the preedit string.
2792 */
2793 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002794cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2797 return MAXCOL;
2798
2799# ifdef FEAT_MBYTE
2800 if (has_mbyte)
2801 {
2802 colnr_T col;
2803 int i = 0;
2804
2805 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002806 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807
2808 return col;
2809 }
2810 else
2811# endif
2812 return ccline.cmdpos;
2813}
2814#endif
2815
2816#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2817/*
2818 * If part of the command line is an IM preedit string, redraw it with
2819 * IM feedback attributes. The cursor position is restored after drawing.
2820 */
2821 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002822redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
2824 if ((State & CMDLINE)
2825 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002826 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 && !p_imdisable
2828 && im_is_preediting())
2829 {
2830 int cmdpos = 0;
2831 int cmdspos;
2832 int old_row;
2833 int old_col;
2834 colnr_T col;
2835
2836 old_row = msg_row;
2837 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002838 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839
2840# ifdef FEAT_MBYTE
2841 if (has_mbyte)
2842 {
2843 for (col = 0; col < preedit_start_col
2844 && cmdpos < ccline.cmdlen; ++col)
2845 {
2846 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002847 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 }
2849 }
2850 else
2851# endif
2852 {
2853 cmdspos += preedit_start_col;
2854 cmdpos += preedit_start_col;
2855 }
2856
2857 msg_row = cmdline_row + (cmdspos / (int)Columns);
2858 msg_col = cmdspos % (int)Columns;
2859 if (msg_row >= Rows)
2860 msg_row = Rows - 1;
2861
2862 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2863 {
2864 int char_len;
2865 int char_attr;
2866
2867 char_attr = im_get_feedback_attr(col);
2868 if (char_attr < 0)
2869 break; /* end of preedit string */
2870
2871# ifdef FEAT_MBYTE
2872 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002873 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 else
2875# endif
2876 char_len = 1;
2877
2878 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
2879 cmdpos += char_len;
2880 }
2881
2882 msg_row = old_row;
2883 msg_col = old_col;
2884 }
2885}
2886#endif /* FEAT_XIM && FEAT_GUI_GTK */
2887
2888/*
2889 * Allocate a new command line buffer.
2890 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
2891 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
2892 */
2893 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002894alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 /*
2897 * give some extra space to avoid having to allocate all the time
2898 */
2899 if (len < 80)
2900 len = 100;
2901 else
2902 len += 20;
2903
2904 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
2905 ccline.cmdbufflen = len;
2906}
2907
2908/*
2909 * Re-allocate the command line to length len + something extra.
2910 * return FAIL for failure, OK otherwise
2911 */
2912 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002913realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915 char_u *p;
2916
Bram Moolenaar673b87b2010-08-13 19:12:07 +02002917 if (len < ccline.cmdbufflen)
2918 return OK; /* no need to resize */
2919
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 p = ccline.cmdbuff;
2921 alloc_cmdbuff(len); /* will get some more */
2922 if (ccline.cmdbuff == NULL) /* out of memory */
2923 {
2924 ccline.cmdbuff = p; /* keep the old one */
2925 return FAIL;
2926 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02002927 /* There isn't always a NUL after the command, but it may need to be
2928 * there, thus copy up to the NUL and add a NUL. */
2929 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
2930 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002932
2933 if (ccline.xpc != NULL
2934 && ccline.xpc->xp_pattern != NULL
2935 && ccline.xpc->xp_context != EXPAND_NOTHING
2936 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
2937 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00002938 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002939
2940 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
2941 * to point into the newly allocated memory. */
2942 if (i >= 0 && i <= ccline.cmdlen)
2943 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
2944 }
2945
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 return OK;
2947}
2948
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002949#if defined(FEAT_ARABIC) || defined(PROTO)
2950static char_u *arshape_buf = NULL;
2951
2952# if defined(EXITFREE) || defined(PROTO)
2953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002954free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002955{
2956 vim_free(arshape_buf);
2957}
2958# endif
2959#endif
2960
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961/*
2962 * Draw part of the cmdline at the current cursor position. But draw stars
2963 * when cmdline_star is TRUE.
2964 */
2965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002966draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
2968#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2969 int i;
2970
2971 if (cmdline_star > 0)
2972 for (i = 0; i < len; ++i)
2973 {
2974 msg_putchar('*');
2975# ifdef FEAT_MBYTE
2976 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002977 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978# endif
2979 }
2980 else
2981#endif
2982#ifdef FEAT_ARABIC
2983 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
2984 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 static int buflen = 0;
2986 char_u *p;
2987 int j;
2988 int newlen = 0;
2989 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00002990 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 int prev_c = 0;
2992 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002993 int u8c;
2994 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996
2997 /*
2998 * Do arabic shaping into a temporary buffer. This is very
2999 * inefficient!
3000 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003001 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 {
3003 /* Re-allocate the buffer. We keep it around to avoid a lot of
3004 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003005 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003006 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003007 arshape_buf = alloc(buflen);
3008 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 return; /* out of memory */
3010 }
3011
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003012 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3013 {
3014 /* Prepend a space to draw the leading composing char on. */
3015 arshape_buf[0] = ' ';
3016 newlen = 1;
3017 }
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 for (j = start; j < start + len; j += mb_l)
3020 {
3021 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003022 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003023 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 if (ARABIC_CHAR(u8c))
3025 {
3026 /* Do Arabic shaping. */
3027 if (cmdmsg_rl)
3028 {
3029 /* displaying from right to left */
3030 pc = prev_c;
3031 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003032 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 if (j + mb_l >= start + len)
3034 nc = NUL;
3035 else
3036 nc = utf_ptr2char(p + mb_l);
3037 }
3038 else
3039 {
3040 /* displaying from left to right */
3041 if (j + mb_l >= start + len)
3042 pc = NUL;
3043 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003044 {
3045 int pcc[MAX_MCO];
3046
3047 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003049 pc1 = pcc[0];
3050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 nc = prev_c;
3052 }
3053 prev_c = u8c;
3054
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003055 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003057 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003058 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003060 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3061 if (u8cc[1] != 0)
3062 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003063 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 }
3065 }
3066 else
3067 {
3068 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003069 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 newlen += mb_l;
3071 }
3072 }
3073
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003074 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076 else
3077#endif
3078 msg_outtrans_len(ccline.cmdbuff + start, len);
3079}
3080
3081/*
3082 * Put a character on the command line. Shifts the following text to the
3083 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3084 * "c" must be printable (fit in one display cell)!
3085 */
3086 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003087putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088{
3089 if (cmd_silent)
3090 return;
3091 msg_no_more = TRUE;
3092 msg_putchar(c);
3093 if (shift)
3094 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3095 msg_no_more = FALSE;
3096 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003097 extra_char = c;
3098 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099}
3100
3101/*
3102 * Undo a putcmdline(c, FALSE).
3103 */
3104 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003105unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106{
3107 if (cmd_silent)
3108 return;
3109 msg_no_more = TRUE;
3110 if (ccline.cmdlen == ccline.cmdpos)
3111 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003112#ifdef FEAT_MBYTE
3113 else if (has_mbyte)
3114 draw_cmdline(ccline.cmdpos,
3115 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 else
3118 draw_cmdline(ccline.cmdpos, 1);
3119 msg_no_more = FALSE;
3120 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003121 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122}
3123
3124/*
3125 * Put the given string, of the given length, onto the command line.
3126 * If len is -1, then STRLEN() is used to calculate the length.
3127 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3128 * part will be redrawn, otherwise it will not. If this function is called
3129 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3130 * called afterwards.
3131 */
3132 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003133put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134{
3135 int retval;
3136 int i;
3137 int m;
3138 int c;
3139
3140 if (len < 0)
3141 len = (int)STRLEN(str);
3142
3143 /* Check if ccline.cmdbuff needs to be longer */
3144 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003145 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 else
3147 retval = OK;
3148 if (retval == OK)
3149 {
3150 if (!ccline.overstrike)
3151 {
3152 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3153 ccline.cmdbuff + ccline.cmdpos,
3154 (size_t)(ccline.cmdlen - ccline.cmdpos));
3155 ccline.cmdlen += len;
3156 }
3157 else
3158 {
3159#ifdef FEAT_MBYTE
3160 if (has_mbyte)
3161 {
3162 /* Count nr of characters in the new string. */
3163 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003164 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 ++m;
3166 /* Count nr of bytes in cmdline that are overwritten by these
3167 * characters. */
3168 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003169 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 --m;
3171 if (i < ccline.cmdlen)
3172 {
3173 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3174 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3175 ccline.cmdlen += ccline.cmdpos + len - i;
3176 }
3177 else
3178 ccline.cmdlen = ccline.cmdpos + len;
3179 }
3180 else
3181#endif
3182 if (ccline.cmdpos + len > ccline.cmdlen)
3183 ccline.cmdlen = ccline.cmdpos + len;
3184 }
3185 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3186 ccline.cmdbuff[ccline.cmdlen] = NUL;
3187
3188#ifdef FEAT_MBYTE
3189 if (enc_utf8)
3190 {
3191 /* When the inserted text starts with a composing character,
3192 * backup to the character before it. There could be two of them.
3193 */
3194 i = 0;
3195 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3196 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3197 {
3198 i = (*mb_head_off)(ccline.cmdbuff,
3199 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3200 ccline.cmdpos -= i;
3201 len += i;
3202 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3203 }
3204# ifdef FEAT_ARABIC
3205 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3206 {
3207 /* Check the previous character for Arabic combining pair. */
3208 i = (*mb_head_off)(ccline.cmdbuff,
3209 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3210 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3211 + ccline.cmdpos - i), c))
3212 {
3213 ccline.cmdpos -= i;
3214 len += i;
3215 }
3216 else
3217 i = 0;
3218 }
3219# endif
3220 if (i != 0)
3221 {
3222 /* Also backup the cursor position. */
3223 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3224 ccline.cmdspos -= i;
3225 msg_col -= i;
3226 if (msg_col < 0)
3227 {
3228 msg_col += Columns;
3229 --msg_row;
3230 }
3231 }
3232 }
3233#endif
3234
3235 if (redraw && !cmd_silent)
3236 {
3237 msg_no_more = TRUE;
3238 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003239 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3241 /* Avoid clearing the rest of the line too often. */
3242 if (cmdline_row != i || ccline.overstrike)
3243 msg_clr_eos();
3244 msg_no_more = FALSE;
3245 }
3246#ifdef FEAT_FKMAP
3247 /*
3248 * If we are in Farsi command mode, the character input must be in
3249 * Insert mode. So do not advance the cmdpos.
3250 */
3251 if (!cmd_fkmap)
3252#endif
3253 {
3254 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003255 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003257 if (m < 0) /* overflow, Columns or Rows at weird value */
3258 m = MAXCOL;
3259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
3261 m = MAXCOL;
3262 for (i = 0; i < len; ++i)
3263 {
3264 c = cmdline_charsize(ccline.cmdpos);
3265#ifdef FEAT_MBYTE
3266 /* count ">" for a double-wide char that doesn't fit. */
3267 if (has_mbyte)
3268 correct_cmdspos(ccline.cmdpos, c);
3269#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003270 /* Stop cursor at the end of the screen, but do increment the
3271 * insert position, so that entering a very long command
3272 * works, even though you can't see it. */
3273 if (ccline.cmdspos + c < m)
3274 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef FEAT_MBYTE
3276 if (has_mbyte)
3277 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003278 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 if (c > len - i - 1)
3280 c = len - i - 1;
3281 ccline.cmdpos += c;
3282 i += c;
3283 }
3284#endif
3285 ++ccline.cmdpos;
3286 }
3287 }
3288 }
3289 if (redraw)
3290 msg_check();
3291 return retval;
3292}
3293
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003294static struct cmdline_info prev_ccline;
3295static int prev_ccline_used = FALSE;
3296
3297/*
3298 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3299 * and overwrite it. But get_cmdline_str() may need it, thus make it
3300 * available globally in prev_ccline.
3301 */
3302 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003303save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003304{
3305 if (!prev_ccline_used)
3306 {
3307 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3308 prev_ccline_used = TRUE;
3309 }
3310 *ccp = prev_ccline;
3311 prev_ccline = ccline;
3312 ccline.cmdbuff = NULL;
3313 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003314 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003315}
3316
3317/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003318 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003319 */
3320 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003321restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003322{
3323 ccline = prev_ccline;
3324 prev_ccline = *ccp;
3325}
3326
Bram Moolenaar5a305422006-04-28 22:38:25 +00003327#if defined(FEAT_EVAL) || defined(PROTO)
3328/*
3329 * Save the command line into allocated memory. Returns a pointer to be
3330 * passed to restore_cmdline_alloc() later.
3331 * Returns NULL when failed.
3332 */
3333 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003334save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003335{
3336 struct cmdline_info *p;
3337
3338 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3339 if (p != NULL)
3340 save_cmdline(p);
3341 return (char_u *)p;
3342}
3343
3344/*
3345 * Restore the command line from the return value of save_cmdline_alloc().
3346 */
3347 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003348restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003349{
3350 if (p != NULL)
3351 {
3352 restore_cmdline((struct cmdline_info *)p);
3353 vim_free(p);
3354 }
3355}
3356#endif
3357
Bram Moolenaar8299df92004-07-10 09:47:34 +00003358/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003359 * Paste a yank register into the command line.
3360 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003361 * insert_reg() can't be used here, because special characters from the
3362 * register contents will be interpreted as commands.
3363 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003364 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003365 */
3366 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003367cmdline_paste(
3368 int regname,
3369 int literally, /* Insert text literally instead of "as typed" */
3370 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003371{
3372 long i;
3373 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003374 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003375 int allocated;
3376 struct cmdline_info save_ccline;
3377
3378 /* check for valid regname; also accept special characters for CTRL-R in
3379 * the command line */
3380 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003381 && regname != Ctrl_A && regname != Ctrl_L
3382 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003383 return FAIL;
3384
3385 /* A register containing CTRL-R can cause an endless loop. Allow using
3386 * CTRL-C to break the loop. */
3387 line_breakcheck();
3388 if (got_int)
3389 return FAIL;
3390
3391#ifdef FEAT_CLIPBOARD
3392 regname = may_get_selection(regname);
3393#endif
3394
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003395 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003396 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003397 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003398 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003399 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003400 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003401 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003402
3403 if (i)
3404 {
3405 /* Got the value of a special register in "arg". */
3406 if (arg == NULL)
3407 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003408
3409 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3410 * part of the word. */
3411 p = arg;
3412 if (p_is && regname == Ctrl_W)
3413 {
3414 char_u *w;
3415 int len;
3416
3417 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003418 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003419 {
3420#ifdef FEAT_MBYTE
3421 if (has_mbyte)
3422 {
3423 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3424 if (!vim_iswordc(mb_ptr2char(w - len)))
3425 break;
3426 w -= len;
3427 }
3428 else
3429#endif
3430 {
3431 if (!vim_iswordc(w[-1]))
3432 break;
3433 --w;
3434 }
3435 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003436 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003437 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3438 p += len;
3439 }
3440
3441 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003442 if (allocated)
3443 vim_free(arg);
3444 return OK;
3445 }
3446
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003447 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003448}
3449
3450/*
3451 * Put a string on the command line.
3452 * When "literally" is TRUE, insert literally.
3453 * When "literally" is FALSE, insert as typed, but don't leave the command
3454 * line.
3455 */
3456 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003457cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003458{
3459 int c, cv;
3460
3461 if (literally)
3462 put_on_cmdline(s, -1, TRUE);
3463 else
3464 while (*s != NUL)
3465 {
3466 cv = *s;
3467 if (cv == Ctrl_V && s[1])
3468 ++s;
3469#ifdef FEAT_MBYTE
3470 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003471 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003472 else
3473#endif
3474 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003475 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3476 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003477#ifdef UNIX
3478 || c == intr_char
3479#endif
3480 || (c == Ctrl_BSL && *s == Ctrl_N))
3481 stuffcharReadbuff(Ctrl_V);
3482 stuffcharReadbuff(c);
3483 }
3484}
3485
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486#ifdef FEAT_WILDMENU
3487/*
3488 * Delete characters on the command line, from "from" to the current
3489 * position.
3490 */
3491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003492cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493{
3494 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3495 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3496 ccline.cmdlen -= ccline.cmdpos - from;
3497 ccline.cmdpos = from;
3498}
3499#endif
3500
3501/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003502 * This function is called when the screen size changes and with incremental
3503 * search and in other situations where the command line may have been
3504 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 */
3506 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003509 redrawcmdline_ex(TRUE);
3510}
3511
3512 void
3513redrawcmdline_ex(int do_compute_cmdrow)
3514{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 if (cmd_silent)
3516 return;
3517 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003518 if (do_compute_cmdrow)
3519 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 redrawcmd();
3521 cursorcmd();
3522}
3523
3524 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003525redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526{
3527 int i;
3528
3529 if (cmd_silent)
3530 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003531 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 msg_putchar(ccline.cmdfirstc);
3533 if (ccline.cmdprompt != NULL)
3534 {
3535 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3536 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3537 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003538 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 --ccline.cmdindent;
3540 }
3541 else
3542 for (i = ccline.cmdindent; i > 0; --i)
3543 msg_putchar(' ');
3544}
3545
3546/*
3547 * Redraw what is currently on the command line.
3548 */
3549 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003550redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551{
3552 if (cmd_silent)
3553 return;
3554
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003555 /* when 'incsearch' is set there may be no command line while redrawing */
3556 if (ccline.cmdbuff == NULL)
3557 {
3558 windgoto(cmdline_row, 0);
3559 msg_clr_eos();
3560 return;
3561 }
3562
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 msg_start();
3564 redrawcmdprompt();
3565
3566 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3567 msg_no_more = TRUE;
3568 draw_cmdline(0, ccline.cmdlen);
3569 msg_clr_eos();
3570 msg_no_more = FALSE;
3571
3572 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003573 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003574 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575
3576 /*
3577 * An emsg() before may have set msg_scroll. This is used in normal mode,
3578 * in cmdline mode we can reset them now.
3579 */
3580 msg_scroll = FALSE; /* next message overwrites cmdline */
3581
3582 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3583 * in cmdline mode */
3584 skip_redraw = FALSE;
3585}
3586
3587 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003588compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003590 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 cmdline_row = Rows - 1;
3592 else
3593 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003594 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595}
3596
3597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003598cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599{
3600 if (cmd_silent)
3601 return;
3602
3603#ifdef FEAT_RIGHTLEFT
3604 if (cmdmsg_rl)
3605 {
3606 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3607 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3608 if (msg_row <= 0)
3609 msg_row = Rows - 1;
3610 }
3611 else
3612#endif
3613 {
3614 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3615 msg_col = ccline.cmdspos % (int)Columns;
3616 if (msg_row >= Rows)
3617 msg_row = Rows - 1;
3618 }
3619
3620 windgoto(msg_row, msg_col);
3621#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003622 if (p_imst == IM_ON_THE_SPOT)
3623 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624#endif
3625#ifdef MCH_CURSOR_SHAPE
3626 mch_update_cursor();
3627#endif
3628}
3629
3630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003631gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632{
3633 msg_start();
3634#ifdef FEAT_RIGHTLEFT
3635 if (cmdmsg_rl)
3636 msg_col = Columns - 1;
3637 else
3638#endif
3639 msg_col = 0; /* always start in column 0 */
3640 if (clr) /* clear the bottom line(s) */
3641 msg_clr_eos(); /* will reset clear_cmdline */
3642 windgoto(cmdline_row, 0);
3643}
3644
3645/*
3646 * Check the word in front of the cursor for an abbreviation.
3647 * Called when the non-id character "c" has been entered.
3648 * When an abbreviation is recognized it is removed from the text with
3649 * backspaces and the replacement string is inserted, followed by "c".
3650 */
3651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003652ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003654 int spos = 0;
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3657 return FALSE;
3658
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003659 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3660 * Actually accepts any mark. */
3661 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3662 spos++;
3663 if (ccline.cmdlen - spos > 5
3664 && ccline.cmdbuff[spos] == '\''
3665 && ccline.cmdbuff[spos + 2] == ','
3666 && ccline.cmdbuff[spos + 3] == '\'')
3667 spos += 5;
3668 else
3669 /* check abbreviation from the beginning of the commandline */
3670 spos = 0;
3671
3672 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673}
3674
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003675#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3676 static int
3677#ifdef __BORLANDC__
3678_RTLENTRYF
3679#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003680sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003681{
3682 char_u *p1 = *(char_u **)s1;
3683 char_u *p2 = *(char_u **)s2;
3684
3685 if (*p1 != '<' && *p2 == '<') return -1;
3686 if (*p1 == '<' && *p2 != '<') return 1;
3687 return STRCMP(p1, p2);
3688}
3689#endif
3690
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691/*
3692 * Return FAIL if this is not an appropriate context in which to do
3693 * completion of anything, return OK if it is (even if there are no matches).
3694 * For the caller, this means that the character is just passed through like a
3695 * normal character (instead of being expanded). This allows :s/^I^D etc.
3696 */
3697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003698nextwild(
3699 expand_T *xp,
3700 int type,
3701 int options, /* extra options for ExpandOne() */
3702 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703{
3704 int i, j;
3705 char_u *p1;
3706 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 int difflen;
3708 int v;
3709
3710 if (xp->xp_numfiles == -1)
3711 {
3712 set_expand_context(xp);
3713 cmd_showtail = expand_showtail(xp);
3714 }
3715
3716 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3717 {
3718 beep_flush();
3719 return OK; /* Something illegal on command line */
3720 }
3721 if (xp->xp_context == EXPAND_NOTHING)
3722 {
3723 /* Caller can use the character as a normal char instead */
3724 return FAIL;
3725 }
3726
3727 MSG_PUTS("..."); /* show that we are busy */
3728 out_flush();
3729
3730 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003731 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732
3733 if (type == WILD_NEXT || type == WILD_PREV)
3734 {
3735 /*
3736 * Get next/previous match for a previous expanded pattern.
3737 */
3738 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3739 }
3740 else
3741 {
3742 /*
3743 * Translate string into pattern and expand it.
3744 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003745 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3746 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 p2 = NULL;
3748 else
3749 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003750 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003751 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3752 if (escape)
3753 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003754
3755 if (p_wic)
3756 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003757 p2 = ExpandOne(xp, p1,
3758 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003759 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003761 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 if (p2 != NULL && type == WILD_LONGEST)
3763 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003764 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (ccline.cmdbuff[i + j] == '*'
3766 || ccline.cmdbuff[i + j] == '?')
3767 break;
3768 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003769 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 }
3771 }
3772 }
3773
3774 if (p2 != NULL && !got_int)
3775 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003776 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003777 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003779 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 xp->xp_pattern = ccline.cmdbuff + i;
3781 }
3782 else
3783 v = OK;
3784 if (v == OK)
3785 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003786 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3787 &ccline.cmdbuff[ccline.cmdpos],
3788 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3789 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 ccline.cmdlen += difflen;
3791 ccline.cmdpos += difflen;
3792 }
3793 }
3794 vim_free(p2);
3795
3796 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003797 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 /* When expanding a ":map" command and no matches are found, assume that
3800 * the key is supposed to be inserted literally */
3801 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3802 return FAIL;
3803
3804 if (xp->xp_numfiles <= 0 && p2 == NULL)
3805 beep_flush();
3806 else if (xp->xp_numfiles == 1)
3807 /* free expanded pattern */
3808 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3809
3810 return OK;
3811}
3812
3813/*
3814 * Do wildcard expansion on the string 'str'.
3815 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003816 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 * Return NULL for failure.
3818 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003819 * "orig" is the originally expanded string, copied to allocated memory. It
3820 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3821 * WILD_PREV "orig" should be NULL.
3822 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003823 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3824 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 *
3826 * mode = WILD_FREE: just free previously expanded matches
3827 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3828 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3829 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3830 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3831 * mode = WILD_ALL: return all matches concatenated
3832 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003833 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 *
3835 * options = WILD_LIST_NOTFOUND: list entries without a match
3836 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3837 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3838 * options = WILD_NO_BEEP: Don't beep for multiple matches
3839 * options = WILD_ADD_SLASH: add a slash after directory names
3840 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3841 * options = WILD_SILENT: don't print warning messages
3842 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003843 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 *
3845 * The variables xp->xp_context and xp->xp_backslash must have been set!
3846 */
3847 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003848ExpandOne(
3849 expand_T *xp,
3850 char_u *str,
3851 char_u *orig, /* allocated copy of original of expanded string */
3852 int options,
3853 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 char_u *ss = NULL;
3856 static int findex;
3857 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003858 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 int i;
3860 long_u len;
3861 int non_suf_match; /* number without matching suffix */
3862
3863 /*
3864 * first handle the case of using an old match
3865 */
3866 if (mode == WILD_NEXT || mode == WILD_PREV)
3867 {
3868 if (xp->xp_numfiles > 0)
3869 {
3870 if (mode == WILD_PREV)
3871 {
3872 if (findex == -1)
3873 findex = xp->xp_numfiles;
3874 --findex;
3875 }
3876 else /* mode == WILD_NEXT */
3877 ++findex;
3878
3879 /*
3880 * When wrapping around, return the original string, set findex to
3881 * -1.
3882 */
3883 if (findex < 0)
3884 {
3885 if (orig_save == NULL)
3886 findex = xp->xp_numfiles - 1;
3887 else
3888 findex = -1;
3889 }
3890 if (findex >= xp->xp_numfiles)
3891 {
3892 if (orig_save == NULL)
3893 findex = 0;
3894 else
3895 findex = -1;
3896 }
3897#ifdef FEAT_WILDMENU
3898 if (p_wmnu)
3899 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
3900 findex, cmd_showtail);
3901#endif
3902 if (findex == -1)
3903 return vim_strsave(orig_save);
3904 return vim_strsave(xp->xp_files[findex]);
3905 }
3906 else
3907 return NULL;
3908 }
3909
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003910 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
3912 {
3913 FreeWild(xp->xp_numfiles, xp->xp_files);
3914 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01003915 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 }
3917 findex = 0;
3918
3919 if (mode == WILD_FREE) /* only release file name */
3920 return NULL;
3921
3922 if (xp->xp_numfiles == -1)
3923 {
3924 vim_free(orig_save);
3925 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00003926 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 /*
3929 * Do the expansion.
3930 */
3931 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
3932 options) == FAIL)
3933 {
3934#ifdef FNAME_ILLEGAL
3935 /* Illegal file name has been silently skipped. But when there
3936 * are wildcards, the real problem is that there was no match,
3937 * causing the pattern to be added, which has illegal characters.
3938 */
3939 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
3940 EMSG2(_(e_nomatch2), str);
3941#endif
3942 }
3943 else if (xp->xp_numfiles == 0)
3944 {
3945 if (!(options & WILD_SILENT))
3946 EMSG2(_(e_nomatch2), str);
3947 }
3948 else
3949 {
3950 /* Escape the matches for use on the command line. */
3951 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
3952
3953 /*
3954 * Check for matching suffixes in file names.
3955 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003956 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
3957 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 {
3959 if (xp->xp_numfiles)
3960 non_suf_match = xp->xp_numfiles;
3961 else
3962 non_suf_match = 1;
3963 if ((xp->xp_context == EXPAND_FILES
3964 || xp->xp_context == EXPAND_DIRECTORIES)
3965 && xp->xp_numfiles > 1)
3966 {
3967 /*
3968 * More than one match; check suffix.
3969 * The files will have been sorted on matching suffix in
3970 * expand_wildcards, only need to check the first two.
3971 */
3972 non_suf_match = 0;
3973 for (i = 0; i < 2; ++i)
3974 if (match_suffix(xp->xp_files[i]))
3975 ++non_suf_match;
3976 }
3977 if (non_suf_match != 1)
3978 {
3979 /* Can we ever get here unless it's while expanding
3980 * interactively? If not, we can get rid of this all
3981 * together. Don't really want to wait for this message
3982 * (and possibly have to hit return to continue!).
3983 */
3984 if (!(options & WILD_SILENT))
3985 EMSG(_(e_toomany));
3986 else if (!(options & WILD_NO_BEEP))
3987 beep_flush();
3988 }
3989 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
3990 ss = vim_strsave(xp->xp_files[0]);
3991 }
3992 }
3993 }
3994
3995 /* Find longest common part */
3996 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
3997 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01003998 int mb_len = 1;
3999 int c0, ci;
4000
4001 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004003#ifdef FEAT_MBYTE
4004 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004006 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4007 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4008 }
4009 else
4010#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004011 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004012 for (i = 1; i < xp->xp_numfiles; ++i)
4013 {
4014#ifdef FEAT_MBYTE
4015 if (has_mbyte)
4016 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4017 else
4018#endif
4019 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004020 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004022 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004023 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004025 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 break;
4027 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004028 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 break;
4030 }
4031 if (i < xp->xp_numfiles)
4032 {
4033 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004034 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035 break;
4036 }
4037 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004038
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 ss = alloc((unsigned)len + 1);
4040 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004041 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 findex = -1; /* next p_wc gets first one */
4043 }
4044
4045 /* Concatenate all matching names */
4046 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4047 {
4048 len = 0;
4049 for (i = 0; i < xp->xp_numfiles; ++i)
4050 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4051 ss = lalloc(len, TRUE);
4052 if (ss != NULL)
4053 {
4054 *ss = NUL;
4055 for (i = 0; i < xp->xp_numfiles; ++i)
4056 {
4057 STRCAT(ss, xp->xp_files[i]);
4058 if (i != xp->xp_numfiles - 1)
4059 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4060 }
4061 }
4062 }
4063
4064 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4065 ExpandCleanup(xp);
4066
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004067 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004068 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004069 vim_free(orig);
4070
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 return ss;
4072}
4073
4074/*
4075 * Prepare an expand structure for use.
4076 */
4077 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004078ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004080 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004081 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004083#ifndef BACKSLASH_IN_FILENAME
4084 xp->xp_shell = FALSE;
4085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 xp->xp_numfiles = -1;
4087 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004088#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4089 xp->xp_arg = NULL;
4090#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004091 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092}
4093
4094/*
4095 * Cleanup an expand structure after use.
4096 */
4097 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004098ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099{
4100 if (xp->xp_numfiles >= 0)
4101 {
4102 FreeWild(xp->xp_numfiles, xp->xp_files);
4103 xp->xp_numfiles = -1;
4104 }
4105}
4106
4107 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004108ExpandEscape(
4109 expand_T *xp,
4110 char_u *str,
4111 int numfiles,
4112 char_u **files,
4113 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114{
4115 int i;
4116 char_u *p;
4117
4118 /*
4119 * May change home directory back to "~"
4120 */
4121 if (options & WILD_HOME_REPLACE)
4122 tilde_replace(str, numfiles, files);
4123
4124 if (options & WILD_ESCAPE)
4125 {
4126 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004127 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004128 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 || xp->xp_context == EXPAND_BUFFERS
4130 || xp->xp_context == EXPAND_DIRECTORIES)
4131 {
4132 /*
4133 * Insert a backslash into a file name before a space, \, %, #
4134 * and wildmatch characters, except '~'.
4135 */
4136 for (i = 0; i < numfiles; ++i)
4137 {
4138 /* for ":set path=" we need to escape spaces twice */
4139 if (xp->xp_backslash == XP_BS_THREE)
4140 {
4141 p = vim_strsave_escaped(files[i], (char_u *)" ");
4142 if (p != NULL)
4143 {
4144 vim_free(files[i]);
4145 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004146#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 p = vim_strsave_escaped(files[i], (char_u *)" ");
4148 if (p != NULL)
4149 {
4150 vim_free(files[i]);
4151 files[i] = p;
4152 }
4153#endif
4154 }
4155 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004156#ifdef BACKSLASH_IN_FILENAME
4157 p = vim_strsave_fnameescape(files[i], FALSE);
4158#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004159 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 if (p != NULL)
4162 {
4163 vim_free(files[i]);
4164 files[i] = p;
4165 }
4166
4167 /* If 'str' starts with "\~", replace "~" at start of
4168 * files[i] with "\~". */
4169 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004170 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004173
4174 /* If the first file starts with a '+' escape it. Otherwise it
4175 * could be seen as "+cmd". */
4176 if (*files[0] == '+')
4177 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 else if (xp->xp_context == EXPAND_TAGS)
4180 {
4181 /*
4182 * Insert a backslash before characters in a tag name that
4183 * would terminate the ":tag" command.
4184 */
4185 for (i = 0; i < numfiles; ++i)
4186 {
4187 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4188 if (p != NULL)
4189 {
4190 vim_free(files[i]);
4191 files[i] = p;
4192 }
4193 }
4194 }
4195 }
4196}
4197
4198/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004199 * Escape special characters in "fname" for when used as a file name argument
4200 * after a Vim command, or, when "shell" is non-zero, a shell command.
4201 * Returns the result in allocated memory.
4202 */
4203 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004204vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004205{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004206 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004207#ifdef BACKSLASH_IN_FILENAME
4208 char_u buf[20];
4209 int j = 0;
4210
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004211 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004212 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004213 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004214 buf[j++] = *p;
4215 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004216 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004217#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004218 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4219 if (shell && csh_like_shell() && p != NULL)
4220 {
4221 char_u *s;
4222
4223 /* For csh and similar shells need to put two backslashes before '!'.
4224 * One is taken by Vim, one by the shell. */
4225 s = vim_strsave_escaped(p, (char_u *)"!");
4226 vim_free(p);
4227 p = s;
4228 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004229#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004230
4231 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4232 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004233 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004234 escape_fname(&p);
4235
4236 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004237}
4238
4239/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004240 * Put a backslash before the file name in "pp", which is in allocated memory.
4241 */
4242 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004243escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004244{
4245 char_u *p;
4246
4247 p = alloc((unsigned)(STRLEN(*pp) + 2));
4248 if (p != NULL)
4249 {
4250 p[0] = '\\';
4251 STRCPY(p + 1, *pp);
4252 vim_free(*pp);
4253 *pp = p;
4254 }
4255}
4256
4257/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 * For each file name in files[num_files]:
4259 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4260 */
4261 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004262tilde_replace(
4263 char_u *orig_pat,
4264 int num_files,
4265 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
4267 int i;
4268 char_u *p;
4269
4270 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4271 {
4272 for (i = 0; i < num_files; ++i)
4273 {
4274 p = home_replace_save(NULL, files[i]);
4275 if (p != NULL)
4276 {
4277 vim_free(files[i]);
4278 files[i] = p;
4279 }
4280 }
4281 }
4282}
4283
4284/*
4285 * Show all matches for completion on the command line.
4286 * Returns EXPAND_NOTHING when the character that triggered expansion should
4287 * be inserted like a normal character.
4288 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004290showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291{
4292#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4293 int num_files;
4294 char_u **files_found;
4295 int i, j, k;
4296 int maxlen;
4297 int lines;
4298 int columns;
4299 char_u *p;
4300 int lastlen;
4301 int attr;
4302 int showtail;
4303
4304 if (xp->xp_numfiles == -1)
4305 {
4306 set_expand_context(xp);
4307 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4308 &num_files, &files_found);
4309 showtail = expand_showtail(xp);
4310 if (i != EXPAND_OK)
4311 return i;
4312
4313 }
4314 else
4315 {
4316 num_files = xp->xp_numfiles;
4317 files_found = xp->xp_files;
4318 showtail = cmd_showtail;
4319 }
4320
4321#ifdef FEAT_WILDMENU
4322 if (!wildmenu)
4323 {
4324#endif
4325 msg_didany = FALSE; /* lines_left will be set */
4326 msg_start(); /* prepare for paging */
4327 msg_putchar('\n');
4328 out_flush();
4329 cmdline_row = msg_row;
4330 msg_didany = FALSE; /* lines_left will be set again */
4331 msg_start(); /* prepare for paging */
4332#ifdef FEAT_WILDMENU
4333 }
4334#endif
4335
4336 if (got_int)
4337 got_int = FALSE; /* only int. the completion, not the cmd line */
4338#ifdef FEAT_WILDMENU
4339 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004340 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341#endif
4342 else
4343 {
4344 /* find the length of the longest file name */
4345 maxlen = 0;
4346 for (i = 0; i < num_files; ++i)
4347 {
4348 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004349 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 || xp->xp_context == EXPAND_BUFFERS))
4351 {
4352 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4353 j = vim_strsize(NameBuff);
4354 }
4355 else
4356 j = vim_strsize(L_SHOWFILE(i));
4357 if (j > maxlen)
4358 maxlen = j;
4359 }
4360
4361 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4362 lines = num_files;
4363 else
4364 {
4365 /* compute the number of columns and lines for the listing */
4366 maxlen += 2; /* two spaces between file names */
4367 columns = ((int)Columns + 2) / maxlen;
4368 if (columns < 1)
4369 columns = 1;
4370 lines = (num_files + columns - 1) / columns;
4371 }
4372
Bram Moolenaar8820b482017-03-16 17:23:31 +01004373 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374
4375 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4376 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004377 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 msg_clr_eos();
4379 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004380 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 }
4382
4383 /* list the files line by line */
4384 for (i = 0; i < lines; ++i)
4385 {
4386 lastlen = 999;
4387 for (k = i; k < num_files; k += lines)
4388 {
4389 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4390 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004391 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 p = files_found[k] + STRLEN(files_found[k]) + 1;
4393 msg_advance(maxlen + 1);
4394 msg_puts(p);
4395 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004396 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398 }
4399 for (j = maxlen - lastlen; --j >= 0; )
4400 msg_putchar(' ');
4401 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004402 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 || xp->xp_context == EXPAND_BUFFERS)
4404 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004405 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004406 if (xp->xp_numfiles != -1)
4407 {
4408 char_u *halved_slash;
4409 char_u *exp_path;
4410
4411 /* Expansion was done before and special characters
4412 * were escaped, need to halve backslashes. Also
4413 * $HOME has been replaced with ~/. */
4414 exp_path = expand_env_save_opt(files_found[k], TRUE);
4415 halved_slash = backslash_halve_save(
4416 exp_path != NULL ? exp_path : files_found[k]);
4417 j = mch_isdir(halved_slash != NULL ? halved_slash
4418 : files_found[k]);
4419 vim_free(exp_path);
4420 vim_free(halved_slash);
4421 }
4422 else
4423 /* Expansion was done here, file names are literal. */
4424 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (showtail)
4426 p = L_SHOWFILE(k);
4427 else
4428 {
4429 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4430 TRUE);
4431 p = NameBuff;
4432 }
4433 }
4434 else
4435 {
4436 j = FALSE;
4437 p = L_SHOWFILE(k);
4438 }
4439 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4440 }
4441 if (msg_col > 0) /* when not wrapped around */
4442 {
4443 msg_clr_eos();
4444 msg_putchar('\n');
4445 }
4446 out_flush(); /* show one line at a time */
4447 if (got_int)
4448 {
4449 got_int = FALSE;
4450 break;
4451 }
4452 }
4453
4454 /*
4455 * we redraw the command below the lines that we have just listed
4456 * This is a bit tricky, but it saves a lot of screen updating.
4457 */
4458 cmdline_row = msg_row; /* will put it back later */
4459 }
4460
4461 if (xp->xp_numfiles == -1)
4462 FreeWild(num_files, files_found);
4463
4464 return EXPAND_OK;
4465}
4466
4467/*
4468 * Private gettail for showmatches() (and win_redr_status_matches()):
4469 * Find tail of file name path, but ignore trailing "/".
4470 */
4471 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004472sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
4474 char_u *p;
4475 char_u *t = s;
4476 int had_sep = FALSE;
4477
4478 for (p = s; *p != NUL; )
4479 {
4480 if (vim_ispathsep(*p)
4481#ifdef BACKSLASH_IN_FILENAME
4482 && !rem_backslash(p)
4483#endif
4484 )
4485 had_sep = TRUE;
4486 else if (had_sep)
4487 {
4488 t = p;
4489 had_sep = FALSE;
4490 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004491 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 }
4493 return t;
4494}
4495
4496/*
4497 * Return TRUE if we only need to show the tail of completion matches.
4498 * When not completing file names or there is a wildcard in the path FALSE is
4499 * returned.
4500 */
4501 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004502expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503{
4504 char_u *s;
4505 char_u *end;
4506
4507 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004508 if (xp->xp_context != EXPAND_FILES
4509 && xp->xp_context != EXPAND_SHELLCMD
4510 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 return FALSE;
4512
4513 end = gettail(xp->xp_pattern);
4514 if (end == xp->xp_pattern) /* there is no path separator */
4515 return FALSE;
4516
4517 for (s = xp->xp_pattern; s < end; s++)
4518 {
4519 /* Skip escaped wildcards. Only when the backslash is not a path
4520 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4521 if (rem_backslash(s))
4522 ++s;
4523 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4524 return FALSE;
4525 }
4526 return TRUE;
4527}
4528
4529/*
4530 * Prepare a string for expansion.
4531 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004532 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 * When expanding other names: The string will be used with regcomp(). Copy
4534 * the name into allocated memory and prepend "^".
4535 */
4536 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004537addstar(
4538 char_u *fname,
4539 int len,
4540 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541{
4542 char_u *retval;
4543 int i, j;
4544 int new_len;
4545 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004546 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004548 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004549 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004550 && context != EXPAND_SHELLCMD
4551 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 {
4553 /*
4554 * Matching will be done internally (on something other than files).
4555 * So we convert the file-matching-type wildcards into our kind for
4556 * use with vim_regcomp(). First work out how long it will be:
4557 */
4558
4559 /* For help tags the translation is done in find_help_tags().
4560 * For a tag pattern starting with "/" no translation is needed. */
4561 if (context == EXPAND_HELP
4562 || context == EXPAND_COLORS
4563 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004564 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004565 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004566 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004567 || ((context == EXPAND_TAGS_LISTFILES
4568 || context == EXPAND_TAGS)
4569 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 retval = vim_strnsave(fname, len);
4571 else
4572 {
4573 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4574 for (i = 0; i < len; i++)
4575 {
4576 if (fname[i] == '*' || fname[i] == '~')
4577 new_len++; /* '*' needs to be replaced by ".*"
4578 '~' needs to be replaced by "\~" */
4579
4580 /* Buffer names are like file names. "." should be literal */
4581 if (context == EXPAND_BUFFERS && fname[i] == '.')
4582 new_len++; /* "." becomes "\." */
4583
4584 /* Custom expansion takes care of special things, match
4585 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004586 if ((context == EXPAND_USER_DEFINED
4587 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 new_len++; /* '\' becomes "\\" */
4589 }
4590 retval = alloc(new_len);
4591 if (retval != NULL)
4592 {
4593 retval[0] = '^';
4594 j = 1;
4595 for (i = 0; i < len; i++, j++)
4596 {
4597 /* Skip backslash. But why? At least keep it for custom
4598 * expansion. */
4599 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004600 && context != EXPAND_USER_LIST
4601 && fname[i] == '\\'
4602 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 break;
4604
4605 switch (fname[i])
4606 {
4607 case '*': retval[j++] = '.';
4608 break;
4609 case '~': retval[j++] = '\\';
4610 break;
4611 case '?': retval[j] = '.';
4612 continue;
4613 case '.': if (context == EXPAND_BUFFERS)
4614 retval[j++] = '\\';
4615 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004616 case '\\': if (context == EXPAND_USER_DEFINED
4617 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 retval[j++] = '\\';
4619 break;
4620 }
4621 retval[j] = fname[i];
4622 }
4623 retval[j] = NUL;
4624 }
4625 }
4626 }
4627 else
4628 {
4629 retval = alloc(len + 4);
4630 if (retval != NULL)
4631 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004632 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633
4634 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004635 * Don't add a star to *, ~, ~user, $var or `cmd`.
4636 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 * ~ would be at the start of the file name, but not the tail.
4638 * $ could be anywhere in the tail.
4639 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004640 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 */
4642 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004643 ends_in_star = (len > 0 && retval[len - 1] == '*');
4644#ifndef BACKSLASH_IN_FILENAME
4645 for (i = len - 2; i >= 0; --i)
4646 {
4647 if (retval[i] != '\\')
4648 break;
4649 ends_in_star = !ends_in_star;
4650 }
4651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004653 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 && vim_strchr(tail, '$') == NULL
4655 && vim_strchr(retval, '`') == NULL)
4656 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004657 else if (len > 0 && retval[len - 1] == '$')
4658 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 retval[len] = NUL;
4660 }
4661 }
4662 return retval;
4663}
4664
4665/*
4666 * Must parse the command line so far to work out what context we are in.
4667 * Completion can then be done based on that context.
4668 * This routine sets the variables:
4669 * xp->xp_pattern The start of the pattern to be expanded within
4670 * the command line (ends at the cursor).
4671 * xp->xp_context The type of thing to expand. Will be one of:
4672 *
4673 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4674 * the command line, like an unknown command. Caller
4675 * should beep.
4676 * EXPAND_NOTHING Unrecognised context for completion, use char like
4677 * a normal char, rather than for completion. eg
4678 * :s/^I/
4679 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4680 * it.
4681 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4682 * EXPAND_FILES After command with XFILE set, or after setting
4683 * with P_EXPAND set. eg :e ^I, :w>>^I
4684 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4685 * when we know only directories are of interest. eg
4686 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004687 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4689 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4690 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4691 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4692 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4693 * EXPAND_EVENTS Complete event names
4694 * EXPAND_SYNTAX Complete :syntax command arguments
4695 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4696 * EXPAND_AUGROUP Complete autocommand group names
4697 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4698 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4699 * eg :unmap a^I , :cunab x^I
4700 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4701 * eg :call sub^I
4702 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4703 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4704 * names in expressions, eg :while s^I
4705 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004706 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 */
4708 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004709set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004711 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 if (ccline.cmdfirstc != ':'
4713#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004714 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004715 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716#endif
4717 )
4718 {
4719 xp->xp_context = EXPAND_NOTHING;
4720 return;
4721 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004722 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723}
4724
4725 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004726set_cmd_context(
4727 expand_T *xp,
4728 char_u *str, /* start of command line */
4729 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004730 int col, /* position of cursor */
4731 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732{
4733 int old_char = NUL;
4734 char_u *nextcomm;
4735
4736 /*
4737 * Avoid a UMR warning from Purify, only save the character if it has been
4738 * written before.
4739 */
4740 if (col < len)
4741 old_char = str[col];
4742 str[col] = NUL;
4743 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004744
4745#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004746 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004747 {
4748# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004749 /* pass CMD_SIZE because there is no real command */
4750 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004751# endif
4752 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004753 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004754 {
4755 xp->xp_context = ccline.xp_context;
4756 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004757# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004758 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004759# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004760 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004761 else
4762#endif
4763 while (nextcomm != NULL)
4764 nextcomm = set_one_cmd_context(xp, nextcomm);
4765
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004766 /* Store the string here so that call_user_expand_func() can get to them
4767 * easily. */
4768 xp->xp_line = str;
4769 xp->xp_col = col;
4770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 str[col] = old_char;
4772}
4773
4774/*
4775 * Expand the command line "str" from context "xp".
4776 * "xp" must have been set by set_cmd_context().
4777 * xp->xp_pattern points into "str", to where the text that is to be expanded
4778 * starts.
4779 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4780 * cursor.
4781 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4782 * key that triggered expansion literally.
4783 * Returns EXPAND_OK otherwise.
4784 */
4785 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004786expand_cmdline(
4787 expand_T *xp,
4788 char_u *str, /* start of command line */
4789 int col, /* position of cursor */
4790 int *matchcount, /* return: nr of matches */
4791 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
4793 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004794 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795
4796 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4797 {
4798 beep_flush();
4799 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4800 }
4801 if (xp->xp_context == EXPAND_NOTHING)
4802 {
4803 /* Caller can use the character as a normal char instead */
4804 return EXPAND_NOTHING;
4805 }
4806
4807 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004808 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4809 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 if (file_str == NULL)
4811 return EXPAND_UNSUCCESSFUL;
4812
Bram Moolenaar94950a92010-12-02 16:01:29 +01004813 if (p_wic)
4814 options += WILD_ICASE;
4815
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004817 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 *matchcount = 0;
4820 *matches = NULL;
4821 }
4822 vim_free(file_str);
4823
4824 return EXPAND_OK;
4825}
4826
4827#ifdef FEAT_MULTI_LANG
4828/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004829 * Cleanup matches for help tags:
4830 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4831 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004833static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834
4835 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004836cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837{
4838 int i, j;
4839 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004840 char_u buf[4];
4841 char_u *p = buf;
4842
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004843 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004844 {
4845 *p++ = '@';
4846 *p++ = p_hlg[0];
4847 *p++ = p_hlg[1];
4848 }
4849 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 for (i = 0; i < num_file; ++i)
4852 {
4853 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004854 if (len <= 0)
4855 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004856 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
4858 /* Sorting on priority means the same item in another language may
4859 * be anywhere. Search all items for a match up to the "@en". */
4860 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004861 if (j != i && (int)STRLEN(file[j]) == len + 3
4862 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 break;
4864 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004865 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 file[i][len] = NUL;
4867 }
4868 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004869
4870 if (*buf != NUL)
4871 for (i = 0; i < num_file; ++i)
4872 {
4873 len = (int)STRLEN(file[i]) - 3;
4874 if (len <= 0)
4875 continue;
4876 if (STRCMP(file[i] + len, buf) == 0)
4877 {
4878 /* remove the default language */
4879 file[i][len] = NUL;
4880 }
4881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882}
4883#endif
4884
4885/*
4886 * Do the expansion based on xp->xp_context and "pat".
4887 */
4888 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004889ExpandFromContext(
4890 expand_T *xp,
4891 char_u *pat,
4892 int *num_file,
4893 char_u ***file,
4894 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895{
4896#ifdef FEAT_CMDL_COMPL
4897 regmatch_T regmatch;
4898#endif
4899 int ret;
4900 int flags;
4901
4902 flags = EW_DIR; /* include directories */
4903 if (options & WILD_LIST_NOTFOUND)
4904 flags |= EW_NOTFOUND;
4905 if (options & WILD_ADD_SLASH)
4906 flags |= EW_ADDSLASH;
4907 if (options & WILD_KEEP_ALL)
4908 flags |= EW_KEEPALL;
4909 if (options & WILD_SILENT)
4910 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01004911 if (options & WILD_ALLLINKS)
4912 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004914 if (xp->xp_context == EXPAND_FILES
4915 || xp->xp_context == EXPAND_DIRECTORIES
4916 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 {
4918 /*
4919 * Expand file or directory names.
4920 */
4921 int free_pat = FALSE;
4922 int i;
4923
4924 /* for ":set path=" and ":set tags=" halve backslashes for escaped
4925 * space */
4926 if (xp->xp_backslash != XP_BS_NONE)
4927 {
4928 free_pat = TRUE;
4929 pat = vim_strsave(pat);
4930 for (i = 0; pat[i]; ++i)
4931 if (pat[i] == '\\')
4932 {
4933 if (xp->xp_backslash == XP_BS_THREE
4934 && pat[i + 1] == '\\'
4935 && pat[i + 2] == '\\'
4936 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004937 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (xp->xp_backslash == XP_BS_ONE
4939 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00004940 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 }
4943
4944 if (xp->xp_context == EXPAND_FILES)
4945 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004946 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
4947 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 else
4949 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004950 if (options & WILD_ICASE)
4951 flags |= EW_ICASE;
4952
Bram Moolenaard7834d32009-12-02 16:14:36 +00004953 /* Expand wildcards, supporting %:h and the like. */
4954 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 if (free_pat)
4956 vim_free(pat);
4957 return ret;
4958 }
4959
4960 *file = (char_u **)"";
4961 *num_file = 0;
4962 if (xp->xp_context == EXPAND_HELP)
4963 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00004964 /* With an empty argument we would get all the help tags, which is
4965 * very slow. Get matches for "help" instead. */
4966 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
4967 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969#ifdef FEAT_MULTI_LANG
4970 cleanup_help_tags(*num_file, *file);
4971#endif
4972 return OK;
4973 }
4974 return FAIL;
4975 }
4976
4977#ifndef FEAT_CMDL_COMPL
4978 return FAIL;
4979#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004980 if (xp->xp_context == EXPAND_SHELLCMD)
4981 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 if (xp->xp_context == EXPAND_OLD_SETTING)
4983 return ExpandOldSetting(num_file, file);
4984 if (xp->xp_context == EXPAND_BUFFERS)
4985 return ExpandBufnames(pat, num_file, file, options);
4986 if (xp->xp_context == EXPAND_TAGS
4987 || xp->xp_context == EXPAND_TAGS_LISTFILES)
4988 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
4989 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004990 {
4991 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004992 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
4993 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004996 {
Bram Moolenaara627c962011-09-30 16:23:32 +02004997 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01004998 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02004999 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005000 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005001 {
5002 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005003 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005004 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005005 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005006 {
5007 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005008 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005009 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005010# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5011 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005012 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005013# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005014 if (xp->xp_context == EXPAND_PACKADD)
5015 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5018 if (regmatch.regprog == NULL)
5019 return FAIL;
5020
5021 /* set ignore-case according to p_ic, p_scs and pat */
5022 regmatch.rm_ic = ignorecase(pat);
5023
5024 if (xp->xp_context == EXPAND_SETTINGS
5025 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5026 ret = ExpandSettings(xp, &regmatch, num_file, file);
5027 else if (xp->xp_context == EXPAND_MAPPINGS)
5028 ret = ExpandMappings(&regmatch, num_file, file);
5029# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5030 else if (xp->xp_context == EXPAND_USER_DEFINED)
5031 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5032# endif
5033 else
5034 {
5035 static struct expgen
5036 {
5037 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005038 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005040 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 } tab[] =
5042 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005043 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5044 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005045 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005046 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005047#ifdef FEAT_CMDHIST
5048 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5049#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005051 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005052 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005053 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5054 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5055 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056#endif
5057#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005058 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5059 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5060 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5061 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#endif
5063#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005064 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5065 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066#endif
5067#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005068 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005070#ifdef FEAT_PROFILE
5071 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5072#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005073 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005074 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5075 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005076#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005077 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005078#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005079#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005080 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005081#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005082#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005083 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5086 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005087 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5088 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005090 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005091 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005092 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 };
5094 int i;
5095
5096 /*
5097 * Find a context in the table and call the ExpandGeneric() with the
5098 * right function to do the expansion.
5099 */
5100 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005101 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 if (xp->xp_context == tab[i].context)
5103 {
5104 if (tab[i].ic)
5105 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005106 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005107 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 break;
5109 }
5110 }
5111
Bram Moolenaar473de612013-06-08 18:19:48 +02005112 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 return ret;
5115#endif /* FEAT_CMDL_COMPL */
5116}
5117
5118#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5119/*
5120 * Expand a list of names.
5121 *
5122 * Generic function for command line completion. It calls a function to
5123 * obtain strings, one by one. The strings are matched against a regexp
5124 * program. Matching strings are copied into an array, which is returned.
5125 *
5126 * Returns OK when no problems encountered, FAIL for error (out of memory).
5127 */
5128 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005129ExpandGeneric(
5130 expand_T *xp,
5131 regmatch_T *regmatch,
5132 int *num_file,
5133 char_u ***file,
5134 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005136 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137{
5138 int i;
5139 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005140 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 char_u *str;
5142
5143 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005144 * round == 0: count the number of matching names
5145 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005147 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 {
5149 for (i = 0; ; ++i)
5150 {
5151 str = (*func)(xp, i);
5152 if (str == NULL) /* end of list */
5153 break;
5154 if (*str == NUL) /* skip empty strings */
5155 continue;
5156
5157 if (vim_regexec(regmatch, str, (colnr_T)0))
5158 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005159 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005161 if (escaped)
5162 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5163 else
5164 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 (*file)[count] = str;
5166#ifdef FEAT_MENU
5167 if (func == get_menu_names && str != NULL)
5168 {
5169 /* test for separator added by get_menu_names() */
5170 str += STRLEN(str) - 1;
5171 if (*str == '\001')
5172 *str = '.';
5173 }
5174#endif
5175 }
5176 ++count;
5177 }
5178 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005179 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
5181 if (count == 0)
5182 return OK;
5183 *num_file = count;
5184 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5185 if (*file == NULL)
5186 {
5187 *file = (char_u **)"";
5188 return FAIL;
5189 }
5190 count = 0;
5191 }
5192 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005193
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005194 /* Sort the results. Keep menu's in the specified order. */
5195 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005196 {
5197 if (xp->xp_context == EXPAND_EXPRESSION
5198 || xp->xp_context == EXPAND_FUNCTIONS
5199 || xp->xp_context == EXPAND_USER_FUNC)
5200 /* <SNR> functions should be sorted to the end. */
5201 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5202 sort_func_compare);
5203 else
5204 sort_strings(*file, *num_file);
5205 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005206
Bram Moolenaar4f688582007-07-24 12:34:30 +00005207#ifdef FEAT_CMDL_COMPL
5208 /* Reset the variables used for special highlight names expansion, so that
5209 * they don't show up when getting normal highlight names by ID. */
5210 reset_expand_highlight();
5211#endif
5212
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 return OK;
5214}
5215
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005216/*
5217 * Complete a shell command.
5218 * Returns FAIL or OK;
5219 */
5220 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005221expand_shellcmd(
5222 char_u *filepat, /* pattern to match with command names */
5223 int *num_file, /* return: number of matches */
5224 char_u ***file, /* return: array with matches */
5225 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005226{
5227 char_u *pat;
5228 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005229 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005230 int mustfree = FALSE;
5231 garray_T ga;
5232 char_u *buf = alloc(MAXPATHL);
5233 size_t l;
5234 char_u *s, *e;
5235 int flags = flagsarg;
5236 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005237 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005238 hashtab_T found_ht;
5239 hashitem_T *hi;
5240 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005241
5242 if (buf == NULL)
5243 return FAIL;
5244
5245 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5246 * space */
5247 pat = vim_strsave(filepat);
5248 for (i = 0; pat[i]; ++i)
5249 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005250 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005251
Bram Moolenaarb5971142015-03-21 17:32:19 +01005252 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005253
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005254 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5255 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005256 path = (char_u *)".";
5257 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005258 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005259 /* For an absolute name we don't use $PATH. */
5260 if (!mch_isFullName(pat))
5261 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005262 if (path == NULL)
5263 path = (char_u *)"";
5264 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005265
5266 /*
5267 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005268 * collect them in "ga". When "." is not in $PATH also expand for the
5269 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005270 */
5271 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005272 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005273 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005274 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005275#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005276 e = vim_strchr(s, ';');
5277#else
5278 e = vim_strchr(s, ':');
5279#endif
5280 if (e == NULL)
5281 e = s + STRLEN(s);
5282
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005283 if (*s == NUL)
5284 {
5285 if (did_curdir)
5286 break;
5287 // Find directories in the current directory, path is empty.
5288 did_curdir = TRUE;
5289 flags |= EW_DIR;
5290 }
5291 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5292 {
5293 did_curdir = TRUE;
5294 flags |= EW_DIR;
5295 }
5296 else
5297 // Do not match directories inside a $PATH item.
5298 flags &= ~EW_DIR;
5299
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005300 l = e - s;
5301 if (l > MAXPATHL - 5)
5302 break;
5303 vim_strncpy(buf, s, l);
5304 add_pathsep(buf);
5305 l = STRLEN(buf);
5306 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5307
5308 /* Expand matches in one directory of $PATH. */
5309 ret = expand_wildcards(1, &buf, num_file, file, flags);
5310 if (ret == OK)
5311 {
5312 if (ga_grow(&ga, *num_file) == FAIL)
5313 FreeWild(*num_file, *file);
5314 else
5315 {
5316 for (i = 0; i < *num_file; ++i)
5317 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005318 char_u *name = (*file)[i];
5319
5320 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005321 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005322 // Check if this name was already found.
5323 hash = hash_hash(name + l);
5324 hi = hash_lookup(&found_ht, name + l, hash);
5325 if (HASHITEM_EMPTY(hi))
5326 {
5327 // Remove the path that was prepended.
5328 STRMOVE(name, name + l);
5329 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5330 hash_add_item(&found_ht, hi, name, hash);
5331 name = NULL;
5332 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005333 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005334 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005335 }
5336 vim_free(*file);
5337 }
5338 }
5339 if (*e != NUL)
5340 ++e;
5341 }
5342 *file = ga.ga_data;
5343 *num_file = ga.ga_len;
5344
5345 vim_free(buf);
5346 vim_free(pat);
5347 if (mustfree)
5348 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005349 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005350 return OK;
5351}
5352
5353
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5355/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005356 * Call "user_expand_func()" to invoke a user defined Vim script function and
5357 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005359 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005360call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005361 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362 expand_T *xp,
5363 int *num_file,
5364 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005366 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005367 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005369 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005370 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005371 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
Bram Moolenaarb7515462013-06-29 12:58:33 +02005373 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005374 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 *num_file = 0;
5376 *file = NULL;
5377
Bram Moolenaarb7515462013-06-29 12:58:33 +02005378 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005379 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005380 keep = ccline.cmdbuff[ccline.cmdlen];
5381 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005382 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005383
Bram Moolenaarffa96842018-06-12 22:05:14 +02005384 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5385
5386 args[0].v_type = VAR_STRING;
5387 args[0].vval.v_string = pat;
5388 args[1].v_type = VAR_STRING;
5389 args[1].vval.v_string = xp->xp_line;
5390 args[2].v_type = VAR_NUMBER;
5391 args[2].vval.v_number = xp->xp_col;
5392 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005394 /* Save the cmdline, we don't know what the function may do. */
5395 save_ccline = ccline;
5396 ccline.cmdbuff = NULL;
5397 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005399
Bram Moolenaarded27a12018-08-01 19:06:03 +02005400 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005401
5402 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005404 if (ccline.cmdbuff != NULL)
5405 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005406
Bram Moolenaarffa96842018-06-12 22:05:14 +02005407 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005408 return ret;
5409}
5410
5411/*
5412 * Expand names with a function defined by the user.
5413 */
5414 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005415ExpandUserDefined(
5416 expand_T *xp,
5417 regmatch_T *regmatch,
5418 int *num_file,
5419 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005420{
5421 char_u *retstr;
5422 char_u *s;
5423 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005424 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005425 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005426 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005427
5428 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5429 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 return FAIL;
5431
5432 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005433 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 e = vim_strchr(s, '\n');
5436 if (e == NULL)
5437 e = s + STRLEN(s);
5438 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005439 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005441 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5442 *e = keep;
5443
5444 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005446 if (ga_grow(&ga, 1) == FAIL)
5447 break;
5448 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5449 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 }
5451
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 if (*e != NUL)
5453 ++e;
5454 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005455 vim_free(retstr);
5456 *file = ga.ga_data;
5457 *num_file = ga.ga_len;
5458 return OK;
5459}
5460
5461/*
5462 * Expand names with a list returned by a function defined by the user.
5463 */
5464 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005465ExpandUserList(
5466 expand_T *xp,
5467 int *num_file,
5468 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005469{
5470 list_T *retlist;
5471 listitem_T *li;
5472 garray_T ga;
5473
5474 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5475 if (retlist == NULL)
5476 return FAIL;
5477
5478 ga_init2(&ga, (int)sizeof(char *), 3);
5479 /* Loop over the items in the list. */
5480 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5481 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005482 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5483 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005484
5485 if (ga_grow(&ga, 1) == FAIL)
5486 break;
5487
5488 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005489 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005490 ++ga.ga_len;
5491 }
5492 list_unref(retlist);
5493
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 *file = ga.ga_data;
5495 *num_file = ga.ga_len;
5496 return OK;
5497}
5498#endif
5499
5500/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005501 * Expand color scheme, compiler or filetype names.
5502 * Search from 'runtimepath':
5503 * 'runtimepath'/{dirnames}/{pat}.vim
5504 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5505 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5506 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5507 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005508 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
5510 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511ExpandRTDir(
5512 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005513 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005514 int *num_file,
5515 char_u ***file,
5516 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 char_u *s;
5519 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005520 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005522 int i;
5523 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 *num_file = 0;
5526 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005527 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005528 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005530 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005532 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5533 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005535 ga_clear_strings(&ga);
5536 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005538 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005539 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005540 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005542
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005543 if (flags & DIP_START) {
5544 for (i = 0; dirnames[i] != NULL; ++i)
5545 {
5546 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5547 if (s == NULL)
5548 {
5549 ga_clear_strings(&ga);
5550 return FAIL;
5551 }
5552 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5553 globpath(p_pp, s, &ga, 0);
5554 vim_free(s);
5555 }
5556 }
5557
5558 if (flags & DIP_OPT) {
5559 for (i = 0; dirnames[i] != NULL; ++i)
5560 {
5561 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5562 if (s == NULL)
5563 {
5564 ga_clear_strings(&ga);
5565 return FAIL;
5566 }
5567 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5568 globpath(p_pp, s, &ga, 0);
5569 vim_free(s);
5570 }
5571 }
5572
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005573 for (i = 0; i < ga.ga_len; ++i)
5574 {
5575 match = ((char_u **)ga.ga_data)[i];
5576 s = match;
5577 e = s + STRLEN(s);
5578 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5579 {
5580 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005581 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005582 if (s < match || vim_ispathsep(*s))
5583 break;
5584 ++s;
5585 *e = NUL;
5586 mch_memmove(match, s, e - s + 1);
5587 }
5588 }
5589
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005590 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005591 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005592
5593 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005594 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005595 remove_duplicates(&ga);
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 *file = ga.ga_data;
5598 *num_file = ga.ga_len;
5599 return OK;
5600}
5601
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005602/*
5603 * Expand loadplugin names:
5604 * 'packpath'/pack/ * /opt/{pat}
5605 */
5606 static int
5607ExpandPackAddDir(
5608 char_u *pat,
5609 int *num_file,
5610 char_u ***file)
5611{
5612 char_u *s;
5613 char_u *e;
5614 char_u *match;
5615 garray_T ga;
5616 int i;
5617 int pat_len;
5618
5619 *num_file = 0;
5620 *file = NULL;
5621 pat_len = (int)STRLEN(pat);
5622 ga_init2(&ga, (int)sizeof(char *), 10);
5623
5624 s = alloc((unsigned)(pat_len + 26));
5625 if (s == NULL)
5626 {
5627 ga_clear_strings(&ga);
5628 return FAIL;
5629 }
5630 sprintf((char *)s, "pack/*/opt/%s*", pat);
5631 globpath(p_pp, s, &ga, 0);
5632 vim_free(s);
5633
5634 for (i = 0; i < ga.ga_len; ++i)
5635 {
5636 match = ((char_u **)ga.ga_data)[i];
5637 s = gettail(match);
5638 e = s + STRLEN(s);
5639 mch_memmove(match, s, e - s + 1);
5640 }
5641
5642 if (ga.ga_len == 0)
5643 return FAIL;
5644
5645 /* Sort and remove duplicates which can happen when specifying multiple
5646 * directories in dirnames. */
5647 remove_duplicates(&ga);
5648
5649 *file = ga.ga_data;
5650 *num_file = ga.ga_len;
5651 return OK;
5652}
5653
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654#endif
5655
5656#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5657/*
5658 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005659 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005661 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005662globpath(
5663 char_u *path,
5664 char_u *file,
5665 garray_T *ga,
5666 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667{
5668 expand_T xpc;
5669 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 int num_p;
5672 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674 buf = alloc(MAXPATHL);
5675 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005676 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005678 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 /* Loop over all entries in {path}. */
5682 while (*path != NUL)
5683 {
5684 /* Copy one item of the path to buf[] and concatenate the file name. */
5685 copy_option_part(&path, buf, MAXPATHL, ",");
5686 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5687 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005688# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005689 /* Using the platform's path separator (\) makes vim incorrectly
5690 * treat it as an escape character, use '/' instead. */
5691 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5692 STRCAT(buf, "/");
5693# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005697 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5698 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005700 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005702 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 for (i = 0; i < num_p; ++i)
5705 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005706 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005707 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005708 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 FreeWild(num_p, p);
5713 }
5714 }
5715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
5717 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718}
5719
5720#endif
5721
5722#if defined(FEAT_CMDHIST) || defined(PROTO)
5723
5724/*********************************
5725 * Command line history stuff *
5726 *********************************/
5727
5728/*
5729 * Translate a history character to the associated type number.
5730 */
5731 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005732hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733{
5734 if (c == ':')
5735 return HIST_CMD;
5736 if (c == '=')
5737 return HIST_EXPR;
5738 if (c == '@')
5739 return HIST_INPUT;
5740 if (c == '>')
5741 return HIST_DEBUG;
5742 return HIST_SEARCH; /* must be '?' or '/' */
5743}
5744
5745/*
5746 * Table of history names.
5747 * These names are used in :history and various hist...() functions.
5748 * It is sufficient to give the significant prefix of a history name.
5749 */
5750
5751static char *(history_names[]) =
5752{
5753 "cmd",
5754 "search",
5755 "expr",
5756 "input",
5757 "debug",
5758 NULL
5759};
5760
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005761#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5762/*
5763 * Function given to ExpandGeneric() to obtain the possible first
5764 * arguments of the ":history command.
5765 */
5766 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005767get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005768{
5769 static char_u compl[2] = { NUL, NUL };
5770 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005771 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005772 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5773
5774 if (idx < short_names_count)
5775 {
5776 compl[0] = (char_u)short_names[idx];
5777 return compl;
5778 }
5779 if (idx < short_names_count + history_name_count)
5780 return (char_u *)history_names[idx - short_names_count];
5781 if (idx == short_names_count + history_name_count)
5782 return (char_u *)"all";
5783 return NULL;
5784}
5785#endif
5786
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787/*
5788 * init_history() - Initialize the command line history.
5789 * Also used to re-allocate the history when the size changes.
5790 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005791 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005792init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793{
5794 int newlen; /* new length of history table */
5795 histentry_T *temp;
5796 int i;
5797 int j;
5798 int type;
5799
5800 /*
5801 * If size of history table changed, reallocate it
5802 */
5803 newlen = (int)p_hi;
5804 if (newlen != hislen) /* history length changed */
5805 {
5806 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5807 {
5808 if (newlen)
5809 {
5810 temp = (histentry_T *)lalloc(
5811 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5812 if (temp == NULL) /* out of memory! */
5813 {
5814 if (type == 0) /* first one: just keep the old length */
5815 {
5816 newlen = hislen;
5817 break;
5818 }
5819 /* Already changed one table, now we can only have zero
5820 * length for all tables. */
5821 newlen = 0;
5822 type = -1;
5823 continue;
5824 }
5825 }
5826 else
5827 temp = NULL;
5828 if (newlen == 0 || temp != NULL)
5829 {
5830 if (hisidx[type] < 0) /* there are no entries yet */
5831 {
5832 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005833 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 }
5835 else if (newlen > hislen) /* array becomes bigger */
5836 {
5837 for (i = 0; i <= hisidx[type]; ++i)
5838 temp[i] = history[type][i];
5839 j = i;
5840 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005841 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 for ( ; j < hislen; ++i, ++j)
5843 temp[i] = history[type][j];
5844 }
5845 else /* array becomes smaller or 0 */
5846 {
5847 j = hisidx[type];
5848 for (i = newlen - 1; ; --i)
5849 {
5850 if (i >= 0) /* copy newest entries */
5851 temp[i] = history[type][j];
5852 else /* remove older entries */
5853 vim_free(history[type][j].hisstr);
5854 if (--j < 0)
5855 j = hislen - 1;
5856 if (j == hisidx[type])
5857 break;
5858 }
5859 hisidx[type] = newlen - 1;
5860 }
5861 vim_free(history[type]);
5862 history[type] = temp;
5863 }
5864 }
5865 hislen = newlen;
5866 }
5867}
5868
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005869 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005870clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005871{
5872 hisptr->hisnum = 0;
5873 hisptr->viminfo = FALSE;
5874 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005875 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005876}
5877
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878/*
5879 * Check if command line 'str' is already in history.
5880 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5881 */
5882 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005883in_history(
5884 int type,
5885 char_u *str,
5886 int move_to_front, /* Move the entry to the front if it exists */
5887 int sep,
5888 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 int i;
5891 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005892 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893
5894 if (hisidx[type] < 0)
5895 return FALSE;
5896 i = hisidx[type];
5897 do
5898 {
5899 if (history[type][i].hisstr == NULL)
5900 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005901
5902 /* For search history, check that the separator character matches as
5903 * well. */
5904 p = history[type][i].hisstr;
5905 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02005906 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02005907 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 {
5909 if (!move_to_front)
5910 return TRUE;
5911 last_i = i;
5912 break;
5913 }
5914 if (--i < 0)
5915 i = hislen - 1;
5916 } while (i != hisidx[type]);
5917
5918 if (last_i >= 0)
5919 {
5920 str = history[type][i].hisstr;
5921 while (i != hisidx[type])
5922 {
5923 if (++i >= hislen)
5924 i = 0;
5925 history[type][last_i] = history[type][i];
5926 last_i = i;
5927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005929 history[type][i].viminfo = FALSE;
5930 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02005931 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 return TRUE;
5933 }
5934 return FALSE;
5935}
5936
5937/*
5938 * Convert history name (from table above) to its HIST_ equivalent.
5939 * When "name" is empty, return "cmd" history.
5940 * Returns -1 for unknown history name.
5941 */
5942 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005943get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944{
5945 int i;
5946 int len = (int)STRLEN(name);
5947
5948 /* No argument: use current history. */
5949 if (len == 0)
5950 return hist_char2type(ccline.cmdfirstc);
5951
5952 for (i = 0; history_names[i] != NULL; ++i)
5953 if (STRNICMP(name, history_names[i], len) == 0)
5954 return i;
5955
5956 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
5957 return hist_char2type(name[0]);
5958
5959 return -1;
5960}
5961
5962static int last_maptick = -1; /* last seen maptick */
5963
5964/*
5965 * Add the given string to the given history. If the string is already in the
5966 * history then it is moved to the front. "histype" may be one of he HIST_
5967 * values.
5968 */
5969 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005970add_to_history(
5971 int histype,
5972 char_u *new_entry,
5973 int in_map, /* consider maptick when inside a mapping */
5974 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 histentry_T *hisptr;
5977 int len;
5978
5979 if (hislen == 0) /* no history */
5980 return;
5981
Bram Moolenaara939e432013-11-09 05:30:26 +01005982 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
5983 return;
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985 /*
5986 * Searches inside the same mapping overwrite each other, so that only
5987 * the last line is kept. Be careful not to remove a line that was moved
5988 * down, only lines that were added.
5989 */
5990 if (histype == HIST_SEARCH && in_map)
5991 {
Bram Moolenaar46643712016-09-09 21:42:36 +02005992 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 {
5994 /* Current line is from the same mapping, remove it */
5995 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
5996 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005997 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 --hisnum[histype];
5999 if (--hisidx[HIST_SEARCH] < 0)
6000 hisidx[HIST_SEARCH] = hislen - 1;
6001 }
6002 last_maptick = -1;
6003 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006004 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 {
6006 if (++hisidx[histype] == hislen)
6007 hisidx[histype] = 0;
6008 hisptr = &history[histype][hisidx[histype]];
6009 vim_free(hisptr->hisstr);
6010
6011 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006012 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6014 if (hisptr->hisstr != NULL)
6015 hisptr->hisstr[len + 1] = sep;
6016
6017 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006018 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006019 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (histype == HIST_SEARCH && in_map)
6021 last_maptick = maptick;
6022 }
6023}
6024
6025#if defined(FEAT_EVAL) || defined(PROTO)
6026
6027/*
6028 * Get identifier of newest history entry.
6029 * "histype" may be one of the HIST_ values.
6030 */
6031 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006032get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6035 || hisidx[histype] < 0)
6036 return -1;
6037
6038 return history[histype][hisidx[histype]].hisnum;
6039}
6040
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 * Calculate history index from a number:
6043 * num > 0: seen as identifying number of a history entry
6044 * num < 0: relative position in history wrt newest entry
6045 * "histype" may be one of the HIST_ values.
6046 */
6047 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006048calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049{
6050 int i;
6051 histentry_T *hist;
6052 int wrapped = FALSE;
6053
6054 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6055 || (i = hisidx[histype]) < 0 || num == 0)
6056 return -1;
6057
6058 hist = history[histype];
6059 if (num > 0)
6060 {
6061 while (hist[i].hisnum > num)
6062 if (--i < 0)
6063 {
6064 if (wrapped)
6065 break;
6066 i += hislen;
6067 wrapped = TRUE;
6068 }
6069 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6070 return i;
6071 }
6072 else if (-num <= hislen)
6073 {
6074 i += num + 1;
6075 if (i < 0)
6076 i += hislen;
6077 if (hist[i].hisstr != NULL)
6078 return i;
6079 }
6080 return -1;
6081}
6082
6083/*
6084 * Get a history entry by its index.
6085 * "histype" may be one of the HIST_ values.
6086 */
6087 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006088get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 idx = calc_hist_idx(histype, idx);
6091 if (idx >= 0)
6092 return history[histype][idx].hisstr;
6093 else
6094 return (char_u *)"";
6095}
6096
6097/*
6098 * Clear all entries of a history.
6099 * "histype" may be one of the HIST_ values.
6100 */
6101 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006102clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
6104 int i;
6105 histentry_T *hisptr;
6106
6107 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6108 {
6109 hisptr = history[histype];
6110 for (i = hislen; i--;)
6111 {
6112 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006113 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006114 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 }
6116 hisidx[histype] = -1; /* mark history as cleared */
6117 hisnum[histype] = 0; /* reset identifier counter */
6118 return OK;
6119 }
6120 return FAIL;
6121}
6122
6123/*
6124 * Remove all entries matching {str} from a history.
6125 * "histype" may be one of the HIST_ values.
6126 */
6127 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006128del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129{
6130 regmatch_T regmatch;
6131 histentry_T *hisptr;
6132 int idx;
6133 int i;
6134 int last;
6135 int found = FALSE;
6136
6137 regmatch.regprog = NULL;
6138 regmatch.rm_ic = FALSE; /* always match case */
6139 if (hislen != 0
6140 && histype >= 0
6141 && histype < HIST_COUNT
6142 && *str != NUL
6143 && (idx = hisidx[histype]) >= 0
6144 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6145 != NULL)
6146 {
6147 i = last = idx;
6148 do
6149 {
6150 hisptr = &history[histype][i];
6151 if (hisptr->hisstr == NULL)
6152 break;
6153 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6154 {
6155 found = TRUE;
6156 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006157 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159 else
6160 {
6161 if (i != last)
6162 {
6163 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006164 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 }
6166 if (--last < 0)
6167 last += hislen;
6168 }
6169 if (--i < 0)
6170 i += hislen;
6171 } while (i != idx);
6172 if (history[histype][idx].hisstr == NULL)
6173 hisidx[histype] = -1;
6174 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006175 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 return found;
6177}
6178
6179/*
6180 * Remove an indexed entry from a history.
6181 * "histype" may be one of the HIST_ values.
6182 */
6183 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006184del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185{
6186 int i, j;
6187
6188 i = calc_hist_idx(histype, idx);
6189 if (i < 0)
6190 return FALSE;
6191 idx = hisidx[histype];
6192 vim_free(history[histype][i].hisstr);
6193
6194 /* When deleting the last added search string in a mapping, reset
6195 * last_maptick, so that the last added search string isn't deleted again.
6196 */
6197 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6198 last_maptick = -1;
6199
6200 while (i != idx)
6201 {
6202 j = (i + 1) % hislen;
6203 history[histype][i] = history[histype][j];
6204 i = j;
6205 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006206 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (--i < 0)
6208 i += hislen;
6209 hisidx[histype] = i;
6210 return TRUE;
6211}
6212
6213#endif /* FEAT_EVAL */
6214
6215#if defined(FEAT_CRYPT) || defined(PROTO)
6216/*
6217 * Very specific function to remove the value in ":set key=val" from the
6218 * history.
6219 */
6220 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006221remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 char_u *p;
6224 int i;
6225
6226 i = hisidx[HIST_CMD];
6227 if (i < 0)
6228 return;
6229 p = history[HIST_CMD][i].hisstr;
6230 if (p != NULL)
6231 for ( ; *p; ++p)
6232 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6233 {
6234 p = vim_strchr(p + 3, '=');
6235 if (p == NULL)
6236 break;
6237 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006238 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 if (p[i] == '\\' && p[i + 1])
6240 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006241 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006242 --p;
6243 }
6244}
6245#endif
6246
6247#endif /* FEAT_CMDHIST */
6248
Bram Moolenaar064154c2016-03-19 22:50:43 +01006249#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006250/*
6251 * Get pointer to the command line info to use. cmdline_paste() may clear
6252 * ccline and put the previous value in prev_ccline.
6253 */
6254 static struct cmdline_info *
6255get_ccline_ptr(void)
6256{
6257 if ((State & CMDLINE) == 0)
6258 return NULL;
6259 if (ccline.cmdbuff != NULL)
6260 return &ccline;
6261 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6262 return &prev_ccline;
6263 return NULL;
6264}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006265#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006266
Bram Moolenaar064154c2016-03-19 22:50:43 +01006267#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006268/*
6269 * Get the current command line in allocated memory.
6270 * Only works when the command line is being edited.
6271 * Returns NULL when something is wrong.
6272 */
6273 char_u *
6274get_cmdline_str(void)
6275{
6276 struct cmdline_info *p = get_ccline_ptr();
6277
6278 if (p == NULL)
6279 return NULL;
6280 return vim_strnsave(p->cmdbuff, p->cmdlen);
6281}
6282
6283/*
6284 * Get the current command line position, counted in bytes.
6285 * Zero is the first position.
6286 * Only works when the command line is being edited.
6287 * Returns -1 when something is wrong.
6288 */
6289 int
6290get_cmdline_pos(void)
6291{
6292 struct cmdline_info *p = get_ccline_ptr();
6293
6294 if (p == NULL)
6295 return -1;
6296 return p->cmdpos;
6297}
6298
6299/*
6300 * Set the command line byte position to "pos". Zero is the first position.
6301 * Only works when the command line is being edited.
6302 * Returns 1 when failed, 0 when OK.
6303 */
6304 int
6305set_cmdline_pos(
6306 int pos)
6307{
6308 struct cmdline_info *p = get_ccline_ptr();
6309
6310 if (p == NULL)
6311 return 1;
6312
6313 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6314 * changed the command line. */
6315 if (pos < 0)
6316 new_cmdpos = 0;
6317 else
6318 new_cmdpos = pos;
6319 return 0;
6320}
6321#endif
6322
6323#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6324/*
6325 * Get the current command-line type.
6326 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6327 * Only works when the command line is being edited.
6328 * Returns NUL when something is wrong.
6329 */
6330 int
6331get_cmdline_type(void)
6332{
6333 struct cmdline_info *p = get_ccline_ptr();
6334
6335 if (p == NULL)
6336 return NUL;
6337 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006338 return
6339# ifdef FEAT_EVAL
6340 (p->input_fn) ? '@' :
6341# endif
6342 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006343 return p->cmdfirstc;
6344}
6345#endif
6346
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6348/*
6349 * Get indices "num1,num2" that specify a range within a list (not a range of
6350 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6351 * Returns OK if parsed successfully, otherwise FAIL.
6352 */
6353 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006354get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
6356 int len;
6357 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006358 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359
6360 *str = skipwhite(*str);
6361 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6362 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006363 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 *str += len;
6365 *num1 = (int)num;
6366 first = TRUE;
6367 }
6368 *str = skipwhite(*str);
6369 if (**str == ',') /* parse "to" part of range */
6370 {
6371 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006372 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 if (len > 0)
6374 {
6375 *num2 = (int)num;
6376 *str = skipwhite(*str + len);
6377 }
6378 else if (!first) /* no number given at all */
6379 return FAIL;
6380 }
6381 else if (first) /* only one number given */
6382 *num2 = *num1;
6383 return OK;
6384}
6385#endif
6386
6387#if defined(FEAT_CMDHIST) || defined(PROTO)
6388/*
6389 * :history command - print a history
6390 */
6391 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006392ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393{
6394 histentry_T *hist;
6395 int histype1 = HIST_CMD;
6396 int histype2 = HIST_CMD;
6397 int hisidx1 = 1;
6398 int hisidx2 = -1;
6399 int idx;
6400 int i, j, k;
6401 char_u *end;
6402 char_u *arg = eap->arg;
6403
6404 if (hislen == 0)
6405 {
6406 MSG(_("'history' option is zero"));
6407 return;
6408 }
6409
6410 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6411 {
6412 end = arg;
6413 while (ASCII_ISALPHA(*end)
6414 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6415 end++;
6416 i = *end;
6417 *end = NUL;
6418 histype1 = get_histtype(arg);
6419 if (histype1 == -1)
6420 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006421 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422 {
6423 histype1 = 0;
6424 histype2 = HIST_COUNT-1;
6425 }
6426 else
6427 {
6428 *end = i;
6429 EMSG(_(e_trailing));
6430 return;
6431 }
6432 }
6433 else
6434 histype2 = histype1;
6435 *end = i;
6436 }
6437 else
6438 end = arg;
6439 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6440 {
6441 EMSG(_(e_trailing));
6442 return;
6443 }
6444
6445 for (; !got_int && histype1 <= histype2; ++histype1)
6446 {
6447 STRCPY(IObuff, "\n # ");
6448 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6449 MSG_PUTS_TITLE(IObuff);
6450 idx = hisidx[histype1];
6451 hist = history[histype1];
6452 j = hisidx1;
6453 k = hisidx2;
6454 if (j < 0)
6455 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6456 if (k < 0)
6457 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6458 if (idx >= 0 && j <= k)
6459 for (i = idx + 1; !got_int; ++i)
6460 {
6461 if (i == hislen)
6462 i = 0;
6463 if (hist[i].hisstr != NULL
6464 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6465 {
6466 msg_putchar('\n');
6467 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6468 hist[i].hisnum);
6469 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6470 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006471 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 else
6473 STRCAT(IObuff, hist[i].hisstr);
6474 msg_outtrans(IObuff);
6475 out_flush();
6476 }
6477 if (i == idx)
6478 break;
6479 }
6480 }
6481}
6482#endif
6483
6484#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006485/*
6486 * Buffers for history read from a viminfo file. Only valid while reading.
6487 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006488static histentry_T *viminfo_history[HIST_COUNT] =
6489 {NULL, NULL, NULL, NULL, NULL};
6490static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6491static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492static int viminfo_add_at_front = FALSE;
6493
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006494static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495
6496/*
6497 * Translate a history type number to the associated character.
6498 */
6499 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006500hist_type2char(
6501 int type,
6502 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 if (type == HIST_CMD)
6505 return ':';
6506 if (type == HIST_SEARCH)
6507 {
6508 if (use_question)
6509 return '?';
6510 else
6511 return '/';
6512 }
6513 if (type == HIST_EXPR)
6514 return '=';
6515 return '@';
6516}
6517
6518/*
6519 * Prepare for reading the history from the viminfo file.
6520 * This allocates history arrays to store the read history lines.
6521 */
6522 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006523prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 int i;
6526 int num;
6527 int type;
6528 int len;
6529
6530 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006531 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 if (asklen > hislen)
6533 asklen = hislen;
6534
6535 for (type = 0; type < HIST_COUNT; ++type)
6536 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006537 /* Count the number of empty spaces in the history list. Entries read
6538 * from viminfo previously are also considered empty. If there are
6539 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006541 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 num++;
6543 len = asklen;
6544 if (num > len)
6545 len = num;
6546 if (len <= 0)
6547 viminfo_history[type] = NULL;
6548 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006549 viminfo_history[type] = (histentry_T *)lalloc(
6550 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 if (viminfo_history[type] == NULL)
6552 len = 0;
6553 viminfo_hislen[type] = len;
6554 viminfo_hisidx[type] = 0;
6555 }
6556}
6557
6558/*
6559 * Accept a line from the viminfo, store it in the history array when it's
6560 * new.
6561 */
6562 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006563read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564{
6565 int type;
6566 long_u len;
6567 char_u *val;
6568 char_u *p;
6569
6570 type = hist_char2type(virp->vir_line[0]);
6571 if (viminfo_hisidx[type] < viminfo_hislen[type])
6572 {
6573 val = viminfo_readstring(virp, 1, TRUE);
6574 if (val != NULL && *val != NUL)
6575 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006576 int sep = (*val == ' ' ? NUL : *val);
6577
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006579 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
6581 /* Need to re-allocate to append the separator byte. */
6582 len = STRLEN(val);
6583 p = lalloc(len + 2, TRUE);
6584 if (p != NULL)
6585 {
6586 if (type == HIST_SEARCH)
6587 {
6588 /* Search entry: Move the separator from the first
6589 * column to after the NUL. */
6590 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006591 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 }
6593 else
6594 {
6595 /* Not a search entry: No separator in the viminfo
6596 * file, add a NUL separator. */
6597 mch_memmove(p, val, (size_t)len + 1);
6598 p[len + 1] = NUL;
6599 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006600 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6601 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006602 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6603 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006604 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 }
6606 }
6607 }
6608 vim_free(val);
6609 }
6610 return viminfo_readline(virp);
6611}
6612
Bram Moolenaar07219f92013-04-14 23:19:36 +02006613/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006614 * Accept a new style history line from the viminfo, store it in the history
6615 * array when it's new.
6616 */
6617 void
6618handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006619 garray_T *values,
6620 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006621{
6622 int type;
6623 long_u len;
6624 char_u *val;
6625 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006626 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006627
6628 /* Check the format:
6629 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006630 if (values->ga_len < 4
6631 || vp[0].bv_type != BVAL_NR
6632 || vp[1].bv_type != BVAL_NR
6633 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6634 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006635 return;
6636
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006637 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006638 if (type >= HIST_COUNT)
6639 return;
6640 if (viminfo_hisidx[type] < viminfo_hislen[type])
6641 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006642 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006643 if (val != NULL && *val != NUL)
6644 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006645 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6646 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006647 int idx;
6648 int overwrite = FALSE;
6649
6650 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6651 {
6652 /* If lines were written by an older Vim we need to avoid
6653 * getting duplicates. See if the entry already exists. */
6654 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6655 {
6656 p = viminfo_history[type][idx].hisstr;
6657 if (STRCMP(val, p) == 0
6658 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6659 {
6660 overwrite = TRUE;
6661 break;
6662 }
6663 }
6664
6665 if (!overwrite)
6666 {
6667 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006668 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006669 p = lalloc(len + 2, TRUE);
6670 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006671 else
6672 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006673 if (p != NULL)
6674 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006675 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006676 if (!overwrite)
6677 {
6678 mch_memmove(p, val, (size_t)len + 1);
6679 /* Put the separator after the NUL. */
6680 p[len + 1] = sep;
6681 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006682 viminfo_history[type][idx].hisnum = 0;
6683 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006684 viminfo_hisidx[type]++;
6685 }
6686 }
6687 }
6688 }
6689 }
6690}
6691
6692/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006693 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006694 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006695 static void
6696concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 int idx;
6699 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006700
6701 idx = hisidx[type] + viminfo_hisidx[type];
6702 if (idx >= hislen)
6703 idx -= hislen;
6704 else if (idx < 0)
6705 idx = hislen - 1;
6706 if (viminfo_add_at_front)
6707 hisidx[type] = idx;
6708 else
6709 {
6710 if (hisidx[type] == -1)
6711 hisidx[type] = hislen - 1;
6712 do
6713 {
6714 if (history[type][idx].hisstr != NULL
6715 || history[type][idx].viminfo)
6716 break;
6717 if (++idx == hislen)
6718 idx = 0;
6719 } while (idx != hisidx[type]);
6720 if (idx != hisidx[type] && --idx < 0)
6721 idx = hislen - 1;
6722 }
6723 for (i = 0; i < viminfo_hisidx[type]; i++)
6724 {
6725 vim_free(history[type][idx].hisstr);
6726 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6727 history[type][idx].viminfo = TRUE;
6728 history[type][idx].time_set = viminfo_history[type][i].time_set;
6729 if (--idx < 0)
6730 idx = hislen - 1;
6731 }
6732 idx += 1;
6733 idx %= hislen;
6734 for (i = 0; i < viminfo_hisidx[type]; i++)
6735 {
6736 history[type][idx++].hisnum = ++hisnum[type];
6737 idx %= hislen;
6738 }
6739}
6740
6741#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6742 static int
6743#ifdef __BORLANDC__
6744_RTLENTRYF
6745#endif
6746sort_hist(const void *s1, const void *s2)
6747{
6748 histentry_T *p1 = *(histentry_T **)s1;
6749 histentry_T *p2 = *(histentry_T **)s2;
6750
6751 if (p1->time_set < p2->time_set) return -1;
6752 if (p1->time_set > p2->time_set) return 1;
6753 return 0;
6754}
6755#endif
6756
6757/*
6758 * Merge history lines from viminfo and lines typed in this Vim based on the
6759 * timestamp;
6760 */
6761 static void
6762merge_history(int type)
6763{
6764 int max_len;
6765 histentry_T **tot_hist;
6766 histentry_T *new_hist;
6767 int i;
6768 int len;
6769
6770 /* Make one long list with all entries. */
6771 max_len = hislen + viminfo_hisidx[type];
6772 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6773 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6774 if (tot_hist == NULL || new_hist == NULL)
6775 {
6776 vim_free(tot_hist);
6777 vim_free(new_hist);
6778 return;
6779 }
6780 for (i = 0; i < viminfo_hisidx[type]; i++)
6781 tot_hist[i] = &viminfo_history[type][i];
6782 len = i;
6783 for (i = 0; i < hislen; i++)
6784 if (history[type][i].hisstr != NULL)
6785 tot_hist[len++] = &history[type][i];
6786
6787 /* Sort the list on timestamp. */
6788 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6789
6790 /* Keep the newest ones. */
6791 for (i = 0; i < hislen; i++)
6792 {
6793 if (i < len)
6794 {
6795 new_hist[i] = *tot_hist[i];
6796 tot_hist[i]->hisstr = NULL;
6797 if (new_hist[i].hisnum == 0)
6798 new_hist[i].hisnum = ++hisnum[type];
6799 }
6800 else
6801 clear_hist_entry(&new_hist[i]);
6802 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006803 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006804
6805 /* Free what is not kept. */
6806 for (i = 0; i < viminfo_hisidx[type]; i++)
6807 vim_free(viminfo_history[type][i].hisstr);
6808 for (i = 0; i < hislen; i++)
6809 vim_free(history[type][i].hisstr);
6810 vim_free(history[type]);
6811 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006812 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006813}
6814
6815/*
6816 * Finish reading history lines from viminfo. Not used when writing viminfo.
6817 */
6818 void
6819finish_viminfo_history(vir_T *virp)
6820{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006822 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823
6824 for (type = 0; type < HIST_COUNT; ++type)
6825 {
6826 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006827 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006828
6829 if (merge)
6830 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006832 concat_history(type);
6833
Bram Moolenaard23a8232018-02-10 18:45:26 +01006834 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006835 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 }
6837}
6838
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006839/*
6840 * Write history to viminfo file in "fp".
6841 * When "merge" is TRUE merge history lines with a previously read viminfo
6842 * file, data is in viminfo_history[].
6843 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006846write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006847{
6848 int i;
6849 int type;
6850 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006851 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852
6853 init_history();
6854 if (hislen == 0)
6855 return;
6856 for (type = 0; type < HIST_COUNT; ++type)
6857 {
6858 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6859 if (num_saved == 0)
6860 continue;
6861 if (num_saved < 0) /* Use default */
6862 num_saved = hislen;
6863 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6864 type == HIST_CMD ? _("Command Line") :
6865 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006866 type == HIST_EXPR ? _("Expression") :
6867 type == HIST_INPUT ? _("Input Line") :
6868 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 if (num_saved > hislen)
6870 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006871
6872 /*
6873 * Merge typed and viminfo history:
6874 * round 1: history of typed commands.
6875 * round 2: history from recently read viminfo.
6876 */
6877 for (round = 1; round <= 2; ++round)
6878 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006879 if (round == 1)
6880 /* start at newest entry, somewhere in the list */
6881 i = hisidx[type];
6882 else if (viminfo_hisidx[type] > 0)
6883 /* start at newest entry, first in the list */
6884 i = 0;
6885 else
6886 /* empty list */
6887 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006888 if (i >= 0)
6889 while (num_saved > 0
6890 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006892 char_u *p;
6893 time_t timestamp;
6894 int c = NUL;
6895
6896 if (round == 1)
6897 {
6898 p = history[type][i].hisstr;
6899 timestamp = history[type][i].time_set;
6900 }
6901 else
6902 {
6903 p = viminfo_history[type] == NULL ? NULL
6904 : viminfo_history[type][i].hisstr;
6905 timestamp = viminfo_history[type] == NULL ? 0
6906 : viminfo_history[type][i].time_set;
6907 }
6908
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006909 if (p != NULL && (round == 2
6910 || !merge
6911 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006913 --num_saved;
6914 fputc(hist_type2char(type, TRUE), fp);
6915 /* For the search history: put the separator in the
6916 * second column; use a space if there isn't one. */
6917 if (type == HIST_SEARCH)
6918 {
6919 c = p[STRLEN(p) + 1];
6920 putc(c == NUL ? ' ' : c, fp);
6921 }
6922 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006923
6924 {
6925 char cbuf[NUMBUFLEN];
6926
6927 /* New style history with a bar line. Format:
6928 * |{bartype},{histtype},{timestamp},{separator},"text" */
6929 if (c == NUL)
6930 cbuf[0] = NUL;
6931 else
6932 sprintf(cbuf, "%d", c);
6933 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
6934 type, (long)timestamp, cbuf);
6935 barline_writestring(fp, p, LSIZE - 20);
6936 putc('\n', fp);
6937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006939 if (round == 1)
6940 {
6941 /* Decrement index, loop around and stop when back at
6942 * the start. */
6943 if (--i < 0)
6944 i = hislen - 1;
6945 if (i == hisidx[type])
6946 break;
6947 }
6948 else
6949 {
6950 /* Increment index. Stop at the end in the while. */
6951 ++i;
6952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006954 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006955 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006956 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006957 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01006958 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02006959 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961}
6962#endif /* FEAT_VIMINFO */
6963
6964#if defined(FEAT_FKMAP) || defined(PROTO)
6965/*
6966 * Write a character at the current cursor+offset position.
6967 * It is directly written into the command buffer block.
6968 */
6969 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006970cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
6972 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6973 {
6974 EMSG(_("E198: cmd_pchar beyond the command length"));
6975 return;
6976 }
6977 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
6978 ccline.cmdbuff[ccline.cmdlen] = NUL;
6979}
6980
6981 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006982cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983{
6984 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
6985 {
6986 /* EMSG(_("cmd_gchar beyond the command length")); */
6987 return NUL;
6988 }
6989 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
6990}
6991#endif
6992
6993#if defined(FEAT_CMDWIN) || defined(PROTO)
6994/*
6995 * Open a window on the current command line and history. Allow editing in
6996 * the window. Returns when the window is closed.
6997 * Returns:
6998 * CR if the command is to be executed
6999 * Ctrl_C if it is to be abandoned
7000 * K_IGNORE if editing continues
7001 */
7002 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007003open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004{
7005 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007006 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007008 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 win_T *wp;
7010 int i;
7011 linenr_T lnum;
7012 int histtype;
7013 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007014 int save_restart_edit = restart_edit;
7015 int save_State = State;
7016 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007017#ifdef FEAT_RIGHTLEFT
7018 int save_cmdmsg_rl = cmdmsg_rl;
7019#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007020#ifdef FEAT_FOLDING
7021 int save_KeyTyped;
7022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
7024 /* Can't do this recursively. Can't do it when typing a password. */
7025 if (cmdwin_type != 0
7026# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7027 || cmdline_star > 0
7028# endif
7029 )
7030 {
7031 beep_flush();
7032 return K_IGNORE;
7033 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007034 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036 /* Save current window sizes. */
7037 win_size_save(&winsizes);
7038
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007040 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007041
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007042 /* don't use a new tab page */
7043 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007044 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007045
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 /* Create a window for the command-line buffer. */
7047 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7048 {
7049 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007050 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 return K_IGNORE;
7052 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007053 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054
7055 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007056 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007057 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007060#ifdef FEAT_FOLDING
7061 curwin->w_p_fen = FALSE;
7062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007064 curwin->w_p_rl = cmdmsg_rl;
7065 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007067 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007070 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007071 /* But don't allow switching to another buffer. */
7072 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073
Bram Moolenaar46152342005-09-07 21:18:43 +00007074 /* Showing the prompt may have set need_wait_return, reset it. */
7075 need_wait_return = FALSE;
7076
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007077 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7079 {
7080 if (p_wc == TAB)
7081 {
7082 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7083 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7084 }
7085 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7086 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007087 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007089 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7090 * sets 'textwidth' to 78). */
7091 curbuf->b_p_tw = 0;
7092
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 /* Fill the buffer with the history. */
7094 init_history();
7095 if (hislen > 0)
7096 {
7097 i = hisidx[histtype];
7098 if (i >= 0)
7099 {
7100 lnum = 0;
7101 do
7102 {
7103 if (++i == hislen)
7104 i = 0;
7105 if (history[histtype][i].hisstr != NULL)
7106 ml_append(lnum++, history[histtype][i].hisstr,
7107 (colnr_T)0, FALSE);
7108 }
7109 while (i != hisidx[histtype]);
7110 }
7111 }
7112
7113 /* Replace the empty last line with the current command-line and put the
7114 * cursor there. */
7115 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7116 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7117 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007118 changed_line_abv_curs();
7119 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007120 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121
7122 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007123 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124
7125 /* No Ex mode here! */
7126 exmode_active = 0;
7127
7128 State = NORMAL;
7129# ifdef FEAT_MOUSE
7130 setmouse();
7131# endif
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007134 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007135 if (restart_edit != 0) /* autocmd with ":startinsert" */
7136 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137
7138 i = RedrawingDisabled;
7139 RedrawingDisabled = 0;
7140
7141 /*
7142 * Call the main loop until <CR> or CTRL-C is typed.
7143 */
7144 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007145 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147 RedrawingDisabled = i;
7148
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007149# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007150 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007151# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007152
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007154 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007155
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007156# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007157 /* Restore KeyTyped in case it is modified by autocommands */
7158 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159# endif
7160
Bram Moolenaarccc18222007-05-10 18:25:20 +00007161 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007162 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 cmdwin_type = 0;
7164
7165 exmode_active = save_exmode;
7166
Bram Moolenaarf9821062008-06-20 16:31:07 +00007167 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007169 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 {
7171 cmdwin_result = Ctrl_C;
7172 EMSG(_("E199: Active window or buffer deleted"));
7173 }
7174 else
7175 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007176# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 /* autocmds may abort script processing */
7178 if (aborting() && cmdwin_result != K_IGNORE)
7179 cmdwin_result = Ctrl_C;
7180# endif
7181 /* Set the new command line from the cmdline buffer. */
7182 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007183 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007185 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7186
7187 if (histtype == HIST_CMD)
7188 {
7189 /* Execute the command directly. */
7190 ccline.cmdbuff = vim_strsave((char_u *)p);
7191 cmdwin_result = CAR;
7192 }
7193 else
7194 {
7195 /* First need to cancel what we were doing. */
7196 ccline.cmdbuff = NULL;
7197 stuffcharReadbuff(':');
7198 stuffReadbuff((char_u *)p);
7199 stuffcharReadbuff(CAR);
7200 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 }
7202 else if (cmdwin_result == K_XF2) /* :qa typed */
7203 {
7204 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7205 cmdwin_result = CAR;
7206 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007207 else if (cmdwin_result == Ctrl_C)
7208 {
7209 /* :q or :close, don't execute any command
7210 * and don't modify the cmd window. */
7211 ccline.cmdbuff = NULL;
7212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 else
7214 ccline.cmdbuff = vim_strsave(ml_get_curline());
7215 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007216 {
7217 ccline.cmdbuff = vim_strsave((char_u *)"");
7218 ccline.cmdlen = 0;
7219 ccline.cmdbufflen = 1;
7220 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 else
7224 {
7225 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7226 ccline.cmdbufflen = ccline.cmdlen + 1;
7227 ccline.cmdpos = curwin->w_cursor.col;
7228 if (ccline.cmdpos > ccline.cmdlen)
7229 ccline.cmdpos = ccline.cmdlen;
7230 if (cmdwin_result == K_IGNORE)
7231 {
7232 set_cmdspos_cursor();
7233 redrawcmd();
7234 }
7235 }
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007238 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007239# ifdef FEAT_CONCEAL
7240 /* Avoid command-line window first character being concealed. */
7241 curwin->w_p_cole = 0;
7242# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007244 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 win_goto(old_curwin);
7246 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007247
7248 /* win_close() may have already wiped the buffer when 'bh' is
7249 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007250 if (bufref_valid(&bufref))
7251 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 /* Restore window sizes. */
7254 win_size_restore(&winsizes);
7255
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007256 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 }
7258
7259 ga_clear(&winsizes);
7260 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007261# ifdef FEAT_RIGHTLEFT
7262 cmdmsg_rl = save_cmdmsg_rl;
7263# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 State = save_State;
7266# ifdef FEAT_MOUSE
7267 setmouse();
7268# endif
7269
7270 return cmdwin_result;
7271}
7272#endif /* FEAT_CMDWIN */
7273
7274/*
7275 * Used for commands that either take a simple command string argument, or:
7276 * cmd << endmarker
7277 * {script}
7278 * endmarker
7279 * Returns a pointer to allocated memory with {script} or NULL.
7280 */
7281 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007282script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283{
7284 char_u *theline;
7285 char *end_pattern = NULL;
7286 char dot[] = ".";
7287 garray_T ga;
7288
7289 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7290 return NULL;
7291
7292 ga_init2(&ga, 1, 0x400);
7293
7294 if (cmd[2] != NUL)
7295 end_pattern = (char *)skipwhite(cmd + 2);
7296 else
7297 end_pattern = dot;
7298
7299 for (;;)
7300 {
7301 theline = eap->getline(
7302#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007303 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304#endif
7305 NUL, eap->cookie, 0);
7306
7307 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007308 {
7309 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312
7313 ga_concat(&ga, theline);
7314 ga_append(&ga, '\n');
7315 vim_free(theline);
7316 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007317 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318
7319 return (char_u *)ga.ga_data;
7320}