blob: a1cea9baf2ea50be84e75faf7bd947d0632443d6 [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 {
323 search_first_line = ea.line1;
324 search_last_line = ea.line2;
325 }
326 else if (*cmd == 's')
327 {
328 // :s defaults to the current line
329 search_first_line = curwin->w_cursor.lnum;
330 search_last_line = curwin->w_cursor.lnum;
331 }
332
333 curwin->w_cursor = save_cursor;
334 return TRUE;
335 }
336 }
337 }
338 }
339 }
340
341 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200342}
343
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200344 static void
345finish_incsearch_highlighting(
346 int gotesc,
347 incsearch_state_T *is_state,
348 int call_update_screen)
349{
350 if (is_state->did_incsearch)
351 {
352 is_state->did_incsearch = FALSE;
353 if (gotesc)
354 curwin->w_cursor = is_state->save_cursor;
355 else
356 {
357 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
358 {
359 // put the '" mark at the original position
360 curwin->w_cursor = is_state->save_cursor;
361 setpcmark();
362 }
363 curwin->w_cursor = is_state->search_start;
364 }
365 restore_viewstate(&is_state->old_viewstate);
366 highlight_match = FALSE;
367 validate_cursor(); /* needed for TAB */
368 if (call_update_screen)
369 update_screen(SOME_VALID);
370 else
371 redraw_all_later(SOME_VALID);
372 }
373}
374
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200375/*
376 * Do 'incsearch' highlighting if desired.
377 */
378 static void
379may_do_incsearch_highlighting(
380 int firstc,
381 long count,
382 incsearch_state_T *is_state)
383{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200384 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200385 int i;
386 pos_T end_pos;
387 struct cmdline_info save_ccline;
388#ifdef FEAT_RELTIME
389 proftime_T tm;
390#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200391 int next_char;
392 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200393
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200394 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200395 {
396 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200397 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200398 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200399
400 // If there is a character waiting, search and redraw later.
401 if (char_avail())
402 {
403 is_state->incsearch_postponed = TRUE;
404 return;
405 }
406 is_state->incsearch_postponed = FALSE;
407
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200408 if (search_first_line == 0)
409 // start at the original cursor position
410 curwin->w_cursor = is_state->search_start;
411 else
412 {
413 // start at the first line in the range
414 curwin->w_cursor.lnum = search_first_line;
415 curwin->w_cursor.col = 0;
416 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200417 save_last_search_pattern();
418
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200419 // Use the previous pattern for ":s//".
420 next_char = ccline.cmdbuff[skiplen + patlen];
421 use_last_pat = patlen == 0 && skiplen > 0
422 && ccline.cmdbuff[skiplen - 1] == next_char;
423
424 // If there is no pattern, don't do anything.
425 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200426 {
427 i = 0;
428 set_no_hlsearch(TRUE); // turn off previous highlight
429 redraw_all_later(SOME_VALID);
430 }
431 else
432 {
433 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
434
435 cursor_off(); // so the user knows we're busy
436 out_flush();
437 ++emsg_off; // so it doesn't beep if bad expr
438#ifdef FEAT_RELTIME
439 // Set the time limit to half a second.
440 profile_setlimit(500L, &tm);
441#endif
442 if (!p_hls)
443 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200444 if (search_first_line != 0)
445 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200446 ccline.cmdbuff[skiplen + patlen] = NUL;
447 i = do_search(NULL, firstc == ':' ? '/' : firstc,
448 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200449#ifdef FEAT_RELTIME
450 &tm, NULL
451#else
452 NULL, NULL
453#endif
454 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200455 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200456 --emsg_off;
457
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200458 if (curwin->w_cursor.lnum < search_first_line
459 || curwin->w_cursor.lnum > search_last_line)
460 // match outside of address range
461 i = 0;
462
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200463 // if interrupted while searching, behave like it failed
464 if (got_int)
465 {
466 (void)vpeekc(); // remove <C-C> from input stream
467 got_int = FALSE; // don't abandon the command line
468 i = 0;
469 }
470 else if (char_avail())
471 // cancelled searching because a char was typed
472 is_state->incsearch_postponed = TRUE;
473 }
474 if (i != 0)
475 highlight_match = TRUE; // highlight position
476 else
477 highlight_match = FALSE; // remove highlight
478
479 // First restore the old curwin values, so the screen is positioned in the
480 // same way as the actual search command.
481 restore_viewstate(&is_state->old_viewstate);
482 changed_cline_bef_curs();
483 update_topline();
484
485 if (i != 0)
486 {
487 pos_T save_pos = curwin->w_cursor;
488
489 is_state->match_start = curwin->w_cursor;
490 set_search_match(&curwin->w_cursor);
491 validate_cursor();
492 end_pos = curwin->w_cursor;
493 is_state->match_end = end_pos;
494 curwin->w_cursor = save_pos;
495 }
496 else
497 end_pos = curwin->w_cursor; // shutup gcc 4
498
499 // Disable 'hlsearch' highlighting if the pattern matches everything.
500 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200501 if (!use_last_pat)
502 {
503 next_char = ccline.cmdbuff[skiplen + patlen];
504 ccline.cmdbuff[skiplen + patlen] = NUL;
505 if (empty_pattern(ccline.cmdbuff))
506 set_no_hlsearch(TRUE);
507 ccline.cmdbuff[skiplen + patlen] = next_char;
508 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200509
510 validate_cursor();
511 // May redraw the status line to show the cursor position.
512 if (p_ru && curwin->w_status_height > 0)
513 curwin->w_redr_status = TRUE;
514
515 save_cmdline(&save_ccline);
516 update_screen(SOME_VALID);
517 restore_cmdline(&save_ccline);
518 restore_last_search_pattern();
519
520 // Leave it at the end to make CTRL-R CTRL-W work.
521 if (i != 0)
522 curwin->w_cursor = end_pos;
523
524 msg_starthere();
525 redrawcmdline();
526 is_state->did_incsearch = TRUE;
527}
528
529/*
530 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
531 * or previous match.
532 * Returns FAIL when jumping to cmdline_not_changed;
533 */
534 static int
535may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200536 int firstc,
537 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200538 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200539 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200541 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200542 pos_T t;
543 char_u *pat;
544 int search_flags = SEARCH_NOOF;
545 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200546 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200548 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200550 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200551 return FAIL;
552
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200553 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200554 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200555 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200556 skiplen = 0;
557 patlen = STRLEN(pat);
558 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200560 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561
562 save_last_search_pattern();
563 cursor_off();
564 out_flush();
565 if (c == Ctrl_G)
566 {
567 t = is_state->match_end;
568 if (LT_POS(is_state->match_start, is_state->match_end))
569 // Start searching at the end of the match not at the beginning of
570 // the next column.
571 (void)decl(&t);
572 search_flags += SEARCH_COL;
573 }
574 else
575 t = is_state->match_start;
576 if (!p_hls)
577 search_flags += SEARCH_KEEP;
578 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200579 save = pat[patlen];
580 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200581 i = searchit(curwin, curbuf, &t,
582 c == Ctrl_G ? FORWARD : BACKWARD,
583 pat, count, search_flags,
584 RE_SEARCH, 0, NULL, NULL);
585 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200586 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587 if (i)
588 {
589 is_state->search_start = is_state->match_start;
590 is_state->match_end = t;
591 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200592 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 {
594 // Move just before the current match, so that when nv_search
595 // finishes the cursor will be put back on the match.
596 is_state->search_start = t;
597 (void)decl(&is_state->search_start);
598 }
599 else if (c == Ctrl_G && firstc == '?')
600 {
601 // Move just after the current match, so that when nv_search
602 // finishes the cursor will be put back on the match.
603 is_state->search_start = t;
604 (void)incl(&is_state->search_start);
605 }
606 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
607 {
608 // wrap around
609 is_state->search_start = t;
610 if (firstc == '?')
611 (void)incl(&is_state->search_start);
612 else
613 (void)decl(&is_state->search_start);
614 }
615
616 set_search_match(&is_state->match_end);
617 curwin->w_cursor = is_state->match_start;
618 changed_cline_bef_curs();
619 update_topline();
620 validate_cursor();
621 highlight_match = TRUE;
622 save_viewstate(&is_state->old_viewstate);
623 update_screen(NOT_VALID);
624 redrawcmdline();
625 }
626 else
627 vim_beep(BO_ERROR);
628 restore_last_search_pattern();
629 return FAIL;
630}
631
632/*
633 * When CTRL-L typed: add character from the match to the pattern.
634 * May set "*c" to the added character.
635 * Return OK when jumping to cmdline_not_changed.
636 */
637 static int
638may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
639{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200640 int skiplen, patlen;
641
642 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200643 return FAIL;
644
645 // Add a character from under the cursor for 'incsearch'.
646 if (is_state->did_incsearch)
647 {
648 curwin->w_cursor = is_state->match_end;
649 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
650 {
651 *c = gchar_cursor();
652
653 // If 'ignorecase' and 'smartcase' are set and the
654 // command line has no uppercase characters, convert
655 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200656 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200657 *c = MB_TOLOWER(*c);
658 if (*c != NUL)
659 {
660 if (*c == firstc || vim_strchr((char_u *)(
661 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
662 {
663 // put a backslash before special characters
664 stuffcharReadbuff(*c);
665 *c = '\\';
666 }
667 return FAIL;
668 }
669 }
670 }
671 return OK;
672}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200673#endif
674
Bram Moolenaar66216052017-12-16 16:33:44 +0100675/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 * getcmdline() - accept a command line starting with firstc.
677 *
678 * firstc == ':' get ":" command line.
679 * firstc == '/' or '?' get search pattern
680 * firstc == '=' get expression
681 * firstc == '@' get text for input() function
682 * firstc == '>' get text for debug mode
683 * firstc == NUL get text for :insert command
684 * firstc == -1 like NUL, and break on CTRL-C
685 *
686 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
687 * command line.
688 *
689 * Careful: getcmdline() can be called recursively!
690 *
691 * Return pointer to allocated string if there is a commandline, NULL
692 * otherwise.
693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100695getcmdline(
696 int firstc,
697 long count UNUSED, /* only used for incremental search */
698 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699{
700 int c;
701 int i;
702 int j;
703 int gotesc = FALSE; /* TRUE when <ESC> just typed */
704 int do_abbr; /* when TRUE check for abbr. */
705#ifdef FEAT_CMDHIST
706 char_u *lookfor = NULL; /* string to match */
707 int hiscnt; /* current history line in use */
708 int histype; /* history type to be used */
709#endif
710#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200711 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#endif
713 int did_wild_list = FALSE; /* did wild_list() recently */
714 int wim_index = 0; /* index in wim_flags[] */
715 int res;
716 int save_msg_scroll = msg_scroll;
717 int save_State = State; /* remember State when called */
718 int some_key_typed = FALSE; /* one of the keys was typed */
719#ifdef FEAT_MOUSE
720 /* mouse drag and release events are ignored, unless they are
721 * preceded with a mouse down event */
722 int ignore_drag_release = TRUE;
723#endif
724#ifdef FEAT_EVAL
725 int break_ctrl_c = FALSE;
726#endif
727 expand_T xpc;
728 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200729#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000730 /* Everything that may work recursively should save and restore the
731 * current command line in save_ccline. That includes update_screen(), a
732 * custom status line may invoke ":normal". */
733 struct cmdline_info save_ccline;
734#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200735 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737#ifdef FEAT_EVAL
738 if (firstc == -1)
739 {
740 firstc = NUL;
741 break_ctrl_c = TRUE;
742 }
743#endif
744#ifdef FEAT_RIGHTLEFT
745 /* start without Hebrew mapping for a command line */
746 if (firstc == ':' || firstc == '=' || firstc == '>')
747 cmd_hkmap = 0;
748#endif
749
750 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200753 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754#endif
755
756 /*
757 * set some variables for redrawcmd()
758 */
759 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000760 ccline.cmdindent = (firstc > 0 ? indent : 0);
761
762 /* alloc initial ccline.cmdbuff */
763 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764 if (ccline.cmdbuff == NULL)
765 return NULL; /* out of memory */
766 ccline.cmdlen = ccline.cmdpos = 0;
767 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100768 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000769
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000770 /* autoindent for :insert and :append */
771 if (firstc <= 0)
772 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200773 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000774 ccline.cmdbuff[indent] = NUL;
775 ccline.cmdpos = indent;
776 ccline.cmdspos = indent;
777 ccline.cmdlen = indent;
778 }
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000781 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782
783#ifdef FEAT_RIGHTLEFT
784 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
785 && (firstc == '/' || firstc == '?'))
786 cmdmsg_rl = TRUE;
787 else
788 cmdmsg_rl = FALSE;
789#endif
790
791 redir_off = TRUE; /* don't redirect the typed command */
792 if (!cmd_silent)
793 {
794 i = msg_scrolled;
795 msg_scrolled = 0; /* avoid wait_return message */
796 gotocmdline(TRUE);
797 msg_scrolled += i;
798 redrawcmdprompt(); /* draw prompt or indent */
799 set_cmdspos();
800 }
801 xpc.xp_context = EXPAND_NOTHING;
802 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000803#ifndef BACKSLASH_IN_FILENAME
804 xpc.xp_shell = FALSE;
805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000807#if defined(FEAT_EVAL)
808 if (ccline.input_fn)
809 {
810 xpc.xp_context = ccline.xp_context;
811 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000812# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000813 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000814# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000815 }
816#endif
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /*
819 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
820 * doing ":@0" when register 0 doesn't contain a CR.
821 */
822 msg_scroll = FALSE;
823
824 State = CMDLINE;
825
826 if (firstc == '/' || firstc == '?' || firstc == '@')
827 {
828 /* Use ":lmap" mappings for search pattern and input(). */
829 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
830 b_im_ptr = &curbuf->b_p_iminsert;
831 else
832 b_im_ptr = &curbuf->b_p_imsearch;
833 if (*b_im_ptr == B_IMODE_LMAP)
834 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100835#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 im_set_active(*b_im_ptr == B_IMODE_IM);
837#endif
838 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100839#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 else if (p_imcmdline)
841 im_set_active(TRUE);
842#endif
843
844#ifdef FEAT_MOUSE
845 setmouse();
846#endif
847#ifdef CURSOR_SHAPE
848 ui_cursor_shape(); /* may show different cursor shape */
849#endif
850
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000851 /* When inside an autocommand for writing "exiting" may be set and
852 * terminal mode set to cooked. Need to set raw mode here then. */
853 settmode(TMODE_RAW);
854
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200855 /* Trigger CmdlineEnter autocommands. */
856 cmdline_type = firstc == NUL ? '-' : firstc;
857 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200858
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859#ifdef FEAT_CMDHIST
860 init_history();
861 hiscnt = hislen; /* set hiscnt to impossible history value */
862 histype = hist_char2type(firstc);
863#endif
864
865#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000866 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#endif
868
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200869 /* If something above caused an error, reset the flags, we do want to type
870 * and execute commands. Display may be messed up a bit. */
871 if (did_emsg)
872 redrawcmd();
873 did_emsg = FALSE;
874 got_int = FALSE;
875
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 /*
877 * Collect the command string, handling editing keys.
878 */
879 for (;;)
880 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000881 redir_off = TRUE; /* Don't redirect the typed command.
882 Repeated, because a ":redir" inside
883 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#ifdef USE_ON_FLY_SCROLL
885 dont_scroll = FALSE; /* allow scrolling here */
886#endif
887 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
888
Bram Moolenaar72532d32018-04-07 19:09:09 +0200889 did_emsg = FALSE; /* There can't really be a reason why an error
890 that occurs while typing a command should
891 cause the command not to be executed. */
892
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000894
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100895 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
896 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000897 do
898 {
899 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100900 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 if (KeyTyped)
903 {
904 some_key_typed = TRUE;
905#ifdef FEAT_RIGHTLEFT
906 if (cmd_hkmap)
907 c = hkmap(c);
908# ifdef FEAT_FKMAP
909 if (cmd_fkmap)
910 c = cmdl_fkmap(c);
911# endif
912 if (cmdmsg_rl && !KeyStuffed)
913 {
914 /* Invert horizontal movements and operations. Only when
915 * typed by the user directly, not when the result of a
916 * mapping. */
917 switch (c)
918 {
919 case K_RIGHT: c = K_LEFT; break;
920 case K_S_RIGHT: c = K_S_LEFT; break;
921 case K_C_RIGHT: c = K_C_LEFT; break;
922 case K_LEFT: c = K_RIGHT; break;
923 case K_S_LEFT: c = K_S_RIGHT; break;
924 case K_C_LEFT: c = K_C_RIGHT; break;
925 }
926 }
927#endif
928 }
929
930 /*
931 * Ignore got_int when CTRL-C was typed here.
932 * Don't ignore it in :global, we really need to break then, e.g., for
933 * ":g/pat/normal /pat" (without the <CR>).
934 * Don't ignore it for the input() function.
935 */
936 if ((c == Ctrl_C
937#ifdef UNIX
938 || c == intr_char
939#endif
940 )
941#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
942 && firstc != '@'
943#endif
944#ifdef FEAT_EVAL
945 && !break_ctrl_c
946#endif
947 && !global_busy)
948 got_int = FALSE;
949
950#ifdef FEAT_CMDHIST
951 /* free old command line when finished moving around in the history
952 * list */
953 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +0000954 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000955 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 && c != K_PAGEDOWN && c != K_PAGEUP
957 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000958 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +0100960 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#endif
962
963 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000964 * When there are matching completions to select <S-Tab> works like
965 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000967 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 c = Ctrl_P;
969
970#ifdef FEAT_WILDMENU
971 /* Special translations for 'wildmenu' */
972 if (did_wild_list && p_wmnu)
973 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000974 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000976 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 c = Ctrl_N;
978 }
979 /* Hitting CR after "emenu Name.": complete submenu */
980 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
981 && ccline.cmdpos > 1
982 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
983 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
984 && (c == '\n' || c == '\r' || c == K_KENTER))
985 c = K_DOWN;
986#endif
987
988 /* free expanded names when finished walking through matches */
989 if (xpc.xp_numfiles != -1
990 && !(c == p_wc && KeyTyped) && c != p_wcm
991 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
992 && c != Ctrl_L)
993 {
994 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
995 did_wild_list = FALSE;
996#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000997 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998#endif
999 xpc.xp_context = EXPAND_NOTHING;
1000 wim_index = 0;
1001#ifdef FEAT_WILDMENU
1002 if (p_wmnu && wild_menu_showing != 0)
1003 {
1004 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001005 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001006
1007 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001008 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
1010 if (wild_menu_showing == WM_SCROLLED)
1011 {
1012 /* Entered command line, move it up */
1013 cmdline_row--;
1014 redrawcmd();
1015 }
1016 else if (save_p_ls != -1)
1017 {
1018 /* restore 'laststatus' and 'winminheight' */
1019 p_ls = save_p_ls;
1020 p_wmh = save_p_wmh;
1021 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001022 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001024 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 redrawcmd();
1026 save_p_ls = -1;
1027 }
1028 else
1029 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 redraw_statuslines();
1032 }
1033 KeyTyped = skt;
1034 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001035 if (ccline.input_fn)
1036 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 }
1038#endif
1039 }
1040
1041#ifdef FEAT_WILDMENU
1042 /* Special translations for 'wildmenu' */
1043 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1044 {
1045 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001046 if (c == K_DOWN && ccline.cmdpos > 0
1047 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001049 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 {
1051 /* Hitting <Up>: Remove one submenu name in front of the
1052 * cursor */
1053 int found = FALSE;
1054
1055 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1056 i = 0;
1057 while (--j > 0)
1058 {
1059 /* check for start of menu name */
1060 if (ccline.cmdbuff[j] == ' '
1061 && ccline.cmdbuff[j - 1] != '\\')
1062 {
1063 i = j + 1;
1064 break;
1065 }
1066 /* check for start of submenu name */
1067 if (ccline.cmdbuff[j] == '.'
1068 && ccline.cmdbuff[j - 1] != '\\')
1069 {
1070 if (found)
1071 {
1072 i = j + 1;
1073 break;
1074 }
1075 else
1076 found = TRUE;
1077 }
1078 }
1079 if (i > 0)
1080 cmdline_del(i);
1081 c = p_wc;
1082 xpc.xp_context = EXPAND_NOTHING;
1083 }
1084 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001085 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001086 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001087 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
1089 char_u upseg[5];
1090
1091 upseg[0] = PATHSEP;
1092 upseg[1] = '.';
1093 upseg[2] = '.';
1094 upseg[3] = PATHSEP;
1095 upseg[4] = NUL;
1096
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001097 if (c == K_DOWN
1098 && ccline.cmdpos > 0
1099 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1100 && (ccline.cmdpos < 3
1101 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1103 {
1104 /* go down a directory */
1105 c = p_wc;
1106 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001107 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 {
1109 /* If in a direct ancestor, strip off one ../ to go down */
1110 int found = FALSE;
1111
1112 j = ccline.cmdpos;
1113 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1114 while (--j > i)
1115 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001116#ifdef FEAT_MBYTE
1117 if (has_mbyte)
1118 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (vim_ispathsep(ccline.cmdbuff[j]))
1121 {
1122 found = TRUE;
1123 break;
1124 }
1125 }
1126 if (found
1127 && ccline.cmdbuff[j - 1] == '.'
1128 && ccline.cmdbuff[j - 2] == '.'
1129 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1130 {
1131 cmdline_del(j - 2);
1132 c = p_wc;
1133 }
1134 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001135 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 {
1137 /* go up a directory */
1138 int found = FALSE;
1139
1140 j = ccline.cmdpos - 1;
1141 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1142 while (--j > i)
1143 {
1144#ifdef FEAT_MBYTE
1145 if (has_mbyte)
1146 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1147#endif
1148 if (vim_ispathsep(ccline.cmdbuff[j])
1149#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001150 && vim_strchr((char_u *)" *?[{`$%#",
1151 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152#endif
1153 )
1154 {
1155 if (found)
1156 {
1157 i = j + 1;
1158 break;
1159 }
1160 else
1161 found = TRUE;
1162 }
1163 }
1164
1165 if (!found)
1166 j = i;
1167 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1168 j += 4;
1169 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1170 && j == i)
1171 j += 3;
1172 else
1173 j = 0;
1174 if (j > 0)
1175 {
1176 /* TODO this is only for DOS/UNIX systems - need to put in
1177 * machine-specific stuff here and in upseg init */
1178 cmdline_del(j);
1179 put_on_cmdline(upseg + 1, 3, FALSE);
1180 }
1181 else if (ccline.cmdpos > i)
1182 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001183
1184 /* Now complete in the new directory. Set KeyTyped in case the
1185 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001187 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 }
1189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190
1191#endif /* FEAT_WILDMENU */
1192
1193 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1194 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1195 if (c == Ctrl_BSL)
1196 {
1197 ++no_mapping;
1198 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001199 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 --no_mapping;
1201 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001202 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1203 * is in a mapping. */
1204 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1205 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 {
1207 vungetc(c);
1208 c = Ctrl_BSL;
1209 }
1210#ifdef FEAT_EVAL
1211 else if (c == 'e')
1212 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001213 char_u *p = NULL;
1214 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 /*
1217 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001218 * Need to save and restore the current command line, to be
1219 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
1221 if (ccline.cmdpos == ccline.cmdlen)
1222 new_cmdpos = 99999; /* keep it at the end */
1223 else
1224 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001225
1226 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001228 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 if (c == '=')
1230 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001231 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001232 * to avoid nasty things like going to another buffer when
1233 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001234 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001235 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001237 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001238 restore_cmdline(&save_ccline);
1239
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001240 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001242 len = (int)STRLEN(p);
1243 if (realloc_cmdbuff(len + 1) == OK)
1244 {
1245 ccline.cmdlen = len;
1246 STRCPY(ccline.cmdbuff, p);
1247 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001249 /* Restore the cursor or use the position set with
1250 * set_cmdline_pos(). */
1251 if (new_cmdpos > ccline.cmdlen)
1252 ccline.cmdpos = ccline.cmdlen;
1253 else
1254 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001256 KeyTyped = FALSE; /* Don't do p_wc completion. */
1257 redrawcmd();
1258 goto cmdline_changed;
1259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 }
1261 }
1262 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001263 got_int = FALSE; /* don't abandon the command line */
1264 did_emsg = FALSE;
1265 emsg_on_display = FALSE;
1266 redrawcmd();
1267 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 }
1269#endif
1270 else
1271 {
1272 if (c == Ctrl_G && p_im && restart_edit == 0)
1273 restart_edit = 'a';
1274 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1275 in history */
1276 goto returncmd; /* back to Normal mode */
1277 }
1278 }
1279
1280#ifdef FEAT_CMDWIN
1281 if (c == cedit_key || c == K_CMDWIN)
1282 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001283 if (ex_normal_busy == 0 && got_int == FALSE)
1284 {
1285 /*
1286 * Open a window to edit the command line (and history).
1287 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001288 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001289 some_key_typed = TRUE;
1290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291 }
1292# ifdef FEAT_DIGRAPHS
1293 else
1294# endif
1295#endif
1296#ifdef FEAT_DIGRAPHS
1297 c = do_digraph(c);
1298#endif
1299
1300 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1301 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1302 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001303 /* In Ex mode a backslash escapes a newline. */
1304 if (exmode_active
1305 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001306 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001307 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001308 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001310 if (c == K_KENTER)
1311 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001313 else
1314 {
1315 gotesc = FALSE; /* Might have typed ESC previously, don't
1316 truncate the cmdline now. */
1317 if (ccheck_abbr(c + ABBR_OFF))
1318 goto cmdline_changed;
1319 if (!cmd_silent)
1320 {
1321 windgoto(msg_row, 0);
1322 out_flush();
1323 }
1324 break;
1325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327
1328 /*
1329 * Completion for 'wildchar' or 'wildcharm' key.
1330 * - hitting <ESC> twice means: abandon command line.
1331 * - wildcard expansion is only done when the 'wildchar' key is really
1332 * typed, not when it comes from a macro
1333 */
1334 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1335 {
1336 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1337 {
1338 /* if 'wildmode' contains "list" may still need to list */
1339 if (xpc.xp_numfiles > 1
1340 && !did_wild_list
1341 && (wim_flags[wim_index] & WIM_LIST))
1342 {
1343 (void)showmatches(&xpc, FALSE);
1344 redrawcmd();
1345 did_wild_list = TRUE;
1346 }
1347 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001348 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1349 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001351 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1352 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 else
1354 res = OK; /* don't insert 'wildchar' now */
1355 }
1356 else /* typed p_wc first time */
1357 {
1358 wim_index = 0;
1359 j = ccline.cmdpos;
1360 /* if 'wildmode' first contains "longest", get longest
1361 * common part */
1362 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001363 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1364 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001366 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1367 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368
1369 /* if interrupted while completing, behave like it failed */
1370 if (got_int)
1371 {
1372 (void)vpeekc(); /* remove <C-C> from input stream */
1373 got_int = FALSE; /* don't abandon the command line */
1374 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1375#ifdef FEAT_WILDMENU
1376 xpc.xp_context = EXPAND_NOTHING;
1377#endif
1378 goto cmdline_changed;
1379 }
1380
1381 /* when more than one match, and 'wildmode' first contains
1382 * "list", or no change and 'wildmode' contains "longest,list",
1383 * list all matches */
1384 if (res == OK && xpc.xp_numfiles > 1)
1385 {
1386 /* a "longest" that didn't do anything is skipped (but not
1387 * "list:longest") */
1388 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1389 wim_index = 1;
1390 if ((wim_flags[wim_index] & WIM_LIST)
1391#ifdef FEAT_WILDMENU
1392 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1393#endif
1394 )
1395 {
1396 if (!(wim_flags[0] & WIM_LONGEST))
1397 {
1398#ifdef FEAT_WILDMENU
1399 int p_wmnu_save = p_wmnu;
1400 p_wmnu = 0;
1401#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001402 /* remove match */
1403 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404#ifdef FEAT_WILDMENU
1405 p_wmnu = p_wmnu_save;
1406#endif
1407 }
1408#ifdef FEAT_WILDMENU
1409 (void)showmatches(&xpc, p_wmnu
1410 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1411#else
1412 (void)showmatches(&xpc, FALSE);
1413#endif
1414 redrawcmd();
1415 did_wild_list = TRUE;
1416 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001417 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1418 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001420 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1421 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 }
1423 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001424 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426#ifdef FEAT_WILDMENU
1427 else if (xpc.xp_numfiles == -1)
1428 xpc.xp_context = EXPAND_NOTHING;
1429#endif
1430 }
1431 if (wim_index < 3)
1432 ++wim_index;
1433 if (c == ESC)
1434 gotesc = TRUE;
1435 if (res == OK)
1436 goto cmdline_changed;
1437 }
1438
1439 gotesc = FALSE;
1440
1441 /* <S-Tab> goes to last match, in a clumsy way */
1442 if (c == K_S_TAB && KeyTyped)
1443 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001444 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1445 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1446 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 goto cmdline_changed;
1448 }
1449
1450 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1451 c = NL;
1452
1453 do_abbr = TRUE; /* default: check for abbreviation */
1454
1455 /*
1456 * Big switch for a typed command line character.
1457 */
1458 switch (c)
1459 {
1460 case K_BS:
1461 case Ctrl_H:
1462 case K_DEL:
1463 case K_KDEL:
1464 case Ctrl_W:
1465#ifdef FEAT_FKMAP
1466 if (cmd_fkmap && c == K_BS)
1467 c = K_DEL;
1468#endif
1469 if (c == K_KDEL)
1470 c = K_DEL;
1471
1472 /*
1473 * delete current character is the same as backspace on next
1474 * character, except at end of line
1475 */
1476 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1477 ++ccline.cmdpos;
1478#ifdef FEAT_MBYTE
1479 if (has_mbyte && c == K_DEL)
1480 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1481 ccline.cmdbuff + ccline.cmdpos);
1482#endif
1483 if (ccline.cmdpos > 0)
1484 {
1485 char_u *p;
1486
1487 j = ccline.cmdpos;
1488 p = ccline.cmdbuff + j;
1489#ifdef FEAT_MBYTE
1490 if (has_mbyte)
1491 {
1492 p = mb_prevptr(ccline.cmdbuff, p);
1493 if (c == Ctrl_W)
1494 {
1495 while (p > ccline.cmdbuff && vim_isspace(*p))
1496 p = mb_prevptr(ccline.cmdbuff, p);
1497 i = mb_get_class(p);
1498 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1499 p = mb_prevptr(ccline.cmdbuff, p);
1500 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001501 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 }
1503 }
1504 else
1505#endif
1506 if (c == Ctrl_W)
1507 {
1508 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1509 --p;
1510 i = vim_iswordc(p[-1]);
1511 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1512 && vim_iswordc(p[-1]) == i)
1513 --p;
1514 }
1515 else
1516 --p;
1517 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1518 ccline.cmdlen -= j - ccline.cmdpos;
1519 i = ccline.cmdpos;
1520 while (i < ccline.cmdlen)
1521 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1522
1523 /* Truncate at the end, required for multi-byte chars. */
1524 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001525#ifdef FEAT_SEARCH_EXTRA
1526 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001527 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001528 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001529 /* save view settings, so that the screen
1530 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001531 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001532 }
1533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 redrawcmd();
1535 }
1536 else if (ccline.cmdlen == 0 && c != Ctrl_W
1537 && ccline.cmdprompt == NULL && indent == 0)
1538 {
1539 /* In ex and debug mode it doesn't make sense to return. */
1540 if (exmode_active
1541#ifdef FEAT_EVAL
1542 || ccline.cmdfirstc == '>'
1543#endif
1544 )
1545 goto cmdline_not_changed;
1546
Bram Moolenaard23a8232018-02-10 18:45:26 +01001547 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 if (!cmd_silent)
1549 {
1550#ifdef FEAT_RIGHTLEFT
1551 if (cmdmsg_rl)
1552 msg_col = Columns;
1553 else
1554#endif
1555 msg_col = 0;
1556 msg_putchar(' '); /* delete ':' */
1557 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001558#ifdef FEAT_SEARCH_EXTRA
1559 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001560 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001561#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 redraw_cmdline = TRUE;
1563 goto returncmd; /* back to cmd mode */
1564 }
1565 goto cmdline_changed;
1566
1567 case K_INS:
1568 case K_KINS:
1569#ifdef FEAT_FKMAP
1570 /* if Farsi mode set, we are in reverse insert mode -
1571 Do not change the mode */
1572 if (cmd_fkmap)
1573 beep_flush();
1574 else
1575#endif
1576 ccline.overstrike = !ccline.overstrike;
1577#ifdef CURSOR_SHAPE
1578 ui_cursor_shape(); /* may show different cursor shape */
1579#endif
1580 goto cmdline_not_changed;
1581
1582 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001583 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
1585 /* ":lmap" mappings exists, toggle use of mappings. */
1586 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001587#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 im_set_active(FALSE); /* Disable input method */
1589#endif
1590 if (b_im_ptr != NULL)
1591 {
1592 if (State & LANGMAP)
1593 *b_im_ptr = B_IMODE_LMAP;
1594 else
1595 *b_im_ptr = B_IMODE_NONE;
1596 }
1597 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001598#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 else
1600 {
1601 /* There are no ":lmap" mappings, toggle IM. When
1602 * 'imdisable' is set don't try getting the status, it's
1603 * always off. */
1604 if ((p_imdisable && b_im_ptr != NULL)
1605 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1606 {
1607 im_set_active(FALSE); /* Disable input method */
1608 if (b_im_ptr != NULL)
1609 *b_im_ptr = B_IMODE_NONE;
1610 }
1611 else
1612 {
1613 im_set_active(TRUE); /* Enable input method */
1614 if (b_im_ptr != NULL)
1615 *b_im_ptr = B_IMODE_IM;
1616 }
1617 }
1618#endif
1619 if (b_im_ptr != NULL)
1620 {
1621 if (b_im_ptr == &curbuf->b_p_iminsert)
1622 set_iminsert_global();
1623 else
1624 set_imsearch_global();
1625 }
1626#ifdef CURSOR_SHAPE
1627 ui_cursor_shape(); /* may show different cursor shape */
1628#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001629#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 /* Show/unshow value of 'keymap' in status lines later. */
1631 status_redraw_curbuf();
1632#endif
1633 goto cmdline_not_changed;
1634
1635/* case '@': only in very old vi */
1636 case Ctrl_U:
1637 /* delete all characters left of the cursor */
1638 j = ccline.cmdpos;
1639 ccline.cmdlen -= j;
1640 i = ccline.cmdpos = 0;
1641 while (i < ccline.cmdlen)
1642 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1643 /* Truncate at the end, required for multi-byte chars. */
1644 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001645#ifdef FEAT_SEARCH_EXTRA
1646 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001647 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 redrawcmd();
1650 goto cmdline_changed;
1651
1652#ifdef FEAT_CLIPBOARD
1653 case Ctrl_Y:
1654 /* Copy the modeless selection, if there is one. */
1655 if (clip_star.state != SELECT_CLEARED)
1656 {
1657 if (clip_star.state == SELECT_DONE)
1658 clip_copy_modeless_selection(TRUE);
1659 goto cmdline_not_changed;
1660 }
1661 break;
1662#endif
1663
1664 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1665 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001666 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001667 * ":normal" runs out of characters. */
1668 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001669 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 goto cmdline_not_changed;
1671
1672 gotesc = TRUE; /* will free ccline.cmdbuff after
1673 putting it in history */
1674 goto returncmd; /* back to cmd mode */
1675
1676 case Ctrl_R: /* insert register */
1677#ifdef USE_ON_FLY_SCROLL
1678 dont_scroll = TRUE; /* disallow scrolling here */
1679#endif
1680 putcmdline('"', TRUE);
1681 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001682 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (i == Ctrl_O)
1684 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1685 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001686 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001687 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 --no_mapping;
1689#ifdef FEAT_EVAL
1690 /*
1691 * Insert the result of an expression.
1692 * Need to save the current command line, to be able to enter
1693 * a new one...
1694 */
1695 new_cmdpos = -1;
1696 if (c == '=')
1697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1699 {
1700 beep_flush();
1701 c = ESC;
1702 }
1703 else
1704 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001705 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001707 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 }
1709 }
1710#endif
1711 if (c != ESC) /* use ESC to cancel inserting register */
1712 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001713 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001714
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001715#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001716 /* When there was a serious error abort getting the
1717 * command line. */
1718 if (aborting())
1719 {
1720 gotesc = TRUE; /* will free ccline.cmdbuff after
1721 putting it in history */
1722 goto returncmd; /* back to cmd mode */
1723 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 KeyTyped = FALSE; /* Don't do p_wc completion. */
1726#ifdef FEAT_EVAL
1727 if (new_cmdpos >= 0)
1728 {
1729 /* set_cmdline_pos() was used */
1730 if (new_cmdpos > ccline.cmdlen)
1731 ccline.cmdpos = ccline.cmdlen;
1732 else
1733 ccline.cmdpos = new_cmdpos;
1734 }
1735#endif
1736 }
1737 redrawcmd();
1738 goto cmdline_changed;
1739
1740 case Ctrl_D:
1741 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1742 break; /* Use ^D as normal char instead */
1743
1744 redrawcmd();
1745 continue; /* don't do incremental search now */
1746
1747 case K_RIGHT:
1748 case K_S_RIGHT:
1749 case K_C_RIGHT:
1750 do
1751 {
1752 if (ccline.cmdpos >= ccline.cmdlen)
1753 break;
1754 i = cmdline_charsize(ccline.cmdpos);
1755 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1756 break;
1757 ccline.cmdspos += i;
1758#ifdef FEAT_MBYTE
1759 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001760 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 + ccline.cmdpos);
1762 else
1763#endif
1764 ++ccline.cmdpos;
1765 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001766 while ((c == K_S_RIGHT || c == K_C_RIGHT
1767 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1769#ifdef FEAT_MBYTE
1770 if (has_mbyte)
1771 set_cmdspos_cursor();
1772#endif
1773 goto cmdline_not_changed;
1774
1775 case K_LEFT:
1776 case K_S_LEFT:
1777 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001778 if (ccline.cmdpos == 0)
1779 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 do
1781 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 --ccline.cmdpos;
1783#ifdef FEAT_MBYTE
1784 if (has_mbyte) /* move to first byte of char */
1785 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1786 ccline.cmdbuff + ccline.cmdpos);
1787#endif
1788 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1789 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001790 while (ccline.cmdpos > 0
1791 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001792 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1794#ifdef FEAT_MBYTE
1795 if (has_mbyte)
1796 set_cmdspos_cursor();
1797#endif
1798 goto cmdline_not_changed;
1799
1800 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001801 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001802 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
Bram Moolenaar4770d092006-01-12 23:22:24 +00001804#ifdef FEAT_GUI_W32
1805 /* On Win32 ignore <M-F4>, we get it when closing the window was
1806 * cancelled. */
1807 case K_F4:
1808 if (mod_mask == MOD_MASK_ALT)
1809 {
1810 redrawcmd(); /* somehow the cmdline is cleared */
1811 goto cmdline_not_changed;
1812 }
1813 break;
1814#endif
1815
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816#ifdef FEAT_MOUSE
1817 case K_MIDDLEDRAG:
1818 case K_MIDDLERELEASE:
1819 goto cmdline_not_changed; /* Ignore mouse */
1820
1821 case K_MIDDLEMOUSE:
1822# ifdef FEAT_GUI
1823 /* When GUI is active, also paste when 'mouse' is empty */
1824 if (!gui.in_use)
1825# endif
1826 if (!mouse_has(MOUSE_COMMAND))
1827 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001828# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001830 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001832# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001833 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 redrawcmd();
1835 goto cmdline_changed;
1836
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001837# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001839 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 redrawcmd();
1841 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001842# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844 case K_LEFTDRAG:
1845 case K_LEFTRELEASE:
1846 case K_RIGHTDRAG:
1847 case K_RIGHTRELEASE:
1848 /* Ignore drag and release events when the button-down wasn't
1849 * seen before. */
1850 if (ignore_drag_release)
1851 goto cmdline_not_changed;
1852 /* FALLTHROUGH */
1853 case K_LEFTMOUSE:
1854 case K_RIGHTMOUSE:
1855 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1856 ignore_drag_release = TRUE;
1857 else
1858 ignore_drag_release = FALSE;
1859# ifdef FEAT_GUI
1860 /* When GUI is active, also move when 'mouse' is empty */
1861 if (!gui.in_use)
1862# endif
1863 if (!mouse_has(MOUSE_COMMAND))
1864 goto cmdline_not_changed; /* Ignore mouse */
1865# ifdef FEAT_CLIPBOARD
1866 if (mouse_row < cmdline_row && clip_star.available)
1867 {
1868 int button, is_click, is_drag;
1869
1870 /*
1871 * Handle modeless selection.
1872 */
1873 button = get_mouse_button(KEY2TERMCAP1(c),
1874 &is_click, &is_drag);
1875 if (mouse_model_popup() && button == MOUSE_LEFT
1876 && (mod_mask & MOD_MASK_SHIFT))
1877 {
1878 /* Translate shift-left to right button. */
1879 button = MOUSE_RIGHT;
1880 mod_mask &= ~MOD_MASK_SHIFT;
1881 }
1882 clip_modeless(button, is_click, is_drag);
1883 goto cmdline_not_changed;
1884 }
1885# endif
1886
1887 set_cmdspos();
1888 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1889 ++ccline.cmdpos)
1890 {
1891 i = cmdline_charsize(ccline.cmdpos);
1892 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1893 && mouse_col < ccline.cmdspos % Columns + i)
1894 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001895# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (has_mbyte)
1897 {
1898 /* Count ">" for double-wide char that doesn't fit. */
1899 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001900 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 + ccline.cmdpos) - 1;
1902 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001903# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 ccline.cmdspos += i;
1905 }
1906 goto cmdline_not_changed;
1907
1908 /* Mouse scroll wheel: ignored here */
1909 case K_MOUSEDOWN:
1910 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001911 case K_MOUSELEFT:
1912 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 /* Alternate buttons ignored here */
1914 case K_X1MOUSE:
1915 case K_X1DRAG:
1916 case K_X1RELEASE:
1917 case K_X2MOUSE:
1918 case K_X2DRAG:
1919 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001920 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 goto cmdline_not_changed;
1922
1923#endif /* FEAT_MOUSE */
1924
1925#ifdef FEAT_GUI
1926 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1927 case K_LEFTRELEASE_NM:
1928 goto cmdline_not_changed;
1929
1930 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001931 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
1933 gui_do_scroll();
1934 redrawcmd();
1935 }
1936 goto cmdline_not_changed;
1937
1938 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001939 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001941 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 redrawcmd();
1943 }
1944 goto cmdline_not_changed;
1945#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001946#ifdef FEAT_GUI_TABLINE
1947 case K_TABLINE:
1948 case K_TABMENU:
1949 /* Don't want to change any tabs here. Make sure the same tab
1950 * is still selected. */
1951 if (gui_use_tabline())
1952 gui_mch_set_curtab(tabpage_index(curtab));
1953 goto cmdline_not_changed;
1954#endif
1955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 case K_SELECT: /* end of Select mode mapping - ignore */
1957 goto cmdline_not_changed;
1958
1959 case Ctrl_B: /* begin of command line */
1960 case K_HOME:
1961 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 case K_S_HOME:
1963 case K_C_HOME:
1964 ccline.cmdpos = 0;
1965 set_cmdspos();
1966 goto cmdline_not_changed;
1967
1968 case Ctrl_E: /* end of command line */
1969 case K_END:
1970 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 case K_S_END:
1972 case K_C_END:
1973 ccline.cmdpos = ccline.cmdlen;
1974 set_cmdspos_cursor();
1975 goto cmdline_not_changed;
1976
1977 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001978 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 break;
1980 goto cmdline_changed;
1981
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001982 case Ctrl_L:
1983#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001984 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001985 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00001986#endif
1987
1988 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01001989 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 break;
1991 goto cmdline_changed;
1992
1993 case Ctrl_N: /* next match */
1994 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02001995 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001997 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
1998 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002000 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002003 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 case K_UP:
2005 case K_DOWN:
2006 case K_S_UP:
2007 case K_S_DOWN:
2008 case K_PAGEUP:
2009 case K_KPAGEUP:
2010 case K_PAGEDOWN:
2011 case K_KPAGEDOWN:
2012 if (hislen == 0 || firstc == NUL) /* no history */
2013 goto cmdline_not_changed;
2014
2015 i = hiscnt;
2016
2017 /* save current command string so it can be restored later */
2018 if (lookfor == NULL)
2019 {
2020 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2021 goto cmdline_not_changed;
2022 lookfor[ccline.cmdpos] = NUL;
2023 }
2024
2025 j = (int)STRLEN(lookfor);
2026 for (;;)
2027 {
2028 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002029 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002030 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
2032 if (hiscnt == hislen) /* first time */
2033 hiscnt = hisidx[histype];
2034 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2035 hiscnt = hislen - 1;
2036 else if (hiscnt != hisidx[histype] + 1)
2037 --hiscnt;
2038 else /* at top of list */
2039 {
2040 hiscnt = i;
2041 break;
2042 }
2043 }
2044 else /* one step forwards */
2045 {
2046 /* on last entry, clear the line */
2047 if (hiscnt == hisidx[histype])
2048 {
2049 hiscnt = hislen;
2050 break;
2051 }
2052
2053 /* not on a history line, nothing to do */
2054 if (hiscnt == hislen)
2055 break;
2056 if (hiscnt == hislen - 1) /* wrap around */
2057 hiscnt = 0;
2058 else
2059 ++hiscnt;
2060 }
2061 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2062 {
2063 hiscnt = i;
2064 break;
2065 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002066 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002067 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 || STRNCMP(history[histype][hiscnt].hisstr,
2069 lookfor, (size_t)j) == 0)
2070 break;
2071 }
2072
2073 if (hiscnt != i) /* jumped to other entry */
2074 {
2075 char_u *p;
2076 int len;
2077 int old_firstc;
2078
2079 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002080 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 if (hiscnt == hislen)
2082 p = lookfor; /* back to the old one */
2083 else
2084 p = history[histype][hiscnt].hisstr;
2085
2086 if (histype == HIST_SEARCH
2087 && p != lookfor
2088 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2089 {
2090 /* Correct for the separator character used when
2091 * adding the history entry vs the one used now.
2092 * First loop: count length.
2093 * Second loop: copy the characters. */
2094 for (i = 0; i <= 1; ++i)
2095 {
2096 len = 0;
2097 for (j = 0; p[j] != NUL; ++j)
2098 {
2099 /* Replace old sep with new sep, unless it is
2100 * escaped. */
2101 if (p[j] == old_firstc
2102 && (j == 0 || p[j - 1] != '\\'))
2103 {
2104 if (i > 0)
2105 ccline.cmdbuff[len] = firstc;
2106 }
2107 else
2108 {
2109 /* Escape new sep, unless it is already
2110 * escaped. */
2111 if (p[j] == firstc
2112 && (j == 0 || p[j - 1] != '\\'))
2113 {
2114 if (i > 0)
2115 ccline.cmdbuff[len] = '\\';
2116 ++len;
2117 }
2118 if (i > 0)
2119 ccline.cmdbuff[len] = p[j];
2120 }
2121 ++len;
2122 }
2123 if (i == 0)
2124 {
2125 alloc_cmdbuff(len);
2126 if (ccline.cmdbuff == NULL)
2127 goto returncmd;
2128 }
2129 }
2130 ccline.cmdbuff[len] = NUL;
2131 }
2132 else
2133 {
2134 alloc_cmdbuff((int)STRLEN(p));
2135 if (ccline.cmdbuff == NULL)
2136 goto returncmd;
2137 STRCPY(ccline.cmdbuff, p);
2138 }
2139
2140 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2141 redrawcmd();
2142 goto cmdline_changed;
2143 }
2144 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002145#endif
2146 goto cmdline_not_changed;
2147
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002148#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002149 case Ctrl_G: /* next match */
2150 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002151 if (may_adjust_incsearch_highlighting(
2152 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002153 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#endif
2156
2157 case Ctrl_V:
2158 case Ctrl_Q:
2159#ifdef FEAT_MOUSE
2160 ignore_drag_release = TRUE;
2161#endif
2162 putcmdline('^', TRUE);
2163 c = get_literal(); /* get next (two) character(s) */
2164 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002165 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166#ifdef FEAT_MBYTE
2167 /* may need to remove ^ when composing char was typed */
2168 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2169 {
2170 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2171 msg_putchar(' ');
2172 cursorcmd();
2173 }
2174#endif
2175 break;
2176
2177#ifdef FEAT_DIGRAPHS
2178 case Ctrl_K:
2179#ifdef FEAT_MOUSE
2180 ignore_drag_release = TRUE;
2181#endif
2182 putcmdline('?', TRUE);
2183#ifdef USE_ON_FLY_SCROLL
2184 dont_scroll = TRUE; /* disallow scrolling here */
2185#endif
2186 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002187 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (c != NUL)
2189 break;
2190
2191 redrawcmd();
2192 goto cmdline_not_changed;
2193#endif /* FEAT_DIGRAPHS */
2194
2195#ifdef FEAT_RIGHTLEFT
2196 case Ctrl__: /* CTRL-_: switch language mode */
2197 if (!p_ari)
2198 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002199# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 if (p_altkeymap)
2201 {
2202 cmd_fkmap = !cmd_fkmap;
2203 if (cmd_fkmap) /* in Farsi always in Insert mode */
2204 ccline.overstrike = FALSE;
2205 }
2206 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002207# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 cmd_hkmap = !cmd_hkmap;
2209 goto cmdline_not_changed;
2210#endif
2211
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002212 case K_PS:
2213 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2214 goto cmdline_changed;
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 default:
2217#ifdef UNIX
2218 if (c == intr_char)
2219 {
2220 gotesc = TRUE; /* will free ccline.cmdbuff after
2221 putting it in history */
2222 goto returncmd; /* back to Normal mode */
2223 }
2224#endif
2225 /*
2226 * Normal character with no special meaning. Just set mod_mask
2227 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2228 * the string <S-Space>. This should only happen after ^V.
2229 */
2230 if (!IS_SPECIAL(c))
2231 mod_mask = 0x0;
2232 break;
2233 }
2234 /*
2235 * End of switch on command line character.
2236 * We come here if we have a normal character.
2237 */
2238
Bram Moolenaarede3e632013-06-23 16:16:19 +02002239 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240#ifdef FEAT_MBYTE
2241 /* Add ABBR_OFF for characters above 0x100, this is
2242 * what check_abbr() expects. */
2243 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2244#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002245 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 goto cmdline_changed;
2247
2248 /*
2249 * put the character in the command line
2250 */
2251 if (IS_SPECIAL(c) || mod_mask != 0)
2252 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2253 else
2254 {
2255#ifdef FEAT_MBYTE
2256 if (has_mbyte)
2257 {
2258 j = (*mb_char2bytes)(c, IObuff);
2259 IObuff[j] = NUL; /* exclude composing chars */
2260 put_on_cmdline(IObuff, j, TRUE);
2261 }
2262 else
2263#endif
2264 {
2265 IObuff[0] = c;
2266 put_on_cmdline(IObuff, 1, TRUE);
2267 }
2268 }
2269 goto cmdline_changed;
2270
2271/*
2272 * This part implements incremental searches for "/" and "?"
2273 * Jump to cmdline_not_changed when a character has been read but the command
2274 * line did not change. Then we only search and redraw if something changed in
2275 * the past.
2276 * Jump to cmdline_changed when the command line did change.
2277 * (Sorry for the goto's, I know it is ugly).
2278 */
2279cmdline_not_changed:
2280#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002281 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 continue;
2283#endif
2284
2285cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002286 /* Trigger CmdlineChanged autocommands. */
2287 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002288
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002290 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#endif
2292
2293#ifdef FEAT_RIGHTLEFT
2294 if (cmdmsg_rl
2295# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002296 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297# endif
2298 )
2299 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002300 * right-left typing. Not efficient, but it works.
2301 * Do it only when there are no characters left to read
2302 * to avoid useless intermediate redraws. */
2303 if (vpeekc() == NUL)
2304 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#endif
2306 }
2307
2308returncmd:
2309
2310#ifdef FEAT_RIGHTLEFT
2311 cmdmsg_rl = FALSE;
2312#endif
2313
2314#ifdef FEAT_FKMAP
2315 cmd_fkmap = 0;
2316#endif
2317
2318 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002319 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320
2321#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002322 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323#endif
2324
2325 if (ccline.cmdbuff != NULL)
2326 {
2327 /*
2328 * Put line in history buffer (":" and "=" only when it was typed).
2329 */
2330#ifdef FEAT_CMDHIST
2331 if (ccline.cmdlen && firstc != NUL
2332 && (some_key_typed || histype == HIST_SEARCH))
2333 {
2334 add_to_history(histype, ccline.cmdbuff, TRUE,
2335 histype == HIST_SEARCH ? firstc : NUL);
2336 if (firstc == ':')
2337 {
2338 vim_free(new_last_cmdline);
2339 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2340 }
2341 }
2342#endif
2343
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002344 if (gotesc)
2345 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347
2348 /*
2349 * If the screen was shifted up, redraw the whole screen (later).
2350 * If the line is too long, clear it, so ruler and shown command do
2351 * not get printed in the middle of it.
2352 */
2353 msg_check();
2354 msg_scroll = save_msg_scroll;
2355 redir_off = FALSE;
2356
2357 /* When the command line was typed, no need for a wait-return prompt. */
2358 if (some_key_typed)
2359 need_wait_return = FALSE;
2360
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002361 /* Trigger CmdlineLeave autocommands. */
2362 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002363
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002365#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2367 im_save_status(b_im_ptr);
2368 im_set_active(FALSE);
2369#endif
2370#ifdef FEAT_MOUSE
2371 setmouse();
2372#endif
2373#ifdef CURSOR_SHAPE
2374 ui_cursor_shape(); /* may show different cursor shape */
2375#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002376 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002378 {
2379 char_u *p = ccline.cmdbuff;
2380
2381 /* Make ccline empty, getcmdline() may try to use it. */
2382 ccline.cmdbuff = NULL;
2383 return p;
2384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385}
2386
2387#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2388/*
2389 * Get a command line with a prompt.
2390 * This is prepared to be called recursively from getcmdline() (e.g. by
2391 * f_input() when evaluating an expression from CTRL-R =).
2392 * Returns the command line in allocated memory, or NULL.
2393 */
2394 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002395getcmdline_prompt(
2396 int firstc,
2397 char_u *prompt, /* command line prompt */
2398 int attr, /* attributes for prompt */
2399 int xp_context, /* type of expansion */
2400 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401{
2402 char_u *s;
2403 struct cmdline_info save_ccline;
2404 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002405 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002407 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 ccline.cmdprompt = prompt;
2409 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002410# ifdef FEAT_EVAL
2411 ccline.xp_context = xp_context;
2412 ccline.xp_arg = xp_arg;
2413 ccline.input_fn = (firstc == '@');
2414# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002415 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002417 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002418 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002419 /* Restore msg_col, the prompt from input() may have changed it.
2420 * But only if called recursively and the commandline is therefore being
2421 * restored to an old one; if not, the input() prompt stays on the screen,
2422 * so we need its modified msg_col left intact. */
2423 if (ccline.cmdbuff != NULL)
2424 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426 return s;
2427}
2428#endif
2429
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002430/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002431 * Return TRUE when the text must not be changed and we can't switch to
2432 * another window or buffer. Used when editing the command line, evaluating
2433 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002434 */
2435 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002436text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002437{
2438#ifdef FEAT_CMDWIN
2439 if (cmdwin_type != 0)
2440 return TRUE;
2441#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002442 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002443}
2444
2445/*
2446 * Give an error message for a command that isn't allowed while the cmdline
2447 * window is open or editing the cmdline in another way.
2448 */
2449 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002450text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002451{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002452 EMSG(_(get_text_locked_msg()));
2453}
2454
2455 char_u *
2456get_text_locked_msg(void)
2457{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002458#ifdef FEAT_CMDWIN
2459 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002460 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002461#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002462 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002463}
2464
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002465/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002466 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2467 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002468 */
2469 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002470curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002471{
2472 if (curbuf_lock > 0)
2473 {
2474 EMSG(_("E788: Not allowed to edit another buffer now"));
2475 return TRUE;
2476 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002477 return allbuf_locked();
2478}
2479
2480/*
2481 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2482 * message.
2483 */
2484 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002485allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002486{
2487 if (allbuf_lock > 0)
2488 {
2489 EMSG(_("E811: Not allowed to change buffer information now"));
2490 return TRUE;
2491 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002492 return FALSE;
2493}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002496cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2499 if (cmdline_star > 0) /* showing '*', always 1 position */
2500 return 1;
2501#endif
2502 return ptr2cells(ccline.cmdbuff + idx);
2503}
2504
2505/*
2506 * Compute the offset of the cursor on the command line for the prompt and
2507 * indent.
2508 */
2509 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002510set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002512 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 ccline.cmdspos = 1 + ccline.cmdindent;
2514 else
2515 ccline.cmdspos = 0 + ccline.cmdindent;
2516}
2517
2518/*
2519 * Compute the screen position for the cursor on the command line.
2520 */
2521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002522set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523{
2524 int i, m, c;
2525
2526 set_cmdspos();
2527 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002528 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002530 if (m < 0) /* overflow, Columns or Rows at weird value */
2531 m = MAXCOL;
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 else
2534 m = MAXCOL;
2535 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2536 {
2537 c = cmdline_charsize(i);
2538#ifdef FEAT_MBYTE
2539 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2540 if (has_mbyte)
2541 correct_cmdspos(i, c);
2542#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002543 /* If the cmdline doesn't fit, show cursor on last visible char.
2544 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 if ((ccline.cmdspos += c) >= m)
2546 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 ccline.cmdspos -= c;
2548 break;
2549 }
2550#ifdef FEAT_MBYTE
2551 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002552 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553#endif
2554 }
2555}
2556
2557#ifdef FEAT_MBYTE
2558/*
2559 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2560 * character that doesn't fit, so that a ">" must be displayed.
2561 */
2562 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002563correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002565 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2567 && ccline.cmdspos % Columns + cells > Columns)
2568 ccline.cmdspos++;
2569}
2570#endif
2571
2572/*
2573 * Get an Ex command line for the ":" command.
2574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002576getexline(
2577 int c, /* normally ':', NUL for ":append" */
2578 void *cookie UNUSED,
2579 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 /* When executing a register, remove ':' that's in front of each line. */
2582 if (exec_from_reg && vpeekc() == ':')
2583 (void)vgetc();
2584 return getcmdline(c, 1L, indent);
2585}
2586
2587/*
2588 * Get an Ex command line for Ex mode.
2589 * In Ex mode we only use the OS supplied line editing features and no
2590 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002591 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002594getexmodeline(
2595 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002596 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002597 void *cookie UNUSED,
2598 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002600 garray_T line_ga;
2601 char_u *pend;
2602 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002603 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002604 int escaped = FALSE; /* CTRL-V typed */
2605 int vcol = 0;
2606 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002607 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002608 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610 /* Switch cursor on now. This avoids that it happens after the "\n", which
2611 * confuses the system function that computes tabstops. */
2612 cursor_on();
2613
2614 /* always start in column 0; write a newline if necessary */
2615 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002616 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002618 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002620 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002621 if (p_prompt)
2622 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 while (indent-- > 0)
2624 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 }
2627
2628 ga_init2(&line_ga, 1, 30);
2629
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002630 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002631 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002632 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002633 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002634 while (indent >= 8)
2635 {
2636 ga_append(&line_ga, TAB);
2637 msg_puts((char_u *)" ");
2638 indent -= 8;
2639 }
2640 while (indent-- > 0)
2641 {
2642 ga_append(&line_ga, ' ');
2643 msg_putchar(' ');
2644 }
2645 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002646 ++no_mapping;
2647 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002648
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 /*
2650 * Get the line, one character at a time.
2651 */
2652 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002653 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002655 long sw;
2656 char_u *s;
2657
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 if (ga_grow(&line_ga, 40) == FAIL)
2659 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
Bram Moolenaarba748c82017-02-26 14:00:07 +01002661 /*
2662 * Get one character at a time.
2663 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002664 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002665
2666 /* Check for a ":normal" command and no more characters left. */
2667 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2668 c1 = '\n';
2669 else
2670 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002673 * Handle line editing.
2674 * Previously this was left to the system, putting the terminal in
2675 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002679 msg_putchar('\n');
2680 break;
2681 }
2682
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002683 if (c1 == K_PS)
2684 {
2685 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2686 goto redraw;
2687 }
2688
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 if (!escaped)
2690 {
2691 /* CR typed means "enter", which is NL */
2692 if (c1 == '\r')
2693 c1 = '\n';
2694
2695 if (c1 == BS || c1 == K_BS
2696 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 if (line_ga.ga_len > 0)
2699 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002700#ifdef FEAT_MBYTE
2701 if (has_mbyte)
2702 {
2703 p = (char_u *)line_ga.ga_data;
2704 p[line_ga.ga_len] = NUL;
2705 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2706 line_ga.ga_len -= len;
2707 }
2708 else
2709#endif
2710 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711 goto redraw;
2712 }
2713 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 }
2715
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002716 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 msg_col = startcol;
2719 msg_clr_eos();
2720 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002721 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 }
2723
2724 if (c1 == Ctrl_T)
2725 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002726 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 p = (char_u *)line_ga.ga_data;
2728 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002729 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002730 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002732 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002734 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002735 p = (char_u *)line_ga.ga_data;
2736 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2738 *s = ' ';
2739 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002741redraw:
2742 /* redraw the line */
2743 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002745 p = (char_u *)line_ga.ga_data;
2746 p[line_ga.ga_len] = NUL;
2747 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002751 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 msg_putchar(' ');
2754 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002755 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002759 len = MB_PTR2LEN(p);
2760 msg_outtrans_len(p, len);
2761 vcol += ptr2cells(p);
2762 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002766 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 continue;
2768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 if (c1 == Ctrl_D)
2771 {
2772 /* Delete one shiftwidth. */
2773 p = (char_u *)line_ga.ga_data;
2774 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 if (prev_char == '^')
2777 ex_keep_indent = TRUE;
2778 indent = 0;
2779 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 }
2781 else
2782 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002784 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002785 if (indent > 0)
2786 {
2787 --indent;
2788 indent -= indent % get_sw_value(curbuf);
2789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002791 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002793 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002794 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2795 --line_ga.ga_len;
2796 }
2797 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799
2800 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2801 {
2802 escaped = TRUE;
2803 continue;
2804 }
2805
2806 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2807 if (IS_SPECIAL(c1))
2808 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810
2811 if (IS_SPECIAL(c1))
2812 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002813#ifdef FEAT_MBYTE
2814 if (has_mbyte)
2815 len = (*mb_char2bytes)(c1,
2816 (char_u *)line_ga.ga_data + line_ga.ga_len);
2817 else
2818#endif
2819 {
2820 len = 1;
2821 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2822 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 if (c1 == '\n')
2824 msg_putchar('\n');
2825 else if (c1 == TAB)
2826 {
2827 /* Don't use chartabsize(), 'ts' can be different */
2828 do
2829 {
2830 msg_putchar(' ');
2831 } while (++vcol % 8);
2832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002836 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002837 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002839 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002840 escaped = FALSE;
2841
2842 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002843 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002845 /* We are done when a NL is entered, but not when it comes after an
2846 * odd number of backslashes, that results in a NUL. */
2847 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002849 int bcount = 0;
2850
2851 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2852 ++bcount;
2853
2854 if (bcount > 0)
2855 {
2856 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2857 * "\NL", etc. */
2858 line_ga.ga_len -= (bcount + 1) / 2;
2859 pend -= (bcount + 1) / 2;
2860 pend[-1] = '\n';
2861 }
2862
2863 if ((bcount & 1) == 0)
2864 {
2865 --line_ga.ga_len;
2866 --pend;
2867 *pend = NUL;
2868 break;
2869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 }
2871 }
2872
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 --no_mapping;
2874 --allow_keys;
2875
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 /* make following messages go to the next line */
2877 msg_didout = FALSE;
2878 msg_col = 0;
2879 if (msg_row < Rows - 1)
2880 ++msg_row;
2881 emsg_on_display = FALSE; /* don't want ui_delay() */
2882
2883 if (got_int)
2884 ga_clear(&line_ga);
2885
2886 return (char_u *)line_ga.ga_data;
2887}
2888
Bram Moolenaare344bea2005-09-01 20:46:49 +00002889# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2890 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891/*
2892 * Return TRUE if ccline.overstrike is on.
2893 */
2894 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002895cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896{
2897 return ccline.overstrike;
2898}
2899
2900/*
2901 * Return TRUE if the cursor is at the end of the cmdline.
2902 */
2903 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002904cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 return (ccline.cmdpos >= ccline.cmdlen);
2907}
2908#endif
2909
Bram Moolenaar9372a112005-12-06 19:59:18 +00002910#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911/*
2912 * Return the virtual column number at the current cursor position.
2913 * This is used by the IM code to obtain the start of the preedit string.
2914 */
2915 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002916cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917{
2918 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2919 return MAXCOL;
2920
2921# ifdef FEAT_MBYTE
2922 if (has_mbyte)
2923 {
2924 colnr_T col;
2925 int i = 0;
2926
2927 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002928 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929
2930 return col;
2931 }
2932 else
2933# endif
2934 return ccline.cmdpos;
2935}
2936#endif
2937
2938#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2939/*
2940 * If part of the command line is an IM preedit string, redraw it with
2941 * IM feedback attributes. The cursor position is restored after drawing.
2942 */
2943 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002944redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945{
2946 if ((State & CMDLINE)
2947 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002948 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 && !p_imdisable
2950 && im_is_preediting())
2951 {
2952 int cmdpos = 0;
2953 int cmdspos;
2954 int old_row;
2955 int old_col;
2956 colnr_T col;
2957
2958 old_row = msg_row;
2959 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002960 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961
2962# ifdef FEAT_MBYTE
2963 if (has_mbyte)
2964 {
2965 for (col = 0; col < preedit_start_col
2966 && cmdpos < ccline.cmdlen; ++col)
2967 {
2968 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002969 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 }
2971 }
2972 else
2973# endif
2974 {
2975 cmdspos += preedit_start_col;
2976 cmdpos += preedit_start_col;
2977 }
2978
2979 msg_row = cmdline_row + (cmdspos / (int)Columns);
2980 msg_col = cmdspos % (int)Columns;
2981 if (msg_row >= Rows)
2982 msg_row = Rows - 1;
2983
2984 for (col = 0; cmdpos < ccline.cmdlen; ++col)
2985 {
2986 int char_len;
2987 int char_attr;
2988
2989 char_attr = im_get_feedback_attr(col);
2990 if (char_attr < 0)
2991 break; /* end of preedit string */
2992
2993# ifdef FEAT_MBYTE
2994 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002995 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 else
2997# endif
2998 char_len = 1;
2999
3000 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3001 cmdpos += char_len;
3002 }
3003
3004 msg_row = old_row;
3005 msg_col = old_col;
3006 }
3007}
3008#endif /* FEAT_XIM && FEAT_GUI_GTK */
3009
3010/*
3011 * Allocate a new command line buffer.
3012 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3013 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3014 */
3015 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003016alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017{
3018 /*
3019 * give some extra space to avoid having to allocate all the time
3020 */
3021 if (len < 80)
3022 len = 100;
3023 else
3024 len += 20;
3025
3026 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3027 ccline.cmdbufflen = len;
3028}
3029
3030/*
3031 * Re-allocate the command line to length len + something extra.
3032 * return FAIL for failure, OK otherwise
3033 */
3034 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003035realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
3037 char_u *p;
3038
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003039 if (len < ccline.cmdbufflen)
3040 return OK; /* no need to resize */
3041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 p = ccline.cmdbuff;
3043 alloc_cmdbuff(len); /* will get some more */
3044 if (ccline.cmdbuff == NULL) /* out of memory */
3045 {
3046 ccline.cmdbuff = p; /* keep the old one */
3047 return FAIL;
3048 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003049 /* There isn't always a NUL after the command, but it may need to be
3050 * there, thus copy up to the NUL and add a NUL. */
3051 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3052 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003054
3055 if (ccline.xpc != NULL
3056 && ccline.xpc->xp_pattern != NULL
3057 && ccline.xpc->xp_context != EXPAND_NOTHING
3058 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3059 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003060 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003061
3062 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3063 * to point into the newly allocated memory. */
3064 if (i >= 0 && i <= ccline.cmdlen)
3065 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3066 }
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 return OK;
3069}
3070
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003071#if defined(FEAT_ARABIC) || defined(PROTO)
3072static char_u *arshape_buf = NULL;
3073
3074# if defined(EXITFREE) || defined(PROTO)
3075 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003077{
3078 vim_free(arshape_buf);
3079}
3080# endif
3081#endif
3082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083/*
3084 * Draw part of the cmdline at the current cursor position. But draw stars
3085 * when cmdline_star is TRUE.
3086 */
3087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3091 int i;
3092
3093 if (cmdline_star > 0)
3094 for (i = 0; i < len; ++i)
3095 {
3096 msg_putchar('*');
3097# ifdef FEAT_MBYTE
3098 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003099 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100# endif
3101 }
3102 else
3103#endif
3104#ifdef FEAT_ARABIC
3105 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3106 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 static int buflen = 0;
3108 char_u *p;
3109 int j;
3110 int newlen = 0;
3111 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003112 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 int prev_c = 0;
3114 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003115 int u8c;
3116 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118
3119 /*
3120 * Do arabic shaping into a temporary buffer. This is very
3121 * inefficient!
3122 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003123 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 {
3125 /* Re-allocate the buffer. We keep it around to avoid a lot of
3126 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003127 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003128 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003129 arshape_buf = alloc(buflen);
3130 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 return; /* out of memory */
3132 }
3133
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003134 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3135 {
3136 /* Prepend a space to draw the leading composing char on. */
3137 arshape_buf[0] = ' ';
3138 newlen = 1;
3139 }
3140
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 for (j = start; j < start + len; j += mb_l)
3142 {
3143 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003144 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003145 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 if (ARABIC_CHAR(u8c))
3147 {
3148 /* Do Arabic shaping. */
3149 if (cmdmsg_rl)
3150 {
3151 /* displaying from right to left */
3152 pc = prev_c;
3153 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003154 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 if (j + mb_l >= start + len)
3156 nc = NUL;
3157 else
3158 nc = utf_ptr2char(p + mb_l);
3159 }
3160 else
3161 {
3162 /* displaying from left to right */
3163 if (j + mb_l >= start + len)
3164 pc = NUL;
3165 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003166 {
3167 int pcc[MAX_MCO];
3168
3169 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003171 pc1 = pcc[0];
3172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 nc = prev_c;
3174 }
3175 prev_c = u8c;
3176
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003177 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003179 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003182 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3183 if (u8cc[1] != 0)
3184 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003185 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 }
3187 }
3188 else
3189 {
3190 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003191 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 newlen += mb_l;
3193 }
3194 }
3195
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003196 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 }
3198 else
3199#endif
3200 msg_outtrans_len(ccline.cmdbuff + start, len);
3201}
3202
3203/*
3204 * Put a character on the command line. Shifts the following text to the
3205 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3206 * "c" must be printable (fit in one display cell)!
3207 */
3208 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003209putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 if (cmd_silent)
3212 return;
3213 msg_no_more = TRUE;
3214 msg_putchar(c);
3215 if (shift)
3216 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3217 msg_no_more = FALSE;
3218 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003219 extra_char = c;
3220 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221}
3222
3223/*
3224 * Undo a putcmdline(c, FALSE).
3225 */
3226 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003227unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 if (cmd_silent)
3230 return;
3231 msg_no_more = TRUE;
3232 if (ccline.cmdlen == ccline.cmdpos)
3233 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003234#ifdef FEAT_MBYTE
3235 else if (has_mbyte)
3236 draw_cmdline(ccline.cmdpos,
3237 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 else
3240 draw_cmdline(ccline.cmdpos, 1);
3241 msg_no_more = FALSE;
3242 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003243 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
3247 * Put the given string, of the given length, onto the command line.
3248 * If len is -1, then STRLEN() is used to calculate the length.
3249 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3250 * part will be redrawn, otherwise it will not. If this function is called
3251 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3252 * called afterwards.
3253 */
3254 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003255put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 int retval;
3258 int i;
3259 int m;
3260 int c;
3261
3262 if (len < 0)
3263 len = (int)STRLEN(str);
3264
3265 /* Check if ccline.cmdbuff needs to be longer */
3266 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003267 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 else
3269 retval = OK;
3270 if (retval == OK)
3271 {
3272 if (!ccline.overstrike)
3273 {
3274 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3275 ccline.cmdbuff + ccline.cmdpos,
3276 (size_t)(ccline.cmdlen - ccline.cmdpos));
3277 ccline.cmdlen += len;
3278 }
3279 else
3280 {
3281#ifdef FEAT_MBYTE
3282 if (has_mbyte)
3283 {
3284 /* Count nr of characters in the new string. */
3285 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003286 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 ++m;
3288 /* Count nr of bytes in cmdline that are overwritten by these
3289 * characters. */
3290 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003291 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 --m;
3293 if (i < ccline.cmdlen)
3294 {
3295 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3296 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3297 ccline.cmdlen += ccline.cmdpos + len - i;
3298 }
3299 else
3300 ccline.cmdlen = ccline.cmdpos + len;
3301 }
3302 else
3303#endif
3304 if (ccline.cmdpos + len > ccline.cmdlen)
3305 ccline.cmdlen = ccline.cmdpos + len;
3306 }
3307 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3308 ccline.cmdbuff[ccline.cmdlen] = NUL;
3309
3310#ifdef FEAT_MBYTE
3311 if (enc_utf8)
3312 {
3313 /* When the inserted text starts with a composing character,
3314 * backup to the character before it. There could be two of them.
3315 */
3316 i = 0;
3317 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3318 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3319 {
3320 i = (*mb_head_off)(ccline.cmdbuff,
3321 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3322 ccline.cmdpos -= i;
3323 len += i;
3324 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3325 }
3326# ifdef FEAT_ARABIC
3327 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3328 {
3329 /* Check the previous character for Arabic combining pair. */
3330 i = (*mb_head_off)(ccline.cmdbuff,
3331 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3332 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3333 + ccline.cmdpos - i), c))
3334 {
3335 ccline.cmdpos -= i;
3336 len += i;
3337 }
3338 else
3339 i = 0;
3340 }
3341# endif
3342 if (i != 0)
3343 {
3344 /* Also backup the cursor position. */
3345 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3346 ccline.cmdspos -= i;
3347 msg_col -= i;
3348 if (msg_col < 0)
3349 {
3350 msg_col += Columns;
3351 --msg_row;
3352 }
3353 }
3354 }
3355#endif
3356
3357 if (redraw && !cmd_silent)
3358 {
3359 msg_no_more = TRUE;
3360 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003361 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3363 /* Avoid clearing the rest of the line too often. */
3364 if (cmdline_row != i || ccline.overstrike)
3365 msg_clr_eos();
3366 msg_no_more = FALSE;
3367 }
3368#ifdef FEAT_FKMAP
3369 /*
3370 * If we are in Farsi command mode, the character input must be in
3371 * Insert mode. So do not advance the cmdpos.
3372 */
3373 if (!cmd_fkmap)
3374#endif
3375 {
3376 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003379 if (m < 0) /* overflow, Columns or Rows at weird value */
3380 m = MAXCOL;
3381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 else
3383 m = MAXCOL;
3384 for (i = 0; i < len; ++i)
3385 {
3386 c = cmdline_charsize(ccline.cmdpos);
3387#ifdef FEAT_MBYTE
3388 /* count ">" for a double-wide char that doesn't fit. */
3389 if (has_mbyte)
3390 correct_cmdspos(ccline.cmdpos, c);
3391#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003392 /* Stop cursor at the end of the screen, but do increment the
3393 * insert position, so that entering a very long command
3394 * works, even though you can't see it. */
3395 if (ccline.cmdspos + c < m)
3396 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397#ifdef FEAT_MBYTE
3398 if (has_mbyte)
3399 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003400 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 if (c > len - i - 1)
3402 c = len - i - 1;
3403 ccline.cmdpos += c;
3404 i += c;
3405 }
3406#endif
3407 ++ccline.cmdpos;
3408 }
3409 }
3410 }
3411 if (redraw)
3412 msg_check();
3413 return retval;
3414}
3415
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003416static struct cmdline_info prev_ccline;
3417static int prev_ccline_used = FALSE;
3418
3419/*
3420 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3421 * and overwrite it. But get_cmdline_str() may need it, thus make it
3422 * available globally in prev_ccline.
3423 */
3424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003425save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003426{
3427 if (!prev_ccline_used)
3428 {
3429 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3430 prev_ccline_used = TRUE;
3431 }
3432 *ccp = prev_ccline;
3433 prev_ccline = ccline;
3434 ccline.cmdbuff = NULL;
3435 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003436 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003437}
3438
3439/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003440 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003441 */
3442 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003443restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003444{
3445 ccline = prev_ccline;
3446 prev_ccline = *ccp;
3447}
3448
Bram Moolenaar5a305422006-04-28 22:38:25 +00003449#if defined(FEAT_EVAL) || defined(PROTO)
3450/*
3451 * Save the command line into allocated memory. Returns a pointer to be
3452 * passed to restore_cmdline_alloc() later.
3453 * Returns NULL when failed.
3454 */
3455 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003456save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003457{
3458 struct cmdline_info *p;
3459
3460 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3461 if (p != NULL)
3462 save_cmdline(p);
3463 return (char_u *)p;
3464}
3465
3466/*
3467 * Restore the command line from the return value of save_cmdline_alloc().
3468 */
3469 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003470restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003471{
3472 if (p != NULL)
3473 {
3474 restore_cmdline((struct cmdline_info *)p);
3475 vim_free(p);
3476 }
3477}
3478#endif
3479
Bram Moolenaar8299df92004-07-10 09:47:34 +00003480/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003481 * Paste a yank register into the command line.
3482 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003483 * insert_reg() can't be used here, because special characters from the
3484 * register contents will be interpreted as commands.
3485 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003486 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003487 */
3488 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003489cmdline_paste(
3490 int regname,
3491 int literally, /* Insert text literally instead of "as typed" */
3492 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003493{
3494 long i;
3495 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003496 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497 int allocated;
3498 struct cmdline_info save_ccline;
3499
3500 /* check for valid regname; also accept special characters for CTRL-R in
3501 * the command line */
3502 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003503 && regname != Ctrl_A && regname != Ctrl_L
3504 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003505 return FAIL;
3506
3507 /* A register containing CTRL-R can cause an endless loop. Allow using
3508 * CTRL-C to break the loop. */
3509 line_breakcheck();
3510 if (got_int)
3511 return FAIL;
3512
3513#ifdef FEAT_CLIPBOARD
3514 regname = may_get_selection(regname);
3515#endif
3516
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003517 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003518 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003519 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003520 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003521 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003522 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003523 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003524
3525 if (i)
3526 {
3527 /* Got the value of a special register in "arg". */
3528 if (arg == NULL)
3529 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003530
3531 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3532 * part of the word. */
3533 p = arg;
3534 if (p_is && regname == Ctrl_W)
3535 {
3536 char_u *w;
3537 int len;
3538
3539 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003540 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003541 {
3542#ifdef FEAT_MBYTE
3543 if (has_mbyte)
3544 {
3545 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3546 if (!vim_iswordc(mb_ptr2char(w - len)))
3547 break;
3548 w -= len;
3549 }
3550 else
3551#endif
3552 {
3553 if (!vim_iswordc(w[-1]))
3554 break;
3555 --w;
3556 }
3557 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003558 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003559 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3560 p += len;
3561 }
3562
3563 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003564 if (allocated)
3565 vim_free(arg);
3566 return OK;
3567 }
3568
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003569 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003570}
3571
3572/*
3573 * Put a string on the command line.
3574 * When "literally" is TRUE, insert literally.
3575 * When "literally" is FALSE, insert as typed, but don't leave the command
3576 * line.
3577 */
3578 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003579cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003580{
3581 int c, cv;
3582
3583 if (literally)
3584 put_on_cmdline(s, -1, TRUE);
3585 else
3586 while (*s != NUL)
3587 {
3588 cv = *s;
3589 if (cv == Ctrl_V && s[1])
3590 ++s;
3591#ifdef FEAT_MBYTE
3592 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003593 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003594 else
3595#endif
3596 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003597 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3598 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003599#ifdef UNIX
3600 || c == intr_char
3601#endif
3602 || (c == Ctrl_BSL && *s == Ctrl_N))
3603 stuffcharReadbuff(Ctrl_V);
3604 stuffcharReadbuff(c);
3605 }
3606}
3607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#ifdef FEAT_WILDMENU
3609/*
3610 * Delete characters on the command line, from "from" to the current
3611 * position.
3612 */
3613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003614cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615{
3616 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3617 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3618 ccline.cmdlen -= ccline.cmdpos - from;
3619 ccline.cmdpos = from;
3620}
3621#endif
3622
3623/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003624 * This function is called when the screen size changes and with incremental
3625 * search and in other situations where the command line may have been
3626 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 */
3628 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003629redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003631 redrawcmdline_ex(TRUE);
3632}
3633
3634 void
3635redrawcmdline_ex(int do_compute_cmdrow)
3636{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 if (cmd_silent)
3638 return;
3639 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003640 if (do_compute_cmdrow)
3641 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 redrawcmd();
3643 cursorcmd();
3644}
3645
3646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003647redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648{
3649 int i;
3650
3651 if (cmd_silent)
3652 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003653 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 msg_putchar(ccline.cmdfirstc);
3655 if (ccline.cmdprompt != NULL)
3656 {
3657 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3658 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3659 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003660 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 --ccline.cmdindent;
3662 }
3663 else
3664 for (i = ccline.cmdindent; i > 0; --i)
3665 msg_putchar(' ');
3666}
3667
3668/*
3669 * Redraw what is currently on the command line.
3670 */
3671 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003672redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673{
3674 if (cmd_silent)
3675 return;
3676
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003677 /* when 'incsearch' is set there may be no command line while redrawing */
3678 if (ccline.cmdbuff == NULL)
3679 {
3680 windgoto(cmdline_row, 0);
3681 msg_clr_eos();
3682 return;
3683 }
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 msg_start();
3686 redrawcmdprompt();
3687
3688 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3689 msg_no_more = TRUE;
3690 draw_cmdline(0, ccline.cmdlen);
3691 msg_clr_eos();
3692 msg_no_more = FALSE;
3693
3694 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003695 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003696 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697
3698 /*
3699 * An emsg() before may have set msg_scroll. This is used in normal mode,
3700 * in cmdline mode we can reset them now.
3701 */
3702 msg_scroll = FALSE; /* next message overwrites cmdline */
3703
3704 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3705 * in cmdline mode */
3706 skip_redraw = FALSE;
3707}
3708
3709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003710compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003712 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 cmdline_row = Rows - 1;
3714 else
3715 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003716 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717}
3718
3719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003720cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 if (cmd_silent)
3723 return;
3724
3725#ifdef FEAT_RIGHTLEFT
3726 if (cmdmsg_rl)
3727 {
3728 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3729 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3730 if (msg_row <= 0)
3731 msg_row = Rows - 1;
3732 }
3733 else
3734#endif
3735 {
3736 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3737 msg_col = ccline.cmdspos % (int)Columns;
3738 if (msg_row >= Rows)
3739 msg_row = Rows - 1;
3740 }
3741
3742 windgoto(msg_row, msg_col);
3743#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003744 if (p_imst == IM_ON_THE_SPOT)
3745 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746#endif
3747#ifdef MCH_CURSOR_SHAPE
3748 mch_update_cursor();
3749#endif
3750}
3751
3752 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003753gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754{
3755 msg_start();
3756#ifdef FEAT_RIGHTLEFT
3757 if (cmdmsg_rl)
3758 msg_col = Columns - 1;
3759 else
3760#endif
3761 msg_col = 0; /* always start in column 0 */
3762 if (clr) /* clear the bottom line(s) */
3763 msg_clr_eos(); /* will reset clear_cmdline */
3764 windgoto(cmdline_row, 0);
3765}
3766
3767/*
3768 * Check the word in front of the cursor for an abbreviation.
3769 * Called when the non-id character "c" has been entered.
3770 * When an abbreviation is recognized it is removed from the text with
3771 * backspaces and the replacement string is inserted, followed by "c".
3772 */
3773 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003774ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003776 int spos = 0;
3777
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3779 return FALSE;
3780
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003781 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3782 * Actually accepts any mark. */
3783 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3784 spos++;
3785 if (ccline.cmdlen - spos > 5
3786 && ccline.cmdbuff[spos] == '\''
3787 && ccline.cmdbuff[spos + 2] == ','
3788 && ccline.cmdbuff[spos + 3] == '\'')
3789 spos += 5;
3790 else
3791 /* check abbreviation from the beginning of the commandline */
3792 spos = 0;
3793
3794 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795}
3796
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003797#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3798 static int
3799#ifdef __BORLANDC__
3800_RTLENTRYF
3801#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003802sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003803{
3804 char_u *p1 = *(char_u **)s1;
3805 char_u *p2 = *(char_u **)s2;
3806
3807 if (*p1 != '<' && *p2 == '<') return -1;
3808 if (*p1 == '<' && *p2 != '<') return 1;
3809 return STRCMP(p1, p2);
3810}
3811#endif
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813/*
3814 * Return FAIL if this is not an appropriate context in which to do
3815 * completion of anything, return OK if it is (even if there are no matches).
3816 * For the caller, this means that the character is just passed through like a
3817 * normal character (instead of being expanded). This allows :s/^I^D etc.
3818 */
3819 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003820nextwild(
3821 expand_T *xp,
3822 int type,
3823 int options, /* extra options for ExpandOne() */
3824 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 int i, j;
3827 char_u *p1;
3828 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int difflen;
3830 int v;
3831
3832 if (xp->xp_numfiles == -1)
3833 {
3834 set_expand_context(xp);
3835 cmd_showtail = expand_showtail(xp);
3836 }
3837
3838 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3839 {
3840 beep_flush();
3841 return OK; /* Something illegal on command line */
3842 }
3843 if (xp->xp_context == EXPAND_NOTHING)
3844 {
3845 /* Caller can use the character as a normal char instead */
3846 return FAIL;
3847 }
3848
3849 MSG_PUTS("..."); /* show that we are busy */
3850 out_flush();
3851
3852 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003853 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if (type == WILD_NEXT || type == WILD_PREV)
3856 {
3857 /*
3858 * Get next/previous match for a previous expanded pattern.
3859 */
3860 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3861 }
3862 else
3863 {
3864 /*
3865 * Translate string into pattern and expand it.
3866 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003867 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3868 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 p2 = NULL;
3870 else
3871 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003872 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003873 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3874 if (escape)
3875 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003876
3877 if (p_wic)
3878 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003879 p2 = ExpandOne(xp, p1,
3880 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003881 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003883 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (p2 != NULL && type == WILD_LONGEST)
3885 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003886 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (ccline.cmdbuff[i + j] == '*'
3888 || ccline.cmdbuff[i + j] == '?')
3889 break;
3890 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003891 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894 }
3895
3896 if (p2 != NULL && !got_int)
3897 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003898 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003899 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003901 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 xp->xp_pattern = ccline.cmdbuff + i;
3903 }
3904 else
3905 v = OK;
3906 if (v == OK)
3907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3909 &ccline.cmdbuff[ccline.cmdpos],
3910 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3911 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 ccline.cmdlen += difflen;
3913 ccline.cmdpos += difflen;
3914 }
3915 }
3916 vim_free(p2);
3917
3918 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003919 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 /* When expanding a ":map" command and no matches are found, assume that
3922 * the key is supposed to be inserted literally */
3923 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3924 return FAIL;
3925
3926 if (xp->xp_numfiles <= 0 && p2 == NULL)
3927 beep_flush();
3928 else if (xp->xp_numfiles == 1)
3929 /* free expanded pattern */
3930 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3931
3932 return OK;
3933}
3934
3935/*
3936 * Do wildcard expansion on the string 'str'.
3937 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003938 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 * Return NULL for failure.
3940 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003941 * "orig" is the originally expanded string, copied to allocated memory. It
3942 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3943 * WILD_PREV "orig" should be NULL.
3944 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003945 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3946 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 *
3948 * mode = WILD_FREE: just free previously expanded matches
3949 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3950 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3951 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3952 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3953 * mode = WILD_ALL: return all matches concatenated
3954 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003955 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 *
3957 * options = WILD_LIST_NOTFOUND: list entries without a match
3958 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3959 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3960 * options = WILD_NO_BEEP: Don't beep for multiple matches
3961 * options = WILD_ADD_SLASH: add a slash after directory names
3962 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3963 * options = WILD_SILENT: don't print warning messages
3964 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003965 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 *
3967 * The variables xp->xp_context and xp->xp_backslash must have been set!
3968 */
3969 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003970ExpandOne(
3971 expand_T *xp,
3972 char_u *str,
3973 char_u *orig, /* allocated copy of original of expanded string */
3974 int options,
3975 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977 char_u *ss = NULL;
3978 static int findex;
3979 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003980 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 int i;
3982 long_u len;
3983 int non_suf_match; /* number without matching suffix */
3984
3985 /*
3986 * first handle the case of using an old match
3987 */
3988 if (mode == WILD_NEXT || mode == WILD_PREV)
3989 {
3990 if (xp->xp_numfiles > 0)
3991 {
3992 if (mode == WILD_PREV)
3993 {
3994 if (findex == -1)
3995 findex = xp->xp_numfiles;
3996 --findex;
3997 }
3998 else /* mode == WILD_NEXT */
3999 ++findex;
4000
4001 /*
4002 * When wrapping around, return the original string, set findex to
4003 * -1.
4004 */
4005 if (findex < 0)
4006 {
4007 if (orig_save == NULL)
4008 findex = xp->xp_numfiles - 1;
4009 else
4010 findex = -1;
4011 }
4012 if (findex >= xp->xp_numfiles)
4013 {
4014 if (orig_save == NULL)
4015 findex = 0;
4016 else
4017 findex = -1;
4018 }
4019#ifdef FEAT_WILDMENU
4020 if (p_wmnu)
4021 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4022 findex, cmd_showtail);
4023#endif
4024 if (findex == -1)
4025 return vim_strsave(orig_save);
4026 return vim_strsave(xp->xp_files[findex]);
4027 }
4028 else
4029 return NULL;
4030 }
4031
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004032 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4034 {
4035 FreeWild(xp->xp_numfiles, xp->xp_files);
4036 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004037 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 findex = 0;
4040
4041 if (mode == WILD_FREE) /* only release file name */
4042 return NULL;
4043
4044 if (xp->xp_numfiles == -1)
4045 {
4046 vim_free(orig_save);
4047 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004048 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 /*
4051 * Do the expansion.
4052 */
4053 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4054 options) == FAIL)
4055 {
4056#ifdef FNAME_ILLEGAL
4057 /* Illegal file name has been silently skipped. But when there
4058 * are wildcards, the real problem is that there was no match,
4059 * causing the pattern to be added, which has illegal characters.
4060 */
4061 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4062 EMSG2(_(e_nomatch2), str);
4063#endif
4064 }
4065 else if (xp->xp_numfiles == 0)
4066 {
4067 if (!(options & WILD_SILENT))
4068 EMSG2(_(e_nomatch2), str);
4069 }
4070 else
4071 {
4072 /* Escape the matches for use on the command line. */
4073 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4074
4075 /*
4076 * Check for matching suffixes in file names.
4077 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004078 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4079 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 if (xp->xp_numfiles)
4082 non_suf_match = xp->xp_numfiles;
4083 else
4084 non_suf_match = 1;
4085 if ((xp->xp_context == EXPAND_FILES
4086 || xp->xp_context == EXPAND_DIRECTORIES)
4087 && xp->xp_numfiles > 1)
4088 {
4089 /*
4090 * More than one match; check suffix.
4091 * The files will have been sorted on matching suffix in
4092 * expand_wildcards, only need to check the first two.
4093 */
4094 non_suf_match = 0;
4095 for (i = 0; i < 2; ++i)
4096 if (match_suffix(xp->xp_files[i]))
4097 ++non_suf_match;
4098 }
4099 if (non_suf_match != 1)
4100 {
4101 /* Can we ever get here unless it's while expanding
4102 * interactively? If not, we can get rid of this all
4103 * together. Don't really want to wait for this message
4104 * (and possibly have to hit return to continue!).
4105 */
4106 if (!(options & WILD_SILENT))
4107 EMSG(_(e_toomany));
4108 else if (!(options & WILD_NO_BEEP))
4109 beep_flush();
4110 }
4111 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4112 ss = vim_strsave(xp->xp_files[0]);
4113 }
4114 }
4115 }
4116
4117 /* Find longest common part */
4118 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4119 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004120 int mb_len = 1;
4121 int c0, ci;
4122
4123 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004125#ifdef FEAT_MBYTE
4126 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004128 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4129 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4130 }
4131 else
4132#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004133 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004134 for (i = 1; i < xp->xp_numfiles; ++i)
4135 {
4136#ifdef FEAT_MBYTE
4137 if (has_mbyte)
4138 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4139 else
4140#endif
4141 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004142 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004144 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004145 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004147 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 break;
4149 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004150 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 break;
4152 }
4153 if (i < xp->xp_numfiles)
4154 {
4155 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004156 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 break;
4158 }
4159 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004160
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 ss = alloc((unsigned)len + 1);
4162 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004163 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 findex = -1; /* next p_wc gets first one */
4165 }
4166
4167 /* Concatenate all matching names */
4168 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4169 {
4170 len = 0;
4171 for (i = 0; i < xp->xp_numfiles; ++i)
4172 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4173 ss = lalloc(len, TRUE);
4174 if (ss != NULL)
4175 {
4176 *ss = NUL;
4177 for (i = 0; i < xp->xp_numfiles; ++i)
4178 {
4179 STRCAT(ss, xp->xp_files[i]);
4180 if (i != xp->xp_numfiles - 1)
4181 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4182 }
4183 }
4184 }
4185
4186 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4187 ExpandCleanup(xp);
4188
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004189 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004190 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004191 vim_free(orig);
4192
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 return ss;
4194}
4195
4196/*
4197 * Prepare an expand structure for use.
4198 */
4199 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004200ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004202 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004203 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004205#ifndef BACKSLASH_IN_FILENAME
4206 xp->xp_shell = FALSE;
4207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 xp->xp_numfiles = -1;
4209 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004210#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4211 xp->xp_arg = NULL;
4212#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004213 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214}
4215
4216/*
4217 * Cleanup an expand structure after use.
4218 */
4219 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004220ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221{
4222 if (xp->xp_numfiles >= 0)
4223 {
4224 FreeWild(xp->xp_numfiles, xp->xp_files);
4225 xp->xp_numfiles = -1;
4226 }
4227}
4228
4229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004230ExpandEscape(
4231 expand_T *xp,
4232 char_u *str,
4233 int numfiles,
4234 char_u **files,
4235 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236{
4237 int i;
4238 char_u *p;
4239
4240 /*
4241 * May change home directory back to "~"
4242 */
4243 if (options & WILD_HOME_REPLACE)
4244 tilde_replace(str, numfiles, files);
4245
4246 if (options & WILD_ESCAPE)
4247 {
4248 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004249 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004250 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 || xp->xp_context == EXPAND_BUFFERS
4252 || xp->xp_context == EXPAND_DIRECTORIES)
4253 {
4254 /*
4255 * Insert a backslash into a file name before a space, \, %, #
4256 * and wildmatch characters, except '~'.
4257 */
4258 for (i = 0; i < numfiles; ++i)
4259 {
4260 /* for ":set path=" we need to escape spaces twice */
4261 if (xp->xp_backslash == XP_BS_THREE)
4262 {
4263 p = vim_strsave_escaped(files[i], (char_u *)" ");
4264 if (p != NULL)
4265 {
4266 vim_free(files[i]);
4267 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004268#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 p = vim_strsave_escaped(files[i], (char_u *)" ");
4270 if (p != NULL)
4271 {
4272 vim_free(files[i]);
4273 files[i] = p;
4274 }
4275#endif
4276 }
4277 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004278#ifdef BACKSLASH_IN_FILENAME
4279 p = vim_strsave_fnameescape(files[i], FALSE);
4280#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004281 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 if (p != NULL)
4284 {
4285 vim_free(files[i]);
4286 files[i] = p;
4287 }
4288
4289 /* If 'str' starts with "\~", replace "~" at start of
4290 * files[i] with "\~". */
4291 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004292 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004295
4296 /* If the first file starts with a '+' escape it. Otherwise it
4297 * could be seen as "+cmd". */
4298 if (*files[0] == '+')
4299 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 else if (xp->xp_context == EXPAND_TAGS)
4302 {
4303 /*
4304 * Insert a backslash before characters in a tag name that
4305 * would terminate the ":tag" command.
4306 */
4307 for (i = 0; i < numfiles; ++i)
4308 {
4309 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4310 if (p != NULL)
4311 {
4312 vim_free(files[i]);
4313 files[i] = p;
4314 }
4315 }
4316 }
4317 }
4318}
4319
4320/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004321 * Escape special characters in "fname" for when used as a file name argument
4322 * after a Vim command, or, when "shell" is non-zero, a shell command.
4323 * Returns the result in allocated memory.
4324 */
4325 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004326vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004327{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004328 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004329#ifdef BACKSLASH_IN_FILENAME
4330 char_u buf[20];
4331 int j = 0;
4332
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004333 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004334 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004335 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004336 buf[j++] = *p;
4337 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004338 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004339#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004340 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4341 if (shell && csh_like_shell() && p != NULL)
4342 {
4343 char_u *s;
4344
4345 /* For csh and similar shells need to put two backslashes before '!'.
4346 * One is taken by Vim, one by the shell. */
4347 s = vim_strsave_escaped(p, (char_u *)"!");
4348 vim_free(p);
4349 p = s;
4350 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004351#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004352
4353 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4354 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004355 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004356 escape_fname(&p);
4357
4358 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004359}
4360
4361/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004362 * Put a backslash before the file name in "pp", which is in allocated memory.
4363 */
4364 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004365escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004366{
4367 char_u *p;
4368
4369 p = alloc((unsigned)(STRLEN(*pp) + 2));
4370 if (p != NULL)
4371 {
4372 p[0] = '\\';
4373 STRCPY(p + 1, *pp);
4374 vim_free(*pp);
4375 *pp = p;
4376 }
4377}
4378
4379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * For each file name in files[num_files]:
4381 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4382 */
4383 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004384tilde_replace(
4385 char_u *orig_pat,
4386 int num_files,
4387 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 int i;
4390 char_u *p;
4391
4392 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4393 {
4394 for (i = 0; i < num_files; ++i)
4395 {
4396 p = home_replace_save(NULL, files[i]);
4397 if (p != NULL)
4398 {
4399 vim_free(files[i]);
4400 files[i] = p;
4401 }
4402 }
4403 }
4404}
4405
4406/*
4407 * Show all matches for completion on the command line.
4408 * Returns EXPAND_NOTHING when the character that triggered expansion should
4409 * be inserted like a normal character.
4410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004412showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413{
4414#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4415 int num_files;
4416 char_u **files_found;
4417 int i, j, k;
4418 int maxlen;
4419 int lines;
4420 int columns;
4421 char_u *p;
4422 int lastlen;
4423 int attr;
4424 int showtail;
4425
4426 if (xp->xp_numfiles == -1)
4427 {
4428 set_expand_context(xp);
4429 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4430 &num_files, &files_found);
4431 showtail = expand_showtail(xp);
4432 if (i != EXPAND_OK)
4433 return i;
4434
4435 }
4436 else
4437 {
4438 num_files = xp->xp_numfiles;
4439 files_found = xp->xp_files;
4440 showtail = cmd_showtail;
4441 }
4442
4443#ifdef FEAT_WILDMENU
4444 if (!wildmenu)
4445 {
4446#endif
4447 msg_didany = FALSE; /* lines_left will be set */
4448 msg_start(); /* prepare for paging */
4449 msg_putchar('\n');
4450 out_flush();
4451 cmdline_row = msg_row;
4452 msg_didany = FALSE; /* lines_left will be set again */
4453 msg_start(); /* prepare for paging */
4454#ifdef FEAT_WILDMENU
4455 }
4456#endif
4457
4458 if (got_int)
4459 got_int = FALSE; /* only int. the completion, not the cmd line */
4460#ifdef FEAT_WILDMENU
4461 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004462 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463#endif
4464 else
4465 {
4466 /* find the length of the longest file name */
4467 maxlen = 0;
4468 for (i = 0; i < num_files; ++i)
4469 {
4470 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004471 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 || xp->xp_context == EXPAND_BUFFERS))
4473 {
4474 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4475 j = vim_strsize(NameBuff);
4476 }
4477 else
4478 j = vim_strsize(L_SHOWFILE(i));
4479 if (j > maxlen)
4480 maxlen = j;
4481 }
4482
4483 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4484 lines = num_files;
4485 else
4486 {
4487 /* compute the number of columns and lines for the listing */
4488 maxlen += 2; /* two spaces between file names */
4489 columns = ((int)Columns + 2) / maxlen;
4490 if (columns < 1)
4491 columns = 1;
4492 lines = (num_files + columns - 1) / columns;
4493 }
4494
Bram Moolenaar8820b482017-03-16 17:23:31 +01004495 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496
4497 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4498 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004499 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 msg_clr_eos();
4501 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 }
4504
4505 /* list the files line by line */
4506 for (i = 0; i < lines; ++i)
4507 {
4508 lastlen = 999;
4509 for (k = i; k < num_files; k += lines)
4510 {
4511 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4512 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004513 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 p = files_found[k] + STRLEN(files_found[k]) + 1;
4515 msg_advance(maxlen + 1);
4516 msg_puts(p);
4517 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004518 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 break;
4520 }
4521 for (j = maxlen - lastlen; --j >= 0; )
4522 msg_putchar(' ');
4523 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004524 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 || xp->xp_context == EXPAND_BUFFERS)
4526 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004527 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004528 if (xp->xp_numfiles != -1)
4529 {
4530 char_u *halved_slash;
4531 char_u *exp_path;
4532
4533 /* Expansion was done before and special characters
4534 * were escaped, need to halve backslashes. Also
4535 * $HOME has been replaced with ~/. */
4536 exp_path = expand_env_save_opt(files_found[k], TRUE);
4537 halved_slash = backslash_halve_save(
4538 exp_path != NULL ? exp_path : files_found[k]);
4539 j = mch_isdir(halved_slash != NULL ? halved_slash
4540 : files_found[k]);
4541 vim_free(exp_path);
4542 vim_free(halved_slash);
4543 }
4544 else
4545 /* Expansion was done here, file names are literal. */
4546 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (showtail)
4548 p = L_SHOWFILE(k);
4549 else
4550 {
4551 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4552 TRUE);
4553 p = NameBuff;
4554 }
4555 }
4556 else
4557 {
4558 j = FALSE;
4559 p = L_SHOWFILE(k);
4560 }
4561 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4562 }
4563 if (msg_col > 0) /* when not wrapped around */
4564 {
4565 msg_clr_eos();
4566 msg_putchar('\n');
4567 }
4568 out_flush(); /* show one line at a time */
4569 if (got_int)
4570 {
4571 got_int = FALSE;
4572 break;
4573 }
4574 }
4575
4576 /*
4577 * we redraw the command below the lines that we have just listed
4578 * This is a bit tricky, but it saves a lot of screen updating.
4579 */
4580 cmdline_row = msg_row; /* will put it back later */
4581 }
4582
4583 if (xp->xp_numfiles == -1)
4584 FreeWild(num_files, files_found);
4585
4586 return EXPAND_OK;
4587}
4588
4589/*
4590 * Private gettail for showmatches() (and win_redr_status_matches()):
4591 * Find tail of file name path, but ignore trailing "/".
4592 */
4593 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004594sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595{
4596 char_u *p;
4597 char_u *t = s;
4598 int had_sep = FALSE;
4599
4600 for (p = s; *p != NUL; )
4601 {
4602 if (vim_ispathsep(*p)
4603#ifdef BACKSLASH_IN_FILENAME
4604 && !rem_backslash(p)
4605#endif
4606 )
4607 had_sep = TRUE;
4608 else if (had_sep)
4609 {
4610 t = p;
4611 had_sep = FALSE;
4612 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004613 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615 return t;
4616}
4617
4618/*
4619 * Return TRUE if we only need to show the tail of completion matches.
4620 * When not completing file names or there is a wildcard in the path FALSE is
4621 * returned.
4622 */
4623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004624expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
4626 char_u *s;
4627 char_u *end;
4628
4629 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 if (xp->xp_context != EXPAND_FILES
4631 && xp->xp_context != EXPAND_SHELLCMD
4632 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 return FALSE;
4634
4635 end = gettail(xp->xp_pattern);
4636 if (end == xp->xp_pattern) /* there is no path separator */
4637 return FALSE;
4638
4639 for (s = xp->xp_pattern; s < end; s++)
4640 {
4641 /* Skip escaped wildcards. Only when the backslash is not a path
4642 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4643 if (rem_backslash(s))
4644 ++s;
4645 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4646 return FALSE;
4647 }
4648 return TRUE;
4649}
4650
4651/*
4652 * Prepare a string for expansion.
4653 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004654 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 * When expanding other names: The string will be used with regcomp(). Copy
4656 * the name into allocated memory and prepend "^".
4657 */
4658 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004659addstar(
4660 char_u *fname,
4661 int len,
4662 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663{
4664 char_u *retval;
4665 int i, j;
4666 int new_len;
4667 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004668 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004670 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004671 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004672 && context != EXPAND_SHELLCMD
4673 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 {
4675 /*
4676 * Matching will be done internally (on something other than files).
4677 * So we convert the file-matching-type wildcards into our kind for
4678 * use with vim_regcomp(). First work out how long it will be:
4679 */
4680
4681 /* For help tags the translation is done in find_help_tags().
4682 * For a tag pattern starting with "/" no translation is needed. */
4683 if (context == EXPAND_HELP
4684 || context == EXPAND_COLORS
4685 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004686 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004687 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004688 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004689 || ((context == EXPAND_TAGS_LISTFILES
4690 || context == EXPAND_TAGS)
4691 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 retval = vim_strnsave(fname, len);
4693 else
4694 {
4695 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4696 for (i = 0; i < len; i++)
4697 {
4698 if (fname[i] == '*' || fname[i] == '~')
4699 new_len++; /* '*' needs to be replaced by ".*"
4700 '~' needs to be replaced by "\~" */
4701
4702 /* Buffer names are like file names. "." should be literal */
4703 if (context == EXPAND_BUFFERS && fname[i] == '.')
4704 new_len++; /* "." becomes "\." */
4705
4706 /* Custom expansion takes care of special things, match
4707 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004708 if ((context == EXPAND_USER_DEFINED
4709 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 new_len++; /* '\' becomes "\\" */
4711 }
4712 retval = alloc(new_len);
4713 if (retval != NULL)
4714 {
4715 retval[0] = '^';
4716 j = 1;
4717 for (i = 0; i < len; i++, j++)
4718 {
4719 /* Skip backslash. But why? At least keep it for custom
4720 * expansion. */
4721 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004722 && context != EXPAND_USER_LIST
4723 && fname[i] == '\\'
4724 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 break;
4726
4727 switch (fname[i])
4728 {
4729 case '*': retval[j++] = '.';
4730 break;
4731 case '~': retval[j++] = '\\';
4732 break;
4733 case '?': retval[j] = '.';
4734 continue;
4735 case '.': if (context == EXPAND_BUFFERS)
4736 retval[j++] = '\\';
4737 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004738 case '\\': if (context == EXPAND_USER_DEFINED
4739 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 retval[j++] = '\\';
4741 break;
4742 }
4743 retval[j] = fname[i];
4744 }
4745 retval[j] = NUL;
4746 }
4747 }
4748 }
4749 else
4750 {
4751 retval = alloc(len + 4);
4752 if (retval != NULL)
4753 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004754 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004757 * Don't add a star to *, ~, ~user, $var or `cmd`.
4758 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 * ~ would be at the start of the file name, but not the tail.
4760 * $ could be anywhere in the tail.
4761 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004762 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 */
4764 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004765 ends_in_star = (len > 0 && retval[len - 1] == '*');
4766#ifndef BACKSLASH_IN_FILENAME
4767 for (i = len - 2; i >= 0; --i)
4768 {
4769 if (retval[i] != '\\')
4770 break;
4771 ends_in_star = !ends_in_star;
4772 }
4773#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004775 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 && vim_strchr(tail, '$') == NULL
4777 && vim_strchr(retval, '`') == NULL)
4778 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004779 else if (len > 0 && retval[len - 1] == '$')
4780 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 retval[len] = NUL;
4782 }
4783 }
4784 return retval;
4785}
4786
4787/*
4788 * Must parse the command line so far to work out what context we are in.
4789 * Completion can then be done based on that context.
4790 * This routine sets the variables:
4791 * xp->xp_pattern The start of the pattern to be expanded within
4792 * the command line (ends at the cursor).
4793 * xp->xp_context The type of thing to expand. Will be one of:
4794 *
4795 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4796 * the command line, like an unknown command. Caller
4797 * should beep.
4798 * EXPAND_NOTHING Unrecognised context for completion, use char like
4799 * a normal char, rather than for completion. eg
4800 * :s/^I/
4801 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4802 * it.
4803 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4804 * EXPAND_FILES After command with XFILE set, or after setting
4805 * with P_EXPAND set. eg :e ^I, :w>>^I
4806 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4807 * when we know only directories are of interest. eg
4808 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004809 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4811 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4812 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4813 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4814 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4815 * EXPAND_EVENTS Complete event names
4816 * EXPAND_SYNTAX Complete :syntax command arguments
4817 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4818 * EXPAND_AUGROUP Complete autocommand group names
4819 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4820 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4821 * eg :unmap a^I , :cunab x^I
4822 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4823 * eg :call sub^I
4824 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4825 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4826 * names in expressions, eg :while s^I
4827 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004828 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 */
4830 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004831set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004833 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if (ccline.cmdfirstc != ':'
4835#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004836 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004837 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
4839 )
4840 {
4841 xp->xp_context = EXPAND_NOTHING;
4842 return;
4843 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004844 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845}
4846
4847 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004848set_cmd_context(
4849 expand_T *xp,
4850 char_u *str, /* start of command line */
4851 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004852 int col, /* position of cursor */
4853 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854{
4855 int old_char = NUL;
4856 char_u *nextcomm;
4857
4858 /*
4859 * Avoid a UMR warning from Purify, only save the character if it has been
4860 * written before.
4861 */
4862 if (col < len)
4863 old_char = str[col];
4864 str[col] = NUL;
4865 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004866
4867#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004868 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004869 {
4870# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004871 /* pass CMD_SIZE because there is no real command */
4872 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004873# endif
4874 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004875 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004876 {
4877 xp->xp_context = ccline.xp_context;
4878 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004879# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004880 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004881# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004882 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004883 else
4884#endif
4885 while (nextcomm != NULL)
4886 nextcomm = set_one_cmd_context(xp, nextcomm);
4887
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004888 /* Store the string here so that call_user_expand_func() can get to them
4889 * easily. */
4890 xp->xp_line = str;
4891 xp->xp_col = col;
4892
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 str[col] = old_char;
4894}
4895
4896/*
4897 * Expand the command line "str" from context "xp".
4898 * "xp" must have been set by set_cmd_context().
4899 * xp->xp_pattern points into "str", to where the text that is to be expanded
4900 * starts.
4901 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4902 * cursor.
4903 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4904 * key that triggered expansion literally.
4905 * Returns EXPAND_OK otherwise.
4906 */
4907 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004908expand_cmdline(
4909 expand_T *xp,
4910 char_u *str, /* start of command line */
4911 int col, /* position of cursor */
4912 int *matchcount, /* return: nr of matches */
4913 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004916 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
4918 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4919 {
4920 beep_flush();
4921 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4922 }
4923 if (xp->xp_context == EXPAND_NOTHING)
4924 {
4925 /* Caller can use the character as a normal char instead */
4926 return EXPAND_NOTHING;
4927 }
4928
4929 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004930 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4931 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (file_str == NULL)
4933 return EXPAND_UNSUCCESSFUL;
4934
Bram Moolenaar94950a92010-12-02 16:01:29 +01004935 if (p_wic)
4936 options += WILD_ICASE;
4937
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004939 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 *matchcount = 0;
4942 *matches = NULL;
4943 }
4944 vim_free(file_str);
4945
4946 return EXPAND_OK;
4947}
4948
4949#ifdef FEAT_MULTI_LANG
4950/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004951 * Cleanup matches for help tags:
4952 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4953 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01004955static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956
4957 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004958cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
4960 int i, j;
4961 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004962 char_u buf[4];
4963 char_u *p = buf;
4964
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004965 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004966 {
4967 *p++ = '@';
4968 *p++ = p_hlg[0];
4969 *p++ = p_hlg[1];
4970 }
4971 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 for (i = 0; i < num_file; ++i)
4974 {
4975 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004976 if (len <= 0)
4977 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004978 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 /* Sorting on priority means the same item in another language may
4981 * be anywhere. Search all items for a match up to the "@en". */
4982 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004983 if (j != i && (int)STRLEN(file[j]) == len + 3
4984 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 break;
4986 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004987 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 file[i][len] = NUL;
4989 }
4990 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004991
4992 if (*buf != NUL)
4993 for (i = 0; i < num_file; ++i)
4994 {
4995 len = (int)STRLEN(file[i]) - 3;
4996 if (len <= 0)
4997 continue;
4998 if (STRCMP(file[i] + len, buf) == 0)
4999 {
5000 /* remove the default language */
5001 file[i][len] = NUL;
5002 }
5003 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004}
5005#endif
5006
5007/*
5008 * Do the expansion based on xp->xp_context and "pat".
5009 */
5010 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005011ExpandFromContext(
5012 expand_T *xp,
5013 char_u *pat,
5014 int *num_file,
5015 char_u ***file,
5016 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017{
5018#ifdef FEAT_CMDL_COMPL
5019 regmatch_T regmatch;
5020#endif
5021 int ret;
5022 int flags;
5023
5024 flags = EW_DIR; /* include directories */
5025 if (options & WILD_LIST_NOTFOUND)
5026 flags |= EW_NOTFOUND;
5027 if (options & WILD_ADD_SLASH)
5028 flags |= EW_ADDSLASH;
5029 if (options & WILD_KEEP_ALL)
5030 flags |= EW_KEEPALL;
5031 if (options & WILD_SILENT)
5032 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005033 if (options & WILD_ALLLINKS)
5034 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005036 if (xp->xp_context == EXPAND_FILES
5037 || xp->xp_context == EXPAND_DIRECTORIES
5038 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
5040 /*
5041 * Expand file or directory names.
5042 */
5043 int free_pat = FALSE;
5044 int i;
5045
5046 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5047 * space */
5048 if (xp->xp_backslash != XP_BS_NONE)
5049 {
5050 free_pat = TRUE;
5051 pat = vim_strsave(pat);
5052 for (i = 0; pat[i]; ++i)
5053 if (pat[i] == '\\')
5054 {
5055 if (xp->xp_backslash == XP_BS_THREE
5056 && pat[i + 1] == '\\'
5057 && pat[i + 2] == '\\'
5058 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005059 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 if (xp->xp_backslash == XP_BS_ONE
5061 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005062 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 }
5064 }
5065
5066 if (xp->xp_context == EXPAND_FILES)
5067 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005068 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5069 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 else
5071 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005072 if (options & WILD_ICASE)
5073 flags |= EW_ICASE;
5074
Bram Moolenaard7834d32009-12-02 16:14:36 +00005075 /* Expand wildcards, supporting %:h and the like. */
5076 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (free_pat)
5078 vim_free(pat);
5079 return ret;
5080 }
5081
5082 *file = (char_u **)"";
5083 *num_file = 0;
5084 if (xp->xp_context == EXPAND_HELP)
5085 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005086 /* With an empty argument we would get all the help tags, which is
5087 * very slow. Get matches for "help" instead. */
5088 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5089 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091#ifdef FEAT_MULTI_LANG
5092 cleanup_help_tags(*num_file, *file);
5093#endif
5094 return OK;
5095 }
5096 return FAIL;
5097 }
5098
5099#ifndef FEAT_CMDL_COMPL
5100 return FAIL;
5101#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005102 if (xp->xp_context == EXPAND_SHELLCMD)
5103 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 if (xp->xp_context == EXPAND_OLD_SETTING)
5105 return ExpandOldSetting(num_file, file);
5106 if (xp->xp_context == EXPAND_BUFFERS)
5107 return ExpandBufnames(pat, num_file, file, options);
5108 if (xp->xp_context == EXPAND_TAGS
5109 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5110 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5111 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005112 {
5113 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005114 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5115 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005118 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005119 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005120 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005121 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005122 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005123 {
5124 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005125 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005126 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005127 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005128 {
5129 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005130 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005131 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005132# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5133 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005134 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005135# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005136 if (xp->xp_context == EXPAND_PACKADD)
5137 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138
5139 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5140 if (regmatch.regprog == NULL)
5141 return FAIL;
5142
5143 /* set ignore-case according to p_ic, p_scs and pat */
5144 regmatch.rm_ic = ignorecase(pat);
5145
5146 if (xp->xp_context == EXPAND_SETTINGS
5147 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5148 ret = ExpandSettings(xp, &regmatch, num_file, file);
5149 else if (xp->xp_context == EXPAND_MAPPINGS)
5150 ret = ExpandMappings(&regmatch, num_file, file);
5151# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5152 else if (xp->xp_context == EXPAND_USER_DEFINED)
5153 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5154# endif
5155 else
5156 {
5157 static struct expgen
5158 {
5159 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005160 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005162 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 } tab[] =
5164 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005165 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5166 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005167 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005168 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005169#ifdef FEAT_CMDHIST
5170 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005173 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005174 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005175 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5176 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5177 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#endif
5179#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005180 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5181 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5182 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5183 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#endif
5185#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005186 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5187 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#endif
5189#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005190 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005192#ifdef FEAT_PROFILE
5193 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5194#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005195 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005196 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5197 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005198#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005199 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005200#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005201#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005202 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005203#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005204#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005205 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005206#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5208 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005209 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5210 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005212 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005213 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005214 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 };
5216 int i;
5217
5218 /*
5219 * Find a context in the table and call the ExpandGeneric() with the
5220 * right function to do the expansion.
5221 */
5222 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005223 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (xp->xp_context == tab[i].context)
5225 {
5226 if (tab[i].ic)
5227 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005228 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005229 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 break;
5231 }
5232 }
5233
Bram Moolenaar473de612013-06-08 18:19:48 +02005234 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235
5236 return ret;
5237#endif /* FEAT_CMDL_COMPL */
5238}
5239
5240#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5241/*
5242 * Expand a list of names.
5243 *
5244 * Generic function for command line completion. It calls a function to
5245 * obtain strings, one by one. The strings are matched against a regexp
5246 * program. Matching strings are copied into an array, which is returned.
5247 *
5248 * Returns OK when no problems encountered, FAIL for error (out of memory).
5249 */
5250 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005251ExpandGeneric(
5252 expand_T *xp,
5253 regmatch_T *regmatch,
5254 int *num_file,
5255 char_u ***file,
5256 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005258 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 int i;
5261 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005262 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 char_u *str;
5264
5265 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005266 * round == 0: count the number of matching names
5267 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005269 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 for (i = 0; ; ++i)
5272 {
5273 str = (*func)(xp, i);
5274 if (str == NULL) /* end of list */
5275 break;
5276 if (*str == NUL) /* skip empty strings */
5277 continue;
5278
5279 if (vim_regexec(regmatch, str, (colnr_T)0))
5280 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005281 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005283 if (escaped)
5284 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5285 else
5286 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 (*file)[count] = str;
5288#ifdef FEAT_MENU
5289 if (func == get_menu_names && str != NULL)
5290 {
5291 /* test for separator added by get_menu_names() */
5292 str += STRLEN(str) - 1;
5293 if (*str == '\001')
5294 *str = '.';
5295 }
5296#endif
5297 }
5298 ++count;
5299 }
5300 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005301 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
5303 if (count == 0)
5304 return OK;
5305 *num_file = count;
5306 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5307 if (*file == NULL)
5308 {
5309 *file = (char_u **)"";
5310 return FAIL;
5311 }
5312 count = 0;
5313 }
5314 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005315
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005316 /* Sort the results. Keep menu's in the specified order. */
5317 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005318 {
5319 if (xp->xp_context == EXPAND_EXPRESSION
5320 || xp->xp_context == EXPAND_FUNCTIONS
5321 || xp->xp_context == EXPAND_USER_FUNC)
5322 /* <SNR> functions should be sorted to the end. */
5323 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5324 sort_func_compare);
5325 else
5326 sort_strings(*file, *num_file);
5327 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005328
Bram Moolenaar4f688582007-07-24 12:34:30 +00005329#ifdef FEAT_CMDL_COMPL
5330 /* Reset the variables used for special highlight names expansion, so that
5331 * they don't show up when getting normal highlight names by ID. */
5332 reset_expand_highlight();
5333#endif
5334
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 return OK;
5336}
5337
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005338/*
5339 * Complete a shell command.
5340 * Returns FAIL or OK;
5341 */
5342 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005343expand_shellcmd(
5344 char_u *filepat, /* pattern to match with command names */
5345 int *num_file, /* return: number of matches */
5346 char_u ***file, /* return: array with matches */
5347 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005348{
5349 char_u *pat;
5350 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005351 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005352 int mustfree = FALSE;
5353 garray_T ga;
5354 char_u *buf = alloc(MAXPATHL);
5355 size_t l;
5356 char_u *s, *e;
5357 int flags = flagsarg;
5358 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005359 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005360 hashtab_T found_ht;
5361 hashitem_T *hi;
5362 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005363
5364 if (buf == NULL)
5365 return FAIL;
5366
5367 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5368 * space */
5369 pat = vim_strsave(filepat);
5370 for (i = 0; pat[i]; ++i)
5371 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005372 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005373
Bram Moolenaarb5971142015-03-21 17:32:19 +01005374 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005375
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005376 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5377 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005378 path = (char_u *)".";
5379 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005380 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005381 /* For an absolute name we don't use $PATH. */
5382 if (!mch_isFullName(pat))
5383 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005384 if (path == NULL)
5385 path = (char_u *)"";
5386 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005387
5388 /*
5389 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005390 * collect them in "ga". When "." is not in $PATH also expand for the
5391 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005392 */
5393 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005394 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005395 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005396 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005397#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005398 e = vim_strchr(s, ';');
5399#else
5400 e = vim_strchr(s, ':');
5401#endif
5402 if (e == NULL)
5403 e = s + STRLEN(s);
5404
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005405 if (*s == NUL)
5406 {
5407 if (did_curdir)
5408 break;
5409 // Find directories in the current directory, path is empty.
5410 did_curdir = TRUE;
5411 flags |= EW_DIR;
5412 }
5413 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5414 {
5415 did_curdir = TRUE;
5416 flags |= EW_DIR;
5417 }
5418 else
5419 // Do not match directories inside a $PATH item.
5420 flags &= ~EW_DIR;
5421
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005422 l = e - s;
5423 if (l > MAXPATHL - 5)
5424 break;
5425 vim_strncpy(buf, s, l);
5426 add_pathsep(buf);
5427 l = STRLEN(buf);
5428 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5429
5430 /* Expand matches in one directory of $PATH. */
5431 ret = expand_wildcards(1, &buf, num_file, file, flags);
5432 if (ret == OK)
5433 {
5434 if (ga_grow(&ga, *num_file) == FAIL)
5435 FreeWild(*num_file, *file);
5436 else
5437 {
5438 for (i = 0; i < *num_file; ++i)
5439 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005440 char_u *name = (*file)[i];
5441
5442 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005443 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005444 // Check if this name was already found.
5445 hash = hash_hash(name + l);
5446 hi = hash_lookup(&found_ht, name + l, hash);
5447 if (HASHITEM_EMPTY(hi))
5448 {
5449 // Remove the path that was prepended.
5450 STRMOVE(name, name + l);
5451 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5452 hash_add_item(&found_ht, hi, name, hash);
5453 name = NULL;
5454 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005455 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005456 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005457 }
5458 vim_free(*file);
5459 }
5460 }
5461 if (*e != NUL)
5462 ++e;
5463 }
5464 *file = ga.ga_data;
5465 *num_file = ga.ga_len;
5466
5467 vim_free(buf);
5468 vim_free(pat);
5469 if (mustfree)
5470 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005471 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005472 return OK;
5473}
5474
5475
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5477/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005478 * Call "user_expand_func()" to invoke a user defined Vim script function and
5479 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005481 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005483 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005484 expand_T *xp,
5485 int *num_file,
5486 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005488 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005489 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005491 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005492 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005493 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494
Bram Moolenaarb7515462013-06-29 12:58:33 +02005495 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005496 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 *num_file = 0;
5498 *file = NULL;
5499
Bram Moolenaarb7515462013-06-29 12:58:33 +02005500 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005501 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005502 keep = ccline.cmdbuff[ccline.cmdlen];
5503 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005504 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005505
Bram Moolenaarffa96842018-06-12 22:05:14 +02005506 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5507
5508 args[0].v_type = VAR_STRING;
5509 args[0].vval.v_string = pat;
5510 args[1].v_type = VAR_STRING;
5511 args[1].vval.v_string = xp->xp_line;
5512 args[2].v_type = VAR_NUMBER;
5513 args[2].vval.v_number = xp->xp_col;
5514 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005516 /* Save the cmdline, we don't know what the function may do. */
5517 save_ccline = ccline;
5518 ccline.cmdbuff = NULL;
5519 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005521
Bram Moolenaarded27a12018-08-01 19:06:03 +02005522 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005523
5524 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005526 if (ccline.cmdbuff != NULL)
5527 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005528
Bram Moolenaarffa96842018-06-12 22:05:14 +02005529 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005530 return ret;
5531}
5532
5533/*
5534 * Expand names with a function defined by the user.
5535 */
5536 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005537ExpandUserDefined(
5538 expand_T *xp,
5539 regmatch_T *regmatch,
5540 int *num_file,
5541 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005542{
5543 char_u *retstr;
5544 char_u *s;
5545 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005546 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005547 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005548 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005549
5550 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5551 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 return FAIL;
5553
5554 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005555 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 {
5557 e = vim_strchr(s, '\n');
5558 if (e == NULL)
5559 e = s + STRLEN(s);
5560 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005561 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005563 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5564 *e = keep;
5565
5566 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005568 if (ga_grow(&ga, 1) == FAIL)
5569 break;
5570 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5571 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 if (*e != NUL)
5575 ++e;
5576 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005577 vim_free(retstr);
5578 *file = ga.ga_data;
5579 *num_file = ga.ga_len;
5580 return OK;
5581}
5582
5583/*
5584 * Expand names with a list returned by a function defined by the user.
5585 */
5586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005587ExpandUserList(
5588 expand_T *xp,
5589 int *num_file,
5590 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005591{
5592 list_T *retlist;
5593 listitem_T *li;
5594 garray_T ga;
5595
5596 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5597 if (retlist == NULL)
5598 return FAIL;
5599
5600 ga_init2(&ga, (int)sizeof(char *), 3);
5601 /* Loop over the items in the list. */
5602 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5603 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005604 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5605 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005606
5607 if (ga_grow(&ga, 1) == FAIL)
5608 break;
5609
5610 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005611 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005612 ++ga.ga_len;
5613 }
5614 list_unref(retlist);
5615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 *file = ga.ga_data;
5617 *num_file = ga.ga_len;
5618 return OK;
5619}
5620#endif
5621
5622/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005623 * Expand color scheme, compiler or filetype names.
5624 * Search from 'runtimepath':
5625 * 'runtimepath'/{dirnames}/{pat}.vim
5626 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5627 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5628 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5629 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005630 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 */
5632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005633ExpandRTDir(
5634 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005635 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005636 int *num_file,
5637 char_u ***file,
5638 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 char_u *s;
5641 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005642 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005644 int i;
5645 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
5647 *num_file = 0;
5648 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005649 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005650 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005652 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005654 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5655 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005657 ga_clear_strings(&ga);
5658 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005660 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005661 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005662 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005664
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005665 if (flags & DIP_START) {
5666 for (i = 0; dirnames[i] != NULL; ++i)
5667 {
5668 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5669 if (s == NULL)
5670 {
5671 ga_clear_strings(&ga);
5672 return FAIL;
5673 }
5674 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5675 globpath(p_pp, s, &ga, 0);
5676 vim_free(s);
5677 }
5678 }
5679
5680 if (flags & DIP_OPT) {
5681 for (i = 0; dirnames[i] != NULL; ++i)
5682 {
5683 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5684 if (s == NULL)
5685 {
5686 ga_clear_strings(&ga);
5687 return FAIL;
5688 }
5689 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5690 globpath(p_pp, s, &ga, 0);
5691 vim_free(s);
5692 }
5693 }
5694
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005695 for (i = 0; i < ga.ga_len; ++i)
5696 {
5697 match = ((char_u **)ga.ga_data)[i];
5698 s = match;
5699 e = s + STRLEN(s);
5700 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5701 {
5702 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005703 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005704 if (s < match || vim_ispathsep(*s))
5705 break;
5706 ++s;
5707 *e = NUL;
5708 mch_memmove(match, s, e - s + 1);
5709 }
5710 }
5711
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005712 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005713 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005714
5715 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005716 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005717 remove_duplicates(&ga);
5718
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 *file = ga.ga_data;
5720 *num_file = ga.ga_len;
5721 return OK;
5722}
5723
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005724/*
5725 * Expand loadplugin names:
5726 * 'packpath'/pack/ * /opt/{pat}
5727 */
5728 static int
5729ExpandPackAddDir(
5730 char_u *pat,
5731 int *num_file,
5732 char_u ***file)
5733{
5734 char_u *s;
5735 char_u *e;
5736 char_u *match;
5737 garray_T ga;
5738 int i;
5739 int pat_len;
5740
5741 *num_file = 0;
5742 *file = NULL;
5743 pat_len = (int)STRLEN(pat);
5744 ga_init2(&ga, (int)sizeof(char *), 10);
5745
5746 s = alloc((unsigned)(pat_len + 26));
5747 if (s == NULL)
5748 {
5749 ga_clear_strings(&ga);
5750 return FAIL;
5751 }
5752 sprintf((char *)s, "pack/*/opt/%s*", pat);
5753 globpath(p_pp, s, &ga, 0);
5754 vim_free(s);
5755
5756 for (i = 0; i < ga.ga_len; ++i)
5757 {
5758 match = ((char_u **)ga.ga_data)[i];
5759 s = gettail(match);
5760 e = s + STRLEN(s);
5761 mch_memmove(match, s, e - s + 1);
5762 }
5763
5764 if (ga.ga_len == 0)
5765 return FAIL;
5766
5767 /* Sort and remove duplicates which can happen when specifying multiple
5768 * directories in dirnames. */
5769 remove_duplicates(&ga);
5770
5771 *file = ga.ga_data;
5772 *num_file = ga.ga_len;
5773 return OK;
5774}
5775
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776#endif
5777
5778#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5779/*
5780 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005781 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005783 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005784globpath(
5785 char_u *path,
5786 char_u *file,
5787 garray_T *ga,
5788 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789{
5790 expand_T xpc;
5791 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 int num_p;
5794 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795
5796 buf = alloc(MAXPATHL);
5797 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005798 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005800 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005802
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 /* Loop over all entries in {path}. */
5804 while (*path != NUL)
5805 {
5806 /* Copy one item of the path to buf[] and concatenate the file name. */
5807 copy_option_part(&path, buf, MAXPATHL, ",");
5808 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5809 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005810# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005811 /* Using the platform's path separator (\) makes vim incorrectly
5812 * treat it as an escape character, use '/' instead. */
5813 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5814 STRCAT(buf, "/");
5815# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005817# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005819 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5820 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005822 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005824 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 for (i = 0; i < num_p; ++i)
5827 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005828 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005829 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005830 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005833
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 FreeWild(num_p, p);
5835 }
5836 }
5837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
5839 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840}
5841
5842#endif
5843
5844#if defined(FEAT_CMDHIST) || defined(PROTO)
5845
5846/*********************************
5847 * Command line history stuff *
5848 *********************************/
5849
5850/*
5851 * Translate a history character to the associated type number.
5852 */
5853 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005854hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855{
5856 if (c == ':')
5857 return HIST_CMD;
5858 if (c == '=')
5859 return HIST_EXPR;
5860 if (c == '@')
5861 return HIST_INPUT;
5862 if (c == '>')
5863 return HIST_DEBUG;
5864 return HIST_SEARCH; /* must be '?' or '/' */
5865}
5866
5867/*
5868 * Table of history names.
5869 * These names are used in :history and various hist...() functions.
5870 * It is sufficient to give the significant prefix of a history name.
5871 */
5872
5873static char *(history_names[]) =
5874{
5875 "cmd",
5876 "search",
5877 "expr",
5878 "input",
5879 "debug",
5880 NULL
5881};
5882
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005883#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5884/*
5885 * Function given to ExpandGeneric() to obtain the possible first
5886 * arguments of the ":history command.
5887 */
5888 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005889get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005890{
5891 static char_u compl[2] = { NUL, NUL };
5892 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005893 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005894 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5895
5896 if (idx < short_names_count)
5897 {
5898 compl[0] = (char_u)short_names[idx];
5899 return compl;
5900 }
5901 if (idx < short_names_count + history_name_count)
5902 return (char_u *)history_names[idx - short_names_count];
5903 if (idx == short_names_count + history_name_count)
5904 return (char_u *)"all";
5905 return NULL;
5906}
5907#endif
5908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909/*
5910 * init_history() - Initialize the command line history.
5911 * Also used to re-allocate the history when the size changes.
5912 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005913 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005914init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 int newlen; /* new length of history table */
5917 histentry_T *temp;
5918 int i;
5919 int j;
5920 int type;
5921
5922 /*
5923 * If size of history table changed, reallocate it
5924 */
5925 newlen = (int)p_hi;
5926 if (newlen != hislen) /* history length changed */
5927 {
5928 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5929 {
5930 if (newlen)
5931 {
5932 temp = (histentry_T *)lalloc(
5933 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5934 if (temp == NULL) /* out of memory! */
5935 {
5936 if (type == 0) /* first one: just keep the old length */
5937 {
5938 newlen = hislen;
5939 break;
5940 }
5941 /* Already changed one table, now we can only have zero
5942 * length for all tables. */
5943 newlen = 0;
5944 type = -1;
5945 continue;
5946 }
5947 }
5948 else
5949 temp = NULL;
5950 if (newlen == 0 || temp != NULL)
5951 {
5952 if (hisidx[type] < 0) /* there are no entries yet */
5953 {
5954 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005955 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 }
5957 else if (newlen > hislen) /* array becomes bigger */
5958 {
5959 for (i = 0; i <= hisidx[type]; ++i)
5960 temp[i] = history[type][i];
5961 j = i;
5962 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005963 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964 for ( ; j < hislen; ++i, ++j)
5965 temp[i] = history[type][j];
5966 }
5967 else /* array becomes smaller or 0 */
5968 {
5969 j = hisidx[type];
5970 for (i = newlen - 1; ; --i)
5971 {
5972 if (i >= 0) /* copy newest entries */
5973 temp[i] = history[type][j];
5974 else /* remove older entries */
5975 vim_free(history[type][j].hisstr);
5976 if (--j < 0)
5977 j = hislen - 1;
5978 if (j == hisidx[type])
5979 break;
5980 }
5981 hisidx[type] = newlen - 1;
5982 }
5983 vim_free(history[type]);
5984 history[type] = temp;
5985 }
5986 }
5987 hislen = newlen;
5988 }
5989}
5990
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005991 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005992clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005993{
5994 hisptr->hisnum = 0;
5995 hisptr->viminfo = FALSE;
5996 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005997 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005998}
5999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000/*
6001 * Check if command line 'str' is already in history.
6002 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6003 */
6004 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006005in_history(
6006 int type,
6007 char_u *str,
6008 int move_to_front, /* Move the entry to the front if it exists */
6009 int sep,
6010 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012 int i;
6013 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006014 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015
6016 if (hisidx[type] < 0)
6017 return FALSE;
6018 i = hisidx[type];
6019 do
6020 {
6021 if (history[type][i].hisstr == NULL)
6022 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006023
6024 /* For search history, check that the separator character matches as
6025 * well. */
6026 p = history[type][i].hisstr;
6027 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006028 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006029 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 {
6031 if (!move_to_front)
6032 return TRUE;
6033 last_i = i;
6034 break;
6035 }
6036 if (--i < 0)
6037 i = hislen - 1;
6038 } while (i != hisidx[type]);
6039
6040 if (last_i >= 0)
6041 {
6042 str = history[type][i].hisstr;
6043 while (i != hisidx[type])
6044 {
6045 if (++i >= hislen)
6046 i = 0;
6047 history[type][last_i] = history[type][i];
6048 last_i = i;
6049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006051 history[type][i].viminfo = FALSE;
6052 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006053 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 return TRUE;
6055 }
6056 return FALSE;
6057}
6058
6059/*
6060 * Convert history name (from table above) to its HIST_ equivalent.
6061 * When "name" is empty, return "cmd" history.
6062 * Returns -1 for unknown history name.
6063 */
6064 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006065get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066{
6067 int i;
6068 int len = (int)STRLEN(name);
6069
6070 /* No argument: use current history. */
6071 if (len == 0)
6072 return hist_char2type(ccline.cmdfirstc);
6073
6074 for (i = 0; history_names[i] != NULL; ++i)
6075 if (STRNICMP(name, history_names[i], len) == 0)
6076 return i;
6077
6078 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6079 return hist_char2type(name[0]);
6080
6081 return -1;
6082}
6083
6084static int last_maptick = -1; /* last seen maptick */
6085
6086/*
6087 * Add the given string to the given history. If the string is already in the
6088 * history then it is moved to the front. "histype" may be one of he HIST_
6089 * values.
6090 */
6091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006092add_to_history(
6093 int histype,
6094 char_u *new_entry,
6095 int in_map, /* consider maptick when inside a mapping */
6096 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097{
6098 histentry_T *hisptr;
6099 int len;
6100
6101 if (hislen == 0) /* no history */
6102 return;
6103
Bram Moolenaara939e432013-11-09 05:30:26 +01006104 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6105 return;
6106
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 /*
6108 * Searches inside the same mapping overwrite each other, so that only
6109 * the last line is kept. Be careful not to remove a line that was moved
6110 * down, only lines that were added.
6111 */
6112 if (histype == HIST_SEARCH && in_map)
6113 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006114 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 {
6116 /* Current line is from the same mapping, remove it */
6117 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6118 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006119 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 --hisnum[histype];
6121 if (--hisidx[HIST_SEARCH] < 0)
6122 hisidx[HIST_SEARCH] = hislen - 1;
6123 }
6124 last_maptick = -1;
6125 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006126 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 {
6128 if (++hisidx[histype] == hislen)
6129 hisidx[histype] = 0;
6130 hisptr = &history[histype][hisidx[histype]];
6131 vim_free(hisptr->hisstr);
6132
6133 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006134 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6136 if (hisptr->hisstr != NULL)
6137 hisptr->hisstr[len + 1] = sep;
6138
6139 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006140 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006141 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 if (histype == HIST_SEARCH && in_map)
6143 last_maptick = maptick;
6144 }
6145}
6146
6147#if defined(FEAT_EVAL) || defined(PROTO)
6148
6149/*
6150 * Get identifier of newest history entry.
6151 * "histype" may be one of the HIST_ values.
6152 */
6153 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006154get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6157 || hisidx[histype] < 0)
6158 return -1;
6159
6160 return history[histype][hisidx[histype]].hisnum;
6161}
6162
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 * Calculate history index from a number:
6165 * num > 0: seen as identifying number of a history entry
6166 * num < 0: relative position in history wrt newest entry
6167 * "histype" may be one of the HIST_ values.
6168 */
6169 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006170calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 int i;
6173 histentry_T *hist;
6174 int wrapped = FALSE;
6175
6176 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6177 || (i = hisidx[histype]) < 0 || num == 0)
6178 return -1;
6179
6180 hist = history[histype];
6181 if (num > 0)
6182 {
6183 while (hist[i].hisnum > num)
6184 if (--i < 0)
6185 {
6186 if (wrapped)
6187 break;
6188 i += hislen;
6189 wrapped = TRUE;
6190 }
6191 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6192 return i;
6193 }
6194 else if (-num <= hislen)
6195 {
6196 i += num + 1;
6197 if (i < 0)
6198 i += hislen;
6199 if (hist[i].hisstr != NULL)
6200 return i;
6201 }
6202 return -1;
6203}
6204
6205/*
6206 * Get a history entry by its index.
6207 * "histype" may be one of the HIST_ values.
6208 */
6209 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006210get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211{
6212 idx = calc_hist_idx(histype, idx);
6213 if (idx >= 0)
6214 return history[histype][idx].hisstr;
6215 else
6216 return (char_u *)"";
6217}
6218
6219/*
6220 * Clear all entries of a history.
6221 * "histype" may be one of the HIST_ values.
6222 */
6223 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006224clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225{
6226 int i;
6227 histentry_T *hisptr;
6228
6229 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6230 {
6231 hisptr = history[histype];
6232 for (i = hislen; i--;)
6233 {
6234 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006235 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006236 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237 }
6238 hisidx[histype] = -1; /* mark history as cleared */
6239 hisnum[histype] = 0; /* reset identifier counter */
6240 return OK;
6241 }
6242 return FAIL;
6243}
6244
6245/*
6246 * Remove all entries matching {str} from a history.
6247 * "histype" may be one of the HIST_ values.
6248 */
6249 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006250del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251{
6252 regmatch_T regmatch;
6253 histentry_T *hisptr;
6254 int idx;
6255 int i;
6256 int last;
6257 int found = FALSE;
6258
6259 regmatch.regprog = NULL;
6260 regmatch.rm_ic = FALSE; /* always match case */
6261 if (hislen != 0
6262 && histype >= 0
6263 && histype < HIST_COUNT
6264 && *str != NUL
6265 && (idx = hisidx[histype]) >= 0
6266 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6267 != NULL)
6268 {
6269 i = last = idx;
6270 do
6271 {
6272 hisptr = &history[histype][i];
6273 if (hisptr->hisstr == NULL)
6274 break;
6275 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6276 {
6277 found = TRUE;
6278 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006279 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006280 }
6281 else
6282 {
6283 if (i != last)
6284 {
6285 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006286 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287 }
6288 if (--last < 0)
6289 last += hislen;
6290 }
6291 if (--i < 0)
6292 i += hislen;
6293 } while (i != idx);
6294 if (history[histype][idx].hisstr == NULL)
6295 hisidx[histype] = -1;
6296 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006297 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 return found;
6299}
6300
6301/*
6302 * Remove an indexed entry from a history.
6303 * "histype" may be one of the HIST_ values.
6304 */
6305 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006306del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307{
6308 int i, j;
6309
6310 i = calc_hist_idx(histype, idx);
6311 if (i < 0)
6312 return FALSE;
6313 idx = hisidx[histype];
6314 vim_free(history[histype][i].hisstr);
6315
6316 /* When deleting the last added search string in a mapping, reset
6317 * last_maptick, so that the last added search string isn't deleted again.
6318 */
6319 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6320 last_maptick = -1;
6321
6322 while (i != idx)
6323 {
6324 j = (i + 1) % hislen;
6325 history[histype][i] = history[histype][j];
6326 i = j;
6327 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006328 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 if (--i < 0)
6330 i += hislen;
6331 hisidx[histype] = i;
6332 return TRUE;
6333}
6334
6335#endif /* FEAT_EVAL */
6336
6337#if defined(FEAT_CRYPT) || defined(PROTO)
6338/*
6339 * Very specific function to remove the value in ":set key=val" from the
6340 * history.
6341 */
6342 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006343remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006344{
6345 char_u *p;
6346 int i;
6347
6348 i = hisidx[HIST_CMD];
6349 if (i < 0)
6350 return;
6351 p = history[HIST_CMD][i].hisstr;
6352 if (p != NULL)
6353 for ( ; *p; ++p)
6354 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6355 {
6356 p = vim_strchr(p + 3, '=');
6357 if (p == NULL)
6358 break;
6359 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006360 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361 if (p[i] == '\\' && p[i + 1])
6362 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006363 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364 --p;
6365 }
6366}
6367#endif
6368
6369#endif /* FEAT_CMDHIST */
6370
Bram Moolenaar064154c2016-03-19 22:50:43 +01006371#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006372/*
6373 * Get pointer to the command line info to use. cmdline_paste() may clear
6374 * ccline and put the previous value in prev_ccline.
6375 */
6376 static struct cmdline_info *
6377get_ccline_ptr(void)
6378{
6379 if ((State & CMDLINE) == 0)
6380 return NULL;
6381 if (ccline.cmdbuff != NULL)
6382 return &ccline;
6383 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6384 return &prev_ccline;
6385 return NULL;
6386}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006387#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006388
Bram Moolenaar064154c2016-03-19 22:50:43 +01006389#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006390/*
6391 * Get the current command line in allocated memory.
6392 * Only works when the command line is being edited.
6393 * Returns NULL when something is wrong.
6394 */
6395 char_u *
6396get_cmdline_str(void)
6397{
6398 struct cmdline_info *p = get_ccline_ptr();
6399
6400 if (p == NULL)
6401 return NULL;
6402 return vim_strnsave(p->cmdbuff, p->cmdlen);
6403}
6404
6405/*
6406 * Get the current command line position, counted in bytes.
6407 * Zero is the first position.
6408 * Only works when the command line is being edited.
6409 * Returns -1 when something is wrong.
6410 */
6411 int
6412get_cmdline_pos(void)
6413{
6414 struct cmdline_info *p = get_ccline_ptr();
6415
6416 if (p == NULL)
6417 return -1;
6418 return p->cmdpos;
6419}
6420
6421/*
6422 * Set the command line byte position to "pos". Zero is the first position.
6423 * Only works when the command line is being edited.
6424 * Returns 1 when failed, 0 when OK.
6425 */
6426 int
6427set_cmdline_pos(
6428 int pos)
6429{
6430 struct cmdline_info *p = get_ccline_ptr();
6431
6432 if (p == NULL)
6433 return 1;
6434
6435 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6436 * changed the command line. */
6437 if (pos < 0)
6438 new_cmdpos = 0;
6439 else
6440 new_cmdpos = pos;
6441 return 0;
6442}
6443#endif
6444
6445#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6446/*
6447 * Get the current command-line type.
6448 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6449 * Only works when the command line is being edited.
6450 * Returns NUL when something is wrong.
6451 */
6452 int
6453get_cmdline_type(void)
6454{
6455 struct cmdline_info *p = get_ccline_ptr();
6456
6457 if (p == NULL)
6458 return NUL;
6459 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006460 return
6461# ifdef FEAT_EVAL
6462 (p->input_fn) ? '@' :
6463# endif
6464 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006465 return p->cmdfirstc;
6466}
6467#endif
6468
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6470/*
6471 * Get indices "num1,num2" that specify a range within a list (not a range of
6472 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6473 * Returns OK if parsed successfully, otherwise FAIL.
6474 */
6475 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006476get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477{
6478 int len;
6479 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006480 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
6482 *str = skipwhite(*str);
6483 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6484 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006485 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 *str += len;
6487 *num1 = (int)num;
6488 first = TRUE;
6489 }
6490 *str = skipwhite(*str);
6491 if (**str == ',') /* parse "to" part of range */
6492 {
6493 *str = skipwhite(*str + 1);
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 if (len > 0)
6496 {
6497 *num2 = (int)num;
6498 *str = skipwhite(*str + len);
6499 }
6500 else if (!first) /* no number given at all */
6501 return FAIL;
6502 }
6503 else if (first) /* only one number given */
6504 *num2 = *num1;
6505 return OK;
6506}
6507#endif
6508
6509#if defined(FEAT_CMDHIST) || defined(PROTO)
6510/*
6511 * :history command - print a history
6512 */
6513 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006514ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 histentry_T *hist;
6517 int histype1 = HIST_CMD;
6518 int histype2 = HIST_CMD;
6519 int hisidx1 = 1;
6520 int hisidx2 = -1;
6521 int idx;
6522 int i, j, k;
6523 char_u *end;
6524 char_u *arg = eap->arg;
6525
6526 if (hislen == 0)
6527 {
6528 MSG(_("'history' option is zero"));
6529 return;
6530 }
6531
6532 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6533 {
6534 end = arg;
6535 while (ASCII_ISALPHA(*end)
6536 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6537 end++;
6538 i = *end;
6539 *end = NUL;
6540 histype1 = get_histtype(arg);
6541 if (histype1 == -1)
6542 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006543 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 {
6545 histype1 = 0;
6546 histype2 = HIST_COUNT-1;
6547 }
6548 else
6549 {
6550 *end = i;
6551 EMSG(_(e_trailing));
6552 return;
6553 }
6554 }
6555 else
6556 histype2 = histype1;
6557 *end = i;
6558 }
6559 else
6560 end = arg;
6561 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6562 {
6563 EMSG(_(e_trailing));
6564 return;
6565 }
6566
6567 for (; !got_int && histype1 <= histype2; ++histype1)
6568 {
6569 STRCPY(IObuff, "\n # ");
6570 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6571 MSG_PUTS_TITLE(IObuff);
6572 idx = hisidx[histype1];
6573 hist = history[histype1];
6574 j = hisidx1;
6575 k = hisidx2;
6576 if (j < 0)
6577 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6578 if (k < 0)
6579 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6580 if (idx >= 0 && j <= k)
6581 for (i = idx + 1; !got_int; ++i)
6582 {
6583 if (i == hislen)
6584 i = 0;
6585 if (hist[i].hisstr != NULL
6586 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6587 {
6588 msg_putchar('\n');
6589 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6590 hist[i].hisnum);
6591 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6592 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006593 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 else
6595 STRCAT(IObuff, hist[i].hisstr);
6596 msg_outtrans(IObuff);
6597 out_flush();
6598 }
6599 if (i == idx)
6600 break;
6601 }
6602 }
6603}
6604#endif
6605
6606#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006607/*
6608 * Buffers for history read from a viminfo file. Only valid while reading.
6609 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006610static histentry_T *viminfo_history[HIST_COUNT] =
6611 {NULL, NULL, NULL, NULL, NULL};
6612static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6613static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614static int viminfo_add_at_front = FALSE;
6615
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006616static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617
6618/*
6619 * Translate a history type number to the associated character.
6620 */
6621 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006622hist_type2char(
6623 int type,
6624 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625{
6626 if (type == HIST_CMD)
6627 return ':';
6628 if (type == HIST_SEARCH)
6629 {
6630 if (use_question)
6631 return '?';
6632 else
6633 return '/';
6634 }
6635 if (type == HIST_EXPR)
6636 return '=';
6637 return '@';
6638}
6639
6640/*
6641 * Prepare for reading the history from the viminfo file.
6642 * This allocates history arrays to store the read history lines.
6643 */
6644 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646{
6647 int i;
6648 int num;
6649 int type;
6650 int len;
6651
6652 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006653 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (asklen > hislen)
6655 asklen = hislen;
6656
6657 for (type = 0; type < HIST_COUNT; ++type)
6658 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006659 /* Count the number of empty spaces in the history list. Entries read
6660 * from viminfo previously are also considered empty. If there are
6661 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006663 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 num++;
6665 len = asklen;
6666 if (num > len)
6667 len = num;
6668 if (len <= 0)
6669 viminfo_history[type] = NULL;
6670 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006671 viminfo_history[type] = (histentry_T *)lalloc(
6672 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (viminfo_history[type] == NULL)
6674 len = 0;
6675 viminfo_hislen[type] = len;
6676 viminfo_hisidx[type] = 0;
6677 }
6678}
6679
6680/*
6681 * Accept a line from the viminfo, store it in the history array when it's
6682 * new.
6683 */
6684 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006685read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 int type;
6688 long_u len;
6689 char_u *val;
6690 char_u *p;
6691
6692 type = hist_char2type(virp->vir_line[0]);
6693 if (viminfo_hisidx[type] < viminfo_hislen[type])
6694 {
6695 val = viminfo_readstring(virp, 1, TRUE);
6696 if (val != NULL && *val != NUL)
6697 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006698 int sep = (*val == ' ' ? NUL : *val);
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006701 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 {
6703 /* Need to re-allocate to append the separator byte. */
6704 len = STRLEN(val);
6705 p = lalloc(len + 2, TRUE);
6706 if (p != NULL)
6707 {
6708 if (type == HIST_SEARCH)
6709 {
6710 /* Search entry: Move the separator from the first
6711 * column to after the NUL. */
6712 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006713 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 }
6715 else
6716 {
6717 /* Not a search entry: No separator in the viminfo
6718 * file, add a NUL separator. */
6719 mch_memmove(p, val, (size_t)len + 1);
6720 p[len + 1] = NUL;
6721 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006722 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6723 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006724 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6725 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006726 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 }
6728 }
6729 }
6730 vim_free(val);
6731 }
6732 return viminfo_readline(virp);
6733}
6734
Bram Moolenaar07219f92013-04-14 23:19:36 +02006735/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006736 * Accept a new style history line from the viminfo, store it in the history
6737 * array when it's new.
6738 */
6739 void
6740handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006741 garray_T *values,
6742 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006743{
6744 int type;
6745 long_u len;
6746 char_u *val;
6747 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006748 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006749
6750 /* Check the format:
6751 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006752 if (values->ga_len < 4
6753 || vp[0].bv_type != BVAL_NR
6754 || vp[1].bv_type != BVAL_NR
6755 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6756 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006757 return;
6758
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006759 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006760 if (type >= HIST_COUNT)
6761 return;
6762 if (viminfo_hisidx[type] < viminfo_hislen[type])
6763 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006764 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006765 if (val != NULL && *val != NUL)
6766 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006767 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6768 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006769 int idx;
6770 int overwrite = FALSE;
6771
6772 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6773 {
6774 /* If lines were written by an older Vim we need to avoid
6775 * getting duplicates. See if the entry already exists. */
6776 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6777 {
6778 p = viminfo_history[type][idx].hisstr;
6779 if (STRCMP(val, p) == 0
6780 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6781 {
6782 overwrite = TRUE;
6783 break;
6784 }
6785 }
6786
6787 if (!overwrite)
6788 {
6789 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006790 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006791 p = lalloc(len + 2, TRUE);
6792 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006793 else
6794 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006795 if (p != NULL)
6796 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006797 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006798 if (!overwrite)
6799 {
6800 mch_memmove(p, val, (size_t)len + 1);
6801 /* Put the separator after the NUL. */
6802 p[len + 1] = sep;
6803 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006804 viminfo_history[type][idx].hisnum = 0;
6805 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006806 viminfo_hisidx[type]++;
6807 }
6808 }
6809 }
6810 }
6811 }
6812}
6813
6814/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006815 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006816 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006817 static void
6818concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819{
6820 int idx;
6821 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006822
6823 idx = hisidx[type] + viminfo_hisidx[type];
6824 if (idx >= hislen)
6825 idx -= hislen;
6826 else if (idx < 0)
6827 idx = hislen - 1;
6828 if (viminfo_add_at_front)
6829 hisidx[type] = idx;
6830 else
6831 {
6832 if (hisidx[type] == -1)
6833 hisidx[type] = hislen - 1;
6834 do
6835 {
6836 if (history[type][idx].hisstr != NULL
6837 || history[type][idx].viminfo)
6838 break;
6839 if (++idx == hislen)
6840 idx = 0;
6841 } while (idx != hisidx[type]);
6842 if (idx != hisidx[type] && --idx < 0)
6843 idx = hislen - 1;
6844 }
6845 for (i = 0; i < viminfo_hisidx[type]; i++)
6846 {
6847 vim_free(history[type][idx].hisstr);
6848 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6849 history[type][idx].viminfo = TRUE;
6850 history[type][idx].time_set = viminfo_history[type][i].time_set;
6851 if (--idx < 0)
6852 idx = hislen - 1;
6853 }
6854 idx += 1;
6855 idx %= hislen;
6856 for (i = 0; i < viminfo_hisidx[type]; i++)
6857 {
6858 history[type][idx++].hisnum = ++hisnum[type];
6859 idx %= hislen;
6860 }
6861}
6862
6863#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6864 static int
6865#ifdef __BORLANDC__
6866_RTLENTRYF
6867#endif
6868sort_hist(const void *s1, const void *s2)
6869{
6870 histentry_T *p1 = *(histentry_T **)s1;
6871 histentry_T *p2 = *(histentry_T **)s2;
6872
6873 if (p1->time_set < p2->time_set) return -1;
6874 if (p1->time_set > p2->time_set) return 1;
6875 return 0;
6876}
6877#endif
6878
6879/*
6880 * Merge history lines from viminfo and lines typed in this Vim based on the
6881 * timestamp;
6882 */
6883 static void
6884merge_history(int type)
6885{
6886 int max_len;
6887 histentry_T **tot_hist;
6888 histentry_T *new_hist;
6889 int i;
6890 int len;
6891
6892 /* Make one long list with all entries. */
6893 max_len = hislen + viminfo_hisidx[type];
6894 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6895 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6896 if (tot_hist == NULL || new_hist == NULL)
6897 {
6898 vim_free(tot_hist);
6899 vim_free(new_hist);
6900 return;
6901 }
6902 for (i = 0; i < viminfo_hisidx[type]; i++)
6903 tot_hist[i] = &viminfo_history[type][i];
6904 len = i;
6905 for (i = 0; i < hislen; i++)
6906 if (history[type][i].hisstr != NULL)
6907 tot_hist[len++] = &history[type][i];
6908
6909 /* Sort the list on timestamp. */
6910 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6911
6912 /* Keep the newest ones. */
6913 for (i = 0; i < hislen; i++)
6914 {
6915 if (i < len)
6916 {
6917 new_hist[i] = *tot_hist[i];
6918 tot_hist[i]->hisstr = NULL;
6919 if (new_hist[i].hisnum == 0)
6920 new_hist[i].hisnum = ++hisnum[type];
6921 }
6922 else
6923 clear_hist_entry(&new_hist[i]);
6924 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006925 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006926
6927 /* Free what is not kept. */
6928 for (i = 0; i < viminfo_hisidx[type]; i++)
6929 vim_free(viminfo_history[type][i].hisstr);
6930 for (i = 0; i < hislen; i++)
6931 vim_free(history[type][i].hisstr);
6932 vim_free(history[type]);
6933 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006934 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006935}
6936
6937/*
6938 * Finish reading history lines from viminfo. Not used when writing viminfo.
6939 */
6940 void
6941finish_viminfo_history(vir_T *virp)
6942{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006944 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945
6946 for (type = 0; type < HIST_COUNT; ++type)
6947 {
6948 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006949 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006950
6951 if (merge)
6952 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006954 concat_history(type);
6955
Bram Moolenaard23a8232018-02-10 18:45:26 +01006956 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006957 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 }
6959}
6960
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006961/*
6962 * Write history to viminfo file in "fp".
6963 * When "merge" is TRUE merge history lines with a previously read viminfo
6964 * file, data is in viminfo_history[].
6965 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006968write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969{
6970 int i;
6971 int type;
6972 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006973 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
6975 init_history();
6976 if (hislen == 0)
6977 return;
6978 for (type = 0; type < HIST_COUNT; ++type)
6979 {
6980 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6981 if (num_saved == 0)
6982 continue;
6983 if (num_saved < 0) /* Use default */
6984 num_saved = hislen;
6985 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6986 type == HIST_CMD ? _("Command Line") :
6987 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006988 type == HIST_EXPR ? _("Expression") :
6989 type == HIST_INPUT ? _("Input Line") :
6990 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 if (num_saved > hislen)
6992 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006993
6994 /*
6995 * Merge typed and viminfo history:
6996 * round 1: history of typed commands.
6997 * round 2: history from recently read viminfo.
6998 */
6999 for (round = 1; round <= 2; ++round)
7000 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007001 if (round == 1)
7002 /* start at newest entry, somewhere in the list */
7003 i = hisidx[type];
7004 else if (viminfo_hisidx[type] > 0)
7005 /* start at newest entry, first in the list */
7006 i = 0;
7007 else
7008 /* empty list */
7009 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007010 if (i >= 0)
7011 while (num_saved > 0
7012 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007014 char_u *p;
7015 time_t timestamp;
7016 int c = NUL;
7017
7018 if (round == 1)
7019 {
7020 p = history[type][i].hisstr;
7021 timestamp = history[type][i].time_set;
7022 }
7023 else
7024 {
7025 p = viminfo_history[type] == NULL ? NULL
7026 : viminfo_history[type][i].hisstr;
7027 timestamp = viminfo_history[type] == NULL ? 0
7028 : viminfo_history[type][i].time_set;
7029 }
7030
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007031 if (p != NULL && (round == 2
7032 || !merge
7033 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007035 --num_saved;
7036 fputc(hist_type2char(type, TRUE), fp);
7037 /* For the search history: put the separator in the
7038 * second column; use a space if there isn't one. */
7039 if (type == HIST_SEARCH)
7040 {
7041 c = p[STRLEN(p) + 1];
7042 putc(c == NUL ? ' ' : c, fp);
7043 }
7044 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007045
7046 {
7047 char cbuf[NUMBUFLEN];
7048
7049 /* New style history with a bar line. Format:
7050 * |{bartype},{histtype},{timestamp},{separator},"text" */
7051 if (c == NUL)
7052 cbuf[0] = NUL;
7053 else
7054 sprintf(cbuf, "%d", c);
7055 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7056 type, (long)timestamp, cbuf);
7057 barline_writestring(fp, p, LSIZE - 20);
7058 putc('\n', fp);
7059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007061 if (round == 1)
7062 {
7063 /* Decrement index, loop around and stop when back at
7064 * the start. */
7065 if (--i < 0)
7066 i = hislen - 1;
7067 if (i == hisidx[type])
7068 break;
7069 }
7070 else
7071 {
7072 /* Increment index. Stop at the end in the while. */
7073 ++i;
7074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007076 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007077 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007078 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007079 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007080 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007081 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 }
7083}
7084#endif /* FEAT_VIMINFO */
7085
7086#if defined(FEAT_FKMAP) || defined(PROTO)
7087/*
7088 * Write a character at the current cursor+offset position.
7089 * It is directly written into the command buffer block.
7090 */
7091 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007092cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7095 {
7096 EMSG(_("E198: cmd_pchar beyond the command length"));
7097 return;
7098 }
7099 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7100 ccline.cmdbuff[ccline.cmdlen] = NUL;
7101}
7102
7103 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007104cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7107 {
7108 /* EMSG(_("cmd_gchar beyond the command length")); */
7109 return NUL;
7110 }
7111 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7112}
7113#endif
7114
7115#if defined(FEAT_CMDWIN) || defined(PROTO)
7116/*
7117 * Open a window on the current command line and history. Allow editing in
7118 * the window. Returns when the window is closed.
7119 * Returns:
7120 * CR if the command is to be executed
7121 * Ctrl_C if it is to be abandoned
7122 * K_IGNORE if editing continues
7123 */
7124 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007125open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126{
7127 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007128 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007130 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 win_T *wp;
7132 int i;
7133 linenr_T lnum;
7134 int histtype;
7135 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 int save_restart_edit = restart_edit;
7137 int save_State = State;
7138 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007139#ifdef FEAT_RIGHTLEFT
7140 int save_cmdmsg_rl = cmdmsg_rl;
7141#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007142#ifdef FEAT_FOLDING
7143 int save_KeyTyped;
7144#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 /* Can't do this recursively. Can't do it when typing a password. */
7147 if (cmdwin_type != 0
7148# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7149 || cmdline_star > 0
7150# endif
7151 )
7152 {
7153 beep_flush();
7154 return K_IGNORE;
7155 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007156 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158 /* Save current window sizes. */
7159 win_size_save(&winsizes);
7160
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007162 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007163
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007164 /* don't use a new tab page */
7165 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007166 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007167
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 /* Create a window for the command-line buffer. */
7169 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7170 {
7171 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007172 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 return K_IGNORE;
7174 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007175 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176
7177 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007178 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007179 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007182#ifdef FEAT_FOLDING
7183 curwin->w_p_fen = FALSE;
7184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007186 curwin->w_p_rl = cmdmsg_rl;
7187 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007189 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007192 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007193 /* But don't allow switching to another buffer. */
7194 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
Bram Moolenaar46152342005-09-07 21:18:43 +00007196 /* Showing the prompt may have set need_wait_return, reset it. */
7197 need_wait_return = FALSE;
7198
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007199 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7201 {
7202 if (p_wc == TAB)
7203 {
7204 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7205 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7206 }
7207 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7208 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007209 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007211 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7212 * sets 'textwidth' to 78). */
7213 curbuf->b_p_tw = 0;
7214
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215 /* Fill the buffer with the history. */
7216 init_history();
7217 if (hislen > 0)
7218 {
7219 i = hisidx[histtype];
7220 if (i >= 0)
7221 {
7222 lnum = 0;
7223 do
7224 {
7225 if (++i == hislen)
7226 i = 0;
7227 if (history[histtype][i].hisstr != NULL)
7228 ml_append(lnum++, history[histtype][i].hisstr,
7229 (colnr_T)0, FALSE);
7230 }
7231 while (i != hisidx[histtype]);
7232 }
7233 }
7234
7235 /* Replace the empty last line with the current command-line and put the
7236 * cursor there. */
7237 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7238 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7239 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007240 changed_line_abv_curs();
7241 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007242 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007245 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
7247 /* No Ex mode here! */
7248 exmode_active = 0;
7249
7250 State = NORMAL;
7251# ifdef FEAT_MOUSE
7252 setmouse();
7253# endif
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007256 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007257 if (restart_edit != 0) /* autocmd with ":startinsert" */
7258 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259
7260 i = RedrawingDisabled;
7261 RedrawingDisabled = 0;
7262
7263 /*
7264 * Call the main loop until <CR> or CTRL-C is typed.
7265 */
7266 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007267 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269 RedrawingDisabled = i;
7270
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007271# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007272 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007273# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007276 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007277
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007278# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007279 /* Restore KeyTyped in case it is modified by autocommands */
7280 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281# endif
7282
Bram Moolenaarccc18222007-05-10 18:25:20 +00007283 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007284 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 cmdwin_type = 0;
7286
7287 exmode_active = save_exmode;
7288
Bram Moolenaarf9821062008-06-20 16:31:07 +00007289 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007290 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007291 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 {
7293 cmdwin_result = Ctrl_C;
7294 EMSG(_("E199: Active window or buffer deleted"));
7295 }
7296 else
7297 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007298# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 /* autocmds may abort script processing */
7300 if (aborting() && cmdwin_result != K_IGNORE)
7301 cmdwin_result = Ctrl_C;
7302# endif
7303 /* Set the new command line from the cmdline buffer. */
7304 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007305 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007307 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7308
7309 if (histtype == HIST_CMD)
7310 {
7311 /* Execute the command directly. */
7312 ccline.cmdbuff = vim_strsave((char_u *)p);
7313 cmdwin_result = CAR;
7314 }
7315 else
7316 {
7317 /* First need to cancel what we were doing. */
7318 ccline.cmdbuff = NULL;
7319 stuffcharReadbuff(':');
7320 stuffReadbuff((char_u *)p);
7321 stuffcharReadbuff(CAR);
7322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007323 }
7324 else if (cmdwin_result == K_XF2) /* :qa typed */
7325 {
7326 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7327 cmdwin_result = CAR;
7328 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007329 else if (cmdwin_result == Ctrl_C)
7330 {
7331 /* :q or :close, don't execute any command
7332 * and don't modify the cmd window. */
7333 ccline.cmdbuff = NULL;
7334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 else
7336 ccline.cmdbuff = vim_strsave(ml_get_curline());
7337 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007338 {
7339 ccline.cmdbuff = vim_strsave((char_u *)"");
7340 ccline.cmdlen = 0;
7341 ccline.cmdbufflen = 1;
7342 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007344 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 else
7346 {
7347 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7348 ccline.cmdbufflen = ccline.cmdlen + 1;
7349 ccline.cmdpos = curwin->w_cursor.col;
7350 if (ccline.cmdpos > ccline.cmdlen)
7351 ccline.cmdpos = ccline.cmdlen;
7352 if (cmdwin_result == K_IGNORE)
7353 {
7354 set_cmdspos_cursor();
7355 redrawcmd();
7356 }
7357 }
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007360 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007361# ifdef FEAT_CONCEAL
7362 /* Avoid command-line window first character being concealed. */
7363 curwin->w_p_cole = 0;
7364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007366 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007367 win_goto(old_curwin);
7368 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007369
7370 /* win_close() may have already wiped the buffer when 'bh' is
7371 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007372 if (bufref_valid(&bufref))
7373 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
7375 /* Restore window sizes. */
7376 win_size_restore(&winsizes);
7377
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007378 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 }
7380
7381 ga_clear(&winsizes);
7382 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007383# ifdef FEAT_RIGHTLEFT
7384 cmdmsg_rl = save_cmdmsg_rl;
7385# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386
7387 State = save_State;
7388# ifdef FEAT_MOUSE
7389 setmouse();
7390# endif
7391
7392 return cmdwin_result;
7393}
7394#endif /* FEAT_CMDWIN */
7395
7396/*
7397 * Used for commands that either take a simple command string argument, or:
7398 * cmd << endmarker
7399 * {script}
7400 * endmarker
7401 * Returns a pointer to allocated memory with {script} or NULL.
7402 */
7403 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007404script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405{
7406 char_u *theline;
7407 char *end_pattern = NULL;
7408 char dot[] = ".";
7409 garray_T ga;
7410
7411 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7412 return NULL;
7413
7414 ga_init2(&ga, 1, 0x400);
7415
7416 if (cmd[2] != NUL)
7417 end_pattern = (char *)skipwhite(cmd + 2);
7418 else
7419 end_pattern = dot;
7420
7421 for (;;)
7422 {
7423 theline = eap->getline(
7424#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007425 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426#endif
7427 NUL, eap->cookie, 0);
7428
7429 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007430 {
7431 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 ga_concat(&ga, theline);
7436 ga_append(&ga, '\n');
7437 vim_free(theline);
7438 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007439 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007440
7441 return (char_u *)ga.ga_data;
7442}