blob: 9a8806c14bd5e25ecd91374bcead06bf66530467 [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 Moolenaar198cb662018-09-06 21:44:17 +0200274 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200275 */
276 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200277do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
278 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200279{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 char_u *cmd;
281 cmdmod_T save_cmdmod = cmdmod;
282 char_u *p;
283 int delim_optional = FALSE;
284 int delim;
285 char_u *end;
286 char_u *dummy;
287 exarg_T ea;
288 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200289 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200290
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200291 *skiplen = 0;
292 *patlen = ccline.cmdlen;
293
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200294 if (!p_is || cmd_silent)
295 return FALSE;
296
297 // by default search all lines
298 search_first_line = 0;
299 search_last_line = MAXLNUM;
300
301 if (firstc == '/' || firstc == '?')
302 return TRUE;
303 if (firstc != ':')
304 return FALSE;
305
306 vim_memset(&ea, 0, sizeof(ea));
307 ea.line1 = 1;
308 ea.line2 = 1;
309 ea.cmd = ccline.cmdbuff;
310 ea.addr_type = ADDR_LINES;
311
312 parse_command_modifiers(&ea, &dummy, TRUE);
313 cmdmod = save_cmdmod;
314
315 cmd = skip_range(ea.cmd, NULL);
316 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
317 return FALSE;
318
319 // Skip over "substitute" to find the pattern separator.
320 for (p = cmd; ASCII_ISALPHA(*p); ++p)
321 ;
322 if (*skipwhite(p) == NUL)
323 return FALSE;
324
325 if (STRNCMP(cmd, "substitute", p - cmd) == 0
326 || STRNCMP(cmd, "smagic", p - cmd) == 0
327 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
328 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200329 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200330 if (*cmd == 's' && cmd[1] == 'm')
331 p_magic = TRUE;
332 else if (*cmd == 's' && cmd[1] == 'n')
333 p_magic = FALSE;
334 }
335 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
336 {
337 // skip over flags
338 while (ASCII_ISALPHA(*(p = skipwhite(p))))
339 ++p;
340 if (*p == NUL)
341 return FALSE;
342 }
343 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
344 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
345 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
346 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
347 || STRNCMP(cmd, "global", p - cmd) == 0)
348 {
349 // skip over "!"
350 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200351 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200352 p++;
353 if (*skipwhite(p) == NUL)
354 return FALSE;
355 }
356 if (*cmd != 'g')
357 delim_optional = TRUE;
358 }
359 else
360 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200361
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200362 p = skipwhite(p);
363 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
364 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200365
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200366 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200367
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200368 if (end == p && !use_last_pat)
369 return FALSE;
370
371 // Don't do 'hlsearch' highlighting if the pattern matches everything.
372 if (!use_last_pat)
373 {
374 char c = *end;
375 int empty;
376
377 *end = NUL;
378 empty = empty_pattern(p);
379 *end = c;
380 if (empty)
381 return FALSE;
382 }
383
384 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200385 *skiplen = (int)(p - ccline.cmdbuff);
386 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200387
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200388 // parse the address range
389 save_cursor = curwin->w_cursor;
390 curwin->w_cursor = is_state->search_start;
391 parse_cmd_address(&ea, &dummy);
392 if (ea.addr_count > 0)
393 {
394 // Allow for reverse match.
395 if (ea.line2 < ea.line1)
396 {
397 search_first_line = ea.line2;
398 search_last_line = ea.line1;
399 }
400 else
401 {
402 search_first_line = ea.line1;
403 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404 }
405 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200406 else if (cmd[0] == 's' && cmd[1] != 'o')
407 {
408 // :s defaults to the current line
409 search_first_line = curwin->w_cursor.lnum;
410 search_last_line = curwin->w_cursor.lnum;
411 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200412
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200413 curwin->w_cursor = save_cursor;
414 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200415}
416
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200417 static void
418finish_incsearch_highlighting(
419 int gotesc,
420 incsearch_state_T *is_state,
421 int call_update_screen)
422{
423 if (is_state->did_incsearch)
424 {
425 is_state->did_incsearch = FALSE;
426 if (gotesc)
427 curwin->w_cursor = is_state->save_cursor;
428 else
429 {
430 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
431 {
432 // put the '" mark at the original position
433 curwin->w_cursor = is_state->save_cursor;
434 setpcmark();
435 }
436 curwin->w_cursor = is_state->search_start;
437 }
438 restore_viewstate(&is_state->old_viewstate);
439 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200440
441 // by default search all lines
442 search_first_line = 0;
443 search_last_line = MAXLNUM;
444
445 p_magic = is_state->magic_save;
446
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200447 validate_cursor(); /* needed for TAB */
448 if (call_update_screen)
449 update_screen(SOME_VALID);
450 else
451 redraw_all_later(SOME_VALID);
452 }
453}
454
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200455/*
456 * Do 'incsearch' highlighting if desired.
457 */
458 static void
459may_do_incsearch_highlighting(
460 int firstc,
461 long count,
462 incsearch_state_T *is_state)
463{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200464 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200465 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466 pos_T end_pos;
467 struct cmdline_info save_ccline;
468#ifdef FEAT_RELTIME
469 proftime_T tm;
470#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200471 int next_char;
472 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473
Bram Moolenaar198cb662018-09-06 21:44:17 +0200474 // Parsing range may already set the last search pattern.
475 save_last_search_pattern();
476
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200477 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200478 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200479 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200480 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200481 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200482 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200483
484 // If there is a character waiting, search and redraw later.
485 if (char_avail())
486 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200487 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200488 is_state->incsearch_postponed = TRUE;
489 return;
490 }
491 is_state->incsearch_postponed = FALSE;
492
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200493 if (search_first_line == 0)
494 // start at the original cursor position
495 curwin->w_cursor = is_state->search_start;
496 else
497 {
498 // start at the first line in the range
499 curwin->w_cursor.lnum = search_first_line;
500 curwin->w_cursor.col = 0;
501 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200502
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200503 // Use the previous pattern for ":s//".
504 next_char = ccline.cmdbuff[skiplen + patlen];
505 use_last_pat = patlen == 0 && skiplen > 0
506 && ccline.cmdbuff[skiplen - 1] == next_char;
507
508 // If there is no pattern, don't do anything.
509 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200510 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200511 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200512 set_no_hlsearch(TRUE); // turn off previous highlight
513 redraw_all_later(SOME_VALID);
514 }
515 else
516 {
517 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
518
519 cursor_off(); // so the user knows we're busy
520 out_flush();
521 ++emsg_off; // so it doesn't beep if bad expr
522#ifdef FEAT_RELTIME
523 // Set the time limit to half a second.
524 profile_setlimit(500L, &tm);
525#endif
526 if (!p_hls)
527 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200528 if (search_first_line != 0)
529 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200530 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200531 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200532 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200533#ifdef FEAT_RELTIME
534 &tm, NULL
535#else
536 NULL, NULL
537#endif
538 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200539 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200540 --emsg_off;
541
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200542 if (curwin->w_cursor.lnum < search_first_line
543 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200544 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200545 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200546 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200547 curwin->w_cursor = is_state->search_start;
548 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200549
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200550 // if interrupted while searching, behave like it failed
551 if (got_int)
552 {
553 (void)vpeekc(); // remove <C-C> from input stream
554 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200555 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200556 }
557 else if (char_avail())
558 // cancelled searching because a char was typed
559 is_state->incsearch_postponed = TRUE;
560 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200561 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200562 highlight_match = TRUE; // highlight position
563 else
564 highlight_match = FALSE; // remove highlight
565
566 // First restore the old curwin values, so the screen is positioned in the
567 // same way as the actual search command.
568 restore_viewstate(&is_state->old_viewstate);
569 changed_cline_bef_curs();
570 update_topline();
571
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200572 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200573 {
574 pos_T save_pos = curwin->w_cursor;
575
576 is_state->match_start = curwin->w_cursor;
577 set_search_match(&curwin->w_cursor);
578 validate_cursor();
579 end_pos = curwin->w_cursor;
580 is_state->match_end = end_pos;
581 curwin->w_cursor = save_pos;
582 }
583 else
584 end_pos = curwin->w_cursor; // shutup gcc 4
585
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200586 // Disable 'hlsearch' highlighting if the pattern matches everything.
587 // Avoids a flash when typing "foo\|".
588 if (!use_last_pat)
589 {
590 next_char = ccline.cmdbuff[skiplen + patlen];
591 ccline.cmdbuff[skiplen + patlen] = NUL;
592 if (empty_pattern(ccline.cmdbuff))
593 set_no_hlsearch(TRUE);
594 ccline.cmdbuff[skiplen + patlen] = next_char;
595 }
596
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 validate_cursor();
598 // May redraw the status line to show the cursor position.
599 if (p_ru && curwin->w_status_height > 0)
600 curwin->w_redr_status = TRUE;
601
602 save_cmdline(&save_ccline);
603 update_screen(SOME_VALID);
604 restore_cmdline(&save_ccline);
605 restore_last_search_pattern();
606
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200607 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
608 // end of the pattern, e.g. for ":s/pat/".
609 if (ccline.cmdbuff[skiplen + patlen] != NUL)
610 curwin->w_cursor = is_state->search_start;
611 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200612 curwin->w_cursor = end_pos;
613
614 msg_starthere();
615 redrawcmdline();
616 is_state->did_incsearch = TRUE;
617}
618
619/*
620 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
621 * or previous match.
622 * Returns FAIL when jumping to cmdline_not_changed;
623 */
624 static int
625may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200626 int firstc,
627 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200629 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200631 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200632 pos_T t;
633 char_u *pat;
634 int search_flags = SEARCH_NOOF;
635 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200636 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200637
Bram Moolenaar198cb662018-09-06 21:44:17 +0200638 // Parsing range may already set the last search pattern.
639 save_last_search_pattern();
640
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200641 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200642 {
643 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200644 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200645 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200646 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200647 {
648 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200649 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200650 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200652 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200653 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200655 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200656 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200657 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200658 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200659 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200661 cursor_off();
662 out_flush();
663 if (c == Ctrl_G)
664 {
665 t = is_state->match_end;
666 if (LT_POS(is_state->match_start, is_state->match_end))
667 // Start searching at the end of the match not at the beginning of
668 // the next column.
669 (void)decl(&t);
670 search_flags += SEARCH_COL;
671 }
672 else
673 t = is_state->match_start;
674 if (!p_hls)
675 search_flags += SEARCH_KEEP;
676 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 save = pat[patlen];
678 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200679 i = searchit(curwin, curbuf, &t,
680 c == Ctrl_G ? FORWARD : BACKWARD,
681 pat, count, search_flags,
682 RE_SEARCH, 0, NULL, NULL);
683 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200684 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200685 if (i)
686 {
687 is_state->search_start = is_state->match_start;
688 is_state->match_end = t;
689 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200690 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200691 {
692 // Move just before the current match, so that when nv_search
693 // finishes the cursor will be put back on the match.
694 is_state->search_start = t;
695 (void)decl(&is_state->search_start);
696 }
697 else if (c == Ctrl_G && firstc == '?')
698 {
699 // Move just after the current match, so that when nv_search
700 // finishes the cursor will be put back on the match.
701 is_state->search_start = t;
702 (void)incl(&is_state->search_start);
703 }
704 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
705 {
706 // wrap around
707 is_state->search_start = t;
708 if (firstc == '?')
709 (void)incl(&is_state->search_start);
710 else
711 (void)decl(&is_state->search_start);
712 }
713
714 set_search_match(&is_state->match_end);
715 curwin->w_cursor = is_state->match_start;
716 changed_cline_bef_curs();
717 update_topline();
718 validate_cursor();
719 highlight_match = TRUE;
720 save_viewstate(&is_state->old_viewstate);
721 update_screen(NOT_VALID);
722 redrawcmdline();
723 }
724 else
725 vim_beep(BO_ERROR);
726 restore_last_search_pattern();
727 return FAIL;
728}
729
730/*
731 * When CTRL-L typed: add character from the match to the pattern.
732 * May set "*c" to the added character.
733 * Return OK when jumping to cmdline_not_changed.
734 */
735 static int
736may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
737{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200738 int skiplen, patlen;
739
Bram Moolenaar198cb662018-09-06 21:44:17 +0200740 // Parsing range may already set the last search pattern.
741 save_last_search_pattern();
742
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200743 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200744 {
745 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200746 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200747 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200748
749 // Add a character from under the cursor for 'incsearch'.
750 if (is_state->did_incsearch)
751 {
752 curwin->w_cursor = is_state->match_end;
753 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
754 {
755 *c = gchar_cursor();
756
757 // If 'ignorecase' and 'smartcase' are set and the
758 // command line has no uppercase characters, convert
759 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200760 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200761 *c = MB_TOLOWER(*c);
762 if (*c != NUL)
763 {
764 if (*c == firstc || vim_strchr((char_u *)(
765 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
766 {
767 // put a backslash before special characters
768 stuffcharReadbuff(*c);
769 *c = '\\';
770 }
771 return FAIL;
772 }
773 }
774 }
775 return OK;
776}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200777#endif
778
Bram Moolenaar66216052017-12-16 16:33:44 +0100779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 * getcmdline() - accept a command line starting with firstc.
781 *
782 * firstc == ':' get ":" command line.
783 * firstc == '/' or '?' get search pattern
784 * firstc == '=' get expression
785 * firstc == '@' get text for input() function
786 * firstc == '>' get text for debug mode
787 * firstc == NUL get text for :insert command
788 * firstc == -1 like NUL, and break on CTRL-C
789 *
790 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
791 * command line.
792 *
793 * Careful: getcmdline() can be called recursively!
794 *
795 * Return pointer to allocated string if there is a commandline, NULL
796 * otherwise.
797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100799getcmdline(
800 int firstc,
801 long count UNUSED, /* only used for incremental search */
802 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803{
804 int c;
805 int i;
806 int j;
807 int gotesc = FALSE; /* TRUE when <ESC> just typed */
808 int do_abbr; /* when TRUE check for abbr. */
809#ifdef FEAT_CMDHIST
810 char_u *lookfor = NULL; /* string to match */
811 int hiscnt; /* current history line in use */
812 int histype; /* history type to be used */
813#endif
814#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200815 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816#endif
817 int did_wild_list = FALSE; /* did wild_list() recently */
818 int wim_index = 0; /* index in wim_flags[] */
819 int res;
820 int save_msg_scroll = msg_scroll;
821 int save_State = State; /* remember State when called */
822 int some_key_typed = FALSE; /* one of the keys was typed */
823#ifdef FEAT_MOUSE
824 /* mouse drag and release events are ignored, unless they are
825 * preceded with a mouse down event */
826 int ignore_drag_release = TRUE;
827#endif
828#ifdef FEAT_EVAL
829 int break_ctrl_c = FALSE;
830#endif
831 expand_T xpc;
832 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200833#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000834 /* Everything that may work recursively should save and restore the
835 * current command line in save_ccline. That includes update_screen(), a
836 * custom status line may invoke ":normal". */
837 struct cmdline_info save_ccline;
838#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200839 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841#ifdef FEAT_EVAL
842 if (firstc == -1)
843 {
844 firstc = NUL;
845 break_ctrl_c = TRUE;
846 }
847#endif
848#ifdef FEAT_RIGHTLEFT
849 /* start without Hebrew mapping for a command line */
850 if (firstc == ':' || firstc == '=' || firstc == '>')
851 cmd_hkmap = 0;
852#endif
853
854 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200855
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200857 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#endif
859
860 /*
861 * set some variables for redrawcmd()
862 */
863 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000864 ccline.cmdindent = (firstc > 0 ? indent : 0);
865
866 /* alloc initial ccline.cmdbuff */
867 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 if (ccline.cmdbuff == NULL)
869 return NULL; /* out of memory */
870 ccline.cmdlen = ccline.cmdpos = 0;
871 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100872 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000874 /* autoindent for :insert and :append */
875 if (firstc <= 0)
876 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200877 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000878 ccline.cmdbuff[indent] = NUL;
879 ccline.cmdpos = indent;
880 ccline.cmdspos = indent;
881 ccline.cmdlen = indent;
882 }
883
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000885 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886
887#ifdef FEAT_RIGHTLEFT
888 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
889 && (firstc == '/' || firstc == '?'))
890 cmdmsg_rl = TRUE;
891 else
892 cmdmsg_rl = FALSE;
893#endif
894
895 redir_off = TRUE; /* don't redirect the typed command */
896 if (!cmd_silent)
897 {
898 i = msg_scrolled;
899 msg_scrolled = 0; /* avoid wait_return message */
900 gotocmdline(TRUE);
901 msg_scrolled += i;
902 redrawcmdprompt(); /* draw prompt or indent */
903 set_cmdspos();
904 }
905 xpc.xp_context = EXPAND_NOTHING;
906 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000907#ifndef BACKSLASH_IN_FILENAME
908 xpc.xp_shell = FALSE;
909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000911#if defined(FEAT_EVAL)
912 if (ccline.input_fn)
913 {
914 xpc.xp_context = ccline.xp_context;
915 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000916# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000917 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000918# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000919 }
920#endif
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 /*
923 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
924 * doing ":@0" when register 0 doesn't contain a CR.
925 */
926 msg_scroll = FALSE;
927
928 State = CMDLINE;
929
930 if (firstc == '/' || firstc == '?' || firstc == '@')
931 {
932 /* Use ":lmap" mappings for search pattern and input(). */
933 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
934 b_im_ptr = &curbuf->b_p_iminsert;
935 else
936 b_im_ptr = &curbuf->b_p_imsearch;
937 if (*b_im_ptr == B_IMODE_LMAP)
938 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100939#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 im_set_active(*b_im_ptr == B_IMODE_IM);
941#endif
942 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100943#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 else if (p_imcmdline)
945 im_set_active(TRUE);
946#endif
947
948#ifdef FEAT_MOUSE
949 setmouse();
950#endif
951#ifdef CURSOR_SHAPE
952 ui_cursor_shape(); /* may show different cursor shape */
953#endif
954
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000955 /* When inside an autocommand for writing "exiting" may be set and
956 * terminal mode set to cooked. Need to set raw mode here then. */
957 settmode(TMODE_RAW);
958
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200959 /* Trigger CmdlineEnter autocommands. */
960 cmdline_type = firstc == NUL ? '-' : firstc;
961 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200962
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963#ifdef FEAT_CMDHIST
964 init_history();
965 hiscnt = hislen; /* set hiscnt to impossible history value */
966 histype = hist_char2type(firstc);
967#endif
968
969#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000970 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#endif
972
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200973 /* If something above caused an error, reset the flags, we do want to type
974 * and execute commands. Display may be messed up a bit. */
975 if (did_emsg)
976 redrawcmd();
977 did_emsg = FALSE;
978 got_int = FALSE;
979
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 /*
981 * Collect the command string, handling editing keys.
982 */
983 for (;;)
984 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000985 redir_off = TRUE; /* Don't redirect the typed command.
986 Repeated, because a ":redir" inside
987 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988#ifdef USE_ON_FLY_SCROLL
989 dont_scroll = FALSE; /* allow scrolling here */
990#endif
991 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
992
Bram Moolenaar72532d32018-04-07 19:09:09 +0200993 did_emsg = FALSE; /* There can't really be a reason why an error
994 that occurs while typing a command should
995 cause the command not to be executed. */
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000998
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100999 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1000 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001001 do
1002 {
1003 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001004 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (KeyTyped)
1007 {
1008 some_key_typed = TRUE;
1009#ifdef FEAT_RIGHTLEFT
1010 if (cmd_hkmap)
1011 c = hkmap(c);
1012# ifdef FEAT_FKMAP
1013 if (cmd_fkmap)
1014 c = cmdl_fkmap(c);
1015# endif
1016 if (cmdmsg_rl && !KeyStuffed)
1017 {
1018 /* Invert horizontal movements and operations. Only when
1019 * typed by the user directly, not when the result of a
1020 * mapping. */
1021 switch (c)
1022 {
1023 case K_RIGHT: c = K_LEFT; break;
1024 case K_S_RIGHT: c = K_S_LEFT; break;
1025 case K_C_RIGHT: c = K_C_LEFT; break;
1026 case K_LEFT: c = K_RIGHT; break;
1027 case K_S_LEFT: c = K_S_RIGHT; break;
1028 case K_C_LEFT: c = K_C_RIGHT; break;
1029 }
1030 }
1031#endif
1032 }
1033
1034 /*
1035 * Ignore got_int when CTRL-C was typed here.
1036 * Don't ignore it in :global, we really need to break then, e.g., for
1037 * ":g/pat/normal /pat" (without the <CR>).
1038 * Don't ignore it for the input() function.
1039 */
1040 if ((c == Ctrl_C
1041#ifdef UNIX
1042 || c == intr_char
1043#endif
1044 )
1045#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1046 && firstc != '@'
1047#endif
1048#ifdef FEAT_EVAL
1049 && !break_ctrl_c
1050#endif
1051 && !global_busy)
1052 got_int = FALSE;
1053
1054#ifdef FEAT_CMDHIST
1055 /* free old command line when finished moving around in the history
1056 * list */
1057 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001058 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001059 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 && c != K_PAGEDOWN && c != K_PAGEUP
1061 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001062 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001064 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065#endif
1066
1067 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001068 * When there are matching completions to select <S-Tab> works like
1069 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001071 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 c = Ctrl_P;
1073
1074#ifdef FEAT_WILDMENU
1075 /* Special translations for 'wildmenu' */
1076 if (did_wild_list && p_wmnu)
1077 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001078 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001080 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 c = Ctrl_N;
1082 }
1083 /* Hitting CR after "emenu Name.": complete submenu */
1084 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1085 && ccline.cmdpos > 1
1086 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1087 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1088 && (c == '\n' || c == '\r' || c == K_KENTER))
1089 c = K_DOWN;
1090#endif
1091
1092 /* free expanded names when finished walking through matches */
1093 if (xpc.xp_numfiles != -1
1094 && !(c == p_wc && KeyTyped) && c != p_wcm
1095 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1096 && c != Ctrl_L)
1097 {
1098 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1099 did_wild_list = FALSE;
1100#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001101 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102#endif
1103 xpc.xp_context = EXPAND_NOTHING;
1104 wim_index = 0;
1105#ifdef FEAT_WILDMENU
1106 if (p_wmnu && wild_menu_showing != 0)
1107 {
1108 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001109 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001110
1111 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001112 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
1114 if (wild_menu_showing == WM_SCROLLED)
1115 {
1116 /* Entered command line, move it up */
1117 cmdline_row--;
1118 redrawcmd();
1119 }
1120 else if (save_p_ls != -1)
1121 {
1122 /* restore 'laststatus' and 'winminheight' */
1123 p_ls = save_p_ls;
1124 p_wmh = save_p_wmh;
1125 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001126 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001128 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 redrawcmd();
1130 save_p_ls = -1;
1131 }
1132 else
1133 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 redraw_statuslines();
1136 }
1137 KeyTyped = skt;
1138 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001139 if (ccline.input_fn)
1140 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 }
1142#endif
1143 }
1144
1145#ifdef FEAT_WILDMENU
1146 /* Special translations for 'wildmenu' */
1147 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1148 {
1149 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001150 if (c == K_DOWN && ccline.cmdpos > 0
1151 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001153 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 {
1155 /* Hitting <Up>: Remove one submenu name in front of the
1156 * cursor */
1157 int found = FALSE;
1158
1159 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1160 i = 0;
1161 while (--j > 0)
1162 {
1163 /* check for start of menu name */
1164 if (ccline.cmdbuff[j] == ' '
1165 && ccline.cmdbuff[j - 1] != '\\')
1166 {
1167 i = j + 1;
1168 break;
1169 }
1170 /* check for start of submenu name */
1171 if (ccline.cmdbuff[j] == '.'
1172 && ccline.cmdbuff[j - 1] != '\\')
1173 {
1174 if (found)
1175 {
1176 i = j + 1;
1177 break;
1178 }
1179 else
1180 found = TRUE;
1181 }
1182 }
1183 if (i > 0)
1184 cmdline_del(i);
1185 c = p_wc;
1186 xpc.xp_context = EXPAND_NOTHING;
1187 }
1188 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001189 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001190 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001191 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192 {
1193 char_u upseg[5];
1194
1195 upseg[0] = PATHSEP;
1196 upseg[1] = '.';
1197 upseg[2] = '.';
1198 upseg[3] = PATHSEP;
1199 upseg[4] = NUL;
1200
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001201 if (c == K_DOWN
1202 && ccline.cmdpos > 0
1203 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1204 && (ccline.cmdpos < 3
1205 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1207 {
1208 /* go down a directory */
1209 c = p_wc;
1210 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001211 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 {
1213 /* If in a direct ancestor, strip off one ../ to go down */
1214 int found = FALSE;
1215
1216 j = ccline.cmdpos;
1217 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1218 while (--j > i)
1219 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001220#ifdef FEAT_MBYTE
1221 if (has_mbyte)
1222 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 if (vim_ispathsep(ccline.cmdbuff[j]))
1225 {
1226 found = TRUE;
1227 break;
1228 }
1229 }
1230 if (found
1231 && ccline.cmdbuff[j - 1] == '.'
1232 && ccline.cmdbuff[j - 2] == '.'
1233 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1234 {
1235 cmdline_del(j - 2);
1236 c = p_wc;
1237 }
1238 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001239 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 {
1241 /* go up a directory */
1242 int found = FALSE;
1243
1244 j = ccline.cmdpos - 1;
1245 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1246 while (--j > i)
1247 {
1248#ifdef FEAT_MBYTE
1249 if (has_mbyte)
1250 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1251#endif
1252 if (vim_ispathsep(ccline.cmdbuff[j])
1253#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001254 && vim_strchr((char_u *)" *?[{`$%#",
1255 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256#endif
1257 )
1258 {
1259 if (found)
1260 {
1261 i = j + 1;
1262 break;
1263 }
1264 else
1265 found = TRUE;
1266 }
1267 }
1268
1269 if (!found)
1270 j = i;
1271 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1272 j += 4;
1273 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1274 && j == i)
1275 j += 3;
1276 else
1277 j = 0;
1278 if (j > 0)
1279 {
1280 /* TODO this is only for DOS/UNIX systems - need to put in
1281 * machine-specific stuff here and in upseg init */
1282 cmdline_del(j);
1283 put_on_cmdline(upseg + 1, 3, FALSE);
1284 }
1285 else if (ccline.cmdpos > i)
1286 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001287
1288 /* Now complete in the new directory. Set KeyTyped in case the
1289 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001291 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294
1295#endif /* FEAT_WILDMENU */
1296
1297 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1298 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1299 if (c == Ctrl_BSL)
1300 {
1301 ++no_mapping;
1302 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001303 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 --no_mapping;
1305 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001306 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1307 * is in a mapping. */
1308 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1309 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 {
1311 vungetc(c);
1312 c = Ctrl_BSL;
1313 }
1314#ifdef FEAT_EVAL
1315 else if (c == 'e')
1316 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001317 char_u *p = NULL;
1318 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319
1320 /*
1321 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001322 * Need to save and restore the current command line, to be
1323 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 */
1325 if (ccline.cmdpos == ccline.cmdlen)
1326 new_cmdpos = 99999; /* keep it at the end */
1327 else
1328 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001329
1330 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001332 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 if (c == '=')
1334 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001335 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001336 * to avoid nasty things like going to another buffer when
1337 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001338 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001339 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001341 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001342 restore_cmdline(&save_ccline);
1343
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001344 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001346 len = (int)STRLEN(p);
1347 if (realloc_cmdbuff(len + 1) == OK)
1348 {
1349 ccline.cmdlen = len;
1350 STRCPY(ccline.cmdbuff, p);
1351 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001353 /* Restore the cursor or use the position set with
1354 * set_cmdline_pos(). */
1355 if (new_cmdpos > ccline.cmdlen)
1356 ccline.cmdpos = ccline.cmdlen;
1357 else
1358 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001360 KeyTyped = FALSE; /* Don't do p_wc completion. */
1361 redrawcmd();
1362 goto cmdline_changed;
1363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 }
1365 }
1366 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001367 got_int = FALSE; /* don't abandon the command line */
1368 did_emsg = FALSE;
1369 emsg_on_display = FALSE;
1370 redrawcmd();
1371 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373#endif
1374 else
1375 {
1376 if (c == Ctrl_G && p_im && restart_edit == 0)
1377 restart_edit = 'a';
1378 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1379 in history */
1380 goto returncmd; /* back to Normal mode */
1381 }
1382 }
1383
1384#ifdef FEAT_CMDWIN
1385 if (c == cedit_key || c == K_CMDWIN)
1386 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001387 if (ex_normal_busy == 0 && got_int == FALSE)
1388 {
1389 /*
1390 * Open a window to edit the command line (and history).
1391 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001392 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001393 some_key_typed = TRUE;
1394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396# ifdef FEAT_DIGRAPHS
1397 else
1398# endif
1399#endif
1400#ifdef FEAT_DIGRAPHS
1401 c = do_digraph(c);
1402#endif
1403
1404 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1405 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1406 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001407 /* In Ex mode a backslash escapes a newline. */
1408 if (exmode_active
1409 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001410 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001411 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001412 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001414 if (c == K_KENTER)
1415 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001417 else
1418 {
1419 gotesc = FALSE; /* Might have typed ESC previously, don't
1420 truncate the cmdline now. */
1421 if (ccheck_abbr(c + ABBR_OFF))
1422 goto cmdline_changed;
1423 if (!cmd_silent)
1424 {
1425 windgoto(msg_row, 0);
1426 out_flush();
1427 }
1428 break;
1429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 }
1431
1432 /*
1433 * Completion for 'wildchar' or 'wildcharm' key.
1434 * - hitting <ESC> twice means: abandon command line.
1435 * - wildcard expansion is only done when the 'wildchar' key is really
1436 * typed, not when it comes from a macro
1437 */
1438 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1439 {
1440 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1441 {
1442 /* if 'wildmode' contains "list" may still need to list */
1443 if (xpc.xp_numfiles > 1
1444 && !did_wild_list
1445 && (wim_flags[wim_index] & WIM_LIST))
1446 {
1447 (void)showmatches(&xpc, FALSE);
1448 redrawcmd();
1449 did_wild_list = TRUE;
1450 }
1451 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001452 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1453 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001455 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1456 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 else
1458 res = OK; /* don't insert 'wildchar' now */
1459 }
1460 else /* typed p_wc first time */
1461 {
1462 wim_index = 0;
1463 j = ccline.cmdpos;
1464 /* if 'wildmode' first contains "longest", get longest
1465 * common part */
1466 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001467 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1468 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001470 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1471 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 /* if interrupted while completing, behave like it failed */
1474 if (got_int)
1475 {
1476 (void)vpeekc(); /* remove <C-C> from input stream */
1477 got_int = FALSE; /* don't abandon the command line */
1478 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1479#ifdef FEAT_WILDMENU
1480 xpc.xp_context = EXPAND_NOTHING;
1481#endif
1482 goto cmdline_changed;
1483 }
1484
1485 /* when more than one match, and 'wildmode' first contains
1486 * "list", or no change and 'wildmode' contains "longest,list",
1487 * list all matches */
1488 if (res == OK && xpc.xp_numfiles > 1)
1489 {
1490 /* a "longest" that didn't do anything is skipped (but not
1491 * "list:longest") */
1492 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1493 wim_index = 1;
1494 if ((wim_flags[wim_index] & WIM_LIST)
1495#ifdef FEAT_WILDMENU
1496 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1497#endif
1498 )
1499 {
1500 if (!(wim_flags[0] & WIM_LONGEST))
1501 {
1502#ifdef FEAT_WILDMENU
1503 int p_wmnu_save = p_wmnu;
1504 p_wmnu = 0;
1505#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001506 /* remove match */
1507 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508#ifdef FEAT_WILDMENU
1509 p_wmnu = p_wmnu_save;
1510#endif
1511 }
1512#ifdef FEAT_WILDMENU
1513 (void)showmatches(&xpc, p_wmnu
1514 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1515#else
1516 (void)showmatches(&xpc, FALSE);
1517#endif
1518 redrawcmd();
1519 did_wild_list = TRUE;
1520 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001521 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1522 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001523 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001524 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1525 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 }
1527 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001528 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 }
1530#ifdef FEAT_WILDMENU
1531 else if (xpc.xp_numfiles == -1)
1532 xpc.xp_context = EXPAND_NOTHING;
1533#endif
1534 }
1535 if (wim_index < 3)
1536 ++wim_index;
1537 if (c == ESC)
1538 gotesc = TRUE;
1539 if (res == OK)
1540 goto cmdline_changed;
1541 }
1542
1543 gotesc = FALSE;
1544
1545 /* <S-Tab> goes to last match, in a clumsy way */
1546 if (c == K_S_TAB && KeyTyped)
1547 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001548 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1549 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1550 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 goto cmdline_changed;
1552 }
1553
1554 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1555 c = NL;
1556
1557 do_abbr = TRUE; /* default: check for abbreviation */
1558
1559 /*
1560 * Big switch for a typed command line character.
1561 */
1562 switch (c)
1563 {
1564 case K_BS:
1565 case Ctrl_H:
1566 case K_DEL:
1567 case K_KDEL:
1568 case Ctrl_W:
1569#ifdef FEAT_FKMAP
1570 if (cmd_fkmap && c == K_BS)
1571 c = K_DEL;
1572#endif
1573 if (c == K_KDEL)
1574 c = K_DEL;
1575
1576 /*
1577 * delete current character is the same as backspace on next
1578 * character, except at end of line
1579 */
1580 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1581 ++ccline.cmdpos;
1582#ifdef FEAT_MBYTE
1583 if (has_mbyte && c == K_DEL)
1584 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1585 ccline.cmdbuff + ccline.cmdpos);
1586#endif
1587 if (ccline.cmdpos > 0)
1588 {
1589 char_u *p;
1590
1591 j = ccline.cmdpos;
1592 p = ccline.cmdbuff + j;
1593#ifdef FEAT_MBYTE
1594 if (has_mbyte)
1595 {
1596 p = mb_prevptr(ccline.cmdbuff, p);
1597 if (c == Ctrl_W)
1598 {
1599 while (p > ccline.cmdbuff && vim_isspace(*p))
1600 p = mb_prevptr(ccline.cmdbuff, p);
1601 i = mb_get_class(p);
1602 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1603 p = mb_prevptr(ccline.cmdbuff, p);
1604 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001605 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608 else
1609#endif
1610 if (c == Ctrl_W)
1611 {
1612 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1613 --p;
1614 i = vim_iswordc(p[-1]);
1615 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1616 && vim_iswordc(p[-1]) == i)
1617 --p;
1618 }
1619 else
1620 --p;
1621 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1622 ccline.cmdlen -= j - ccline.cmdpos;
1623 i = ccline.cmdpos;
1624 while (i < ccline.cmdlen)
1625 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1626
1627 /* Truncate at the end, required for multi-byte chars. */
1628 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001629#ifdef FEAT_SEARCH_EXTRA
1630 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001631 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001632 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001633 /* save view settings, so that the screen
1634 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001635 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001636 }
1637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 redrawcmd();
1639 }
1640 else if (ccline.cmdlen == 0 && c != Ctrl_W
1641 && ccline.cmdprompt == NULL && indent == 0)
1642 {
1643 /* In ex and debug mode it doesn't make sense to return. */
1644 if (exmode_active
1645#ifdef FEAT_EVAL
1646 || ccline.cmdfirstc == '>'
1647#endif
1648 )
1649 goto cmdline_not_changed;
1650
Bram Moolenaard23a8232018-02-10 18:45:26 +01001651 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 if (!cmd_silent)
1653 {
1654#ifdef FEAT_RIGHTLEFT
1655 if (cmdmsg_rl)
1656 msg_col = Columns;
1657 else
1658#endif
1659 msg_col = 0;
1660 msg_putchar(' '); /* delete ':' */
1661 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001662#ifdef FEAT_SEARCH_EXTRA
1663 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001664 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 redraw_cmdline = TRUE;
1667 goto returncmd; /* back to cmd mode */
1668 }
1669 goto cmdline_changed;
1670
1671 case K_INS:
1672 case K_KINS:
1673#ifdef FEAT_FKMAP
1674 /* if Farsi mode set, we are in reverse insert mode -
1675 Do not change the mode */
1676 if (cmd_fkmap)
1677 beep_flush();
1678 else
1679#endif
1680 ccline.overstrike = !ccline.overstrike;
1681#ifdef CURSOR_SHAPE
1682 ui_cursor_shape(); /* may show different cursor shape */
1683#endif
1684 goto cmdline_not_changed;
1685
1686 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001687 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
1689 /* ":lmap" mappings exists, toggle use of mappings. */
1690 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001691#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 im_set_active(FALSE); /* Disable input method */
1693#endif
1694 if (b_im_ptr != NULL)
1695 {
1696 if (State & LANGMAP)
1697 *b_im_ptr = B_IMODE_LMAP;
1698 else
1699 *b_im_ptr = B_IMODE_NONE;
1700 }
1701 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 else
1704 {
1705 /* There are no ":lmap" mappings, toggle IM. When
1706 * 'imdisable' is set don't try getting the status, it's
1707 * always off. */
1708 if ((p_imdisable && b_im_ptr != NULL)
1709 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1710 {
1711 im_set_active(FALSE); /* Disable input method */
1712 if (b_im_ptr != NULL)
1713 *b_im_ptr = B_IMODE_NONE;
1714 }
1715 else
1716 {
1717 im_set_active(TRUE); /* Enable input method */
1718 if (b_im_ptr != NULL)
1719 *b_im_ptr = B_IMODE_IM;
1720 }
1721 }
1722#endif
1723 if (b_im_ptr != NULL)
1724 {
1725 if (b_im_ptr == &curbuf->b_p_iminsert)
1726 set_iminsert_global();
1727 else
1728 set_imsearch_global();
1729 }
1730#ifdef CURSOR_SHAPE
1731 ui_cursor_shape(); /* may show different cursor shape */
1732#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001733#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /* Show/unshow value of 'keymap' in status lines later. */
1735 status_redraw_curbuf();
1736#endif
1737 goto cmdline_not_changed;
1738
1739/* case '@': only in very old vi */
1740 case Ctrl_U:
1741 /* delete all characters left of the cursor */
1742 j = ccline.cmdpos;
1743 ccline.cmdlen -= j;
1744 i = ccline.cmdpos = 0;
1745 while (i < ccline.cmdlen)
1746 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1747 /* Truncate at the end, required for multi-byte chars. */
1748 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001749#ifdef FEAT_SEARCH_EXTRA
1750 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001751 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 redrawcmd();
1754 goto cmdline_changed;
1755
1756#ifdef FEAT_CLIPBOARD
1757 case Ctrl_Y:
1758 /* Copy the modeless selection, if there is one. */
1759 if (clip_star.state != SELECT_CLEARED)
1760 {
1761 if (clip_star.state == SELECT_DONE)
1762 clip_copy_modeless_selection(TRUE);
1763 goto cmdline_not_changed;
1764 }
1765 break;
1766#endif
1767
1768 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1769 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001770 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001771 * ":normal" runs out of characters. */
1772 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001773 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 goto cmdline_not_changed;
1775
1776 gotesc = TRUE; /* will free ccline.cmdbuff after
1777 putting it in history */
1778 goto returncmd; /* back to cmd mode */
1779
1780 case Ctrl_R: /* insert register */
1781#ifdef USE_ON_FLY_SCROLL
1782 dont_scroll = TRUE; /* disallow scrolling here */
1783#endif
1784 putcmdline('"', TRUE);
1785 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001786 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (i == Ctrl_O)
1788 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1789 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001790 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001791 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 --no_mapping;
1793#ifdef FEAT_EVAL
1794 /*
1795 * Insert the result of an expression.
1796 * Need to save the current command line, to be able to enter
1797 * a new one...
1798 */
1799 new_cmdpos = -1;
1800 if (c == '=')
1801 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1803 {
1804 beep_flush();
1805 c = ESC;
1806 }
1807 else
1808 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001809 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001811 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 }
1813 }
1814#endif
1815 if (c != ESC) /* use ESC to cancel inserting register */
1816 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001817 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001818
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001819#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001820 /* When there was a serious error abort getting the
1821 * command line. */
1822 if (aborting())
1823 {
1824 gotesc = TRUE; /* will free ccline.cmdbuff after
1825 putting it in history */
1826 goto returncmd; /* back to cmd mode */
1827 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 KeyTyped = FALSE; /* Don't do p_wc completion. */
1830#ifdef FEAT_EVAL
1831 if (new_cmdpos >= 0)
1832 {
1833 /* set_cmdline_pos() was used */
1834 if (new_cmdpos > ccline.cmdlen)
1835 ccline.cmdpos = ccline.cmdlen;
1836 else
1837 ccline.cmdpos = new_cmdpos;
1838 }
1839#endif
1840 }
1841 redrawcmd();
1842 goto cmdline_changed;
1843
1844 case Ctrl_D:
1845 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1846 break; /* Use ^D as normal char instead */
1847
1848 redrawcmd();
1849 continue; /* don't do incremental search now */
1850
1851 case K_RIGHT:
1852 case K_S_RIGHT:
1853 case K_C_RIGHT:
1854 do
1855 {
1856 if (ccline.cmdpos >= ccline.cmdlen)
1857 break;
1858 i = cmdline_charsize(ccline.cmdpos);
1859 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1860 break;
1861 ccline.cmdspos += i;
1862#ifdef FEAT_MBYTE
1863 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001864 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 + ccline.cmdpos);
1866 else
1867#endif
1868 ++ccline.cmdpos;
1869 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001870 while ((c == K_S_RIGHT || c == K_C_RIGHT
1871 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1873#ifdef FEAT_MBYTE
1874 if (has_mbyte)
1875 set_cmdspos_cursor();
1876#endif
1877 goto cmdline_not_changed;
1878
1879 case K_LEFT:
1880 case K_S_LEFT:
1881 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001882 if (ccline.cmdpos == 0)
1883 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 do
1885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 --ccline.cmdpos;
1887#ifdef FEAT_MBYTE
1888 if (has_mbyte) /* move to first byte of char */
1889 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1890 ccline.cmdbuff + ccline.cmdpos);
1891#endif
1892 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1893 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001894 while (ccline.cmdpos > 0
1895 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001896 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1898#ifdef FEAT_MBYTE
1899 if (has_mbyte)
1900 set_cmdspos_cursor();
1901#endif
1902 goto cmdline_not_changed;
1903
1904 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001905 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001906 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
Bram Moolenaar4770d092006-01-12 23:22:24 +00001908#ifdef FEAT_GUI_W32
1909 /* On Win32 ignore <M-F4>, we get it when closing the window was
1910 * cancelled. */
1911 case K_F4:
1912 if (mod_mask == MOD_MASK_ALT)
1913 {
1914 redrawcmd(); /* somehow the cmdline is cleared */
1915 goto cmdline_not_changed;
1916 }
1917 break;
1918#endif
1919
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920#ifdef FEAT_MOUSE
1921 case K_MIDDLEDRAG:
1922 case K_MIDDLERELEASE:
1923 goto cmdline_not_changed; /* Ignore mouse */
1924
1925 case K_MIDDLEMOUSE:
1926# ifdef FEAT_GUI
1927 /* When GUI is active, also paste when 'mouse' is empty */
1928 if (!gui.in_use)
1929# endif
1930 if (!mouse_has(MOUSE_COMMAND))
1931 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001932# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001934 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001936# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001937 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 redrawcmd();
1939 goto cmdline_changed;
1940
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001941# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001943 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 redrawcmd();
1945 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 case K_LEFTDRAG:
1949 case K_LEFTRELEASE:
1950 case K_RIGHTDRAG:
1951 case K_RIGHTRELEASE:
1952 /* Ignore drag and release events when the button-down wasn't
1953 * seen before. */
1954 if (ignore_drag_release)
1955 goto cmdline_not_changed;
1956 /* FALLTHROUGH */
1957 case K_LEFTMOUSE:
1958 case K_RIGHTMOUSE:
1959 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1960 ignore_drag_release = TRUE;
1961 else
1962 ignore_drag_release = FALSE;
1963# ifdef FEAT_GUI
1964 /* When GUI is active, also move when 'mouse' is empty */
1965 if (!gui.in_use)
1966# endif
1967 if (!mouse_has(MOUSE_COMMAND))
1968 goto cmdline_not_changed; /* Ignore mouse */
1969# ifdef FEAT_CLIPBOARD
1970 if (mouse_row < cmdline_row && clip_star.available)
1971 {
1972 int button, is_click, is_drag;
1973
1974 /*
1975 * Handle modeless selection.
1976 */
1977 button = get_mouse_button(KEY2TERMCAP1(c),
1978 &is_click, &is_drag);
1979 if (mouse_model_popup() && button == MOUSE_LEFT
1980 && (mod_mask & MOD_MASK_SHIFT))
1981 {
1982 /* Translate shift-left to right button. */
1983 button = MOUSE_RIGHT;
1984 mod_mask &= ~MOD_MASK_SHIFT;
1985 }
1986 clip_modeless(button, is_click, is_drag);
1987 goto cmdline_not_changed;
1988 }
1989# endif
1990
1991 set_cmdspos();
1992 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1993 ++ccline.cmdpos)
1994 {
1995 i = cmdline_charsize(ccline.cmdpos);
1996 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1997 && mouse_col < ccline.cmdspos % Columns + i)
1998 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001999# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 if (has_mbyte)
2001 {
2002 /* Count ">" for double-wide char that doesn't fit. */
2003 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002004 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 + ccline.cmdpos) - 1;
2006 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 ccline.cmdspos += i;
2009 }
2010 goto cmdline_not_changed;
2011
2012 /* Mouse scroll wheel: ignored here */
2013 case K_MOUSEDOWN:
2014 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002015 case K_MOUSELEFT:
2016 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 /* Alternate buttons ignored here */
2018 case K_X1MOUSE:
2019 case K_X1DRAG:
2020 case K_X1RELEASE:
2021 case K_X2MOUSE:
2022 case K_X2DRAG:
2023 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002024 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 goto cmdline_not_changed;
2026
2027#endif /* FEAT_MOUSE */
2028
2029#ifdef FEAT_GUI
2030 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2031 case K_LEFTRELEASE_NM:
2032 goto cmdline_not_changed;
2033
2034 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002035 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
2037 gui_do_scroll();
2038 redrawcmd();
2039 }
2040 goto cmdline_not_changed;
2041
2042 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002043 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002045 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 redrawcmd();
2047 }
2048 goto cmdline_not_changed;
2049#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002050#ifdef FEAT_GUI_TABLINE
2051 case K_TABLINE:
2052 case K_TABMENU:
2053 /* Don't want to change any tabs here. Make sure the same tab
2054 * is still selected. */
2055 if (gui_use_tabline())
2056 gui_mch_set_curtab(tabpage_index(curtab));
2057 goto cmdline_not_changed;
2058#endif
2059
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 case K_SELECT: /* end of Select mode mapping - ignore */
2061 goto cmdline_not_changed;
2062
2063 case Ctrl_B: /* begin of command line */
2064 case K_HOME:
2065 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 case K_S_HOME:
2067 case K_C_HOME:
2068 ccline.cmdpos = 0;
2069 set_cmdspos();
2070 goto cmdline_not_changed;
2071
2072 case Ctrl_E: /* end of command line */
2073 case K_END:
2074 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 case K_S_END:
2076 case K_C_END:
2077 ccline.cmdpos = ccline.cmdlen;
2078 set_cmdspos_cursor();
2079 goto cmdline_not_changed;
2080
2081 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002082 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 break;
2084 goto cmdline_changed;
2085
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002086 case Ctrl_L:
2087#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002088 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002089 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002090#endif
2091
2092 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002093 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 break;
2095 goto cmdline_changed;
2096
2097 case Ctrl_N: /* next match */
2098 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002099 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002101 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2102 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002104 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002107 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 case K_UP:
2109 case K_DOWN:
2110 case K_S_UP:
2111 case K_S_DOWN:
2112 case K_PAGEUP:
2113 case K_KPAGEUP:
2114 case K_PAGEDOWN:
2115 case K_KPAGEDOWN:
2116 if (hislen == 0 || firstc == NUL) /* no history */
2117 goto cmdline_not_changed;
2118
2119 i = hiscnt;
2120
2121 /* save current command string so it can be restored later */
2122 if (lookfor == NULL)
2123 {
2124 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2125 goto cmdline_not_changed;
2126 lookfor[ccline.cmdpos] = NUL;
2127 }
2128
2129 j = (int)STRLEN(lookfor);
2130 for (;;)
2131 {
2132 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002133 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002134 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 {
2136 if (hiscnt == hislen) /* first time */
2137 hiscnt = hisidx[histype];
2138 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2139 hiscnt = hislen - 1;
2140 else if (hiscnt != hisidx[histype] + 1)
2141 --hiscnt;
2142 else /* at top of list */
2143 {
2144 hiscnt = i;
2145 break;
2146 }
2147 }
2148 else /* one step forwards */
2149 {
2150 /* on last entry, clear the line */
2151 if (hiscnt == hisidx[histype])
2152 {
2153 hiscnt = hislen;
2154 break;
2155 }
2156
2157 /* not on a history line, nothing to do */
2158 if (hiscnt == hislen)
2159 break;
2160 if (hiscnt == hislen - 1) /* wrap around */
2161 hiscnt = 0;
2162 else
2163 ++hiscnt;
2164 }
2165 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2166 {
2167 hiscnt = i;
2168 break;
2169 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002170 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002171 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 || STRNCMP(history[histype][hiscnt].hisstr,
2173 lookfor, (size_t)j) == 0)
2174 break;
2175 }
2176
2177 if (hiscnt != i) /* jumped to other entry */
2178 {
2179 char_u *p;
2180 int len;
2181 int old_firstc;
2182
2183 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002184 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 if (hiscnt == hislen)
2186 p = lookfor; /* back to the old one */
2187 else
2188 p = history[histype][hiscnt].hisstr;
2189
2190 if (histype == HIST_SEARCH
2191 && p != lookfor
2192 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2193 {
2194 /* Correct for the separator character used when
2195 * adding the history entry vs the one used now.
2196 * First loop: count length.
2197 * Second loop: copy the characters. */
2198 for (i = 0; i <= 1; ++i)
2199 {
2200 len = 0;
2201 for (j = 0; p[j] != NUL; ++j)
2202 {
2203 /* Replace old sep with new sep, unless it is
2204 * escaped. */
2205 if (p[j] == old_firstc
2206 && (j == 0 || p[j - 1] != '\\'))
2207 {
2208 if (i > 0)
2209 ccline.cmdbuff[len] = firstc;
2210 }
2211 else
2212 {
2213 /* Escape new sep, unless it is already
2214 * escaped. */
2215 if (p[j] == firstc
2216 && (j == 0 || p[j - 1] != '\\'))
2217 {
2218 if (i > 0)
2219 ccline.cmdbuff[len] = '\\';
2220 ++len;
2221 }
2222 if (i > 0)
2223 ccline.cmdbuff[len] = p[j];
2224 }
2225 ++len;
2226 }
2227 if (i == 0)
2228 {
2229 alloc_cmdbuff(len);
2230 if (ccline.cmdbuff == NULL)
2231 goto returncmd;
2232 }
2233 }
2234 ccline.cmdbuff[len] = NUL;
2235 }
2236 else
2237 {
2238 alloc_cmdbuff((int)STRLEN(p));
2239 if (ccline.cmdbuff == NULL)
2240 goto returncmd;
2241 STRCPY(ccline.cmdbuff, p);
2242 }
2243
2244 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2245 redrawcmd();
2246 goto cmdline_changed;
2247 }
2248 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002249#endif
2250 goto cmdline_not_changed;
2251
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002252#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002253 case Ctrl_G: /* next match */
2254 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002255 if (may_adjust_incsearch_highlighting(
2256 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002257 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002258 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259#endif
2260
2261 case Ctrl_V:
2262 case Ctrl_Q:
2263#ifdef FEAT_MOUSE
2264 ignore_drag_release = TRUE;
2265#endif
2266 putcmdline('^', TRUE);
2267 c = get_literal(); /* get next (two) character(s) */
2268 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002269 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270#ifdef FEAT_MBYTE
2271 /* may need to remove ^ when composing char was typed */
2272 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2273 {
2274 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2275 msg_putchar(' ');
2276 cursorcmd();
2277 }
2278#endif
2279 break;
2280
2281#ifdef FEAT_DIGRAPHS
2282 case Ctrl_K:
2283#ifdef FEAT_MOUSE
2284 ignore_drag_release = TRUE;
2285#endif
2286 putcmdline('?', TRUE);
2287#ifdef USE_ON_FLY_SCROLL
2288 dont_scroll = TRUE; /* disallow scrolling here */
2289#endif
2290 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002291 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (c != NUL)
2293 break;
2294
2295 redrawcmd();
2296 goto cmdline_not_changed;
2297#endif /* FEAT_DIGRAPHS */
2298
2299#ifdef FEAT_RIGHTLEFT
2300 case Ctrl__: /* CTRL-_: switch language mode */
2301 if (!p_ari)
2302 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002303# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 if (p_altkeymap)
2305 {
2306 cmd_fkmap = !cmd_fkmap;
2307 if (cmd_fkmap) /* in Farsi always in Insert mode */
2308 ccline.overstrike = FALSE;
2309 }
2310 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002311# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 cmd_hkmap = !cmd_hkmap;
2313 goto cmdline_not_changed;
2314#endif
2315
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002316 case K_PS:
2317 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2318 goto cmdline_changed;
2319
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 default:
2321#ifdef UNIX
2322 if (c == intr_char)
2323 {
2324 gotesc = TRUE; /* will free ccline.cmdbuff after
2325 putting it in history */
2326 goto returncmd; /* back to Normal mode */
2327 }
2328#endif
2329 /*
2330 * Normal character with no special meaning. Just set mod_mask
2331 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2332 * the string <S-Space>. This should only happen after ^V.
2333 */
2334 if (!IS_SPECIAL(c))
2335 mod_mask = 0x0;
2336 break;
2337 }
2338 /*
2339 * End of switch on command line character.
2340 * We come here if we have a normal character.
2341 */
2342
Bram Moolenaarede3e632013-06-23 16:16:19 +02002343 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344#ifdef FEAT_MBYTE
2345 /* Add ABBR_OFF for characters above 0x100, this is
2346 * what check_abbr() expects. */
2347 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2348#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002349 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 goto cmdline_changed;
2351
2352 /*
2353 * put the character in the command line
2354 */
2355 if (IS_SPECIAL(c) || mod_mask != 0)
2356 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2357 else
2358 {
2359#ifdef FEAT_MBYTE
2360 if (has_mbyte)
2361 {
2362 j = (*mb_char2bytes)(c, IObuff);
2363 IObuff[j] = NUL; /* exclude composing chars */
2364 put_on_cmdline(IObuff, j, TRUE);
2365 }
2366 else
2367#endif
2368 {
2369 IObuff[0] = c;
2370 put_on_cmdline(IObuff, 1, TRUE);
2371 }
2372 }
2373 goto cmdline_changed;
2374
2375/*
2376 * This part implements incremental searches for "/" and "?"
2377 * Jump to cmdline_not_changed when a character has been read but the command
2378 * line did not change. Then we only search and redraw if something changed in
2379 * the past.
2380 * Jump to cmdline_changed when the command line did change.
2381 * (Sorry for the goto's, I know it is ugly).
2382 */
2383cmdline_not_changed:
2384#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002385 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 continue;
2387#endif
2388
2389cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002390 /* Trigger CmdlineChanged autocommands. */
2391 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002392
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002394 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#endif
2396
2397#ifdef FEAT_RIGHTLEFT
2398 if (cmdmsg_rl
2399# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002400 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401# endif
2402 )
2403 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002404 * right-left typing. Not efficient, but it works.
2405 * Do it only when there are no characters left to read
2406 * to avoid useless intermediate redraws. */
2407 if (vpeekc() == NUL)
2408 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409#endif
2410 }
2411
2412returncmd:
2413
2414#ifdef FEAT_RIGHTLEFT
2415 cmdmsg_rl = FALSE;
2416#endif
2417
2418#ifdef FEAT_FKMAP
2419 cmd_fkmap = 0;
2420#endif
2421
2422 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002423 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424
2425#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002426 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#endif
2428
2429 if (ccline.cmdbuff != NULL)
2430 {
2431 /*
2432 * Put line in history buffer (":" and "=" only when it was typed).
2433 */
2434#ifdef FEAT_CMDHIST
2435 if (ccline.cmdlen && firstc != NUL
2436 && (some_key_typed || histype == HIST_SEARCH))
2437 {
2438 add_to_history(histype, ccline.cmdbuff, TRUE,
2439 histype == HIST_SEARCH ? firstc : NUL);
2440 if (firstc == ':')
2441 {
2442 vim_free(new_last_cmdline);
2443 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2444 }
2445 }
2446#endif
2447
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002448 if (gotesc)
2449 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451
2452 /*
2453 * If the screen was shifted up, redraw the whole screen (later).
2454 * If the line is too long, clear it, so ruler and shown command do
2455 * not get printed in the middle of it.
2456 */
2457 msg_check();
2458 msg_scroll = save_msg_scroll;
2459 redir_off = FALSE;
2460
2461 /* When the command line was typed, no need for a wait-return prompt. */
2462 if (some_key_typed)
2463 need_wait_return = FALSE;
2464
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002465 /* Trigger CmdlineLeave autocommands. */
2466 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002467
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002469#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2471 im_save_status(b_im_ptr);
2472 im_set_active(FALSE);
2473#endif
2474#ifdef FEAT_MOUSE
2475 setmouse();
2476#endif
2477#ifdef CURSOR_SHAPE
2478 ui_cursor_shape(); /* may show different cursor shape */
2479#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002480 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002482 {
2483 char_u *p = ccline.cmdbuff;
2484
2485 /* Make ccline empty, getcmdline() may try to use it. */
2486 ccline.cmdbuff = NULL;
2487 return p;
2488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489}
2490
2491#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2492/*
2493 * Get a command line with a prompt.
2494 * This is prepared to be called recursively from getcmdline() (e.g. by
2495 * f_input() when evaluating an expression from CTRL-R =).
2496 * Returns the command line in allocated memory, or NULL.
2497 */
2498 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002499getcmdline_prompt(
2500 int firstc,
2501 char_u *prompt, /* command line prompt */
2502 int attr, /* attributes for prompt */
2503 int xp_context, /* type of expansion */
2504 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
2506 char_u *s;
2507 struct cmdline_info save_ccline;
2508 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002509 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002511 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 ccline.cmdprompt = prompt;
2513 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002514# ifdef FEAT_EVAL
2515 ccline.xp_context = xp_context;
2516 ccline.xp_arg = xp_arg;
2517 ccline.input_fn = (firstc == '@');
2518# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002519 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002521 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002522 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002523 /* Restore msg_col, the prompt from input() may have changed it.
2524 * But only if called recursively and the commandline is therefore being
2525 * restored to an old one; if not, the input() prompt stays on the screen,
2526 * so we need its modified msg_col left intact. */
2527 if (ccline.cmdbuff != NULL)
2528 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529
2530 return s;
2531}
2532#endif
2533
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002534/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002535 * Return TRUE when the text must not be changed and we can't switch to
2536 * another window or buffer. Used when editing the command line, evaluating
2537 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002538 */
2539 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002540text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002541{
2542#ifdef FEAT_CMDWIN
2543 if (cmdwin_type != 0)
2544 return TRUE;
2545#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002546 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002547}
2548
2549/*
2550 * Give an error message for a command that isn't allowed while the cmdline
2551 * window is open or editing the cmdline in another way.
2552 */
2553 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002554text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002555{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002556 EMSG(_(get_text_locked_msg()));
2557}
2558
2559 char_u *
2560get_text_locked_msg(void)
2561{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562#ifdef FEAT_CMDWIN
2563 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002564 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002565#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002566 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567}
2568
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002569/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002570 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2571 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002572 */
2573 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002574curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002575{
2576 if (curbuf_lock > 0)
2577 {
2578 EMSG(_("E788: Not allowed to edit another buffer now"));
2579 return TRUE;
2580 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002581 return allbuf_locked();
2582}
2583
2584/*
2585 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2586 * message.
2587 */
2588 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002589allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002590{
2591 if (allbuf_lock > 0)
2592 {
2593 EMSG(_("E811: Not allowed to change buffer information now"));
2594 return TRUE;
2595 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002596 return FALSE;
2597}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002598
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002600cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601{
2602#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2603 if (cmdline_star > 0) /* showing '*', always 1 position */
2604 return 1;
2605#endif
2606 return ptr2cells(ccline.cmdbuff + idx);
2607}
2608
2609/*
2610 * Compute the offset of the cursor on the command line for the prompt and
2611 * indent.
2612 */
2613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002616 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 ccline.cmdspos = 1 + ccline.cmdindent;
2618 else
2619 ccline.cmdspos = 0 + ccline.cmdindent;
2620}
2621
2622/*
2623 * Compute the screen position for the cursor on the command line.
2624 */
2625 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002626set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627{
2628 int i, m, c;
2629
2630 set_cmdspos();
2631 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002634 if (m < 0) /* overflow, Columns or Rows at weird value */
2635 m = MAXCOL;
2636 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 else
2638 m = MAXCOL;
2639 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2640 {
2641 c = cmdline_charsize(i);
2642#ifdef FEAT_MBYTE
2643 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2644 if (has_mbyte)
2645 correct_cmdspos(i, c);
2646#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002647 /* If the cmdline doesn't fit, show cursor on last visible char.
2648 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 if ((ccline.cmdspos += c) >= m)
2650 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 ccline.cmdspos -= c;
2652 break;
2653 }
2654#ifdef FEAT_MBYTE
2655 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002656 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657#endif
2658 }
2659}
2660
2661#ifdef FEAT_MBYTE
2662/*
2663 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2664 * character that doesn't fit, so that a ">" must be displayed.
2665 */
2666 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002667correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002669 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2671 && ccline.cmdspos % Columns + cells > Columns)
2672 ccline.cmdspos++;
2673}
2674#endif
2675
2676/*
2677 * Get an Ex command line for the ":" command.
2678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002680getexline(
2681 int c, /* normally ':', NUL for ":append" */
2682 void *cookie UNUSED,
2683 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
2685 /* When executing a register, remove ':' that's in front of each line. */
2686 if (exec_from_reg && vpeekc() == ':')
2687 (void)vgetc();
2688 return getcmdline(c, 1L, indent);
2689}
2690
2691/*
2692 * Get an Ex command line for Ex mode.
2693 * In Ex mode we only use the OS supplied line editing features and no
2694 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002695 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002698getexmodeline(
2699 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002700 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002701 void *cookie UNUSED,
2702 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 garray_T line_ga;
2705 char_u *pend;
2706 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002707 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 int escaped = FALSE; /* CTRL-V typed */
2709 int vcol = 0;
2710 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002711 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002712 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713
2714 /* Switch cursor on now. This avoids that it happens after the "\n", which
2715 * confuses the system function that computes tabstops. */
2716 cursor_on();
2717
2718 /* always start in column 0; write a newline if necessary */
2719 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002720 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002724 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002725 if (p_prompt)
2726 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 while (indent-- > 0)
2728 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731
2732 ga_init2(&line_ga, 1, 30);
2733
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002734 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002735 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002736 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002737 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002738 while (indent >= 8)
2739 {
2740 ga_append(&line_ga, TAB);
2741 msg_puts((char_u *)" ");
2742 indent -= 8;
2743 }
2744 while (indent-- > 0)
2745 {
2746 ga_append(&line_ga, ' ');
2747 msg_putchar(' ');
2748 }
2749 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 ++no_mapping;
2751 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002752
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 /*
2754 * Get the line, one character at a time.
2755 */
2756 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002757 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002759 long sw;
2760 char_u *s;
2761
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (ga_grow(&line_ga, 40) == FAIL)
2763 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764
Bram Moolenaarba748c82017-02-26 14:00:07 +01002765 /*
2766 * Get one character at a time.
2767 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002768 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002769
2770 /* Check for a ":normal" command and no more characters left. */
2771 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2772 c1 = '\n';
2773 else
2774 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
2776 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002777 * Handle line editing.
2778 * Previously this was left to the system, putting the terminal in
2779 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 msg_putchar('\n');
2784 break;
2785 }
2786
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002787 if (c1 == K_PS)
2788 {
2789 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2790 goto redraw;
2791 }
2792
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 if (!escaped)
2794 {
2795 /* CR typed means "enter", which is NL */
2796 if (c1 == '\r')
2797 c1 = '\n';
2798
2799 if (c1 == BS || c1 == K_BS
2800 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 if (line_ga.ga_len > 0)
2803 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002804#ifdef FEAT_MBYTE
2805 if (has_mbyte)
2806 {
2807 p = (char_u *)line_ga.ga_data;
2808 p[line_ga.ga_len] = NUL;
2809 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2810 line_ga.ga_len -= len;
2811 }
2812 else
2813#endif
2814 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815 goto redraw;
2816 }
2817 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 }
2819
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 msg_col = startcol;
2823 msg_clr_eos();
2824 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002825 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 }
2827
2828 if (c1 == Ctrl_T)
2829 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002830 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 p = (char_u *)line_ga.ga_data;
2832 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002833 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002834 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002836 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002838 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002839 p = (char_u *)line_ga.ga_data;
2840 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2842 *s = ' ';
2843 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845redraw:
2846 /* redraw the line */
2847 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002849 p = (char_u *)line_ga.ga_data;
2850 p[line_ga.ga_len] = NUL;
2851 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 msg_putchar(' ');
2858 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002859 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002863 len = MB_PTR2LEN(p);
2864 msg_outtrans_len(p, len);
2865 vcol += ptr2cells(p);
2866 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 }
2868 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002870 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 continue;
2872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 if (c1 == Ctrl_D)
2875 {
2876 /* Delete one shiftwidth. */
2877 p = (char_u *)line_ga.ga_data;
2878 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002880 if (prev_char == '^')
2881 ex_keep_indent = TRUE;
2882 indent = 0;
2883 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 else
2886 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002887 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002888 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002889 if (indent > 0)
2890 {
2891 --indent;
2892 indent -= indent % get_sw_value(curbuf);
2893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002895 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002896 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002897 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2899 --line_ga.ga_len;
2900 }
2901 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903
2904 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2905 {
2906 escaped = TRUE;
2907 continue;
2908 }
2909
2910 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2911 if (IS_SPECIAL(c1))
2912 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002914
2915 if (IS_SPECIAL(c1))
2916 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002917#ifdef FEAT_MBYTE
2918 if (has_mbyte)
2919 len = (*mb_char2bytes)(c1,
2920 (char_u *)line_ga.ga_data + line_ga.ga_len);
2921 else
2922#endif
2923 {
2924 len = 1;
2925 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2926 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002927 if (c1 == '\n')
2928 msg_putchar('\n');
2929 else if (c1 == TAB)
2930 {
2931 /* Don't use chartabsize(), 'ts' can be different */
2932 do
2933 {
2934 msg_putchar(' ');
2935 } while (++vcol % 8);
2936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002939 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002940 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002943 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944 escaped = FALSE;
2945
2946 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002947 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002948
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002949 /* We are done when a NL is entered, but not when it comes after an
2950 * odd number of backslashes, that results in a NUL. */
2951 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002953 int bcount = 0;
2954
2955 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2956 ++bcount;
2957
2958 if (bcount > 0)
2959 {
2960 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2961 * "\NL", etc. */
2962 line_ga.ga_len -= (bcount + 1) / 2;
2963 pend -= (bcount + 1) / 2;
2964 pend[-1] = '\n';
2965 }
2966
2967 if ((bcount & 1) == 0)
2968 {
2969 --line_ga.ga_len;
2970 --pend;
2971 *pend = NUL;
2972 break;
2973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 }
2975 }
2976
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002977 --no_mapping;
2978 --allow_keys;
2979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 /* make following messages go to the next line */
2981 msg_didout = FALSE;
2982 msg_col = 0;
2983 if (msg_row < Rows - 1)
2984 ++msg_row;
2985 emsg_on_display = FALSE; /* don't want ui_delay() */
2986
2987 if (got_int)
2988 ga_clear(&line_ga);
2989
2990 return (char_u *)line_ga.ga_data;
2991}
2992
Bram Moolenaare344bea2005-09-01 20:46:49 +00002993# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2994 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995/*
2996 * Return TRUE if ccline.overstrike is on.
2997 */
2998 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002999cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
3001 return ccline.overstrike;
3002}
3003
3004/*
3005 * Return TRUE if the cursor is at the end of the cmdline.
3006 */
3007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003008cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009{
3010 return (ccline.cmdpos >= ccline.cmdlen);
3011}
3012#endif
3013
Bram Moolenaar9372a112005-12-06 19:59:18 +00003014#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015/*
3016 * Return the virtual column number at the current cursor position.
3017 * This is used by the IM code to obtain the start of the preedit string.
3018 */
3019 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003020cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021{
3022 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3023 return MAXCOL;
3024
3025# ifdef FEAT_MBYTE
3026 if (has_mbyte)
3027 {
3028 colnr_T col;
3029 int i = 0;
3030
3031 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003032 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033
3034 return col;
3035 }
3036 else
3037# endif
3038 return ccline.cmdpos;
3039}
3040#endif
3041
3042#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3043/*
3044 * If part of the command line is an IM preedit string, redraw it with
3045 * IM feedback attributes. The cursor position is restored after drawing.
3046 */
3047 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003048redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049{
3050 if ((State & CMDLINE)
3051 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003052 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 && !p_imdisable
3054 && im_is_preediting())
3055 {
3056 int cmdpos = 0;
3057 int cmdspos;
3058 int old_row;
3059 int old_col;
3060 colnr_T col;
3061
3062 old_row = msg_row;
3063 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003064 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
3066# ifdef FEAT_MBYTE
3067 if (has_mbyte)
3068 {
3069 for (col = 0; col < preedit_start_col
3070 && cmdpos < ccline.cmdlen; ++col)
3071 {
3072 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003073 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 }
3075 }
3076 else
3077# endif
3078 {
3079 cmdspos += preedit_start_col;
3080 cmdpos += preedit_start_col;
3081 }
3082
3083 msg_row = cmdline_row + (cmdspos / (int)Columns);
3084 msg_col = cmdspos % (int)Columns;
3085 if (msg_row >= Rows)
3086 msg_row = Rows - 1;
3087
3088 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3089 {
3090 int char_len;
3091 int char_attr;
3092
3093 char_attr = im_get_feedback_attr(col);
3094 if (char_attr < 0)
3095 break; /* end of preedit string */
3096
3097# ifdef FEAT_MBYTE
3098 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003099 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
3101# endif
3102 char_len = 1;
3103
3104 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3105 cmdpos += char_len;
3106 }
3107
3108 msg_row = old_row;
3109 msg_col = old_col;
3110 }
3111}
3112#endif /* FEAT_XIM && FEAT_GUI_GTK */
3113
3114/*
3115 * Allocate a new command line buffer.
3116 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3117 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3118 */
3119 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003120alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121{
3122 /*
3123 * give some extra space to avoid having to allocate all the time
3124 */
3125 if (len < 80)
3126 len = 100;
3127 else
3128 len += 20;
3129
3130 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3131 ccline.cmdbufflen = len;
3132}
3133
3134/*
3135 * Re-allocate the command line to length len + something extra.
3136 * return FAIL for failure, OK otherwise
3137 */
3138 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003139realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
3141 char_u *p;
3142
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003143 if (len < ccline.cmdbufflen)
3144 return OK; /* no need to resize */
3145
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 p = ccline.cmdbuff;
3147 alloc_cmdbuff(len); /* will get some more */
3148 if (ccline.cmdbuff == NULL) /* out of memory */
3149 {
3150 ccline.cmdbuff = p; /* keep the old one */
3151 return FAIL;
3152 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003153 /* There isn't always a NUL after the command, but it may need to be
3154 * there, thus copy up to the NUL and add a NUL. */
3155 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3156 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003158
3159 if (ccline.xpc != NULL
3160 && ccline.xpc->xp_pattern != NULL
3161 && ccline.xpc->xp_context != EXPAND_NOTHING
3162 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3163 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003164 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003165
3166 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3167 * to point into the newly allocated memory. */
3168 if (i >= 0 && i <= ccline.cmdlen)
3169 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3170 }
3171
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 return OK;
3173}
3174
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003175#if defined(FEAT_ARABIC) || defined(PROTO)
3176static char_u *arshape_buf = NULL;
3177
3178# if defined(EXITFREE) || defined(PROTO)
3179 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003180free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003181{
3182 vim_free(arshape_buf);
3183}
3184# endif
3185#endif
3186
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187/*
3188 * Draw part of the cmdline at the current cursor position. But draw stars
3189 * when cmdline_star is TRUE.
3190 */
3191 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003192draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193{
3194#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3195 int i;
3196
3197 if (cmdline_star > 0)
3198 for (i = 0; i < len; ++i)
3199 {
3200 msg_putchar('*');
3201# ifdef FEAT_MBYTE
3202 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003203 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204# endif
3205 }
3206 else
3207#endif
3208#ifdef FEAT_ARABIC
3209 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3210 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 static int buflen = 0;
3212 char_u *p;
3213 int j;
3214 int newlen = 0;
3215 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003216 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int prev_c = 0;
3218 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003219 int u8c;
3220 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222
3223 /*
3224 * Do arabic shaping into a temporary buffer. This is very
3225 * inefficient!
3226 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003227 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 {
3229 /* Re-allocate the buffer. We keep it around to avoid a lot of
3230 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003231 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003232 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003233 arshape_buf = alloc(buflen);
3234 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 return; /* out of memory */
3236 }
3237
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003238 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3239 {
3240 /* Prepend a space to draw the leading composing char on. */
3241 arshape_buf[0] = ' ';
3242 newlen = 1;
3243 }
3244
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 for (j = start; j < start + len; j += mb_l)
3246 {
3247 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003248 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003249 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (ARABIC_CHAR(u8c))
3251 {
3252 /* Do Arabic shaping. */
3253 if (cmdmsg_rl)
3254 {
3255 /* displaying from right to left */
3256 pc = prev_c;
3257 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003258 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 if (j + mb_l >= start + len)
3260 nc = NUL;
3261 else
3262 nc = utf_ptr2char(p + mb_l);
3263 }
3264 else
3265 {
3266 /* displaying from left to right */
3267 if (j + mb_l >= start + len)
3268 pc = NUL;
3269 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003270 {
3271 int pcc[MAX_MCO];
3272
3273 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003275 pc1 = pcc[0];
3276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 nc = prev_c;
3278 }
3279 prev_c = u8c;
3280
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003281 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003283 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003284 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003286 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3287 if (u8cc[1] != 0)
3288 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003289 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 }
3291 }
3292 else
3293 {
3294 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003295 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 newlen += mb_l;
3297 }
3298 }
3299
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003300 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 }
3302 else
3303#endif
3304 msg_outtrans_len(ccline.cmdbuff + start, len);
3305}
3306
3307/*
3308 * Put a character on the command line. Shifts the following text to the
3309 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3310 * "c" must be printable (fit in one display cell)!
3311 */
3312 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003313putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314{
3315 if (cmd_silent)
3316 return;
3317 msg_no_more = TRUE;
3318 msg_putchar(c);
3319 if (shift)
3320 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3321 msg_no_more = FALSE;
3322 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003323 extra_char = c;
3324 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327/*
3328 * Undo a putcmdline(c, FALSE).
3329 */
3330 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003331unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332{
3333 if (cmd_silent)
3334 return;
3335 msg_no_more = TRUE;
3336 if (ccline.cmdlen == ccline.cmdpos)
3337 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003338#ifdef FEAT_MBYTE
3339 else if (has_mbyte)
3340 draw_cmdline(ccline.cmdpos,
3341 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 else
3344 draw_cmdline(ccline.cmdpos, 1);
3345 msg_no_more = FALSE;
3346 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003347 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348}
3349
3350/*
3351 * Put the given string, of the given length, onto the command line.
3352 * If len is -1, then STRLEN() is used to calculate the length.
3353 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3354 * part will be redrawn, otherwise it will not. If this function is called
3355 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3356 * called afterwards.
3357 */
3358 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003359put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 int retval;
3362 int i;
3363 int m;
3364 int c;
3365
3366 if (len < 0)
3367 len = (int)STRLEN(str);
3368
3369 /* Check if ccline.cmdbuff needs to be longer */
3370 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003371 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
3373 retval = OK;
3374 if (retval == OK)
3375 {
3376 if (!ccline.overstrike)
3377 {
3378 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3379 ccline.cmdbuff + ccline.cmdpos,
3380 (size_t)(ccline.cmdlen - ccline.cmdpos));
3381 ccline.cmdlen += len;
3382 }
3383 else
3384 {
3385#ifdef FEAT_MBYTE
3386 if (has_mbyte)
3387 {
3388 /* Count nr of characters in the new string. */
3389 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003390 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 ++m;
3392 /* Count nr of bytes in cmdline that are overwritten by these
3393 * characters. */
3394 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003395 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396 --m;
3397 if (i < ccline.cmdlen)
3398 {
3399 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3400 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3401 ccline.cmdlen += ccline.cmdpos + len - i;
3402 }
3403 else
3404 ccline.cmdlen = ccline.cmdpos + len;
3405 }
3406 else
3407#endif
3408 if (ccline.cmdpos + len > ccline.cmdlen)
3409 ccline.cmdlen = ccline.cmdpos + len;
3410 }
3411 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3412 ccline.cmdbuff[ccline.cmdlen] = NUL;
3413
3414#ifdef FEAT_MBYTE
3415 if (enc_utf8)
3416 {
3417 /* When the inserted text starts with a composing character,
3418 * backup to the character before it. There could be two of them.
3419 */
3420 i = 0;
3421 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3422 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3423 {
3424 i = (*mb_head_off)(ccline.cmdbuff,
3425 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3426 ccline.cmdpos -= i;
3427 len += i;
3428 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3429 }
3430# ifdef FEAT_ARABIC
3431 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3432 {
3433 /* Check the previous character for Arabic combining pair. */
3434 i = (*mb_head_off)(ccline.cmdbuff,
3435 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3436 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3437 + ccline.cmdpos - i), c))
3438 {
3439 ccline.cmdpos -= i;
3440 len += i;
3441 }
3442 else
3443 i = 0;
3444 }
3445# endif
3446 if (i != 0)
3447 {
3448 /* Also backup the cursor position. */
3449 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3450 ccline.cmdspos -= i;
3451 msg_col -= i;
3452 if (msg_col < 0)
3453 {
3454 msg_col += Columns;
3455 --msg_row;
3456 }
3457 }
3458 }
3459#endif
3460
3461 if (redraw && !cmd_silent)
3462 {
3463 msg_no_more = TRUE;
3464 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003465 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3467 /* Avoid clearing the rest of the line too often. */
3468 if (cmdline_row != i || ccline.overstrike)
3469 msg_clr_eos();
3470 msg_no_more = FALSE;
3471 }
3472#ifdef FEAT_FKMAP
3473 /*
3474 * If we are in Farsi command mode, the character input must be in
3475 * Insert mode. So do not advance the cmdpos.
3476 */
3477 if (!cmd_fkmap)
3478#endif
3479 {
3480 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003481 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003483 if (m < 0) /* overflow, Columns or Rows at weird value */
3484 m = MAXCOL;
3485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 else
3487 m = MAXCOL;
3488 for (i = 0; i < len; ++i)
3489 {
3490 c = cmdline_charsize(ccline.cmdpos);
3491#ifdef FEAT_MBYTE
3492 /* count ">" for a double-wide char that doesn't fit. */
3493 if (has_mbyte)
3494 correct_cmdspos(ccline.cmdpos, c);
3495#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003496 /* Stop cursor at the end of the screen, but do increment the
3497 * insert position, so that entering a very long command
3498 * works, even though you can't see it. */
3499 if (ccline.cmdspos + c < m)
3500 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501#ifdef FEAT_MBYTE
3502 if (has_mbyte)
3503 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003504 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (c > len - i - 1)
3506 c = len - i - 1;
3507 ccline.cmdpos += c;
3508 i += c;
3509 }
3510#endif
3511 ++ccline.cmdpos;
3512 }
3513 }
3514 }
3515 if (redraw)
3516 msg_check();
3517 return retval;
3518}
3519
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003520static struct cmdline_info prev_ccline;
3521static int prev_ccline_used = FALSE;
3522
3523/*
3524 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3525 * and overwrite it. But get_cmdline_str() may need it, thus make it
3526 * available globally in prev_ccline.
3527 */
3528 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003529save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003530{
3531 if (!prev_ccline_used)
3532 {
3533 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3534 prev_ccline_used = TRUE;
3535 }
3536 *ccp = prev_ccline;
3537 prev_ccline = ccline;
3538 ccline.cmdbuff = NULL;
3539 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003540 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003541}
3542
3543/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003544 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003545 */
3546 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003547restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003548{
3549 ccline = prev_ccline;
3550 prev_ccline = *ccp;
3551}
3552
Bram Moolenaar5a305422006-04-28 22:38:25 +00003553#if defined(FEAT_EVAL) || defined(PROTO)
3554/*
3555 * Save the command line into allocated memory. Returns a pointer to be
3556 * passed to restore_cmdline_alloc() later.
3557 * Returns NULL when failed.
3558 */
3559 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003560save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003561{
3562 struct cmdline_info *p;
3563
3564 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3565 if (p != NULL)
3566 save_cmdline(p);
3567 return (char_u *)p;
3568}
3569
3570/*
3571 * Restore the command line from the return value of save_cmdline_alloc().
3572 */
3573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003574restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003575{
3576 if (p != NULL)
3577 {
3578 restore_cmdline((struct cmdline_info *)p);
3579 vim_free(p);
3580 }
3581}
3582#endif
3583
Bram Moolenaar8299df92004-07-10 09:47:34 +00003584/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003585 * Paste a yank register into the command line.
3586 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003587 * insert_reg() can't be used here, because special characters from the
3588 * register contents will be interpreted as commands.
3589 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003590 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003591 */
3592 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003593cmdline_paste(
3594 int regname,
3595 int literally, /* Insert text literally instead of "as typed" */
3596 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003597{
3598 long i;
3599 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003600 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003601 int allocated;
3602 struct cmdline_info save_ccline;
3603
3604 /* check for valid regname; also accept special characters for CTRL-R in
3605 * the command line */
3606 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003607 && regname != Ctrl_A && regname != Ctrl_L
3608 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003609 return FAIL;
3610
3611 /* A register containing CTRL-R can cause an endless loop. Allow using
3612 * CTRL-C to break the loop. */
3613 line_breakcheck();
3614 if (got_int)
3615 return FAIL;
3616
3617#ifdef FEAT_CLIPBOARD
3618 regname = may_get_selection(regname);
3619#endif
3620
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003621 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003622 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003623 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003624 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003625 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003626 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003627 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003628
3629 if (i)
3630 {
3631 /* Got the value of a special register in "arg". */
3632 if (arg == NULL)
3633 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003634
3635 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3636 * part of the word. */
3637 p = arg;
3638 if (p_is && regname == Ctrl_W)
3639 {
3640 char_u *w;
3641 int len;
3642
3643 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003644 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003645 {
3646#ifdef FEAT_MBYTE
3647 if (has_mbyte)
3648 {
3649 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3650 if (!vim_iswordc(mb_ptr2char(w - len)))
3651 break;
3652 w -= len;
3653 }
3654 else
3655#endif
3656 {
3657 if (!vim_iswordc(w[-1]))
3658 break;
3659 --w;
3660 }
3661 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003662 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003663 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3664 p += len;
3665 }
3666
3667 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003668 if (allocated)
3669 vim_free(arg);
3670 return OK;
3671 }
3672
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003673 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003674}
3675
3676/*
3677 * Put a string on the command line.
3678 * When "literally" is TRUE, insert literally.
3679 * When "literally" is FALSE, insert as typed, but don't leave the command
3680 * line.
3681 */
3682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003683cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003684{
3685 int c, cv;
3686
3687 if (literally)
3688 put_on_cmdline(s, -1, TRUE);
3689 else
3690 while (*s != NUL)
3691 {
3692 cv = *s;
3693 if (cv == Ctrl_V && s[1])
3694 ++s;
3695#ifdef FEAT_MBYTE
3696 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003697 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003698 else
3699#endif
3700 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003701 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3702 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003703#ifdef UNIX
3704 || c == intr_char
3705#endif
3706 || (c == Ctrl_BSL && *s == Ctrl_N))
3707 stuffcharReadbuff(Ctrl_V);
3708 stuffcharReadbuff(c);
3709 }
3710}
3711
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712#ifdef FEAT_WILDMENU
3713/*
3714 * Delete characters on the command line, from "from" to the current
3715 * position.
3716 */
3717 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003718cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719{
3720 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3721 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3722 ccline.cmdlen -= ccline.cmdpos - from;
3723 ccline.cmdpos = from;
3724}
3725#endif
3726
3727/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003728 * This function is called when the screen size changes and with incremental
3729 * search and in other situations where the command line may have been
3730 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 */
3732 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003733redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003735 redrawcmdline_ex(TRUE);
3736}
3737
3738 void
3739redrawcmdline_ex(int do_compute_cmdrow)
3740{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (cmd_silent)
3742 return;
3743 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003744 if (do_compute_cmdrow)
3745 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 redrawcmd();
3747 cursorcmd();
3748}
3749
3750 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003751redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753 int i;
3754
3755 if (cmd_silent)
3756 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003757 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 msg_putchar(ccline.cmdfirstc);
3759 if (ccline.cmdprompt != NULL)
3760 {
3761 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3762 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3763 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003764 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 --ccline.cmdindent;
3766 }
3767 else
3768 for (i = ccline.cmdindent; i > 0; --i)
3769 msg_putchar(' ');
3770}
3771
3772/*
3773 * Redraw what is currently on the command line.
3774 */
3775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003776redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777{
3778 if (cmd_silent)
3779 return;
3780
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003781 /* when 'incsearch' is set there may be no command line while redrawing */
3782 if (ccline.cmdbuff == NULL)
3783 {
3784 windgoto(cmdline_row, 0);
3785 msg_clr_eos();
3786 return;
3787 }
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 msg_start();
3790 redrawcmdprompt();
3791
3792 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3793 msg_no_more = TRUE;
3794 draw_cmdline(0, ccline.cmdlen);
3795 msg_clr_eos();
3796 msg_no_more = FALSE;
3797
3798 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003799 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003800 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
3802 /*
3803 * An emsg() before may have set msg_scroll. This is used in normal mode,
3804 * in cmdline mode we can reset them now.
3805 */
3806 msg_scroll = FALSE; /* next message overwrites cmdline */
3807
3808 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3809 * in cmdline mode */
3810 skip_redraw = FALSE;
3811}
3812
3813 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003814compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003816 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 cmdline_row = Rows - 1;
3818 else
3819 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003820 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821}
3822
3823 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003824cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 if (cmd_silent)
3827 return;
3828
3829#ifdef FEAT_RIGHTLEFT
3830 if (cmdmsg_rl)
3831 {
3832 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3833 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3834 if (msg_row <= 0)
3835 msg_row = Rows - 1;
3836 }
3837 else
3838#endif
3839 {
3840 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3841 msg_col = ccline.cmdspos % (int)Columns;
3842 if (msg_row >= Rows)
3843 msg_row = Rows - 1;
3844 }
3845
3846 windgoto(msg_row, msg_col);
3847#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003848 if (p_imst == IM_ON_THE_SPOT)
3849 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850#endif
3851#ifdef MCH_CURSOR_SHAPE
3852 mch_update_cursor();
3853#endif
3854}
3855
3856 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003857gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
3859 msg_start();
3860#ifdef FEAT_RIGHTLEFT
3861 if (cmdmsg_rl)
3862 msg_col = Columns - 1;
3863 else
3864#endif
3865 msg_col = 0; /* always start in column 0 */
3866 if (clr) /* clear the bottom line(s) */
3867 msg_clr_eos(); /* will reset clear_cmdline */
3868 windgoto(cmdline_row, 0);
3869}
3870
3871/*
3872 * Check the word in front of the cursor for an abbreviation.
3873 * Called when the non-id character "c" has been entered.
3874 * When an abbreviation is recognized it is removed from the text with
3875 * backspaces and the replacement string is inserted, followed by "c".
3876 */
3877 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003878ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003880 int spos = 0;
3881
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3883 return FALSE;
3884
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003885 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3886 * Actually accepts any mark. */
3887 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3888 spos++;
3889 if (ccline.cmdlen - spos > 5
3890 && ccline.cmdbuff[spos] == '\''
3891 && ccline.cmdbuff[spos + 2] == ','
3892 && ccline.cmdbuff[spos + 3] == '\'')
3893 spos += 5;
3894 else
3895 /* check abbreviation from the beginning of the commandline */
3896 spos = 0;
3897
3898 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899}
3900
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003901#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3902 static int
3903#ifdef __BORLANDC__
3904_RTLENTRYF
3905#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003906sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003907{
3908 char_u *p1 = *(char_u **)s1;
3909 char_u *p2 = *(char_u **)s2;
3910
3911 if (*p1 != '<' && *p2 == '<') return -1;
3912 if (*p1 == '<' && *p2 != '<') return 1;
3913 return STRCMP(p1, p2);
3914}
3915#endif
3916
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917/*
3918 * Return FAIL if this is not an appropriate context in which to do
3919 * completion of anything, return OK if it is (even if there are no matches).
3920 * For the caller, this means that the character is just passed through like a
3921 * normal character (instead of being expanded). This allows :s/^I^D etc.
3922 */
3923 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003924nextwild(
3925 expand_T *xp,
3926 int type,
3927 int options, /* extra options for ExpandOne() */
3928 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929{
3930 int i, j;
3931 char_u *p1;
3932 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 int difflen;
3934 int v;
3935
3936 if (xp->xp_numfiles == -1)
3937 {
3938 set_expand_context(xp);
3939 cmd_showtail = expand_showtail(xp);
3940 }
3941
3942 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3943 {
3944 beep_flush();
3945 return OK; /* Something illegal on command line */
3946 }
3947 if (xp->xp_context == EXPAND_NOTHING)
3948 {
3949 /* Caller can use the character as a normal char instead */
3950 return FAIL;
3951 }
3952
3953 MSG_PUTS("..."); /* show that we are busy */
3954 out_flush();
3955
3956 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003957 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 if (type == WILD_NEXT || type == WILD_PREV)
3960 {
3961 /*
3962 * Get next/previous match for a previous expanded pattern.
3963 */
3964 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3965 }
3966 else
3967 {
3968 /*
3969 * Translate string into pattern and expand it.
3970 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003971 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3972 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 p2 = NULL;
3974 else
3975 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003976 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003977 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3978 if (escape)
3979 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003980
3981 if (p_wic)
3982 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003983 p2 = ExpandOne(xp, p1,
3984 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003985 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003987 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 if (p2 != NULL && type == WILD_LONGEST)
3989 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003990 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 if (ccline.cmdbuff[i + j] == '*'
3992 || ccline.cmdbuff[i + j] == '?')
3993 break;
3994 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003995 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 }
3997 }
3998 }
3999
4000 if (p2 != NULL && !got_int)
4001 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004002 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004003 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004005 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 xp->xp_pattern = ccline.cmdbuff + i;
4007 }
4008 else
4009 v = OK;
4010 if (v == OK)
4011 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004012 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4013 &ccline.cmdbuff[ccline.cmdpos],
4014 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4015 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 ccline.cmdlen += difflen;
4017 ccline.cmdpos += difflen;
4018 }
4019 }
4020 vim_free(p2);
4021
4022 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004023 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024
4025 /* When expanding a ":map" command and no matches are found, assume that
4026 * the key is supposed to be inserted literally */
4027 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4028 return FAIL;
4029
4030 if (xp->xp_numfiles <= 0 && p2 == NULL)
4031 beep_flush();
4032 else if (xp->xp_numfiles == 1)
4033 /* free expanded pattern */
4034 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4035
4036 return OK;
4037}
4038
4039/*
4040 * Do wildcard expansion on the string 'str'.
4041 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004042 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 * Return NULL for failure.
4044 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004045 * "orig" is the originally expanded string, copied to allocated memory. It
4046 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4047 * WILD_PREV "orig" should be NULL.
4048 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004049 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4050 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 *
4052 * mode = WILD_FREE: just free previously expanded matches
4053 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4054 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4055 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4056 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4057 * mode = WILD_ALL: return all matches concatenated
4058 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004059 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 *
4061 * options = WILD_LIST_NOTFOUND: list entries without a match
4062 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4063 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4064 * options = WILD_NO_BEEP: Don't beep for multiple matches
4065 * options = WILD_ADD_SLASH: add a slash after directory names
4066 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4067 * options = WILD_SILENT: don't print warning messages
4068 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004069 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 *
4071 * The variables xp->xp_context and xp->xp_backslash must have been set!
4072 */
4073 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004074ExpandOne(
4075 expand_T *xp,
4076 char_u *str,
4077 char_u *orig, /* allocated copy of original of expanded string */
4078 int options,
4079 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080{
4081 char_u *ss = NULL;
4082 static int findex;
4083 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004084 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 int i;
4086 long_u len;
4087 int non_suf_match; /* number without matching suffix */
4088
4089 /*
4090 * first handle the case of using an old match
4091 */
4092 if (mode == WILD_NEXT || mode == WILD_PREV)
4093 {
4094 if (xp->xp_numfiles > 0)
4095 {
4096 if (mode == WILD_PREV)
4097 {
4098 if (findex == -1)
4099 findex = xp->xp_numfiles;
4100 --findex;
4101 }
4102 else /* mode == WILD_NEXT */
4103 ++findex;
4104
4105 /*
4106 * When wrapping around, return the original string, set findex to
4107 * -1.
4108 */
4109 if (findex < 0)
4110 {
4111 if (orig_save == NULL)
4112 findex = xp->xp_numfiles - 1;
4113 else
4114 findex = -1;
4115 }
4116 if (findex >= xp->xp_numfiles)
4117 {
4118 if (orig_save == NULL)
4119 findex = 0;
4120 else
4121 findex = -1;
4122 }
4123#ifdef FEAT_WILDMENU
4124 if (p_wmnu)
4125 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4126 findex, cmd_showtail);
4127#endif
4128 if (findex == -1)
4129 return vim_strsave(orig_save);
4130 return vim_strsave(xp->xp_files[findex]);
4131 }
4132 else
4133 return NULL;
4134 }
4135
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004136 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4138 {
4139 FreeWild(xp->xp_numfiles, xp->xp_files);
4140 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004141 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 }
4143 findex = 0;
4144
4145 if (mode == WILD_FREE) /* only release file name */
4146 return NULL;
4147
4148 if (xp->xp_numfiles == -1)
4149 {
4150 vim_free(orig_save);
4151 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004152 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153
4154 /*
4155 * Do the expansion.
4156 */
4157 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4158 options) == FAIL)
4159 {
4160#ifdef FNAME_ILLEGAL
4161 /* Illegal file name has been silently skipped. But when there
4162 * are wildcards, the real problem is that there was no match,
4163 * causing the pattern to be added, which has illegal characters.
4164 */
4165 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4166 EMSG2(_(e_nomatch2), str);
4167#endif
4168 }
4169 else if (xp->xp_numfiles == 0)
4170 {
4171 if (!(options & WILD_SILENT))
4172 EMSG2(_(e_nomatch2), str);
4173 }
4174 else
4175 {
4176 /* Escape the matches for use on the command line. */
4177 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4178
4179 /*
4180 * Check for matching suffixes in file names.
4181 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004182 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4183 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 if (xp->xp_numfiles)
4186 non_suf_match = xp->xp_numfiles;
4187 else
4188 non_suf_match = 1;
4189 if ((xp->xp_context == EXPAND_FILES
4190 || xp->xp_context == EXPAND_DIRECTORIES)
4191 && xp->xp_numfiles > 1)
4192 {
4193 /*
4194 * More than one match; check suffix.
4195 * The files will have been sorted on matching suffix in
4196 * expand_wildcards, only need to check the first two.
4197 */
4198 non_suf_match = 0;
4199 for (i = 0; i < 2; ++i)
4200 if (match_suffix(xp->xp_files[i]))
4201 ++non_suf_match;
4202 }
4203 if (non_suf_match != 1)
4204 {
4205 /* Can we ever get here unless it's while expanding
4206 * interactively? If not, we can get rid of this all
4207 * together. Don't really want to wait for this message
4208 * (and possibly have to hit return to continue!).
4209 */
4210 if (!(options & WILD_SILENT))
4211 EMSG(_(e_toomany));
4212 else if (!(options & WILD_NO_BEEP))
4213 beep_flush();
4214 }
4215 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4216 ss = vim_strsave(xp->xp_files[0]);
4217 }
4218 }
4219 }
4220
4221 /* Find longest common part */
4222 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4223 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004224 int mb_len = 1;
4225 int c0, ci;
4226
4227 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004229#ifdef FEAT_MBYTE
4230 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004232 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4233 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4234 }
4235 else
4236#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004237 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004238 for (i = 1; i < xp->xp_numfiles; ++i)
4239 {
4240#ifdef FEAT_MBYTE
4241 if (has_mbyte)
4242 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4243 else
4244#endif
4245 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004246 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004248 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004249 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004251 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 break;
4253 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004254 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 break;
4256 }
4257 if (i < xp->xp_numfiles)
4258 {
4259 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004260 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 break;
4262 }
4263 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004264
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 ss = alloc((unsigned)len + 1);
4266 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004267 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 findex = -1; /* next p_wc gets first one */
4269 }
4270
4271 /* Concatenate all matching names */
4272 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4273 {
4274 len = 0;
4275 for (i = 0; i < xp->xp_numfiles; ++i)
4276 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4277 ss = lalloc(len, TRUE);
4278 if (ss != NULL)
4279 {
4280 *ss = NUL;
4281 for (i = 0; i < xp->xp_numfiles; ++i)
4282 {
4283 STRCAT(ss, xp->xp_files[i]);
4284 if (i != xp->xp_numfiles - 1)
4285 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4286 }
4287 }
4288 }
4289
4290 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4291 ExpandCleanup(xp);
4292
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004293 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004294 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004295 vim_free(orig);
4296
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 return ss;
4298}
4299
4300/*
4301 * Prepare an expand structure for use.
4302 */
4303 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004304ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004306 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004307 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004309#ifndef BACKSLASH_IN_FILENAME
4310 xp->xp_shell = FALSE;
4311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 xp->xp_numfiles = -1;
4313 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004314#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4315 xp->xp_arg = NULL;
4316#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004317 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318}
4319
4320/*
4321 * Cleanup an expand structure after use.
4322 */
4323 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004324ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325{
4326 if (xp->xp_numfiles >= 0)
4327 {
4328 FreeWild(xp->xp_numfiles, xp->xp_files);
4329 xp->xp_numfiles = -1;
4330 }
4331}
4332
4333 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004334ExpandEscape(
4335 expand_T *xp,
4336 char_u *str,
4337 int numfiles,
4338 char_u **files,
4339 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340{
4341 int i;
4342 char_u *p;
4343
4344 /*
4345 * May change home directory back to "~"
4346 */
4347 if (options & WILD_HOME_REPLACE)
4348 tilde_replace(str, numfiles, files);
4349
4350 if (options & WILD_ESCAPE)
4351 {
4352 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004353 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004354 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 || xp->xp_context == EXPAND_BUFFERS
4356 || xp->xp_context == EXPAND_DIRECTORIES)
4357 {
4358 /*
4359 * Insert a backslash into a file name before a space, \, %, #
4360 * and wildmatch characters, except '~'.
4361 */
4362 for (i = 0; i < numfiles; ++i)
4363 {
4364 /* for ":set path=" we need to escape spaces twice */
4365 if (xp->xp_backslash == XP_BS_THREE)
4366 {
4367 p = vim_strsave_escaped(files[i], (char_u *)" ");
4368 if (p != NULL)
4369 {
4370 vim_free(files[i]);
4371 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004372#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 p = vim_strsave_escaped(files[i], (char_u *)" ");
4374 if (p != NULL)
4375 {
4376 vim_free(files[i]);
4377 files[i] = p;
4378 }
4379#endif
4380 }
4381 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004382#ifdef BACKSLASH_IN_FILENAME
4383 p = vim_strsave_fnameescape(files[i], FALSE);
4384#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004385 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 if (p != NULL)
4388 {
4389 vim_free(files[i]);
4390 files[i] = p;
4391 }
4392
4393 /* If 'str' starts with "\~", replace "~" at start of
4394 * files[i] with "\~". */
4395 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004396 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004399
4400 /* If the first file starts with a '+' escape it. Otherwise it
4401 * could be seen as "+cmd". */
4402 if (*files[0] == '+')
4403 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 }
4405 else if (xp->xp_context == EXPAND_TAGS)
4406 {
4407 /*
4408 * Insert a backslash before characters in a tag name that
4409 * would terminate the ":tag" command.
4410 */
4411 for (i = 0; i < numfiles; ++i)
4412 {
4413 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4414 if (p != NULL)
4415 {
4416 vim_free(files[i]);
4417 files[i] = p;
4418 }
4419 }
4420 }
4421 }
4422}
4423
4424/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004425 * Escape special characters in "fname" for when used as a file name argument
4426 * after a Vim command, or, when "shell" is non-zero, a shell command.
4427 * Returns the result in allocated memory.
4428 */
4429 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004430vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004431{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004432 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004433#ifdef BACKSLASH_IN_FILENAME
4434 char_u buf[20];
4435 int j = 0;
4436
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004437 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004438 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004439 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004440 buf[j++] = *p;
4441 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004442 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004443#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004444 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4445 if (shell && csh_like_shell() && p != NULL)
4446 {
4447 char_u *s;
4448
4449 /* For csh and similar shells need to put two backslashes before '!'.
4450 * One is taken by Vim, one by the shell. */
4451 s = vim_strsave_escaped(p, (char_u *)"!");
4452 vim_free(p);
4453 p = s;
4454 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004455#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004456
4457 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4458 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004459 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004460 escape_fname(&p);
4461
4462 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004463}
4464
4465/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004466 * Put a backslash before the file name in "pp", which is in allocated memory.
4467 */
4468 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004469escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004470{
4471 char_u *p;
4472
4473 p = alloc((unsigned)(STRLEN(*pp) + 2));
4474 if (p != NULL)
4475 {
4476 p[0] = '\\';
4477 STRCPY(p + 1, *pp);
4478 vim_free(*pp);
4479 *pp = p;
4480 }
4481}
4482
4483/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 * For each file name in files[num_files]:
4485 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4486 */
4487 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004488tilde_replace(
4489 char_u *orig_pat,
4490 int num_files,
4491 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492{
4493 int i;
4494 char_u *p;
4495
4496 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4497 {
4498 for (i = 0; i < num_files; ++i)
4499 {
4500 p = home_replace_save(NULL, files[i]);
4501 if (p != NULL)
4502 {
4503 vim_free(files[i]);
4504 files[i] = p;
4505 }
4506 }
4507 }
4508}
4509
4510/*
4511 * Show all matches for completion on the command line.
4512 * Returns EXPAND_NOTHING when the character that triggered expansion should
4513 * be inserted like a normal character.
4514 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004516showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
4518#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4519 int num_files;
4520 char_u **files_found;
4521 int i, j, k;
4522 int maxlen;
4523 int lines;
4524 int columns;
4525 char_u *p;
4526 int lastlen;
4527 int attr;
4528 int showtail;
4529
4530 if (xp->xp_numfiles == -1)
4531 {
4532 set_expand_context(xp);
4533 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4534 &num_files, &files_found);
4535 showtail = expand_showtail(xp);
4536 if (i != EXPAND_OK)
4537 return i;
4538
4539 }
4540 else
4541 {
4542 num_files = xp->xp_numfiles;
4543 files_found = xp->xp_files;
4544 showtail = cmd_showtail;
4545 }
4546
4547#ifdef FEAT_WILDMENU
4548 if (!wildmenu)
4549 {
4550#endif
4551 msg_didany = FALSE; /* lines_left will be set */
4552 msg_start(); /* prepare for paging */
4553 msg_putchar('\n');
4554 out_flush();
4555 cmdline_row = msg_row;
4556 msg_didany = FALSE; /* lines_left will be set again */
4557 msg_start(); /* prepare for paging */
4558#ifdef FEAT_WILDMENU
4559 }
4560#endif
4561
4562 if (got_int)
4563 got_int = FALSE; /* only int. the completion, not the cmd line */
4564#ifdef FEAT_WILDMENU
4565 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004566 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567#endif
4568 else
4569 {
4570 /* find the length of the longest file name */
4571 maxlen = 0;
4572 for (i = 0; i < num_files; ++i)
4573 {
4574 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004575 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 || xp->xp_context == EXPAND_BUFFERS))
4577 {
4578 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4579 j = vim_strsize(NameBuff);
4580 }
4581 else
4582 j = vim_strsize(L_SHOWFILE(i));
4583 if (j > maxlen)
4584 maxlen = j;
4585 }
4586
4587 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4588 lines = num_files;
4589 else
4590 {
4591 /* compute the number of columns and lines for the listing */
4592 maxlen += 2; /* two spaces between file names */
4593 columns = ((int)Columns + 2) / maxlen;
4594 if (columns < 1)
4595 columns = 1;
4596 lines = (num_files + columns - 1) / columns;
4597 }
4598
Bram Moolenaar8820b482017-03-16 17:23:31 +01004599 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
4601 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4602 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004603 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 msg_clr_eos();
4605 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004606 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
4609 /* list the files line by line */
4610 for (i = 0; i < lines; ++i)
4611 {
4612 lastlen = 999;
4613 for (k = i; k < num_files; k += lines)
4614 {
4615 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4616 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004617 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 p = files_found[k] + STRLEN(files_found[k]) + 1;
4619 msg_advance(maxlen + 1);
4620 msg_puts(p);
4621 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004622 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624 }
4625 for (j = maxlen - lastlen; --j >= 0; )
4626 msg_putchar(' ');
4627 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004628 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 || xp->xp_context == EXPAND_BUFFERS)
4630 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004631 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004632 if (xp->xp_numfiles != -1)
4633 {
4634 char_u *halved_slash;
4635 char_u *exp_path;
4636
4637 /* Expansion was done before and special characters
4638 * were escaped, need to halve backslashes. Also
4639 * $HOME has been replaced with ~/. */
4640 exp_path = expand_env_save_opt(files_found[k], TRUE);
4641 halved_slash = backslash_halve_save(
4642 exp_path != NULL ? exp_path : files_found[k]);
4643 j = mch_isdir(halved_slash != NULL ? halved_slash
4644 : files_found[k]);
4645 vim_free(exp_path);
4646 vim_free(halved_slash);
4647 }
4648 else
4649 /* Expansion was done here, file names are literal. */
4650 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 if (showtail)
4652 p = L_SHOWFILE(k);
4653 else
4654 {
4655 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4656 TRUE);
4657 p = NameBuff;
4658 }
4659 }
4660 else
4661 {
4662 j = FALSE;
4663 p = L_SHOWFILE(k);
4664 }
4665 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4666 }
4667 if (msg_col > 0) /* when not wrapped around */
4668 {
4669 msg_clr_eos();
4670 msg_putchar('\n');
4671 }
4672 out_flush(); /* show one line at a time */
4673 if (got_int)
4674 {
4675 got_int = FALSE;
4676 break;
4677 }
4678 }
4679
4680 /*
4681 * we redraw the command below the lines that we have just listed
4682 * This is a bit tricky, but it saves a lot of screen updating.
4683 */
4684 cmdline_row = msg_row; /* will put it back later */
4685 }
4686
4687 if (xp->xp_numfiles == -1)
4688 FreeWild(num_files, files_found);
4689
4690 return EXPAND_OK;
4691}
4692
4693/*
4694 * Private gettail for showmatches() (and win_redr_status_matches()):
4695 * Find tail of file name path, but ignore trailing "/".
4696 */
4697 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004698sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
4700 char_u *p;
4701 char_u *t = s;
4702 int had_sep = FALSE;
4703
4704 for (p = s; *p != NUL; )
4705 {
4706 if (vim_ispathsep(*p)
4707#ifdef BACKSLASH_IN_FILENAME
4708 && !rem_backslash(p)
4709#endif
4710 )
4711 had_sep = TRUE;
4712 else if (had_sep)
4713 {
4714 t = p;
4715 had_sep = FALSE;
4716 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004717 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 }
4719 return t;
4720}
4721
4722/*
4723 * Return TRUE if we only need to show the tail of completion matches.
4724 * When not completing file names or there is a wildcard in the path FALSE is
4725 * returned.
4726 */
4727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004728expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729{
4730 char_u *s;
4731 char_u *end;
4732
4733 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004734 if (xp->xp_context != EXPAND_FILES
4735 && xp->xp_context != EXPAND_SHELLCMD
4736 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 return FALSE;
4738
4739 end = gettail(xp->xp_pattern);
4740 if (end == xp->xp_pattern) /* there is no path separator */
4741 return FALSE;
4742
4743 for (s = xp->xp_pattern; s < end; s++)
4744 {
4745 /* Skip escaped wildcards. Only when the backslash is not a path
4746 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4747 if (rem_backslash(s))
4748 ++s;
4749 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4750 return FALSE;
4751 }
4752 return TRUE;
4753}
4754
4755/*
4756 * Prepare a string for expansion.
4757 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004758 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 * When expanding other names: The string will be used with regcomp(). Copy
4760 * the name into allocated memory and prepend "^".
4761 */
4762 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004763addstar(
4764 char_u *fname,
4765 int len,
4766 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767{
4768 char_u *retval;
4769 int i, j;
4770 int new_len;
4771 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004772 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004774 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004775 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004776 && context != EXPAND_SHELLCMD
4777 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
4779 /*
4780 * Matching will be done internally (on something other than files).
4781 * So we convert the file-matching-type wildcards into our kind for
4782 * use with vim_regcomp(). First work out how long it will be:
4783 */
4784
4785 /* For help tags the translation is done in find_help_tags().
4786 * For a tag pattern starting with "/" no translation is needed. */
4787 if (context == EXPAND_HELP
4788 || context == EXPAND_COLORS
4789 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004790 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004791 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004792 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004793 || ((context == EXPAND_TAGS_LISTFILES
4794 || context == EXPAND_TAGS)
4795 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 retval = vim_strnsave(fname, len);
4797 else
4798 {
4799 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4800 for (i = 0; i < len; i++)
4801 {
4802 if (fname[i] == '*' || fname[i] == '~')
4803 new_len++; /* '*' needs to be replaced by ".*"
4804 '~' needs to be replaced by "\~" */
4805
4806 /* Buffer names are like file names. "." should be literal */
4807 if (context == EXPAND_BUFFERS && fname[i] == '.')
4808 new_len++; /* "." becomes "\." */
4809
4810 /* Custom expansion takes care of special things, match
4811 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004812 if ((context == EXPAND_USER_DEFINED
4813 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 new_len++; /* '\' becomes "\\" */
4815 }
4816 retval = alloc(new_len);
4817 if (retval != NULL)
4818 {
4819 retval[0] = '^';
4820 j = 1;
4821 for (i = 0; i < len; i++, j++)
4822 {
4823 /* Skip backslash. But why? At least keep it for custom
4824 * expansion. */
4825 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004826 && context != EXPAND_USER_LIST
4827 && fname[i] == '\\'
4828 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 break;
4830
4831 switch (fname[i])
4832 {
4833 case '*': retval[j++] = '.';
4834 break;
4835 case '~': retval[j++] = '\\';
4836 break;
4837 case '?': retval[j] = '.';
4838 continue;
4839 case '.': if (context == EXPAND_BUFFERS)
4840 retval[j++] = '\\';
4841 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004842 case '\\': if (context == EXPAND_USER_DEFINED
4843 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 retval[j++] = '\\';
4845 break;
4846 }
4847 retval[j] = fname[i];
4848 }
4849 retval[j] = NUL;
4850 }
4851 }
4852 }
4853 else
4854 {
4855 retval = alloc(len + 4);
4856 if (retval != NULL)
4857 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004858 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859
4860 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004861 * Don't add a star to *, ~, ~user, $var or `cmd`.
4862 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 * ~ would be at the start of the file name, but not the tail.
4864 * $ could be anywhere in the tail.
4865 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004866 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 */
4868 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004869 ends_in_star = (len > 0 && retval[len - 1] == '*');
4870#ifndef BACKSLASH_IN_FILENAME
4871 for (i = len - 2; i >= 0; --i)
4872 {
4873 if (retval[i] != '\\')
4874 break;
4875 ends_in_star = !ends_in_star;
4876 }
4877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004879 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 && vim_strchr(tail, '$') == NULL
4881 && vim_strchr(retval, '`') == NULL)
4882 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004883 else if (len > 0 && retval[len - 1] == '$')
4884 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 retval[len] = NUL;
4886 }
4887 }
4888 return retval;
4889}
4890
4891/*
4892 * Must parse the command line so far to work out what context we are in.
4893 * Completion can then be done based on that context.
4894 * This routine sets the variables:
4895 * xp->xp_pattern The start of the pattern to be expanded within
4896 * the command line (ends at the cursor).
4897 * xp->xp_context The type of thing to expand. Will be one of:
4898 *
4899 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4900 * the command line, like an unknown command. Caller
4901 * should beep.
4902 * EXPAND_NOTHING Unrecognised context for completion, use char like
4903 * a normal char, rather than for completion. eg
4904 * :s/^I/
4905 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4906 * it.
4907 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4908 * EXPAND_FILES After command with XFILE set, or after setting
4909 * with P_EXPAND set. eg :e ^I, :w>>^I
4910 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4911 * when we know only directories are of interest. eg
4912 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004913 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4915 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4916 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4917 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4918 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4919 * EXPAND_EVENTS Complete event names
4920 * EXPAND_SYNTAX Complete :syntax command arguments
4921 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4922 * EXPAND_AUGROUP Complete autocommand group names
4923 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4924 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4925 * eg :unmap a^I , :cunab x^I
4926 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4927 * eg :call sub^I
4928 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4929 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4930 * names in expressions, eg :while s^I
4931 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004932 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 */
4934 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004935set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004937 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (ccline.cmdfirstc != ':'
4939#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004940 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004941 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942#endif
4943 )
4944 {
4945 xp->xp_context = EXPAND_NOTHING;
4946 return;
4947 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004948 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949}
4950
4951 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004952set_cmd_context(
4953 expand_T *xp,
4954 char_u *str, /* start of command line */
4955 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004956 int col, /* position of cursor */
4957 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958{
4959 int old_char = NUL;
4960 char_u *nextcomm;
4961
4962 /*
4963 * Avoid a UMR warning from Purify, only save the character if it has been
4964 * written before.
4965 */
4966 if (col < len)
4967 old_char = str[col];
4968 str[col] = NUL;
4969 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004970
4971#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004972 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004973 {
4974# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004975 /* pass CMD_SIZE because there is no real command */
4976 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004977# endif
4978 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004979 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004980 {
4981 xp->xp_context = ccline.xp_context;
4982 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004983# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004984 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004985# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004986 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004987 else
4988#endif
4989 while (nextcomm != NULL)
4990 nextcomm = set_one_cmd_context(xp, nextcomm);
4991
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004992 /* Store the string here so that call_user_expand_func() can get to them
4993 * easily. */
4994 xp->xp_line = str;
4995 xp->xp_col = col;
4996
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 str[col] = old_char;
4998}
4999
5000/*
5001 * Expand the command line "str" from context "xp".
5002 * "xp" must have been set by set_cmd_context().
5003 * xp->xp_pattern points into "str", to where the text that is to be expanded
5004 * starts.
5005 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
5006 * cursor.
5007 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5008 * key that triggered expansion literally.
5009 * Returns EXPAND_OK otherwise.
5010 */
5011 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005012expand_cmdline(
5013 expand_T *xp,
5014 char_u *str, /* start of command line */
5015 int col, /* position of cursor */
5016 int *matchcount, /* return: nr of matches */
5017 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005020 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5023 {
5024 beep_flush();
5025 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5026 }
5027 if (xp->xp_context == EXPAND_NOTHING)
5028 {
5029 /* Caller can use the character as a normal char instead */
5030 return EXPAND_NOTHING;
5031 }
5032
5033 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005034 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5035 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 if (file_str == NULL)
5037 return EXPAND_UNSUCCESSFUL;
5038
Bram Moolenaar94950a92010-12-02 16:01:29 +01005039 if (p_wic)
5040 options += WILD_ICASE;
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005043 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 *matchcount = 0;
5046 *matches = NULL;
5047 }
5048 vim_free(file_str);
5049
5050 return EXPAND_OK;
5051}
5052
5053#ifdef FEAT_MULTI_LANG
5054/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005055 * Cleanup matches for help tags:
5056 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5057 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005059static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060
5061 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063{
5064 int i, j;
5065 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005066 char_u buf[4];
5067 char_u *p = buf;
5068
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005069 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005070 {
5071 *p++ = '@';
5072 *p++ = p_hlg[0];
5073 *p++ = p_hlg[1];
5074 }
5075 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 for (i = 0; i < num_file; ++i)
5078 {
5079 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005080 if (len <= 0)
5081 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005082 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
5084 /* Sorting on priority means the same item in another language may
5085 * be anywhere. Search all items for a match up to the "@en". */
5086 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005087 if (j != i && (int)STRLEN(file[j]) == len + 3
5088 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005091 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 file[i][len] = NUL;
5093 }
5094 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005095
5096 if (*buf != NUL)
5097 for (i = 0; i < num_file; ++i)
5098 {
5099 len = (int)STRLEN(file[i]) - 3;
5100 if (len <= 0)
5101 continue;
5102 if (STRCMP(file[i] + len, buf) == 0)
5103 {
5104 /* remove the default language */
5105 file[i][len] = NUL;
5106 }
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108}
5109#endif
5110
5111/*
5112 * Do the expansion based on xp->xp_context and "pat".
5113 */
5114 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005115ExpandFromContext(
5116 expand_T *xp,
5117 char_u *pat,
5118 int *num_file,
5119 char_u ***file,
5120 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121{
5122#ifdef FEAT_CMDL_COMPL
5123 regmatch_T regmatch;
5124#endif
5125 int ret;
5126 int flags;
5127
5128 flags = EW_DIR; /* include directories */
5129 if (options & WILD_LIST_NOTFOUND)
5130 flags |= EW_NOTFOUND;
5131 if (options & WILD_ADD_SLASH)
5132 flags |= EW_ADDSLASH;
5133 if (options & WILD_KEEP_ALL)
5134 flags |= EW_KEEPALL;
5135 if (options & WILD_SILENT)
5136 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005137 if (options & WILD_ALLLINKS)
5138 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005140 if (xp->xp_context == EXPAND_FILES
5141 || xp->xp_context == EXPAND_DIRECTORIES
5142 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 {
5144 /*
5145 * Expand file or directory names.
5146 */
5147 int free_pat = FALSE;
5148 int i;
5149
5150 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5151 * space */
5152 if (xp->xp_backslash != XP_BS_NONE)
5153 {
5154 free_pat = TRUE;
5155 pat = vim_strsave(pat);
5156 for (i = 0; pat[i]; ++i)
5157 if (pat[i] == '\\')
5158 {
5159 if (xp->xp_backslash == XP_BS_THREE
5160 && pat[i + 1] == '\\'
5161 && pat[i + 2] == '\\'
5162 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005163 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (xp->xp_backslash == XP_BS_ONE
5165 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005166 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 }
5169
5170 if (xp->xp_context == EXPAND_FILES)
5171 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005172 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5173 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 else
5175 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005176 if (options & WILD_ICASE)
5177 flags |= EW_ICASE;
5178
Bram Moolenaard7834d32009-12-02 16:14:36 +00005179 /* Expand wildcards, supporting %:h and the like. */
5180 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 if (free_pat)
5182 vim_free(pat);
5183 return ret;
5184 }
5185
5186 *file = (char_u **)"";
5187 *num_file = 0;
5188 if (xp->xp_context == EXPAND_HELP)
5189 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005190 /* With an empty argument we would get all the help tags, which is
5191 * very slow. Get matches for "help" instead. */
5192 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5193 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195#ifdef FEAT_MULTI_LANG
5196 cleanup_help_tags(*num_file, *file);
5197#endif
5198 return OK;
5199 }
5200 return FAIL;
5201 }
5202
5203#ifndef FEAT_CMDL_COMPL
5204 return FAIL;
5205#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005206 if (xp->xp_context == EXPAND_SHELLCMD)
5207 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 if (xp->xp_context == EXPAND_OLD_SETTING)
5209 return ExpandOldSetting(num_file, file);
5210 if (xp->xp_context == EXPAND_BUFFERS)
5211 return ExpandBufnames(pat, num_file, file, options);
5212 if (xp->xp_context == EXPAND_TAGS
5213 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5214 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5215 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005216 {
5217 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005218 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5219 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005222 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005223 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005224 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005225 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005226 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005227 {
5228 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005229 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005230 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005231 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005232 {
5233 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005234 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005235 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005236# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5237 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005238 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005239# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005240 if (xp->xp_context == EXPAND_PACKADD)
5241 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5244 if (regmatch.regprog == NULL)
5245 return FAIL;
5246
5247 /* set ignore-case according to p_ic, p_scs and pat */
5248 regmatch.rm_ic = ignorecase(pat);
5249
5250 if (xp->xp_context == EXPAND_SETTINGS
5251 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5252 ret = ExpandSettings(xp, &regmatch, num_file, file);
5253 else if (xp->xp_context == EXPAND_MAPPINGS)
5254 ret = ExpandMappings(&regmatch, num_file, file);
5255# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5256 else if (xp->xp_context == EXPAND_USER_DEFINED)
5257 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5258# endif
5259 else
5260 {
5261 static struct expgen
5262 {
5263 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005264 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005266 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 } tab[] =
5268 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005269 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5270 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005271 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005272 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005273#ifdef FEAT_CMDHIST
5274 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5275#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005278 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005279 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5280 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5281 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#endif
5283#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005284 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5285 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5286 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5287 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288#endif
5289#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005290 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5291 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292#endif
5293#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005294 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005296#ifdef FEAT_PROFILE
5297 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5298#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005299 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005300 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5301 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005302#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005303 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005304#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005305#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005306 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005307#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005308#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005309 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5312 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005313 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5314 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005316 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005317 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005318 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 };
5320 int i;
5321
5322 /*
5323 * Find a context in the table and call the ExpandGeneric() with the
5324 * right function to do the expansion.
5325 */
5326 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005327 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 if (xp->xp_context == tab[i].context)
5329 {
5330 if (tab[i].ic)
5331 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005332 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005333 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 break;
5335 }
5336 }
5337
Bram Moolenaar473de612013-06-08 18:19:48 +02005338 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 return ret;
5341#endif /* FEAT_CMDL_COMPL */
5342}
5343
5344#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5345/*
5346 * Expand a list of names.
5347 *
5348 * Generic function for command line completion. It calls a function to
5349 * obtain strings, one by one. The strings are matched against a regexp
5350 * program. Matching strings are copied into an array, which is returned.
5351 *
5352 * Returns OK when no problems encountered, FAIL for error (out of memory).
5353 */
5354 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005355ExpandGeneric(
5356 expand_T *xp,
5357 regmatch_T *regmatch,
5358 int *num_file,
5359 char_u ***file,
5360 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005362 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363{
5364 int i;
5365 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005366 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 char_u *str;
5368
5369 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005370 * round == 0: count the number of matching names
5371 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005373 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 {
5375 for (i = 0; ; ++i)
5376 {
5377 str = (*func)(xp, i);
5378 if (str == NULL) /* end of list */
5379 break;
5380 if (*str == NUL) /* skip empty strings */
5381 continue;
5382
5383 if (vim_regexec(regmatch, str, (colnr_T)0))
5384 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005385 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005387 if (escaped)
5388 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5389 else
5390 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 (*file)[count] = str;
5392#ifdef FEAT_MENU
5393 if (func == get_menu_names && str != NULL)
5394 {
5395 /* test for separator added by get_menu_names() */
5396 str += STRLEN(str) - 1;
5397 if (*str == '\001')
5398 *str = '.';
5399 }
5400#endif
5401 }
5402 ++count;
5403 }
5404 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005405 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
5407 if (count == 0)
5408 return OK;
5409 *num_file = count;
5410 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5411 if (*file == NULL)
5412 {
5413 *file = (char_u **)"";
5414 return FAIL;
5415 }
5416 count = 0;
5417 }
5418 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005419
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005420 /* Sort the results. Keep menu's in the specified order. */
5421 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005422 {
5423 if (xp->xp_context == EXPAND_EXPRESSION
5424 || xp->xp_context == EXPAND_FUNCTIONS
5425 || xp->xp_context == EXPAND_USER_FUNC)
5426 /* <SNR> functions should be sorted to the end. */
5427 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5428 sort_func_compare);
5429 else
5430 sort_strings(*file, *num_file);
5431 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005432
Bram Moolenaar4f688582007-07-24 12:34:30 +00005433#ifdef FEAT_CMDL_COMPL
5434 /* Reset the variables used for special highlight names expansion, so that
5435 * they don't show up when getting normal highlight names by ID. */
5436 reset_expand_highlight();
5437#endif
5438
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 return OK;
5440}
5441
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005442/*
5443 * Complete a shell command.
5444 * Returns FAIL or OK;
5445 */
5446 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005447expand_shellcmd(
5448 char_u *filepat, /* pattern to match with command names */
5449 int *num_file, /* return: number of matches */
5450 char_u ***file, /* return: array with matches */
5451 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005452{
5453 char_u *pat;
5454 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005455 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005456 int mustfree = FALSE;
5457 garray_T ga;
5458 char_u *buf = alloc(MAXPATHL);
5459 size_t l;
5460 char_u *s, *e;
5461 int flags = flagsarg;
5462 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005463 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005464 hashtab_T found_ht;
5465 hashitem_T *hi;
5466 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005467
5468 if (buf == NULL)
5469 return FAIL;
5470
5471 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5472 * space */
5473 pat = vim_strsave(filepat);
5474 for (i = 0; pat[i]; ++i)
5475 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005476 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005477
Bram Moolenaarb5971142015-03-21 17:32:19 +01005478 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005479
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005480 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5481 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005482 path = (char_u *)".";
5483 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005484 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005485 /* For an absolute name we don't use $PATH. */
5486 if (!mch_isFullName(pat))
5487 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005488 if (path == NULL)
5489 path = (char_u *)"";
5490 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005491
5492 /*
5493 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005494 * collect them in "ga". When "." is not in $PATH also expand for the
5495 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005496 */
5497 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005498 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005499 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005500 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005501#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005502 e = vim_strchr(s, ';');
5503#else
5504 e = vim_strchr(s, ':');
5505#endif
5506 if (e == NULL)
5507 e = s + STRLEN(s);
5508
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005509 if (*s == NUL)
5510 {
5511 if (did_curdir)
5512 break;
5513 // Find directories in the current directory, path is empty.
5514 did_curdir = TRUE;
5515 flags |= EW_DIR;
5516 }
5517 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5518 {
5519 did_curdir = TRUE;
5520 flags |= EW_DIR;
5521 }
5522 else
5523 // Do not match directories inside a $PATH item.
5524 flags &= ~EW_DIR;
5525
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005526 l = e - s;
5527 if (l > MAXPATHL - 5)
5528 break;
5529 vim_strncpy(buf, s, l);
5530 add_pathsep(buf);
5531 l = STRLEN(buf);
5532 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5533
5534 /* Expand matches in one directory of $PATH. */
5535 ret = expand_wildcards(1, &buf, num_file, file, flags);
5536 if (ret == OK)
5537 {
5538 if (ga_grow(&ga, *num_file) == FAIL)
5539 FreeWild(*num_file, *file);
5540 else
5541 {
5542 for (i = 0; i < *num_file; ++i)
5543 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005544 char_u *name = (*file)[i];
5545
5546 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005547 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005548 // Check if this name was already found.
5549 hash = hash_hash(name + l);
5550 hi = hash_lookup(&found_ht, name + l, hash);
5551 if (HASHITEM_EMPTY(hi))
5552 {
5553 // Remove the path that was prepended.
5554 STRMOVE(name, name + l);
5555 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5556 hash_add_item(&found_ht, hi, name, hash);
5557 name = NULL;
5558 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005559 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005560 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005561 }
5562 vim_free(*file);
5563 }
5564 }
5565 if (*e != NUL)
5566 ++e;
5567 }
5568 *file = ga.ga_data;
5569 *num_file = ga.ga_len;
5570
5571 vim_free(buf);
5572 vim_free(pat);
5573 if (mustfree)
5574 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005575 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005576 return OK;
5577}
5578
5579
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5581/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005582 * Call "user_expand_func()" to invoke a user defined Vim script function and
5583 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005585 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005586call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005587 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588 expand_T *xp,
5589 int *num_file,
5590 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005592 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005593 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005595 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005596 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005597 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598
Bram Moolenaarb7515462013-06-29 12:58:33 +02005599 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005600 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 *num_file = 0;
5602 *file = NULL;
5603
Bram Moolenaarb7515462013-06-29 12:58:33 +02005604 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005605 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005606 keep = ccline.cmdbuff[ccline.cmdlen];
5607 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005608 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005609
Bram Moolenaarffa96842018-06-12 22:05:14 +02005610 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5611
5612 args[0].v_type = VAR_STRING;
5613 args[0].vval.v_string = pat;
5614 args[1].v_type = VAR_STRING;
5615 args[1].vval.v_string = xp->xp_line;
5616 args[2].v_type = VAR_NUMBER;
5617 args[2].vval.v_number = xp->xp_col;
5618 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005620 /* Save the cmdline, we don't know what the function may do. */
5621 save_ccline = ccline;
5622 ccline.cmdbuff = NULL;
5623 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005625
Bram Moolenaarded27a12018-08-01 19:06:03 +02005626 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005627
5628 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005630 if (ccline.cmdbuff != NULL)
5631 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005632
Bram Moolenaarffa96842018-06-12 22:05:14 +02005633 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005634 return ret;
5635}
5636
5637/*
5638 * Expand names with a function defined by the user.
5639 */
5640 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005641ExpandUserDefined(
5642 expand_T *xp,
5643 regmatch_T *regmatch,
5644 int *num_file,
5645 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005646{
5647 char_u *retstr;
5648 char_u *s;
5649 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005650 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005651 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005652 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005653
5654 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5655 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005656 return FAIL;
5657
5658 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005659 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 {
5661 e = vim_strchr(s, '\n');
5662 if (e == NULL)
5663 e = s + STRLEN(s);
5664 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005665 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005667 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5668 *e = keep;
5669
5670 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005672 if (ga_grow(&ga, 1) == FAIL)
5673 break;
5674 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5675 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 if (*e != NUL)
5679 ++e;
5680 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005681 vim_free(retstr);
5682 *file = ga.ga_data;
5683 *num_file = ga.ga_len;
5684 return OK;
5685}
5686
5687/*
5688 * Expand names with a list returned by a function defined by the user.
5689 */
5690 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005691ExpandUserList(
5692 expand_T *xp,
5693 int *num_file,
5694 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005695{
5696 list_T *retlist;
5697 listitem_T *li;
5698 garray_T ga;
5699
5700 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5701 if (retlist == NULL)
5702 return FAIL;
5703
5704 ga_init2(&ga, (int)sizeof(char *), 3);
5705 /* Loop over the items in the list. */
5706 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5707 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005708 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5709 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005710
5711 if (ga_grow(&ga, 1) == FAIL)
5712 break;
5713
5714 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005715 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005716 ++ga.ga_len;
5717 }
5718 list_unref(retlist);
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 *file = ga.ga_data;
5721 *num_file = ga.ga_len;
5722 return OK;
5723}
5724#endif
5725
5726/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005727 * Expand color scheme, compiler or filetype names.
5728 * Search from 'runtimepath':
5729 * 'runtimepath'/{dirnames}/{pat}.vim
5730 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5731 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5732 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5733 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005734 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 */
5736 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005737ExpandRTDir(
5738 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005739 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005740 int *num_file,
5741 char_u ***file,
5742 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 char_u *s;
5745 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005746 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005748 int i;
5749 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750
5751 *num_file = 0;
5752 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005753 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005754 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005756 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005758 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5759 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005761 ga_clear_strings(&ga);
5762 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005764 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005765 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005766 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005768
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005769 if (flags & DIP_START) {
5770 for (i = 0; dirnames[i] != NULL; ++i)
5771 {
5772 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5773 if (s == NULL)
5774 {
5775 ga_clear_strings(&ga);
5776 return FAIL;
5777 }
5778 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5779 globpath(p_pp, s, &ga, 0);
5780 vim_free(s);
5781 }
5782 }
5783
5784 if (flags & DIP_OPT) {
5785 for (i = 0; dirnames[i] != NULL; ++i)
5786 {
5787 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5788 if (s == NULL)
5789 {
5790 ga_clear_strings(&ga);
5791 return FAIL;
5792 }
5793 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5794 globpath(p_pp, s, &ga, 0);
5795 vim_free(s);
5796 }
5797 }
5798
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005799 for (i = 0; i < ga.ga_len; ++i)
5800 {
5801 match = ((char_u **)ga.ga_data)[i];
5802 s = match;
5803 e = s + STRLEN(s);
5804 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5805 {
5806 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005807 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005808 if (s < match || vim_ispathsep(*s))
5809 break;
5810 ++s;
5811 *e = NUL;
5812 mch_memmove(match, s, e - s + 1);
5813 }
5814 }
5815
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005816 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005817 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005818
5819 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005820 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005821 remove_duplicates(&ga);
5822
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 *file = ga.ga_data;
5824 *num_file = ga.ga_len;
5825 return OK;
5826}
5827
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005828/*
5829 * Expand loadplugin names:
5830 * 'packpath'/pack/ * /opt/{pat}
5831 */
5832 static int
5833ExpandPackAddDir(
5834 char_u *pat,
5835 int *num_file,
5836 char_u ***file)
5837{
5838 char_u *s;
5839 char_u *e;
5840 char_u *match;
5841 garray_T ga;
5842 int i;
5843 int pat_len;
5844
5845 *num_file = 0;
5846 *file = NULL;
5847 pat_len = (int)STRLEN(pat);
5848 ga_init2(&ga, (int)sizeof(char *), 10);
5849
5850 s = alloc((unsigned)(pat_len + 26));
5851 if (s == NULL)
5852 {
5853 ga_clear_strings(&ga);
5854 return FAIL;
5855 }
5856 sprintf((char *)s, "pack/*/opt/%s*", pat);
5857 globpath(p_pp, s, &ga, 0);
5858 vim_free(s);
5859
5860 for (i = 0; i < ga.ga_len; ++i)
5861 {
5862 match = ((char_u **)ga.ga_data)[i];
5863 s = gettail(match);
5864 e = s + STRLEN(s);
5865 mch_memmove(match, s, e - s + 1);
5866 }
5867
5868 if (ga.ga_len == 0)
5869 return FAIL;
5870
5871 /* Sort and remove duplicates which can happen when specifying multiple
5872 * directories in dirnames. */
5873 remove_duplicates(&ga);
5874
5875 *file = ga.ga_data;
5876 *num_file = ga.ga_len;
5877 return OK;
5878}
5879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880#endif
5881
5882#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5883/*
5884 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005885 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005887 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005888globpath(
5889 char_u *path,
5890 char_u *file,
5891 garray_T *ga,
5892 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 expand_T xpc;
5895 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 int num_p;
5898 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899
5900 buf = alloc(MAXPATHL);
5901 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005902 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005904 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 /* Loop over all entries in {path}. */
5908 while (*path != NUL)
5909 {
5910 /* Copy one item of the path to buf[] and concatenate the file name. */
5911 copy_option_part(&path, buf, MAXPATHL, ",");
5912 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5913 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005914# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005915 /* Using the platform's path separator (\) makes vim incorrectly
5916 * treat it as an escape character, use '/' instead. */
5917 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5918 STRCAT(buf, "/");
5919# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005921# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005923 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5924 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005926 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005928 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930 for (i = 0; i < num_p; ++i)
5931 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005932 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005933 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005934 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005937
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 FreeWild(num_p, p);
5939 }
5940 }
5941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942
5943 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944}
5945
5946#endif
5947
5948#if defined(FEAT_CMDHIST) || defined(PROTO)
5949
5950/*********************************
5951 * Command line history stuff *
5952 *********************************/
5953
5954/*
5955 * Translate a history character to the associated type number.
5956 */
5957 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005958hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959{
5960 if (c == ':')
5961 return HIST_CMD;
5962 if (c == '=')
5963 return HIST_EXPR;
5964 if (c == '@')
5965 return HIST_INPUT;
5966 if (c == '>')
5967 return HIST_DEBUG;
5968 return HIST_SEARCH; /* must be '?' or '/' */
5969}
5970
5971/*
5972 * Table of history names.
5973 * These names are used in :history and various hist...() functions.
5974 * It is sufficient to give the significant prefix of a history name.
5975 */
5976
5977static char *(history_names[]) =
5978{
5979 "cmd",
5980 "search",
5981 "expr",
5982 "input",
5983 "debug",
5984 NULL
5985};
5986
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005987#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5988/*
5989 * Function given to ExpandGeneric() to obtain the possible first
5990 * arguments of the ":history command.
5991 */
5992 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005994{
5995 static char_u compl[2] = { NUL, NUL };
5996 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005997 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005998 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5999
6000 if (idx < short_names_count)
6001 {
6002 compl[0] = (char_u)short_names[idx];
6003 return compl;
6004 }
6005 if (idx < short_names_count + history_name_count)
6006 return (char_u *)history_names[idx - short_names_count];
6007 if (idx == short_names_count + history_name_count)
6008 return (char_u *)"all";
6009 return NULL;
6010}
6011#endif
6012
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013/*
6014 * init_history() - Initialize the command line history.
6015 * Also used to re-allocate the history when the size changes.
6016 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006017 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006018init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019{
6020 int newlen; /* new length of history table */
6021 histentry_T *temp;
6022 int i;
6023 int j;
6024 int type;
6025
6026 /*
6027 * If size of history table changed, reallocate it
6028 */
6029 newlen = (int)p_hi;
6030 if (newlen != hislen) /* history length changed */
6031 {
6032 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6033 {
6034 if (newlen)
6035 {
6036 temp = (histentry_T *)lalloc(
6037 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6038 if (temp == NULL) /* out of memory! */
6039 {
6040 if (type == 0) /* first one: just keep the old length */
6041 {
6042 newlen = hislen;
6043 break;
6044 }
6045 /* Already changed one table, now we can only have zero
6046 * length for all tables. */
6047 newlen = 0;
6048 type = -1;
6049 continue;
6050 }
6051 }
6052 else
6053 temp = NULL;
6054 if (newlen == 0 || temp != NULL)
6055 {
6056 if (hisidx[type] < 0) /* there are no entries yet */
6057 {
6058 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006059 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060 }
6061 else if (newlen > hislen) /* array becomes bigger */
6062 {
6063 for (i = 0; i <= hisidx[type]; ++i)
6064 temp[i] = history[type][i];
6065 j = i;
6066 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006067 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 for ( ; j < hislen; ++i, ++j)
6069 temp[i] = history[type][j];
6070 }
6071 else /* array becomes smaller or 0 */
6072 {
6073 j = hisidx[type];
6074 for (i = newlen - 1; ; --i)
6075 {
6076 if (i >= 0) /* copy newest entries */
6077 temp[i] = history[type][j];
6078 else /* remove older entries */
6079 vim_free(history[type][j].hisstr);
6080 if (--j < 0)
6081 j = hislen - 1;
6082 if (j == hisidx[type])
6083 break;
6084 }
6085 hisidx[type] = newlen - 1;
6086 }
6087 vim_free(history[type]);
6088 history[type] = temp;
6089 }
6090 }
6091 hislen = newlen;
6092 }
6093}
6094
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006095 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006096clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006097{
6098 hisptr->hisnum = 0;
6099 hisptr->viminfo = FALSE;
6100 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006101 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006102}
6103
Bram Moolenaar071d4272004-06-13 20:20:40 +00006104/*
6105 * Check if command line 'str' is already in history.
6106 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6107 */
6108 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006109in_history(
6110 int type,
6111 char_u *str,
6112 int move_to_front, /* Move the entry to the front if it exists */
6113 int sep,
6114 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115{
6116 int i;
6117 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006118 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119
6120 if (hisidx[type] < 0)
6121 return FALSE;
6122 i = hisidx[type];
6123 do
6124 {
6125 if (history[type][i].hisstr == NULL)
6126 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006127
6128 /* For search history, check that the separator character matches as
6129 * well. */
6130 p = history[type][i].hisstr;
6131 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006132 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006133 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 {
6135 if (!move_to_front)
6136 return TRUE;
6137 last_i = i;
6138 break;
6139 }
6140 if (--i < 0)
6141 i = hislen - 1;
6142 } while (i != hisidx[type]);
6143
6144 if (last_i >= 0)
6145 {
6146 str = history[type][i].hisstr;
6147 while (i != hisidx[type])
6148 {
6149 if (++i >= hislen)
6150 i = 0;
6151 history[type][last_i] = history[type][i];
6152 last_i = i;
6153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006155 history[type][i].viminfo = FALSE;
6156 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006157 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 return TRUE;
6159 }
6160 return FALSE;
6161}
6162
6163/*
6164 * Convert history name (from table above) to its HIST_ equivalent.
6165 * When "name" is empty, return "cmd" history.
6166 * Returns -1 for unknown history name.
6167 */
6168 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006169get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170{
6171 int i;
6172 int len = (int)STRLEN(name);
6173
6174 /* No argument: use current history. */
6175 if (len == 0)
6176 return hist_char2type(ccline.cmdfirstc);
6177
6178 for (i = 0; history_names[i] != NULL; ++i)
6179 if (STRNICMP(name, history_names[i], len) == 0)
6180 return i;
6181
6182 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6183 return hist_char2type(name[0]);
6184
6185 return -1;
6186}
6187
6188static int last_maptick = -1; /* last seen maptick */
6189
6190/*
6191 * Add the given string to the given history. If the string is already in the
6192 * history then it is moved to the front. "histype" may be one of he HIST_
6193 * values.
6194 */
6195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006196add_to_history(
6197 int histype,
6198 char_u *new_entry,
6199 int in_map, /* consider maptick when inside a mapping */
6200 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006201{
6202 histentry_T *hisptr;
6203 int len;
6204
6205 if (hislen == 0) /* no history */
6206 return;
6207
Bram Moolenaara939e432013-11-09 05:30:26 +01006208 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6209 return;
6210
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 /*
6212 * Searches inside the same mapping overwrite each other, so that only
6213 * the last line is kept. Be careful not to remove a line that was moved
6214 * down, only lines that were added.
6215 */
6216 if (histype == HIST_SEARCH && in_map)
6217 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006218 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220 /* Current line is from the same mapping, remove it */
6221 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6222 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006223 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 --hisnum[histype];
6225 if (--hisidx[HIST_SEARCH] < 0)
6226 hisidx[HIST_SEARCH] = hislen - 1;
6227 }
6228 last_maptick = -1;
6229 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006230 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 {
6232 if (++hisidx[histype] == hislen)
6233 hisidx[histype] = 0;
6234 hisptr = &history[histype][hisidx[histype]];
6235 vim_free(hisptr->hisstr);
6236
6237 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006238 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006239 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6240 if (hisptr->hisstr != NULL)
6241 hisptr->hisstr[len + 1] = sep;
6242
6243 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006244 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006245 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006246 if (histype == HIST_SEARCH && in_map)
6247 last_maptick = maptick;
6248 }
6249}
6250
6251#if defined(FEAT_EVAL) || defined(PROTO)
6252
6253/*
6254 * Get identifier of newest history entry.
6255 * "histype" may be one of the HIST_ values.
6256 */
6257 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006258get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259{
6260 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6261 || hisidx[histype] < 0)
6262 return -1;
6263
6264 return history[histype][hisidx[histype]].hisnum;
6265}
6266
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006267/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006268 * Calculate history index from a number:
6269 * num > 0: seen as identifying number of a history entry
6270 * num < 0: relative position in history wrt newest entry
6271 * "histype" may be one of the HIST_ values.
6272 */
6273 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006274calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006275{
6276 int i;
6277 histentry_T *hist;
6278 int wrapped = FALSE;
6279
6280 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6281 || (i = hisidx[histype]) < 0 || num == 0)
6282 return -1;
6283
6284 hist = history[histype];
6285 if (num > 0)
6286 {
6287 while (hist[i].hisnum > num)
6288 if (--i < 0)
6289 {
6290 if (wrapped)
6291 break;
6292 i += hislen;
6293 wrapped = TRUE;
6294 }
6295 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6296 return i;
6297 }
6298 else if (-num <= hislen)
6299 {
6300 i += num + 1;
6301 if (i < 0)
6302 i += hislen;
6303 if (hist[i].hisstr != NULL)
6304 return i;
6305 }
6306 return -1;
6307}
6308
6309/*
6310 * Get a history entry by its index.
6311 * "histype" may be one of the HIST_ values.
6312 */
6313 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006314get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315{
6316 idx = calc_hist_idx(histype, idx);
6317 if (idx >= 0)
6318 return history[histype][idx].hisstr;
6319 else
6320 return (char_u *)"";
6321}
6322
6323/*
6324 * Clear all entries of a history.
6325 * "histype" may be one of the HIST_ values.
6326 */
6327 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006328clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 int i;
6331 histentry_T *hisptr;
6332
6333 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6334 {
6335 hisptr = history[histype];
6336 for (i = hislen; i--;)
6337 {
6338 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006339 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006340 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 }
6342 hisidx[histype] = -1; /* mark history as cleared */
6343 hisnum[histype] = 0; /* reset identifier counter */
6344 return OK;
6345 }
6346 return FAIL;
6347}
6348
6349/*
6350 * Remove all entries matching {str} from a history.
6351 * "histype" may be one of the HIST_ values.
6352 */
6353 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006354del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355{
6356 regmatch_T regmatch;
6357 histentry_T *hisptr;
6358 int idx;
6359 int i;
6360 int last;
6361 int found = FALSE;
6362
6363 regmatch.regprog = NULL;
6364 regmatch.rm_ic = FALSE; /* always match case */
6365 if (hislen != 0
6366 && histype >= 0
6367 && histype < HIST_COUNT
6368 && *str != NUL
6369 && (idx = hisidx[histype]) >= 0
6370 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6371 != NULL)
6372 {
6373 i = last = idx;
6374 do
6375 {
6376 hisptr = &history[histype][i];
6377 if (hisptr->hisstr == NULL)
6378 break;
6379 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6380 {
6381 found = TRUE;
6382 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006383 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 }
6385 else
6386 {
6387 if (i != last)
6388 {
6389 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006390 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 }
6392 if (--last < 0)
6393 last += hislen;
6394 }
6395 if (--i < 0)
6396 i += hislen;
6397 } while (i != idx);
6398 if (history[histype][idx].hisstr == NULL)
6399 hisidx[histype] = -1;
6400 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006401 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 return found;
6403}
6404
6405/*
6406 * Remove an indexed entry from a history.
6407 * "histype" may be one of the HIST_ values.
6408 */
6409 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006410del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006411{
6412 int i, j;
6413
6414 i = calc_hist_idx(histype, idx);
6415 if (i < 0)
6416 return FALSE;
6417 idx = hisidx[histype];
6418 vim_free(history[histype][i].hisstr);
6419
6420 /* When deleting the last added search string in a mapping, reset
6421 * last_maptick, so that the last added search string isn't deleted again.
6422 */
6423 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6424 last_maptick = -1;
6425
6426 while (i != idx)
6427 {
6428 j = (i + 1) % hislen;
6429 history[histype][i] = history[histype][j];
6430 i = j;
6431 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006432 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 if (--i < 0)
6434 i += hislen;
6435 hisidx[histype] = i;
6436 return TRUE;
6437}
6438
6439#endif /* FEAT_EVAL */
6440
6441#if defined(FEAT_CRYPT) || defined(PROTO)
6442/*
6443 * Very specific function to remove the value in ":set key=val" from the
6444 * history.
6445 */
6446 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006447remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448{
6449 char_u *p;
6450 int i;
6451
6452 i = hisidx[HIST_CMD];
6453 if (i < 0)
6454 return;
6455 p = history[HIST_CMD][i].hisstr;
6456 if (p != NULL)
6457 for ( ; *p; ++p)
6458 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6459 {
6460 p = vim_strchr(p + 3, '=');
6461 if (p == NULL)
6462 break;
6463 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006464 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 if (p[i] == '\\' && p[i + 1])
6466 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006467 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006468 --p;
6469 }
6470}
6471#endif
6472
6473#endif /* FEAT_CMDHIST */
6474
Bram Moolenaar064154c2016-03-19 22:50:43 +01006475#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006476/*
6477 * Get pointer to the command line info to use. cmdline_paste() may clear
6478 * ccline and put the previous value in prev_ccline.
6479 */
6480 static struct cmdline_info *
6481get_ccline_ptr(void)
6482{
6483 if ((State & CMDLINE) == 0)
6484 return NULL;
6485 if (ccline.cmdbuff != NULL)
6486 return &ccline;
6487 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6488 return &prev_ccline;
6489 return NULL;
6490}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006491#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006492
Bram Moolenaar064154c2016-03-19 22:50:43 +01006493#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006494/*
6495 * Get the current command line in allocated memory.
6496 * Only works when the command line is being edited.
6497 * Returns NULL when something is wrong.
6498 */
6499 char_u *
6500get_cmdline_str(void)
6501{
6502 struct cmdline_info *p = get_ccline_ptr();
6503
6504 if (p == NULL)
6505 return NULL;
6506 return vim_strnsave(p->cmdbuff, p->cmdlen);
6507}
6508
6509/*
6510 * Get the current command line position, counted in bytes.
6511 * Zero is the first position.
6512 * Only works when the command line is being edited.
6513 * Returns -1 when something is wrong.
6514 */
6515 int
6516get_cmdline_pos(void)
6517{
6518 struct cmdline_info *p = get_ccline_ptr();
6519
6520 if (p == NULL)
6521 return -1;
6522 return p->cmdpos;
6523}
6524
6525/*
6526 * Set the command line byte position to "pos". Zero is the first position.
6527 * Only works when the command line is being edited.
6528 * Returns 1 when failed, 0 when OK.
6529 */
6530 int
6531set_cmdline_pos(
6532 int pos)
6533{
6534 struct cmdline_info *p = get_ccline_ptr();
6535
6536 if (p == NULL)
6537 return 1;
6538
6539 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6540 * changed the command line. */
6541 if (pos < 0)
6542 new_cmdpos = 0;
6543 else
6544 new_cmdpos = pos;
6545 return 0;
6546}
6547#endif
6548
6549#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6550/*
6551 * Get the current command-line type.
6552 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6553 * Only works when the command line is being edited.
6554 * Returns NUL when something is wrong.
6555 */
6556 int
6557get_cmdline_type(void)
6558{
6559 struct cmdline_info *p = get_ccline_ptr();
6560
6561 if (p == NULL)
6562 return NUL;
6563 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006564 return
6565# ifdef FEAT_EVAL
6566 (p->input_fn) ? '@' :
6567# endif
6568 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006569 return p->cmdfirstc;
6570}
6571#endif
6572
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6574/*
6575 * Get indices "num1,num2" that specify a range within a list (not a range of
6576 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6577 * Returns OK if parsed successfully, otherwise FAIL.
6578 */
6579 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006580get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 int len;
6583 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006584 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586 *str = skipwhite(*str);
6587 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6588 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006589 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 *str += len;
6591 *num1 = (int)num;
6592 first = TRUE;
6593 }
6594 *str = skipwhite(*str);
6595 if (**str == ',') /* parse "to" part of range */
6596 {
6597 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006598 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 if (len > 0)
6600 {
6601 *num2 = (int)num;
6602 *str = skipwhite(*str + len);
6603 }
6604 else if (!first) /* no number given at all */
6605 return FAIL;
6606 }
6607 else if (first) /* only one number given */
6608 *num2 = *num1;
6609 return OK;
6610}
6611#endif
6612
6613#if defined(FEAT_CMDHIST) || defined(PROTO)
6614/*
6615 * :history command - print a history
6616 */
6617 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006618ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619{
6620 histentry_T *hist;
6621 int histype1 = HIST_CMD;
6622 int histype2 = HIST_CMD;
6623 int hisidx1 = 1;
6624 int hisidx2 = -1;
6625 int idx;
6626 int i, j, k;
6627 char_u *end;
6628 char_u *arg = eap->arg;
6629
6630 if (hislen == 0)
6631 {
6632 MSG(_("'history' option is zero"));
6633 return;
6634 }
6635
6636 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6637 {
6638 end = arg;
6639 while (ASCII_ISALPHA(*end)
6640 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6641 end++;
6642 i = *end;
6643 *end = NUL;
6644 histype1 = get_histtype(arg);
6645 if (histype1 == -1)
6646 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006647 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 {
6649 histype1 = 0;
6650 histype2 = HIST_COUNT-1;
6651 }
6652 else
6653 {
6654 *end = i;
6655 EMSG(_(e_trailing));
6656 return;
6657 }
6658 }
6659 else
6660 histype2 = histype1;
6661 *end = i;
6662 }
6663 else
6664 end = arg;
6665 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6666 {
6667 EMSG(_(e_trailing));
6668 return;
6669 }
6670
6671 for (; !got_int && histype1 <= histype2; ++histype1)
6672 {
6673 STRCPY(IObuff, "\n # ");
6674 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6675 MSG_PUTS_TITLE(IObuff);
6676 idx = hisidx[histype1];
6677 hist = history[histype1];
6678 j = hisidx1;
6679 k = hisidx2;
6680 if (j < 0)
6681 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6682 if (k < 0)
6683 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6684 if (idx >= 0 && j <= k)
6685 for (i = idx + 1; !got_int; ++i)
6686 {
6687 if (i == hislen)
6688 i = 0;
6689 if (hist[i].hisstr != NULL
6690 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6691 {
6692 msg_putchar('\n');
6693 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6694 hist[i].hisnum);
6695 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6696 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006697 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698 else
6699 STRCAT(IObuff, hist[i].hisstr);
6700 msg_outtrans(IObuff);
6701 out_flush();
6702 }
6703 if (i == idx)
6704 break;
6705 }
6706 }
6707}
6708#endif
6709
6710#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006711/*
6712 * Buffers for history read from a viminfo file. Only valid while reading.
6713 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006714static histentry_T *viminfo_history[HIST_COUNT] =
6715 {NULL, NULL, NULL, NULL, NULL};
6716static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6717static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718static int viminfo_add_at_front = FALSE;
6719
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006720static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721
6722/*
6723 * Translate a history type number to the associated character.
6724 */
6725 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006726hist_type2char(
6727 int type,
6728 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729{
6730 if (type == HIST_CMD)
6731 return ':';
6732 if (type == HIST_SEARCH)
6733 {
6734 if (use_question)
6735 return '?';
6736 else
6737 return '/';
6738 }
6739 if (type == HIST_EXPR)
6740 return '=';
6741 return '@';
6742}
6743
6744/*
6745 * Prepare for reading the history from the viminfo file.
6746 * This allocates history arrays to store the read history lines.
6747 */
6748 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006749prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750{
6751 int i;
6752 int num;
6753 int type;
6754 int len;
6755
6756 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006757 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 if (asklen > hislen)
6759 asklen = hislen;
6760
6761 for (type = 0; type < HIST_COUNT; ++type)
6762 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006763 /* Count the number of empty spaces in the history list. Entries read
6764 * from viminfo previously are also considered empty. If there are
6765 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006767 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 num++;
6769 len = asklen;
6770 if (num > len)
6771 len = num;
6772 if (len <= 0)
6773 viminfo_history[type] = NULL;
6774 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006775 viminfo_history[type] = (histentry_T *)lalloc(
6776 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 if (viminfo_history[type] == NULL)
6778 len = 0;
6779 viminfo_hislen[type] = len;
6780 viminfo_hisidx[type] = 0;
6781 }
6782}
6783
6784/*
6785 * Accept a line from the viminfo, store it in the history array when it's
6786 * new.
6787 */
6788 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006789read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790{
6791 int type;
6792 long_u len;
6793 char_u *val;
6794 char_u *p;
6795
6796 type = hist_char2type(virp->vir_line[0]);
6797 if (viminfo_hisidx[type] < viminfo_hislen[type])
6798 {
6799 val = viminfo_readstring(virp, 1, TRUE);
6800 if (val != NULL && *val != NUL)
6801 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006802 int sep = (*val == ' ' ? NUL : *val);
6803
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006805 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807 /* Need to re-allocate to append the separator byte. */
6808 len = STRLEN(val);
6809 p = lalloc(len + 2, TRUE);
6810 if (p != NULL)
6811 {
6812 if (type == HIST_SEARCH)
6813 {
6814 /* Search entry: Move the separator from the first
6815 * column to after the NUL. */
6816 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006817 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 }
6819 else
6820 {
6821 /* Not a search entry: No separator in the viminfo
6822 * file, add a NUL separator. */
6823 mch_memmove(p, val, (size_t)len + 1);
6824 p[len + 1] = NUL;
6825 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006826 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6827 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006828 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6829 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006830 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 }
6832 }
6833 }
6834 vim_free(val);
6835 }
6836 return viminfo_readline(virp);
6837}
6838
Bram Moolenaar07219f92013-04-14 23:19:36 +02006839/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006840 * Accept a new style history line from the viminfo, store it in the history
6841 * array when it's new.
6842 */
6843 void
6844handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006845 garray_T *values,
6846 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006847{
6848 int type;
6849 long_u len;
6850 char_u *val;
6851 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006852 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006853
6854 /* Check the format:
6855 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006856 if (values->ga_len < 4
6857 || vp[0].bv_type != BVAL_NR
6858 || vp[1].bv_type != BVAL_NR
6859 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6860 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006861 return;
6862
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006863 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006864 if (type >= HIST_COUNT)
6865 return;
6866 if (viminfo_hisidx[type] < viminfo_hislen[type])
6867 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006868 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006869 if (val != NULL && *val != NUL)
6870 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006871 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6872 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006873 int idx;
6874 int overwrite = FALSE;
6875
6876 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6877 {
6878 /* If lines were written by an older Vim we need to avoid
6879 * getting duplicates. See if the entry already exists. */
6880 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6881 {
6882 p = viminfo_history[type][idx].hisstr;
6883 if (STRCMP(val, p) == 0
6884 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6885 {
6886 overwrite = TRUE;
6887 break;
6888 }
6889 }
6890
6891 if (!overwrite)
6892 {
6893 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006894 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006895 p = lalloc(len + 2, TRUE);
6896 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006897 else
6898 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006899 if (p != NULL)
6900 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006901 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006902 if (!overwrite)
6903 {
6904 mch_memmove(p, val, (size_t)len + 1);
6905 /* Put the separator after the NUL. */
6906 p[len + 1] = sep;
6907 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006908 viminfo_history[type][idx].hisnum = 0;
6909 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006910 viminfo_hisidx[type]++;
6911 }
6912 }
6913 }
6914 }
6915 }
6916}
6917
6918/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006919 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006920 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006921 static void
6922concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923{
6924 int idx;
6925 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006926
6927 idx = hisidx[type] + viminfo_hisidx[type];
6928 if (idx >= hislen)
6929 idx -= hislen;
6930 else if (idx < 0)
6931 idx = hislen - 1;
6932 if (viminfo_add_at_front)
6933 hisidx[type] = idx;
6934 else
6935 {
6936 if (hisidx[type] == -1)
6937 hisidx[type] = hislen - 1;
6938 do
6939 {
6940 if (history[type][idx].hisstr != NULL
6941 || history[type][idx].viminfo)
6942 break;
6943 if (++idx == hislen)
6944 idx = 0;
6945 } while (idx != hisidx[type]);
6946 if (idx != hisidx[type] && --idx < 0)
6947 idx = hislen - 1;
6948 }
6949 for (i = 0; i < viminfo_hisidx[type]; i++)
6950 {
6951 vim_free(history[type][idx].hisstr);
6952 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6953 history[type][idx].viminfo = TRUE;
6954 history[type][idx].time_set = viminfo_history[type][i].time_set;
6955 if (--idx < 0)
6956 idx = hislen - 1;
6957 }
6958 idx += 1;
6959 idx %= hislen;
6960 for (i = 0; i < viminfo_hisidx[type]; i++)
6961 {
6962 history[type][idx++].hisnum = ++hisnum[type];
6963 idx %= hislen;
6964 }
6965}
6966
6967#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6968 static int
6969#ifdef __BORLANDC__
6970_RTLENTRYF
6971#endif
6972sort_hist(const void *s1, const void *s2)
6973{
6974 histentry_T *p1 = *(histentry_T **)s1;
6975 histentry_T *p2 = *(histentry_T **)s2;
6976
6977 if (p1->time_set < p2->time_set) return -1;
6978 if (p1->time_set > p2->time_set) return 1;
6979 return 0;
6980}
6981#endif
6982
6983/*
6984 * Merge history lines from viminfo and lines typed in this Vim based on the
6985 * timestamp;
6986 */
6987 static void
6988merge_history(int type)
6989{
6990 int max_len;
6991 histentry_T **tot_hist;
6992 histentry_T *new_hist;
6993 int i;
6994 int len;
6995
6996 /* Make one long list with all entries. */
6997 max_len = hislen + viminfo_hisidx[type];
6998 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6999 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
7000 if (tot_hist == NULL || new_hist == NULL)
7001 {
7002 vim_free(tot_hist);
7003 vim_free(new_hist);
7004 return;
7005 }
7006 for (i = 0; i < viminfo_hisidx[type]; i++)
7007 tot_hist[i] = &viminfo_history[type][i];
7008 len = i;
7009 for (i = 0; i < hislen; i++)
7010 if (history[type][i].hisstr != NULL)
7011 tot_hist[len++] = &history[type][i];
7012
7013 /* Sort the list on timestamp. */
7014 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7015
7016 /* Keep the newest ones. */
7017 for (i = 0; i < hislen; i++)
7018 {
7019 if (i < len)
7020 {
7021 new_hist[i] = *tot_hist[i];
7022 tot_hist[i]->hisstr = NULL;
7023 if (new_hist[i].hisnum == 0)
7024 new_hist[i].hisnum = ++hisnum[type];
7025 }
7026 else
7027 clear_hist_entry(&new_hist[i]);
7028 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007029 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007030
7031 /* Free what is not kept. */
7032 for (i = 0; i < viminfo_hisidx[type]; i++)
7033 vim_free(viminfo_history[type][i].hisstr);
7034 for (i = 0; i < hislen; i++)
7035 vim_free(history[type][i].hisstr);
7036 vim_free(history[type]);
7037 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007038 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007039}
7040
7041/*
7042 * Finish reading history lines from viminfo. Not used when writing viminfo.
7043 */
7044 void
7045finish_viminfo_history(vir_T *virp)
7046{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007048 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049
7050 for (type = 0; type < HIST_COUNT; ++type)
7051 {
7052 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007053 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007054
7055 if (merge)
7056 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007058 concat_history(type);
7059
Bram Moolenaard23a8232018-02-10 18:45:26 +01007060 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007061 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
7063}
7064
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007065/*
7066 * Write history to viminfo file in "fp".
7067 * When "merge" is TRUE merge history lines with a previously read viminfo
7068 * file, data is in viminfo_history[].
7069 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007071 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007072write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073{
7074 int i;
7075 int type;
7076 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007077 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 init_history();
7080 if (hislen == 0)
7081 return;
7082 for (type = 0; type < HIST_COUNT; ++type)
7083 {
7084 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7085 if (num_saved == 0)
7086 continue;
7087 if (num_saved < 0) /* Use default */
7088 num_saved = hislen;
7089 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7090 type == HIST_CMD ? _("Command Line") :
7091 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007092 type == HIST_EXPR ? _("Expression") :
7093 type == HIST_INPUT ? _("Input Line") :
7094 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095 if (num_saved > hislen)
7096 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007097
7098 /*
7099 * Merge typed and viminfo history:
7100 * round 1: history of typed commands.
7101 * round 2: history from recently read viminfo.
7102 */
7103 for (round = 1; round <= 2; ++round)
7104 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007105 if (round == 1)
7106 /* start at newest entry, somewhere in the list */
7107 i = hisidx[type];
7108 else if (viminfo_hisidx[type] > 0)
7109 /* start at newest entry, first in the list */
7110 i = 0;
7111 else
7112 /* empty list */
7113 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007114 if (i >= 0)
7115 while (num_saved > 0
7116 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007118 char_u *p;
7119 time_t timestamp;
7120 int c = NUL;
7121
7122 if (round == 1)
7123 {
7124 p = history[type][i].hisstr;
7125 timestamp = history[type][i].time_set;
7126 }
7127 else
7128 {
7129 p = viminfo_history[type] == NULL ? NULL
7130 : viminfo_history[type][i].hisstr;
7131 timestamp = viminfo_history[type] == NULL ? 0
7132 : viminfo_history[type][i].time_set;
7133 }
7134
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007135 if (p != NULL && (round == 2
7136 || !merge
7137 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007138 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007139 --num_saved;
7140 fputc(hist_type2char(type, TRUE), fp);
7141 /* For the search history: put the separator in the
7142 * second column; use a space if there isn't one. */
7143 if (type == HIST_SEARCH)
7144 {
7145 c = p[STRLEN(p) + 1];
7146 putc(c == NUL ? ' ' : c, fp);
7147 }
7148 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007149
7150 {
7151 char cbuf[NUMBUFLEN];
7152
7153 /* New style history with a bar line. Format:
7154 * |{bartype},{histtype},{timestamp},{separator},"text" */
7155 if (c == NUL)
7156 cbuf[0] = NUL;
7157 else
7158 sprintf(cbuf, "%d", c);
7159 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7160 type, (long)timestamp, cbuf);
7161 barline_writestring(fp, p, LSIZE - 20);
7162 putc('\n', fp);
7163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007165 if (round == 1)
7166 {
7167 /* Decrement index, loop around and stop when back at
7168 * the start. */
7169 if (--i < 0)
7170 i = hislen - 1;
7171 if (i == hisidx[type])
7172 break;
7173 }
7174 else
7175 {
7176 /* Increment index. Stop at the end in the while. */
7177 ++i;
7178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007180 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007181 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007182 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007183 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007184 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007185 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 }
7187}
7188#endif /* FEAT_VIMINFO */
7189
7190#if defined(FEAT_FKMAP) || defined(PROTO)
7191/*
7192 * Write a character at the current cursor+offset position.
7193 * It is directly written into the command buffer block.
7194 */
7195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007196cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197{
7198 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7199 {
7200 EMSG(_("E198: cmd_pchar beyond the command length"));
7201 return;
7202 }
7203 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7204 ccline.cmdbuff[ccline.cmdlen] = NUL;
7205}
7206
7207 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007208cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209{
7210 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7211 {
7212 /* EMSG(_("cmd_gchar beyond the command length")); */
7213 return NUL;
7214 }
7215 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7216}
7217#endif
7218
7219#if defined(FEAT_CMDWIN) || defined(PROTO)
7220/*
7221 * Open a window on the current command line and history. Allow editing in
7222 * the window. Returns when the window is closed.
7223 * Returns:
7224 * CR if the command is to be executed
7225 * Ctrl_C if it is to be abandoned
7226 * K_IGNORE if editing continues
7227 */
7228 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007229open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230{
7231 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007232 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007234 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 win_T *wp;
7236 int i;
7237 linenr_T lnum;
7238 int histtype;
7239 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 int save_restart_edit = restart_edit;
7241 int save_State = State;
7242 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007243#ifdef FEAT_RIGHTLEFT
7244 int save_cmdmsg_rl = cmdmsg_rl;
7245#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007246#ifdef FEAT_FOLDING
7247 int save_KeyTyped;
7248#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
7250 /* Can't do this recursively. Can't do it when typing a password. */
7251 if (cmdwin_type != 0
7252# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7253 || cmdline_star > 0
7254# endif
7255 )
7256 {
7257 beep_flush();
7258 return K_IGNORE;
7259 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007260 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
7262 /* Save current window sizes. */
7263 win_size_save(&winsizes);
7264
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007266 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007267
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007268 /* don't use a new tab page */
7269 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007270 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007271
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 /* Create a window for the command-line buffer. */
7273 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7274 {
7275 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007276 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277 return K_IGNORE;
7278 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007279 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280
7281 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007282 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007283 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007286#ifdef FEAT_FOLDING
7287 curwin->w_p_fen = FALSE;
7288#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007290 curwin->w_p_rl = cmdmsg_rl;
7291 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007293 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007296 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007297 /* But don't allow switching to another buffer. */
7298 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299
Bram Moolenaar46152342005-09-07 21:18:43 +00007300 /* Showing the prompt may have set need_wait_return, reset it. */
7301 need_wait_return = FALSE;
7302
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007303 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7305 {
7306 if (p_wc == TAB)
7307 {
7308 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7309 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7310 }
7311 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7312 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007313 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007315 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7316 * sets 'textwidth' to 78). */
7317 curbuf->b_p_tw = 0;
7318
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319 /* Fill the buffer with the history. */
7320 init_history();
7321 if (hislen > 0)
7322 {
7323 i = hisidx[histtype];
7324 if (i >= 0)
7325 {
7326 lnum = 0;
7327 do
7328 {
7329 if (++i == hislen)
7330 i = 0;
7331 if (history[histtype][i].hisstr != NULL)
7332 ml_append(lnum++, history[histtype][i].hisstr,
7333 (colnr_T)0, FALSE);
7334 }
7335 while (i != hisidx[histtype]);
7336 }
7337 }
7338
7339 /* Replace the empty last line with the current command-line and put the
7340 * cursor there. */
7341 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7342 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7343 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007344 changed_line_abv_curs();
7345 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007346 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347
7348 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007349 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350
7351 /* No Ex mode here! */
7352 exmode_active = 0;
7353
7354 State = NORMAL;
7355# ifdef FEAT_MOUSE
7356 setmouse();
7357# endif
7358
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007360 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007361 if (restart_edit != 0) /* autocmd with ":startinsert" */
7362 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 i = RedrawingDisabled;
7365 RedrawingDisabled = 0;
7366
7367 /*
7368 * Call the main loop until <CR> or CTRL-C is typed.
7369 */
7370 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007371 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372
7373 RedrawingDisabled = i;
7374
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007375# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007376 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007377# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007378
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007380 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007381
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007382# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007383 /* Restore KeyTyped in case it is modified by autocommands */
7384 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385# endif
7386
Bram Moolenaarccc18222007-05-10 18:25:20 +00007387 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007388 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 cmdwin_type = 0;
7390
7391 exmode_active = save_exmode;
7392
Bram Moolenaarf9821062008-06-20 16:31:07 +00007393 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007395 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 {
7397 cmdwin_result = Ctrl_C;
7398 EMSG(_("E199: Active window or buffer deleted"));
7399 }
7400 else
7401 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007402# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 /* autocmds may abort script processing */
7404 if (aborting() && cmdwin_result != K_IGNORE)
7405 cmdwin_result = Ctrl_C;
7406# endif
7407 /* Set the new command line from the cmdline buffer. */
7408 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007409 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007411 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7412
7413 if (histtype == HIST_CMD)
7414 {
7415 /* Execute the command directly. */
7416 ccline.cmdbuff = vim_strsave((char_u *)p);
7417 cmdwin_result = CAR;
7418 }
7419 else
7420 {
7421 /* First need to cancel what we were doing. */
7422 ccline.cmdbuff = NULL;
7423 stuffcharReadbuff(':');
7424 stuffReadbuff((char_u *)p);
7425 stuffcharReadbuff(CAR);
7426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 }
7428 else if (cmdwin_result == K_XF2) /* :qa typed */
7429 {
7430 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7431 cmdwin_result = CAR;
7432 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007433 else if (cmdwin_result == Ctrl_C)
7434 {
7435 /* :q or :close, don't execute any command
7436 * and don't modify the cmd window. */
7437 ccline.cmdbuff = NULL;
7438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 else
7440 ccline.cmdbuff = vim_strsave(ml_get_curline());
7441 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007442 {
7443 ccline.cmdbuff = vim_strsave((char_u *)"");
7444 ccline.cmdlen = 0;
7445 ccline.cmdbufflen = 1;
7446 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007447 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 else
7450 {
7451 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7452 ccline.cmdbufflen = ccline.cmdlen + 1;
7453 ccline.cmdpos = curwin->w_cursor.col;
7454 if (ccline.cmdpos > ccline.cmdlen)
7455 ccline.cmdpos = ccline.cmdlen;
7456 if (cmdwin_result == K_IGNORE)
7457 {
7458 set_cmdspos_cursor();
7459 redrawcmd();
7460 }
7461 }
7462
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007464 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007465# ifdef FEAT_CONCEAL
7466 /* Avoid command-line window first character being concealed. */
7467 curwin->w_p_cole = 0;
7468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007469 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007470 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 win_goto(old_curwin);
7472 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007473
7474 /* win_close() may have already wiped the buffer when 'bh' is
7475 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007476 if (bufref_valid(&bufref))
7477 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478
7479 /* Restore window sizes. */
7480 win_size_restore(&winsizes);
7481
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007482 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 }
7484
7485 ga_clear(&winsizes);
7486 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007487# ifdef FEAT_RIGHTLEFT
7488 cmdmsg_rl = save_cmdmsg_rl;
7489# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490
7491 State = save_State;
7492# ifdef FEAT_MOUSE
7493 setmouse();
7494# endif
7495
7496 return cmdwin_result;
7497}
7498#endif /* FEAT_CMDWIN */
7499
7500/*
7501 * Used for commands that either take a simple command string argument, or:
7502 * cmd << endmarker
7503 * {script}
7504 * endmarker
7505 * Returns a pointer to allocated memory with {script} or NULL.
7506 */
7507 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007508script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007509{
7510 char_u *theline;
7511 char *end_pattern = NULL;
7512 char dot[] = ".";
7513 garray_T ga;
7514
7515 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7516 return NULL;
7517
7518 ga_init2(&ga, 1, 0x400);
7519
7520 if (cmd[2] != NUL)
7521 end_pattern = (char *)skipwhite(cmd + 2);
7522 else
7523 end_pattern = dot;
7524
7525 for (;;)
7526 {
7527 theline = eap->getline(
7528#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007529 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530#endif
7531 NUL, eap->cookie, 0);
7532
7533 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007534 {
7535 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
7539 ga_concat(&ga, theline);
7540 ga_append(&ga, '\n');
7541 vim_free(theline);
7542 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007543 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007544
7545 return (char_u *)ga.ga_data;
7546}