blob: 22f35e1ecb213184ff5f8b434dda8da88db4f4b1 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
16/*
17 * Variables shared between getcmdline(), redrawcmdline() and others.
18 * These need to be saved when using CTRL-R |, that's why they are in a
19 * structure.
20 */
21struct cmdline_info
22{
23 char_u *cmdbuff; /* pointer to command line buffer */
24 int cmdbufflen; /* length of cmdbuff */
25 int cmdlen; /* number of chars in command line */
26 int cmdpos; /* current cursor position */
27 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020028 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000029 int cmdindent; /* number of spaces before cmdline */
30 char_u *cmdprompt; /* message in front of cmdline */
31 int cmdattr; /* attributes for prompt */
32 int overstrike; /* Typing mode on the command line. Shared by
33 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000034 expand_T *xpc; /* struct being used for expansion, xp_pattern
35 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000036 int xp_context; /* type of expansion */
37# ifdef FEAT_EVAL
38 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000039 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000041};
42
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000043/* The current cmdline_info. It is initialized in getcmdline() and after that
44 * used by other functions. When invoking getcmdline() recursively it needs
45 * to be saved with save_cmdline() and restored with restore_cmdline().
46 * TODO: make it local to getcmdline() and pass it around. */
47static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000048
49static int cmd_showtail; /* Only show path tail in lists ? */
50
51#ifdef FEAT_EVAL
52static int new_cmdpos; /* position set by set_cmdline_pos() */
53#endif
54
Bram Moolenaara92522f2017-07-15 15:21:38 +020055static int extra_char = NUL; /* extra character to display when redrawing
56 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020057static int extra_char_shift;
58
Bram Moolenaar071d4272004-06-13 20:20:40 +000059#ifdef FEAT_CMDHIST
60typedef struct hist_entry
61{
62 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020063 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000064 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020065 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000066} histentry_T;
67
68static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
69static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
70static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
71 /* identifying (unique) number of newest history entry */
72static int hislen = 0; /* actual length of history tables */
73
Bram Moolenaard25c16e2016-01-29 22:13:30 +010074static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000075
Bram Moolenaard25c16e2016-01-29 22:13:30 +010076static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000077# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079# endif
80#endif
81
82#ifdef FEAT_RIGHTLEFT
83static int cmd_hkmap = 0; /* Hebrew mapping during command line */
84#endif
85
86#ifdef FEAT_FKMAP
87static int cmd_fkmap = 0; /* Farsi mapping during command line */
88#endif
89
Bram Moolenaard25c16e2016-01-29 22:13:30 +010090static int cmdline_charsize(int idx);
91static void set_cmdspos(void);
92static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000093#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void alloc_cmdbuff(int len);
97static int realloc_cmdbuff(int len);
98static void draw_cmdline(int start, int len);
99static void save_cmdline(struct cmdline_info *ccp);
100static void restore_cmdline(struct cmdline_info *ccp);
101static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100103static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100106static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static void redrawcmdprompt(void);
109static void cursorcmd(void);
110static int ccheck_abbr(int);
111static int nextwild(expand_T *xp, int type, int options, int escape);
112static void escape_fname(char_u **pp);
113static int showmatches(expand_T *xp, int wildmenu);
114static void set_expand_context(expand_T *xp);
115static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
116static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100119static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100120static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200121# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100125static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
126static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127# endif
128#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200129#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100130static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200134static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135#endif
136
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200137#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
138static int
139#ifdef __BORLANDC__
140_RTLENTRYF
141#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100142sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200143#endif
144
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200146 static void
147trigger_cmd_autocmd(int typechar, int evt)
148{
149 char_u typestr[2];
150
151 typestr[0] = typechar;
152 typestr[1] = NUL;
153 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
154}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200155
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200157 * Abandon the command line.
158 */
159 static void
160abandon_cmdline(void)
161{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100162 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200163 if (msg_scrolled == 0)
164 compute_cmdrow();
165 MSG("");
166 redraw_cmdline = TRUE;
167}
168
Bram Moolenaaree219b02017-12-17 14:55:01 +0100169#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200170/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100171 * Guess that the pattern matches everything. Only finds specific cases, such
172 * as a trailing \|, which can happen while typing a pattern.
173 */
174 static int
175empty_pattern(char_u *p)
176{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100177 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100178
179 /* remove trailing \v and the like */
180 while (n >= 2 && p[n - 2] == '\\'
181 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
182 n -= 2;
183 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
184}
185
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200186// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200187typedef struct {
188 colnr_T vs_curswant;
189 colnr_T vs_leftcol;
190 linenr_T vs_topline;
191# ifdef FEAT_DIFF
192 int vs_topfill;
193# endif
194 linenr_T vs_botline;
195 linenr_T vs_empty_rows;
196} viewstate_T;
197
198 static void
199save_viewstate(viewstate_T *vs)
200{
201 vs->vs_curswant = curwin->w_curswant;
202 vs->vs_leftcol = curwin->w_leftcol;
203 vs->vs_topline = curwin->w_topline;
204# ifdef FEAT_DIFF
205 vs->vs_topfill = curwin->w_topfill;
206# endif
207 vs->vs_botline = curwin->w_botline;
208 vs->vs_empty_rows = curwin->w_empty_rows;
209}
210
211 static void
212restore_viewstate(viewstate_T *vs)
213{
214 curwin->w_curswant = vs->vs_curswant;
215 curwin->w_leftcol = vs->vs_leftcol;
216 curwin->w_topline = vs->vs_topline;
217# ifdef FEAT_DIFF
218 curwin->w_topfill = vs->vs_topfill;
219# endif
220 curwin->w_botline = vs->vs_botline;
221 curwin->w_empty_rows = vs->vs_empty_rows;
222}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200223
224// Struct to store the state of 'incsearch' highlighting.
225typedef struct {
226 pos_T search_start; // where 'incsearch' starts searching
227 pos_T save_cursor;
228 viewstate_T init_viewstate;
229 viewstate_T old_viewstate;
230 pos_T match_start;
231 pos_T match_end;
232 int did_incsearch;
233 int incsearch_postponed;
234} incsearch_state_T;
235
236 static void
237init_incsearch_state(incsearch_state_T *is_state)
238{
239 is_state->match_start = curwin->w_cursor;
240 is_state->did_incsearch = FALSE;
241 is_state->incsearch_postponed = FALSE;
242 CLEAR_POS(&is_state->match_end);
243 is_state->save_cursor = curwin->w_cursor; // may be restored later
244 is_state->search_start = curwin->w_cursor;
245 save_viewstate(&is_state->init_viewstate);
246 save_viewstate(&is_state->old_viewstate);
247}
248
249/*
250 * First move cursor to end of match, then to the start. This
251 * moves the whole match onto the screen when 'nowrap' is set.
252 */
253 static void
254set_search_match(pos_T *t)
255{
256 t->lnum += search_match_lines;
257 t->col = search_match_endcol;
258 if (t->lnum > curbuf->b_ml.ml_line_count)
259 {
260 t->lnum = curbuf->b_ml.ml_line_count;
261 coladvance((colnr_T)MAXCOL);
262 }
263}
264
265/*
266 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200267 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200268 */
269 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200270do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
271 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200272{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
276 if (p_is && !cmd_silent)
277 {
278 // by default search all lines
279 search_first_line = 0;
280 search_last_line = MAXLNUM;
281
282 if (firstc == '/' || firstc == '?')
283 return TRUE;
284 if (firstc == ':')
285 {
286 char_u *cmd = skip_range(ccline.cmdbuff, NULL);
287 char_u *p;
288 int delim;
289 char_u *end;
290
291 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
292 {
293 // Skip over "substitute" to find the pattern separator.
294 for (p = cmd; ASCII_ISALPHA(*p); ++p)
295 ;
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200296 if (*p != NUL
297 && (STRNCMP(cmd, "substitute", p - cmd) == 0
298 || STRNCMP(cmd, "global", p - cmd) == 0
299 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200300 {
301 delim = *p++;
302 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200303 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200304 {
305 char_u *dummy;
306 exarg_T ea;
307 pos_T save_cursor = curwin->w_cursor;
308
309 // found a non-empty pattern
310 *skiplen = (int)(p - ccline.cmdbuff);
311 *patlen = (int)(end - p);
312
313 // parse the address range
314 vim_memset(&ea, 0, sizeof(ea));
315 ea.line1 = 1;
316 ea.line2 = 1;
317 ea.cmd = ccline.cmdbuff;
318 ea.addr_type = ADDR_LINES;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200319 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200320 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200321 if (ea.addr_count > 0)
322 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200323 // Allow for reverse match.
324 if (ea.line2 < ea.line1)
325 {
326 search_first_line = ea.line2;
327 search_last_line = ea.line1;
328 }
329 else
330 {
331 search_first_line = ea.line1;
332 search_last_line = ea.line2;
333 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200334 }
335 else if (*cmd == 's')
336 {
337 // :s defaults to the current line
338 search_first_line = curwin->w_cursor.lnum;
339 search_last_line = curwin->w_cursor.lnum;
340 }
341
342 curwin->w_cursor = save_cursor;
343 return TRUE;
344 }
345 }
346 }
347 }
348 }
349
350 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200351}
352
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200353 static void
354finish_incsearch_highlighting(
355 int gotesc,
356 incsearch_state_T *is_state,
357 int call_update_screen)
358{
359 if (is_state->did_incsearch)
360 {
361 is_state->did_incsearch = FALSE;
362 if (gotesc)
363 curwin->w_cursor = is_state->save_cursor;
364 else
365 {
366 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
367 {
368 // put the '" mark at the original position
369 curwin->w_cursor = is_state->save_cursor;
370 setpcmark();
371 }
372 curwin->w_cursor = is_state->search_start;
373 }
374 restore_viewstate(&is_state->old_viewstate);
375 highlight_match = FALSE;
376 validate_cursor(); /* needed for TAB */
377 if (call_update_screen)
378 update_screen(SOME_VALID);
379 else
380 redraw_all_later(SOME_VALID);
381 }
382}
383
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200384/*
385 * Do 'incsearch' highlighting if desired.
386 */
387 static void
388may_do_incsearch_highlighting(
389 int firstc,
390 long count,
391 incsearch_state_T *is_state)
392{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200393 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200394 int i;
395 pos_T end_pos;
396 struct cmdline_info save_ccline;
397#ifdef FEAT_RELTIME
398 proftime_T tm;
399#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200400 int next_char;
401 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200402
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200403 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200404 {
405 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200406 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200407 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200408
409 // If there is a character waiting, search and redraw later.
410 if (char_avail())
411 {
412 is_state->incsearch_postponed = TRUE;
413 return;
414 }
415 is_state->incsearch_postponed = FALSE;
416
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200417 if (search_first_line == 0)
418 // start at the original cursor position
419 curwin->w_cursor = is_state->search_start;
420 else
421 {
422 // start at the first line in the range
423 curwin->w_cursor.lnum = search_first_line;
424 curwin->w_cursor.col = 0;
425 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200426 save_last_search_pattern();
427
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200428 // Use the previous pattern for ":s//".
429 next_char = ccline.cmdbuff[skiplen + patlen];
430 use_last_pat = patlen == 0 && skiplen > 0
431 && ccline.cmdbuff[skiplen - 1] == next_char;
432
433 // If there is no pattern, don't do anything.
434 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200435 {
436 i = 0;
437 set_no_hlsearch(TRUE); // turn off previous highlight
438 redraw_all_later(SOME_VALID);
439 }
440 else
441 {
442 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
443
444 cursor_off(); // so the user knows we're busy
445 out_flush();
446 ++emsg_off; // so it doesn't beep if bad expr
447#ifdef FEAT_RELTIME
448 // Set the time limit to half a second.
449 profile_setlimit(500L, &tm);
450#endif
451 if (!p_hls)
452 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200453 if (search_first_line != 0)
454 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 ccline.cmdbuff[skiplen + patlen] = NUL;
456 i = do_search(NULL, firstc == ':' ? '/' : firstc,
457 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200458#ifdef FEAT_RELTIME
459 &tm, NULL
460#else
461 NULL, NULL
462#endif
463 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200464 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465 --emsg_off;
466
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200467 if (curwin->w_cursor.lnum < search_first_line
468 || curwin->w_cursor.lnum > search_last_line)
469 // match outside of address range
470 i = 0;
471
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472 // if interrupted while searching, behave like it failed
473 if (got_int)
474 {
475 (void)vpeekc(); // remove <C-C> from input stream
476 got_int = FALSE; // don't abandon the command line
477 i = 0;
478 }
479 else if (char_avail())
480 // cancelled searching because a char was typed
481 is_state->incsearch_postponed = TRUE;
482 }
483 if (i != 0)
484 highlight_match = TRUE; // highlight position
485 else
486 highlight_match = FALSE; // remove highlight
487
488 // First restore the old curwin values, so the screen is positioned in the
489 // same way as the actual search command.
490 restore_viewstate(&is_state->old_viewstate);
491 changed_cline_bef_curs();
492 update_topline();
493
494 if (i != 0)
495 {
496 pos_T save_pos = curwin->w_cursor;
497
498 is_state->match_start = curwin->w_cursor;
499 set_search_match(&curwin->w_cursor);
500 validate_cursor();
501 end_pos = curwin->w_cursor;
502 is_state->match_end = end_pos;
503 curwin->w_cursor = save_pos;
504 }
505 else
506 end_pos = curwin->w_cursor; // shutup gcc 4
507
508 // Disable 'hlsearch' highlighting if the pattern matches everything.
509 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200510 if (!use_last_pat)
511 {
512 next_char = ccline.cmdbuff[skiplen + patlen];
513 ccline.cmdbuff[skiplen + patlen] = NUL;
514 if (empty_pattern(ccline.cmdbuff))
515 set_no_hlsearch(TRUE);
516 ccline.cmdbuff[skiplen + patlen] = next_char;
517 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200518
519 validate_cursor();
520 // May redraw the status line to show the cursor position.
521 if (p_ru && curwin->w_status_height > 0)
522 curwin->w_redr_status = TRUE;
523
524 save_cmdline(&save_ccline);
525 update_screen(SOME_VALID);
526 restore_cmdline(&save_ccline);
527 restore_last_search_pattern();
528
529 // Leave it at the end to make CTRL-R CTRL-W work.
530 if (i != 0)
531 curwin->w_cursor = end_pos;
532
533 msg_starthere();
534 redrawcmdline();
535 is_state->did_incsearch = TRUE;
536}
537
538/*
539 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
540 * or previous match.
541 * Returns FAIL when jumping to cmdline_not_changed;
542 */
543 static int
544may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200545 int firstc,
546 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200548 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200550 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200551 pos_T t;
552 char_u *pat;
553 int search_flags = SEARCH_NOOF;
554 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200555 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200556
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200557 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200558 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200559 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200560 return FAIL;
561
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200562 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200563 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200565 skiplen = 0;
566 patlen = STRLEN(pat);
567 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200568 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200569 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200570
571 save_last_search_pattern();
572 cursor_off();
573 out_flush();
574 if (c == Ctrl_G)
575 {
576 t = is_state->match_end;
577 if (LT_POS(is_state->match_start, is_state->match_end))
578 // Start searching at the end of the match not at the beginning of
579 // the next column.
580 (void)decl(&t);
581 search_flags += SEARCH_COL;
582 }
583 else
584 t = is_state->match_start;
585 if (!p_hls)
586 search_flags += SEARCH_KEEP;
587 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200588 save = pat[patlen];
589 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200590 i = searchit(curwin, curbuf, &t,
591 c == Ctrl_G ? FORWARD : BACKWARD,
592 pat, count, search_flags,
593 RE_SEARCH, 0, NULL, NULL);
594 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200595 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 if (i)
597 {
598 is_state->search_start = is_state->match_start;
599 is_state->match_end = t;
600 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200601 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 {
603 // Move just before the current match, so that when nv_search
604 // finishes the cursor will be put back on the match.
605 is_state->search_start = t;
606 (void)decl(&is_state->search_start);
607 }
608 else if (c == Ctrl_G && firstc == '?')
609 {
610 // Move just after the current match, so that when nv_search
611 // finishes the cursor will be put back on the match.
612 is_state->search_start = t;
613 (void)incl(&is_state->search_start);
614 }
615 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
616 {
617 // wrap around
618 is_state->search_start = t;
619 if (firstc == '?')
620 (void)incl(&is_state->search_start);
621 else
622 (void)decl(&is_state->search_start);
623 }
624
625 set_search_match(&is_state->match_end);
626 curwin->w_cursor = is_state->match_start;
627 changed_cline_bef_curs();
628 update_topline();
629 validate_cursor();
630 highlight_match = TRUE;
631 save_viewstate(&is_state->old_viewstate);
632 update_screen(NOT_VALID);
633 redrawcmdline();
634 }
635 else
636 vim_beep(BO_ERROR);
637 restore_last_search_pattern();
638 return FAIL;
639}
640
641/*
642 * When CTRL-L typed: add character from the match to the pattern.
643 * May set "*c" to the added character.
644 * Return OK when jumping to cmdline_not_changed.
645 */
646 static int
647may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
648{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200649 int skiplen, patlen;
650
651 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200652 return FAIL;
653
654 // Add a character from under the cursor for 'incsearch'.
655 if (is_state->did_incsearch)
656 {
657 curwin->w_cursor = is_state->match_end;
658 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
659 {
660 *c = gchar_cursor();
661
662 // If 'ignorecase' and 'smartcase' are set and the
663 // command line has no uppercase characters, convert
664 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200665 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200666 *c = MB_TOLOWER(*c);
667 if (*c != NUL)
668 {
669 if (*c == firstc || vim_strchr((char_u *)(
670 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
671 {
672 // put a backslash before special characters
673 stuffcharReadbuff(*c);
674 *c = '\\';
675 }
676 return FAIL;
677 }
678 }
679 }
680 return OK;
681}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200682#endif
683
Bram Moolenaar66216052017-12-16 16:33:44 +0100684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 * getcmdline() - accept a command line starting with firstc.
686 *
687 * firstc == ':' get ":" command line.
688 * firstc == '/' or '?' get search pattern
689 * firstc == '=' get expression
690 * firstc == '@' get text for input() function
691 * firstc == '>' get text for debug mode
692 * firstc == NUL get text for :insert command
693 * firstc == -1 like NUL, and break on CTRL-C
694 *
695 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
696 * command line.
697 *
698 * Careful: getcmdline() can be called recursively!
699 *
700 * Return pointer to allocated string if there is a commandline, NULL
701 * otherwise.
702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100704getcmdline(
705 int firstc,
706 long count UNUSED, /* only used for incremental search */
707 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
709 int c;
710 int i;
711 int j;
712 int gotesc = FALSE; /* TRUE when <ESC> just typed */
713 int do_abbr; /* when TRUE check for abbr. */
714#ifdef FEAT_CMDHIST
715 char_u *lookfor = NULL; /* string to match */
716 int hiscnt; /* current history line in use */
717 int histype; /* history type to be used */
718#endif
719#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200720 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#endif
722 int did_wild_list = FALSE; /* did wild_list() recently */
723 int wim_index = 0; /* index in wim_flags[] */
724 int res;
725 int save_msg_scroll = msg_scroll;
726 int save_State = State; /* remember State when called */
727 int some_key_typed = FALSE; /* one of the keys was typed */
728#ifdef FEAT_MOUSE
729 /* mouse drag and release events are ignored, unless they are
730 * preceded with a mouse down event */
731 int ignore_drag_release = TRUE;
732#endif
733#ifdef FEAT_EVAL
734 int break_ctrl_c = FALSE;
735#endif
736 expand_T xpc;
737 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200738#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000739 /* Everything that may work recursively should save and restore the
740 * current command line in save_ccline. That includes update_screen(), a
741 * custom status line may invoke ":normal". */
742 struct cmdline_info save_ccline;
743#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200744 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746#ifdef FEAT_EVAL
747 if (firstc == -1)
748 {
749 firstc = NUL;
750 break_ctrl_c = TRUE;
751 }
752#endif
753#ifdef FEAT_RIGHTLEFT
754 /* start without Hebrew mapping for a command line */
755 if (firstc == ':' || firstc == '=' || firstc == '>')
756 cmd_hkmap = 0;
757#endif
758
759 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200762 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
764
765 /*
766 * set some variables for redrawcmd()
767 */
768 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000769 ccline.cmdindent = (firstc > 0 ? indent : 0);
770
771 /* alloc initial ccline.cmdbuff */
772 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 if (ccline.cmdbuff == NULL)
774 return NULL; /* out of memory */
775 ccline.cmdlen = ccline.cmdpos = 0;
776 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100777 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000779 /* autoindent for :insert and :append */
780 if (firstc <= 0)
781 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200782 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000783 ccline.cmdbuff[indent] = NUL;
784 ccline.cmdpos = indent;
785 ccline.cmdspos = indent;
786 ccline.cmdlen = indent;
787 }
788
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000790 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791
792#ifdef FEAT_RIGHTLEFT
793 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
794 && (firstc == '/' || firstc == '?'))
795 cmdmsg_rl = TRUE;
796 else
797 cmdmsg_rl = FALSE;
798#endif
799
800 redir_off = TRUE; /* don't redirect the typed command */
801 if (!cmd_silent)
802 {
803 i = msg_scrolled;
804 msg_scrolled = 0; /* avoid wait_return message */
805 gotocmdline(TRUE);
806 msg_scrolled += i;
807 redrawcmdprompt(); /* draw prompt or indent */
808 set_cmdspos();
809 }
810 xpc.xp_context = EXPAND_NOTHING;
811 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000812#ifndef BACKSLASH_IN_FILENAME
813 xpc.xp_shell = FALSE;
814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000816#if defined(FEAT_EVAL)
817 if (ccline.input_fn)
818 {
819 xpc.xp_context = ccline.xp_context;
820 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000821# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000822 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000823# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000824 }
825#endif
826
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 /*
828 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
829 * doing ":@0" when register 0 doesn't contain a CR.
830 */
831 msg_scroll = FALSE;
832
833 State = CMDLINE;
834
835 if (firstc == '/' || firstc == '?' || firstc == '@')
836 {
837 /* Use ":lmap" mappings for search pattern and input(). */
838 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
839 b_im_ptr = &curbuf->b_p_iminsert;
840 else
841 b_im_ptr = &curbuf->b_p_imsearch;
842 if (*b_im_ptr == B_IMODE_LMAP)
843 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100844#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 im_set_active(*b_im_ptr == B_IMODE_IM);
846#endif
847 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100848#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 else if (p_imcmdline)
850 im_set_active(TRUE);
851#endif
852
853#ifdef FEAT_MOUSE
854 setmouse();
855#endif
856#ifdef CURSOR_SHAPE
857 ui_cursor_shape(); /* may show different cursor shape */
858#endif
859
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000860 /* When inside an autocommand for writing "exiting" may be set and
861 * terminal mode set to cooked. Need to set raw mode here then. */
862 settmode(TMODE_RAW);
863
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200864 /* Trigger CmdlineEnter autocommands. */
865 cmdline_type = firstc == NUL ? '-' : firstc;
866 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200867
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868#ifdef FEAT_CMDHIST
869 init_history();
870 hiscnt = hislen; /* set hiscnt to impossible history value */
871 histype = hist_char2type(firstc);
872#endif
873
874#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000875 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876#endif
877
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200878 /* If something above caused an error, reset the flags, we do want to type
879 * and execute commands. Display may be messed up a bit. */
880 if (did_emsg)
881 redrawcmd();
882 did_emsg = FALSE;
883 got_int = FALSE;
884
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 /*
886 * Collect the command string, handling editing keys.
887 */
888 for (;;)
889 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000890 redir_off = TRUE; /* Don't redirect the typed command.
891 Repeated, because a ":redir" inside
892 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893#ifdef USE_ON_FLY_SCROLL
894 dont_scroll = FALSE; /* allow scrolling here */
895#endif
896 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
897
Bram Moolenaar72532d32018-04-07 19:09:09 +0200898 did_emsg = FALSE; /* There can't really be a reason why an error
899 that occurs while typing a command should
900 cause the command not to be executed. */
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000903
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100904 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
905 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000906 do
907 {
908 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100909 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 if (KeyTyped)
912 {
913 some_key_typed = TRUE;
914#ifdef FEAT_RIGHTLEFT
915 if (cmd_hkmap)
916 c = hkmap(c);
917# ifdef FEAT_FKMAP
918 if (cmd_fkmap)
919 c = cmdl_fkmap(c);
920# endif
921 if (cmdmsg_rl && !KeyStuffed)
922 {
923 /* Invert horizontal movements and operations. Only when
924 * typed by the user directly, not when the result of a
925 * mapping. */
926 switch (c)
927 {
928 case K_RIGHT: c = K_LEFT; break;
929 case K_S_RIGHT: c = K_S_LEFT; break;
930 case K_C_RIGHT: c = K_C_LEFT; break;
931 case K_LEFT: c = K_RIGHT; break;
932 case K_S_LEFT: c = K_S_RIGHT; break;
933 case K_C_LEFT: c = K_C_RIGHT; break;
934 }
935 }
936#endif
937 }
938
939 /*
940 * Ignore got_int when CTRL-C was typed here.
941 * Don't ignore it in :global, we really need to break then, e.g., for
942 * ":g/pat/normal /pat" (without the <CR>).
943 * Don't ignore it for the input() function.
944 */
945 if ((c == Ctrl_C
946#ifdef UNIX
947 || c == intr_char
948#endif
949 )
950#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
951 && firstc != '@'
952#endif
953#ifdef FEAT_EVAL
954 && !break_ctrl_c
955#endif
956 && !global_busy)
957 got_int = FALSE;
958
959#ifdef FEAT_CMDHIST
960 /* free old command line when finished moving around in the history
961 * list */
962 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000963 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000964 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 && c != K_PAGEDOWN && c != K_PAGEUP
966 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000967 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100969 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970#endif
971
972 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000973 * When there are matching completions to select <S-Tab> works like
974 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000976 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 c = Ctrl_P;
978
979#ifdef FEAT_WILDMENU
980 /* Special translations for 'wildmenu' */
981 if (did_wild_list && p_wmnu)
982 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000983 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000985 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 c = Ctrl_N;
987 }
988 /* Hitting CR after "emenu Name.": complete submenu */
989 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
990 && ccline.cmdpos > 1
991 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
992 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
993 && (c == '\n' || c == '\r' || c == K_KENTER))
994 c = K_DOWN;
995#endif
996
997 /* free expanded names when finished walking through matches */
998 if (xpc.xp_numfiles != -1
999 && !(c == p_wc && KeyTyped) && c != p_wcm
1000 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1001 && c != Ctrl_L)
1002 {
1003 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1004 did_wild_list = FALSE;
1005#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001006 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007#endif
1008 xpc.xp_context = EXPAND_NOTHING;
1009 wim_index = 0;
1010#ifdef FEAT_WILDMENU
1011 if (p_wmnu && wild_menu_showing != 0)
1012 {
1013 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001014 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001015
1016 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001017 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018
1019 if (wild_menu_showing == WM_SCROLLED)
1020 {
1021 /* Entered command line, move it up */
1022 cmdline_row--;
1023 redrawcmd();
1024 }
1025 else if (save_p_ls != -1)
1026 {
1027 /* restore 'laststatus' and 'winminheight' */
1028 p_ls = save_p_ls;
1029 p_wmh = save_p_wmh;
1030 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001031 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001033 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 redrawcmd();
1035 save_p_ls = -1;
1036 }
1037 else
1038 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 redraw_statuslines();
1041 }
1042 KeyTyped = skt;
1043 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001044 if (ccline.input_fn)
1045 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046 }
1047#endif
1048 }
1049
1050#ifdef FEAT_WILDMENU
1051 /* Special translations for 'wildmenu' */
1052 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1053 {
1054 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001055 if (c == K_DOWN && ccline.cmdpos > 0
1056 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001058 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 /* Hitting <Up>: Remove one submenu name in front of the
1061 * cursor */
1062 int found = FALSE;
1063
1064 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1065 i = 0;
1066 while (--j > 0)
1067 {
1068 /* check for start of menu name */
1069 if (ccline.cmdbuff[j] == ' '
1070 && ccline.cmdbuff[j - 1] != '\\')
1071 {
1072 i = j + 1;
1073 break;
1074 }
1075 /* check for start of submenu name */
1076 if (ccline.cmdbuff[j] == '.'
1077 && ccline.cmdbuff[j - 1] != '\\')
1078 {
1079 if (found)
1080 {
1081 i = j + 1;
1082 break;
1083 }
1084 else
1085 found = TRUE;
1086 }
1087 }
1088 if (i > 0)
1089 cmdline_del(i);
1090 c = p_wc;
1091 xpc.xp_context = EXPAND_NOTHING;
1092 }
1093 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001094 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001095 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001096 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 {
1098 char_u upseg[5];
1099
1100 upseg[0] = PATHSEP;
1101 upseg[1] = '.';
1102 upseg[2] = '.';
1103 upseg[3] = PATHSEP;
1104 upseg[4] = NUL;
1105
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001106 if (c == K_DOWN
1107 && ccline.cmdpos > 0
1108 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1109 && (ccline.cmdpos < 3
1110 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1112 {
1113 /* go down a directory */
1114 c = p_wc;
1115 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001116 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
1118 /* If in a direct ancestor, strip off one ../ to go down */
1119 int found = FALSE;
1120
1121 j = ccline.cmdpos;
1122 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1123 while (--j > i)
1124 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001125#ifdef FEAT_MBYTE
1126 if (has_mbyte)
1127 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 if (vim_ispathsep(ccline.cmdbuff[j]))
1130 {
1131 found = TRUE;
1132 break;
1133 }
1134 }
1135 if (found
1136 && ccline.cmdbuff[j - 1] == '.'
1137 && ccline.cmdbuff[j - 2] == '.'
1138 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1139 {
1140 cmdline_del(j - 2);
1141 c = p_wc;
1142 }
1143 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001144 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 {
1146 /* go up a directory */
1147 int found = FALSE;
1148
1149 j = ccline.cmdpos - 1;
1150 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1151 while (--j > i)
1152 {
1153#ifdef FEAT_MBYTE
1154 if (has_mbyte)
1155 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1156#endif
1157 if (vim_ispathsep(ccline.cmdbuff[j])
1158#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001159 && vim_strchr((char_u *)" *?[{`$%#",
1160 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161#endif
1162 )
1163 {
1164 if (found)
1165 {
1166 i = j + 1;
1167 break;
1168 }
1169 else
1170 found = TRUE;
1171 }
1172 }
1173
1174 if (!found)
1175 j = i;
1176 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1177 j += 4;
1178 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1179 && j == i)
1180 j += 3;
1181 else
1182 j = 0;
1183 if (j > 0)
1184 {
1185 /* TODO this is only for DOS/UNIX systems - need to put in
1186 * machine-specific stuff here and in upseg init */
1187 cmdline_del(j);
1188 put_on_cmdline(upseg + 1, 3, FALSE);
1189 }
1190 else if (ccline.cmdpos > i)
1191 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001192
1193 /* Now complete in the new directory. Set KeyTyped in case the
1194 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001196 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 }
1198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199
1200#endif /* FEAT_WILDMENU */
1201
1202 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1203 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1204 if (c == Ctrl_BSL)
1205 {
1206 ++no_mapping;
1207 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001208 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 --no_mapping;
1210 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001211 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1212 * is in a mapping. */
1213 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1214 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
1216 vungetc(c);
1217 c = Ctrl_BSL;
1218 }
1219#ifdef FEAT_EVAL
1220 else if (c == 'e')
1221 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001222 char_u *p = NULL;
1223 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224
1225 /*
1226 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001227 * Need to save and restore the current command line, to be
1228 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 */
1230 if (ccline.cmdpos == ccline.cmdlen)
1231 new_cmdpos = 99999; /* keep it at the end */
1232 else
1233 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001234
1235 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001237 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 if (c == '=')
1239 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001240 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001241 * to avoid nasty things like going to another buffer when
1242 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001243 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001244 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001246 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001247 restore_cmdline(&save_ccline);
1248
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001249 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001251 len = (int)STRLEN(p);
1252 if (realloc_cmdbuff(len + 1) == OK)
1253 {
1254 ccline.cmdlen = len;
1255 STRCPY(ccline.cmdbuff, p);
1256 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001258 /* Restore the cursor or use the position set with
1259 * set_cmdline_pos(). */
1260 if (new_cmdpos > ccline.cmdlen)
1261 ccline.cmdpos = ccline.cmdlen;
1262 else
1263 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001265 KeyTyped = FALSE; /* Don't do p_wc completion. */
1266 redrawcmd();
1267 goto cmdline_changed;
1268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270 }
1271 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001272 got_int = FALSE; /* don't abandon the command line */
1273 did_emsg = FALSE;
1274 emsg_on_display = FALSE;
1275 redrawcmd();
1276 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 }
1278#endif
1279 else
1280 {
1281 if (c == Ctrl_G && p_im && restart_edit == 0)
1282 restart_edit = 'a';
1283 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1284 in history */
1285 goto returncmd; /* back to Normal mode */
1286 }
1287 }
1288
1289#ifdef FEAT_CMDWIN
1290 if (c == cedit_key || c == K_CMDWIN)
1291 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001292 if (ex_normal_busy == 0 && got_int == FALSE)
1293 {
1294 /*
1295 * Open a window to edit the command line (and history).
1296 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001297 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001298 some_key_typed = TRUE;
1299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 }
1301# ifdef FEAT_DIGRAPHS
1302 else
1303# endif
1304#endif
1305#ifdef FEAT_DIGRAPHS
1306 c = do_digraph(c);
1307#endif
1308
1309 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1310 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1311 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001312 /* In Ex mode a backslash escapes a newline. */
1313 if (exmode_active
1314 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001315 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001316 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001317 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001319 if (c == K_KENTER)
1320 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001322 else
1323 {
1324 gotesc = FALSE; /* Might have typed ESC previously, don't
1325 truncate the cmdline now. */
1326 if (ccheck_abbr(c + ABBR_OFF))
1327 goto cmdline_changed;
1328 if (!cmd_silent)
1329 {
1330 windgoto(msg_row, 0);
1331 out_flush();
1332 }
1333 break;
1334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 }
1336
1337 /*
1338 * Completion for 'wildchar' or 'wildcharm' key.
1339 * - hitting <ESC> twice means: abandon command line.
1340 * - wildcard expansion is only done when the 'wildchar' key is really
1341 * typed, not when it comes from a macro
1342 */
1343 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1344 {
1345 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1346 {
1347 /* if 'wildmode' contains "list" may still need to list */
1348 if (xpc.xp_numfiles > 1
1349 && !did_wild_list
1350 && (wim_flags[wim_index] & WIM_LIST))
1351 {
1352 (void)showmatches(&xpc, FALSE);
1353 redrawcmd();
1354 did_wild_list = TRUE;
1355 }
1356 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001357 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1358 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001360 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1361 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 else
1363 res = OK; /* don't insert 'wildchar' now */
1364 }
1365 else /* typed p_wc first time */
1366 {
1367 wim_index = 0;
1368 j = ccline.cmdpos;
1369 /* if 'wildmode' first contains "longest", get longest
1370 * common part */
1371 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001372 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1373 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001375 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1376 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377
1378 /* if interrupted while completing, behave like it failed */
1379 if (got_int)
1380 {
1381 (void)vpeekc(); /* remove <C-C> from input stream */
1382 got_int = FALSE; /* don't abandon the command line */
1383 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1384#ifdef FEAT_WILDMENU
1385 xpc.xp_context = EXPAND_NOTHING;
1386#endif
1387 goto cmdline_changed;
1388 }
1389
1390 /* when more than one match, and 'wildmode' first contains
1391 * "list", or no change and 'wildmode' contains "longest,list",
1392 * list all matches */
1393 if (res == OK && xpc.xp_numfiles > 1)
1394 {
1395 /* a "longest" that didn't do anything is skipped (but not
1396 * "list:longest") */
1397 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1398 wim_index = 1;
1399 if ((wim_flags[wim_index] & WIM_LIST)
1400#ifdef FEAT_WILDMENU
1401 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1402#endif
1403 )
1404 {
1405 if (!(wim_flags[0] & WIM_LONGEST))
1406 {
1407#ifdef FEAT_WILDMENU
1408 int p_wmnu_save = p_wmnu;
1409 p_wmnu = 0;
1410#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001411 /* remove match */
1412 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413#ifdef FEAT_WILDMENU
1414 p_wmnu = p_wmnu_save;
1415#endif
1416 }
1417#ifdef FEAT_WILDMENU
1418 (void)showmatches(&xpc, p_wmnu
1419 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1420#else
1421 (void)showmatches(&xpc, FALSE);
1422#endif
1423 redrawcmd();
1424 did_wild_list = TRUE;
1425 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001426 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1427 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001429 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1430 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 }
1432 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001433 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 }
1435#ifdef FEAT_WILDMENU
1436 else if (xpc.xp_numfiles == -1)
1437 xpc.xp_context = EXPAND_NOTHING;
1438#endif
1439 }
1440 if (wim_index < 3)
1441 ++wim_index;
1442 if (c == ESC)
1443 gotesc = TRUE;
1444 if (res == OK)
1445 goto cmdline_changed;
1446 }
1447
1448 gotesc = FALSE;
1449
1450 /* <S-Tab> goes to last match, in a clumsy way */
1451 if (c == K_S_TAB && KeyTyped)
1452 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001453 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1454 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1455 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 goto cmdline_changed;
1457 }
1458
1459 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1460 c = NL;
1461
1462 do_abbr = TRUE; /* default: check for abbreviation */
1463
1464 /*
1465 * Big switch for a typed command line character.
1466 */
1467 switch (c)
1468 {
1469 case K_BS:
1470 case Ctrl_H:
1471 case K_DEL:
1472 case K_KDEL:
1473 case Ctrl_W:
1474#ifdef FEAT_FKMAP
1475 if (cmd_fkmap && c == K_BS)
1476 c = K_DEL;
1477#endif
1478 if (c == K_KDEL)
1479 c = K_DEL;
1480
1481 /*
1482 * delete current character is the same as backspace on next
1483 * character, except at end of line
1484 */
1485 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1486 ++ccline.cmdpos;
1487#ifdef FEAT_MBYTE
1488 if (has_mbyte && c == K_DEL)
1489 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1490 ccline.cmdbuff + ccline.cmdpos);
1491#endif
1492 if (ccline.cmdpos > 0)
1493 {
1494 char_u *p;
1495
1496 j = ccline.cmdpos;
1497 p = ccline.cmdbuff + j;
1498#ifdef FEAT_MBYTE
1499 if (has_mbyte)
1500 {
1501 p = mb_prevptr(ccline.cmdbuff, p);
1502 if (c == Ctrl_W)
1503 {
1504 while (p > ccline.cmdbuff && vim_isspace(*p))
1505 p = mb_prevptr(ccline.cmdbuff, p);
1506 i = mb_get_class(p);
1507 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1508 p = mb_prevptr(ccline.cmdbuff, p);
1509 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001510 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 }
1512 }
1513 else
1514#endif
1515 if (c == Ctrl_W)
1516 {
1517 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1518 --p;
1519 i = vim_iswordc(p[-1]);
1520 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1521 && vim_iswordc(p[-1]) == i)
1522 --p;
1523 }
1524 else
1525 --p;
1526 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1527 ccline.cmdlen -= j - ccline.cmdpos;
1528 i = ccline.cmdpos;
1529 while (i < ccline.cmdlen)
1530 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1531
1532 /* Truncate at the end, required for multi-byte chars. */
1533 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001534#ifdef FEAT_SEARCH_EXTRA
1535 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001536 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001537 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001538 /* save view settings, so that the screen
1539 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001540 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001541 }
1542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 redrawcmd();
1544 }
1545 else if (ccline.cmdlen == 0 && c != Ctrl_W
1546 && ccline.cmdprompt == NULL && indent == 0)
1547 {
1548 /* In ex and debug mode it doesn't make sense to return. */
1549 if (exmode_active
1550#ifdef FEAT_EVAL
1551 || ccline.cmdfirstc == '>'
1552#endif
1553 )
1554 goto cmdline_not_changed;
1555
Bram Moolenaard23a8232018-02-10 18:45:26 +01001556 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 if (!cmd_silent)
1558 {
1559#ifdef FEAT_RIGHTLEFT
1560 if (cmdmsg_rl)
1561 msg_col = Columns;
1562 else
1563#endif
1564 msg_col = 0;
1565 msg_putchar(' '); /* delete ':' */
1566 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001567#ifdef FEAT_SEARCH_EXTRA
1568 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001569 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 redraw_cmdline = TRUE;
1572 goto returncmd; /* back to cmd mode */
1573 }
1574 goto cmdline_changed;
1575
1576 case K_INS:
1577 case K_KINS:
1578#ifdef FEAT_FKMAP
1579 /* if Farsi mode set, we are in reverse insert mode -
1580 Do not change the mode */
1581 if (cmd_fkmap)
1582 beep_flush();
1583 else
1584#endif
1585 ccline.overstrike = !ccline.overstrike;
1586#ifdef CURSOR_SHAPE
1587 ui_cursor_shape(); /* may show different cursor shape */
1588#endif
1589 goto cmdline_not_changed;
1590
1591 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001592 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 {
1594 /* ":lmap" mappings exists, toggle use of mappings. */
1595 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001596#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 im_set_active(FALSE); /* Disable input method */
1598#endif
1599 if (b_im_ptr != NULL)
1600 {
1601 if (State & LANGMAP)
1602 *b_im_ptr = B_IMODE_LMAP;
1603 else
1604 *b_im_ptr = B_IMODE_NONE;
1605 }
1606 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001607#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 else
1609 {
1610 /* There are no ":lmap" mappings, toggle IM. When
1611 * 'imdisable' is set don't try getting the status, it's
1612 * always off. */
1613 if ((p_imdisable && b_im_ptr != NULL)
1614 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1615 {
1616 im_set_active(FALSE); /* Disable input method */
1617 if (b_im_ptr != NULL)
1618 *b_im_ptr = B_IMODE_NONE;
1619 }
1620 else
1621 {
1622 im_set_active(TRUE); /* Enable input method */
1623 if (b_im_ptr != NULL)
1624 *b_im_ptr = B_IMODE_IM;
1625 }
1626 }
1627#endif
1628 if (b_im_ptr != NULL)
1629 {
1630 if (b_im_ptr == &curbuf->b_p_iminsert)
1631 set_iminsert_global();
1632 else
1633 set_imsearch_global();
1634 }
1635#ifdef CURSOR_SHAPE
1636 ui_cursor_shape(); /* may show different cursor shape */
1637#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001638#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 /* Show/unshow value of 'keymap' in status lines later. */
1640 status_redraw_curbuf();
1641#endif
1642 goto cmdline_not_changed;
1643
1644/* case '@': only in very old vi */
1645 case Ctrl_U:
1646 /* delete all characters left of the cursor */
1647 j = ccline.cmdpos;
1648 ccline.cmdlen -= j;
1649 i = ccline.cmdpos = 0;
1650 while (i < ccline.cmdlen)
1651 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1652 /* Truncate at the end, required for multi-byte chars. */
1653 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001654#ifdef FEAT_SEARCH_EXTRA
1655 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001656 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 redrawcmd();
1659 goto cmdline_changed;
1660
1661#ifdef FEAT_CLIPBOARD
1662 case Ctrl_Y:
1663 /* Copy the modeless selection, if there is one. */
1664 if (clip_star.state != SELECT_CLEARED)
1665 {
1666 if (clip_star.state == SELECT_DONE)
1667 clip_copy_modeless_selection(TRUE);
1668 goto cmdline_not_changed;
1669 }
1670 break;
1671#endif
1672
1673 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1674 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001675 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001676 * ":normal" runs out of characters. */
1677 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001678 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679 goto cmdline_not_changed;
1680
1681 gotesc = TRUE; /* will free ccline.cmdbuff after
1682 putting it in history */
1683 goto returncmd; /* back to cmd mode */
1684
1685 case Ctrl_R: /* insert register */
1686#ifdef USE_ON_FLY_SCROLL
1687 dont_scroll = TRUE; /* disallow scrolling here */
1688#endif
1689 putcmdline('"', TRUE);
1690 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001691 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 if (i == Ctrl_O)
1693 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1694 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001695 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001696 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 --no_mapping;
1698#ifdef FEAT_EVAL
1699 /*
1700 * Insert the result of an expression.
1701 * Need to save the current command line, to be able to enter
1702 * a new one...
1703 */
1704 new_cmdpos = -1;
1705 if (c == '=')
1706 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1708 {
1709 beep_flush();
1710 c = ESC;
1711 }
1712 else
1713 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001714 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001716 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 }
1718 }
1719#endif
1720 if (c != ESC) /* use ESC to cancel inserting register */
1721 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001722 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001723
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001724#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001725 /* When there was a serious error abort getting the
1726 * command line. */
1727 if (aborting())
1728 {
1729 gotesc = TRUE; /* will free ccline.cmdbuff after
1730 putting it in history */
1731 goto returncmd; /* back to cmd mode */
1732 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 KeyTyped = FALSE; /* Don't do p_wc completion. */
1735#ifdef FEAT_EVAL
1736 if (new_cmdpos >= 0)
1737 {
1738 /* set_cmdline_pos() was used */
1739 if (new_cmdpos > ccline.cmdlen)
1740 ccline.cmdpos = ccline.cmdlen;
1741 else
1742 ccline.cmdpos = new_cmdpos;
1743 }
1744#endif
1745 }
1746 redrawcmd();
1747 goto cmdline_changed;
1748
1749 case Ctrl_D:
1750 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1751 break; /* Use ^D as normal char instead */
1752
1753 redrawcmd();
1754 continue; /* don't do incremental search now */
1755
1756 case K_RIGHT:
1757 case K_S_RIGHT:
1758 case K_C_RIGHT:
1759 do
1760 {
1761 if (ccline.cmdpos >= ccline.cmdlen)
1762 break;
1763 i = cmdline_charsize(ccline.cmdpos);
1764 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1765 break;
1766 ccline.cmdspos += i;
1767#ifdef FEAT_MBYTE
1768 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001769 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 + ccline.cmdpos);
1771 else
1772#endif
1773 ++ccline.cmdpos;
1774 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001775 while ((c == K_S_RIGHT || c == K_C_RIGHT
1776 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1778#ifdef FEAT_MBYTE
1779 if (has_mbyte)
1780 set_cmdspos_cursor();
1781#endif
1782 goto cmdline_not_changed;
1783
1784 case K_LEFT:
1785 case K_S_LEFT:
1786 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001787 if (ccline.cmdpos == 0)
1788 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 do
1790 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 --ccline.cmdpos;
1792#ifdef FEAT_MBYTE
1793 if (has_mbyte) /* move to first byte of char */
1794 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1795 ccline.cmdbuff + ccline.cmdpos);
1796#endif
1797 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1798 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001799 while (ccline.cmdpos > 0
1800 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001801 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1803#ifdef FEAT_MBYTE
1804 if (has_mbyte)
1805 set_cmdspos_cursor();
1806#endif
1807 goto cmdline_not_changed;
1808
1809 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001810 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001811 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812
Bram Moolenaar4770d092006-01-12 23:22:24 +00001813#ifdef FEAT_GUI_W32
1814 /* On Win32 ignore <M-F4>, we get it when closing the window was
1815 * cancelled. */
1816 case K_F4:
1817 if (mod_mask == MOD_MASK_ALT)
1818 {
1819 redrawcmd(); /* somehow the cmdline is cleared */
1820 goto cmdline_not_changed;
1821 }
1822 break;
1823#endif
1824
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825#ifdef FEAT_MOUSE
1826 case K_MIDDLEDRAG:
1827 case K_MIDDLERELEASE:
1828 goto cmdline_not_changed; /* Ignore mouse */
1829
1830 case K_MIDDLEMOUSE:
1831# ifdef FEAT_GUI
1832 /* When GUI is active, also paste when 'mouse' is empty */
1833 if (!gui.in_use)
1834# endif
1835 if (!mouse_has(MOUSE_COMMAND))
1836 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001837# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001839 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001841# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001842 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 redrawcmd();
1844 goto cmdline_changed;
1845
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001846# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001848 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 redrawcmd();
1850 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852
1853 case K_LEFTDRAG:
1854 case K_LEFTRELEASE:
1855 case K_RIGHTDRAG:
1856 case K_RIGHTRELEASE:
1857 /* Ignore drag and release events when the button-down wasn't
1858 * seen before. */
1859 if (ignore_drag_release)
1860 goto cmdline_not_changed;
1861 /* FALLTHROUGH */
1862 case K_LEFTMOUSE:
1863 case K_RIGHTMOUSE:
1864 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1865 ignore_drag_release = TRUE;
1866 else
1867 ignore_drag_release = FALSE;
1868# ifdef FEAT_GUI
1869 /* When GUI is active, also move when 'mouse' is empty */
1870 if (!gui.in_use)
1871# endif
1872 if (!mouse_has(MOUSE_COMMAND))
1873 goto cmdline_not_changed; /* Ignore mouse */
1874# ifdef FEAT_CLIPBOARD
1875 if (mouse_row < cmdline_row && clip_star.available)
1876 {
1877 int button, is_click, is_drag;
1878
1879 /*
1880 * Handle modeless selection.
1881 */
1882 button = get_mouse_button(KEY2TERMCAP1(c),
1883 &is_click, &is_drag);
1884 if (mouse_model_popup() && button == MOUSE_LEFT
1885 && (mod_mask & MOD_MASK_SHIFT))
1886 {
1887 /* Translate shift-left to right button. */
1888 button = MOUSE_RIGHT;
1889 mod_mask &= ~MOD_MASK_SHIFT;
1890 }
1891 clip_modeless(button, is_click, is_drag);
1892 goto cmdline_not_changed;
1893 }
1894# endif
1895
1896 set_cmdspos();
1897 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1898 ++ccline.cmdpos)
1899 {
1900 i = cmdline_charsize(ccline.cmdpos);
1901 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1902 && mouse_col < ccline.cmdspos % Columns + i)
1903 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001904# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (has_mbyte)
1906 {
1907 /* Count ">" for double-wide char that doesn't fit. */
1908 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001909 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 + ccline.cmdpos) - 1;
1911 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 ccline.cmdspos += i;
1914 }
1915 goto cmdline_not_changed;
1916
1917 /* Mouse scroll wheel: ignored here */
1918 case K_MOUSEDOWN:
1919 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001920 case K_MOUSELEFT:
1921 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 /* Alternate buttons ignored here */
1923 case K_X1MOUSE:
1924 case K_X1DRAG:
1925 case K_X1RELEASE:
1926 case K_X2MOUSE:
1927 case K_X2DRAG:
1928 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001929 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 goto cmdline_not_changed;
1931
1932#endif /* FEAT_MOUSE */
1933
1934#ifdef FEAT_GUI
1935 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1936 case K_LEFTRELEASE_NM:
1937 goto cmdline_not_changed;
1938
1939 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001940 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 {
1942 gui_do_scroll();
1943 redrawcmd();
1944 }
1945 goto cmdline_not_changed;
1946
1947 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001948 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001950 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 redrawcmd();
1952 }
1953 goto cmdline_not_changed;
1954#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001955#ifdef FEAT_GUI_TABLINE
1956 case K_TABLINE:
1957 case K_TABMENU:
1958 /* Don't want to change any tabs here. Make sure the same tab
1959 * is still selected. */
1960 if (gui_use_tabline())
1961 gui_mch_set_curtab(tabpage_index(curtab));
1962 goto cmdline_not_changed;
1963#endif
1964
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 case K_SELECT: /* end of Select mode mapping - ignore */
1966 goto cmdline_not_changed;
1967
1968 case Ctrl_B: /* begin of command line */
1969 case K_HOME:
1970 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 case K_S_HOME:
1972 case K_C_HOME:
1973 ccline.cmdpos = 0;
1974 set_cmdspos();
1975 goto cmdline_not_changed;
1976
1977 case Ctrl_E: /* end of command line */
1978 case K_END:
1979 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 case K_S_END:
1981 case K_C_END:
1982 ccline.cmdpos = ccline.cmdlen;
1983 set_cmdspos_cursor();
1984 goto cmdline_not_changed;
1985
1986 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001987 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 break;
1989 goto cmdline_changed;
1990
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001991 case Ctrl_L:
1992#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001993 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001994 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001995#endif
1996
1997 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001998 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 break;
2000 goto cmdline_changed;
2001
2002 case Ctrl_N: /* next match */
2003 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002004 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002006 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2007 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002009 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002012 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 case K_UP:
2014 case K_DOWN:
2015 case K_S_UP:
2016 case K_S_DOWN:
2017 case K_PAGEUP:
2018 case K_KPAGEUP:
2019 case K_PAGEDOWN:
2020 case K_KPAGEDOWN:
2021 if (hislen == 0 || firstc == NUL) /* no history */
2022 goto cmdline_not_changed;
2023
2024 i = hiscnt;
2025
2026 /* save current command string so it can be restored later */
2027 if (lookfor == NULL)
2028 {
2029 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2030 goto cmdline_not_changed;
2031 lookfor[ccline.cmdpos] = NUL;
2032 }
2033
2034 j = (int)STRLEN(lookfor);
2035 for (;;)
2036 {
2037 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002038 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002039 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 {
2041 if (hiscnt == hislen) /* first time */
2042 hiscnt = hisidx[histype];
2043 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2044 hiscnt = hislen - 1;
2045 else if (hiscnt != hisidx[histype] + 1)
2046 --hiscnt;
2047 else /* at top of list */
2048 {
2049 hiscnt = i;
2050 break;
2051 }
2052 }
2053 else /* one step forwards */
2054 {
2055 /* on last entry, clear the line */
2056 if (hiscnt == hisidx[histype])
2057 {
2058 hiscnt = hislen;
2059 break;
2060 }
2061
2062 /* not on a history line, nothing to do */
2063 if (hiscnt == hislen)
2064 break;
2065 if (hiscnt == hislen - 1) /* wrap around */
2066 hiscnt = 0;
2067 else
2068 ++hiscnt;
2069 }
2070 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2071 {
2072 hiscnt = i;
2073 break;
2074 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002075 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002076 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 || STRNCMP(history[histype][hiscnt].hisstr,
2078 lookfor, (size_t)j) == 0)
2079 break;
2080 }
2081
2082 if (hiscnt != i) /* jumped to other entry */
2083 {
2084 char_u *p;
2085 int len;
2086 int old_firstc;
2087
2088 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002089 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 if (hiscnt == hislen)
2091 p = lookfor; /* back to the old one */
2092 else
2093 p = history[histype][hiscnt].hisstr;
2094
2095 if (histype == HIST_SEARCH
2096 && p != lookfor
2097 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2098 {
2099 /* Correct for the separator character used when
2100 * adding the history entry vs the one used now.
2101 * First loop: count length.
2102 * Second loop: copy the characters. */
2103 for (i = 0; i <= 1; ++i)
2104 {
2105 len = 0;
2106 for (j = 0; p[j] != NUL; ++j)
2107 {
2108 /* Replace old sep with new sep, unless it is
2109 * escaped. */
2110 if (p[j] == old_firstc
2111 && (j == 0 || p[j - 1] != '\\'))
2112 {
2113 if (i > 0)
2114 ccline.cmdbuff[len] = firstc;
2115 }
2116 else
2117 {
2118 /* Escape new sep, unless it is already
2119 * escaped. */
2120 if (p[j] == firstc
2121 && (j == 0 || p[j - 1] != '\\'))
2122 {
2123 if (i > 0)
2124 ccline.cmdbuff[len] = '\\';
2125 ++len;
2126 }
2127 if (i > 0)
2128 ccline.cmdbuff[len] = p[j];
2129 }
2130 ++len;
2131 }
2132 if (i == 0)
2133 {
2134 alloc_cmdbuff(len);
2135 if (ccline.cmdbuff == NULL)
2136 goto returncmd;
2137 }
2138 }
2139 ccline.cmdbuff[len] = NUL;
2140 }
2141 else
2142 {
2143 alloc_cmdbuff((int)STRLEN(p));
2144 if (ccline.cmdbuff == NULL)
2145 goto returncmd;
2146 STRCPY(ccline.cmdbuff, p);
2147 }
2148
2149 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2150 redrawcmd();
2151 goto cmdline_changed;
2152 }
2153 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002154#endif
2155 goto cmdline_not_changed;
2156
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002157#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002158 case Ctrl_G: /* next match */
2159 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002160 if (may_adjust_incsearch_highlighting(
2161 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002162 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#endif
2165
2166 case Ctrl_V:
2167 case Ctrl_Q:
2168#ifdef FEAT_MOUSE
2169 ignore_drag_release = TRUE;
2170#endif
2171 putcmdline('^', TRUE);
2172 c = get_literal(); /* get next (two) character(s) */
2173 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002174 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#ifdef FEAT_MBYTE
2176 /* may need to remove ^ when composing char was typed */
2177 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2178 {
2179 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2180 msg_putchar(' ');
2181 cursorcmd();
2182 }
2183#endif
2184 break;
2185
2186#ifdef FEAT_DIGRAPHS
2187 case Ctrl_K:
2188#ifdef FEAT_MOUSE
2189 ignore_drag_release = TRUE;
2190#endif
2191 putcmdline('?', TRUE);
2192#ifdef USE_ON_FLY_SCROLL
2193 dont_scroll = TRUE; /* disallow scrolling here */
2194#endif
2195 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002196 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 if (c != NUL)
2198 break;
2199
2200 redrawcmd();
2201 goto cmdline_not_changed;
2202#endif /* FEAT_DIGRAPHS */
2203
2204#ifdef FEAT_RIGHTLEFT
2205 case Ctrl__: /* CTRL-_: switch language mode */
2206 if (!p_ari)
2207 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002208# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 if (p_altkeymap)
2210 {
2211 cmd_fkmap = !cmd_fkmap;
2212 if (cmd_fkmap) /* in Farsi always in Insert mode */
2213 ccline.overstrike = FALSE;
2214 }
2215 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002216# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 cmd_hkmap = !cmd_hkmap;
2218 goto cmdline_not_changed;
2219#endif
2220
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002221 case K_PS:
2222 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2223 goto cmdline_changed;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 default:
2226#ifdef UNIX
2227 if (c == intr_char)
2228 {
2229 gotesc = TRUE; /* will free ccline.cmdbuff after
2230 putting it in history */
2231 goto returncmd; /* back to Normal mode */
2232 }
2233#endif
2234 /*
2235 * Normal character with no special meaning. Just set mod_mask
2236 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2237 * the string <S-Space>. This should only happen after ^V.
2238 */
2239 if (!IS_SPECIAL(c))
2240 mod_mask = 0x0;
2241 break;
2242 }
2243 /*
2244 * End of switch on command line character.
2245 * We come here if we have a normal character.
2246 */
2247
Bram Moolenaarede3e632013-06-23 16:16:19 +02002248 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249#ifdef FEAT_MBYTE
2250 /* Add ABBR_OFF for characters above 0x100, this is
2251 * what check_abbr() expects. */
2252 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2253#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002254 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 goto cmdline_changed;
2256
2257 /*
2258 * put the character in the command line
2259 */
2260 if (IS_SPECIAL(c) || mod_mask != 0)
2261 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2262 else
2263 {
2264#ifdef FEAT_MBYTE
2265 if (has_mbyte)
2266 {
2267 j = (*mb_char2bytes)(c, IObuff);
2268 IObuff[j] = NUL; /* exclude composing chars */
2269 put_on_cmdline(IObuff, j, TRUE);
2270 }
2271 else
2272#endif
2273 {
2274 IObuff[0] = c;
2275 put_on_cmdline(IObuff, 1, TRUE);
2276 }
2277 }
2278 goto cmdline_changed;
2279
2280/*
2281 * This part implements incremental searches for "/" and "?"
2282 * Jump to cmdline_not_changed when a character has been read but the command
2283 * line did not change. Then we only search and redraw if something changed in
2284 * the past.
2285 * Jump to cmdline_changed when the command line did change.
2286 * (Sorry for the goto's, I know it is ugly).
2287 */
2288cmdline_not_changed:
2289#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002290 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 continue;
2292#endif
2293
2294cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002295 /* Trigger CmdlineChanged autocommands. */
2296 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002299 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#endif
2301
2302#ifdef FEAT_RIGHTLEFT
2303 if (cmdmsg_rl
2304# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002305 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306# endif
2307 )
2308 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002309 * right-left typing. Not efficient, but it works.
2310 * Do it only when there are no characters left to read
2311 * to avoid useless intermediate redraws. */
2312 if (vpeekc() == NUL)
2313 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314#endif
2315 }
2316
2317returncmd:
2318
2319#ifdef FEAT_RIGHTLEFT
2320 cmdmsg_rl = FALSE;
2321#endif
2322
2323#ifdef FEAT_FKMAP
2324 cmd_fkmap = 0;
2325#endif
2326
2327 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002328 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
2330#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002331 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332#endif
2333
2334 if (ccline.cmdbuff != NULL)
2335 {
2336 /*
2337 * Put line in history buffer (":" and "=" only when it was typed).
2338 */
2339#ifdef FEAT_CMDHIST
2340 if (ccline.cmdlen && firstc != NUL
2341 && (some_key_typed || histype == HIST_SEARCH))
2342 {
2343 add_to_history(histype, ccline.cmdbuff, TRUE,
2344 histype == HIST_SEARCH ? firstc : NUL);
2345 if (firstc == ':')
2346 {
2347 vim_free(new_last_cmdline);
2348 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2349 }
2350 }
2351#endif
2352
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002353 if (gotesc)
2354 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
2356
2357 /*
2358 * If the screen was shifted up, redraw the whole screen (later).
2359 * If the line is too long, clear it, so ruler and shown command do
2360 * not get printed in the middle of it.
2361 */
2362 msg_check();
2363 msg_scroll = save_msg_scroll;
2364 redir_off = FALSE;
2365
2366 /* When the command line was typed, no need for a wait-return prompt. */
2367 if (some_key_typed)
2368 need_wait_return = FALSE;
2369
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002370 /* Trigger CmdlineLeave autocommands. */
2371 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002372
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002374#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2376 im_save_status(b_im_ptr);
2377 im_set_active(FALSE);
2378#endif
2379#ifdef FEAT_MOUSE
2380 setmouse();
2381#endif
2382#ifdef CURSOR_SHAPE
2383 ui_cursor_shape(); /* may show different cursor shape */
2384#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002385 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002387 {
2388 char_u *p = ccline.cmdbuff;
2389
2390 /* Make ccline empty, getcmdline() may try to use it. */
2391 ccline.cmdbuff = NULL;
2392 return p;
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394}
2395
2396#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2397/*
2398 * Get a command line with a prompt.
2399 * This is prepared to be called recursively from getcmdline() (e.g. by
2400 * f_input() when evaluating an expression from CTRL-R =).
2401 * Returns the command line in allocated memory, or NULL.
2402 */
2403 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002404getcmdline_prompt(
2405 int firstc,
2406 char_u *prompt, /* command line prompt */
2407 int attr, /* attributes for prompt */
2408 int xp_context, /* type of expansion */
2409 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410{
2411 char_u *s;
2412 struct cmdline_info save_ccline;
2413 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002414 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002416 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 ccline.cmdprompt = prompt;
2418 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002419# ifdef FEAT_EVAL
2420 ccline.xp_context = xp_context;
2421 ccline.xp_arg = xp_arg;
2422 ccline.input_fn = (firstc == '@');
2423# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002424 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002426 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002427 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002428 /* Restore msg_col, the prompt from input() may have changed it.
2429 * But only if called recursively and the commandline is therefore being
2430 * restored to an old one; if not, the input() prompt stays on the screen,
2431 * so we need its modified msg_col left intact. */
2432 if (ccline.cmdbuff != NULL)
2433 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 return s;
2436}
2437#endif
2438
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002439/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002440 * Return TRUE when the text must not be changed and we can't switch to
2441 * another window or buffer. Used when editing the command line, evaluating
2442 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002443 */
2444 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002445text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002446{
2447#ifdef FEAT_CMDWIN
2448 if (cmdwin_type != 0)
2449 return TRUE;
2450#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002451 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002452}
2453
2454/*
2455 * Give an error message for a command that isn't allowed while the cmdline
2456 * window is open or editing the cmdline in another way.
2457 */
2458 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002459text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002460{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002461 EMSG(_(get_text_locked_msg()));
2462}
2463
2464 char_u *
2465get_text_locked_msg(void)
2466{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002467#ifdef FEAT_CMDWIN
2468 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002469 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002470#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002471 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002472}
2473
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002474/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002475 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2476 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002477 */
2478 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002479curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002480{
2481 if (curbuf_lock > 0)
2482 {
2483 EMSG(_("E788: Not allowed to edit another buffer now"));
2484 return TRUE;
2485 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002486 return allbuf_locked();
2487}
2488
2489/*
2490 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2491 * message.
2492 */
2493 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002494allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002495{
2496 if (allbuf_lock > 0)
2497 {
2498 EMSG(_("E811: Not allowed to change buffer information now"));
2499 return TRUE;
2500 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002501 return FALSE;
2502}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002503
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002505cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506{
2507#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2508 if (cmdline_star > 0) /* showing '*', always 1 position */
2509 return 1;
2510#endif
2511 return ptr2cells(ccline.cmdbuff + idx);
2512}
2513
2514/*
2515 * Compute the offset of the cursor on the command line for the prompt and
2516 * indent.
2517 */
2518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002519set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002521 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 ccline.cmdspos = 1 + ccline.cmdindent;
2523 else
2524 ccline.cmdspos = 0 + ccline.cmdindent;
2525}
2526
2527/*
2528 * Compute the screen position for the cursor on the command line.
2529 */
2530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002531set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 int i, m, c;
2534
2535 set_cmdspos();
2536 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002537 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002539 if (m < 0) /* overflow, Columns or Rows at weird value */
2540 m = MAXCOL;
2541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 else
2543 m = MAXCOL;
2544 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2545 {
2546 c = cmdline_charsize(i);
2547#ifdef FEAT_MBYTE
2548 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2549 if (has_mbyte)
2550 correct_cmdspos(i, c);
2551#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002552 /* If the cmdline doesn't fit, show cursor on last visible char.
2553 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if ((ccline.cmdspos += c) >= m)
2555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 ccline.cmdspos -= c;
2557 break;
2558 }
2559#ifdef FEAT_MBYTE
2560 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002561 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562#endif
2563 }
2564}
2565
2566#ifdef FEAT_MBYTE
2567/*
2568 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2569 * character that doesn't fit, so that a ">" must be displayed.
2570 */
2571 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002572correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002574 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2576 && ccline.cmdspos % Columns + cells > Columns)
2577 ccline.cmdspos++;
2578}
2579#endif
2580
2581/*
2582 * Get an Ex command line for the ":" command.
2583 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002585getexline(
2586 int c, /* normally ':', NUL for ":append" */
2587 void *cookie UNUSED,
2588 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590 /* When executing a register, remove ':' that's in front of each line. */
2591 if (exec_from_reg && vpeekc() == ':')
2592 (void)vgetc();
2593 return getcmdline(c, 1L, indent);
2594}
2595
2596/*
2597 * Get an Ex command line for Ex mode.
2598 * In Ex mode we only use the OS supplied line editing features and no
2599 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002600 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002603getexmodeline(
2604 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002605 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002606 void *cookie UNUSED,
2607 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002609 garray_T line_ga;
2610 char_u *pend;
2611 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002612 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002613 int escaped = FALSE; /* CTRL-V typed */
2614 int vcol = 0;
2615 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002616 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002617 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618
2619 /* Switch cursor on now. This avoids that it happens after the "\n", which
2620 * confuses the system function that computes tabstops. */
2621 cursor_on();
2622
2623 /* always start in column 0; write a newline if necessary */
2624 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002625 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002627 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002629 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002630 if (p_prompt)
2631 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 while (indent-- > 0)
2633 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 }
2636
2637 ga_init2(&line_ga, 1, 30);
2638
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002639 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002640 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002641 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002642 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002643 while (indent >= 8)
2644 {
2645 ga_append(&line_ga, TAB);
2646 msg_puts((char_u *)" ");
2647 indent -= 8;
2648 }
2649 while (indent-- > 0)
2650 {
2651 ga_append(&line_ga, ' ');
2652 msg_putchar(' ');
2653 }
2654 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002655 ++no_mapping;
2656 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 /*
2659 * Get the line, one character at a time.
2660 */
2661 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002662 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002664 long sw;
2665 char_u *s;
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (ga_grow(&line_ga, 40) == FAIL)
2668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaarba748c82017-02-26 14:00:07 +01002670 /*
2671 * Get one character at a time.
2672 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002673 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002674
2675 /* Check for a ":normal" command and no more characters left. */
2676 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2677 c1 = '\n';
2678 else
2679 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002682 * Handle line editing.
2683 * Previously this was left to the system, putting the terminal in
2684 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002688 msg_putchar('\n');
2689 break;
2690 }
2691
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002692 if (c1 == K_PS)
2693 {
2694 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2695 goto redraw;
2696 }
2697
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 if (!escaped)
2699 {
2700 /* CR typed means "enter", which is NL */
2701 if (c1 == '\r')
2702 c1 = '\n';
2703
2704 if (c1 == BS || c1 == K_BS
2705 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 if (line_ga.ga_len > 0)
2708 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002709#ifdef FEAT_MBYTE
2710 if (has_mbyte)
2711 {
2712 p = (char_u *)line_ga.ga_data;
2713 p[line_ga.ga_len] = NUL;
2714 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2715 line_ga.ga_len -= len;
2716 }
2717 else
2718#endif
2719 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002720 goto redraw;
2721 }
2722 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 }
2724
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 msg_col = startcol;
2728 msg_clr_eos();
2729 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002730 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 }
2732
2733 if (c1 == Ctrl_T)
2734 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002735 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002736 p = (char_u *)line_ga.ga_data;
2737 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002738 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002739 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002740add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002741 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002743 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002744 p = (char_u *)line_ga.ga_data;
2745 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002746 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2747 *s = ' ';
2748 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750redraw:
2751 /* redraw the line */
2752 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002754 p = (char_u *)line_ga.ga_data;
2755 p[line_ga.ga_len] = NUL;
2756 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 msg_putchar(' ');
2763 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002764 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002768 len = MB_PTR2LEN(p);
2769 msg_outtrans_len(p, len);
2770 vcol += ptr2cells(p);
2771 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
2773 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002775 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 continue;
2777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 if (c1 == Ctrl_D)
2780 {
2781 /* Delete one shiftwidth. */
2782 p = (char_u *)line_ga.ga_data;
2783 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 if (prev_char == '^')
2786 ex_keep_indent = TRUE;
2787 indent = 0;
2788 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790 else
2791 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002793 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002794 if (indent > 0)
2795 {
2796 --indent;
2797 indent -= indent % get_sw_value(curbuf);
2798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002800 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002802 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2804 --line_ga.ga_len;
2805 }
2806 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002808
2809 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2810 {
2811 escaped = TRUE;
2812 continue;
2813 }
2814
2815 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2816 if (IS_SPECIAL(c1))
2817 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819
2820 if (IS_SPECIAL(c1))
2821 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002822#ifdef FEAT_MBYTE
2823 if (has_mbyte)
2824 len = (*mb_char2bytes)(c1,
2825 (char_u *)line_ga.ga_data + line_ga.ga_len);
2826 else
2827#endif
2828 {
2829 len = 1;
2830 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2831 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 if (c1 == '\n')
2833 msg_putchar('\n');
2834 else if (c1 == TAB)
2835 {
2836 /* Don't use chartabsize(), 'ts' can be different */
2837 do
2838 {
2839 msg_putchar(' ');
2840 } while (++vcol % 8);
2841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002845 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002848 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 escaped = FALSE;
2850
2851 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002852 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002854 /* We are done when a NL is entered, but not when it comes after an
2855 * odd number of backslashes, that results in a NUL. */
2856 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002858 int bcount = 0;
2859
2860 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2861 ++bcount;
2862
2863 if (bcount > 0)
2864 {
2865 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2866 * "\NL", etc. */
2867 line_ga.ga_len -= (bcount + 1) / 2;
2868 pend -= (bcount + 1) / 2;
2869 pend[-1] = '\n';
2870 }
2871
2872 if ((bcount & 1) == 0)
2873 {
2874 --line_ga.ga_len;
2875 --pend;
2876 *pend = NUL;
2877 break;
2878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880 }
2881
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002882 --no_mapping;
2883 --allow_keys;
2884
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 /* make following messages go to the next line */
2886 msg_didout = FALSE;
2887 msg_col = 0;
2888 if (msg_row < Rows - 1)
2889 ++msg_row;
2890 emsg_on_display = FALSE; /* don't want ui_delay() */
2891
2892 if (got_int)
2893 ga_clear(&line_ga);
2894
2895 return (char_u *)line_ga.ga_data;
2896}
2897
Bram Moolenaare344bea2005-09-01 20:46:49 +00002898# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2899 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900/*
2901 * Return TRUE if ccline.overstrike is on.
2902 */
2903 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002904cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 return ccline.overstrike;
2907}
2908
2909/*
2910 * Return TRUE if the cursor is at the end of the cmdline.
2911 */
2912 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002913cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915 return (ccline.cmdpos >= ccline.cmdlen);
2916}
2917#endif
2918
Bram Moolenaar9372a112005-12-06 19:59:18 +00002919#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920/*
2921 * Return the virtual column number at the current cursor position.
2922 * This is used by the IM code to obtain the start of the preedit string.
2923 */
2924 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002925cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
2927 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2928 return MAXCOL;
2929
2930# ifdef FEAT_MBYTE
2931 if (has_mbyte)
2932 {
2933 colnr_T col;
2934 int i = 0;
2935
2936 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002937 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938
2939 return col;
2940 }
2941 else
2942# endif
2943 return ccline.cmdpos;
2944}
2945#endif
2946
2947#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2948/*
2949 * If part of the command line is an IM preedit string, redraw it with
2950 * IM feedback attributes. The cursor position is restored after drawing.
2951 */
2952 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002953redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 if ((State & CMDLINE)
2956 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002957 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 && !p_imdisable
2959 && im_is_preediting())
2960 {
2961 int cmdpos = 0;
2962 int cmdspos;
2963 int old_row;
2964 int old_col;
2965 colnr_T col;
2966
2967 old_row = msg_row;
2968 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002969 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
2971# ifdef FEAT_MBYTE
2972 if (has_mbyte)
2973 {
2974 for (col = 0; col < preedit_start_col
2975 && cmdpos < ccline.cmdlen; ++col)
2976 {
2977 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002978 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 }
2981 else
2982# endif
2983 {
2984 cmdspos += preedit_start_col;
2985 cmdpos += preedit_start_col;
2986 }
2987
2988 msg_row = cmdline_row + (cmdspos / (int)Columns);
2989 msg_col = cmdspos % (int)Columns;
2990 if (msg_row >= Rows)
2991 msg_row = Rows - 1;
2992
2993 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2994 {
2995 int char_len;
2996 int char_attr;
2997
2998 char_attr = im_get_feedback_attr(col);
2999 if (char_attr < 0)
3000 break; /* end of preedit string */
3001
3002# ifdef FEAT_MBYTE
3003 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003004 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 else
3006# endif
3007 char_len = 1;
3008
3009 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3010 cmdpos += char_len;
3011 }
3012
3013 msg_row = old_row;
3014 msg_col = old_col;
3015 }
3016}
3017#endif /* FEAT_XIM && FEAT_GUI_GTK */
3018
3019/*
3020 * Allocate a new command line buffer.
3021 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3022 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3023 */
3024 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 /*
3028 * give some extra space to avoid having to allocate all the time
3029 */
3030 if (len < 80)
3031 len = 100;
3032 else
3033 len += 20;
3034
3035 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3036 ccline.cmdbufflen = len;
3037}
3038
3039/*
3040 * Re-allocate the command line to length len + something extra.
3041 * return FAIL for failure, OK otherwise
3042 */
3043 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003044realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 char_u *p;
3047
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003048 if (len < ccline.cmdbufflen)
3049 return OK; /* no need to resize */
3050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 p = ccline.cmdbuff;
3052 alloc_cmdbuff(len); /* will get some more */
3053 if (ccline.cmdbuff == NULL) /* out of memory */
3054 {
3055 ccline.cmdbuff = p; /* keep the old one */
3056 return FAIL;
3057 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003058 /* There isn't always a NUL after the command, but it may need to be
3059 * there, thus copy up to the NUL and add a NUL. */
3060 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3061 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003063
3064 if (ccline.xpc != NULL
3065 && ccline.xpc->xp_pattern != NULL
3066 && ccline.xpc->xp_context != EXPAND_NOTHING
3067 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3068 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003069 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003070
3071 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3072 * to point into the newly allocated memory. */
3073 if (i >= 0 && i <= ccline.cmdlen)
3074 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3075 }
3076
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 return OK;
3078}
3079
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003080#if defined(FEAT_ARABIC) || defined(PROTO)
3081static char_u *arshape_buf = NULL;
3082
3083# if defined(EXITFREE) || defined(PROTO)
3084 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003085free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003086{
3087 vim_free(arshape_buf);
3088}
3089# endif
3090#endif
3091
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092/*
3093 * Draw part of the cmdline at the current cursor position. But draw stars
3094 * when cmdline_star is TRUE.
3095 */
3096 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003097draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098{
3099#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3100 int i;
3101
3102 if (cmdline_star > 0)
3103 for (i = 0; i < len; ++i)
3104 {
3105 msg_putchar('*');
3106# ifdef FEAT_MBYTE
3107 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003108 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109# endif
3110 }
3111 else
3112#endif
3113#ifdef FEAT_ARABIC
3114 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 static int buflen = 0;
3117 char_u *p;
3118 int j;
3119 int newlen = 0;
3120 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003121 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 int prev_c = 0;
3123 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003124 int u8c;
3125 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 /*
3129 * Do arabic shaping into a temporary buffer. This is very
3130 * inefficient!
3131 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003132 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 {
3134 /* Re-allocate the buffer. We keep it around to avoid a lot of
3135 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003136 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003137 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003138 arshape_buf = alloc(buflen);
3139 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return; /* out of memory */
3141 }
3142
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003143 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3144 {
3145 /* Prepend a space to draw the leading composing char on. */
3146 arshape_buf[0] = ' ';
3147 newlen = 1;
3148 }
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 for (j = start; j < start + len; j += mb_l)
3151 {
3152 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003153 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003154 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (ARABIC_CHAR(u8c))
3156 {
3157 /* Do Arabic shaping. */
3158 if (cmdmsg_rl)
3159 {
3160 /* displaying from right to left */
3161 pc = prev_c;
3162 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003163 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 if (j + mb_l >= start + len)
3165 nc = NUL;
3166 else
3167 nc = utf_ptr2char(p + mb_l);
3168 }
3169 else
3170 {
3171 /* displaying from left to right */
3172 if (j + mb_l >= start + len)
3173 pc = NUL;
3174 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003175 {
3176 int pcc[MAX_MCO];
3177
3178 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 pc1 = pcc[0];
3181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 nc = prev_c;
3183 }
3184 prev_c = u8c;
3185
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003186 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003188 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003189 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003191 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3192 if (u8cc[1] != 0)
3193 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003194 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196 }
3197 else
3198 {
3199 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 newlen += mb_l;
3202 }
3203 }
3204
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
3207 else
3208#endif
3209 msg_outtrans_len(ccline.cmdbuff + start, len);
3210}
3211
3212/*
3213 * Put a character on the command line. Shifts the following text to the
3214 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3215 * "c" must be printable (fit in one display cell)!
3216 */
3217 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003218putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 if (cmd_silent)
3221 return;
3222 msg_no_more = TRUE;
3223 msg_putchar(c);
3224 if (shift)
3225 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3226 msg_no_more = FALSE;
3227 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003228 extra_char = c;
3229 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232/*
3233 * Undo a putcmdline(c, FALSE).
3234 */
3235 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003236unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237{
3238 if (cmd_silent)
3239 return;
3240 msg_no_more = TRUE;
3241 if (ccline.cmdlen == ccline.cmdpos)
3242 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003243#ifdef FEAT_MBYTE
3244 else if (has_mbyte)
3245 draw_cmdline(ccline.cmdpos,
3246 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 else
3249 draw_cmdline(ccline.cmdpos, 1);
3250 msg_no_more = FALSE;
3251 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003252 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253}
3254
3255/*
3256 * Put the given string, of the given length, onto the command line.
3257 * If len is -1, then STRLEN() is used to calculate the length.
3258 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3259 * part will be redrawn, otherwise it will not. If this function is called
3260 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3261 * called afterwards.
3262 */
3263 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003264put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 int retval;
3267 int i;
3268 int m;
3269 int c;
3270
3271 if (len < 0)
3272 len = (int)STRLEN(str);
3273
3274 /* Check if ccline.cmdbuff needs to be longer */
3275 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003276 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 else
3278 retval = OK;
3279 if (retval == OK)
3280 {
3281 if (!ccline.overstrike)
3282 {
3283 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3284 ccline.cmdbuff + ccline.cmdpos,
3285 (size_t)(ccline.cmdlen - ccline.cmdpos));
3286 ccline.cmdlen += len;
3287 }
3288 else
3289 {
3290#ifdef FEAT_MBYTE
3291 if (has_mbyte)
3292 {
3293 /* Count nr of characters in the new string. */
3294 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003295 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 ++m;
3297 /* Count nr of bytes in cmdline that are overwritten by these
3298 * characters. */
3299 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003300 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 --m;
3302 if (i < ccline.cmdlen)
3303 {
3304 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3305 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3306 ccline.cmdlen += ccline.cmdpos + len - i;
3307 }
3308 else
3309 ccline.cmdlen = ccline.cmdpos + len;
3310 }
3311 else
3312#endif
3313 if (ccline.cmdpos + len > ccline.cmdlen)
3314 ccline.cmdlen = ccline.cmdpos + len;
3315 }
3316 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3317 ccline.cmdbuff[ccline.cmdlen] = NUL;
3318
3319#ifdef FEAT_MBYTE
3320 if (enc_utf8)
3321 {
3322 /* When the inserted text starts with a composing character,
3323 * backup to the character before it. There could be two of them.
3324 */
3325 i = 0;
3326 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3327 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3328 {
3329 i = (*mb_head_off)(ccline.cmdbuff,
3330 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3331 ccline.cmdpos -= i;
3332 len += i;
3333 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3334 }
3335# ifdef FEAT_ARABIC
3336 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3337 {
3338 /* Check the previous character for Arabic combining pair. */
3339 i = (*mb_head_off)(ccline.cmdbuff,
3340 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3341 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3342 + ccline.cmdpos - i), c))
3343 {
3344 ccline.cmdpos -= i;
3345 len += i;
3346 }
3347 else
3348 i = 0;
3349 }
3350# endif
3351 if (i != 0)
3352 {
3353 /* Also backup the cursor position. */
3354 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3355 ccline.cmdspos -= i;
3356 msg_col -= i;
3357 if (msg_col < 0)
3358 {
3359 msg_col += Columns;
3360 --msg_row;
3361 }
3362 }
3363 }
3364#endif
3365
3366 if (redraw && !cmd_silent)
3367 {
3368 msg_no_more = TRUE;
3369 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003370 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3372 /* Avoid clearing the rest of the line too often. */
3373 if (cmdline_row != i || ccline.overstrike)
3374 msg_clr_eos();
3375 msg_no_more = FALSE;
3376 }
3377#ifdef FEAT_FKMAP
3378 /*
3379 * If we are in Farsi command mode, the character input must be in
3380 * Insert mode. So do not advance the cmdpos.
3381 */
3382 if (!cmd_fkmap)
3383#endif
3384 {
3385 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003386 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003388 if (m < 0) /* overflow, Columns or Rows at weird value */
3389 m = MAXCOL;
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 else
3392 m = MAXCOL;
3393 for (i = 0; i < len; ++i)
3394 {
3395 c = cmdline_charsize(ccline.cmdpos);
3396#ifdef FEAT_MBYTE
3397 /* count ">" for a double-wide char that doesn't fit. */
3398 if (has_mbyte)
3399 correct_cmdspos(ccline.cmdpos, c);
3400#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003401 /* Stop cursor at the end of the screen, but do increment the
3402 * insert position, so that entering a very long command
3403 * works, even though you can't see it. */
3404 if (ccline.cmdspos + c < m)
3405 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406#ifdef FEAT_MBYTE
3407 if (has_mbyte)
3408 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003409 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 if (c > len - i - 1)
3411 c = len - i - 1;
3412 ccline.cmdpos += c;
3413 i += c;
3414 }
3415#endif
3416 ++ccline.cmdpos;
3417 }
3418 }
3419 }
3420 if (redraw)
3421 msg_check();
3422 return retval;
3423}
3424
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003425static struct cmdline_info prev_ccline;
3426static int prev_ccline_used = FALSE;
3427
3428/*
3429 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3430 * and overwrite it. But get_cmdline_str() may need it, thus make it
3431 * available globally in prev_ccline.
3432 */
3433 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003434save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003435{
3436 if (!prev_ccline_used)
3437 {
3438 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3439 prev_ccline_used = TRUE;
3440 }
3441 *ccp = prev_ccline;
3442 prev_ccline = ccline;
3443 ccline.cmdbuff = NULL;
3444 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003445 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003446}
3447
3448/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003449 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003450 */
3451 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003452restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003453{
3454 ccline = prev_ccline;
3455 prev_ccline = *ccp;
3456}
3457
Bram Moolenaar5a305422006-04-28 22:38:25 +00003458#if defined(FEAT_EVAL) || defined(PROTO)
3459/*
3460 * Save the command line into allocated memory. Returns a pointer to be
3461 * passed to restore_cmdline_alloc() later.
3462 * Returns NULL when failed.
3463 */
3464 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003465save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003466{
3467 struct cmdline_info *p;
3468
3469 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3470 if (p != NULL)
3471 save_cmdline(p);
3472 return (char_u *)p;
3473}
3474
3475/*
3476 * Restore the command line from the return value of save_cmdline_alloc().
3477 */
3478 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003479restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003480{
3481 if (p != NULL)
3482 {
3483 restore_cmdline((struct cmdline_info *)p);
3484 vim_free(p);
3485 }
3486}
3487#endif
3488
Bram Moolenaar8299df92004-07-10 09:47:34 +00003489/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003490 * Paste a yank register into the command line.
3491 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003492 * insert_reg() can't be used here, because special characters from the
3493 * register contents will be interpreted as commands.
3494 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003495 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003496 */
3497 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003498cmdline_paste(
3499 int regname,
3500 int literally, /* Insert text literally instead of "as typed" */
3501 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003502{
3503 long i;
3504 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003505 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003506 int allocated;
3507 struct cmdline_info save_ccline;
3508
3509 /* check for valid regname; also accept special characters for CTRL-R in
3510 * the command line */
3511 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003512 && regname != Ctrl_A && regname != Ctrl_L
3513 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003514 return FAIL;
3515
3516 /* A register containing CTRL-R can cause an endless loop. Allow using
3517 * CTRL-C to break the loop. */
3518 line_breakcheck();
3519 if (got_int)
3520 return FAIL;
3521
3522#ifdef FEAT_CLIPBOARD
3523 regname = may_get_selection(regname);
3524#endif
3525
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003526 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003527 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003528 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003529 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003530 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003531 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003532 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003533
3534 if (i)
3535 {
3536 /* Got the value of a special register in "arg". */
3537 if (arg == NULL)
3538 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003539
3540 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3541 * part of the word. */
3542 p = arg;
3543 if (p_is && regname == Ctrl_W)
3544 {
3545 char_u *w;
3546 int len;
3547
3548 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003549 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003550 {
3551#ifdef FEAT_MBYTE
3552 if (has_mbyte)
3553 {
3554 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3555 if (!vim_iswordc(mb_ptr2char(w - len)))
3556 break;
3557 w -= len;
3558 }
3559 else
3560#endif
3561 {
3562 if (!vim_iswordc(w[-1]))
3563 break;
3564 --w;
3565 }
3566 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003567 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003568 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3569 p += len;
3570 }
3571
3572 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003573 if (allocated)
3574 vim_free(arg);
3575 return OK;
3576 }
3577
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003578 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003579}
3580
3581/*
3582 * Put a string on the command line.
3583 * When "literally" is TRUE, insert literally.
3584 * When "literally" is FALSE, insert as typed, but don't leave the command
3585 * line.
3586 */
3587 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003588cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003589{
3590 int c, cv;
3591
3592 if (literally)
3593 put_on_cmdline(s, -1, TRUE);
3594 else
3595 while (*s != NUL)
3596 {
3597 cv = *s;
3598 if (cv == Ctrl_V && s[1])
3599 ++s;
3600#ifdef FEAT_MBYTE
3601 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003602 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 else
3604#endif
3605 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003606 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3607 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003608#ifdef UNIX
3609 || c == intr_char
3610#endif
3611 || (c == Ctrl_BSL && *s == Ctrl_N))
3612 stuffcharReadbuff(Ctrl_V);
3613 stuffcharReadbuff(c);
3614 }
3615}
3616
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#ifdef FEAT_WILDMENU
3618/*
3619 * Delete characters on the command line, from "from" to the current
3620 * position.
3621 */
3622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003623cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624{
3625 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3626 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3627 ccline.cmdlen -= ccline.cmdpos - from;
3628 ccline.cmdpos = from;
3629}
3630#endif
3631
3632/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003633 * This function is called when the screen size changes and with incremental
3634 * search and in other situations where the command line may have been
3635 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 */
3637 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003638redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003640 redrawcmdline_ex(TRUE);
3641}
3642
3643 void
3644redrawcmdline_ex(int do_compute_cmdrow)
3645{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 if (cmd_silent)
3647 return;
3648 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003649 if (do_compute_cmdrow)
3650 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 redrawcmd();
3652 cursorcmd();
3653}
3654
3655 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003656redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657{
3658 int i;
3659
3660 if (cmd_silent)
3661 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003662 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 msg_putchar(ccline.cmdfirstc);
3664 if (ccline.cmdprompt != NULL)
3665 {
3666 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3667 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3668 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003669 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 --ccline.cmdindent;
3671 }
3672 else
3673 for (i = ccline.cmdindent; i > 0; --i)
3674 msg_putchar(' ');
3675}
3676
3677/*
3678 * Redraw what is currently on the command line.
3679 */
3680 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003681redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682{
3683 if (cmd_silent)
3684 return;
3685
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003686 /* when 'incsearch' is set there may be no command line while redrawing */
3687 if (ccline.cmdbuff == NULL)
3688 {
3689 windgoto(cmdline_row, 0);
3690 msg_clr_eos();
3691 return;
3692 }
3693
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 msg_start();
3695 redrawcmdprompt();
3696
3697 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3698 msg_no_more = TRUE;
3699 draw_cmdline(0, ccline.cmdlen);
3700 msg_clr_eos();
3701 msg_no_more = FALSE;
3702
3703 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003704 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003705 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 /*
3708 * An emsg() before may have set msg_scroll. This is used in normal mode,
3709 * in cmdline mode we can reset them now.
3710 */
3711 msg_scroll = FALSE; /* next message overwrites cmdline */
3712
3713 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3714 * in cmdline mode */
3715 skip_redraw = FALSE;
3716}
3717
3718 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003719compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003721 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 cmdline_row = Rows - 1;
3723 else
3724 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003725 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726}
3727
3728 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003729cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
3731 if (cmd_silent)
3732 return;
3733
3734#ifdef FEAT_RIGHTLEFT
3735 if (cmdmsg_rl)
3736 {
3737 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3738 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3739 if (msg_row <= 0)
3740 msg_row = Rows - 1;
3741 }
3742 else
3743#endif
3744 {
3745 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3746 msg_col = ccline.cmdspos % (int)Columns;
3747 if (msg_row >= Rows)
3748 msg_row = Rows - 1;
3749 }
3750
3751 windgoto(msg_row, msg_col);
3752#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003753 if (p_imst == IM_ON_THE_SPOT)
3754 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755#endif
3756#ifdef MCH_CURSOR_SHAPE
3757 mch_update_cursor();
3758#endif
3759}
3760
3761 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003762gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763{
3764 msg_start();
3765#ifdef FEAT_RIGHTLEFT
3766 if (cmdmsg_rl)
3767 msg_col = Columns - 1;
3768 else
3769#endif
3770 msg_col = 0; /* always start in column 0 */
3771 if (clr) /* clear the bottom line(s) */
3772 msg_clr_eos(); /* will reset clear_cmdline */
3773 windgoto(cmdline_row, 0);
3774}
3775
3776/*
3777 * Check the word in front of the cursor for an abbreviation.
3778 * Called when the non-id character "c" has been entered.
3779 * When an abbreviation is recognized it is removed from the text with
3780 * backspaces and the replacement string is inserted, followed by "c".
3781 */
3782 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003783ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003785 int spos = 0;
3786
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3788 return FALSE;
3789
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003790 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3791 * Actually accepts any mark. */
3792 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3793 spos++;
3794 if (ccline.cmdlen - spos > 5
3795 && ccline.cmdbuff[spos] == '\''
3796 && ccline.cmdbuff[spos + 2] == ','
3797 && ccline.cmdbuff[spos + 3] == '\'')
3798 spos += 5;
3799 else
3800 /* check abbreviation from the beginning of the commandline */
3801 spos = 0;
3802
3803 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804}
3805
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003806#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3807 static int
3808#ifdef __BORLANDC__
3809_RTLENTRYF
3810#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003811sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003812{
3813 char_u *p1 = *(char_u **)s1;
3814 char_u *p2 = *(char_u **)s2;
3815
3816 if (*p1 != '<' && *p2 == '<') return -1;
3817 if (*p1 == '<' && *p2 != '<') return 1;
3818 return STRCMP(p1, p2);
3819}
3820#endif
3821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822/*
3823 * Return FAIL if this is not an appropriate context in which to do
3824 * completion of anything, return OK if it is (even if there are no matches).
3825 * For the caller, this means that the character is just passed through like a
3826 * normal character (instead of being expanded). This allows :s/^I^D etc.
3827 */
3828 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003829nextwild(
3830 expand_T *xp,
3831 int type,
3832 int options, /* extra options for ExpandOne() */
3833 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834{
3835 int i, j;
3836 char_u *p1;
3837 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 int difflen;
3839 int v;
3840
3841 if (xp->xp_numfiles == -1)
3842 {
3843 set_expand_context(xp);
3844 cmd_showtail = expand_showtail(xp);
3845 }
3846
3847 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3848 {
3849 beep_flush();
3850 return OK; /* Something illegal on command line */
3851 }
3852 if (xp->xp_context == EXPAND_NOTHING)
3853 {
3854 /* Caller can use the character as a normal char instead */
3855 return FAIL;
3856 }
3857
3858 MSG_PUTS("..."); /* show that we are busy */
3859 out_flush();
3860
3861 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003862 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863
3864 if (type == WILD_NEXT || type == WILD_PREV)
3865 {
3866 /*
3867 * Get next/previous match for a previous expanded pattern.
3868 */
3869 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3870 }
3871 else
3872 {
3873 /*
3874 * Translate string into pattern and expand it.
3875 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003876 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3877 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 p2 = NULL;
3879 else
3880 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003881 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003882 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3883 if (escape)
3884 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003885
3886 if (p_wic)
3887 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003888 p2 = ExpandOne(xp, p1,
3889 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003890 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003892 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (p2 != NULL && type == WILD_LONGEST)
3894 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003895 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 if (ccline.cmdbuff[i + j] == '*'
3897 || ccline.cmdbuff[i + j] == '?')
3898 break;
3899 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003900 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902 }
3903 }
3904
3905 if (p2 != NULL && !got_int)
3906 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003907 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003908 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003910 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 xp->xp_pattern = ccline.cmdbuff + i;
3912 }
3913 else
3914 v = OK;
3915 if (v == OK)
3916 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003917 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3918 &ccline.cmdbuff[ccline.cmdpos],
3919 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3920 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 ccline.cmdlen += difflen;
3922 ccline.cmdpos += difflen;
3923 }
3924 }
3925 vim_free(p2);
3926
3927 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003928 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929
3930 /* When expanding a ":map" command and no matches are found, assume that
3931 * the key is supposed to be inserted literally */
3932 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3933 return FAIL;
3934
3935 if (xp->xp_numfiles <= 0 && p2 == NULL)
3936 beep_flush();
3937 else if (xp->xp_numfiles == 1)
3938 /* free expanded pattern */
3939 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3940
3941 return OK;
3942}
3943
3944/*
3945 * Do wildcard expansion on the string 'str'.
3946 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003947 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 * Return NULL for failure.
3949 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003950 * "orig" is the originally expanded string, copied to allocated memory. It
3951 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3952 * WILD_PREV "orig" should be NULL.
3953 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003954 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3955 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 *
3957 * mode = WILD_FREE: just free previously expanded matches
3958 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3959 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3960 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3961 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3962 * mode = WILD_ALL: return all matches concatenated
3963 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003964 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 *
3966 * options = WILD_LIST_NOTFOUND: list entries without a match
3967 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3968 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3969 * options = WILD_NO_BEEP: Don't beep for multiple matches
3970 * options = WILD_ADD_SLASH: add a slash after directory names
3971 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3972 * options = WILD_SILENT: don't print warning messages
3973 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003974 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 *
3976 * The variables xp->xp_context and xp->xp_backslash must have been set!
3977 */
3978 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003979ExpandOne(
3980 expand_T *xp,
3981 char_u *str,
3982 char_u *orig, /* allocated copy of original of expanded string */
3983 int options,
3984 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
3986 char_u *ss = NULL;
3987 static int findex;
3988 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003989 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 int i;
3991 long_u len;
3992 int non_suf_match; /* number without matching suffix */
3993
3994 /*
3995 * first handle the case of using an old match
3996 */
3997 if (mode == WILD_NEXT || mode == WILD_PREV)
3998 {
3999 if (xp->xp_numfiles > 0)
4000 {
4001 if (mode == WILD_PREV)
4002 {
4003 if (findex == -1)
4004 findex = xp->xp_numfiles;
4005 --findex;
4006 }
4007 else /* mode == WILD_NEXT */
4008 ++findex;
4009
4010 /*
4011 * When wrapping around, return the original string, set findex to
4012 * -1.
4013 */
4014 if (findex < 0)
4015 {
4016 if (orig_save == NULL)
4017 findex = xp->xp_numfiles - 1;
4018 else
4019 findex = -1;
4020 }
4021 if (findex >= xp->xp_numfiles)
4022 {
4023 if (orig_save == NULL)
4024 findex = 0;
4025 else
4026 findex = -1;
4027 }
4028#ifdef FEAT_WILDMENU
4029 if (p_wmnu)
4030 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4031 findex, cmd_showtail);
4032#endif
4033 if (findex == -1)
4034 return vim_strsave(orig_save);
4035 return vim_strsave(xp->xp_files[findex]);
4036 }
4037 else
4038 return NULL;
4039 }
4040
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004041 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4043 {
4044 FreeWild(xp->xp_numfiles, xp->xp_files);
4045 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004046 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
4048 findex = 0;
4049
4050 if (mode == WILD_FREE) /* only release file name */
4051 return NULL;
4052
4053 if (xp->xp_numfiles == -1)
4054 {
4055 vim_free(orig_save);
4056 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004057 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058
4059 /*
4060 * Do the expansion.
4061 */
4062 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4063 options) == FAIL)
4064 {
4065#ifdef FNAME_ILLEGAL
4066 /* Illegal file name has been silently skipped. But when there
4067 * are wildcards, the real problem is that there was no match,
4068 * causing the pattern to be added, which has illegal characters.
4069 */
4070 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4071 EMSG2(_(e_nomatch2), str);
4072#endif
4073 }
4074 else if (xp->xp_numfiles == 0)
4075 {
4076 if (!(options & WILD_SILENT))
4077 EMSG2(_(e_nomatch2), str);
4078 }
4079 else
4080 {
4081 /* Escape the matches for use on the command line. */
4082 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4083
4084 /*
4085 * Check for matching suffixes in file names.
4086 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004087 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4088 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
4090 if (xp->xp_numfiles)
4091 non_suf_match = xp->xp_numfiles;
4092 else
4093 non_suf_match = 1;
4094 if ((xp->xp_context == EXPAND_FILES
4095 || xp->xp_context == EXPAND_DIRECTORIES)
4096 && xp->xp_numfiles > 1)
4097 {
4098 /*
4099 * More than one match; check suffix.
4100 * The files will have been sorted on matching suffix in
4101 * expand_wildcards, only need to check the first two.
4102 */
4103 non_suf_match = 0;
4104 for (i = 0; i < 2; ++i)
4105 if (match_suffix(xp->xp_files[i]))
4106 ++non_suf_match;
4107 }
4108 if (non_suf_match != 1)
4109 {
4110 /* Can we ever get here unless it's while expanding
4111 * interactively? If not, we can get rid of this all
4112 * together. Don't really want to wait for this message
4113 * (and possibly have to hit return to continue!).
4114 */
4115 if (!(options & WILD_SILENT))
4116 EMSG(_(e_toomany));
4117 else if (!(options & WILD_NO_BEEP))
4118 beep_flush();
4119 }
4120 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4121 ss = vim_strsave(xp->xp_files[0]);
4122 }
4123 }
4124 }
4125
4126 /* Find longest common part */
4127 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4128 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004129 int mb_len = 1;
4130 int c0, ci;
4131
4132 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004134#ifdef FEAT_MBYTE
4135 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004137 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4138 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4139 }
4140 else
4141#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004142 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004143 for (i = 1; i < xp->xp_numfiles; ++i)
4144 {
4145#ifdef FEAT_MBYTE
4146 if (has_mbyte)
4147 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4148 else
4149#endif
4150 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004151 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004153 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004154 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004156 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 break;
4158 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004159 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 break;
4161 }
4162 if (i < xp->xp_numfiles)
4163 {
4164 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004165 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 break;
4167 }
4168 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 ss = alloc((unsigned)len + 1);
4171 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004172 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 findex = -1; /* next p_wc gets first one */
4174 }
4175
4176 /* Concatenate all matching names */
4177 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4178 {
4179 len = 0;
4180 for (i = 0; i < xp->xp_numfiles; ++i)
4181 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4182 ss = lalloc(len, TRUE);
4183 if (ss != NULL)
4184 {
4185 *ss = NUL;
4186 for (i = 0; i < xp->xp_numfiles; ++i)
4187 {
4188 STRCAT(ss, xp->xp_files[i]);
4189 if (i != xp->xp_numfiles - 1)
4190 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4191 }
4192 }
4193 }
4194
4195 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4196 ExpandCleanup(xp);
4197
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004198 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004199 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004200 vim_free(orig);
4201
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 return ss;
4203}
4204
4205/*
4206 * Prepare an expand structure for use.
4207 */
4208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004209ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004211 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004212 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004214#ifndef BACKSLASH_IN_FILENAME
4215 xp->xp_shell = FALSE;
4216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 xp->xp_numfiles = -1;
4218 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004219#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4220 xp->xp_arg = NULL;
4221#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004222 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223}
4224
4225/*
4226 * Cleanup an expand structure after use.
4227 */
4228 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004229ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 if (xp->xp_numfiles >= 0)
4232 {
4233 FreeWild(xp->xp_numfiles, xp->xp_files);
4234 xp->xp_numfiles = -1;
4235 }
4236}
4237
4238 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004239ExpandEscape(
4240 expand_T *xp,
4241 char_u *str,
4242 int numfiles,
4243 char_u **files,
4244 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
4246 int i;
4247 char_u *p;
4248
4249 /*
4250 * May change home directory back to "~"
4251 */
4252 if (options & WILD_HOME_REPLACE)
4253 tilde_replace(str, numfiles, files);
4254
4255 if (options & WILD_ESCAPE)
4256 {
4257 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004258 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004259 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 || xp->xp_context == EXPAND_BUFFERS
4261 || xp->xp_context == EXPAND_DIRECTORIES)
4262 {
4263 /*
4264 * Insert a backslash into a file name before a space, \, %, #
4265 * and wildmatch characters, except '~'.
4266 */
4267 for (i = 0; i < numfiles; ++i)
4268 {
4269 /* for ":set path=" we need to escape spaces twice */
4270 if (xp->xp_backslash == XP_BS_THREE)
4271 {
4272 p = vim_strsave_escaped(files[i], (char_u *)" ");
4273 if (p != NULL)
4274 {
4275 vim_free(files[i]);
4276 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004277#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 p = vim_strsave_escaped(files[i], (char_u *)" ");
4279 if (p != NULL)
4280 {
4281 vim_free(files[i]);
4282 files[i] = p;
4283 }
4284#endif
4285 }
4286 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004287#ifdef BACKSLASH_IN_FILENAME
4288 p = vim_strsave_fnameescape(files[i], FALSE);
4289#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004290 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 if (p != NULL)
4293 {
4294 vim_free(files[i]);
4295 files[i] = p;
4296 }
4297
4298 /* If 'str' starts with "\~", replace "~" at start of
4299 * files[i] with "\~". */
4300 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004301 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004304
4305 /* If the first file starts with a '+' escape it. Otherwise it
4306 * could be seen as "+cmd". */
4307 if (*files[0] == '+')
4308 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 }
4310 else if (xp->xp_context == EXPAND_TAGS)
4311 {
4312 /*
4313 * Insert a backslash before characters in a tag name that
4314 * would terminate the ":tag" command.
4315 */
4316 for (i = 0; i < numfiles; ++i)
4317 {
4318 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4319 if (p != NULL)
4320 {
4321 vim_free(files[i]);
4322 files[i] = p;
4323 }
4324 }
4325 }
4326 }
4327}
4328
4329/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004330 * Escape special characters in "fname" for when used as a file name argument
4331 * after a Vim command, or, when "shell" is non-zero, a shell command.
4332 * Returns the result in allocated memory.
4333 */
4334 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004335vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004336{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004337 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004338#ifdef BACKSLASH_IN_FILENAME
4339 char_u buf[20];
4340 int j = 0;
4341
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004342 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004343 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004344 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004345 buf[j++] = *p;
4346 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004347 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004348#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004349 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4350 if (shell && csh_like_shell() && p != NULL)
4351 {
4352 char_u *s;
4353
4354 /* For csh and similar shells need to put two backslashes before '!'.
4355 * One is taken by Vim, one by the shell. */
4356 s = vim_strsave_escaped(p, (char_u *)"!");
4357 vim_free(p);
4358 p = s;
4359 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004360#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004361
4362 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4363 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004364 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004365 escape_fname(&p);
4366
4367 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004368}
4369
4370/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004371 * Put a backslash before the file name in "pp", which is in allocated memory.
4372 */
4373 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004374escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004375{
4376 char_u *p;
4377
4378 p = alloc((unsigned)(STRLEN(*pp) + 2));
4379 if (p != NULL)
4380 {
4381 p[0] = '\\';
4382 STRCPY(p + 1, *pp);
4383 vim_free(*pp);
4384 *pp = p;
4385 }
4386}
4387
4388/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 * For each file name in files[num_files]:
4390 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4391 */
4392 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004393tilde_replace(
4394 char_u *orig_pat,
4395 int num_files,
4396 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397{
4398 int i;
4399 char_u *p;
4400
4401 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4402 {
4403 for (i = 0; i < num_files; ++i)
4404 {
4405 p = home_replace_save(NULL, files[i]);
4406 if (p != NULL)
4407 {
4408 vim_free(files[i]);
4409 files[i] = p;
4410 }
4411 }
4412 }
4413}
4414
4415/*
4416 * Show all matches for completion on the command line.
4417 * Returns EXPAND_NOTHING when the character that triggered expansion should
4418 * be inserted like a normal character.
4419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004421showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4424 int num_files;
4425 char_u **files_found;
4426 int i, j, k;
4427 int maxlen;
4428 int lines;
4429 int columns;
4430 char_u *p;
4431 int lastlen;
4432 int attr;
4433 int showtail;
4434
4435 if (xp->xp_numfiles == -1)
4436 {
4437 set_expand_context(xp);
4438 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4439 &num_files, &files_found);
4440 showtail = expand_showtail(xp);
4441 if (i != EXPAND_OK)
4442 return i;
4443
4444 }
4445 else
4446 {
4447 num_files = xp->xp_numfiles;
4448 files_found = xp->xp_files;
4449 showtail = cmd_showtail;
4450 }
4451
4452#ifdef FEAT_WILDMENU
4453 if (!wildmenu)
4454 {
4455#endif
4456 msg_didany = FALSE; /* lines_left will be set */
4457 msg_start(); /* prepare for paging */
4458 msg_putchar('\n');
4459 out_flush();
4460 cmdline_row = msg_row;
4461 msg_didany = FALSE; /* lines_left will be set again */
4462 msg_start(); /* prepare for paging */
4463#ifdef FEAT_WILDMENU
4464 }
4465#endif
4466
4467 if (got_int)
4468 got_int = FALSE; /* only int. the completion, not the cmd line */
4469#ifdef FEAT_WILDMENU
4470 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004471 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472#endif
4473 else
4474 {
4475 /* find the length of the longest file name */
4476 maxlen = 0;
4477 for (i = 0; i < num_files; ++i)
4478 {
4479 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004480 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 || xp->xp_context == EXPAND_BUFFERS))
4482 {
4483 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4484 j = vim_strsize(NameBuff);
4485 }
4486 else
4487 j = vim_strsize(L_SHOWFILE(i));
4488 if (j > maxlen)
4489 maxlen = j;
4490 }
4491
4492 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4493 lines = num_files;
4494 else
4495 {
4496 /* compute the number of columns and lines for the listing */
4497 maxlen += 2; /* two spaces between file names */
4498 columns = ((int)Columns + 2) / maxlen;
4499 if (columns < 1)
4500 columns = 1;
4501 lines = (num_files + columns - 1) / columns;
4502 }
4503
Bram Moolenaar8820b482017-03-16 17:23:31 +01004504 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505
4506 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4507 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004508 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 msg_clr_eos();
4510 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004511 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513
4514 /* list the files line by line */
4515 for (i = 0; i < lines; ++i)
4516 {
4517 lastlen = 999;
4518 for (k = i; k < num_files; k += lines)
4519 {
4520 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4521 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004522 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 p = files_found[k] + STRLEN(files_found[k]) + 1;
4524 msg_advance(maxlen + 1);
4525 msg_puts(p);
4526 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004527 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 break;
4529 }
4530 for (j = maxlen - lastlen; --j >= 0; )
4531 msg_putchar(' ');
4532 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004533 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 || xp->xp_context == EXPAND_BUFFERS)
4535 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004536 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004537 if (xp->xp_numfiles != -1)
4538 {
4539 char_u *halved_slash;
4540 char_u *exp_path;
4541
4542 /* Expansion was done before and special characters
4543 * were escaped, need to halve backslashes. Also
4544 * $HOME has been replaced with ~/. */
4545 exp_path = expand_env_save_opt(files_found[k], TRUE);
4546 halved_slash = backslash_halve_save(
4547 exp_path != NULL ? exp_path : files_found[k]);
4548 j = mch_isdir(halved_slash != NULL ? halved_slash
4549 : files_found[k]);
4550 vim_free(exp_path);
4551 vim_free(halved_slash);
4552 }
4553 else
4554 /* Expansion was done here, file names are literal. */
4555 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (showtail)
4557 p = L_SHOWFILE(k);
4558 else
4559 {
4560 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4561 TRUE);
4562 p = NameBuff;
4563 }
4564 }
4565 else
4566 {
4567 j = FALSE;
4568 p = L_SHOWFILE(k);
4569 }
4570 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4571 }
4572 if (msg_col > 0) /* when not wrapped around */
4573 {
4574 msg_clr_eos();
4575 msg_putchar('\n');
4576 }
4577 out_flush(); /* show one line at a time */
4578 if (got_int)
4579 {
4580 got_int = FALSE;
4581 break;
4582 }
4583 }
4584
4585 /*
4586 * we redraw the command below the lines that we have just listed
4587 * This is a bit tricky, but it saves a lot of screen updating.
4588 */
4589 cmdline_row = msg_row; /* will put it back later */
4590 }
4591
4592 if (xp->xp_numfiles == -1)
4593 FreeWild(num_files, files_found);
4594
4595 return EXPAND_OK;
4596}
4597
4598/*
4599 * Private gettail for showmatches() (and win_redr_status_matches()):
4600 * Find tail of file name path, but ignore trailing "/".
4601 */
4602 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004603sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604{
4605 char_u *p;
4606 char_u *t = s;
4607 int had_sep = FALSE;
4608
4609 for (p = s; *p != NUL; )
4610 {
4611 if (vim_ispathsep(*p)
4612#ifdef BACKSLASH_IN_FILENAME
4613 && !rem_backslash(p)
4614#endif
4615 )
4616 had_sep = TRUE;
4617 else if (had_sep)
4618 {
4619 t = p;
4620 had_sep = FALSE;
4621 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004622 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
4624 return t;
4625}
4626
4627/*
4628 * Return TRUE if we only need to show the tail of completion matches.
4629 * When not completing file names or there is a wildcard in the path FALSE is
4630 * returned.
4631 */
4632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004633expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 char_u *s;
4636 char_u *end;
4637
4638 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004639 if (xp->xp_context != EXPAND_FILES
4640 && xp->xp_context != EXPAND_SHELLCMD
4641 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 return FALSE;
4643
4644 end = gettail(xp->xp_pattern);
4645 if (end == xp->xp_pattern) /* there is no path separator */
4646 return FALSE;
4647
4648 for (s = xp->xp_pattern; s < end; s++)
4649 {
4650 /* Skip escaped wildcards. Only when the backslash is not a path
4651 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4652 if (rem_backslash(s))
4653 ++s;
4654 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4655 return FALSE;
4656 }
4657 return TRUE;
4658}
4659
4660/*
4661 * Prepare a string for expansion.
4662 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004663 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 * When expanding other names: The string will be used with regcomp(). Copy
4665 * the name into allocated memory and prepend "^".
4666 */
4667 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004668addstar(
4669 char_u *fname,
4670 int len,
4671 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 char_u *retval;
4674 int i, j;
4675 int new_len;
4676 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004677 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004679 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004680 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004681 && context != EXPAND_SHELLCMD
4682 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 {
4684 /*
4685 * Matching will be done internally (on something other than files).
4686 * So we convert the file-matching-type wildcards into our kind for
4687 * use with vim_regcomp(). First work out how long it will be:
4688 */
4689
4690 /* For help tags the translation is done in find_help_tags().
4691 * For a tag pattern starting with "/" no translation is needed. */
4692 if (context == EXPAND_HELP
4693 || context == EXPAND_COLORS
4694 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004695 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004696 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004697 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004698 || ((context == EXPAND_TAGS_LISTFILES
4699 || context == EXPAND_TAGS)
4700 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 retval = vim_strnsave(fname, len);
4702 else
4703 {
4704 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4705 for (i = 0; i < len; i++)
4706 {
4707 if (fname[i] == '*' || fname[i] == '~')
4708 new_len++; /* '*' needs to be replaced by ".*"
4709 '~' needs to be replaced by "\~" */
4710
4711 /* Buffer names are like file names. "." should be literal */
4712 if (context == EXPAND_BUFFERS && fname[i] == '.')
4713 new_len++; /* "." becomes "\." */
4714
4715 /* Custom expansion takes care of special things, match
4716 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004717 if ((context == EXPAND_USER_DEFINED
4718 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 new_len++; /* '\' becomes "\\" */
4720 }
4721 retval = alloc(new_len);
4722 if (retval != NULL)
4723 {
4724 retval[0] = '^';
4725 j = 1;
4726 for (i = 0; i < len; i++, j++)
4727 {
4728 /* Skip backslash. But why? At least keep it for custom
4729 * expansion. */
4730 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004731 && context != EXPAND_USER_LIST
4732 && fname[i] == '\\'
4733 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 break;
4735
4736 switch (fname[i])
4737 {
4738 case '*': retval[j++] = '.';
4739 break;
4740 case '~': retval[j++] = '\\';
4741 break;
4742 case '?': retval[j] = '.';
4743 continue;
4744 case '.': if (context == EXPAND_BUFFERS)
4745 retval[j++] = '\\';
4746 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004747 case '\\': if (context == EXPAND_USER_DEFINED
4748 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 retval[j++] = '\\';
4750 break;
4751 }
4752 retval[j] = fname[i];
4753 }
4754 retval[j] = NUL;
4755 }
4756 }
4757 }
4758 else
4759 {
4760 retval = alloc(len + 4);
4761 if (retval != NULL)
4762 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004763 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
4765 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004766 * Don't add a star to *, ~, ~user, $var or `cmd`.
4767 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 * ~ would be at the start of the file name, but not the tail.
4769 * $ could be anywhere in the tail.
4770 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004771 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 */
4773 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004774 ends_in_star = (len > 0 && retval[len - 1] == '*');
4775#ifndef BACKSLASH_IN_FILENAME
4776 for (i = len - 2; i >= 0; --i)
4777 {
4778 if (retval[i] != '\\')
4779 break;
4780 ends_in_star = !ends_in_star;
4781 }
4782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004784 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 && vim_strchr(tail, '$') == NULL
4786 && vim_strchr(retval, '`') == NULL)
4787 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004788 else if (len > 0 && retval[len - 1] == '$')
4789 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 retval[len] = NUL;
4791 }
4792 }
4793 return retval;
4794}
4795
4796/*
4797 * Must parse the command line so far to work out what context we are in.
4798 * Completion can then be done based on that context.
4799 * This routine sets the variables:
4800 * xp->xp_pattern The start of the pattern to be expanded within
4801 * the command line (ends at the cursor).
4802 * xp->xp_context The type of thing to expand. Will be one of:
4803 *
4804 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4805 * the command line, like an unknown command. Caller
4806 * should beep.
4807 * EXPAND_NOTHING Unrecognised context for completion, use char like
4808 * a normal char, rather than for completion. eg
4809 * :s/^I/
4810 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4811 * it.
4812 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4813 * EXPAND_FILES After command with XFILE set, or after setting
4814 * with P_EXPAND set. eg :e ^I, :w>>^I
4815 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4816 * when we know only directories are of interest. eg
4817 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004818 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4820 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4821 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4822 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4823 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4824 * EXPAND_EVENTS Complete event names
4825 * EXPAND_SYNTAX Complete :syntax command arguments
4826 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4827 * EXPAND_AUGROUP Complete autocommand group names
4828 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4829 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4830 * eg :unmap a^I , :cunab x^I
4831 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4832 * eg :call sub^I
4833 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4834 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4835 * names in expressions, eg :while s^I
4836 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004837 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838 */
4839 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004840set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004842 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (ccline.cmdfirstc != ':'
4844#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004845 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004846 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847#endif
4848 )
4849 {
4850 xp->xp_context = EXPAND_NOTHING;
4851 return;
4852 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004853 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854}
4855
4856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004857set_cmd_context(
4858 expand_T *xp,
4859 char_u *str, /* start of command line */
4860 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004861 int col, /* position of cursor */
4862 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863{
4864 int old_char = NUL;
4865 char_u *nextcomm;
4866
4867 /*
4868 * Avoid a UMR warning from Purify, only save the character if it has been
4869 * written before.
4870 */
4871 if (col < len)
4872 old_char = str[col];
4873 str[col] = NUL;
4874 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004875
4876#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004877 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004878 {
4879# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004880 /* pass CMD_SIZE because there is no real command */
4881 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004882# endif
4883 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004884 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004885 {
4886 xp->xp_context = ccline.xp_context;
4887 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004888# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004889 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004890# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004891 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004892 else
4893#endif
4894 while (nextcomm != NULL)
4895 nextcomm = set_one_cmd_context(xp, nextcomm);
4896
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004897 /* Store the string here so that call_user_expand_func() can get to them
4898 * easily. */
4899 xp->xp_line = str;
4900 xp->xp_col = col;
4901
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 str[col] = old_char;
4903}
4904
4905/*
4906 * Expand the command line "str" from context "xp".
4907 * "xp" must have been set by set_cmd_context().
4908 * xp->xp_pattern points into "str", to where the text that is to be expanded
4909 * starts.
4910 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4911 * cursor.
4912 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4913 * key that triggered expansion literally.
4914 * Returns EXPAND_OK otherwise.
4915 */
4916 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004917expand_cmdline(
4918 expand_T *xp,
4919 char_u *str, /* start of command line */
4920 int col, /* position of cursor */
4921 int *matchcount, /* return: nr of matches */
4922 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923{
4924 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004925 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926
4927 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4928 {
4929 beep_flush();
4930 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4931 }
4932 if (xp->xp_context == EXPAND_NOTHING)
4933 {
4934 /* Caller can use the character as a normal char instead */
4935 return EXPAND_NOTHING;
4936 }
4937
4938 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004939 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4940 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (file_str == NULL)
4942 return EXPAND_UNSUCCESSFUL;
4943
Bram Moolenaar94950a92010-12-02 16:01:29 +01004944 if (p_wic)
4945 options += WILD_ICASE;
4946
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004948 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
4950 *matchcount = 0;
4951 *matches = NULL;
4952 }
4953 vim_free(file_str);
4954
4955 return EXPAND_OK;
4956}
4957
4958#ifdef FEAT_MULTI_LANG
4959/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004960 * Cleanup matches for help tags:
4961 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4962 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004964static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
4966 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004967cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
4969 int i, j;
4970 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004971 char_u buf[4];
4972 char_u *p = buf;
4973
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004974 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004975 {
4976 *p++ = '@';
4977 *p++ = p_hlg[0];
4978 *p++ = p_hlg[1];
4979 }
4980 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981
4982 for (i = 0; i < num_file; ++i)
4983 {
4984 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004985 if (len <= 0)
4986 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004987 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 /* Sorting on priority means the same item in another language may
4990 * be anywhere. Search all items for a match up to the "@en". */
4991 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004992 if (j != i && (int)STRLEN(file[j]) == len + 3
4993 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 break;
4995 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004996 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 file[i][len] = NUL;
4998 }
4999 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005000
5001 if (*buf != NUL)
5002 for (i = 0; i < num_file; ++i)
5003 {
5004 len = (int)STRLEN(file[i]) - 3;
5005 if (len <= 0)
5006 continue;
5007 if (STRCMP(file[i] + len, buf) == 0)
5008 {
5009 /* remove the default language */
5010 file[i][len] = NUL;
5011 }
5012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013}
5014#endif
5015
5016/*
5017 * Do the expansion based on xp->xp_context and "pat".
5018 */
5019 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005020ExpandFromContext(
5021 expand_T *xp,
5022 char_u *pat,
5023 int *num_file,
5024 char_u ***file,
5025 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
5027#ifdef FEAT_CMDL_COMPL
5028 regmatch_T regmatch;
5029#endif
5030 int ret;
5031 int flags;
5032
5033 flags = EW_DIR; /* include directories */
5034 if (options & WILD_LIST_NOTFOUND)
5035 flags |= EW_NOTFOUND;
5036 if (options & WILD_ADD_SLASH)
5037 flags |= EW_ADDSLASH;
5038 if (options & WILD_KEEP_ALL)
5039 flags |= EW_KEEPALL;
5040 if (options & WILD_SILENT)
5041 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005042 if (options & WILD_ALLLINKS)
5043 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005045 if (xp->xp_context == EXPAND_FILES
5046 || xp->xp_context == EXPAND_DIRECTORIES
5047 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
5049 /*
5050 * Expand file or directory names.
5051 */
5052 int free_pat = FALSE;
5053 int i;
5054
5055 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5056 * space */
5057 if (xp->xp_backslash != XP_BS_NONE)
5058 {
5059 free_pat = TRUE;
5060 pat = vim_strsave(pat);
5061 for (i = 0; pat[i]; ++i)
5062 if (pat[i] == '\\')
5063 {
5064 if (xp->xp_backslash == XP_BS_THREE
5065 && pat[i + 1] == '\\'
5066 && pat[i + 2] == '\\'
5067 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005068 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (xp->xp_backslash == XP_BS_ONE
5070 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005071 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073 }
5074
5075 if (xp->xp_context == EXPAND_FILES)
5076 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005077 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5078 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 else
5080 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005081 if (options & WILD_ICASE)
5082 flags |= EW_ICASE;
5083
Bram Moolenaard7834d32009-12-02 16:14:36 +00005084 /* Expand wildcards, supporting %:h and the like. */
5085 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (free_pat)
5087 vim_free(pat);
5088 return ret;
5089 }
5090
5091 *file = (char_u **)"";
5092 *num_file = 0;
5093 if (xp->xp_context == EXPAND_HELP)
5094 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005095 /* With an empty argument we would get all the help tags, which is
5096 * very slow. Get matches for "help" instead. */
5097 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5098 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
5100#ifdef FEAT_MULTI_LANG
5101 cleanup_help_tags(*num_file, *file);
5102#endif
5103 return OK;
5104 }
5105 return FAIL;
5106 }
5107
5108#ifndef FEAT_CMDL_COMPL
5109 return FAIL;
5110#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005111 if (xp->xp_context == EXPAND_SHELLCMD)
5112 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (xp->xp_context == EXPAND_OLD_SETTING)
5114 return ExpandOldSetting(num_file, file);
5115 if (xp->xp_context == EXPAND_BUFFERS)
5116 return ExpandBufnames(pat, num_file, file, options);
5117 if (xp->xp_context == EXPAND_TAGS
5118 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5119 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5120 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005121 {
5122 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005123 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5124 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005127 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005128 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005129 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005130 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005131 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005132 {
5133 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005134 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005135 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005136 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005137 {
5138 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005139 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005140 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005141# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5142 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005143 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005144# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005145 if (xp->xp_context == EXPAND_PACKADD)
5146 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5149 if (regmatch.regprog == NULL)
5150 return FAIL;
5151
5152 /* set ignore-case according to p_ic, p_scs and pat */
5153 regmatch.rm_ic = ignorecase(pat);
5154
5155 if (xp->xp_context == EXPAND_SETTINGS
5156 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5157 ret = ExpandSettings(xp, &regmatch, num_file, file);
5158 else if (xp->xp_context == EXPAND_MAPPINGS)
5159 ret = ExpandMappings(&regmatch, num_file, file);
5160# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5161 else if (xp->xp_context == EXPAND_USER_DEFINED)
5162 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5163# endif
5164 else
5165 {
5166 static struct expgen
5167 {
5168 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005169 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005171 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 } tab[] =
5173 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005174 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5175 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005176 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005177 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005178#ifdef FEAT_CMDHIST
5179 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5180#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005182 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005183 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005184 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5185 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5186 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#endif
5188#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005189 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5190 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5191 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5192 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#endif
5194#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005195 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5196 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197#endif
5198#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005199 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005201#ifdef FEAT_PROFILE
5202 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5203#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005204 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005205 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5206 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005207#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005208 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005209#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005210#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005211 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005212#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005213#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005214 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005216#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5217 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005218 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5219 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005221 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005222 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005223 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 };
5225 int i;
5226
5227 /*
5228 * Find a context in the table and call the ExpandGeneric() with the
5229 * right function to do the expansion.
5230 */
5231 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005232 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 if (xp->xp_context == tab[i].context)
5234 {
5235 if (tab[i].ic)
5236 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005237 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005238 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 break;
5240 }
5241 }
5242
Bram Moolenaar473de612013-06-08 18:19:48 +02005243 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 return ret;
5246#endif /* FEAT_CMDL_COMPL */
5247}
5248
5249#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5250/*
5251 * Expand a list of names.
5252 *
5253 * Generic function for command line completion. It calls a function to
5254 * obtain strings, one by one. The strings are matched against a regexp
5255 * program. Matching strings are copied into an array, which is returned.
5256 *
5257 * Returns OK when no problems encountered, FAIL for error (out of memory).
5258 */
5259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005260ExpandGeneric(
5261 expand_T *xp,
5262 regmatch_T *regmatch,
5263 int *num_file,
5264 char_u ***file,
5265 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005267 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268{
5269 int i;
5270 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005271 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 char_u *str;
5273
5274 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005275 * round == 0: count the number of matching names
5276 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005278 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 {
5280 for (i = 0; ; ++i)
5281 {
5282 str = (*func)(xp, i);
5283 if (str == NULL) /* end of list */
5284 break;
5285 if (*str == NUL) /* skip empty strings */
5286 continue;
5287
5288 if (vim_regexec(regmatch, str, (colnr_T)0))
5289 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005290 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005292 if (escaped)
5293 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5294 else
5295 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 (*file)[count] = str;
5297#ifdef FEAT_MENU
5298 if (func == get_menu_names && str != NULL)
5299 {
5300 /* test for separator added by get_menu_names() */
5301 str += STRLEN(str) - 1;
5302 if (*str == '\001')
5303 *str = '.';
5304 }
5305#endif
5306 }
5307 ++count;
5308 }
5309 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005310 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
5312 if (count == 0)
5313 return OK;
5314 *num_file = count;
5315 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5316 if (*file == NULL)
5317 {
5318 *file = (char_u **)"";
5319 return FAIL;
5320 }
5321 count = 0;
5322 }
5323 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005324
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005325 /* Sort the results. Keep menu's in the specified order. */
5326 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005327 {
5328 if (xp->xp_context == EXPAND_EXPRESSION
5329 || xp->xp_context == EXPAND_FUNCTIONS
5330 || xp->xp_context == EXPAND_USER_FUNC)
5331 /* <SNR> functions should be sorted to the end. */
5332 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5333 sort_func_compare);
5334 else
5335 sort_strings(*file, *num_file);
5336 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005337
Bram Moolenaar4f688582007-07-24 12:34:30 +00005338#ifdef FEAT_CMDL_COMPL
5339 /* Reset the variables used for special highlight names expansion, so that
5340 * they don't show up when getting normal highlight names by ID. */
5341 reset_expand_highlight();
5342#endif
5343
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 return OK;
5345}
5346
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005347/*
5348 * Complete a shell command.
5349 * Returns FAIL or OK;
5350 */
5351 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005352expand_shellcmd(
5353 char_u *filepat, /* pattern to match with command names */
5354 int *num_file, /* return: number of matches */
5355 char_u ***file, /* return: array with matches */
5356 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005357{
5358 char_u *pat;
5359 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005360 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005361 int mustfree = FALSE;
5362 garray_T ga;
5363 char_u *buf = alloc(MAXPATHL);
5364 size_t l;
5365 char_u *s, *e;
5366 int flags = flagsarg;
5367 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005368 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005369 hashtab_T found_ht;
5370 hashitem_T *hi;
5371 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005372
5373 if (buf == NULL)
5374 return FAIL;
5375
5376 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5377 * space */
5378 pat = vim_strsave(filepat);
5379 for (i = 0; pat[i]; ++i)
5380 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005381 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005382
Bram Moolenaarb5971142015-03-21 17:32:19 +01005383 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005384
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005385 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5386 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005387 path = (char_u *)".";
5388 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005389 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005390 /* For an absolute name we don't use $PATH. */
5391 if (!mch_isFullName(pat))
5392 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005393 if (path == NULL)
5394 path = (char_u *)"";
5395 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005396
5397 /*
5398 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005399 * collect them in "ga". When "." is not in $PATH also expand for the
5400 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005401 */
5402 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005403 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005404 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005405 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005406#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005407 e = vim_strchr(s, ';');
5408#else
5409 e = vim_strchr(s, ':');
5410#endif
5411 if (e == NULL)
5412 e = s + STRLEN(s);
5413
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005414 if (*s == NUL)
5415 {
5416 if (did_curdir)
5417 break;
5418 // Find directories in the current directory, path is empty.
5419 did_curdir = TRUE;
5420 flags |= EW_DIR;
5421 }
5422 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5423 {
5424 did_curdir = TRUE;
5425 flags |= EW_DIR;
5426 }
5427 else
5428 // Do not match directories inside a $PATH item.
5429 flags &= ~EW_DIR;
5430
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005431 l = e - s;
5432 if (l > MAXPATHL - 5)
5433 break;
5434 vim_strncpy(buf, s, l);
5435 add_pathsep(buf);
5436 l = STRLEN(buf);
5437 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5438
5439 /* Expand matches in one directory of $PATH. */
5440 ret = expand_wildcards(1, &buf, num_file, file, flags);
5441 if (ret == OK)
5442 {
5443 if (ga_grow(&ga, *num_file) == FAIL)
5444 FreeWild(*num_file, *file);
5445 else
5446 {
5447 for (i = 0; i < *num_file; ++i)
5448 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005449 char_u *name = (*file)[i];
5450
5451 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005452 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005453 // Check if this name was already found.
5454 hash = hash_hash(name + l);
5455 hi = hash_lookup(&found_ht, name + l, hash);
5456 if (HASHITEM_EMPTY(hi))
5457 {
5458 // Remove the path that was prepended.
5459 STRMOVE(name, name + l);
5460 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5461 hash_add_item(&found_ht, hi, name, hash);
5462 name = NULL;
5463 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005464 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005465 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005466 }
5467 vim_free(*file);
5468 }
5469 }
5470 if (*e != NUL)
5471 ++e;
5472 }
5473 *file = ga.ga_data;
5474 *num_file = ga.ga_len;
5475
5476 vim_free(buf);
5477 vim_free(pat);
5478 if (mustfree)
5479 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005480 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005481 return OK;
5482}
5483
5484
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5486/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005487 * Call "user_expand_func()" to invoke a user defined Vim script function and
5488 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005490 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005492 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005493 expand_T *xp,
5494 int *num_file,
5495 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005497 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005498 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005500 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005501 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005502 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503
Bram Moolenaarb7515462013-06-29 12:58:33 +02005504 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005505 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 *num_file = 0;
5507 *file = NULL;
5508
Bram Moolenaarb7515462013-06-29 12:58:33 +02005509 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005510 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005511 keep = ccline.cmdbuff[ccline.cmdlen];
5512 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005513 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005514
Bram Moolenaarffa96842018-06-12 22:05:14 +02005515 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5516
5517 args[0].v_type = VAR_STRING;
5518 args[0].vval.v_string = pat;
5519 args[1].v_type = VAR_STRING;
5520 args[1].vval.v_string = xp->xp_line;
5521 args[2].v_type = VAR_NUMBER;
5522 args[2].vval.v_number = xp->xp_col;
5523 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005525 /* Save the cmdline, we don't know what the function may do. */
5526 save_ccline = ccline;
5527 ccline.cmdbuff = NULL;
5528 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005530
Bram Moolenaarded27a12018-08-01 19:06:03 +02005531 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005532
5533 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005535 if (ccline.cmdbuff != NULL)
5536 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005537
Bram Moolenaarffa96842018-06-12 22:05:14 +02005538 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005539 return ret;
5540}
5541
5542/*
5543 * Expand names with a function defined by the user.
5544 */
5545 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005546ExpandUserDefined(
5547 expand_T *xp,
5548 regmatch_T *regmatch,
5549 int *num_file,
5550 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005551{
5552 char_u *retstr;
5553 char_u *s;
5554 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005555 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005556 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005557 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005558
5559 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5560 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 return FAIL;
5562
5563 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005564 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 e = vim_strchr(s, '\n');
5567 if (e == NULL)
5568 e = s + STRLEN(s);
5569 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005570 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005572 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5573 *e = keep;
5574
5575 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005577 if (ga_grow(&ga, 1) == FAIL)
5578 break;
5579 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5580 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 }
5582
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 if (*e != NUL)
5584 ++e;
5585 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005586 vim_free(retstr);
5587 *file = ga.ga_data;
5588 *num_file = ga.ga_len;
5589 return OK;
5590}
5591
5592/*
5593 * Expand names with a list returned by a function defined by the user.
5594 */
5595 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005596ExpandUserList(
5597 expand_T *xp,
5598 int *num_file,
5599 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005600{
5601 list_T *retlist;
5602 listitem_T *li;
5603 garray_T ga;
5604
5605 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5606 if (retlist == NULL)
5607 return FAIL;
5608
5609 ga_init2(&ga, (int)sizeof(char *), 3);
5610 /* Loop over the items in the list. */
5611 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5612 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005613 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5614 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005615
5616 if (ga_grow(&ga, 1) == FAIL)
5617 break;
5618
5619 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005620 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005621 ++ga.ga_len;
5622 }
5623 list_unref(retlist);
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 *file = ga.ga_data;
5626 *num_file = ga.ga_len;
5627 return OK;
5628}
5629#endif
5630
5631/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005632 * Expand color scheme, compiler or filetype names.
5633 * Search from 'runtimepath':
5634 * 'runtimepath'/{dirnames}/{pat}.vim
5635 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5636 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5637 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5638 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005639 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 */
5641 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005642ExpandRTDir(
5643 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005644 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005645 int *num_file,
5646 char_u ***file,
5647 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 char_u *s;
5650 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005651 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005653 int i;
5654 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655
5656 *num_file = 0;
5657 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005658 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005659 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005661 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005663 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5664 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005666 ga_clear_strings(&ga);
5667 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005669 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005670 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005671 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005673
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005674 if (flags & DIP_START) {
5675 for (i = 0; dirnames[i] != NULL; ++i)
5676 {
5677 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5678 if (s == NULL)
5679 {
5680 ga_clear_strings(&ga);
5681 return FAIL;
5682 }
5683 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5684 globpath(p_pp, s, &ga, 0);
5685 vim_free(s);
5686 }
5687 }
5688
5689 if (flags & DIP_OPT) {
5690 for (i = 0; dirnames[i] != NULL; ++i)
5691 {
5692 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5693 if (s == NULL)
5694 {
5695 ga_clear_strings(&ga);
5696 return FAIL;
5697 }
5698 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5699 globpath(p_pp, s, &ga, 0);
5700 vim_free(s);
5701 }
5702 }
5703
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005704 for (i = 0; i < ga.ga_len; ++i)
5705 {
5706 match = ((char_u **)ga.ga_data)[i];
5707 s = match;
5708 e = s + STRLEN(s);
5709 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5710 {
5711 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005712 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005713 if (s < match || vim_ispathsep(*s))
5714 break;
5715 ++s;
5716 *e = NUL;
5717 mch_memmove(match, s, e - s + 1);
5718 }
5719 }
5720
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005721 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005722 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005723
5724 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005725 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005726 remove_duplicates(&ga);
5727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 *file = ga.ga_data;
5729 *num_file = ga.ga_len;
5730 return OK;
5731}
5732
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005733/*
5734 * Expand loadplugin names:
5735 * 'packpath'/pack/ * /opt/{pat}
5736 */
5737 static int
5738ExpandPackAddDir(
5739 char_u *pat,
5740 int *num_file,
5741 char_u ***file)
5742{
5743 char_u *s;
5744 char_u *e;
5745 char_u *match;
5746 garray_T ga;
5747 int i;
5748 int pat_len;
5749
5750 *num_file = 0;
5751 *file = NULL;
5752 pat_len = (int)STRLEN(pat);
5753 ga_init2(&ga, (int)sizeof(char *), 10);
5754
5755 s = alloc((unsigned)(pat_len + 26));
5756 if (s == NULL)
5757 {
5758 ga_clear_strings(&ga);
5759 return FAIL;
5760 }
5761 sprintf((char *)s, "pack/*/opt/%s*", pat);
5762 globpath(p_pp, s, &ga, 0);
5763 vim_free(s);
5764
5765 for (i = 0; i < ga.ga_len; ++i)
5766 {
5767 match = ((char_u **)ga.ga_data)[i];
5768 s = gettail(match);
5769 e = s + STRLEN(s);
5770 mch_memmove(match, s, e - s + 1);
5771 }
5772
5773 if (ga.ga_len == 0)
5774 return FAIL;
5775
5776 /* Sort and remove duplicates which can happen when specifying multiple
5777 * directories in dirnames. */
5778 remove_duplicates(&ga);
5779
5780 *file = ga.ga_data;
5781 *num_file = ga.ga_len;
5782 return OK;
5783}
5784
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785#endif
5786
5787#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5788/*
5789 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005790 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005792 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005793globpath(
5794 char_u *path,
5795 char_u *file,
5796 garray_T *ga,
5797 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798{
5799 expand_T xpc;
5800 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 int num_p;
5803 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804
5805 buf = alloc(MAXPATHL);
5806 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005807 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005809 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005811
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 /* Loop over all entries in {path}. */
5813 while (*path != NUL)
5814 {
5815 /* Copy one item of the path to buf[] and concatenate the file name. */
5816 copy_option_part(&path, buf, MAXPATHL, ",");
5817 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5818 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005819# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005820 /* Using the platform's path separator (\) makes vim incorrectly
5821 * treat it as an escape character, use '/' instead. */
5822 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5823 STRCAT(buf, "/");
5824# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005826# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005828 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5829 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005831 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005833 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 for (i = 0; i < num_p; ++i)
5836 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005837 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005838 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005839 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 FreeWild(num_p, p);
5844 }
5845 }
5846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847
5848 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849}
5850
5851#endif
5852
5853#if defined(FEAT_CMDHIST) || defined(PROTO)
5854
5855/*********************************
5856 * Command line history stuff *
5857 *********************************/
5858
5859/*
5860 * Translate a history character to the associated type number.
5861 */
5862 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005863hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864{
5865 if (c == ':')
5866 return HIST_CMD;
5867 if (c == '=')
5868 return HIST_EXPR;
5869 if (c == '@')
5870 return HIST_INPUT;
5871 if (c == '>')
5872 return HIST_DEBUG;
5873 return HIST_SEARCH; /* must be '?' or '/' */
5874}
5875
5876/*
5877 * Table of history names.
5878 * These names are used in :history and various hist...() functions.
5879 * It is sufficient to give the significant prefix of a history name.
5880 */
5881
5882static char *(history_names[]) =
5883{
5884 "cmd",
5885 "search",
5886 "expr",
5887 "input",
5888 "debug",
5889 NULL
5890};
5891
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005892#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5893/*
5894 * Function given to ExpandGeneric() to obtain the possible first
5895 * arguments of the ":history command.
5896 */
5897 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005898get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005899{
5900 static char_u compl[2] = { NUL, NUL };
5901 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005902 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005903 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5904
5905 if (idx < short_names_count)
5906 {
5907 compl[0] = (char_u)short_names[idx];
5908 return compl;
5909 }
5910 if (idx < short_names_count + history_name_count)
5911 return (char_u *)history_names[idx - short_names_count];
5912 if (idx == short_names_count + history_name_count)
5913 return (char_u *)"all";
5914 return NULL;
5915}
5916#endif
5917
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918/*
5919 * init_history() - Initialize the command line history.
5920 * Also used to re-allocate the history when the size changes.
5921 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005922 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005923init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 int newlen; /* new length of history table */
5926 histentry_T *temp;
5927 int i;
5928 int j;
5929 int type;
5930
5931 /*
5932 * If size of history table changed, reallocate it
5933 */
5934 newlen = (int)p_hi;
5935 if (newlen != hislen) /* history length changed */
5936 {
5937 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5938 {
5939 if (newlen)
5940 {
5941 temp = (histentry_T *)lalloc(
5942 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5943 if (temp == NULL) /* out of memory! */
5944 {
5945 if (type == 0) /* first one: just keep the old length */
5946 {
5947 newlen = hislen;
5948 break;
5949 }
5950 /* Already changed one table, now we can only have zero
5951 * length for all tables. */
5952 newlen = 0;
5953 type = -1;
5954 continue;
5955 }
5956 }
5957 else
5958 temp = NULL;
5959 if (newlen == 0 || temp != NULL)
5960 {
5961 if (hisidx[type] < 0) /* there are no entries yet */
5962 {
5963 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005964 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 }
5966 else if (newlen > hislen) /* array becomes bigger */
5967 {
5968 for (i = 0; i <= hisidx[type]; ++i)
5969 temp[i] = history[type][i];
5970 j = i;
5971 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005972 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 for ( ; j < hislen; ++i, ++j)
5974 temp[i] = history[type][j];
5975 }
5976 else /* array becomes smaller or 0 */
5977 {
5978 j = hisidx[type];
5979 for (i = newlen - 1; ; --i)
5980 {
5981 if (i >= 0) /* copy newest entries */
5982 temp[i] = history[type][j];
5983 else /* remove older entries */
5984 vim_free(history[type][j].hisstr);
5985 if (--j < 0)
5986 j = hislen - 1;
5987 if (j == hisidx[type])
5988 break;
5989 }
5990 hisidx[type] = newlen - 1;
5991 }
5992 vim_free(history[type]);
5993 history[type] = temp;
5994 }
5995 }
5996 hislen = newlen;
5997 }
5998}
5999
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006000 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006001clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006002{
6003 hisptr->hisnum = 0;
6004 hisptr->viminfo = FALSE;
6005 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006006 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006007}
6008
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009/*
6010 * Check if command line 'str' is already in history.
6011 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6012 */
6013 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006014in_history(
6015 int type,
6016 char_u *str,
6017 int move_to_front, /* Move the entry to the front if it exists */
6018 int sep,
6019 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020{
6021 int i;
6022 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006023 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024
6025 if (hisidx[type] < 0)
6026 return FALSE;
6027 i = hisidx[type];
6028 do
6029 {
6030 if (history[type][i].hisstr == NULL)
6031 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006032
6033 /* For search history, check that the separator character matches as
6034 * well. */
6035 p = history[type][i].hisstr;
6036 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006037 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006038 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 {
6040 if (!move_to_front)
6041 return TRUE;
6042 last_i = i;
6043 break;
6044 }
6045 if (--i < 0)
6046 i = hislen - 1;
6047 } while (i != hisidx[type]);
6048
6049 if (last_i >= 0)
6050 {
6051 str = history[type][i].hisstr;
6052 while (i != hisidx[type])
6053 {
6054 if (++i >= hislen)
6055 i = 0;
6056 history[type][last_i] = history[type][i];
6057 last_i = i;
6058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006060 history[type][i].viminfo = FALSE;
6061 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006062 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 return TRUE;
6064 }
6065 return FALSE;
6066}
6067
6068/*
6069 * Convert history name (from table above) to its HIST_ equivalent.
6070 * When "name" is empty, return "cmd" history.
6071 * Returns -1 for unknown history name.
6072 */
6073 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006074get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 int i;
6077 int len = (int)STRLEN(name);
6078
6079 /* No argument: use current history. */
6080 if (len == 0)
6081 return hist_char2type(ccline.cmdfirstc);
6082
6083 for (i = 0; history_names[i] != NULL; ++i)
6084 if (STRNICMP(name, history_names[i], len) == 0)
6085 return i;
6086
6087 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6088 return hist_char2type(name[0]);
6089
6090 return -1;
6091}
6092
6093static int last_maptick = -1; /* last seen maptick */
6094
6095/*
6096 * Add the given string to the given history. If the string is already in the
6097 * history then it is moved to the front. "histype" may be one of he HIST_
6098 * values.
6099 */
6100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006101add_to_history(
6102 int histype,
6103 char_u *new_entry,
6104 int in_map, /* consider maptick when inside a mapping */
6105 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106{
6107 histentry_T *hisptr;
6108 int len;
6109
6110 if (hislen == 0) /* no history */
6111 return;
6112
Bram Moolenaara939e432013-11-09 05:30:26 +01006113 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6114 return;
6115
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 /*
6117 * Searches inside the same mapping overwrite each other, so that only
6118 * the last line is kept. Be careful not to remove a line that was moved
6119 * down, only lines that were added.
6120 */
6121 if (histype == HIST_SEARCH && in_map)
6122 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006123 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
6125 /* Current line is from the same mapping, remove it */
6126 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6127 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006128 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 --hisnum[histype];
6130 if (--hisidx[HIST_SEARCH] < 0)
6131 hisidx[HIST_SEARCH] = hislen - 1;
6132 }
6133 last_maptick = -1;
6134 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006135 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 if (++hisidx[histype] == hislen)
6138 hisidx[histype] = 0;
6139 hisptr = &history[histype][hisidx[histype]];
6140 vim_free(hisptr->hisstr);
6141
6142 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006143 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6145 if (hisptr->hisstr != NULL)
6146 hisptr->hisstr[len + 1] = sep;
6147
6148 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006149 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006150 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (histype == HIST_SEARCH && in_map)
6152 last_maptick = maptick;
6153 }
6154}
6155
6156#if defined(FEAT_EVAL) || defined(PROTO)
6157
6158/*
6159 * Get identifier of newest history entry.
6160 * "histype" may be one of the HIST_ values.
6161 */
6162 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006163get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164{
6165 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6166 || hisidx[histype] < 0)
6167 return -1;
6168
6169 return history[histype][hisidx[histype]].hisnum;
6170}
6171
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 * Calculate history index from a number:
6174 * num > 0: seen as identifying number of a history entry
6175 * num < 0: relative position in history wrt newest entry
6176 * "histype" may be one of the HIST_ values.
6177 */
6178 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006179calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180{
6181 int i;
6182 histentry_T *hist;
6183 int wrapped = FALSE;
6184
6185 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6186 || (i = hisidx[histype]) < 0 || num == 0)
6187 return -1;
6188
6189 hist = history[histype];
6190 if (num > 0)
6191 {
6192 while (hist[i].hisnum > num)
6193 if (--i < 0)
6194 {
6195 if (wrapped)
6196 break;
6197 i += hislen;
6198 wrapped = TRUE;
6199 }
6200 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6201 return i;
6202 }
6203 else if (-num <= hislen)
6204 {
6205 i += num + 1;
6206 if (i < 0)
6207 i += hislen;
6208 if (hist[i].hisstr != NULL)
6209 return i;
6210 }
6211 return -1;
6212}
6213
6214/*
6215 * Get a history entry by its index.
6216 * "histype" may be one of the HIST_ values.
6217 */
6218 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006219get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 idx = calc_hist_idx(histype, idx);
6222 if (idx >= 0)
6223 return history[histype][idx].hisstr;
6224 else
6225 return (char_u *)"";
6226}
6227
6228/*
6229 * Clear all entries of a history.
6230 * "histype" may be one of the HIST_ values.
6231 */
6232 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006233clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234{
6235 int i;
6236 histentry_T *hisptr;
6237
6238 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6239 {
6240 hisptr = history[histype];
6241 for (i = hislen; i--;)
6242 {
6243 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006244 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006245 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 }
6247 hisidx[histype] = -1; /* mark history as cleared */
6248 hisnum[histype] = 0; /* reset identifier counter */
6249 return OK;
6250 }
6251 return FAIL;
6252}
6253
6254/*
6255 * Remove all entries matching {str} from a history.
6256 * "histype" may be one of the HIST_ values.
6257 */
6258 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006259del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006260{
6261 regmatch_T regmatch;
6262 histentry_T *hisptr;
6263 int idx;
6264 int i;
6265 int last;
6266 int found = FALSE;
6267
6268 regmatch.regprog = NULL;
6269 regmatch.rm_ic = FALSE; /* always match case */
6270 if (hislen != 0
6271 && histype >= 0
6272 && histype < HIST_COUNT
6273 && *str != NUL
6274 && (idx = hisidx[histype]) >= 0
6275 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6276 != NULL)
6277 {
6278 i = last = idx;
6279 do
6280 {
6281 hisptr = &history[histype][i];
6282 if (hisptr->hisstr == NULL)
6283 break;
6284 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6285 {
6286 found = TRUE;
6287 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006288 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 }
6290 else
6291 {
6292 if (i != last)
6293 {
6294 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006295 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 }
6297 if (--last < 0)
6298 last += hislen;
6299 }
6300 if (--i < 0)
6301 i += hislen;
6302 } while (i != idx);
6303 if (history[histype][idx].hisstr == NULL)
6304 hisidx[histype] = -1;
6305 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006306 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307 return found;
6308}
6309
6310/*
6311 * Remove an indexed entry from a history.
6312 * "histype" may be one of the HIST_ values.
6313 */
6314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 int i, j;
6318
6319 i = calc_hist_idx(histype, idx);
6320 if (i < 0)
6321 return FALSE;
6322 idx = hisidx[histype];
6323 vim_free(history[histype][i].hisstr);
6324
6325 /* When deleting the last added search string in a mapping, reset
6326 * last_maptick, so that the last added search string isn't deleted again.
6327 */
6328 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6329 last_maptick = -1;
6330
6331 while (i != idx)
6332 {
6333 j = (i + 1) % hislen;
6334 history[histype][i] = history[histype][j];
6335 i = j;
6336 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006337 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 if (--i < 0)
6339 i += hislen;
6340 hisidx[histype] = i;
6341 return TRUE;
6342}
6343
6344#endif /* FEAT_EVAL */
6345
6346#if defined(FEAT_CRYPT) || defined(PROTO)
6347/*
6348 * Very specific function to remove the value in ":set key=val" from the
6349 * history.
6350 */
6351 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006352remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353{
6354 char_u *p;
6355 int i;
6356
6357 i = hisidx[HIST_CMD];
6358 if (i < 0)
6359 return;
6360 p = history[HIST_CMD][i].hisstr;
6361 if (p != NULL)
6362 for ( ; *p; ++p)
6363 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6364 {
6365 p = vim_strchr(p + 3, '=');
6366 if (p == NULL)
6367 break;
6368 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006369 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 if (p[i] == '\\' && p[i + 1])
6371 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006372 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006373 --p;
6374 }
6375}
6376#endif
6377
6378#endif /* FEAT_CMDHIST */
6379
Bram Moolenaar064154c2016-03-19 22:50:43 +01006380#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006381/*
6382 * Get pointer to the command line info to use. cmdline_paste() may clear
6383 * ccline and put the previous value in prev_ccline.
6384 */
6385 static struct cmdline_info *
6386get_ccline_ptr(void)
6387{
6388 if ((State & CMDLINE) == 0)
6389 return NULL;
6390 if (ccline.cmdbuff != NULL)
6391 return &ccline;
6392 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6393 return &prev_ccline;
6394 return NULL;
6395}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006396#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006397
Bram Moolenaar064154c2016-03-19 22:50:43 +01006398#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006399/*
6400 * Get the current command line in allocated memory.
6401 * Only works when the command line is being edited.
6402 * Returns NULL when something is wrong.
6403 */
6404 char_u *
6405get_cmdline_str(void)
6406{
6407 struct cmdline_info *p = get_ccline_ptr();
6408
6409 if (p == NULL)
6410 return NULL;
6411 return vim_strnsave(p->cmdbuff, p->cmdlen);
6412}
6413
6414/*
6415 * Get the current command line position, counted in bytes.
6416 * Zero is the first position.
6417 * Only works when the command line is being edited.
6418 * Returns -1 when something is wrong.
6419 */
6420 int
6421get_cmdline_pos(void)
6422{
6423 struct cmdline_info *p = get_ccline_ptr();
6424
6425 if (p == NULL)
6426 return -1;
6427 return p->cmdpos;
6428}
6429
6430/*
6431 * Set the command line byte position to "pos". Zero is the first position.
6432 * Only works when the command line is being edited.
6433 * Returns 1 when failed, 0 when OK.
6434 */
6435 int
6436set_cmdline_pos(
6437 int pos)
6438{
6439 struct cmdline_info *p = get_ccline_ptr();
6440
6441 if (p == NULL)
6442 return 1;
6443
6444 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6445 * changed the command line. */
6446 if (pos < 0)
6447 new_cmdpos = 0;
6448 else
6449 new_cmdpos = pos;
6450 return 0;
6451}
6452#endif
6453
6454#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6455/*
6456 * Get the current command-line type.
6457 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6458 * Only works when the command line is being edited.
6459 * Returns NUL when something is wrong.
6460 */
6461 int
6462get_cmdline_type(void)
6463{
6464 struct cmdline_info *p = get_ccline_ptr();
6465
6466 if (p == NULL)
6467 return NUL;
6468 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006469 return
6470# ifdef FEAT_EVAL
6471 (p->input_fn) ? '@' :
6472# endif
6473 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006474 return p->cmdfirstc;
6475}
6476#endif
6477
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6479/*
6480 * Get indices "num1,num2" that specify a range within a list (not a range of
6481 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6482 * Returns OK if parsed successfully, otherwise FAIL.
6483 */
6484 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006485get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 int len;
6488 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006489 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
6491 *str = skipwhite(*str);
6492 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6493 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006494 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 *str += len;
6496 *num1 = (int)num;
6497 first = TRUE;
6498 }
6499 *str = skipwhite(*str);
6500 if (**str == ',') /* parse "to" part of range */
6501 {
6502 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006503 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 if (len > 0)
6505 {
6506 *num2 = (int)num;
6507 *str = skipwhite(*str + len);
6508 }
6509 else if (!first) /* no number given at all */
6510 return FAIL;
6511 }
6512 else if (first) /* only one number given */
6513 *num2 = *num1;
6514 return OK;
6515}
6516#endif
6517
6518#if defined(FEAT_CMDHIST) || defined(PROTO)
6519/*
6520 * :history command - print a history
6521 */
6522 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006523ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 histentry_T *hist;
6526 int histype1 = HIST_CMD;
6527 int histype2 = HIST_CMD;
6528 int hisidx1 = 1;
6529 int hisidx2 = -1;
6530 int idx;
6531 int i, j, k;
6532 char_u *end;
6533 char_u *arg = eap->arg;
6534
6535 if (hislen == 0)
6536 {
6537 MSG(_("'history' option is zero"));
6538 return;
6539 }
6540
6541 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6542 {
6543 end = arg;
6544 while (ASCII_ISALPHA(*end)
6545 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6546 end++;
6547 i = *end;
6548 *end = NUL;
6549 histype1 = get_histtype(arg);
6550 if (histype1 == -1)
6551 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006552 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 {
6554 histype1 = 0;
6555 histype2 = HIST_COUNT-1;
6556 }
6557 else
6558 {
6559 *end = i;
6560 EMSG(_(e_trailing));
6561 return;
6562 }
6563 }
6564 else
6565 histype2 = histype1;
6566 *end = i;
6567 }
6568 else
6569 end = arg;
6570 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6571 {
6572 EMSG(_(e_trailing));
6573 return;
6574 }
6575
6576 for (; !got_int && histype1 <= histype2; ++histype1)
6577 {
6578 STRCPY(IObuff, "\n # ");
6579 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6580 MSG_PUTS_TITLE(IObuff);
6581 idx = hisidx[histype1];
6582 hist = history[histype1];
6583 j = hisidx1;
6584 k = hisidx2;
6585 if (j < 0)
6586 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6587 if (k < 0)
6588 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6589 if (idx >= 0 && j <= k)
6590 for (i = idx + 1; !got_int; ++i)
6591 {
6592 if (i == hislen)
6593 i = 0;
6594 if (hist[i].hisstr != NULL
6595 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6596 {
6597 msg_putchar('\n');
6598 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6599 hist[i].hisnum);
6600 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6601 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006602 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 else
6604 STRCAT(IObuff, hist[i].hisstr);
6605 msg_outtrans(IObuff);
6606 out_flush();
6607 }
6608 if (i == idx)
6609 break;
6610 }
6611 }
6612}
6613#endif
6614
6615#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006616/*
6617 * Buffers for history read from a viminfo file. Only valid while reading.
6618 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006619static histentry_T *viminfo_history[HIST_COUNT] =
6620 {NULL, NULL, NULL, NULL, NULL};
6621static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6622static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623static int viminfo_add_at_front = FALSE;
6624
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006625static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006626
6627/*
6628 * Translate a history type number to the associated character.
6629 */
6630 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006631hist_type2char(
6632 int type,
6633 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634{
6635 if (type == HIST_CMD)
6636 return ':';
6637 if (type == HIST_SEARCH)
6638 {
6639 if (use_question)
6640 return '?';
6641 else
6642 return '/';
6643 }
6644 if (type == HIST_EXPR)
6645 return '=';
6646 return '@';
6647}
6648
6649/*
6650 * Prepare for reading the history from the viminfo file.
6651 * This allocates history arrays to store the read history lines.
6652 */
6653 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006654prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655{
6656 int i;
6657 int num;
6658 int type;
6659 int len;
6660
6661 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006662 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (asklen > hislen)
6664 asklen = hislen;
6665
6666 for (type = 0; type < HIST_COUNT; ++type)
6667 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006668 /* Count the number of empty spaces in the history list. Entries read
6669 * from viminfo previously are also considered empty. If there are
6670 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006672 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 num++;
6674 len = asklen;
6675 if (num > len)
6676 len = num;
6677 if (len <= 0)
6678 viminfo_history[type] = NULL;
6679 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006680 viminfo_history[type] = (histentry_T *)lalloc(
6681 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 if (viminfo_history[type] == NULL)
6683 len = 0;
6684 viminfo_hislen[type] = len;
6685 viminfo_hisidx[type] = 0;
6686 }
6687}
6688
6689/*
6690 * Accept a line from the viminfo, store it in the history array when it's
6691 * new.
6692 */
6693 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006694read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
6696 int type;
6697 long_u len;
6698 char_u *val;
6699 char_u *p;
6700
6701 type = hist_char2type(virp->vir_line[0]);
6702 if (viminfo_hisidx[type] < viminfo_hislen[type])
6703 {
6704 val = viminfo_readstring(virp, 1, TRUE);
6705 if (val != NULL && *val != NUL)
6706 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006707 int sep = (*val == ' ' ? NUL : *val);
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006710 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 {
6712 /* Need to re-allocate to append the separator byte. */
6713 len = STRLEN(val);
6714 p = lalloc(len + 2, TRUE);
6715 if (p != NULL)
6716 {
6717 if (type == HIST_SEARCH)
6718 {
6719 /* Search entry: Move the separator from the first
6720 * column to after the NUL. */
6721 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006722 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723 }
6724 else
6725 {
6726 /* Not a search entry: No separator in the viminfo
6727 * file, add a NUL separator. */
6728 mch_memmove(p, val, (size_t)len + 1);
6729 p[len + 1] = NUL;
6730 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006731 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6732 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006733 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6734 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006735 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737 }
6738 }
6739 vim_free(val);
6740 }
6741 return viminfo_readline(virp);
6742}
6743
Bram Moolenaar07219f92013-04-14 23:19:36 +02006744/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006745 * Accept a new style history line from the viminfo, store it in the history
6746 * array when it's new.
6747 */
6748 void
6749handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006750 garray_T *values,
6751 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006752{
6753 int type;
6754 long_u len;
6755 char_u *val;
6756 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006757 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006758
6759 /* Check the format:
6760 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006761 if (values->ga_len < 4
6762 || vp[0].bv_type != BVAL_NR
6763 || vp[1].bv_type != BVAL_NR
6764 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6765 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006766 return;
6767
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006768 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006769 if (type >= HIST_COUNT)
6770 return;
6771 if (viminfo_hisidx[type] < viminfo_hislen[type])
6772 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006773 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006774 if (val != NULL && *val != NUL)
6775 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006776 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6777 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006778 int idx;
6779 int overwrite = FALSE;
6780
6781 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6782 {
6783 /* If lines were written by an older Vim we need to avoid
6784 * getting duplicates. See if the entry already exists. */
6785 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6786 {
6787 p = viminfo_history[type][idx].hisstr;
6788 if (STRCMP(val, p) == 0
6789 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6790 {
6791 overwrite = TRUE;
6792 break;
6793 }
6794 }
6795
6796 if (!overwrite)
6797 {
6798 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006799 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006800 p = lalloc(len + 2, TRUE);
6801 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006802 else
6803 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006804 if (p != NULL)
6805 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006806 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006807 if (!overwrite)
6808 {
6809 mch_memmove(p, val, (size_t)len + 1);
6810 /* Put the separator after the NUL. */
6811 p[len + 1] = sep;
6812 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006813 viminfo_history[type][idx].hisnum = 0;
6814 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006815 viminfo_hisidx[type]++;
6816 }
6817 }
6818 }
6819 }
6820 }
6821}
6822
6823/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006824 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006825 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006826 static void
6827concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828{
6829 int idx;
6830 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006831
6832 idx = hisidx[type] + viminfo_hisidx[type];
6833 if (idx >= hislen)
6834 idx -= hislen;
6835 else if (idx < 0)
6836 idx = hislen - 1;
6837 if (viminfo_add_at_front)
6838 hisidx[type] = idx;
6839 else
6840 {
6841 if (hisidx[type] == -1)
6842 hisidx[type] = hislen - 1;
6843 do
6844 {
6845 if (history[type][idx].hisstr != NULL
6846 || history[type][idx].viminfo)
6847 break;
6848 if (++idx == hislen)
6849 idx = 0;
6850 } while (idx != hisidx[type]);
6851 if (idx != hisidx[type] && --idx < 0)
6852 idx = hislen - 1;
6853 }
6854 for (i = 0; i < viminfo_hisidx[type]; i++)
6855 {
6856 vim_free(history[type][idx].hisstr);
6857 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6858 history[type][idx].viminfo = TRUE;
6859 history[type][idx].time_set = viminfo_history[type][i].time_set;
6860 if (--idx < 0)
6861 idx = hislen - 1;
6862 }
6863 idx += 1;
6864 idx %= hislen;
6865 for (i = 0; i < viminfo_hisidx[type]; i++)
6866 {
6867 history[type][idx++].hisnum = ++hisnum[type];
6868 idx %= hislen;
6869 }
6870}
6871
6872#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6873 static int
6874#ifdef __BORLANDC__
6875_RTLENTRYF
6876#endif
6877sort_hist(const void *s1, const void *s2)
6878{
6879 histentry_T *p1 = *(histentry_T **)s1;
6880 histentry_T *p2 = *(histentry_T **)s2;
6881
6882 if (p1->time_set < p2->time_set) return -1;
6883 if (p1->time_set > p2->time_set) return 1;
6884 return 0;
6885}
6886#endif
6887
6888/*
6889 * Merge history lines from viminfo and lines typed in this Vim based on the
6890 * timestamp;
6891 */
6892 static void
6893merge_history(int type)
6894{
6895 int max_len;
6896 histentry_T **tot_hist;
6897 histentry_T *new_hist;
6898 int i;
6899 int len;
6900
6901 /* Make one long list with all entries. */
6902 max_len = hislen + viminfo_hisidx[type];
6903 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6904 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6905 if (tot_hist == NULL || new_hist == NULL)
6906 {
6907 vim_free(tot_hist);
6908 vim_free(new_hist);
6909 return;
6910 }
6911 for (i = 0; i < viminfo_hisidx[type]; i++)
6912 tot_hist[i] = &viminfo_history[type][i];
6913 len = i;
6914 for (i = 0; i < hislen; i++)
6915 if (history[type][i].hisstr != NULL)
6916 tot_hist[len++] = &history[type][i];
6917
6918 /* Sort the list on timestamp. */
6919 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6920
6921 /* Keep the newest ones. */
6922 for (i = 0; i < hislen; i++)
6923 {
6924 if (i < len)
6925 {
6926 new_hist[i] = *tot_hist[i];
6927 tot_hist[i]->hisstr = NULL;
6928 if (new_hist[i].hisnum == 0)
6929 new_hist[i].hisnum = ++hisnum[type];
6930 }
6931 else
6932 clear_hist_entry(&new_hist[i]);
6933 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006934 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006935
6936 /* Free what is not kept. */
6937 for (i = 0; i < viminfo_hisidx[type]; i++)
6938 vim_free(viminfo_history[type][i].hisstr);
6939 for (i = 0; i < hislen; i++)
6940 vim_free(history[type][i].hisstr);
6941 vim_free(history[type]);
6942 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006943 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006944}
6945
6946/*
6947 * Finish reading history lines from viminfo. Not used when writing viminfo.
6948 */
6949 void
6950finish_viminfo_history(vir_T *virp)
6951{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006953 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955 for (type = 0; type < HIST_COUNT; ++type)
6956 {
6957 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006958 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006959
6960 if (merge)
6961 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006963 concat_history(type);
6964
Bram Moolenaard23a8232018-02-10 18:45:26 +01006965 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006966 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 }
6968}
6969
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006970/*
6971 * Write history to viminfo file in "fp".
6972 * When "merge" is TRUE merge history lines with a previously read viminfo
6973 * file, data is in viminfo_history[].
6974 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6975 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006977write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978{
6979 int i;
6980 int type;
6981 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006982 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006983
6984 init_history();
6985 if (hislen == 0)
6986 return;
6987 for (type = 0; type < HIST_COUNT; ++type)
6988 {
6989 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6990 if (num_saved == 0)
6991 continue;
6992 if (num_saved < 0) /* Use default */
6993 num_saved = hislen;
6994 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6995 type == HIST_CMD ? _("Command Line") :
6996 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006997 type == HIST_EXPR ? _("Expression") :
6998 type == HIST_INPUT ? _("Input Line") :
6999 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 if (num_saved > hislen)
7001 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007002
7003 /*
7004 * Merge typed and viminfo history:
7005 * round 1: history of typed commands.
7006 * round 2: history from recently read viminfo.
7007 */
7008 for (round = 1; round <= 2; ++round)
7009 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007010 if (round == 1)
7011 /* start at newest entry, somewhere in the list */
7012 i = hisidx[type];
7013 else if (viminfo_hisidx[type] > 0)
7014 /* start at newest entry, first in the list */
7015 i = 0;
7016 else
7017 /* empty list */
7018 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007019 if (i >= 0)
7020 while (num_saved > 0
7021 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007023 char_u *p;
7024 time_t timestamp;
7025 int c = NUL;
7026
7027 if (round == 1)
7028 {
7029 p = history[type][i].hisstr;
7030 timestamp = history[type][i].time_set;
7031 }
7032 else
7033 {
7034 p = viminfo_history[type] == NULL ? NULL
7035 : viminfo_history[type][i].hisstr;
7036 timestamp = viminfo_history[type] == NULL ? 0
7037 : viminfo_history[type][i].time_set;
7038 }
7039
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007040 if (p != NULL && (round == 2
7041 || !merge
7042 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007044 --num_saved;
7045 fputc(hist_type2char(type, TRUE), fp);
7046 /* For the search history: put the separator in the
7047 * second column; use a space if there isn't one. */
7048 if (type == HIST_SEARCH)
7049 {
7050 c = p[STRLEN(p) + 1];
7051 putc(c == NUL ? ' ' : c, fp);
7052 }
7053 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007054
7055 {
7056 char cbuf[NUMBUFLEN];
7057
7058 /* New style history with a bar line. Format:
7059 * |{bartype},{histtype},{timestamp},{separator},"text" */
7060 if (c == NUL)
7061 cbuf[0] = NUL;
7062 else
7063 sprintf(cbuf, "%d", c);
7064 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7065 type, (long)timestamp, cbuf);
7066 barline_writestring(fp, p, LSIZE - 20);
7067 putc('\n', fp);
7068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007070 if (round == 1)
7071 {
7072 /* Decrement index, loop around and stop when back at
7073 * the start. */
7074 if (--i < 0)
7075 i = hislen - 1;
7076 if (i == hisidx[type])
7077 break;
7078 }
7079 else
7080 {
7081 /* Increment index. Stop at the end in the while. */
7082 ++i;
7083 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007085 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007086 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007087 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007088 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007089 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007090 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091 }
7092}
7093#endif /* FEAT_VIMINFO */
7094
7095#if defined(FEAT_FKMAP) || defined(PROTO)
7096/*
7097 * Write a character at the current cursor+offset position.
7098 * It is directly written into the command buffer block.
7099 */
7100 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007101cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102{
7103 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7104 {
7105 EMSG(_("E198: cmd_pchar beyond the command length"));
7106 return;
7107 }
7108 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7109 ccline.cmdbuff[ccline.cmdlen] = NUL;
7110}
7111
7112 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007113cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114{
7115 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7116 {
7117 /* EMSG(_("cmd_gchar beyond the command length")); */
7118 return NUL;
7119 }
7120 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7121}
7122#endif
7123
7124#if defined(FEAT_CMDWIN) || defined(PROTO)
7125/*
7126 * Open a window on the current command line and history. Allow editing in
7127 * the window. Returns when the window is closed.
7128 * Returns:
7129 * CR if the command is to be executed
7130 * Ctrl_C if it is to be abandoned
7131 * K_IGNORE if editing continues
7132 */
7133 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007134open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135{
7136 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007137 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007139 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 win_T *wp;
7141 int i;
7142 linenr_T lnum;
7143 int histtype;
7144 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145 int save_restart_edit = restart_edit;
7146 int save_State = State;
7147 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007148#ifdef FEAT_RIGHTLEFT
7149 int save_cmdmsg_rl = cmdmsg_rl;
7150#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007151#ifdef FEAT_FOLDING
7152 int save_KeyTyped;
7153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 /* Can't do this recursively. Can't do it when typing a password. */
7156 if (cmdwin_type != 0
7157# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7158 || cmdline_star > 0
7159# endif
7160 )
7161 {
7162 beep_flush();
7163 return K_IGNORE;
7164 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007165 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166
7167 /* Save current window sizes. */
7168 win_size_save(&winsizes);
7169
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007171 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007172
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007173 /* don't use a new tab page */
7174 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007175 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007176
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177 /* Create a window for the command-line buffer. */
7178 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7179 {
7180 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007181 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 return K_IGNORE;
7183 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007184 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
7186 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007187 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007188 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007191#ifdef FEAT_FOLDING
7192 curwin->w_p_fen = FALSE;
7193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007195 curwin->w_p_rl = cmdmsg_rl;
7196 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007198 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007201 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007202 /* But don't allow switching to another buffer. */
7203 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204
Bram Moolenaar46152342005-09-07 21:18:43 +00007205 /* Showing the prompt may have set need_wait_return, reset it. */
7206 need_wait_return = FALSE;
7207
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007208 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7210 {
7211 if (p_wc == TAB)
7212 {
7213 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7214 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7215 }
7216 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7217 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007218 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007220 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7221 * sets 'textwidth' to 78). */
7222 curbuf->b_p_tw = 0;
7223
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 /* Fill the buffer with the history. */
7225 init_history();
7226 if (hislen > 0)
7227 {
7228 i = hisidx[histtype];
7229 if (i >= 0)
7230 {
7231 lnum = 0;
7232 do
7233 {
7234 if (++i == hislen)
7235 i = 0;
7236 if (history[histtype][i].hisstr != NULL)
7237 ml_append(lnum++, history[histtype][i].hisstr,
7238 (colnr_T)0, FALSE);
7239 }
7240 while (i != hisidx[histtype]);
7241 }
7242 }
7243
7244 /* Replace the empty last line with the current command-line and put the
7245 * cursor there. */
7246 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7247 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7248 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007249 changed_line_abv_curs();
7250 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007251 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007254 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256 /* No Ex mode here! */
7257 exmode_active = 0;
7258
7259 State = NORMAL;
7260# ifdef FEAT_MOUSE
7261 setmouse();
7262# endif
7263
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007265 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007266 if (restart_edit != 0) /* autocmd with ":startinsert" */
7267 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269 i = RedrawingDisabled;
7270 RedrawingDisabled = 0;
7271
7272 /*
7273 * Call the main loop until <CR> or CTRL-C is typed.
7274 */
7275 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007276 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
7278 RedrawingDisabled = i;
7279
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007280# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007281 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007282# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007285 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007286
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007287# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007288 /* Restore KeyTyped in case it is modified by autocommands */
7289 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290# endif
7291
Bram Moolenaarccc18222007-05-10 18:25:20 +00007292 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007293 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 cmdwin_type = 0;
7295
7296 exmode_active = save_exmode;
7297
Bram Moolenaarf9821062008-06-20 16:31:07 +00007298 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007300 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 {
7302 cmdwin_result = Ctrl_C;
7303 EMSG(_("E199: Active window or buffer deleted"));
7304 }
7305 else
7306 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007307# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 /* autocmds may abort script processing */
7309 if (aborting() && cmdwin_result != K_IGNORE)
7310 cmdwin_result = Ctrl_C;
7311# endif
7312 /* Set the new command line from the cmdline buffer. */
7313 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007314 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007316 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7317
7318 if (histtype == HIST_CMD)
7319 {
7320 /* Execute the command directly. */
7321 ccline.cmdbuff = vim_strsave((char_u *)p);
7322 cmdwin_result = CAR;
7323 }
7324 else
7325 {
7326 /* First need to cancel what we were doing. */
7327 ccline.cmdbuff = NULL;
7328 stuffcharReadbuff(':');
7329 stuffReadbuff((char_u *)p);
7330 stuffcharReadbuff(CAR);
7331 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 }
7333 else if (cmdwin_result == K_XF2) /* :qa typed */
7334 {
7335 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7336 cmdwin_result = CAR;
7337 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007338 else if (cmdwin_result == Ctrl_C)
7339 {
7340 /* :q or :close, don't execute any command
7341 * and don't modify the cmd window. */
7342 ccline.cmdbuff = NULL;
7343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 else
7345 ccline.cmdbuff = vim_strsave(ml_get_curline());
7346 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007347 {
7348 ccline.cmdbuff = vim_strsave((char_u *)"");
7349 ccline.cmdlen = 0;
7350 ccline.cmdbufflen = 1;
7351 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 else
7355 {
7356 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7357 ccline.cmdbufflen = ccline.cmdlen + 1;
7358 ccline.cmdpos = curwin->w_cursor.col;
7359 if (ccline.cmdpos > ccline.cmdlen)
7360 ccline.cmdpos = ccline.cmdlen;
7361 if (cmdwin_result == K_IGNORE)
7362 {
7363 set_cmdspos_cursor();
7364 redrawcmd();
7365 }
7366 }
7367
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007369 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007370# ifdef FEAT_CONCEAL
7371 /* Avoid command-line window first character being concealed. */
7372 curwin->w_p_cole = 0;
7373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007375 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 win_goto(old_curwin);
7377 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007378
7379 /* win_close() may have already wiped the buffer when 'bh' is
7380 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007381 if (bufref_valid(&bufref))
7382 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383
7384 /* Restore window sizes. */
7385 win_size_restore(&winsizes);
7386
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007387 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389
7390 ga_clear(&winsizes);
7391 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007392# ifdef FEAT_RIGHTLEFT
7393 cmdmsg_rl = save_cmdmsg_rl;
7394# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395
7396 State = save_State;
7397# ifdef FEAT_MOUSE
7398 setmouse();
7399# endif
7400
7401 return cmdwin_result;
7402}
7403#endif /* FEAT_CMDWIN */
7404
7405/*
7406 * Used for commands that either take a simple command string argument, or:
7407 * cmd << endmarker
7408 * {script}
7409 * endmarker
7410 * Returns a pointer to allocated memory with {script} or NULL.
7411 */
7412 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007413script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007414{
7415 char_u *theline;
7416 char *end_pattern = NULL;
7417 char dot[] = ".";
7418 garray_T ga;
7419
7420 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7421 return NULL;
7422
7423 ga_init2(&ga, 1, 0x400);
7424
7425 if (cmd[2] != NUL)
7426 end_pattern = (char *)skipwhite(cmd + 2);
7427 else
7428 end_pattern = dot;
7429
7430 for (;;)
7431 {
7432 theline = eap->getline(
7433#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007434 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435#endif
7436 NUL, eap->cookie, 0);
7437
7438 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007439 {
7440 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443
7444 ga_concat(&ga, theline);
7445 ga_append(&ga, '\n');
7446 vim_free(theline);
7447 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007448 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449
7450 return (char_u *)ga.ga_data;
7451}