blob: 7f748cb17d12bccbb22a97b8de6fd003b697402b [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
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
21 * Variables shared between getcmdline(), redrawcmdline() and others.
22 * These need to be saved when using CTRL-R |, that's why they are in a
23 * structure.
24 */
25struct cmdline_info
26{
27 char_u *cmdbuff; /* pointer to command line buffer */
28 int cmdbufflen; /* length of cmdbuff */
29 int cmdlen; /* number of chars in command line */
30 int cmdpos; /* current cursor position */
31 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 int cmdindent; /* number of spaces before cmdline */
34 char_u *cmdprompt; /* message in front of cmdline */
35 int cmdattr; /* attributes for prompt */
36 int overstrike; /* Typing mode on the command line. Shared by
37 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000047/* The current cmdline_info. It is initialized in getcmdline() and after that
48 * used by other functions. When invoking getcmdline() recursively it needs
49 * to be saved with save_cmdline() and restored with restore_cmdline().
50 * TODO: make it local to getcmdline() and pass it around. */
51static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53static int cmd_showtail; /* Only show path tail in lists ? */
54
55#ifdef FEAT_EVAL
56static int new_cmdpos; /* position set by set_cmdline_pos() */
57#endif
58
Bram Moolenaara92522f2017-07-15 15:21:38 +020059static int extra_char = NUL; /* extra character to display when redrawing
60 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020061static int extra_char_shift;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#ifdef FEAT_CMDHIST
64typedef struct hist_entry
65{
66 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020067 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020069 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} histentry_T;
71
72static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
73static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
74static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
75 /* identifying (unique) number of newest history entry */
76static int hislen = 0; /* actual length of history tables */
77
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaard25c16e2016-01-29 22:13:30 +010080static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010082static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083# endif
84#endif
85
86#ifdef FEAT_RIGHTLEFT
87static int cmd_hkmap = 0; /* Hebrew mapping during command line */
88#endif
89
90#ifdef FEAT_FKMAP
91static int cmd_fkmap = 0; /* Farsi mapping during command line */
92#endif
93
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static int cmdline_charsize(int idx);
95static void set_cmdspos(void);
96static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void alloc_cmdbuff(int len);
101static int realloc_cmdbuff(int len);
102static void draw_cmdline(int start, int len);
103static void save_cmdline(struct cmdline_info *ccp);
104static void restore_cmdline(struct cmdline_info *ccp);
105static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100107static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100110static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static void redrawcmdprompt(void);
113static void cursorcmd(void);
114static int ccheck_abbr(int);
115static int nextwild(expand_T *xp, int type, int options, int escape);
116static void escape_fname(char_u **pp);
117static int showmatches(expand_T *xp, int wildmenu);
118static void set_expand_context(expand_T *xp);
119static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
120static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100123static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100124static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200125# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100129static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
130static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# endif
132#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200133#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100134static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200138static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
142static int
143#ifdef __BORLANDC__
144_RTLENTRYF
145#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100146sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147#endif
148
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200161 * Abandon the command line.
162 */
163 static void
164abandon_cmdline(void)
165{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100166 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200167 if (msg_scrolled == 0)
168 compute_cmdrow();
169 MSG("");
170 redraw_cmdline = TRUE;
171}
172
Bram Moolenaaree219b02017-12-17 14:55:01 +0100173#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200174/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100175 * Guess that the pattern matches everything. Only finds specific cases, such
176 * as a trailing \|, which can happen while typing a pattern.
177 */
178 static int
179empty_pattern(char_u *p)
180{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100181 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100182
183 /* remove trailing \v and the like */
184 while (n >= 2 && p[n - 2] == '\\'
185 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
186 n -= 2;
187 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
188}
189
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200190// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200191typedef struct {
192 colnr_T vs_curswant;
193 colnr_T vs_leftcol;
194 linenr_T vs_topline;
195# ifdef FEAT_DIFF
196 int vs_topfill;
197# endif
198 linenr_T vs_botline;
199 linenr_T vs_empty_rows;
200} viewstate_T;
201
202 static void
203save_viewstate(viewstate_T *vs)
204{
205 vs->vs_curswant = curwin->w_curswant;
206 vs->vs_leftcol = curwin->w_leftcol;
207 vs->vs_topline = curwin->w_topline;
208# ifdef FEAT_DIFF
209 vs->vs_topfill = curwin->w_topfill;
210# endif
211 vs->vs_botline = curwin->w_botline;
212 vs->vs_empty_rows = curwin->w_empty_rows;
213}
214
215 static void
216restore_viewstate(viewstate_T *vs)
217{
218 curwin->w_curswant = vs->vs_curswant;
219 curwin->w_leftcol = vs->vs_leftcol;
220 curwin->w_topline = vs->vs_topline;
221# ifdef FEAT_DIFF
222 curwin->w_topfill = vs->vs_topfill;
223# endif
224 curwin->w_botline = vs->vs_botline;
225 curwin->w_empty_rows = vs->vs_empty_rows;
226}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200227
228// Struct to store the state of 'incsearch' highlighting.
229typedef struct {
230 pos_T search_start; // where 'incsearch' starts searching
231 pos_T save_cursor;
232 viewstate_T init_viewstate;
233 viewstate_T old_viewstate;
234 pos_T match_start;
235 pos_T match_end;
236 int did_incsearch;
237 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200238 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200239} incsearch_state_T;
240
241 static void
242init_incsearch_state(incsearch_state_T *is_state)
243{
244 is_state->match_start = curwin->w_cursor;
245 is_state->did_incsearch = FALSE;
246 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200247 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200248 CLEAR_POS(&is_state->match_end);
249 is_state->save_cursor = curwin->w_cursor; // may be restored later
250 is_state->search_start = curwin->w_cursor;
251 save_viewstate(&is_state->init_viewstate);
252 save_viewstate(&is_state->old_viewstate);
253}
254
255/*
256 * First move cursor to end of match, then to the start. This
257 * moves the whole match onto the screen when 'nowrap' is set.
258 */
259 static void
260set_search_match(pos_T *t)
261{
262 t->lnum += search_match_lines;
263 t->col = search_match_endcol;
264 if (t->lnum > curbuf->b_ml.ml_line_count)
265 {
266 t->lnum = curbuf->b_ml.ml_line_count;
267 coladvance((colnr_T)MAXCOL);
268 }
269}
270
271/*
272 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200274 */
275 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200276do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
277 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200278{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200279 char_u *cmd;
280 cmdmod_T save_cmdmod = cmdmod;
281 char_u *p;
282 int delim_optional = FALSE;
283 int delim;
284 char_u *end;
285 char_u *dummy;
286 exarg_T ea;
287 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200288 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200289
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200290 *skiplen = 0;
291 *patlen = ccline.cmdlen;
292
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200293 if (!p_is || cmd_silent)
294 return FALSE;
295
296 // by default search all lines
297 search_first_line = 0;
298 search_last_line = MAXLNUM;
299
300 if (firstc == '/' || firstc == '?')
301 return TRUE;
302 if (firstc != ':')
303 return FALSE;
304
305 vim_memset(&ea, 0, sizeof(ea));
306 ea.line1 = 1;
307 ea.line2 = 1;
308 ea.cmd = ccline.cmdbuff;
309 ea.addr_type = ADDR_LINES;
310
311 parse_command_modifiers(&ea, &dummy, TRUE);
312 cmdmod = save_cmdmod;
313
314 cmd = skip_range(ea.cmd, NULL);
315 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
316 return FALSE;
317
318 // Skip over "substitute" to find the pattern separator.
319 for (p = cmd; ASCII_ISALPHA(*p); ++p)
320 ;
321 if (*skipwhite(p) == NUL)
322 return FALSE;
323
324 if (STRNCMP(cmd, "substitute", p - cmd) == 0
325 || STRNCMP(cmd, "smagic", p - cmd) == 0
326 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
327 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200328 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200329 if (*cmd == 's' && cmd[1] == 'm')
330 p_magic = TRUE;
331 else if (*cmd == 's' && cmd[1] == 'n')
332 p_magic = FALSE;
333 }
334 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
335 {
336 // skip over flags
337 while (ASCII_ISALPHA(*(p = skipwhite(p))))
338 ++p;
339 if (*p == NUL)
340 return FALSE;
341 }
342 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
343 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
344 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
345 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
346 || STRNCMP(cmd, "global", p - cmd) == 0)
347 {
348 // skip over "!"
349 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200351 p++;
352 if (*skipwhite(p) == NUL)
353 return FALSE;
354 }
355 if (*cmd != 'g')
356 delim_optional = TRUE;
357 }
358 else
359 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200360
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200361 p = skipwhite(p);
362 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
363 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200364
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200365 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200366
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200367 if (end == p && !use_last_pat)
368 return FALSE;
369
370 // Don't do 'hlsearch' highlighting if the pattern matches everything.
371 if (!use_last_pat)
372 {
373 char c = *end;
374 int empty;
375
376 *end = NUL;
377 empty = empty_pattern(p);
378 *end = c;
379 if (empty)
380 return FALSE;
381 }
382
383 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200384 *skiplen = (int)(p - ccline.cmdbuff);
385 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200386
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200387 // parse the address range
388 save_cursor = curwin->w_cursor;
389 curwin->w_cursor = is_state->search_start;
390 parse_cmd_address(&ea, &dummy);
391 if (ea.addr_count > 0)
392 {
393 // Allow for reverse match.
394 if (ea.line2 < ea.line1)
395 {
396 search_first_line = ea.line2;
397 search_last_line = ea.line1;
398 }
399 else
400 {
401 search_first_line = ea.line1;
402 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200403 }
404 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200405 else if (cmd[0] == 's' && cmd[1] != 'o')
406 {
407 // :s defaults to the current line
408 search_first_line = curwin->w_cursor.lnum;
409 search_last_line = curwin->w_cursor.lnum;
410 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200411
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200412 curwin->w_cursor = save_cursor;
413 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200414}
415
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200416 static void
417finish_incsearch_highlighting(
418 int gotesc,
419 incsearch_state_T *is_state,
420 int call_update_screen)
421{
422 if (is_state->did_incsearch)
423 {
424 is_state->did_incsearch = FALSE;
425 if (gotesc)
426 curwin->w_cursor = is_state->save_cursor;
427 else
428 {
429 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
430 {
431 // put the '" mark at the original position
432 curwin->w_cursor = is_state->save_cursor;
433 setpcmark();
434 }
435 curwin->w_cursor = is_state->search_start;
436 }
437 restore_viewstate(&is_state->old_viewstate);
438 highlight_match = FALSE;
439 validate_cursor(); /* needed for TAB */
440 if (call_update_screen)
441 update_screen(SOME_VALID);
442 else
443 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200444 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200445 }
446}
447
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200448/*
449 * Do 'incsearch' highlighting if desired.
450 */
451 static void
452may_do_incsearch_highlighting(
453 int firstc,
454 long count,
455 incsearch_state_T *is_state)
456{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200457 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200458 int i;
459 pos_T end_pos;
460 struct cmdline_info save_ccline;
461#ifdef FEAT_RELTIME
462 proftime_T tm;
463#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200464 int next_char;
465 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200467 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 {
469 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200470 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200471 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472
473 // If there is a character waiting, search and redraw later.
474 if (char_avail())
475 {
476 is_state->incsearch_postponed = TRUE;
477 return;
478 }
479 is_state->incsearch_postponed = FALSE;
480
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200481 if (search_first_line == 0)
482 // start at the original cursor position
483 curwin->w_cursor = is_state->search_start;
484 else
485 {
486 // start at the first line in the range
487 curwin->w_cursor.lnum = search_first_line;
488 curwin->w_cursor.col = 0;
489 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 save_last_search_pattern();
491
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200492 // Use the previous pattern for ":s//".
493 next_char = ccline.cmdbuff[skiplen + patlen];
494 use_last_pat = patlen == 0 && skiplen > 0
495 && ccline.cmdbuff[skiplen - 1] == next_char;
496
497 // If there is no pattern, don't do anything.
498 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200499 {
500 i = 0;
501 set_no_hlsearch(TRUE); // turn off previous highlight
502 redraw_all_later(SOME_VALID);
503 }
504 else
505 {
506 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
507
508 cursor_off(); // so the user knows we're busy
509 out_flush();
510 ++emsg_off; // so it doesn't beep if bad expr
511#ifdef FEAT_RELTIME
512 // Set the time limit to half a second.
513 profile_setlimit(500L, &tm);
514#endif
515 if (!p_hls)
516 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200517 if (search_first_line != 0)
518 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200519 ccline.cmdbuff[skiplen + patlen] = NUL;
520 i = do_search(NULL, firstc == ':' ? '/' : firstc,
521 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200522#ifdef FEAT_RELTIME
523 &tm, NULL
524#else
525 NULL, NULL
526#endif
527 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200528 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 --emsg_off;
530
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200531 if (curwin->w_cursor.lnum < search_first_line
532 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200533 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200534 // match outside of address range
535 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200536 curwin->w_cursor = is_state->search_start;
537 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200538
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 // if interrupted while searching, behave like it failed
540 if (got_int)
541 {
542 (void)vpeekc(); // remove <C-C> from input stream
543 got_int = FALSE; // don't abandon the command line
544 i = 0;
545 }
546 else if (char_avail())
547 // cancelled searching because a char was typed
548 is_state->incsearch_postponed = TRUE;
549 }
550 if (i != 0)
551 highlight_match = TRUE; // highlight position
552 else
553 highlight_match = FALSE; // remove highlight
554
555 // First restore the old curwin values, so the screen is positioned in the
556 // same way as the actual search command.
557 restore_viewstate(&is_state->old_viewstate);
558 changed_cline_bef_curs();
559 update_topline();
560
561 if (i != 0)
562 {
563 pos_T save_pos = curwin->w_cursor;
564
565 is_state->match_start = curwin->w_cursor;
566 set_search_match(&curwin->w_cursor);
567 validate_cursor();
568 end_pos = curwin->w_cursor;
569 is_state->match_end = end_pos;
570 curwin->w_cursor = save_pos;
571 }
572 else
573 end_pos = curwin->w_cursor; // shutup gcc 4
574
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200575 // Disable 'hlsearch' highlighting if the pattern matches everything.
576 // Avoids a flash when typing "foo\|".
577 if (!use_last_pat)
578 {
579 next_char = ccline.cmdbuff[skiplen + patlen];
580 ccline.cmdbuff[skiplen + patlen] = NUL;
581 if (empty_pattern(ccline.cmdbuff))
582 set_no_hlsearch(TRUE);
583 ccline.cmdbuff[skiplen + patlen] = next_char;
584 }
585
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200586 validate_cursor();
587 // May redraw the status line to show the cursor position.
588 if (p_ru && curwin->w_status_height > 0)
589 curwin->w_redr_status = TRUE;
590
591 save_cmdline(&save_ccline);
592 update_screen(SOME_VALID);
593 restore_cmdline(&save_ccline);
594 restore_last_search_pattern();
595
596 // Leave it at the end to make CTRL-R CTRL-W work.
597 if (i != 0)
598 curwin->w_cursor = end_pos;
599
600 msg_starthere();
601 redrawcmdline();
602 is_state->did_incsearch = TRUE;
603}
604
605/*
606 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
607 * or previous match.
608 * Returns FAIL when jumping to cmdline_not_changed;
609 */
610 static int
611may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200612 int firstc,
613 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200617 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200618 pos_T t;
619 char_u *pat;
620 int search_flags = SEARCH_NOOF;
621 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200622 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200623
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200624 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200625 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200626 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200627 return FAIL;
628
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200629 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200630 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200631 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200632 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200633 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200634 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200635 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200636 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200637
638 save_last_search_pattern();
639 cursor_off();
640 out_flush();
641 if (c == Ctrl_G)
642 {
643 t = is_state->match_end;
644 if (LT_POS(is_state->match_start, is_state->match_end))
645 // Start searching at the end of the match not at the beginning of
646 // the next column.
647 (void)decl(&t);
648 search_flags += SEARCH_COL;
649 }
650 else
651 t = is_state->match_start;
652 if (!p_hls)
653 search_flags += SEARCH_KEEP;
654 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200655 save = pat[patlen];
656 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200657 i = searchit(curwin, curbuf, &t,
658 c == Ctrl_G ? FORWARD : BACKWARD,
659 pat, count, search_flags,
660 RE_SEARCH, 0, NULL, NULL);
661 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200662 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200663 if (i)
664 {
665 is_state->search_start = is_state->match_start;
666 is_state->match_end = t;
667 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200668 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200669 {
670 // Move just before the current match, so that when nv_search
671 // finishes the cursor will be put back on the match.
672 is_state->search_start = t;
673 (void)decl(&is_state->search_start);
674 }
675 else if (c == Ctrl_G && firstc == '?')
676 {
677 // Move just after the current match, so that when nv_search
678 // finishes the cursor will be put back on the match.
679 is_state->search_start = t;
680 (void)incl(&is_state->search_start);
681 }
682 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
683 {
684 // wrap around
685 is_state->search_start = t;
686 if (firstc == '?')
687 (void)incl(&is_state->search_start);
688 else
689 (void)decl(&is_state->search_start);
690 }
691
692 set_search_match(&is_state->match_end);
693 curwin->w_cursor = is_state->match_start;
694 changed_cline_bef_curs();
695 update_topline();
696 validate_cursor();
697 highlight_match = TRUE;
698 save_viewstate(&is_state->old_viewstate);
699 update_screen(NOT_VALID);
700 redrawcmdline();
701 }
702 else
703 vim_beep(BO_ERROR);
704 restore_last_search_pattern();
705 return FAIL;
706}
707
708/*
709 * When CTRL-L typed: add character from the match to the pattern.
710 * May set "*c" to the added character.
711 * Return OK when jumping to cmdline_not_changed.
712 */
713 static int
714may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
715{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200716 int skiplen, patlen;
717
718 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200719 return FAIL;
720
721 // Add a character from under the cursor for 'incsearch'.
722 if (is_state->did_incsearch)
723 {
724 curwin->w_cursor = is_state->match_end;
725 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
726 {
727 *c = gchar_cursor();
728
729 // If 'ignorecase' and 'smartcase' are set and the
730 // command line has no uppercase characters, convert
731 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200732 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200733 *c = MB_TOLOWER(*c);
734 if (*c != NUL)
735 {
736 if (*c == firstc || vim_strchr((char_u *)(
737 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
738 {
739 // put a backslash before special characters
740 stuffcharReadbuff(*c);
741 *c = '\\';
742 }
743 return FAIL;
744 }
745 }
746 }
747 return OK;
748}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200749#endif
750
Bram Moolenaar66216052017-12-16 16:33:44 +0100751/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 * getcmdline() - accept a command line starting with firstc.
753 *
754 * firstc == ':' get ":" command line.
755 * firstc == '/' or '?' get search pattern
756 * firstc == '=' get expression
757 * firstc == '@' get text for input() function
758 * firstc == '>' get text for debug mode
759 * firstc == NUL get text for :insert command
760 * firstc == -1 like NUL, and break on CTRL-C
761 *
762 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
763 * command line.
764 *
765 * Careful: getcmdline() can be called recursively!
766 *
767 * Return pointer to allocated string if there is a commandline, NULL
768 * otherwise.
769 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100771getcmdline(
772 int firstc,
773 long count UNUSED, /* only used for incremental search */
774 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775{
776 int c;
777 int i;
778 int j;
779 int gotesc = FALSE; /* TRUE when <ESC> just typed */
780 int do_abbr; /* when TRUE check for abbr. */
781#ifdef FEAT_CMDHIST
782 char_u *lookfor = NULL; /* string to match */
783 int hiscnt; /* current history line in use */
784 int histype; /* history type to be used */
785#endif
786#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200787 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
789 int did_wild_list = FALSE; /* did wild_list() recently */
790 int wim_index = 0; /* index in wim_flags[] */
791 int res;
792 int save_msg_scroll = msg_scroll;
793 int save_State = State; /* remember State when called */
794 int some_key_typed = FALSE; /* one of the keys was typed */
795#ifdef FEAT_MOUSE
796 /* mouse drag and release events are ignored, unless they are
797 * preceded with a mouse down event */
798 int ignore_drag_release = TRUE;
799#endif
800#ifdef FEAT_EVAL
801 int break_ctrl_c = FALSE;
802#endif
803 expand_T xpc;
804 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200805#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000806 /* Everything that may work recursively should save and restore the
807 * current command line in save_ccline. That includes update_screen(), a
808 * custom status line may invoke ":normal". */
809 struct cmdline_info save_ccline;
810#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200811 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_EVAL
814 if (firstc == -1)
815 {
816 firstc = NUL;
817 break_ctrl_c = TRUE;
818 }
819#endif
820#ifdef FEAT_RIGHTLEFT
821 /* start without Hebrew mapping for a command line */
822 if (firstc == ':' || firstc == '=' || firstc == '>')
823 cmd_hkmap = 0;
824#endif
825
826 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200829 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#endif
831
832 /*
833 * set some variables for redrawcmd()
834 */
835 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000836 ccline.cmdindent = (firstc > 0 ? indent : 0);
837
838 /* alloc initial ccline.cmdbuff */
839 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 if (ccline.cmdbuff == NULL)
841 return NULL; /* out of memory */
842 ccline.cmdlen = ccline.cmdpos = 0;
843 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100844 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000846 /* autoindent for :insert and :append */
847 if (firstc <= 0)
848 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200849 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000850 ccline.cmdbuff[indent] = NUL;
851 ccline.cmdpos = indent;
852 ccline.cmdspos = indent;
853 ccline.cmdlen = indent;
854 }
855
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000857 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858
859#ifdef FEAT_RIGHTLEFT
860 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
861 && (firstc == '/' || firstc == '?'))
862 cmdmsg_rl = TRUE;
863 else
864 cmdmsg_rl = FALSE;
865#endif
866
867 redir_off = TRUE; /* don't redirect the typed command */
868 if (!cmd_silent)
869 {
870 i = msg_scrolled;
871 msg_scrolled = 0; /* avoid wait_return message */
872 gotocmdline(TRUE);
873 msg_scrolled += i;
874 redrawcmdprompt(); /* draw prompt or indent */
875 set_cmdspos();
876 }
877 xpc.xp_context = EXPAND_NOTHING;
878 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000879#ifndef BACKSLASH_IN_FILENAME
880 xpc.xp_shell = FALSE;
881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000883#if defined(FEAT_EVAL)
884 if (ccline.input_fn)
885 {
886 xpc.xp_context = ccline.xp_context;
887 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000888# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000889 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000890# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000891 }
892#endif
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 /*
895 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
896 * doing ":@0" when register 0 doesn't contain a CR.
897 */
898 msg_scroll = FALSE;
899
900 State = CMDLINE;
901
902 if (firstc == '/' || firstc == '?' || firstc == '@')
903 {
904 /* Use ":lmap" mappings for search pattern and input(). */
905 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
906 b_im_ptr = &curbuf->b_p_iminsert;
907 else
908 b_im_ptr = &curbuf->b_p_imsearch;
909 if (*b_im_ptr == B_IMODE_LMAP)
910 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100911#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 im_set_active(*b_im_ptr == B_IMODE_IM);
913#endif
914 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100915#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 else if (p_imcmdline)
917 im_set_active(TRUE);
918#endif
919
920#ifdef FEAT_MOUSE
921 setmouse();
922#endif
923#ifdef CURSOR_SHAPE
924 ui_cursor_shape(); /* may show different cursor shape */
925#endif
926
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000927 /* When inside an autocommand for writing "exiting" may be set and
928 * terminal mode set to cooked. Need to set raw mode here then. */
929 settmode(TMODE_RAW);
930
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200931 /* Trigger CmdlineEnter autocommands. */
932 cmdline_type = firstc == NUL ? '-' : firstc;
933 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200934
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935#ifdef FEAT_CMDHIST
936 init_history();
937 hiscnt = hislen; /* set hiscnt to impossible history value */
938 histype = hist_char2type(firstc);
939#endif
940
941#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000942 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943#endif
944
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200945 /* If something above caused an error, reset the flags, we do want to type
946 * and execute commands. Display may be messed up a bit. */
947 if (did_emsg)
948 redrawcmd();
949 did_emsg = FALSE;
950 got_int = FALSE;
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 /*
953 * Collect the command string, handling editing keys.
954 */
955 for (;;)
956 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000957 redir_off = TRUE; /* Don't redirect the typed command.
958 Repeated, because a ":redir" inside
959 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960#ifdef USE_ON_FLY_SCROLL
961 dont_scroll = FALSE; /* allow scrolling here */
962#endif
963 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
964
Bram Moolenaar72532d32018-04-07 19:09:09 +0200965 did_emsg = FALSE; /* There can't really be a reason why an error
966 that occurs while typing a command should
967 cause the command not to be executed. */
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000970
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100971 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
972 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000973 do
974 {
975 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100976 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000977
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 if (KeyTyped)
979 {
980 some_key_typed = TRUE;
981#ifdef FEAT_RIGHTLEFT
982 if (cmd_hkmap)
983 c = hkmap(c);
984# ifdef FEAT_FKMAP
985 if (cmd_fkmap)
986 c = cmdl_fkmap(c);
987# endif
988 if (cmdmsg_rl && !KeyStuffed)
989 {
990 /* Invert horizontal movements and operations. Only when
991 * typed by the user directly, not when the result of a
992 * mapping. */
993 switch (c)
994 {
995 case K_RIGHT: c = K_LEFT; break;
996 case K_S_RIGHT: c = K_S_LEFT; break;
997 case K_C_RIGHT: c = K_C_LEFT; break;
998 case K_LEFT: c = K_RIGHT; break;
999 case K_S_LEFT: c = K_S_RIGHT; break;
1000 case K_C_LEFT: c = K_C_RIGHT; break;
1001 }
1002 }
1003#endif
1004 }
1005
1006 /*
1007 * Ignore got_int when CTRL-C was typed here.
1008 * Don't ignore it in :global, we really need to break then, e.g., for
1009 * ":g/pat/normal /pat" (without the <CR>).
1010 * Don't ignore it for the input() function.
1011 */
1012 if ((c == Ctrl_C
1013#ifdef UNIX
1014 || c == intr_char
1015#endif
1016 )
1017#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1018 && firstc != '@'
1019#endif
1020#ifdef FEAT_EVAL
1021 && !break_ctrl_c
1022#endif
1023 && !global_busy)
1024 got_int = FALSE;
1025
1026#ifdef FEAT_CMDHIST
1027 /* free old command line when finished moving around in the history
1028 * list */
1029 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001030 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001031 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 && c != K_PAGEDOWN && c != K_PAGEUP
1033 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001034 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001036 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037#endif
1038
1039 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001040 * When there are matching completions to select <S-Tab> works like
1041 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001043 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 c = Ctrl_P;
1045
1046#ifdef FEAT_WILDMENU
1047 /* Special translations for 'wildmenu' */
1048 if (did_wild_list && p_wmnu)
1049 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001050 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001052 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 c = Ctrl_N;
1054 }
1055 /* Hitting CR after "emenu Name.": complete submenu */
1056 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1057 && ccline.cmdpos > 1
1058 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1059 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1060 && (c == '\n' || c == '\r' || c == K_KENTER))
1061 c = K_DOWN;
1062#endif
1063
1064 /* free expanded names when finished walking through matches */
1065 if (xpc.xp_numfiles != -1
1066 && !(c == p_wc && KeyTyped) && c != p_wcm
1067 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1068 && c != Ctrl_L)
1069 {
1070 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1071 did_wild_list = FALSE;
1072#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001073 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074#endif
1075 xpc.xp_context = EXPAND_NOTHING;
1076 wim_index = 0;
1077#ifdef FEAT_WILDMENU
1078 if (p_wmnu && wild_menu_showing != 0)
1079 {
1080 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001081 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001082
1083 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001084 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 if (wild_menu_showing == WM_SCROLLED)
1087 {
1088 /* Entered command line, move it up */
1089 cmdline_row--;
1090 redrawcmd();
1091 }
1092 else if (save_p_ls != -1)
1093 {
1094 /* restore 'laststatus' and 'winminheight' */
1095 p_ls = save_p_ls;
1096 p_wmh = save_p_wmh;
1097 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001098 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001100 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 redrawcmd();
1102 save_p_ls = -1;
1103 }
1104 else
1105 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 redraw_statuslines();
1108 }
1109 KeyTyped = skt;
1110 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001111 if (ccline.input_fn)
1112 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114#endif
1115 }
1116
1117#ifdef FEAT_WILDMENU
1118 /* Special translations for 'wildmenu' */
1119 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1120 {
1121 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001122 if (c == K_DOWN && ccline.cmdpos > 0
1123 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001125 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126 {
1127 /* Hitting <Up>: Remove one submenu name in front of the
1128 * cursor */
1129 int found = FALSE;
1130
1131 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1132 i = 0;
1133 while (--j > 0)
1134 {
1135 /* check for start of menu name */
1136 if (ccline.cmdbuff[j] == ' '
1137 && ccline.cmdbuff[j - 1] != '\\')
1138 {
1139 i = j + 1;
1140 break;
1141 }
1142 /* check for start of submenu name */
1143 if (ccline.cmdbuff[j] == '.'
1144 && ccline.cmdbuff[j - 1] != '\\')
1145 {
1146 if (found)
1147 {
1148 i = j + 1;
1149 break;
1150 }
1151 else
1152 found = TRUE;
1153 }
1154 }
1155 if (i > 0)
1156 cmdline_del(i);
1157 c = p_wc;
1158 xpc.xp_context = EXPAND_NOTHING;
1159 }
1160 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001161 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001162 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001163 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 {
1165 char_u upseg[5];
1166
1167 upseg[0] = PATHSEP;
1168 upseg[1] = '.';
1169 upseg[2] = '.';
1170 upseg[3] = PATHSEP;
1171 upseg[4] = NUL;
1172
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001173 if (c == K_DOWN
1174 && ccline.cmdpos > 0
1175 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1176 && (ccline.cmdpos < 3
1177 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1179 {
1180 /* go down a directory */
1181 c = p_wc;
1182 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001183 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 {
1185 /* If in a direct ancestor, strip off one ../ to go down */
1186 int found = FALSE;
1187
1188 j = ccline.cmdpos;
1189 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1190 while (--j > i)
1191 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001192#ifdef FEAT_MBYTE
1193 if (has_mbyte)
1194 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (vim_ispathsep(ccline.cmdbuff[j]))
1197 {
1198 found = TRUE;
1199 break;
1200 }
1201 }
1202 if (found
1203 && ccline.cmdbuff[j - 1] == '.'
1204 && ccline.cmdbuff[j - 2] == '.'
1205 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1206 {
1207 cmdline_del(j - 2);
1208 c = p_wc;
1209 }
1210 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001211 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
1213 /* go up a directory */
1214 int found = FALSE;
1215
1216 j = ccline.cmdpos - 1;
1217 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1218 while (--j > i)
1219 {
1220#ifdef FEAT_MBYTE
1221 if (has_mbyte)
1222 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1223#endif
1224 if (vim_ispathsep(ccline.cmdbuff[j])
1225#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001226 && vim_strchr((char_u *)" *?[{`$%#",
1227 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228#endif
1229 )
1230 {
1231 if (found)
1232 {
1233 i = j + 1;
1234 break;
1235 }
1236 else
1237 found = TRUE;
1238 }
1239 }
1240
1241 if (!found)
1242 j = i;
1243 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1244 j += 4;
1245 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1246 && j == i)
1247 j += 3;
1248 else
1249 j = 0;
1250 if (j > 0)
1251 {
1252 /* TODO this is only for DOS/UNIX systems - need to put in
1253 * machine-specific stuff here and in upseg init */
1254 cmdline_del(j);
1255 put_on_cmdline(upseg + 1, 3, FALSE);
1256 }
1257 else if (ccline.cmdpos > i)
1258 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001259
1260 /* Now complete in the new directory. Set KeyTyped in case the
1261 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001263 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267#endif /* FEAT_WILDMENU */
1268
1269 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1270 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1271 if (c == Ctrl_BSL)
1272 {
1273 ++no_mapping;
1274 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001275 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 --no_mapping;
1277 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001278 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1279 * is in a mapping. */
1280 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1281 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
1283 vungetc(c);
1284 c = Ctrl_BSL;
1285 }
1286#ifdef FEAT_EVAL
1287 else if (c == 'e')
1288 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001289 char_u *p = NULL;
1290 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292 /*
1293 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001294 * Need to save and restore the current command line, to be
1295 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 */
1297 if (ccline.cmdpos == ccline.cmdlen)
1298 new_cmdpos = 99999; /* keep it at the end */
1299 else
1300 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001301
1302 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001304 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (c == '=')
1306 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001307 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001308 * to avoid nasty things like going to another buffer when
1309 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001310 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001311 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001313 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001314 restore_cmdline(&save_ccline);
1315
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001316 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001318 len = (int)STRLEN(p);
1319 if (realloc_cmdbuff(len + 1) == OK)
1320 {
1321 ccline.cmdlen = len;
1322 STRCPY(ccline.cmdbuff, p);
1323 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001325 /* Restore the cursor or use the position set with
1326 * set_cmdline_pos(). */
1327 if (new_cmdpos > ccline.cmdlen)
1328 ccline.cmdpos = ccline.cmdlen;
1329 else
1330 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001332 KeyTyped = FALSE; /* Don't do p_wc completion. */
1333 redrawcmd();
1334 goto cmdline_changed;
1335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 }
1337 }
1338 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001339 got_int = FALSE; /* don't abandon the command line */
1340 did_emsg = FALSE;
1341 emsg_on_display = FALSE;
1342 redrawcmd();
1343 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
1345#endif
1346 else
1347 {
1348 if (c == Ctrl_G && p_im && restart_edit == 0)
1349 restart_edit = 'a';
1350 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1351 in history */
1352 goto returncmd; /* back to Normal mode */
1353 }
1354 }
1355
1356#ifdef FEAT_CMDWIN
1357 if (c == cedit_key || c == K_CMDWIN)
1358 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001359 if (ex_normal_busy == 0 && got_int == FALSE)
1360 {
1361 /*
1362 * Open a window to edit the command line (and history).
1363 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001364 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001365 some_key_typed = TRUE;
1366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368# ifdef FEAT_DIGRAPHS
1369 else
1370# endif
1371#endif
1372#ifdef FEAT_DIGRAPHS
1373 c = do_digraph(c);
1374#endif
1375
1376 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1377 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1378 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001379 /* In Ex mode a backslash escapes a newline. */
1380 if (exmode_active
1381 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001382 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001383 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001384 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001386 if (c == K_KENTER)
1387 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001389 else
1390 {
1391 gotesc = FALSE; /* Might have typed ESC previously, don't
1392 truncate the cmdline now. */
1393 if (ccheck_abbr(c + ABBR_OFF))
1394 goto cmdline_changed;
1395 if (!cmd_silent)
1396 {
1397 windgoto(msg_row, 0);
1398 out_flush();
1399 }
1400 break;
1401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 }
1403
1404 /*
1405 * Completion for 'wildchar' or 'wildcharm' key.
1406 * - hitting <ESC> twice means: abandon command line.
1407 * - wildcard expansion is only done when the 'wildchar' key is really
1408 * typed, not when it comes from a macro
1409 */
1410 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1411 {
1412 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1413 {
1414 /* if 'wildmode' contains "list" may still need to list */
1415 if (xpc.xp_numfiles > 1
1416 && !did_wild_list
1417 && (wim_flags[wim_index] & WIM_LIST))
1418 {
1419 (void)showmatches(&xpc, FALSE);
1420 redrawcmd();
1421 did_wild_list = TRUE;
1422 }
1423 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001424 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1425 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001427 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1428 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 else
1430 res = OK; /* don't insert 'wildchar' now */
1431 }
1432 else /* typed p_wc first time */
1433 {
1434 wim_index = 0;
1435 j = ccline.cmdpos;
1436 /* if 'wildmode' first contains "longest", get longest
1437 * common part */
1438 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001439 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1440 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001442 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1443 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444
1445 /* if interrupted while completing, behave like it failed */
1446 if (got_int)
1447 {
1448 (void)vpeekc(); /* remove <C-C> from input stream */
1449 got_int = FALSE; /* don't abandon the command line */
1450 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1451#ifdef FEAT_WILDMENU
1452 xpc.xp_context = EXPAND_NOTHING;
1453#endif
1454 goto cmdline_changed;
1455 }
1456
1457 /* when more than one match, and 'wildmode' first contains
1458 * "list", or no change and 'wildmode' contains "longest,list",
1459 * list all matches */
1460 if (res == OK && xpc.xp_numfiles > 1)
1461 {
1462 /* a "longest" that didn't do anything is skipped (but not
1463 * "list:longest") */
1464 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1465 wim_index = 1;
1466 if ((wim_flags[wim_index] & WIM_LIST)
1467#ifdef FEAT_WILDMENU
1468 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1469#endif
1470 )
1471 {
1472 if (!(wim_flags[0] & WIM_LONGEST))
1473 {
1474#ifdef FEAT_WILDMENU
1475 int p_wmnu_save = p_wmnu;
1476 p_wmnu = 0;
1477#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001478 /* remove match */
1479 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#ifdef FEAT_WILDMENU
1481 p_wmnu = p_wmnu_save;
1482#endif
1483 }
1484#ifdef FEAT_WILDMENU
1485 (void)showmatches(&xpc, p_wmnu
1486 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1487#else
1488 (void)showmatches(&xpc, FALSE);
1489#endif
1490 redrawcmd();
1491 did_wild_list = TRUE;
1492 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001493 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1494 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001496 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1497 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 }
1499 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001500 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 }
1502#ifdef FEAT_WILDMENU
1503 else if (xpc.xp_numfiles == -1)
1504 xpc.xp_context = EXPAND_NOTHING;
1505#endif
1506 }
1507 if (wim_index < 3)
1508 ++wim_index;
1509 if (c == ESC)
1510 gotesc = TRUE;
1511 if (res == OK)
1512 goto cmdline_changed;
1513 }
1514
1515 gotesc = FALSE;
1516
1517 /* <S-Tab> goes to last match, in a clumsy way */
1518 if (c == K_S_TAB && KeyTyped)
1519 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001520 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1521 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1522 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 goto cmdline_changed;
1524 }
1525
1526 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1527 c = NL;
1528
1529 do_abbr = TRUE; /* default: check for abbreviation */
1530
1531 /*
1532 * Big switch for a typed command line character.
1533 */
1534 switch (c)
1535 {
1536 case K_BS:
1537 case Ctrl_H:
1538 case K_DEL:
1539 case K_KDEL:
1540 case Ctrl_W:
1541#ifdef FEAT_FKMAP
1542 if (cmd_fkmap && c == K_BS)
1543 c = K_DEL;
1544#endif
1545 if (c == K_KDEL)
1546 c = K_DEL;
1547
1548 /*
1549 * delete current character is the same as backspace on next
1550 * character, except at end of line
1551 */
1552 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1553 ++ccline.cmdpos;
1554#ifdef FEAT_MBYTE
1555 if (has_mbyte && c == K_DEL)
1556 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1557 ccline.cmdbuff + ccline.cmdpos);
1558#endif
1559 if (ccline.cmdpos > 0)
1560 {
1561 char_u *p;
1562
1563 j = ccline.cmdpos;
1564 p = ccline.cmdbuff + j;
1565#ifdef FEAT_MBYTE
1566 if (has_mbyte)
1567 {
1568 p = mb_prevptr(ccline.cmdbuff, p);
1569 if (c == Ctrl_W)
1570 {
1571 while (p > ccline.cmdbuff && vim_isspace(*p))
1572 p = mb_prevptr(ccline.cmdbuff, p);
1573 i = mb_get_class(p);
1574 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1575 p = mb_prevptr(ccline.cmdbuff, p);
1576 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001577 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 }
1579 }
1580 else
1581#endif
1582 if (c == Ctrl_W)
1583 {
1584 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1585 --p;
1586 i = vim_iswordc(p[-1]);
1587 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1588 && vim_iswordc(p[-1]) == i)
1589 --p;
1590 }
1591 else
1592 --p;
1593 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1594 ccline.cmdlen -= j - ccline.cmdpos;
1595 i = ccline.cmdpos;
1596 while (i < ccline.cmdlen)
1597 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1598
1599 /* Truncate at the end, required for multi-byte chars. */
1600 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001601#ifdef FEAT_SEARCH_EXTRA
1602 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001603 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001604 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001605 /* save view settings, so that the screen
1606 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001607 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001608 }
1609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 redrawcmd();
1611 }
1612 else if (ccline.cmdlen == 0 && c != Ctrl_W
1613 && ccline.cmdprompt == NULL && indent == 0)
1614 {
1615 /* In ex and debug mode it doesn't make sense to return. */
1616 if (exmode_active
1617#ifdef FEAT_EVAL
1618 || ccline.cmdfirstc == '>'
1619#endif
1620 )
1621 goto cmdline_not_changed;
1622
Bram Moolenaard23a8232018-02-10 18:45:26 +01001623 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 if (!cmd_silent)
1625 {
1626#ifdef FEAT_RIGHTLEFT
1627 if (cmdmsg_rl)
1628 msg_col = Columns;
1629 else
1630#endif
1631 msg_col = 0;
1632 msg_putchar(' '); /* delete ':' */
1633 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001634#ifdef FEAT_SEARCH_EXTRA
1635 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001636 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 redraw_cmdline = TRUE;
1639 goto returncmd; /* back to cmd mode */
1640 }
1641 goto cmdline_changed;
1642
1643 case K_INS:
1644 case K_KINS:
1645#ifdef FEAT_FKMAP
1646 /* if Farsi mode set, we are in reverse insert mode -
1647 Do not change the mode */
1648 if (cmd_fkmap)
1649 beep_flush();
1650 else
1651#endif
1652 ccline.overstrike = !ccline.overstrike;
1653#ifdef CURSOR_SHAPE
1654 ui_cursor_shape(); /* may show different cursor shape */
1655#endif
1656 goto cmdline_not_changed;
1657
1658 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001659 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 {
1661 /* ":lmap" mappings exists, toggle use of mappings. */
1662 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 im_set_active(FALSE); /* Disable input method */
1665#endif
1666 if (b_im_ptr != NULL)
1667 {
1668 if (State & LANGMAP)
1669 *b_im_ptr = B_IMODE_LMAP;
1670 else
1671 *b_im_ptr = B_IMODE_NONE;
1672 }
1673 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001674#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 else
1676 {
1677 /* There are no ":lmap" mappings, toggle IM. When
1678 * 'imdisable' is set don't try getting the status, it's
1679 * always off. */
1680 if ((p_imdisable && b_im_ptr != NULL)
1681 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1682 {
1683 im_set_active(FALSE); /* Disable input method */
1684 if (b_im_ptr != NULL)
1685 *b_im_ptr = B_IMODE_NONE;
1686 }
1687 else
1688 {
1689 im_set_active(TRUE); /* Enable input method */
1690 if (b_im_ptr != NULL)
1691 *b_im_ptr = B_IMODE_IM;
1692 }
1693 }
1694#endif
1695 if (b_im_ptr != NULL)
1696 {
1697 if (b_im_ptr == &curbuf->b_p_iminsert)
1698 set_iminsert_global();
1699 else
1700 set_imsearch_global();
1701 }
1702#ifdef CURSOR_SHAPE
1703 ui_cursor_shape(); /* may show different cursor shape */
1704#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001705#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 /* Show/unshow value of 'keymap' in status lines later. */
1707 status_redraw_curbuf();
1708#endif
1709 goto cmdline_not_changed;
1710
1711/* case '@': only in very old vi */
1712 case Ctrl_U:
1713 /* delete all characters left of the cursor */
1714 j = ccline.cmdpos;
1715 ccline.cmdlen -= j;
1716 i = ccline.cmdpos = 0;
1717 while (i < ccline.cmdlen)
1718 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1719 /* Truncate at the end, required for multi-byte chars. */
1720 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001721#ifdef FEAT_SEARCH_EXTRA
1722 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001723 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 redrawcmd();
1726 goto cmdline_changed;
1727
1728#ifdef FEAT_CLIPBOARD
1729 case Ctrl_Y:
1730 /* Copy the modeless selection, if there is one. */
1731 if (clip_star.state != SELECT_CLEARED)
1732 {
1733 if (clip_star.state == SELECT_DONE)
1734 clip_copy_modeless_selection(TRUE);
1735 goto cmdline_not_changed;
1736 }
1737 break;
1738#endif
1739
1740 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1741 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001742 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001743 * ":normal" runs out of characters. */
1744 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001745 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746 goto cmdline_not_changed;
1747
1748 gotesc = TRUE; /* will free ccline.cmdbuff after
1749 putting it in history */
1750 goto returncmd; /* back to cmd mode */
1751
1752 case Ctrl_R: /* insert register */
1753#ifdef USE_ON_FLY_SCROLL
1754 dont_scroll = TRUE; /* disallow scrolling here */
1755#endif
1756 putcmdline('"', TRUE);
1757 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001758 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 if (i == Ctrl_O)
1760 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1761 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001762 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001763 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 --no_mapping;
1765#ifdef FEAT_EVAL
1766 /*
1767 * Insert the result of an expression.
1768 * Need to save the current command line, to be able to enter
1769 * a new one...
1770 */
1771 new_cmdpos = -1;
1772 if (c == '=')
1773 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1775 {
1776 beep_flush();
1777 c = ESC;
1778 }
1779 else
1780 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001781 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001783 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 }
1785 }
1786#endif
1787 if (c != ESC) /* use ESC to cancel inserting register */
1788 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001789 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001790
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001791#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001792 /* When there was a serious error abort getting the
1793 * command line. */
1794 if (aborting())
1795 {
1796 gotesc = TRUE; /* will free ccline.cmdbuff after
1797 putting it in history */
1798 goto returncmd; /* back to cmd mode */
1799 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 KeyTyped = FALSE; /* Don't do p_wc completion. */
1802#ifdef FEAT_EVAL
1803 if (new_cmdpos >= 0)
1804 {
1805 /* set_cmdline_pos() was used */
1806 if (new_cmdpos > ccline.cmdlen)
1807 ccline.cmdpos = ccline.cmdlen;
1808 else
1809 ccline.cmdpos = new_cmdpos;
1810 }
1811#endif
1812 }
1813 redrawcmd();
1814 goto cmdline_changed;
1815
1816 case Ctrl_D:
1817 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1818 break; /* Use ^D as normal char instead */
1819
1820 redrawcmd();
1821 continue; /* don't do incremental search now */
1822
1823 case K_RIGHT:
1824 case K_S_RIGHT:
1825 case K_C_RIGHT:
1826 do
1827 {
1828 if (ccline.cmdpos >= ccline.cmdlen)
1829 break;
1830 i = cmdline_charsize(ccline.cmdpos);
1831 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1832 break;
1833 ccline.cmdspos += i;
1834#ifdef FEAT_MBYTE
1835 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001836 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 + ccline.cmdpos);
1838 else
1839#endif
1840 ++ccline.cmdpos;
1841 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001842 while ((c == K_S_RIGHT || c == K_C_RIGHT
1843 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1845#ifdef FEAT_MBYTE
1846 if (has_mbyte)
1847 set_cmdspos_cursor();
1848#endif
1849 goto cmdline_not_changed;
1850
1851 case K_LEFT:
1852 case K_S_LEFT:
1853 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001854 if (ccline.cmdpos == 0)
1855 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 do
1857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 --ccline.cmdpos;
1859#ifdef FEAT_MBYTE
1860 if (has_mbyte) /* move to first byte of char */
1861 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1862 ccline.cmdbuff + ccline.cmdpos);
1863#endif
1864 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1865 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001866 while (ccline.cmdpos > 0
1867 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001868 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1870#ifdef FEAT_MBYTE
1871 if (has_mbyte)
1872 set_cmdspos_cursor();
1873#endif
1874 goto cmdline_not_changed;
1875
1876 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001877 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001878 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879
Bram Moolenaar4770d092006-01-12 23:22:24 +00001880#ifdef FEAT_GUI_W32
1881 /* On Win32 ignore <M-F4>, we get it when closing the window was
1882 * cancelled. */
1883 case K_F4:
1884 if (mod_mask == MOD_MASK_ALT)
1885 {
1886 redrawcmd(); /* somehow the cmdline is cleared */
1887 goto cmdline_not_changed;
1888 }
1889 break;
1890#endif
1891
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#ifdef FEAT_MOUSE
1893 case K_MIDDLEDRAG:
1894 case K_MIDDLERELEASE:
1895 goto cmdline_not_changed; /* Ignore mouse */
1896
1897 case K_MIDDLEMOUSE:
1898# ifdef FEAT_GUI
1899 /* When GUI is active, also paste when 'mouse' is empty */
1900 if (!gui.in_use)
1901# endif
1902 if (!mouse_has(MOUSE_COMMAND))
1903 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001904# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001906 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001908# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001909 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 redrawcmd();
1911 goto cmdline_changed;
1912
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001913# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001915 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 redrawcmd();
1917 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 case K_LEFTDRAG:
1921 case K_LEFTRELEASE:
1922 case K_RIGHTDRAG:
1923 case K_RIGHTRELEASE:
1924 /* Ignore drag and release events when the button-down wasn't
1925 * seen before. */
1926 if (ignore_drag_release)
1927 goto cmdline_not_changed;
1928 /* FALLTHROUGH */
1929 case K_LEFTMOUSE:
1930 case K_RIGHTMOUSE:
1931 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1932 ignore_drag_release = TRUE;
1933 else
1934 ignore_drag_release = FALSE;
1935# ifdef FEAT_GUI
1936 /* When GUI is active, also move when 'mouse' is empty */
1937 if (!gui.in_use)
1938# endif
1939 if (!mouse_has(MOUSE_COMMAND))
1940 goto cmdline_not_changed; /* Ignore mouse */
1941# ifdef FEAT_CLIPBOARD
1942 if (mouse_row < cmdline_row && clip_star.available)
1943 {
1944 int button, is_click, is_drag;
1945
1946 /*
1947 * Handle modeless selection.
1948 */
1949 button = get_mouse_button(KEY2TERMCAP1(c),
1950 &is_click, &is_drag);
1951 if (mouse_model_popup() && button == MOUSE_LEFT
1952 && (mod_mask & MOD_MASK_SHIFT))
1953 {
1954 /* Translate shift-left to right button. */
1955 button = MOUSE_RIGHT;
1956 mod_mask &= ~MOD_MASK_SHIFT;
1957 }
1958 clip_modeless(button, is_click, is_drag);
1959 goto cmdline_not_changed;
1960 }
1961# endif
1962
1963 set_cmdspos();
1964 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1965 ++ccline.cmdpos)
1966 {
1967 i = cmdline_charsize(ccline.cmdpos);
1968 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1969 && mouse_col < ccline.cmdspos % Columns + i)
1970 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001971# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (has_mbyte)
1973 {
1974 /* Count ">" for double-wide char that doesn't fit. */
1975 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001976 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 + ccline.cmdpos) - 1;
1978 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001979# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 ccline.cmdspos += i;
1981 }
1982 goto cmdline_not_changed;
1983
1984 /* Mouse scroll wheel: ignored here */
1985 case K_MOUSEDOWN:
1986 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001987 case K_MOUSELEFT:
1988 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 /* Alternate buttons ignored here */
1990 case K_X1MOUSE:
1991 case K_X1DRAG:
1992 case K_X1RELEASE:
1993 case K_X2MOUSE:
1994 case K_X2DRAG:
1995 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001996 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 goto cmdline_not_changed;
1998
1999#endif /* FEAT_MOUSE */
2000
2001#ifdef FEAT_GUI
2002 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2003 case K_LEFTRELEASE_NM:
2004 goto cmdline_not_changed;
2005
2006 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002007 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 {
2009 gui_do_scroll();
2010 redrawcmd();
2011 }
2012 goto cmdline_not_changed;
2013
2014 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002015 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002017 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 redrawcmd();
2019 }
2020 goto cmdline_not_changed;
2021#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002022#ifdef FEAT_GUI_TABLINE
2023 case K_TABLINE:
2024 case K_TABMENU:
2025 /* Don't want to change any tabs here. Make sure the same tab
2026 * is still selected. */
2027 if (gui_use_tabline())
2028 gui_mch_set_curtab(tabpage_index(curtab));
2029 goto cmdline_not_changed;
2030#endif
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 case K_SELECT: /* end of Select mode mapping - ignore */
2033 goto cmdline_not_changed;
2034
2035 case Ctrl_B: /* begin of command line */
2036 case K_HOME:
2037 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 case K_S_HOME:
2039 case K_C_HOME:
2040 ccline.cmdpos = 0;
2041 set_cmdspos();
2042 goto cmdline_not_changed;
2043
2044 case Ctrl_E: /* end of command line */
2045 case K_END:
2046 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 case K_S_END:
2048 case K_C_END:
2049 ccline.cmdpos = ccline.cmdlen;
2050 set_cmdspos_cursor();
2051 goto cmdline_not_changed;
2052
2053 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002054 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 break;
2056 goto cmdline_changed;
2057
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002058 case Ctrl_L:
2059#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002060 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002061 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002062#endif
2063
2064 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002065 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 break;
2067 goto cmdline_changed;
2068
2069 case Ctrl_N: /* next match */
2070 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002071 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002073 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2074 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002076 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002079 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 case K_UP:
2081 case K_DOWN:
2082 case K_S_UP:
2083 case K_S_DOWN:
2084 case K_PAGEUP:
2085 case K_KPAGEUP:
2086 case K_PAGEDOWN:
2087 case K_KPAGEDOWN:
2088 if (hislen == 0 || firstc == NUL) /* no history */
2089 goto cmdline_not_changed;
2090
2091 i = hiscnt;
2092
2093 /* save current command string so it can be restored later */
2094 if (lookfor == NULL)
2095 {
2096 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2097 goto cmdline_not_changed;
2098 lookfor[ccline.cmdpos] = NUL;
2099 }
2100
2101 j = (int)STRLEN(lookfor);
2102 for (;;)
2103 {
2104 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002105 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002106 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 {
2108 if (hiscnt == hislen) /* first time */
2109 hiscnt = hisidx[histype];
2110 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2111 hiscnt = hislen - 1;
2112 else if (hiscnt != hisidx[histype] + 1)
2113 --hiscnt;
2114 else /* at top of list */
2115 {
2116 hiscnt = i;
2117 break;
2118 }
2119 }
2120 else /* one step forwards */
2121 {
2122 /* on last entry, clear the line */
2123 if (hiscnt == hisidx[histype])
2124 {
2125 hiscnt = hislen;
2126 break;
2127 }
2128
2129 /* not on a history line, nothing to do */
2130 if (hiscnt == hislen)
2131 break;
2132 if (hiscnt == hislen - 1) /* wrap around */
2133 hiscnt = 0;
2134 else
2135 ++hiscnt;
2136 }
2137 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2138 {
2139 hiscnt = i;
2140 break;
2141 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002142 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002143 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 || STRNCMP(history[histype][hiscnt].hisstr,
2145 lookfor, (size_t)j) == 0)
2146 break;
2147 }
2148
2149 if (hiscnt != i) /* jumped to other entry */
2150 {
2151 char_u *p;
2152 int len;
2153 int old_firstc;
2154
2155 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002156 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 if (hiscnt == hislen)
2158 p = lookfor; /* back to the old one */
2159 else
2160 p = history[histype][hiscnt].hisstr;
2161
2162 if (histype == HIST_SEARCH
2163 && p != lookfor
2164 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2165 {
2166 /* Correct for the separator character used when
2167 * adding the history entry vs the one used now.
2168 * First loop: count length.
2169 * Second loop: copy the characters. */
2170 for (i = 0; i <= 1; ++i)
2171 {
2172 len = 0;
2173 for (j = 0; p[j] != NUL; ++j)
2174 {
2175 /* Replace old sep with new sep, unless it is
2176 * escaped. */
2177 if (p[j] == old_firstc
2178 && (j == 0 || p[j - 1] != '\\'))
2179 {
2180 if (i > 0)
2181 ccline.cmdbuff[len] = firstc;
2182 }
2183 else
2184 {
2185 /* Escape new sep, unless it is already
2186 * escaped. */
2187 if (p[j] == firstc
2188 && (j == 0 || p[j - 1] != '\\'))
2189 {
2190 if (i > 0)
2191 ccline.cmdbuff[len] = '\\';
2192 ++len;
2193 }
2194 if (i > 0)
2195 ccline.cmdbuff[len] = p[j];
2196 }
2197 ++len;
2198 }
2199 if (i == 0)
2200 {
2201 alloc_cmdbuff(len);
2202 if (ccline.cmdbuff == NULL)
2203 goto returncmd;
2204 }
2205 }
2206 ccline.cmdbuff[len] = NUL;
2207 }
2208 else
2209 {
2210 alloc_cmdbuff((int)STRLEN(p));
2211 if (ccline.cmdbuff == NULL)
2212 goto returncmd;
2213 STRCPY(ccline.cmdbuff, p);
2214 }
2215
2216 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2217 redrawcmd();
2218 goto cmdline_changed;
2219 }
2220 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002221#endif
2222 goto cmdline_not_changed;
2223
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002224#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002225 case Ctrl_G: /* next match */
2226 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002227 if (may_adjust_incsearch_highlighting(
2228 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002229 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002230 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#endif
2232
2233 case Ctrl_V:
2234 case Ctrl_Q:
2235#ifdef FEAT_MOUSE
2236 ignore_drag_release = TRUE;
2237#endif
2238 putcmdline('^', TRUE);
2239 c = get_literal(); /* get next (two) character(s) */
2240 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002241 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#ifdef FEAT_MBYTE
2243 /* may need to remove ^ when composing char was typed */
2244 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2245 {
2246 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2247 msg_putchar(' ');
2248 cursorcmd();
2249 }
2250#endif
2251 break;
2252
2253#ifdef FEAT_DIGRAPHS
2254 case Ctrl_K:
2255#ifdef FEAT_MOUSE
2256 ignore_drag_release = TRUE;
2257#endif
2258 putcmdline('?', TRUE);
2259#ifdef USE_ON_FLY_SCROLL
2260 dont_scroll = TRUE; /* disallow scrolling here */
2261#endif
2262 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002263 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 if (c != NUL)
2265 break;
2266
2267 redrawcmd();
2268 goto cmdline_not_changed;
2269#endif /* FEAT_DIGRAPHS */
2270
2271#ifdef FEAT_RIGHTLEFT
2272 case Ctrl__: /* CTRL-_: switch language mode */
2273 if (!p_ari)
2274 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002275# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 if (p_altkeymap)
2277 {
2278 cmd_fkmap = !cmd_fkmap;
2279 if (cmd_fkmap) /* in Farsi always in Insert mode */
2280 ccline.overstrike = FALSE;
2281 }
2282 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002283# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 cmd_hkmap = !cmd_hkmap;
2285 goto cmdline_not_changed;
2286#endif
2287
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002288 case K_PS:
2289 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2290 goto cmdline_changed;
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 default:
2293#ifdef UNIX
2294 if (c == intr_char)
2295 {
2296 gotesc = TRUE; /* will free ccline.cmdbuff after
2297 putting it in history */
2298 goto returncmd; /* back to Normal mode */
2299 }
2300#endif
2301 /*
2302 * Normal character with no special meaning. Just set mod_mask
2303 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2304 * the string <S-Space>. This should only happen after ^V.
2305 */
2306 if (!IS_SPECIAL(c))
2307 mod_mask = 0x0;
2308 break;
2309 }
2310 /*
2311 * End of switch on command line character.
2312 * We come here if we have a normal character.
2313 */
2314
Bram Moolenaarede3e632013-06-23 16:16:19 +02002315 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316#ifdef FEAT_MBYTE
2317 /* Add ABBR_OFF for characters above 0x100, this is
2318 * what check_abbr() expects. */
2319 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2320#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002321 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 goto cmdline_changed;
2323
2324 /*
2325 * put the character in the command line
2326 */
2327 if (IS_SPECIAL(c) || mod_mask != 0)
2328 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2329 else
2330 {
2331#ifdef FEAT_MBYTE
2332 if (has_mbyte)
2333 {
2334 j = (*mb_char2bytes)(c, IObuff);
2335 IObuff[j] = NUL; /* exclude composing chars */
2336 put_on_cmdline(IObuff, j, TRUE);
2337 }
2338 else
2339#endif
2340 {
2341 IObuff[0] = c;
2342 put_on_cmdline(IObuff, 1, TRUE);
2343 }
2344 }
2345 goto cmdline_changed;
2346
2347/*
2348 * This part implements incremental searches for "/" and "?"
2349 * Jump to cmdline_not_changed when a character has been read but the command
2350 * line did not change. Then we only search and redraw if something changed in
2351 * the past.
2352 * Jump to cmdline_changed when the command line did change.
2353 * (Sorry for the goto's, I know it is ugly).
2354 */
2355cmdline_not_changed:
2356#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002357 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 continue;
2359#endif
2360
2361cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002362 /* Trigger CmdlineChanged autocommands. */
2363 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002364
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002366 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367#endif
2368
2369#ifdef FEAT_RIGHTLEFT
2370 if (cmdmsg_rl
2371# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002372 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373# endif
2374 )
2375 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002376 * right-left typing. Not efficient, but it works.
2377 * Do it only when there are no characters left to read
2378 * to avoid useless intermediate redraws. */
2379 if (vpeekc() == NUL)
2380 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381#endif
2382 }
2383
2384returncmd:
2385
2386#ifdef FEAT_RIGHTLEFT
2387 cmdmsg_rl = FALSE;
2388#endif
2389
2390#ifdef FEAT_FKMAP
2391 cmd_fkmap = 0;
2392#endif
2393
2394 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002395 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002398 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#endif
2400
2401 if (ccline.cmdbuff != NULL)
2402 {
2403 /*
2404 * Put line in history buffer (":" and "=" only when it was typed).
2405 */
2406#ifdef FEAT_CMDHIST
2407 if (ccline.cmdlen && firstc != NUL
2408 && (some_key_typed || histype == HIST_SEARCH))
2409 {
2410 add_to_history(histype, ccline.cmdbuff, TRUE,
2411 histype == HIST_SEARCH ? firstc : NUL);
2412 if (firstc == ':')
2413 {
2414 vim_free(new_last_cmdline);
2415 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2416 }
2417 }
2418#endif
2419
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002420 if (gotesc)
2421 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 }
2423
2424 /*
2425 * If the screen was shifted up, redraw the whole screen (later).
2426 * If the line is too long, clear it, so ruler and shown command do
2427 * not get printed in the middle of it.
2428 */
2429 msg_check();
2430 msg_scroll = save_msg_scroll;
2431 redir_off = FALSE;
2432
2433 /* When the command line was typed, no need for a wait-return prompt. */
2434 if (some_key_typed)
2435 need_wait_return = FALSE;
2436
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002437 /* Trigger CmdlineLeave autocommands. */
2438 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002441#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2443 im_save_status(b_im_ptr);
2444 im_set_active(FALSE);
2445#endif
2446#ifdef FEAT_MOUSE
2447 setmouse();
2448#endif
2449#ifdef CURSOR_SHAPE
2450 ui_cursor_shape(); /* may show different cursor shape */
2451#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002452 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002454 {
2455 char_u *p = ccline.cmdbuff;
2456
2457 /* Make ccline empty, getcmdline() may try to use it. */
2458 ccline.cmdbuff = NULL;
2459 return p;
2460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461}
2462
2463#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2464/*
2465 * Get a command line with a prompt.
2466 * This is prepared to be called recursively from getcmdline() (e.g. by
2467 * f_input() when evaluating an expression from CTRL-R =).
2468 * Returns the command line in allocated memory, or NULL.
2469 */
2470 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002471getcmdline_prompt(
2472 int firstc,
2473 char_u *prompt, /* command line prompt */
2474 int attr, /* attributes for prompt */
2475 int xp_context, /* type of expansion */
2476 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 char_u *s;
2479 struct cmdline_info save_ccline;
2480 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002481 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002483 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 ccline.cmdprompt = prompt;
2485 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002486# ifdef FEAT_EVAL
2487 ccline.xp_context = xp_context;
2488 ccline.xp_arg = xp_arg;
2489 ccline.input_fn = (firstc == '@');
2490# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002491 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002493 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002494 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002495 /* Restore msg_col, the prompt from input() may have changed it.
2496 * But only if called recursively and the commandline is therefore being
2497 * restored to an old one; if not, the input() prompt stays on the screen,
2498 * so we need its modified msg_col left intact. */
2499 if (ccline.cmdbuff != NULL)
2500 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 return s;
2503}
2504#endif
2505
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002506/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002507 * Return TRUE when the text must not be changed and we can't switch to
2508 * another window or buffer. Used when editing the command line, evaluating
2509 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002510 */
2511 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002512text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002513{
2514#ifdef FEAT_CMDWIN
2515 if (cmdwin_type != 0)
2516 return TRUE;
2517#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002518 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002519}
2520
2521/*
2522 * Give an error message for a command that isn't allowed while the cmdline
2523 * window is open or editing the cmdline in another way.
2524 */
2525 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002526text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002527{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002528 EMSG(_(get_text_locked_msg()));
2529}
2530
2531 char_u *
2532get_text_locked_msg(void)
2533{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002534#ifdef FEAT_CMDWIN
2535 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002536 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002537#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002538 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002539}
2540
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002541/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002542 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2543 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002544 */
2545 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002546curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002547{
2548 if (curbuf_lock > 0)
2549 {
2550 EMSG(_("E788: Not allowed to edit another buffer now"));
2551 return TRUE;
2552 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002553 return allbuf_locked();
2554}
2555
2556/*
2557 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2558 * message.
2559 */
2560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002561allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002562{
2563 if (allbuf_lock > 0)
2564 {
2565 EMSG(_("E811: Not allowed to change buffer information now"));
2566 return TRUE;
2567 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002568 return FALSE;
2569}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002572cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573{
2574#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2575 if (cmdline_star > 0) /* showing '*', always 1 position */
2576 return 1;
2577#endif
2578 return ptr2cells(ccline.cmdbuff + idx);
2579}
2580
2581/*
2582 * Compute the offset of the cursor on the command line for the prompt and
2583 * indent.
2584 */
2585 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002586set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002588 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 ccline.cmdspos = 1 + ccline.cmdindent;
2590 else
2591 ccline.cmdspos = 0 + ccline.cmdindent;
2592}
2593
2594/*
2595 * Compute the screen position for the cursor on the command line.
2596 */
2597 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002598set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599{
2600 int i, m, c;
2601
2602 set_cmdspos();
2603 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002604 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002606 if (m < 0) /* overflow, Columns or Rows at weird value */
2607 m = MAXCOL;
2608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 else
2610 m = MAXCOL;
2611 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2612 {
2613 c = cmdline_charsize(i);
2614#ifdef FEAT_MBYTE
2615 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2616 if (has_mbyte)
2617 correct_cmdspos(i, c);
2618#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002619 /* If the cmdline doesn't fit, show cursor on last visible char.
2620 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 if ((ccline.cmdspos += c) >= m)
2622 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 ccline.cmdspos -= c;
2624 break;
2625 }
2626#ifdef FEAT_MBYTE
2627 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002628 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629#endif
2630 }
2631}
2632
2633#ifdef FEAT_MBYTE
2634/*
2635 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2636 * character that doesn't fit, so that a ">" must be displayed.
2637 */
2638 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002639correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002641 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2643 && ccline.cmdspos % Columns + cells > Columns)
2644 ccline.cmdspos++;
2645}
2646#endif
2647
2648/*
2649 * Get an Ex command line for the ":" command.
2650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002652getexline(
2653 int c, /* normally ':', NUL for ":append" */
2654 void *cookie UNUSED,
2655 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656{
2657 /* When executing a register, remove ':' that's in front of each line. */
2658 if (exec_from_reg && vpeekc() == ':')
2659 (void)vgetc();
2660 return getcmdline(c, 1L, indent);
2661}
2662
2663/*
2664 * Get an Ex command line for Ex mode.
2665 * In Ex mode we only use the OS supplied line editing features and no
2666 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002667 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002670getexmodeline(
2671 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002673 void *cookie UNUSED,
2674 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002676 garray_T line_ga;
2677 char_u *pend;
2678 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002679 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002680 int escaped = FALSE; /* CTRL-V typed */
2681 int vcol = 0;
2682 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002683 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002684 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 /* Switch cursor on now. This avoids that it happens after the "\n", which
2687 * confuses the system function that computes tabstops. */
2688 cursor_on();
2689
2690 /* always start in column 0; write a newline if necessary */
2691 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002692 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002694 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002696 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 if (p_prompt)
2698 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 while (indent-- > 0)
2700 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
2703
2704 ga_init2(&line_ga, 1, 30);
2705
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002706 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002708 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002709 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002710 while (indent >= 8)
2711 {
2712 ga_append(&line_ga, TAB);
2713 msg_puts((char_u *)" ");
2714 indent -= 8;
2715 }
2716 while (indent-- > 0)
2717 {
2718 ga_append(&line_ga, ' ');
2719 msg_putchar(' ');
2720 }
2721 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 ++no_mapping;
2723 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002724
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 /*
2726 * Get the line, one character at a time.
2727 */
2728 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002729 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002731 long sw;
2732 char_u *s;
2733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (ga_grow(&line_ga, 40) == FAIL)
2735 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
Bram Moolenaarba748c82017-02-26 14:00:07 +01002737 /*
2738 * Get one character at a time.
2739 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002740 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002741
2742 /* Check for a ":normal" command and no more characters left. */
2743 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2744 c1 = '\n';
2745 else
2746 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747
2748 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 * Handle line editing.
2750 * Previously this was left to the system, putting the terminal in
2751 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002755 msg_putchar('\n');
2756 break;
2757 }
2758
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002759 if (c1 == K_PS)
2760 {
2761 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2762 goto redraw;
2763 }
2764
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 if (!escaped)
2766 {
2767 /* CR typed means "enter", which is NL */
2768 if (c1 == '\r')
2769 c1 = '\n';
2770
2771 if (c1 == BS || c1 == K_BS
2772 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 if (line_ga.ga_len > 0)
2775 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002776#ifdef FEAT_MBYTE
2777 if (has_mbyte)
2778 {
2779 p = (char_u *)line_ga.ga_data;
2780 p[line_ga.ga_len] = NUL;
2781 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2782 line_ga.ga_len -= len;
2783 }
2784 else
2785#endif
2786 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 goto redraw;
2788 }
2789 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002790 }
2791
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002794 msg_col = startcol;
2795 msg_clr_eos();
2796 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002797 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002798 }
2799
2800 if (c1 == Ctrl_T)
2801 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002802 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002803 p = (char_u *)line_ga.ga_data;
2804 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002805 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002806 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002807add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002808 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002810 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002811 p = (char_u *)line_ga.ga_data;
2812 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2814 *s = ' ';
2815 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817redraw:
2818 /* redraw the line */
2819 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002821 p = (char_u *)line_ga.ga_data;
2822 p[line_ga.ga_len] = NUL;
2823 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002825 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002829 msg_putchar(' ');
2830 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002831 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002835 len = MB_PTR2LEN(p);
2836 msg_outtrans_len(p, len);
2837 vcol += ptr2cells(p);
2838 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 }
2840 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002842 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 continue;
2844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002846 if (c1 == Ctrl_D)
2847 {
2848 /* Delete one shiftwidth. */
2849 p = (char_u *)line_ga.ga_data;
2850 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 if (prev_char == '^')
2853 ex_keep_indent = TRUE;
2854 indent = 0;
2855 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 }
2857 else
2858 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002860 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002861 if (indent > 0)
2862 {
2863 --indent;
2864 indent -= indent % get_sw_value(curbuf);
2865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002867 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002869 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2871 --line_ga.ga_len;
2872 }
2873 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875
2876 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2877 {
2878 escaped = TRUE;
2879 continue;
2880 }
2881
2882 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2883 if (IS_SPECIAL(c1))
2884 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886
2887 if (IS_SPECIAL(c1))
2888 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002889#ifdef FEAT_MBYTE
2890 if (has_mbyte)
2891 len = (*mb_char2bytes)(c1,
2892 (char_u *)line_ga.ga_data + line_ga.ga_len);
2893 else
2894#endif
2895 {
2896 len = 1;
2897 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2898 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002899 if (c1 == '\n')
2900 msg_putchar('\n');
2901 else if (c1 == TAB)
2902 {
2903 /* Don't use chartabsize(), 'ts' can be different */
2904 do
2905 {
2906 msg_putchar(' ');
2907 } while (++vcol % 8);
2908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002912 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002915 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002916 escaped = FALSE;
2917
2918 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002919 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002921 /* We are done when a NL is entered, but not when it comes after an
2922 * odd number of backslashes, that results in a NUL. */
2923 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002925 int bcount = 0;
2926
2927 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2928 ++bcount;
2929
2930 if (bcount > 0)
2931 {
2932 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2933 * "\NL", etc. */
2934 line_ga.ga_len -= (bcount + 1) / 2;
2935 pend -= (bcount + 1) / 2;
2936 pend[-1] = '\n';
2937 }
2938
2939 if ((bcount & 1) == 0)
2940 {
2941 --line_ga.ga_len;
2942 --pend;
2943 *pend = NUL;
2944 break;
2945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
2947 }
2948
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002949 --no_mapping;
2950 --allow_keys;
2951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 /* make following messages go to the next line */
2953 msg_didout = FALSE;
2954 msg_col = 0;
2955 if (msg_row < Rows - 1)
2956 ++msg_row;
2957 emsg_on_display = FALSE; /* don't want ui_delay() */
2958
2959 if (got_int)
2960 ga_clear(&line_ga);
2961
2962 return (char_u *)line_ga.ga_data;
2963}
2964
Bram Moolenaare344bea2005-09-01 20:46:49 +00002965# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2966 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967/*
2968 * Return TRUE if ccline.overstrike is on.
2969 */
2970 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002971cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972{
2973 return ccline.overstrike;
2974}
2975
2976/*
2977 * Return TRUE if the cursor is at the end of the cmdline.
2978 */
2979 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002980cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981{
2982 return (ccline.cmdpos >= ccline.cmdlen);
2983}
2984#endif
2985
Bram Moolenaar9372a112005-12-06 19:59:18 +00002986#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987/*
2988 * Return the virtual column number at the current cursor position.
2989 * This is used by the IM code to obtain the start of the preedit string.
2990 */
2991 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002992cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993{
2994 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2995 return MAXCOL;
2996
2997# ifdef FEAT_MBYTE
2998 if (has_mbyte)
2999 {
3000 colnr_T col;
3001 int i = 0;
3002
3003 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003004 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005
3006 return col;
3007 }
3008 else
3009# endif
3010 return ccline.cmdpos;
3011}
3012#endif
3013
3014#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3015/*
3016 * If part of the command line is an IM preedit string, redraw it with
3017 * IM feedback attributes. The cursor position is restored after drawing.
3018 */
3019 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if ((State & CMDLINE)
3023 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003024 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 && !p_imdisable
3026 && im_is_preediting())
3027 {
3028 int cmdpos = 0;
3029 int cmdspos;
3030 int old_row;
3031 int old_col;
3032 colnr_T col;
3033
3034 old_row = msg_row;
3035 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003036 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038# ifdef FEAT_MBYTE
3039 if (has_mbyte)
3040 {
3041 for (col = 0; col < preedit_start_col
3042 && cmdpos < ccline.cmdlen; ++col)
3043 {
3044 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003045 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047 }
3048 else
3049# endif
3050 {
3051 cmdspos += preedit_start_col;
3052 cmdpos += preedit_start_col;
3053 }
3054
3055 msg_row = cmdline_row + (cmdspos / (int)Columns);
3056 msg_col = cmdspos % (int)Columns;
3057 if (msg_row >= Rows)
3058 msg_row = Rows - 1;
3059
3060 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3061 {
3062 int char_len;
3063 int char_attr;
3064
3065 char_attr = im_get_feedback_attr(col);
3066 if (char_attr < 0)
3067 break; /* end of preedit string */
3068
3069# ifdef FEAT_MBYTE
3070 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003071 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 else
3073# endif
3074 char_len = 1;
3075
3076 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3077 cmdpos += char_len;
3078 }
3079
3080 msg_row = old_row;
3081 msg_col = old_col;
3082 }
3083}
3084#endif /* FEAT_XIM && FEAT_GUI_GTK */
3085
3086/*
3087 * Allocate a new command line buffer.
3088 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3089 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3090 */
3091 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003092alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093{
3094 /*
3095 * give some extra space to avoid having to allocate all the time
3096 */
3097 if (len < 80)
3098 len = 100;
3099 else
3100 len += 20;
3101
3102 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3103 ccline.cmdbufflen = len;
3104}
3105
3106/*
3107 * Re-allocate the command line to length len + something extra.
3108 * return FAIL for failure, OK otherwise
3109 */
3110 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003111realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112{
3113 char_u *p;
3114
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003115 if (len < ccline.cmdbufflen)
3116 return OK; /* no need to resize */
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 p = ccline.cmdbuff;
3119 alloc_cmdbuff(len); /* will get some more */
3120 if (ccline.cmdbuff == NULL) /* out of memory */
3121 {
3122 ccline.cmdbuff = p; /* keep the old one */
3123 return FAIL;
3124 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003125 /* There isn't always a NUL after the command, but it may need to be
3126 * there, thus copy up to the NUL and add a NUL. */
3127 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3128 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003130
3131 if (ccline.xpc != NULL
3132 && ccline.xpc->xp_pattern != NULL
3133 && ccline.xpc->xp_context != EXPAND_NOTHING
3134 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3135 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003136 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003137
3138 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3139 * to point into the newly allocated memory. */
3140 if (i >= 0 && i <= ccline.cmdlen)
3141 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3142 }
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 return OK;
3145}
3146
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003147#if defined(FEAT_ARABIC) || defined(PROTO)
3148static char_u *arshape_buf = NULL;
3149
3150# if defined(EXITFREE) || defined(PROTO)
3151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003153{
3154 vim_free(arshape_buf);
3155}
3156# endif
3157#endif
3158
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159/*
3160 * Draw part of the cmdline at the current cursor position. But draw stars
3161 * when cmdline_star is TRUE.
3162 */
3163 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003164draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3167 int i;
3168
3169 if (cmdline_star > 0)
3170 for (i = 0; i < len; ++i)
3171 {
3172 msg_putchar('*');
3173# ifdef FEAT_MBYTE
3174 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003175 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176# endif
3177 }
3178 else
3179#endif
3180#ifdef FEAT_ARABIC
3181 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3182 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 static int buflen = 0;
3184 char_u *p;
3185 int j;
3186 int newlen = 0;
3187 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003188 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 int prev_c = 0;
3190 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003191 int u8c;
3192 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 /*
3196 * Do arabic shaping into a temporary buffer. This is very
3197 * inefficient!
3198 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003199 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 {
3201 /* Re-allocate the buffer. We keep it around to avoid a lot of
3202 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003204 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 arshape_buf = alloc(buflen);
3206 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 return; /* out of memory */
3208 }
3209
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003210 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3211 {
3212 /* Prepend a space to draw the leading composing char on. */
3213 arshape_buf[0] = ' ';
3214 newlen = 1;
3215 }
3216
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 for (j = start; j < start + len; j += mb_l)
3218 {
3219 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003220 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003221 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 if (ARABIC_CHAR(u8c))
3223 {
3224 /* Do Arabic shaping. */
3225 if (cmdmsg_rl)
3226 {
3227 /* displaying from right to left */
3228 pc = prev_c;
3229 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003230 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 if (j + mb_l >= start + len)
3232 nc = NUL;
3233 else
3234 nc = utf_ptr2char(p + mb_l);
3235 }
3236 else
3237 {
3238 /* displaying from left to right */
3239 if (j + mb_l >= start + len)
3240 pc = NUL;
3241 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 {
3243 int pcc[MAX_MCO];
3244
3245 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003247 pc1 = pcc[0];
3248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 nc = prev_c;
3250 }
3251 prev_c = u8c;
3252
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003253 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003256 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003258 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3259 if (u8cc[1] != 0)
3260 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 }
3264 else
3265 {
3266 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003267 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 newlen += mb_l;
3269 }
3270 }
3271
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003272 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 }
3274 else
3275#endif
3276 msg_outtrans_len(ccline.cmdbuff + start, len);
3277}
3278
3279/*
3280 * Put a character on the command line. Shifts the following text to the
3281 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3282 * "c" must be printable (fit in one display cell)!
3283 */
3284 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003285putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286{
3287 if (cmd_silent)
3288 return;
3289 msg_no_more = TRUE;
3290 msg_putchar(c);
3291 if (shift)
3292 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3293 msg_no_more = FALSE;
3294 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003295 extra_char = c;
3296 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297}
3298
3299/*
3300 * Undo a putcmdline(c, FALSE).
3301 */
3302 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003303unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 if (cmd_silent)
3306 return;
3307 msg_no_more = TRUE;
3308 if (ccline.cmdlen == ccline.cmdpos)
3309 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003310#ifdef FEAT_MBYTE
3311 else if (has_mbyte)
3312 draw_cmdline(ccline.cmdpos,
3313 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 else
3316 draw_cmdline(ccline.cmdpos, 1);
3317 msg_no_more = FALSE;
3318 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003319 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320}
3321
3322/*
3323 * Put the given string, of the given length, onto the command line.
3324 * If len is -1, then STRLEN() is used to calculate the length.
3325 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3326 * part will be redrawn, otherwise it will not. If this function is called
3327 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3328 * called afterwards.
3329 */
3330 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003331put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 int retval;
3334 int i;
3335 int m;
3336 int c;
3337
3338 if (len < 0)
3339 len = (int)STRLEN(str);
3340
3341 /* Check if ccline.cmdbuff needs to be longer */
3342 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003343 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 else
3345 retval = OK;
3346 if (retval == OK)
3347 {
3348 if (!ccline.overstrike)
3349 {
3350 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3351 ccline.cmdbuff + ccline.cmdpos,
3352 (size_t)(ccline.cmdlen - ccline.cmdpos));
3353 ccline.cmdlen += len;
3354 }
3355 else
3356 {
3357#ifdef FEAT_MBYTE
3358 if (has_mbyte)
3359 {
3360 /* Count nr of characters in the new string. */
3361 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003362 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 ++m;
3364 /* Count nr of bytes in cmdline that are overwritten by these
3365 * characters. */
3366 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003367 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 --m;
3369 if (i < ccline.cmdlen)
3370 {
3371 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3372 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3373 ccline.cmdlen += ccline.cmdpos + len - i;
3374 }
3375 else
3376 ccline.cmdlen = ccline.cmdpos + len;
3377 }
3378 else
3379#endif
3380 if (ccline.cmdpos + len > ccline.cmdlen)
3381 ccline.cmdlen = ccline.cmdpos + len;
3382 }
3383 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3384 ccline.cmdbuff[ccline.cmdlen] = NUL;
3385
3386#ifdef FEAT_MBYTE
3387 if (enc_utf8)
3388 {
3389 /* When the inserted text starts with a composing character,
3390 * backup to the character before it. There could be two of them.
3391 */
3392 i = 0;
3393 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3394 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3395 {
3396 i = (*mb_head_off)(ccline.cmdbuff,
3397 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3398 ccline.cmdpos -= i;
3399 len += i;
3400 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3401 }
3402# ifdef FEAT_ARABIC
3403 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3404 {
3405 /* Check the previous character for Arabic combining pair. */
3406 i = (*mb_head_off)(ccline.cmdbuff,
3407 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3408 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3409 + ccline.cmdpos - i), c))
3410 {
3411 ccline.cmdpos -= i;
3412 len += i;
3413 }
3414 else
3415 i = 0;
3416 }
3417# endif
3418 if (i != 0)
3419 {
3420 /* Also backup the cursor position. */
3421 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3422 ccline.cmdspos -= i;
3423 msg_col -= i;
3424 if (msg_col < 0)
3425 {
3426 msg_col += Columns;
3427 --msg_row;
3428 }
3429 }
3430 }
3431#endif
3432
3433 if (redraw && !cmd_silent)
3434 {
3435 msg_no_more = TRUE;
3436 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003437 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3439 /* Avoid clearing the rest of the line too often. */
3440 if (cmdline_row != i || ccline.overstrike)
3441 msg_clr_eos();
3442 msg_no_more = FALSE;
3443 }
3444#ifdef FEAT_FKMAP
3445 /*
3446 * If we are in Farsi command mode, the character input must be in
3447 * Insert mode. So do not advance the cmdpos.
3448 */
3449 if (!cmd_fkmap)
3450#endif
3451 {
3452 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003455 if (m < 0) /* overflow, Columns or Rows at weird value */
3456 m = MAXCOL;
3457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 else
3459 m = MAXCOL;
3460 for (i = 0; i < len; ++i)
3461 {
3462 c = cmdline_charsize(ccline.cmdpos);
3463#ifdef FEAT_MBYTE
3464 /* count ">" for a double-wide char that doesn't fit. */
3465 if (has_mbyte)
3466 correct_cmdspos(ccline.cmdpos, c);
3467#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003468 /* Stop cursor at the end of the screen, but do increment the
3469 * insert position, so that entering a very long command
3470 * works, even though you can't see it. */
3471 if (ccline.cmdspos + c < m)
3472 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473#ifdef FEAT_MBYTE
3474 if (has_mbyte)
3475 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003476 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (c > len - i - 1)
3478 c = len - i - 1;
3479 ccline.cmdpos += c;
3480 i += c;
3481 }
3482#endif
3483 ++ccline.cmdpos;
3484 }
3485 }
3486 }
3487 if (redraw)
3488 msg_check();
3489 return retval;
3490}
3491
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003492static struct cmdline_info prev_ccline;
3493static int prev_ccline_used = FALSE;
3494
3495/*
3496 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3497 * and overwrite it. But get_cmdline_str() may need it, thus make it
3498 * available globally in prev_ccline.
3499 */
3500 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003501save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003502{
3503 if (!prev_ccline_used)
3504 {
3505 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3506 prev_ccline_used = TRUE;
3507 }
3508 *ccp = prev_ccline;
3509 prev_ccline = ccline;
3510 ccline.cmdbuff = NULL;
3511 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003512 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003513}
3514
3515/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003516 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003517 */
3518 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003519restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003520{
3521 ccline = prev_ccline;
3522 prev_ccline = *ccp;
3523}
3524
Bram Moolenaar5a305422006-04-28 22:38:25 +00003525#if defined(FEAT_EVAL) || defined(PROTO)
3526/*
3527 * Save the command line into allocated memory. Returns a pointer to be
3528 * passed to restore_cmdline_alloc() later.
3529 * Returns NULL when failed.
3530 */
3531 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003532save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003533{
3534 struct cmdline_info *p;
3535
3536 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3537 if (p != NULL)
3538 save_cmdline(p);
3539 return (char_u *)p;
3540}
3541
3542/*
3543 * Restore the command line from the return value of save_cmdline_alloc().
3544 */
3545 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003546restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003547{
3548 if (p != NULL)
3549 {
3550 restore_cmdline((struct cmdline_info *)p);
3551 vim_free(p);
3552 }
3553}
3554#endif
3555
Bram Moolenaar8299df92004-07-10 09:47:34 +00003556/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003557 * Paste a yank register into the command line.
3558 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003559 * insert_reg() can't be used here, because special characters from the
3560 * register contents will be interpreted as commands.
3561 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003562 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003563 */
3564 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003565cmdline_paste(
3566 int regname,
3567 int literally, /* Insert text literally instead of "as typed" */
3568 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003569{
3570 long i;
3571 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003572 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003573 int allocated;
3574 struct cmdline_info save_ccline;
3575
3576 /* check for valid regname; also accept special characters for CTRL-R in
3577 * the command line */
3578 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003579 && regname != Ctrl_A && regname != Ctrl_L
3580 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581 return FAIL;
3582
3583 /* A register containing CTRL-R can cause an endless loop. Allow using
3584 * CTRL-C to break the loop. */
3585 line_breakcheck();
3586 if (got_int)
3587 return FAIL;
3588
3589#ifdef FEAT_CLIPBOARD
3590 regname = may_get_selection(regname);
3591#endif
3592
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003593 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003594 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003595 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003596 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003598 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003599 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003600
3601 if (i)
3602 {
3603 /* Got the value of a special register in "arg". */
3604 if (arg == NULL)
3605 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003606
3607 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3608 * part of the word. */
3609 p = arg;
3610 if (p_is && regname == Ctrl_W)
3611 {
3612 char_u *w;
3613 int len;
3614
3615 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003616 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003617 {
3618#ifdef FEAT_MBYTE
3619 if (has_mbyte)
3620 {
3621 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3622 if (!vim_iswordc(mb_ptr2char(w - len)))
3623 break;
3624 w -= len;
3625 }
3626 else
3627#endif
3628 {
3629 if (!vim_iswordc(w[-1]))
3630 break;
3631 --w;
3632 }
3633 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003634 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003635 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3636 p += len;
3637 }
3638
3639 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003640 if (allocated)
3641 vim_free(arg);
3642 return OK;
3643 }
3644
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003645 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003646}
3647
3648/*
3649 * Put a string on the command line.
3650 * When "literally" is TRUE, insert literally.
3651 * When "literally" is FALSE, insert as typed, but don't leave the command
3652 * line.
3653 */
3654 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003655cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003656{
3657 int c, cv;
3658
3659 if (literally)
3660 put_on_cmdline(s, -1, TRUE);
3661 else
3662 while (*s != NUL)
3663 {
3664 cv = *s;
3665 if (cv == Ctrl_V && s[1])
3666 ++s;
3667#ifdef FEAT_MBYTE
3668 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003669 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003670 else
3671#endif
3672 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003673 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3674 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003675#ifdef UNIX
3676 || c == intr_char
3677#endif
3678 || (c == Ctrl_BSL && *s == Ctrl_N))
3679 stuffcharReadbuff(Ctrl_V);
3680 stuffcharReadbuff(c);
3681 }
3682}
3683
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684#ifdef FEAT_WILDMENU
3685/*
3686 * Delete characters on the command line, from "from" to the current
3687 * position.
3688 */
3689 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003690cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691{
3692 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3693 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3694 ccline.cmdlen -= ccline.cmdpos - from;
3695 ccline.cmdpos = from;
3696}
3697#endif
3698
3699/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003700 * This function is called when the screen size changes and with incremental
3701 * search and in other situations where the command line may have been
3702 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 */
3704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003705redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003707 redrawcmdline_ex(TRUE);
3708}
3709
3710 void
3711redrawcmdline_ex(int do_compute_cmdrow)
3712{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 if (cmd_silent)
3714 return;
3715 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003716 if (do_compute_cmdrow)
3717 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 redrawcmd();
3719 cursorcmd();
3720}
3721
3722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003723redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 int i;
3726
3727 if (cmd_silent)
3728 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003729 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 msg_putchar(ccline.cmdfirstc);
3731 if (ccline.cmdprompt != NULL)
3732 {
3733 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3734 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3735 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003736 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 --ccline.cmdindent;
3738 }
3739 else
3740 for (i = ccline.cmdindent; i > 0; --i)
3741 msg_putchar(' ');
3742}
3743
3744/*
3745 * Redraw what is currently on the command line.
3746 */
3747 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003748redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749{
3750 if (cmd_silent)
3751 return;
3752
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003753 /* when 'incsearch' is set there may be no command line while redrawing */
3754 if (ccline.cmdbuff == NULL)
3755 {
3756 windgoto(cmdline_row, 0);
3757 msg_clr_eos();
3758 return;
3759 }
3760
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 msg_start();
3762 redrawcmdprompt();
3763
3764 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3765 msg_no_more = TRUE;
3766 draw_cmdline(0, ccline.cmdlen);
3767 msg_clr_eos();
3768 msg_no_more = FALSE;
3769
3770 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003771 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003772 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773
3774 /*
3775 * An emsg() before may have set msg_scroll. This is used in normal mode,
3776 * in cmdline mode we can reset them now.
3777 */
3778 msg_scroll = FALSE; /* next message overwrites cmdline */
3779
3780 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3781 * in cmdline mode */
3782 skip_redraw = FALSE;
3783}
3784
3785 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003786compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003788 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 cmdline_row = Rows - 1;
3790 else
3791 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003792 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793}
3794
3795 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003796cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
3798 if (cmd_silent)
3799 return;
3800
3801#ifdef FEAT_RIGHTLEFT
3802 if (cmdmsg_rl)
3803 {
3804 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3805 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3806 if (msg_row <= 0)
3807 msg_row = Rows - 1;
3808 }
3809 else
3810#endif
3811 {
3812 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3813 msg_col = ccline.cmdspos % (int)Columns;
3814 if (msg_row >= Rows)
3815 msg_row = Rows - 1;
3816 }
3817
3818 windgoto(msg_row, msg_col);
3819#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003820 if (p_imst == IM_ON_THE_SPOT)
3821 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822#endif
3823#ifdef MCH_CURSOR_SHAPE
3824 mch_update_cursor();
3825#endif
3826}
3827
3828 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003829gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830{
3831 msg_start();
3832#ifdef FEAT_RIGHTLEFT
3833 if (cmdmsg_rl)
3834 msg_col = Columns - 1;
3835 else
3836#endif
3837 msg_col = 0; /* always start in column 0 */
3838 if (clr) /* clear the bottom line(s) */
3839 msg_clr_eos(); /* will reset clear_cmdline */
3840 windgoto(cmdline_row, 0);
3841}
3842
3843/*
3844 * Check the word in front of the cursor for an abbreviation.
3845 * Called when the non-id character "c" has been entered.
3846 * When an abbreviation is recognized it is removed from the text with
3847 * backspaces and the replacement string is inserted, followed by "c".
3848 */
3849 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003850ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003852 int spos = 0;
3853
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3855 return FALSE;
3856
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003857 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3858 * Actually accepts any mark. */
3859 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3860 spos++;
3861 if (ccline.cmdlen - spos > 5
3862 && ccline.cmdbuff[spos] == '\''
3863 && ccline.cmdbuff[spos + 2] == ','
3864 && ccline.cmdbuff[spos + 3] == '\'')
3865 spos += 5;
3866 else
3867 /* check abbreviation from the beginning of the commandline */
3868 spos = 0;
3869
3870 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871}
3872
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003873#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3874 static int
3875#ifdef __BORLANDC__
3876_RTLENTRYF
3877#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003878sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003879{
3880 char_u *p1 = *(char_u **)s1;
3881 char_u *p2 = *(char_u **)s2;
3882
3883 if (*p1 != '<' && *p2 == '<') return -1;
3884 if (*p1 == '<' && *p2 != '<') return 1;
3885 return STRCMP(p1, p2);
3886}
3887#endif
3888
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889/*
3890 * Return FAIL if this is not an appropriate context in which to do
3891 * completion of anything, return OK if it is (even if there are no matches).
3892 * For the caller, this means that the character is just passed through like a
3893 * normal character (instead of being expanded). This allows :s/^I^D etc.
3894 */
3895 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003896nextwild(
3897 expand_T *xp,
3898 int type,
3899 int options, /* extra options for ExpandOne() */
3900 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901{
3902 int i, j;
3903 char_u *p1;
3904 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 int difflen;
3906 int v;
3907
3908 if (xp->xp_numfiles == -1)
3909 {
3910 set_expand_context(xp);
3911 cmd_showtail = expand_showtail(xp);
3912 }
3913
3914 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3915 {
3916 beep_flush();
3917 return OK; /* Something illegal on command line */
3918 }
3919 if (xp->xp_context == EXPAND_NOTHING)
3920 {
3921 /* Caller can use the character as a normal char instead */
3922 return FAIL;
3923 }
3924
3925 MSG_PUTS("..."); /* show that we are busy */
3926 out_flush();
3927
3928 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003929 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930
3931 if (type == WILD_NEXT || type == WILD_PREV)
3932 {
3933 /*
3934 * Get next/previous match for a previous expanded pattern.
3935 */
3936 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3937 }
3938 else
3939 {
3940 /*
3941 * Translate string into pattern and expand it.
3942 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003943 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3944 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 p2 = NULL;
3946 else
3947 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003948 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003949 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3950 if (escape)
3951 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003952
3953 if (p_wic)
3954 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003955 p2 = ExpandOne(xp, p1,
3956 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003957 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003959 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 if (p2 != NULL && type == WILD_LONGEST)
3961 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003962 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 if (ccline.cmdbuff[i + j] == '*'
3964 || ccline.cmdbuff[i + j] == '?')
3965 break;
3966 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003967 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970 }
3971
3972 if (p2 != NULL && !got_int)
3973 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003974 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003975 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003977 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 xp->xp_pattern = ccline.cmdbuff + i;
3979 }
3980 else
3981 v = OK;
3982 if (v == OK)
3983 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003984 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3985 &ccline.cmdbuff[ccline.cmdpos],
3986 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3987 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 ccline.cmdlen += difflen;
3989 ccline.cmdpos += difflen;
3990 }
3991 }
3992 vim_free(p2);
3993
3994 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003995 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996
3997 /* When expanding a ":map" command and no matches are found, assume that
3998 * the key is supposed to be inserted literally */
3999 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4000 return FAIL;
4001
4002 if (xp->xp_numfiles <= 0 && p2 == NULL)
4003 beep_flush();
4004 else if (xp->xp_numfiles == 1)
4005 /* free expanded pattern */
4006 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4007
4008 return OK;
4009}
4010
4011/*
4012 * Do wildcard expansion on the string 'str'.
4013 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004014 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 * Return NULL for failure.
4016 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004017 * "orig" is the originally expanded string, copied to allocated memory. It
4018 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4019 * WILD_PREV "orig" should be NULL.
4020 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004021 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4022 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 *
4024 * mode = WILD_FREE: just free previously expanded matches
4025 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4026 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4027 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4028 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4029 * mode = WILD_ALL: return all matches concatenated
4030 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004031 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 *
4033 * options = WILD_LIST_NOTFOUND: list entries without a match
4034 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4035 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4036 * options = WILD_NO_BEEP: Don't beep for multiple matches
4037 * options = WILD_ADD_SLASH: add a slash after directory names
4038 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4039 * options = WILD_SILENT: don't print warning messages
4040 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004041 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 *
4043 * The variables xp->xp_context and xp->xp_backslash must have been set!
4044 */
4045 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004046ExpandOne(
4047 expand_T *xp,
4048 char_u *str,
4049 char_u *orig, /* allocated copy of original of expanded string */
4050 int options,
4051 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052{
4053 char_u *ss = NULL;
4054 static int findex;
4055 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004056 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 int i;
4058 long_u len;
4059 int non_suf_match; /* number without matching suffix */
4060
4061 /*
4062 * first handle the case of using an old match
4063 */
4064 if (mode == WILD_NEXT || mode == WILD_PREV)
4065 {
4066 if (xp->xp_numfiles > 0)
4067 {
4068 if (mode == WILD_PREV)
4069 {
4070 if (findex == -1)
4071 findex = xp->xp_numfiles;
4072 --findex;
4073 }
4074 else /* mode == WILD_NEXT */
4075 ++findex;
4076
4077 /*
4078 * When wrapping around, return the original string, set findex to
4079 * -1.
4080 */
4081 if (findex < 0)
4082 {
4083 if (orig_save == NULL)
4084 findex = xp->xp_numfiles - 1;
4085 else
4086 findex = -1;
4087 }
4088 if (findex >= xp->xp_numfiles)
4089 {
4090 if (orig_save == NULL)
4091 findex = 0;
4092 else
4093 findex = -1;
4094 }
4095#ifdef FEAT_WILDMENU
4096 if (p_wmnu)
4097 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4098 findex, cmd_showtail);
4099#endif
4100 if (findex == -1)
4101 return vim_strsave(orig_save);
4102 return vim_strsave(xp->xp_files[findex]);
4103 }
4104 else
4105 return NULL;
4106 }
4107
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004108 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4110 {
4111 FreeWild(xp->xp_numfiles, xp->xp_files);
4112 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004113 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 }
4115 findex = 0;
4116
4117 if (mode == WILD_FREE) /* only release file name */
4118 return NULL;
4119
4120 if (xp->xp_numfiles == -1)
4121 {
4122 vim_free(orig_save);
4123 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004124 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
4126 /*
4127 * Do the expansion.
4128 */
4129 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4130 options) == FAIL)
4131 {
4132#ifdef FNAME_ILLEGAL
4133 /* Illegal file name has been silently skipped. But when there
4134 * are wildcards, the real problem is that there was no match,
4135 * causing the pattern to be added, which has illegal characters.
4136 */
4137 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4138 EMSG2(_(e_nomatch2), str);
4139#endif
4140 }
4141 else if (xp->xp_numfiles == 0)
4142 {
4143 if (!(options & WILD_SILENT))
4144 EMSG2(_(e_nomatch2), str);
4145 }
4146 else
4147 {
4148 /* Escape the matches for use on the command line. */
4149 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4150
4151 /*
4152 * Check for matching suffixes in file names.
4153 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004154 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4155 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
4157 if (xp->xp_numfiles)
4158 non_suf_match = xp->xp_numfiles;
4159 else
4160 non_suf_match = 1;
4161 if ((xp->xp_context == EXPAND_FILES
4162 || xp->xp_context == EXPAND_DIRECTORIES)
4163 && xp->xp_numfiles > 1)
4164 {
4165 /*
4166 * More than one match; check suffix.
4167 * The files will have been sorted on matching suffix in
4168 * expand_wildcards, only need to check the first two.
4169 */
4170 non_suf_match = 0;
4171 for (i = 0; i < 2; ++i)
4172 if (match_suffix(xp->xp_files[i]))
4173 ++non_suf_match;
4174 }
4175 if (non_suf_match != 1)
4176 {
4177 /* Can we ever get here unless it's while expanding
4178 * interactively? If not, we can get rid of this all
4179 * together. Don't really want to wait for this message
4180 * (and possibly have to hit return to continue!).
4181 */
4182 if (!(options & WILD_SILENT))
4183 EMSG(_(e_toomany));
4184 else if (!(options & WILD_NO_BEEP))
4185 beep_flush();
4186 }
4187 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4188 ss = vim_strsave(xp->xp_files[0]);
4189 }
4190 }
4191 }
4192
4193 /* Find longest common part */
4194 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4195 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004196 int mb_len = 1;
4197 int c0, ci;
4198
4199 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004201#ifdef FEAT_MBYTE
4202 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004204 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4205 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4206 }
4207 else
4208#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004209 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004210 for (i = 1; i < xp->xp_numfiles; ++i)
4211 {
4212#ifdef FEAT_MBYTE
4213 if (has_mbyte)
4214 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4215 else
4216#endif
4217 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004218 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004220 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004221 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004223 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 break;
4225 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004226 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 break;
4228 }
4229 if (i < xp->xp_numfiles)
4230 {
4231 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004232 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 break;
4234 }
4235 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004236
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 ss = alloc((unsigned)len + 1);
4238 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004239 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 findex = -1; /* next p_wc gets first one */
4241 }
4242
4243 /* Concatenate all matching names */
4244 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4245 {
4246 len = 0;
4247 for (i = 0; i < xp->xp_numfiles; ++i)
4248 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4249 ss = lalloc(len, TRUE);
4250 if (ss != NULL)
4251 {
4252 *ss = NUL;
4253 for (i = 0; i < xp->xp_numfiles; ++i)
4254 {
4255 STRCAT(ss, xp->xp_files[i]);
4256 if (i != xp->xp_numfiles - 1)
4257 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4258 }
4259 }
4260 }
4261
4262 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4263 ExpandCleanup(xp);
4264
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004265 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004266 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004267 vim_free(orig);
4268
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return ss;
4270}
4271
4272/*
4273 * Prepare an expand structure for use.
4274 */
4275 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004276ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004278 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004279 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004281#ifndef BACKSLASH_IN_FILENAME
4282 xp->xp_shell = FALSE;
4283#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 xp->xp_numfiles = -1;
4285 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004286#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4287 xp->xp_arg = NULL;
4288#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004289 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290}
4291
4292/*
4293 * Cleanup an expand structure after use.
4294 */
4295 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004296ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297{
4298 if (xp->xp_numfiles >= 0)
4299 {
4300 FreeWild(xp->xp_numfiles, xp->xp_files);
4301 xp->xp_numfiles = -1;
4302 }
4303}
4304
4305 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004306ExpandEscape(
4307 expand_T *xp,
4308 char_u *str,
4309 int numfiles,
4310 char_u **files,
4311 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312{
4313 int i;
4314 char_u *p;
4315
4316 /*
4317 * May change home directory back to "~"
4318 */
4319 if (options & WILD_HOME_REPLACE)
4320 tilde_replace(str, numfiles, files);
4321
4322 if (options & WILD_ESCAPE)
4323 {
4324 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004325 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004326 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 || xp->xp_context == EXPAND_BUFFERS
4328 || xp->xp_context == EXPAND_DIRECTORIES)
4329 {
4330 /*
4331 * Insert a backslash into a file name before a space, \, %, #
4332 * and wildmatch characters, except '~'.
4333 */
4334 for (i = 0; i < numfiles; ++i)
4335 {
4336 /* for ":set path=" we need to escape spaces twice */
4337 if (xp->xp_backslash == XP_BS_THREE)
4338 {
4339 p = vim_strsave_escaped(files[i], (char_u *)" ");
4340 if (p != NULL)
4341 {
4342 vim_free(files[i]);
4343 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004344#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 p = vim_strsave_escaped(files[i], (char_u *)" ");
4346 if (p != NULL)
4347 {
4348 vim_free(files[i]);
4349 files[i] = p;
4350 }
4351#endif
4352 }
4353 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004354#ifdef BACKSLASH_IN_FILENAME
4355 p = vim_strsave_fnameescape(files[i], FALSE);
4356#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004357 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 if (p != NULL)
4360 {
4361 vim_free(files[i]);
4362 files[i] = p;
4363 }
4364
4365 /* If 'str' starts with "\~", replace "~" at start of
4366 * files[i] with "\~". */
4367 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004368 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
4370 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004371
4372 /* If the first file starts with a '+' escape it. Otherwise it
4373 * could be seen as "+cmd". */
4374 if (*files[0] == '+')
4375 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 else if (xp->xp_context == EXPAND_TAGS)
4378 {
4379 /*
4380 * Insert a backslash before characters in a tag name that
4381 * would terminate the ":tag" command.
4382 */
4383 for (i = 0; i < numfiles; ++i)
4384 {
4385 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4386 if (p != NULL)
4387 {
4388 vim_free(files[i]);
4389 files[i] = p;
4390 }
4391 }
4392 }
4393 }
4394}
4395
4396/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004397 * Escape special characters in "fname" for when used as a file name argument
4398 * after a Vim command, or, when "shell" is non-zero, a shell command.
4399 * Returns the result in allocated memory.
4400 */
4401 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004402vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004403{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004404 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004405#ifdef BACKSLASH_IN_FILENAME
4406 char_u buf[20];
4407 int j = 0;
4408
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004409 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004410 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004411 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004412 buf[j++] = *p;
4413 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004414 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004415#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004416 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4417 if (shell && csh_like_shell() && p != NULL)
4418 {
4419 char_u *s;
4420
4421 /* For csh and similar shells need to put two backslashes before '!'.
4422 * One is taken by Vim, one by the shell. */
4423 s = vim_strsave_escaped(p, (char_u *)"!");
4424 vim_free(p);
4425 p = s;
4426 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004427#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004428
4429 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4430 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004431 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004432 escape_fname(&p);
4433
4434 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004435}
4436
4437/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004438 * Put a backslash before the file name in "pp", which is in allocated memory.
4439 */
4440 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004441escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004442{
4443 char_u *p;
4444
4445 p = alloc((unsigned)(STRLEN(*pp) + 2));
4446 if (p != NULL)
4447 {
4448 p[0] = '\\';
4449 STRCPY(p + 1, *pp);
4450 vim_free(*pp);
4451 *pp = p;
4452 }
4453}
4454
4455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 * For each file name in files[num_files]:
4457 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4458 */
4459 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004460tilde_replace(
4461 char_u *orig_pat,
4462 int num_files,
4463 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465 int i;
4466 char_u *p;
4467
4468 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4469 {
4470 for (i = 0; i < num_files; ++i)
4471 {
4472 p = home_replace_save(NULL, files[i]);
4473 if (p != NULL)
4474 {
4475 vim_free(files[i]);
4476 files[i] = p;
4477 }
4478 }
4479 }
4480}
4481
4482/*
4483 * Show all matches for completion on the command line.
4484 * Returns EXPAND_NOTHING when the character that triggered expansion should
4485 * be inserted like a normal character.
4486 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004488showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489{
4490#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4491 int num_files;
4492 char_u **files_found;
4493 int i, j, k;
4494 int maxlen;
4495 int lines;
4496 int columns;
4497 char_u *p;
4498 int lastlen;
4499 int attr;
4500 int showtail;
4501
4502 if (xp->xp_numfiles == -1)
4503 {
4504 set_expand_context(xp);
4505 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4506 &num_files, &files_found);
4507 showtail = expand_showtail(xp);
4508 if (i != EXPAND_OK)
4509 return i;
4510
4511 }
4512 else
4513 {
4514 num_files = xp->xp_numfiles;
4515 files_found = xp->xp_files;
4516 showtail = cmd_showtail;
4517 }
4518
4519#ifdef FEAT_WILDMENU
4520 if (!wildmenu)
4521 {
4522#endif
4523 msg_didany = FALSE; /* lines_left will be set */
4524 msg_start(); /* prepare for paging */
4525 msg_putchar('\n');
4526 out_flush();
4527 cmdline_row = msg_row;
4528 msg_didany = FALSE; /* lines_left will be set again */
4529 msg_start(); /* prepare for paging */
4530#ifdef FEAT_WILDMENU
4531 }
4532#endif
4533
4534 if (got_int)
4535 got_int = FALSE; /* only int. the completion, not the cmd line */
4536#ifdef FEAT_WILDMENU
4537 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004538 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539#endif
4540 else
4541 {
4542 /* find the length of the longest file name */
4543 maxlen = 0;
4544 for (i = 0; i < num_files; ++i)
4545 {
4546 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004547 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 || xp->xp_context == EXPAND_BUFFERS))
4549 {
4550 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4551 j = vim_strsize(NameBuff);
4552 }
4553 else
4554 j = vim_strsize(L_SHOWFILE(i));
4555 if (j > maxlen)
4556 maxlen = j;
4557 }
4558
4559 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4560 lines = num_files;
4561 else
4562 {
4563 /* compute the number of columns and lines for the listing */
4564 maxlen += 2; /* two spaces between file names */
4565 columns = ((int)Columns + 2) / maxlen;
4566 if (columns < 1)
4567 columns = 1;
4568 lines = (num_files + columns - 1) / columns;
4569 }
4570
Bram Moolenaar8820b482017-03-16 17:23:31 +01004571 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572
4573 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4574 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004575 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 msg_clr_eos();
4577 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004578 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580
4581 /* list the files line by line */
4582 for (i = 0; i < lines; ++i)
4583 {
4584 lastlen = 999;
4585 for (k = i; k < num_files; k += lines)
4586 {
4587 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4588 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004589 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 p = files_found[k] + STRLEN(files_found[k]) + 1;
4591 msg_advance(maxlen + 1);
4592 msg_puts(p);
4593 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004594 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 break;
4596 }
4597 for (j = maxlen - lastlen; --j >= 0; )
4598 msg_putchar(' ');
4599 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004600 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 || xp->xp_context == EXPAND_BUFFERS)
4602 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004603 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004604 if (xp->xp_numfiles != -1)
4605 {
4606 char_u *halved_slash;
4607 char_u *exp_path;
4608
4609 /* Expansion was done before and special characters
4610 * were escaped, need to halve backslashes. Also
4611 * $HOME has been replaced with ~/. */
4612 exp_path = expand_env_save_opt(files_found[k], TRUE);
4613 halved_slash = backslash_halve_save(
4614 exp_path != NULL ? exp_path : files_found[k]);
4615 j = mch_isdir(halved_slash != NULL ? halved_slash
4616 : files_found[k]);
4617 vim_free(exp_path);
4618 vim_free(halved_slash);
4619 }
4620 else
4621 /* Expansion was done here, file names are literal. */
4622 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 if (showtail)
4624 p = L_SHOWFILE(k);
4625 else
4626 {
4627 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4628 TRUE);
4629 p = NameBuff;
4630 }
4631 }
4632 else
4633 {
4634 j = FALSE;
4635 p = L_SHOWFILE(k);
4636 }
4637 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4638 }
4639 if (msg_col > 0) /* when not wrapped around */
4640 {
4641 msg_clr_eos();
4642 msg_putchar('\n');
4643 }
4644 out_flush(); /* show one line at a time */
4645 if (got_int)
4646 {
4647 got_int = FALSE;
4648 break;
4649 }
4650 }
4651
4652 /*
4653 * we redraw the command below the lines that we have just listed
4654 * This is a bit tricky, but it saves a lot of screen updating.
4655 */
4656 cmdline_row = msg_row; /* will put it back later */
4657 }
4658
4659 if (xp->xp_numfiles == -1)
4660 FreeWild(num_files, files_found);
4661
4662 return EXPAND_OK;
4663}
4664
4665/*
4666 * Private gettail for showmatches() (and win_redr_status_matches()):
4667 * Find tail of file name path, but ignore trailing "/".
4668 */
4669 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004670sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671{
4672 char_u *p;
4673 char_u *t = s;
4674 int had_sep = FALSE;
4675
4676 for (p = s; *p != NUL; )
4677 {
4678 if (vim_ispathsep(*p)
4679#ifdef BACKSLASH_IN_FILENAME
4680 && !rem_backslash(p)
4681#endif
4682 )
4683 had_sep = TRUE;
4684 else if (had_sep)
4685 {
4686 t = p;
4687 had_sep = FALSE;
4688 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004689 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691 return t;
4692}
4693
4694/*
4695 * Return TRUE if we only need to show the tail of completion matches.
4696 * When not completing file names or there is a wildcard in the path FALSE is
4697 * returned.
4698 */
4699 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004700expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 char_u *s;
4703 char_u *end;
4704
4705 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004706 if (xp->xp_context != EXPAND_FILES
4707 && xp->xp_context != EXPAND_SHELLCMD
4708 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 return FALSE;
4710
4711 end = gettail(xp->xp_pattern);
4712 if (end == xp->xp_pattern) /* there is no path separator */
4713 return FALSE;
4714
4715 for (s = xp->xp_pattern; s < end; s++)
4716 {
4717 /* Skip escaped wildcards. Only when the backslash is not a path
4718 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4719 if (rem_backslash(s))
4720 ++s;
4721 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4722 return FALSE;
4723 }
4724 return TRUE;
4725}
4726
4727/*
4728 * Prepare a string for expansion.
4729 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004730 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 * When expanding other names: The string will be used with regcomp(). Copy
4732 * the name into allocated memory and prepend "^".
4733 */
4734 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004735addstar(
4736 char_u *fname,
4737 int len,
4738 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739{
4740 char_u *retval;
4741 int i, j;
4742 int new_len;
4743 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004744 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004746 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004747 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004748 && context != EXPAND_SHELLCMD
4749 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 /*
4752 * Matching will be done internally (on something other than files).
4753 * So we convert the file-matching-type wildcards into our kind for
4754 * use with vim_regcomp(). First work out how long it will be:
4755 */
4756
4757 /* For help tags the translation is done in find_help_tags().
4758 * For a tag pattern starting with "/" no translation is needed. */
4759 if (context == EXPAND_HELP
4760 || context == EXPAND_COLORS
4761 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004762 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004763 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004764 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004765 || ((context == EXPAND_TAGS_LISTFILES
4766 || context == EXPAND_TAGS)
4767 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 retval = vim_strnsave(fname, len);
4769 else
4770 {
4771 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4772 for (i = 0; i < len; i++)
4773 {
4774 if (fname[i] == '*' || fname[i] == '~')
4775 new_len++; /* '*' needs to be replaced by ".*"
4776 '~' needs to be replaced by "\~" */
4777
4778 /* Buffer names are like file names. "." should be literal */
4779 if (context == EXPAND_BUFFERS && fname[i] == '.')
4780 new_len++; /* "." becomes "\." */
4781
4782 /* Custom expansion takes care of special things, match
4783 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004784 if ((context == EXPAND_USER_DEFINED
4785 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 new_len++; /* '\' becomes "\\" */
4787 }
4788 retval = alloc(new_len);
4789 if (retval != NULL)
4790 {
4791 retval[0] = '^';
4792 j = 1;
4793 for (i = 0; i < len; i++, j++)
4794 {
4795 /* Skip backslash. But why? At least keep it for custom
4796 * expansion. */
4797 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004798 && context != EXPAND_USER_LIST
4799 && fname[i] == '\\'
4800 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 break;
4802
4803 switch (fname[i])
4804 {
4805 case '*': retval[j++] = '.';
4806 break;
4807 case '~': retval[j++] = '\\';
4808 break;
4809 case '?': retval[j] = '.';
4810 continue;
4811 case '.': if (context == EXPAND_BUFFERS)
4812 retval[j++] = '\\';
4813 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004814 case '\\': if (context == EXPAND_USER_DEFINED
4815 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 retval[j++] = '\\';
4817 break;
4818 }
4819 retval[j] = fname[i];
4820 }
4821 retval[j] = NUL;
4822 }
4823 }
4824 }
4825 else
4826 {
4827 retval = alloc(len + 4);
4828 if (retval != NULL)
4829 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004830 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831
4832 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004833 * Don't add a star to *, ~, ~user, $var or `cmd`.
4834 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 * ~ would be at the start of the file name, but not the tail.
4836 * $ could be anywhere in the tail.
4837 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004838 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004841 ends_in_star = (len > 0 && retval[len - 1] == '*');
4842#ifndef BACKSLASH_IN_FILENAME
4843 for (i = len - 2; i >= 0; --i)
4844 {
4845 if (retval[i] != '\\')
4846 break;
4847 ends_in_star = !ends_in_star;
4848 }
4849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004851 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 && vim_strchr(tail, '$') == NULL
4853 && vim_strchr(retval, '`') == NULL)
4854 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004855 else if (len > 0 && retval[len - 1] == '$')
4856 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 retval[len] = NUL;
4858 }
4859 }
4860 return retval;
4861}
4862
4863/*
4864 * Must parse the command line so far to work out what context we are in.
4865 * Completion can then be done based on that context.
4866 * This routine sets the variables:
4867 * xp->xp_pattern The start of the pattern to be expanded within
4868 * the command line (ends at the cursor).
4869 * xp->xp_context The type of thing to expand. Will be one of:
4870 *
4871 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4872 * the command line, like an unknown command. Caller
4873 * should beep.
4874 * EXPAND_NOTHING Unrecognised context for completion, use char like
4875 * a normal char, rather than for completion. eg
4876 * :s/^I/
4877 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4878 * it.
4879 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4880 * EXPAND_FILES After command with XFILE set, or after setting
4881 * with P_EXPAND set. eg :e ^I, :w>>^I
4882 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4883 * when we know only directories are of interest. eg
4884 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004885 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4887 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4888 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4889 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4890 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4891 * EXPAND_EVENTS Complete event names
4892 * EXPAND_SYNTAX Complete :syntax command arguments
4893 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4894 * EXPAND_AUGROUP Complete autocommand group names
4895 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4896 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4897 * eg :unmap a^I , :cunab x^I
4898 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4899 * eg :call sub^I
4900 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4901 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4902 * names in expressions, eg :while s^I
4903 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004904 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 */
4906 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004907set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004909 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (ccline.cmdfirstc != ':'
4911#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004912 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004913 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#endif
4915 )
4916 {
4917 xp->xp_context = EXPAND_NOTHING;
4918 return;
4919 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004920 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921}
4922
4923 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004924set_cmd_context(
4925 expand_T *xp,
4926 char_u *str, /* start of command line */
4927 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004928 int col, /* position of cursor */
4929 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930{
4931 int old_char = NUL;
4932 char_u *nextcomm;
4933
4934 /*
4935 * Avoid a UMR warning from Purify, only save the character if it has been
4936 * written before.
4937 */
4938 if (col < len)
4939 old_char = str[col];
4940 str[col] = NUL;
4941 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004942
4943#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004944 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004945 {
4946# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004947 /* pass CMD_SIZE because there is no real command */
4948 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004949# endif
4950 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004951 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004952 {
4953 xp->xp_context = ccline.xp_context;
4954 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004955# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004956 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004957# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004958 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004959 else
4960#endif
4961 while (nextcomm != NULL)
4962 nextcomm = set_one_cmd_context(xp, nextcomm);
4963
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004964 /* Store the string here so that call_user_expand_func() can get to them
4965 * easily. */
4966 xp->xp_line = str;
4967 xp->xp_col = col;
4968
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 str[col] = old_char;
4970}
4971
4972/*
4973 * Expand the command line "str" from context "xp".
4974 * "xp" must have been set by set_cmd_context().
4975 * xp->xp_pattern points into "str", to where the text that is to be expanded
4976 * starts.
4977 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4978 * cursor.
4979 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4980 * key that triggered expansion literally.
4981 * Returns EXPAND_OK otherwise.
4982 */
4983 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004984expand_cmdline(
4985 expand_T *xp,
4986 char_u *str, /* start of command line */
4987 int col, /* position of cursor */
4988 int *matchcount, /* return: nr of matches */
4989 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990{
4991 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004992 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993
4994 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4995 {
4996 beep_flush();
4997 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4998 }
4999 if (xp->xp_context == EXPAND_NOTHING)
5000 {
5001 /* Caller can use the character as a normal char instead */
5002 return EXPAND_NOTHING;
5003 }
5004
5005 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005006 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5007 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 if (file_str == NULL)
5009 return EXPAND_UNSUCCESSFUL;
5010
Bram Moolenaar94950a92010-12-02 16:01:29 +01005011 if (p_wic)
5012 options += WILD_ICASE;
5013
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005015 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 *matchcount = 0;
5018 *matches = NULL;
5019 }
5020 vim_free(file_str);
5021
5022 return EXPAND_OK;
5023}
5024
5025#ifdef FEAT_MULTI_LANG
5026/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005027 * Cleanup matches for help tags:
5028 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5029 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005031static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005034cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
5036 int i, j;
5037 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005038 char_u buf[4];
5039 char_u *p = buf;
5040
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005041 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005042 {
5043 *p++ = '@';
5044 *p++ = p_hlg[0];
5045 *p++ = p_hlg[1];
5046 }
5047 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048
5049 for (i = 0; i < num_file; ++i)
5050 {
5051 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005052 if (len <= 0)
5053 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005054 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 {
5056 /* Sorting on priority means the same item in another language may
5057 * be anywhere. Search all items for a match up to the "@en". */
5058 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005059 if (j != i && (int)STRLEN(file[j]) == len + 3
5060 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 break;
5062 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005063 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 file[i][len] = NUL;
5065 }
5066 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005067
5068 if (*buf != NUL)
5069 for (i = 0; i < num_file; ++i)
5070 {
5071 len = (int)STRLEN(file[i]) - 3;
5072 if (len <= 0)
5073 continue;
5074 if (STRCMP(file[i] + len, buf) == 0)
5075 {
5076 /* remove the default language */
5077 file[i][len] = NUL;
5078 }
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080}
5081#endif
5082
5083/*
5084 * Do the expansion based on xp->xp_context and "pat".
5085 */
5086 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005087ExpandFromContext(
5088 expand_T *xp,
5089 char_u *pat,
5090 int *num_file,
5091 char_u ***file,
5092 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093{
5094#ifdef FEAT_CMDL_COMPL
5095 regmatch_T regmatch;
5096#endif
5097 int ret;
5098 int flags;
5099
5100 flags = EW_DIR; /* include directories */
5101 if (options & WILD_LIST_NOTFOUND)
5102 flags |= EW_NOTFOUND;
5103 if (options & WILD_ADD_SLASH)
5104 flags |= EW_ADDSLASH;
5105 if (options & WILD_KEEP_ALL)
5106 flags |= EW_KEEPALL;
5107 if (options & WILD_SILENT)
5108 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005109 if (options & WILD_ALLLINKS)
5110 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005112 if (xp->xp_context == EXPAND_FILES
5113 || xp->xp_context == EXPAND_DIRECTORIES
5114 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 /*
5117 * Expand file or directory names.
5118 */
5119 int free_pat = FALSE;
5120 int i;
5121
5122 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5123 * space */
5124 if (xp->xp_backslash != XP_BS_NONE)
5125 {
5126 free_pat = TRUE;
5127 pat = vim_strsave(pat);
5128 for (i = 0; pat[i]; ++i)
5129 if (pat[i] == '\\')
5130 {
5131 if (xp->xp_backslash == XP_BS_THREE
5132 && pat[i + 1] == '\\'
5133 && pat[i + 2] == '\\'
5134 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005135 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (xp->xp_backslash == XP_BS_ONE
5137 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005138 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 }
5140 }
5141
5142 if (xp->xp_context == EXPAND_FILES)
5143 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005144 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5145 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else
5147 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005148 if (options & WILD_ICASE)
5149 flags |= EW_ICASE;
5150
Bram Moolenaard7834d32009-12-02 16:14:36 +00005151 /* Expand wildcards, supporting %:h and the like. */
5152 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 if (free_pat)
5154 vim_free(pat);
5155 return ret;
5156 }
5157
5158 *file = (char_u **)"";
5159 *num_file = 0;
5160 if (xp->xp_context == EXPAND_HELP)
5161 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005162 /* With an empty argument we would get all the help tags, which is
5163 * very slow. Get matches for "help" instead. */
5164 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5165 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
5167#ifdef FEAT_MULTI_LANG
5168 cleanup_help_tags(*num_file, *file);
5169#endif
5170 return OK;
5171 }
5172 return FAIL;
5173 }
5174
5175#ifndef FEAT_CMDL_COMPL
5176 return FAIL;
5177#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005178 if (xp->xp_context == EXPAND_SHELLCMD)
5179 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 if (xp->xp_context == EXPAND_OLD_SETTING)
5181 return ExpandOldSetting(num_file, file);
5182 if (xp->xp_context == EXPAND_BUFFERS)
5183 return ExpandBufnames(pat, num_file, file, options);
5184 if (xp->xp_context == EXPAND_TAGS
5185 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5186 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5187 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005188 {
5189 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005190 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5191 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005194 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005195 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005196 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005197 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005198 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005199 {
5200 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005201 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005202 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005203 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005204 {
5205 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005206 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005207 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005208# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5209 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005210 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005211# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005212 if (xp->xp_context == EXPAND_PACKADD)
5213 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214
5215 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5216 if (regmatch.regprog == NULL)
5217 return FAIL;
5218
5219 /* set ignore-case according to p_ic, p_scs and pat */
5220 regmatch.rm_ic = ignorecase(pat);
5221
5222 if (xp->xp_context == EXPAND_SETTINGS
5223 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5224 ret = ExpandSettings(xp, &regmatch, num_file, file);
5225 else if (xp->xp_context == EXPAND_MAPPINGS)
5226 ret = ExpandMappings(&regmatch, num_file, file);
5227# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5228 else if (xp->xp_context == EXPAND_USER_DEFINED)
5229 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5230# endif
5231 else
5232 {
5233 static struct expgen
5234 {
5235 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005236 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005238 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 } tab[] =
5240 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005241 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5242 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005243 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005244 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005245#ifdef FEAT_CMDHIST
5246 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5247#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005249 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005250 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005251 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5252 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5253 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#endif
5255#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005256 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5257 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5258 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5259 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
5261#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005262 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5263 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264#endif
5265#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005266 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005268#ifdef FEAT_PROFILE
5269 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5270#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005271 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005272 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5273 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005274#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005275 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005276#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005277#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005278 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005279#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005280#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005281 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5284 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005285 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5286 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005288 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005289 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005290 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 };
5292 int i;
5293
5294 /*
5295 * Find a context in the table and call the ExpandGeneric() with the
5296 * right function to do the expansion.
5297 */
5298 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005299 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (xp->xp_context == tab[i].context)
5301 {
5302 if (tab[i].ic)
5303 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005304 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005305 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 break;
5307 }
5308 }
5309
Bram Moolenaar473de612013-06-08 18:19:48 +02005310 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311
5312 return ret;
5313#endif /* FEAT_CMDL_COMPL */
5314}
5315
5316#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5317/*
5318 * Expand a list of names.
5319 *
5320 * Generic function for command line completion. It calls a function to
5321 * obtain strings, one by one. The strings are matched against a regexp
5322 * program. Matching strings are copied into an array, which is returned.
5323 *
5324 * Returns OK when no problems encountered, FAIL for error (out of memory).
5325 */
5326 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005327ExpandGeneric(
5328 expand_T *xp,
5329 regmatch_T *regmatch,
5330 int *num_file,
5331 char_u ***file,
5332 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
5336 int i;
5337 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005338 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 char_u *str;
5340
5341 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005342 * round == 0: count the number of matching names
5343 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005345 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 for (i = 0; ; ++i)
5348 {
5349 str = (*func)(xp, i);
5350 if (str == NULL) /* end of list */
5351 break;
5352 if (*str == NUL) /* skip empty strings */
5353 continue;
5354
5355 if (vim_regexec(regmatch, str, (colnr_T)0))
5356 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005357 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005359 if (escaped)
5360 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5361 else
5362 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 (*file)[count] = str;
5364#ifdef FEAT_MENU
5365 if (func == get_menu_names && str != NULL)
5366 {
5367 /* test for separator added by get_menu_names() */
5368 str += STRLEN(str) - 1;
5369 if (*str == '\001')
5370 *str = '.';
5371 }
5372#endif
5373 }
5374 ++count;
5375 }
5376 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005377 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 {
5379 if (count == 0)
5380 return OK;
5381 *num_file = count;
5382 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5383 if (*file == NULL)
5384 {
5385 *file = (char_u **)"";
5386 return FAIL;
5387 }
5388 count = 0;
5389 }
5390 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005391
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005392 /* Sort the results. Keep menu's in the specified order. */
5393 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005394 {
5395 if (xp->xp_context == EXPAND_EXPRESSION
5396 || xp->xp_context == EXPAND_FUNCTIONS
5397 || xp->xp_context == EXPAND_USER_FUNC)
5398 /* <SNR> functions should be sorted to the end. */
5399 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5400 sort_func_compare);
5401 else
5402 sort_strings(*file, *num_file);
5403 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005404
Bram Moolenaar4f688582007-07-24 12:34:30 +00005405#ifdef FEAT_CMDL_COMPL
5406 /* Reset the variables used for special highlight names expansion, so that
5407 * they don't show up when getting normal highlight names by ID. */
5408 reset_expand_highlight();
5409#endif
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 return OK;
5412}
5413
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005414/*
5415 * Complete a shell command.
5416 * Returns FAIL or OK;
5417 */
5418 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005419expand_shellcmd(
5420 char_u *filepat, /* pattern to match with command names */
5421 int *num_file, /* return: number of matches */
5422 char_u ***file, /* return: array with matches */
5423 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005424{
5425 char_u *pat;
5426 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005427 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005428 int mustfree = FALSE;
5429 garray_T ga;
5430 char_u *buf = alloc(MAXPATHL);
5431 size_t l;
5432 char_u *s, *e;
5433 int flags = flagsarg;
5434 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005435 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005436 hashtab_T found_ht;
5437 hashitem_T *hi;
5438 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005439
5440 if (buf == NULL)
5441 return FAIL;
5442
5443 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5444 * space */
5445 pat = vim_strsave(filepat);
5446 for (i = 0; pat[i]; ++i)
5447 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005448 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005449
Bram Moolenaarb5971142015-03-21 17:32:19 +01005450 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005451
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005452 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5453 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005454 path = (char_u *)".";
5455 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005456 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005457 /* For an absolute name we don't use $PATH. */
5458 if (!mch_isFullName(pat))
5459 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005460 if (path == NULL)
5461 path = (char_u *)"";
5462 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005463
5464 /*
5465 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005466 * collect them in "ga". When "." is not in $PATH also expand for the
5467 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005468 */
5469 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005470 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005471 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005472 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005473#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005474 e = vim_strchr(s, ';');
5475#else
5476 e = vim_strchr(s, ':');
5477#endif
5478 if (e == NULL)
5479 e = s + STRLEN(s);
5480
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005481 if (*s == NUL)
5482 {
5483 if (did_curdir)
5484 break;
5485 // Find directories in the current directory, path is empty.
5486 did_curdir = TRUE;
5487 flags |= EW_DIR;
5488 }
5489 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5490 {
5491 did_curdir = TRUE;
5492 flags |= EW_DIR;
5493 }
5494 else
5495 // Do not match directories inside a $PATH item.
5496 flags &= ~EW_DIR;
5497
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005498 l = e - s;
5499 if (l > MAXPATHL - 5)
5500 break;
5501 vim_strncpy(buf, s, l);
5502 add_pathsep(buf);
5503 l = STRLEN(buf);
5504 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5505
5506 /* Expand matches in one directory of $PATH. */
5507 ret = expand_wildcards(1, &buf, num_file, file, flags);
5508 if (ret == OK)
5509 {
5510 if (ga_grow(&ga, *num_file) == FAIL)
5511 FreeWild(*num_file, *file);
5512 else
5513 {
5514 for (i = 0; i < *num_file; ++i)
5515 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005516 char_u *name = (*file)[i];
5517
5518 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005519 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005520 // Check if this name was already found.
5521 hash = hash_hash(name + l);
5522 hi = hash_lookup(&found_ht, name + l, hash);
5523 if (HASHITEM_EMPTY(hi))
5524 {
5525 // Remove the path that was prepended.
5526 STRMOVE(name, name + l);
5527 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5528 hash_add_item(&found_ht, hi, name, hash);
5529 name = NULL;
5530 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005531 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005532 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005533 }
5534 vim_free(*file);
5535 }
5536 }
5537 if (*e != NUL)
5538 ++e;
5539 }
5540 *file = ga.ga_data;
5541 *num_file = ga.ga_len;
5542
5543 vim_free(buf);
5544 vim_free(pat);
5545 if (mustfree)
5546 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005547 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005548 return OK;
5549}
5550
5551
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5553/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005554 * Call "user_expand_func()" to invoke a user defined Vim script function and
5555 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005557 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005558call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005559 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560 expand_T *xp,
5561 int *num_file,
5562 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005564 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005565 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005567 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005568 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005569 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
Bram Moolenaarb7515462013-06-29 12:58:33 +02005571 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005572 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 *num_file = 0;
5574 *file = NULL;
5575
Bram Moolenaarb7515462013-06-29 12:58:33 +02005576 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005577 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005578 keep = ccline.cmdbuff[ccline.cmdlen];
5579 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005580 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005581
Bram Moolenaarffa96842018-06-12 22:05:14 +02005582 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5583
5584 args[0].v_type = VAR_STRING;
5585 args[0].vval.v_string = pat;
5586 args[1].v_type = VAR_STRING;
5587 args[1].vval.v_string = xp->xp_line;
5588 args[2].v_type = VAR_NUMBER;
5589 args[2].vval.v_number = xp->xp_col;
5590 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005592 /* Save the cmdline, we don't know what the function may do. */
5593 save_ccline = ccline;
5594 ccline.cmdbuff = NULL;
5595 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005597
Bram Moolenaarded27a12018-08-01 19:06:03 +02005598 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005599
5600 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005602 if (ccline.cmdbuff != NULL)
5603 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005604
Bram Moolenaarffa96842018-06-12 22:05:14 +02005605 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005606 return ret;
5607}
5608
5609/*
5610 * Expand names with a function defined by the user.
5611 */
5612 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005613ExpandUserDefined(
5614 expand_T *xp,
5615 regmatch_T *regmatch,
5616 int *num_file,
5617 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005618{
5619 char_u *retstr;
5620 char_u *s;
5621 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005622 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005623 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005624 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005625
5626 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5627 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 return FAIL;
5629
5630 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005631 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
5633 e = vim_strchr(s, '\n');
5634 if (e == NULL)
5635 e = s + STRLEN(s);
5636 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005637 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005639 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5640 *e = keep;
5641
5642 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005644 if (ga_grow(&ga, 1) == FAIL)
5645 break;
5646 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5647 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
5649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 if (*e != NUL)
5651 ++e;
5652 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005653 vim_free(retstr);
5654 *file = ga.ga_data;
5655 *num_file = ga.ga_len;
5656 return OK;
5657}
5658
5659/*
5660 * Expand names with a list returned by a function defined by the user.
5661 */
5662 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005663ExpandUserList(
5664 expand_T *xp,
5665 int *num_file,
5666 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005667{
5668 list_T *retlist;
5669 listitem_T *li;
5670 garray_T ga;
5671
5672 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5673 if (retlist == NULL)
5674 return FAIL;
5675
5676 ga_init2(&ga, (int)sizeof(char *), 3);
5677 /* Loop over the items in the list. */
5678 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5679 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005680 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5681 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005682
5683 if (ga_grow(&ga, 1) == FAIL)
5684 break;
5685
5686 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005687 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005688 ++ga.ga_len;
5689 }
5690 list_unref(retlist);
5691
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 *file = ga.ga_data;
5693 *num_file = ga.ga_len;
5694 return OK;
5695}
5696#endif
5697
5698/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005699 * Expand color scheme, compiler or filetype names.
5700 * Search from 'runtimepath':
5701 * 'runtimepath'/{dirnames}/{pat}.vim
5702 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5703 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5704 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5705 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005706 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 */
5708 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005709ExpandRTDir(
5710 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005711 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005712 int *num_file,
5713 char_u ***file,
5714 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 char_u *s;
5717 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005718 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005720 int i;
5721 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722
5723 *num_file = 0;
5724 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005725 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005726 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005728 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005730 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5731 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005733 ga_clear_strings(&ga);
5734 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005736 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005737 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005738 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005740
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005741 if (flags & DIP_START) {
5742 for (i = 0; dirnames[i] != NULL; ++i)
5743 {
5744 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5745 if (s == NULL)
5746 {
5747 ga_clear_strings(&ga);
5748 return FAIL;
5749 }
5750 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5751 globpath(p_pp, s, &ga, 0);
5752 vim_free(s);
5753 }
5754 }
5755
5756 if (flags & DIP_OPT) {
5757 for (i = 0; dirnames[i] != NULL; ++i)
5758 {
5759 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5760 if (s == NULL)
5761 {
5762 ga_clear_strings(&ga);
5763 return FAIL;
5764 }
5765 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5766 globpath(p_pp, s, &ga, 0);
5767 vim_free(s);
5768 }
5769 }
5770
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005771 for (i = 0; i < ga.ga_len; ++i)
5772 {
5773 match = ((char_u **)ga.ga_data)[i];
5774 s = match;
5775 e = s + STRLEN(s);
5776 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5777 {
5778 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005779 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005780 if (s < match || vim_ispathsep(*s))
5781 break;
5782 ++s;
5783 *e = NUL;
5784 mch_memmove(match, s, e - s + 1);
5785 }
5786 }
5787
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005788 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005789 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005790
5791 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005792 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005793 remove_duplicates(&ga);
5794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 *file = ga.ga_data;
5796 *num_file = ga.ga_len;
5797 return OK;
5798}
5799
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005800/*
5801 * Expand loadplugin names:
5802 * 'packpath'/pack/ * /opt/{pat}
5803 */
5804 static int
5805ExpandPackAddDir(
5806 char_u *pat,
5807 int *num_file,
5808 char_u ***file)
5809{
5810 char_u *s;
5811 char_u *e;
5812 char_u *match;
5813 garray_T ga;
5814 int i;
5815 int pat_len;
5816
5817 *num_file = 0;
5818 *file = NULL;
5819 pat_len = (int)STRLEN(pat);
5820 ga_init2(&ga, (int)sizeof(char *), 10);
5821
5822 s = alloc((unsigned)(pat_len + 26));
5823 if (s == NULL)
5824 {
5825 ga_clear_strings(&ga);
5826 return FAIL;
5827 }
5828 sprintf((char *)s, "pack/*/opt/%s*", pat);
5829 globpath(p_pp, s, &ga, 0);
5830 vim_free(s);
5831
5832 for (i = 0; i < ga.ga_len; ++i)
5833 {
5834 match = ((char_u **)ga.ga_data)[i];
5835 s = gettail(match);
5836 e = s + STRLEN(s);
5837 mch_memmove(match, s, e - s + 1);
5838 }
5839
5840 if (ga.ga_len == 0)
5841 return FAIL;
5842
5843 /* Sort and remove duplicates which can happen when specifying multiple
5844 * directories in dirnames. */
5845 remove_duplicates(&ga);
5846
5847 *file = ga.ga_data;
5848 *num_file = ga.ga_len;
5849 return OK;
5850}
5851
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852#endif
5853
5854#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5855/*
5856 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005857 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005859 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005860globpath(
5861 char_u *path,
5862 char_u *file,
5863 garray_T *ga,
5864 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865{
5866 expand_T xpc;
5867 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 int num_p;
5870 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871
5872 buf = alloc(MAXPATHL);
5873 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005874 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005876 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 /* Loop over all entries in {path}. */
5880 while (*path != NUL)
5881 {
5882 /* Copy one item of the path to buf[] and concatenate the file name. */
5883 copy_option_part(&path, buf, MAXPATHL, ",");
5884 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5885 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005886# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005887 /* Using the platform's path separator (\) makes vim incorrectly
5888 * treat it as an escape character, use '/' instead. */
5889 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5890 STRCAT(buf, "/");
5891# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005895 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5896 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005898 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005900 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 for (i = 0; i < num_p; ++i)
5903 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005904 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005905 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005906 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 FreeWild(num_p, p);
5911 }
5912 }
5913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914
5915 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916}
5917
5918#endif
5919
5920#if defined(FEAT_CMDHIST) || defined(PROTO)
5921
5922/*********************************
5923 * Command line history stuff *
5924 *********************************/
5925
5926/*
5927 * Translate a history character to the associated type number.
5928 */
5929 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005930hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
5932 if (c == ':')
5933 return HIST_CMD;
5934 if (c == '=')
5935 return HIST_EXPR;
5936 if (c == '@')
5937 return HIST_INPUT;
5938 if (c == '>')
5939 return HIST_DEBUG;
5940 return HIST_SEARCH; /* must be '?' or '/' */
5941}
5942
5943/*
5944 * Table of history names.
5945 * These names are used in :history and various hist...() functions.
5946 * It is sufficient to give the significant prefix of a history name.
5947 */
5948
5949static char *(history_names[]) =
5950{
5951 "cmd",
5952 "search",
5953 "expr",
5954 "input",
5955 "debug",
5956 NULL
5957};
5958
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5960/*
5961 * Function given to ExpandGeneric() to obtain the possible first
5962 * arguments of the ":history command.
5963 */
5964 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005966{
5967 static char_u compl[2] = { NUL, NUL };
5968 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005969 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005970 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5971
5972 if (idx < short_names_count)
5973 {
5974 compl[0] = (char_u)short_names[idx];
5975 return compl;
5976 }
5977 if (idx < short_names_count + history_name_count)
5978 return (char_u *)history_names[idx - short_names_count];
5979 if (idx == short_names_count + history_name_count)
5980 return (char_u *)"all";
5981 return NULL;
5982}
5983#endif
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985/*
5986 * init_history() - Initialize the command line history.
5987 * Also used to re-allocate the history when the size changes.
5988 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005989 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005990init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991{
5992 int newlen; /* new length of history table */
5993 histentry_T *temp;
5994 int i;
5995 int j;
5996 int type;
5997
5998 /*
5999 * If size of history table changed, reallocate it
6000 */
6001 newlen = (int)p_hi;
6002 if (newlen != hislen) /* history length changed */
6003 {
6004 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6005 {
6006 if (newlen)
6007 {
6008 temp = (histentry_T *)lalloc(
6009 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6010 if (temp == NULL) /* out of memory! */
6011 {
6012 if (type == 0) /* first one: just keep the old length */
6013 {
6014 newlen = hislen;
6015 break;
6016 }
6017 /* Already changed one table, now we can only have zero
6018 * length for all tables. */
6019 newlen = 0;
6020 type = -1;
6021 continue;
6022 }
6023 }
6024 else
6025 temp = NULL;
6026 if (newlen == 0 || temp != NULL)
6027 {
6028 if (hisidx[type] < 0) /* there are no entries yet */
6029 {
6030 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006031 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006032 }
6033 else if (newlen > hislen) /* array becomes bigger */
6034 {
6035 for (i = 0; i <= hisidx[type]; ++i)
6036 temp[i] = history[type][i];
6037 j = i;
6038 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006039 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 for ( ; j < hislen; ++i, ++j)
6041 temp[i] = history[type][j];
6042 }
6043 else /* array becomes smaller or 0 */
6044 {
6045 j = hisidx[type];
6046 for (i = newlen - 1; ; --i)
6047 {
6048 if (i >= 0) /* copy newest entries */
6049 temp[i] = history[type][j];
6050 else /* remove older entries */
6051 vim_free(history[type][j].hisstr);
6052 if (--j < 0)
6053 j = hislen - 1;
6054 if (j == hisidx[type])
6055 break;
6056 }
6057 hisidx[type] = newlen - 1;
6058 }
6059 vim_free(history[type]);
6060 history[type] = temp;
6061 }
6062 }
6063 hislen = newlen;
6064 }
6065}
6066
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006067 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006068clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006069{
6070 hisptr->hisnum = 0;
6071 hisptr->viminfo = FALSE;
6072 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006073 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006074}
6075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076/*
6077 * Check if command line 'str' is already in history.
6078 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6079 */
6080 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006081in_history(
6082 int type,
6083 char_u *str,
6084 int move_to_front, /* Move the entry to the front if it exists */
6085 int sep,
6086 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087{
6088 int i;
6089 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006090 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
6092 if (hisidx[type] < 0)
6093 return FALSE;
6094 i = hisidx[type];
6095 do
6096 {
6097 if (history[type][i].hisstr == NULL)
6098 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006099
6100 /* For search history, check that the separator character matches as
6101 * well. */
6102 p = history[type][i].hisstr;
6103 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006104 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006105 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 {
6107 if (!move_to_front)
6108 return TRUE;
6109 last_i = i;
6110 break;
6111 }
6112 if (--i < 0)
6113 i = hislen - 1;
6114 } while (i != hisidx[type]);
6115
6116 if (last_i >= 0)
6117 {
6118 str = history[type][i].hisstr;
6119 while (i != hisidx[type])
6120 {
6121 if (++i >= hislen)
6122 i = 0;
6123 history[type][last_i] = history[type][i];
6124 last_i = i;
6125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006127 history[type][i].viminfo = FALSE;
6128 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006129 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006130 return TRUE;
6131 }
6132 return FALSE;
6133}
6134
6135/*
6136 * Convert history name (from table above) to its HIST_ equivalent.
6137 * When "name" is empty, return "cmd" history.
6138 * Returns -1 for unknown history name.
6139 */
6140 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006141get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
6143 int i;
6144 int len = (int)STRLEN(name);
6145
6146 /* No argument: use current history. */
6147 if (len == 0)
6148 return hist_char2type(ccline.cmdfirstc);
6149
6150 for (i = 0; history_names[i] != NULL; ++i)
6151 if (STRNICMP(name, history_names[i], len) == 0)
6152 return i;
6153
6154 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6155 return hist_char2type(name[0]);
6156
6157 return -1;
6158}
6159
6160static int last_maptick = -1; /* last seen maptick */
6161
6162/*
6163 * Add the given string to the given history. If the string is already in the
6164 * history then it is moved to the front. "histype" may be one of he HIST_
6165 * values.
6166 */
6167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168add_to_history(
6169 int histype,
6170 char_u *new_entry,
6171 int in_map, /* consider maptick when inside a mapping */
6172 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173{
6174 histentry_T *hisptr;
6175 int len;
6176
6177 if (hislen == 0) /* no history */
6178 return;
6179
Bram Moolenaara939e432013-11-09 05:30:26 +01006180 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6181 return;
6182
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 /*
6184 * Searches inside the same mapping overwrite each other, so that only
6185 * the last line is kept. Be careful not to remove a line that was moved
6186 * down, only lines that were added.
6187 */
6188 if (histype == HIST_SEARCH && in_map)
6189 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006190 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 {
6192 /* Current line is from the same mapping, remove it */
6193 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6194 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006195 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 --hisnum[histype];
6197 if (--hisidx[HIST_SEARCH] < 0)
6198 hisidx[HIST_SEARCH] = hislen - 1;
6199 }
6200 last_maptick = -1;
6201 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006202 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 {
6204 if (++hisidx[histype] == hislen)
6205 hisidx[histype] = 0;
6206 hisptr = &history[histype][hisidx[histype]];
6207 vim_free(hisptr->hisstr);
6208
6209 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006210 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6212 if (hisptr->hisstr != NULL)
6213 hisptr->hisstr[len + 1] = sep;
6214
6215 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006216 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006217 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 if (histype == HIST_SEARCH && in_map)
6219 last_maptick = maptick;
6220 }
6221}
6222
6223#if defined(FEAT_EVAL) || defined(PROTO)
6224
6225/*
6226 * Get identifier of newest history entry.
6227 * "histype" may be one of the HIST_ values.
6228 */
6229 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006230get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6233 || hisidx[histype] < 0)
6234 return -1;
6235
6236 return history[histype][hisidx[histype]].hisnum;
6237}
6238
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006240 * Calculate history index from a number:
6241 * num > 0: seen as identifying number of a history entry
6242 * num < 0: relative position in history wrt newest entry
6243 * "histype" may be one of the HIST_ values.
6244 */
6245 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 int i;
6249 histentry_T *hist;
6250 int wrapped = FALSE;
6251
6252 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6253 || (i = hisidx[histype]) < 0 || num == 0)
6254 return -1;
6255
6256 hist = history[histype];
6257 if (num > 0)
6258 {
6259 while (hist[i].hisnum > num)
6260 if (--i < 0)
6261 {
6262 if (wrapped)
6263 break;
6264 i += hislen;
6265 wrapped = TRUE;
6266 }
6267 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6268 return i;
6269 }
6270 else if (-num <= hislen)
6271 {
6272 i += num + 1;
6273 if (i < 0)
6274 i += hislen;
6275 if (hist[i].hisstr != NULL)
6276 return i;
6277 }
6278 return -1;
6279}
6280
6281/*
6282 * Get a history entry by its index.
6283 * "histype" may be one of the HIST_ values.
6284 */
6285 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006286get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006287{
6288 idx = calc_hist_idx(histype, idx);
6289 if (idx >= 0)
6290 return history[histype][idx].hisstr;
6291 else
6292 return (char_u *)"";
6293}
6294
6295/*
6296 * Clear all entries of a history.
6297 * "histype" may be one of the HIST_ values.
6298 */
6299 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006300clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301{
6302 int i;
6303 histentry_T *hisptr;
6304
6305 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6306 {
6307 hisptr = history[histype];
6308 for (i = hislen; i--;)
6309 {
6310 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006311 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006312 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 }
6314 hisidx[histype] = -1; /* mark history as cleared */
6315 hisnum[histype] = 0; /* reset identifier counter */
6316 return OK;
6317 }
6318 return FAIL;
6319}
6320
6321/*
6322 * Remove all entries matching {str} from a history.
6323 * "histype" may be one of the HIST_ values.
6324 */
6325 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006326del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327{
6328 regmatch_T regmatch;
6329 histentry_T *hisptr;
6330 int idx;
6331 int i;
6332 int last;
6333 int found = FALSE;
6334
6335 regmatch.regprog = NULL;
6336 regmatch.rm_ic = FALSE; /* always match case */
6337 if (hislen != 0
6338 && histype >= 0
6339 && histype < HIST_COUNT
6340 && *str != NUL
6341 && (idx = hisidx[histype]) >= 0
6342 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6343 != NULL)
6344 {
6345 i = last = idx;
6346 do
6347 {
6348 hisptr = &history[histype][i];
6349 if (hisptr->hisstr == NULL)
6350 break;
6351 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6352 {
6353 found = TRUE;
6354 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006355 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 }
6357 else
6358 {
6359 if (i != last)
6360 {
6361 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006362 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 }
6364 if (--last < 0)
6365 last += hislen;
6366 }
6367 if (--i < 0)
6368 i += hislen;
6369 } while (i != idx);
6370 if (history[histype][idx].hisstr == NULL)
6371 hisidx[histype] = -1;
6372 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006373 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 return found;
6375}
6376
6377/*
6378 * Remove an indexed entry from a history.
6379 * "histype" may be one of the HIST_ values.
6380 */
6381 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006382del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 int i, j;
6385
6386 i = calc_hist_idx(histype, idx);
6387 if (i < 0)
6388 return FALSE;
6389 idx = hisidx[histype];
6390 vim_free(history[histype][i].hisstr);
6391
6392 /* When deleting the last added search string in a mapping, reset
6393 * last_maptick, so that the last added search string isn't deleted again.
6394 */
6395 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6396 last_maptick = -1;
6397
6398 while (i != idx)
6399 {
6400 j = (i + 1) % hislen;
6401 history[histype][i] = history[histype][j];
6402 i = j;
6403 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006404 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405 if (--i < 0)
6406 i += hislen;
6407 hisidx[histype] = i;
6408 return TRUE;
6409}
6410
6411#endif /* FEAT_EVAL */
6412
6413#if defined(FEAT_CRYPT) || defined(PROTO)
6414/*
6415 * Very specific function to remove the value in ":set key=val" from the
6416 * history.
6417 */
6418 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006419remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420{
6421 char_u *p;
6422 int i;
6423
6424 i = hisidx[HIST_CMD];
6425 if (i < 0)
6426 return;
6427 p = history[HIST_CMD][i].hisstr;
6428 if (p != NULL)
6429 for ( ; *p; ++p)
6430 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6431 {
6432 p = vim_strchr(p + 3, '=');
6433 if (p == NULL)
6434 break;
6435 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006436 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006437 if (p[i] == '\\' && p[i + 1])
6438 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006439 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 --p;
6441 }
6442}
6443#endif
6444
6445#endif /* FEAT_CMDHIST */
6446
Bram Moolenaar064154c2016-03-19 22:50:43 +01006447#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006448/*
6449 * Get pointer to the command line info to use. cmdline_paste() may clear
6450 * ccline and put the previous value in prev_ccline.
6451 */
6452 static struct cmdline_info *
6453get_ccline_ptr(void)
6454{
6455 if ((State & CMDLINE) == 0)
6456 return NULL;
6457 if (ccline.cmdbuff != NULL)
6458 return &ccline;
6459 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6460 return &prev_ccline;
6461 return NULL;
6462}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006463#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006464
Bram Moolenaar064154c2016-03-19 22:50:43 +01006465#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006466/*
6467 * Get the current command line in allocated memory.
6468 * Only works when the command line is being edited.
6469 * Returns NULL when something is wrong.
6470 */
6471 char_u *
6472get_cmdline_str(void)
6473{
6474 struct cmdline_info *p = get_ccline_ptr();
6475
6476 if (p == NULL)
6477 return NULL;
6478 return vim_strnsave(p->cmdbuff, p->cmdlen);
6479}
6480
6481/*
6482 * Get the current command line position, counted in bytes.
6483 * Zero is the first position.
6484 * Only works when the command line is being edited.
6485 * Returns -1 when something is wrong.
6486 */
6487 int
6488get_cmdline_pos(void)
6489{
6490 struct cmdline_info *p = get_ccline_ptr();
6491
6492 if (p == NULL)
6493 return -1;
6494 return p->cmdpos;
6495}
6496
6497/*
6498 * Set the command line byte position to "pos". Zero is the first position.
6499 * Only works when the command line is being edited.
6500 * Returns 1 when failed, 0 when OK.
6501 */
6502 int
6503set_cmdline_pos(
6504 int pos)
6505{
6506 struct cmdline_info *p = get_ccline_ptr();
6507
6508 if (p == NULL)
6509 return 1;
6510
6511 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6512 * changed the command line. */
6513 if (pos < 0)
6514 new_cmdpos = 0;
6515 else
6516 new_cmdpos = pos;
6517 return 0;
6518}
6519#endif
6520
6521#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6522/*
6523 * Get the current command-line type.
6524 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6525 * Only works when the command line is being edited.
6526 * Returns NUL when something is wrong.
6527 */
6528 int
6529get_cmdline_type(void)
6530{
6531 struct cmdline_info *p = get_ccline_ptr();
6532
6533 if (p == NULL)
6534 return NUL;
6535 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006536 return
6537# ifdef FEAT_EVAL
6538 (p->input_fn) ? '@' :
6539# endif
6540 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006541 return p->cmdfirstc;
6542}
6543#endif
6544
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6546/*
6547 * Get indices "num1,num2" that specify a range within a list (not a range of
6548 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6549 * Returns OK if parsed successfully, otherwise FAIL.
6550 */
6551 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006552get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553{
6554 int len;
6555 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006556 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557
6558 *str = skipwhite(*str);
6559 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6560 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006561 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 *str += len;
6563 *num1 = (int)num;
6564 first = TRUE;
6565 }
6566 *str = skipwhite(*str);
6567 if (**str == ',') /* parse "to" part of range */
6568 {
6569 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006570 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006571 if (len > 0)
6572 {
6573 *num2 = (int)num;
6574 *str = skipwhite(*str + len);
6575 }
6576 else if (!first) /* no number given at all */
6577 return FAIL;
6578 }
6579 else if (first) /* only one number given */
6580 *num2 = *num1;
6581 return OK;
6582}
6583#endif
6584
6585#if defined(FEAT_CMDHIST) || defined(PROTO)
6586/*
6587 * :history command - print a history
6588 */
6589 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006590ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591{
6592 histentry_T *hist;
6593 int histype1 = HIST_CMD;
6594 int histype2 = HIST_CMD;
6595 int hisidx1 = 1;
6596 int hisidx2 = -1;
6597 int idx;
6598 int i, j, k;
6599 char_u *end;
6600 char_u *arg = eap->arg;
6601
6602 if (hislen == 0)
6603 {
6604 MSG(_("'history' option is zero"));
6605 return;
6606 }
6607
6608 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6609 {
6610 end = arg;
6611 while (ASCII_ISALPHA(*end)
6612 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6613 end++;
6614 i = *end;
6615 *end = NUL;
6616 histype1 = get_histtype(arg);
6617 if (histype1 == -1)
6618 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006619 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 histype1 = 0;
6622 histype2 = HIST_COUNT-1;
6623 }
6624 else
6625 {
6626 *end = i;
6627 EMSG(_(e_trailing));
6628 return;
6629 }
6630 }
6631 else
6632 histype2 = histype1;
6633 *end = i;
6634 }
6635 else
6636 end = arg;
6637 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6638 {
6639 EMSG(_(e_trailing));
6640 return;
6641 }
6642
6643 for (; !got_int && histype1 <= histype2; ++histype1)
6644 {
6645 STRCPY(IObuff, "\n # ");
6646 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6647 MSG_PUTS_TITLE(IObuff);
6648 idx = hisidx[histype1];
6649 hist = history[histype1];
6650 j = hisidx1;
6651 k = hisidx2;
6652 if (j < 0)
6653 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6654 if (k < 0)
6655 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6656 if (idx >= 0 && j <= k)
6657 for (i = idx + 1; !got_int; ++i)
6658 {
6659 if (i == hislen)
6660 i = 0;
6661 if (hist[i].hisstr != NULL
6662 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6663 {
6664 msg_putchar('\n');
6665 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6666 hist[i].hisnum);
6667 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6668 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006669 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 else
6671 STRCAT(IObuff, hist[i].hisstr);
6672 msg_outtrans(IObuff);
6673 out_flush();
6674 }
6675 if (i == idx)
6676 break;
6677 }
6678 }
6679}
6680#endif
6681
6682#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006683/*
6684 * Buffers for history read from a viminfo file. Only valid while reading.
6685 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006686static histentry_T *viminfo_history[HIST_COUNT] =
6687 {NULL, NULL, NULL, NULL, NULL};
6688static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6689static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690static int viminfo_add_at_front = FALSE;
6691
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006692static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693
6694/*
6695 * Translate a history type number to the associated character.
6696 */
6697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006698hist_type2char(
6699 int type,
6700 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701{
6702 if (type == HIST_CMD)
6703 return ':';
6704 if (type == HIST_SEARCH)
6705 {
6706 if (use_question)
6707 return '?';
6708 else
6709 return '/';
6710 }
6711 if (type == HIST_EXPR)
6712 return '=';
6713 return '@';
6714}
6715
6716/*
6717 * Prepare for reading the history from the viminfo file.
6718 * This allocates history arrays to store the read history lines.
6719 */
6720 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006721prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722{
6723 int i;
6724 int num;
6725 int type;
6726 int len;
6727
6728 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006729 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 if (asklen > hislen)
6731 asklen = hislen;
6732
6733 for (type = 0; type < HIST_COUNT; ++type)
6734 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006735 /* Count the number of empty spaces in the history list. Entries read
6736 * from viminfo previously are also considered empty. If there are
6737 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006739 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 num++;
6741 len = asklen;
6742 if (num > len)
6743 len = num;
6744 if (len <= 0)
6745 viminfo_history[type] = NULL;
6746 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006747 viminfo_history[type] = (histentry_T *)lalloc(
6748 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 if (viminfo_history[type] == NULL)
6750 len = 0;
6751 viminfo_hislen[type] = len;
6752 viminfo_hisidx[type] = 0;
6753 }
6754}
6755
6756/*
6757 * Accept a line from the viminfo, store it in the history array when it's
6758 * new.
6759 */
6760 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006761read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762{
6763 int type;
6764 long_u len;
6765 char_u *val;
6766 char_u *p;
6767
6768 type = hist_char2type(virp->vir_line[0]);
6769 if (viminfo_hisidx[type] < viminfo_hislen[type])
6770 {
6771 val = viminfo_readstring(virp, 1, TRUE);
6772 if (val != NULL && *val != NUL)
6773 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006774 int sep = (*val == ' ' ? NUL : *val);
6775
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006777 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
6779 /* Need to re-allocate to append the separator byte. */
6780 len = STRLEN(val);
6781 p = lalloc(len + 2, TRUE);
6782 if (p != NULL)
6783 {
6784 if (type == HIST_SEARCH)
6785 {
6786 /* Search entry: Move the separator from the first
6787 * column to after the NUL. */
6788 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006789 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 }
6791 else
6792 {
6793 /* Not a search entry: No separator in the viminfo
6794 * file, add a NUL separator. */
6795 mch_memmove(p, val, (size_t)len + 1);
6796 p[len + 1] = NUL;
6797 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006798 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6799 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006800 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6801 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006802 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 }
6804 }
6805 }
6806 vim_free(val);
6807 }
6808 return viminfo_readline(virp);
6809}
6810
Bram Moolenaar07219f92013-04-14 23:19:36 +02006811/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006812 * Accept a new style history line from the viminfo, store it in the history
6813 * array when it's new.
6814 */
6815 void
6816handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006817 garray_T *values,
6818 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006819{
6820 int type;
6821 long_u len;
6822 char_u *val;
6823 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006824 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825
6826 /* Check the format:
6827 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006828 if (values->ga_len < 4
6829 || vp[0].bv_type != BVAL_NR
6830 || vp[1].bv_type != BVAL_NR
6831 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6832 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006833 return;
6834
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006835 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006836 if (type >= HIST_COUNT)
6837 return;
6838 if (viminfo_hisidx[type] < viminfo_hislen[type])
6839 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006840 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006841 if (val != NULL && *val != NUL)
6842 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006843 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6844 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006845 int idx;
6846 int overwrite = FALSE;
6847
6848 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6849 {
6850 /* If lines were written by an older Vim we need to avoid
6851 * getting duplicates. See if the entry already exists. */
6852 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6853 {
6854 p = viminfo_history[type][idx].hisstr;
6855 if (STRCMP(val, p) == 0
6856 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6857 {
6858 overwrite = TRUE;
6859 break;
6860 }
6861 }
6862
6863 if (!overwrite)
6864 {
6865 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006866 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006867 p = lalloc(len + 2, TRUE);
6868 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006869 else
6870 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006871 if (p != NULL)
6872 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006873 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006874 if (!overwrite)
6875 {
6876 mch_memmove(p, val, (size_t)len + 1);
6877 /* Put the separator after the NUL. */
6878 p[len + 1] = sep;
6879 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006880 viminfo_history[type][idx].hisnum = 0;
6881 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006882 viminfo_hisidx[type]++;
6883 }
6884 }
6885 }
6886 }
6887 }
6888}
6889
6890/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006891 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006892 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006893 static void
6894concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895{
6896 int idx;
6897 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006898
6899 idx = hisidx[type] + viminfo_hisidx[type];
6900 if (idx >= hislen)
6901 idx -= hislen;
6902 else if (idx < 0)
6903 idx = hislen - 1;
6904 if (viminfo_add_at_front)
6905 hisidx[type] = idx;
6906 else
6907 {
6908 if (hisidx[type] == -1)
6909 hisidx[type] = hislen - 1;
6910 do
6911 {
6912 if (history[type][idx].hisstr != NULL
6913 || history[type][idx].viminfo)
6914 break;
6915 if (++idx == hislen)
6916 idx = 0;
6917 } while (idx != hisidx[type]);
6918 if (idx != hisidx[type] && --idx < 0)
6919 idx = hislen - 1;
6920 }
6921 for (i = 0; i < viminfo_hisidx[type]; i++)
6922 {
6923 vim_free(history[type][idx].hisstr);
6924 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6925 history[type][idx].viminfo = TRUE;
6926 history[type][idx].time_set = viminfo_history[type][i].time_set;
6927 if (--idx < 0)
6928 idx = hislen - 1;
6929 }
6930 idx += 1;
6931 idx %= hislen;
6932 for (i = 0; i < viminfo_hisidx[type]; i++)
6933 {
6934 history[type][idx++].hisnum = ++hisnum[type];
6935 idx %= hislen;
6936 }
6937}
6938
6939#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6940 static int
6941#ifdef __BORLANDC__
6942_RTLENTRYF
6943#endif
6944sort_hist(const void *s1, const void *s2)
6945{
6946 histentry_T *p1 = *(histentry_T **)s1;
6947 histentry_T *p2 = *(histentry_T **)s2;
6948
6949 if (p1->time_set < p2->time_set) return -1;
6950 if (p1->time_set > p2->time_set) return 1;
6951 return 0;
6952}
6953#endif
6954
6955/*
6956 * Merge history lines from viminfo and lines typed in this Vim based on the
6957 * timestamp;
6958 */
6959 static void
6960merge_history(int type)
6961{
6962 int max_len;
6963 histentry_T **tot_hist;
6964 histentry_T *new_hist;
6965 int i;
6966 int len;
6967
6968 /* Make one long list with all entries. */
6969 max_len = hislen + viminfo_hisidx[type];
6970 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6971 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6972 if (tot_hist == NULL || new_hist == NULL)
6973 {
6974 vim_free(tot_hist);
6975 vim_free(new_hist);
6976 return;
6977 }
6978 for (i = 0; i < viminfo_hisidx[type]; i++)
6979 tot_hist[i] = &viminfo_history[type][i];
6980 len = i;
6981 for (i = 0; i < hislen; i++)
6982 if (history[type][i].hisstr != NULL)
6983 tot_hist[len++] = &history[type][i];
6984
6985 /* Sort the list on timestamp. */
6986 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6987
6988 /* Keep the newest ones. */
6989 for (i = 0; i < hislen; i++)
6990 {
6991 if (i < len)
6992 {
6993 new_hist[i] = *tot_hist[i];
6994 tot_hist[i]->hisstr = NULL;
6995 if (new_hist[i].hisnum == 0)
6996 new_hist[i].hisnum = ++hisnum[type];
6997 }
6998 else
6999 clear_hist_entry(&new_hist[i]);
7000 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007001 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007002
7003 /* Free what is not kept. */
7004 for (i = 0; i < viminfo_hisidx[type]; i++)
7005 vim_free(viminfo_history[type][i].hisstr);
7006 for (i = 0; i < hislen; i++)
7007 vim_free(history[type][i].hisstr);
7008 vim_free(history[type]);
7009 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007010 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007011}
7012
7013/*
7014 * Finish reading history lines from viminfo. Not used when writing viminfo.
7015 */
7016 void
7017finish_viminfo_history(vir_T *virp)
7018{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007020 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007021
7022 for (type = 0; type < HIST_COUNT; ++type)
7023 {
7024 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007025 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007026
7027 if (merge)
7028 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007030 concat_history(type);
7031
Bram Moolenaard23a8232018-02-10 18:45:26 +01007032 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007033 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 }
7035}
7036
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007037/*
7038 * Write history to viminfo file in "fp".
7039 * When "merge" is TRUE merge history lines with a previously read viminfo
7040 * file, data is in viminfo_history[].
7041 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7042 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007044write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045{
7046 int i;
7047 int type;
7048 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007049 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007050
7051 init_history();
7052 if (hislen == 0)
7053 return;
7054 for (type = 0; type < HIST_COUNT; ++type)
7055 {
7056 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7057 if (num_saved == 0)
7058 continue;
7059 if (num_saved < 0) /* Use default */
7060 num_saved = hislen;
7061 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7062 type == HIST_CMD ? _("Command Line") :
7063 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007064 type == HIST_EXPR ? _("Expression") :
7065 type == HIST_INPUT ? _("Input Line") :
7066 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 if (num_saved > hislen)
7068 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007069
7070 /*
7071 * Merge typed and viminfo history:
7072 * round 1: history of typed commands.
7073 * round 2: history from recently read viminfo.
7074 */
7075 for (round = 1; round <= 2; ++round)
7076 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007077 if (round == 1)
7078 /* start at newest entry, somewhere in the list */
7079 i = hisidx[type];
7080 else if (viminfo_hisidx[type] > 0)
7081 /* start at newest entry, first in the list */
7082 i = 0;
7083 else
7084 /* empty list */
7085 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007086 if (i >= 0)
7087 while (num_saved > 0
7088 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007090 char_u *p;
7091 time_t timestamp;
7092 int c = NUL;
7093
7094 if (round == 1)
7095 {
7096 p = history[type][i].hisstr;
7097 timestamp = history[type][i].time_set;
7098 }
7099 else
7100 {
7101 p = viminfo_history[type] == NULL ? NULL
7102 : viminfo_history[type][i].hisstr;
7103 timestamp = viminfo_history[type] == NULL ? 0
7104 : viminfo_history[type][i].time_set;
7105 }
7106
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007107 if (p != NULL && (round == 2
7108 || !merge
7109 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007111 --num_saved;
7112 fputc(hist_type2char(type, TRUE), fp);
7113 /* For the search history: put the separator in the
7114 * second column; use a space if there isn't one. */
7115 if (type == HIST_SEARCH)
7116 {
7117 c = p[STRLEN(p) + 1];
7118 putc(c == NUL ? ' ' : c, fp);
7119 }
7120 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007121
7122 {
7123 char cbuf[NUMBUFLEN];
7124
7125 /* New style history with a bar line. Format:
7126 * |{bartype},{histtype},{timestamp},{separator},"text" */
7127 if (c == NUL)
7128 cbuf[0] = NUL;
7129 else
7130 sprintf(cbuf, "%d", c);
7131 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7132 type, (long)timestamp, cbuf);
7133 barline_writestring(fp, p, LSIZE - 20);
7134 putc('\n', fp);
7135 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007137 if (round == 1)
7138 {
7139 /* Decrement index, loop around and stop when back at
7140 * the start. */
7141 if (--i < 0)
7142 i = hislen - 1;
7143 if (i == hisidx[type])
7144 break;
7145 }
7146 else
7147 {
7148 /* Increment index. Stop at the end in the while. */
7149 ++i;
7150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007152 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007153 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007154 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007155 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007156 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007157 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 }
7159}
7160#endif /* FEAT_VIMINFO */
7161
7162#if defined(FEAT_FKMAP) || defined(PROTO)
7163/*
7164 * Write a character at the current cursor+offset position.
7165 * It is directly written into the command buffer block.
7166 */
7167 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007168cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169{
7170 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7171 {
7172 EMSG(_("E198: cmd_pchar beyond the command length"));
7173 return;
7174 }
7175 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7176 ccline.cmdbuff[ccline.cmdlen] = NUL;
7177}
7178
7179 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007180cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181{
7182 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7183 {
7184 /* EMSG(_("cmd_gchar beyond the command length")); */
7185 return NUL;
7186 }
7187 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7188}
7189#endif
7190
7191#if defined(FEAT_CMDWIN) || defined(PROTO)
7192/*
7193 * Open a window on the current command line and history. Allow editing in
7194 * the window. Returns when the window is closed.
7195 * Returns:
7196 * CR if the command is to be executed
7197 * Ctrl_C if it is to be abandoned
7198 * K_IGNORE if editing continues
7199 */
7200 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007201open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202{
7203 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007204 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007206 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007207 win_T *wp;
7208 int i;
7209 linenr_T lnum;
7210 int histtype;
7211 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 int save_restart_edit = restart_edit;
7213 int save_State = State;
7214 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007215#ifdef FEAT_RIGHTLEFT
7216 int save_cmdmsg_rl = cmdmsg_rl;
7217#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007218#ifdef FEAT_FOLDING
7219 int save_KeyTyped;
7220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222 /* Can't do this recursively. Can't do it when typing a password. */
7223 if (cmdwin_type != 0
7224# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7225 || cmdline_star > 0
7226# endif
7227 )
7228 {
7229 beep_flush();
7230 return K_IGNORE;
7231 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007232 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233
7234 /* Save current window sizes. */
7235 win_size_save(&winsizes);
7236
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007238 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007239
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007240 /* don't use a new tab page */
7241 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007242 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007243
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 /* Create a window for the command-line buffer. */
7245 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7246 {
7247 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007248 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 return K_IGNORE;
7250 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007251 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252
7253 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007254 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007255 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007258#ifdef FEAT_FOLDING
7259 curwin->w_p_fen = FALSE;
7260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007262 curwin->w_p_rl = cmdmsg_rl;
7263 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007265 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007268 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007269 /* But don't allow switching to another buffer. */
7270 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271
Bram Moolenaar46152342005-09-07 21:18:43 +00007272 /* Showing the prompt may have set need_wait_return, reset it. */
7273 need_wait_return = FALSE;
7274
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007275 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7277 {
7278 if (p_wc == TAB)
7279 {
7280 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7281 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7282 }
7283 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7284 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007285 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007287 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7288 * sets 'textwidth' to 78). */
7289 curbuf->b_p_tw = 0;
7290
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291 /* Fill the buffer with the history. */
7292 init_history();
7293 if (hislen > 0)
7294 {
7295 i = hisidx[histtype];
7296 if (i >= 0)
7297 {
7298 lnum = 0;
7299 do
7300 {
7301 if (++i == hislen)
7302 i = 0;
7303 if (history[histtype][i].hisstr != NULL)
7304 ml_append(lnum++, history[histtype][i].hisstr,
7305 (colnr_T)0, FALSE);
7306 }
7307 while (i != hisidx[histtype]);
7308 }
7309 }
7310
7311 /* Replace the empty last line with the current command-line and put the
7312 * cursor there. */
7313 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7314 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7315 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007316 changed_line_abv_curs();
7317 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007318 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007321 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322
7323 /* No Ex mode here! */
7324 exmode_active = 0;
7325
7326 State = NORMAL;
7327# ifdef FEAT_MOUSE
7328 setmouse();
7329# endif
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007332 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007333 if (restart_edit != 0) /* autocmd with ":startinsert" */
7334 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
7336 i = RedrawingDisabled;
7337 RedrawingDisabled = 0;
7338
7339 /*
7340 * Call the main loop until <CR> or CTRL-C is typed.
7341 */
7342 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007343 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344
7345 RedrawingDisabled = i;
7346
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007347# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007348 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007349# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007350
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007352 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007353
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007354# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007355 /* Restore KeyTyped in case it is modified by autocommands */
7356 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357# endif
7358
Bram Moolenaarccc18222007-05-10 18:25:20 +00007359 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007360 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 cmdwin_type = 0;
7362
7363 exmode_active = save_exmode;
7364
Bram Moolenaarf9821062008-06-20 16:31:07 +00007365 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007367 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 {
7369 cmdwin_result = Ctrl_C;
7370 EMSG(_("E199: Active window or buffer deleted"));
7371 }
7372 else
7373 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007374# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375 /* autocmds may abort script processing */
7376 if (aborting() && cmdwin_result != K_IGNORE)
7377 cmdwin_result = Ctrl_C;
7378# endif
7379 /* Set the new command line from the cmdline buffer. */
7380 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007381 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007383 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7384
7385 if (histtype == HIST_CMD)
7386 {
7387 /* Execute the command directly. */
7388 ccline.cmdbuff = vim_strsave((char_u *)p);
7389 cmdwin_result = CAR;
7390 }
7391 else
7392 {
7393 /* First need to cancel what we were doing. */
7394 ccline.cmdbuff = NULL;
7395 stuffcharReadbuff(':');
7396 stuffReadbuff((char_u *)p);
7397 stuffcharReadbuff(CAR);
7398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 }
7400 else if (cmdwin_result == K_XF2) /* :qa typed */
7401 {
7402 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7403 cmdwin_result = CAR;
7404 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007405 else if (cmdwin_result == Ctrl_C)
7406 {
7407 /* :q or :close, don't execute any command
7408 * and don't modify the cmd window. */
7409 ccline.cmdbuff = NULL;
7410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007411 else
7412 ccline.cmdbuff = vim_strsave(ml_get_curline());
7413 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007414 {
7415 ccline.cmdbuff = vim_strsave((char_u *)"");
7416 ccline.cmdlen = 0;
7417 ccline.cmdbufflen = 1;
7418 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
7422 {
7423 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7424 ccline.cmdbufflen = ccline.cmdlen + 1;
7425 ccline.cmdpos = curwin->w_cursor.col;
7426 if (ccline.cmdpos > ccline.cmdlen)
7427 ccline.cmdpos = ccline.cmdlen;
7428 if (cmdwin_result == K_IGNORE)
7429 {
7430 set_cmdspos_cursor();
7431 redrawcmd();
7432 }
7433 }
7434
Bram Moolenaar071d4272004-06-13 20:20:40 +00007435 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007436 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007437# ifdef FEAT_CONCEAL
7438 /* Avoid command-line window first character being concealed. */
7439 curwin->w_p_cole = 0;
7440# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007442 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 win_goto(old_curwin);
7444 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007445
7446 /* win_close() may have already wiped the buffer when 'bh' is
7447 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007448 if (bufref_valid(&bufref))
7449 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450
7451 /* Restore window sizes. */
7452 win_size_restore(&winsizes);
7453
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007454 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 }
7456
7457 ga_clear(&winsizes);
7458 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007459# ifdef FEAT_RIGHTLEFT
7460 cmdmsg_rl = save_cmdmsg_rl;
7461# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462
7463 State = save_State;
7464# ifdef FEAT_MOUSE
7465 setmouse();
7466# endif
7467
7468 return cmdwin_result;
7469}
7470#endif /* FEAT_CMDWIN */
7471
7472/*
7473 * Used for commands that either take a simple command string argument, or:
7474 * cmd << endmarker
7475 * {script}
7476 * endmarker
7477 * Returns a pointer to allocated memory with {script} or NULL.
7478 */
7479 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007480script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007481{
7482 char_u *theline;
7483 char *end_pattern = NULL;
7484 char dot[] = ".";
7485 garray_T ga;
7486
7487 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7488 return NULL;
7489
7490 ga_init2(&ga, 1, 0x400);
7491
7492 if (cmd[2] != NUL)
7493 end_pattern = (char *)skipwhite(cmd + 2);
7494 else
7495 end_pattern = dot;
7496
7497 for (;;)
7498 {
7499 theline = eap->getline(
7500#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007501 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007502#endif
7503 NUL, eap->cookie, 0);
7504
7505 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007506 {
7507 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007508 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007509 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007510
7511 ga_concat(&ga, theline);
7512 ga_append(&ga, '\n');
7513 vim_free(theline);
7514 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007515 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516
7517 return (char_u *)ga.ga_data;
7518}