blob: c2c74534b072449ac22956a3fef40e240d4636a4 [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;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200391 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200392 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 */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200448 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200449 if (call_update_screen)
450 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200451 }
452}
453
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454/*
455 * Do 'incsearch' highlighting if desired.
456 */
457 static void
458may_do_incsearch_highlighting(
459 int firstc,
460 long count,
461 incsearch_state_T *is_state)
462{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200463 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200464 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465 pos_T end_pos;
466 struct cmdline_info save_ccline;
467#ifdef FEAT_RELTIME
468 proftime_T tm;
469#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200470 int next_char;
471 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472
Bram Moolenaar198cb662018-09-06 21:44:17 +0200473 // Parsing range may already set the last search pattern.
474 save_last_search_pattern();
475
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200476 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200477 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200478 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200479 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200480 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200481 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200482
483 // If there is a character waiting, search and redraw later.
484 if (char_avail())
485 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200486 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200487 is_state->incsearch_postponed = TRUE;
488 return;
489 }
490 is_state->incsearch_postponed = FALSE;
491
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200492 if (search_first_line == 0)
493 // start at the original cursor position
494 curwin->w_cursor = is_state->search_start;
495 else
496 {
497 // start at the first line in the range
498 curwin->w_cursor.lnum = search_first_line;
499 curwin->w_cursor.col = 0;
500 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200501
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200502 // Use the previous pattern for ":s//".
503 next_char = ccline.cmdbuff[skiplen + patlen];
504 use_last_pat = patlen == 0 && skiplen > 0
505 && ccline.cmdbuff[skiplen - 1] == next_char;
506
507 // If there is no pattern, don't do anything.
508 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200509 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200510 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200511 set_no_hlsearch(TRUE); // turn off previous highlight
512 redraw_all_later(SOME_VALID);
513 }
514 else
515 {
516 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
517
518 cursor_off(); // so the user knows we're busy
519 out_flush();
520 ++emsg_off; // so it doesn't beep if bad expr
521#ifdef FEAT_RELTIME
522 // Set the time limit to half a second.
523 profile_setlimit(500L, &tm);
524#endif
525 if (!p_hls)
526 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200527 if (search_first_line != 0)
528 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200529 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200530 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200531 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200532#ifdef FEAT_RELTIME
533 &tm, NULL
534#else
535 NULL, NULL
536#endif
537 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200538 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 --emsg_off;
540
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200541 if (curwin->w_cursor.lnum < search_first_line
542 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200543 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200544 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200545 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200546 curwin->w_cursor = is_state->search_start;
547 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200548
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 // if interrupted while searching, behave like it failed
550 if (got_int)
551 {
552 (void)vpeekc(); // remove <C-C> from input stream
553 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200554 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200555 }
556 else if (char_avail())
557 // cancelled searching because a char was typed
558 is_state->incsearch_postponed = TRUE;
559 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200560 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200561 highlight_match = TRUE; // highlight position
562 else
563 highlight_match = FALSE; // remove highlight
564
565 // First restore the old curwin values, so the screen is positioned in the
566 // same way as the actual search command.
567 restore_viewstate(&is_state->old_viewstate);
568 changed_cline_bef_curs();
569 update_topline();
570
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200571 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200572 {
573 pos_T save_pos = curwin->w_cursor;
574
575 is_state->match_start = curwin->w_cursor;
576 set_search_match(&curwin->w_cursor);
577 validate_cursor();
578 end_pos = curwin->w_cursor;
579 is_state->match_end = end_pos;
580 curwin->w_cursor = save_pos;
581 }
582 else
583 end_pos = curwin->w_cursor; // shutup gcc 4
584
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200585 // Disable 'hlsearch' highlighting if the pattern matches everything.
586 // Avoids a flash when typing "foo\|".
587 if (!use_last_pat)
588 {
589 next_char = ccline.cmdbuff[skiplen + patlen];
590 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200591 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
592 {
593 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200594 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200595 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200596 ccline.cmdbuff[skiplen + patlen] = next_char;
597 }
598
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200599 validate_cursor();
600 // May redraw the status line to show the cursor position.
601 if (p_ru && curwin->w_status_height > 0)
602 curwin->w_redr_status = TRUE;
603
604 save_cmdline(&save_ccline);
605 update_screen(SOME_VALID);
606 restore_cmdline(&save_ccline);
607 restore_last_search_pattern();
608
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200609 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
610 // end of the pattern, e.g. for ":s/pat/".
611 if (ccline.cmdbuff[skiplen + patlen] != NUL)
612 curwin->w_cursor = is_state->search_start;
613 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 curwin->w_cursor = end_pos;
615
616 msg_starthere();
617 redrawcmdline();
618 is_state->did_incsearch = TRUE;
619}
620
621/*
622 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
623 * or previous match.
624 * Returns FAIL when jumping to cmdline_not_changed;
625 */
626 static int
627may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int firstc,
629 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200631 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200632{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200633 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200634 pos_T t;
635 char_u *pat;
636 int search_flags = SEARCH_NOOF;
637 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200638 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200639
Bram Moolenaar198cb662018-09-06 21:44:17 +0200640 // Parsing range may already set the last search pattern.
641 save_last_search_pattern();
642
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200643 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200644 {
645 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200646 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200647 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200648 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200649 {
650 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200652 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200654 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200655 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200656 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200657 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200658 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200659 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200661 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200662
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200663 cursor_off();
664 out_flush();
665 if (c == Ctrl_G)
666 {
667 t = is_state->match_end;
668 if (LT_POS(is_state->match_start, is_state->match_end))
669 // Start searching at the end of the match not at the beginning of
670 // the next column.
671 (void)decl(&t);
672 search_flags += SEARCH_COL;
673 }
674 else
675 t = is_state->match_start;
676 if (!p_hls)
677 search_flags += SEARCH_KEEP;
678 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200679 save = pat[patlen];
680 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200681 i = searchit(curwin, curbuf, &t,
682 c == Ctrl_G ? FORWARD : BACKWARD,
683 pat, count, search_flags,
684 RE_SEARCH, 0, NULL, NULL);
685 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200686 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200687 if (i)
688 {
689 is_state->search_start = is_state->match_start;
690 is_state->match_end = t;
691 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200692 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200693 {
694 // Move just before the current match, so that when nv_search
695 // finishes the cursor will be put back on the match.
696 is_state->search_start = t;
697 (void)decl(&is_state->search_start);
698 }
699 else if (c == Ctrl_G && firstc == '?')
700 {
701 // Move just after the current match, so that when nv_search
702 // finishes the cursor will be put back on the match.
703 is_state->search_start = t;
704 (void)incl(&is_state->search_start);
705 }
706 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
707 {
708 // wrap around
709 is_state->search_start = t;
710 if (firstc == '?')
711 (void)incl(&is_state->search_start);
712 else
713 (void)decl(&is_state->search_start);
714 }
715
716 set_search_match(&is_state->match_end);
717 curwin->w_cursor = is_state->match_start;
718 changed_cline_bef_curs();
719 update_topline();
720 validate_cursor();
721 highlight_match = TRUE;
722 save_viewstate(&is_state->old_viewstate);
723 update_screen(NOT_VALID);
724 redrawcmdline();
725 }
726 else
727 vim_beep(BO_ERROR);
728 restore_last_search_pattern();
729 return FAIL;
730}
731
732/*
733 * When CTRL-L typed: add character from the match to the pattern.
734 * May set "*c" to the added character.
735 * Return OK when jumping to cmdline_not_changed.
736 */
737 static int
738may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
739{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200740 int skiplen, patlen;
741
Bram Moolenaar198cb662018-09-06 21:44:17 +0200742 // Parsing range may already set the last search pattern.
743 save_last_search_pattern();
744
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200745 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200746 {
747 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200748 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200749 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200750
751 // Add a character from under the cursor for 'incsearch'.
752 if (is_state->did_incsearch)
753 {
754 curwin->w_cursor = is_state->match_end;
755 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
756 {
757 *c = gchar_cursor();
758
759 // If 'ignorecase' and 'smartcase' are set and the
760 // command line has no uppercase characters, convert
761 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200762 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200763 *c = MB_TOLOWER(*c);
764 if (*c != NUL)
765 {
766 if (*c == firstc || vim_strchr((char_u *)(
767 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
768 {
769 // put a backslash before special characters
770 stuffcharReadbuff(*c);
771 *c = '\\';
772 }
773 return FAIL;
774 }
775 }
776 }
777 return OK;
778}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200779#endif
780
Bram Moolenaar66216052017-12-16 16:33:44 +0100781/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 * getcmdline() - accept a command line starting with firstc.
783 *
784 * firstc == ':' get ":" command line.
785 * firstc == '/' or '?' get search pattern
786 * firstc == '=' get expression
787 * firstc == '@' get text for input() function
788 * firstc == '>' get text for debug mode
789 * firstc == NUL get text for :insert command
790 * firstc == -1 like NUL, and break on CTRL-C
791 *
792 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
793 * command line.
794 *
795 * Careful: getcmdline() can be called recursively!
796 *
797 * Return pointer to allocated string if there is a commandline, NULL
798 * otherwise.
799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100801getcmdline(
802 int firstc,
803 long count UNUSED, /* only used for incremental search */
804 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805{
806 int c;
807 int i;
808 int j;
809 int gotesc = FALSE; /* TRUE when <ESC> just typed */
810 int do_abbr; /* when TRUE check for abbr. */
811#ifdef FEAT_CMDHIST
812 char_u *lookfor = NULL; /* string to match */
813 int hiscnt; /* current history line in use */
814 int histype; /* history type to be used */
815#endif
816#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200817 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818#endif
819 int did_wild_list = FALSE; /* did wild_list() recently */
820 int wim_index = 0; /* index in wim_flags[] */
821 int res;
822 int save_msg_scroll = msg_scroll;
823 int save_State = State; /* remember State when called */
824 int some_key_typed = FALSE; /* one of the keys was typed */
825#ifdef FEAT_MOUSE
826 /* mouse drag and release events are ignored, unless they are
827 * preceded with a mouse down event */
828 int ignore_drag_release = TRUE;
829#endif
830#ifdef FEAT_EVAL
831 int break_ctrl_c = FALSE;
832#endif
833 expand_T xpc;
834 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200835#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000836 /* Everything that may work recursively should save and restore the
837 * current command line in save_ccline. That includes update_screen(), a
838 * custom status line may invoke ":normal". */
839 struct cmdline_info save_ccline;
840#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200841 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843#ifdef FEAT_EVAL
844 if (firstc == -1)
845 {
846 firstc = NUL;
847 break_ctrl_c = TRUE;
848 }
849#endif
850#ifdef FEAT_RIGHTLEFT
851 /* start without Hebrew mapping for a command line */
852 if (firstc == ':' || firstc == '=' || firstc == '>')
853 cmd_hkmap = 0;
854#endif
855
856 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200857
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200859 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860#endif
861
862 /*
863 * set some variables for redrawcmd()
864 */
865 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000866 ccline.cmdindent = (firstc > 0 ? indent : 0);
867
868 /* alloc initial ccline.cmdbuff */
869 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (ccline.cmdbuff == NULL)
871 return NULL; /* out of memory */
872 ccline.cmdlen = ccline.cmdpos = 0;
873 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100874 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000876 /* autoindent for :insert and :append */
877 if (firstc <= 0)
878 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200879 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000880 ccline.cmdbuff[indent] = NUL;
881 ccline.cmdpos = indent;
882 ccline.cmdspos = indent;
883 ccline.cmdlen = indent;
884 }
885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000887 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888
889#ifdef FEAT_RIGHTLEFT
890 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
891 && (firstc == '/' || firstc == '?'))
892 cmdmsg_rl = TRUE;
893 else
894 cmdmsg_rl = FALSE;
895#endif
896
897 redir_off = TRUE; /* don't redirect the typed command */
898 if (!cmd_silent)
899 {
900 i = msg_scrolled;
901 msg_scrolled = 0; /* avoid wait_return message */
902 gotocmdline(TRUE);
903 msg_scrolled += i;
904 redrawcmdprompt(); /* draw prompt or indent */
905 set_cmdspos();
906 }
907 xpc.xp_context = EXPAND_NOTHING;
908 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000909#ifndef BACKSLASH_IN_FILENAME
910 xpc.xp_shell = FALSE;
911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000913#if defined(FEAT_EVAL)
914 if (ccline.input_fn)
915 {
916 xpc.xp_context = ccline.xp_context;
917 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000918# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000919 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000920# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000921 }
922#endif
923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 /*
925 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
926 * doing ":@0" when register 0 doesn't contain a CR.
927 */
928 msg_scroll = FALSE;
929
930 State = CMDLINE;
931
932 if (firstc == '/' || firstc == '?' || firstc == '@')
933 {
934 /* Use ":lmap" mappings for search pattern and input(). */
935 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
936 b_im_ptr = &curbuf->b_p_iminsert;
937 else
938 b_im_ptr = &curbuf->b_p_imsearch;
939 if (*b_im_ptr == B_IMODE_LMAP)
940 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100941#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 im_set_active(*b_im_ptr == B_IMODE_IM);
943#endif
944 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100945#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 else if (p_imcmdline)
947 im_set_active(TRUE);
948#endif
949
950#ifdef FEAT_MOUSE
951 setmouse();
952#endif
953#ifdef CURSOR_SHAPE
954 ui_cursor_shape(); /* may show different cursor shape */
955#endif
956
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000957 /* When inside an autocommand for writing "exiting" may be set and
958 * terminal mode set to cooked. Need to set raw mode here then. */
959 settmode(TMODE_RAW);
960
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200961 /* Trigger CmdlineEnter autocommands. */
962 cmdline_type = firstc == NUL ? '-' : firstc;
963 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200964
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965#ifdef FEAT_CMDHIST
966 init_history();
967 hiscnt = hislen; /* set hiscnt to impossible history value */
968 histype = hist_char2type(firstc);
969#endif
970
971#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000972 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973#endif
974
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200975 /* If something above caused an error, reset the flags, we do want to type
976 * and execute commands. Display may be messed up a bit. */
977 if (did_emsg)
978 redrawcmd();
979 did_emsg = FALSE;
980 got_int = FALSE;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982 /*
983 * Collect the command string, handling editing keys.
984 */
985 for (;;)
986 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000987 redir_off = TRUE; /* Don't redirect the typed command.
988 Repeated, because a ":redir" inside
989 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990#ifdef USE_ON_FLY_SCROLL
991 dont_scroll = FALSE; /* allow scrolling here */
992#endif
993 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
994
Bram Moolenaar72532d32018-04-07 19:09:09 +0200995 did_emsg = FALSE; /* There can't really be a reason why an error
996 that occurs while typing a command should
997 cause the command not to be executed. */
998
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001000
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001001 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1002 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001003 do
1004 {
1005 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001006 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001007
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 if (KeyTyped)
1009 {
1010 some_key_typed = TRUE;
1011#ifdef FEAT_RIGHTLEFT
1012 if (cmd_hkmap)
1013 c = hkmap(c);
1014# ifdef FEAT_FKMAP
1015 if (cmd_fkmap)
1016 c = cmdl_fkmap(c);
1017# endif
1018 if (cmdmsg_rl && !KeyStuffed)
1019 {
1020 /* Invert horizontal movements and operations. Only when
1021 * typed by the user directly, not when the result of a
1022 * mapping. */
1023 switch (c)
1024 {
1025 case K_RIGHT: c = K_LEFT; break;
1026 case K_S_RIGHT: c = K_S_LEFT; break;
1027 case K_C_RIGHT: c = K_C_LEFT; break;
1028 case K_LEFT: c = K_RIGHT; break;
1029 case K_S_LEFT: c = K_S_RIGHT; break;
1030 case K_C_LEFT: c = K_C_RIGHT; break;
1031 }
1032 }
1033#endif
1034 }
1035
1036 /*
1037 * Ignore got_int when CTRL-C was typed here.
1038 * Don't ignore it in :global, we really need to break then, e.g., for
1039 * ":g/pat/normal /pat" (without the <CR>).
1040 * Don't ignore it for the input() function.
1041 */
1042 if ((c == Ctrl_C
1043#ifdef UNIX
1044 || c == intr_char
1045#endif
1046 )
1047#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1048 && firstc != '@'
1049#endif
1050#ifdef FEAT_EVAL
1051 && !break_ctrl_c
1052#endif
1053 && !global_busy)
1054 got_int = FALSE;
1055
1056#ifdef FEAT_CMDHIST
1057 /* free old command line when finished moving around in the history
1058 * list */
1059 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001060 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001061 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 && c != K_PAGEDOWN && c != K_PAGEUP
1063 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001064 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001066 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#endif
1068
1069 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001070 * When there are matching completions to select <S-Tab> works like
1071 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001073 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 c = Ctrl_P;
1075
1076#ifdef FEAT_WILDMENU
1077 /* Special translations for 'wildmenu' */
1078 if (did_wild_list && p_wmnu)
1079 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001080 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001082 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 c = Ctrl_N;
1084 }
1085 /* Hitting CR after "emenu Name.": complete submenu */
1086 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1087 && ccline.cmdpos > 1
1088 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1089 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1090 && (c == '\n' || c == '\r' || c == K_KENTER))
1091 c = K_DOWN;
1092#endif
1093
1094 /* free expanded names when finished walking through matches */
1095 if (xpc.xp_numfiles != -1
1096 && !(c == p_wc && KeyTyped) && c != p_wcm
1097 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1098 && c != Ctrl_L)
1099 {
1100 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1101 did_wild_list = FALSE;
1102#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001103 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104#endif
1105 xpc.xp_context = EXPAND_NOTHING;
1106 wim_index = 0;
1107#ifdef FEAT_WILDMENU
1108 if (p_wmnu && wild_menu_showing != 0)
1109 {
1110 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001111 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001112
1113 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001114 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
1116 if (wild_menu_showing == WM_SCROLLED)
1117 {
1118 /* Entered command line, move it up */
1119 cmdline_row--;
1120 redrawcmd();
1121 }
1122 else if (save_p_ls != -1)
1123 {
1124 /* restore 'laststatus' and 'winminheight' */
1125 p_ls = save_p_ls;
1126 p_wmh = save_p_wmh;
1127 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001128 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001130 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 redrawcmd();
1132 save_p_ls = -1;
1133 }
1134 else
1135 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 redraw_statuslines();
1138 }
1139 KeyTyped = skt;
1140 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001141 if (ccline.input_fn)
1142 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 }
1144#endif
1145 }
1146
1147#ifdef FEAT_WILDMENU
1148 /* Special translations for 'wildmenu' */
1149 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1150 {
1151 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001152 if (c == K_DOWN && ccline.cmdpos > 0
1153 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001155 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 {
1157 /* Hitting <Up>: Remove one submenu name in front of the
1158 * cursor */
1159 int found = FALSE;
1160
1161 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1162 i = 0;
1163 while (--j > 0)
1164 {
1165 /* check for start of menu name */
1166 if (ccline.cmdbuff[j] == ' '
1167 && ccline.cmdbuff[j - 1] != '\\')
1168 {
1169 i = j + 1;
1170 break;
1171 }
1172 /* check for start of submenu name */
1173 if (ccline.cmdbuff[j] == '.'
1174 && ccline.cmdbuff[j - 1] != '\\')
1175 {
1176 if (found)
1177 {
1178 i = j + 1;
1179 break;
1180 }
1181 else
1182 found = TRUE;
1183 }
1184 }
1185 if (i > 0)
1186 cmdline_del(i);
1187 c = p_wc;
1188 xpc.xp_context = EXPAND_NOTHING;
1189 }
1190 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001191 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001192 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001193 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 {
1195 char_u upseg[5];
1196
1197 upseg[0] = PATHSEP;
1198 upseg[1] = '.';
1199 upseg[2] = '.';
1200 upseg[3] = PATHSEP;
1201 upseg[4] = NUL;
1202
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001203 if (c == K_DOWN
1204 && ccline.cmdpos > 0
1205 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1206 && (ccline.cmdpos < 3
1207 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1209 {
1210 /* go down a directory */
1211 c = p_wc;
1212 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001213 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 {
1215 /* If in a direct ancestor, strip off one ../ to go down */
1216 int found = FALSE;
1217
1218 j = ccline.cmdpos;
1219 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1220 while (--j > i)
1221 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001222#ifdef FEAT_MBYTE
1223 if (has_mbyte)
1224 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 if (vim_ispathsep(ccline.cmdbuff[j]))
1227 {
1228 found = TRUE;
1229 break;
1230 }
1231 }
1232 if (found
1233 && ccline.cmdbuff[j - 1] == '.'
1234 && ccline.cmdbuff[j - 2] == '.'
1235 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1236 {
1237 cmdline_del(j - 2);
1238 c = p_wc;
1239 }
1240 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001241 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 /* go up a directory */
1244 int found = FALSE;
1245
1246 j = ccline.cmdpos - 1;
1247 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1248 while (--j > i)
1249 {
1250#ifdef FEAT_MBYTE
1251 if (has_mbyte)
1252 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1253#endif
1254 if (vim_ispathsep(ccline.cmdbuff[j])
1255#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001256 && vim_strchr((char_u *)" *?[{`$%#",
1257 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#endif
1259 )
1260 {
1261 if (found)
1262 {
1263 i = j + 1;
1264 break;
1265 }
1266 else
1267 found = TRUE;
1268 }
1269 }
1270
1271 if (!found)
1272 j = i;
1273 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1274 j += 4;
1275 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1276 && j == i)
1277 j += 3;
1278 else
1279 j = 0;
1280 if (j > 0)
1281 {
1282 /* TODO this is only for DOS/UNIX systems - need to put in
1283 * machine-specific stuff here and in upseg init */
1284 cmdline_del(j);
1285 put_on_cmdline(upseg + 1, 3, FALSE);
1286 }
1287 else if (ccline.cmdpos > i)
1288 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001289
1290 /* Now complete in the new directory. Set KeyTyped in case the
1291 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001293 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 }
1295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296
1297#endif /* FEAT_WILDMENU */
1298
1299 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1300 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1301 if (c == Ctrl_BSL)
1302 {
1303 ++no_mapping;
1304 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001305 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 --no_mapping;
1307 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001308 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1309 * is in a mapping. */
1310 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1311 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 {
1313 vungetc(c);
1314 c = Ctrl_BSL;
1315 }
1316#ifdef FEAT_EVAL
1317 else if (c == 'e')
1318 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001319 char_u *p = NULL;
1320 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321
1322 /*
1323 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001324 * Need to save and restore the current command line, to be
1325 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 */
1327 if (ccline.cmdpos == ccline.cmdlen)
1328 new_cmdpos = 99999; /* keep it at the end */
1329 else
1330 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001331
1332 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001334 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (c == '=')
1336 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001337 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001338 * to avoid nasty things like going to another buffer when
1339 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001340 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001341 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001343 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001344 restore_cmdline(&save_ccline);
1345
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001346 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001348 len = (int)STRLEN(p);
1349 if (realloc_cmdbuff(len + 1) == OK)
1350 {
1351 ccline.cmdlen = len;
1352 STRCPY(ccline.cmdbuff, p);
1353 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001355 /* Restore the cursor or use the position set with
1356 * set_cmdline_pos(). */
1357 if (new_cmdpos > ccline.cmdlen)
1358 ccline.cmdpos = ccline.cmdlen;
1359 else
1360 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001362 KeyTyped = FALSE; /* Don't do p_wc completion. */
1363 redrawcmd();
1364 goto cmdline_changed;
1365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367 }
1368 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001369 got_int = FALSE; /* don't abandon the command line */
1370 did_emsg = FALSE;
1371 emsg_on_display = FALSE;
1372 redrawcmd();
1373 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375#endif
1376 else
1377 {
1378 if (c == Ctrl_G && p_im && restart_edit == 0)
1379 restart_edit = 'a';
1380 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1381 in history */
1382 goto returncmd; /* back to Normal mode */
1383 }
1384 }
1385
1386#ifdef FEAT_CMDWIN
1387 if (c == cedit_key || c == K_CMDWIN)
1388 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001389 if (ex_normal_busy == 0 && got_int == FALSE)
1390 {
1391 /*
1392 * Open a window to edit the command line (and history).
1393 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001394 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001395 some_key_typed = TRUE;
1396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 }
1398# ifdef FEAT_DIGRAPHS
1399 else
1400# endif
1401#endif
1402#ifdef FEAT_DIGRAPHS
1403 c = do_digraph(c);
1404#endif
1405
1406 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1407 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1408 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001409 /* In Ex mode a backslash escapes a newline. */
1410 if (exmode_active
1411 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001412 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001413 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001414 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001416 if (c == K_KENTER)
1417 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001419 else
1420 {
1421 gotesc = FALSE; /* Might have typed ESC previously, don't
1422 truncate the cmdline now. */
1423 if (ccheck_abbr(c + ABBR_OFF))
1424 goto cmdline_changed;
1425 if (!cmd_silent)
1426 {
1427 windgoto(msg_row, 0);
1428 out_flush();
1429 }
1430 break;
1431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
1433
1434 /*
1435 * Completion for 'wildchar' or 'wildcharm' key.
1436 * - hitting <ESC> twice means: abandon command line.
1437 * - wildcard expansion is only done when the 'wildchar' key is really
1438 * typed, not when it comes from a macro
1439 */
1440 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1441 {
1442 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1443 {
1444 /* if 'wildmode' contains "list" may still need to list */
1445 if (xpc.xp_numfiles > 1
1446 && !did_wild_list
1447 && (wim_flags[wim_index] & WIM_LIST))
1448 {
1449 (void)showmatches(&xpc, FALSE);
1450 redrawcmd();
1451 did_wild_list = TRUE;
1452 }
1453 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001454 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1455 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001457 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1458 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459 else
1460 res = OK; /* don't insert 'wildchar' now */
1461 }
1462 else /* typed p_wc first time */
1463 {
1464 wim_index = 0;
1465 j = ccline.cmdpos;
1466 /* if 'wildmode' first contains "longest", get longest
1467 * common part */
1468 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001469 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1470 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001471 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001472 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1473 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474
1475 /* if interrupted while completing, behave like it failed */
1476 if (got_int)
1477 {
1478 (void)vpeekc(); /* remove <C-C> from input stream */
1479 got_int = FALSE; /* don't abandon the command line */
1480 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1481#ifdef FEAT_WILDMENU
1482 xpc.xp_context = EXPAND_NOTHING;
1483#endif
1484 goto cmdline_changed;
1485 }
1486
1487 /* when more than one match, and 'wildmode' first contains
1488 * "list", or no change and 'wildmode' contains "longest,list",
1489 * list all matches */
1490 if (res == OK && xpc.xp_numfiles > 1)
1491 {
1492 /* a "longest" that didn't do anything is skipped (but not
1493 * "list:longest") */
1494 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1495 wim_index = 1;
1496 if ((wim_flags[wim_index] & WIM_LIST)
1497#ifdef FEAT_WILDMENU
1498 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1499#endif
1500 )
1501 {
1502 if (!(wim_flags[0] & WIM_LONGEST))
1503 {
1504#ifdef FEAT_WILDMENU
1505 int p_wmnu_save = p_wmnu;
1506 p_wmnu = 0;
1507#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001508 /* remove match */
1509 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510#ifdef FEAT_WILDMENU
1511 p_wmnu = p_wmnu_save;
1512#endif
1513 }
1514#ifdef FEAT_WILDMENU
1515 (void)showmatches(&xpc, p_wmnu
1516 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1517#else
1518 (void)showmatches(&xpc, FALSE);
1519#endif
1520 redrawcmd();
1521 did_wild_list = TRUE;
1522 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001523 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1524 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001526 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1527 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 }
1529 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001530 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 }
1532#ifdef FEAT_WILDMENU
1533 else if (xpc.xp_numfiles == -1)
1534 xpc.xp_context = EXPAND_NOTHING;
1535#endif
1536 }
1537 if (wim_index < 3)
1538 ++wim_index;
1539 if (c == ESC)
1540 gotesc = TRUE;
1541 if (res == OK)
1542 goto cmdline_changed;
1543 }
1544
1545 gotesc = FALSE;
1546
1547 /* <S-Tab> goes to last match, in a clumsy way */
1548 if (c == K_S_TAB && KeyTyped)
1549 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001550 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1551 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1552 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 goto cmdline_changed;
1554 }
1555
1556 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1557 c = NL;
1558
1559 do_abbr = TRUE; /* default: check for abbreviation */
1560
1561 /*
1562 * Big switch for a typed command line character.
1563 */
1564 switch (c)
1565 {
1566 case K_BS:
1567 case Ctrl_H:
1568 case K_DEL:
1569 case K_KDEL:
1570 case Ctrl_W:
1571#ifdef FEAT_FKMAP
1572 if (cmd_fkmap && c == K_BS)
1573 c = K_DEL;
1574#endif
1575 if (c == K_KDEL)
1576 c = K_DEL;
1577
1578 /*
1579 * delete current character is the same as backspace on next
1580 * character, except at end of line
1581 */
1582 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1583 ++ccline.cmdpos;
1584#ifdef FEAT_MBYTE
1585 if (has_mbyte && c == K_DEL)
1586 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1587 ccline.cmdbuff + ccline.cmdpos);
1588#endif
1589 if (ccline.cmdpos > 0)
1590 {
1591 char_u *p;
1592
1593 j = ccline.cmdpos;
1594 p = ccline.cmdbuff + j;
1595#ifdef FEAT_MBYTE
1596 if (has_mbyte)
1597 {
1598 p = mb_prevptr(ccline.cmdbuff, p);
1599 if (c == Ctrl_W)
1600 {
1601 while (p > ccline.cmdbuff && vim_isspace(*p))
1602 p = mb_prevptr(ccline.cmdbuff, p);
1603 i = mb_get_class(p);
1604 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1605 p = mb_prevptr(ccline.cmdbuff, p);
1606 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001607 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610 else
1611#endif
1612 if (c == Ctrl_W)
1613 {
1614 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1615 --p;
1616 i = vim_iswordc(p[-1]);
1617 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1618 && vim_iswordc(p[-1]) == i)
1619 --p;
1620 }
1621 else
1622 --p;
1623 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1624 ccline.cmdlen -= j - ccline.cmdpos;
1625 i = ccline.cmdpos;
1626 while (i < ccline.cmdlen)
1627 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1628
1629 /* Truncate at the end, required for multi-byte chars. */
1630 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001631#ifdef FEAT_SEARCH_EXTRA
1632 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001633 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001634 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001635 /* save view settings, so that the screen
1636 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001637 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001638 }
1639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 redrawcmd();
1641 }
1642 else if (ccline.cmdlen == 0 && c != Ctrl_W
1643 && ccline.cmdprompt == NULL && indent == 0)
1644 {
1645 /* In ex and debug mode it doesn't make sense to return. */
1646 if (exmode_active
1647#ifdef FEAT_EVAL
1648 || ccline.cmdfirstc == '>'
1649#endif
1650 )
1651 goto cmdline_not_changed;
1652
Bram Moolenaard23a8232018-02-10 18:45:26 +01001653 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (!cmd_silent)
1655 {
1656#ifdef FEAT_RIGHTLEFT
1657 if (cmdmsg_rl)
1658 msg_col = Columns;
1659 else
1660#endif
1661 msg_col = 0;
1662 msg_putchar(' '); /* delete ':' */
1663 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001664#ifdef FEAT_SEARCH_EXTRA
1665 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001666 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 redraw_cmdline = TRUE;
1669 goto returncmd; /* back to cmd mode */
1670 }
1671 goto cmdline_changed;
1672
1673 case K_INS:
1674 case K_KINS:
1675#ifdef FEAT_FKMAP
1676 /* if Farsi mode set, we are in reverse insert mode -
1677 Do not change the mode */
1678 if (cmd_fkmap)
1679 beep_flush();
1680 else
1681#endif
1682 ccline.overstrike = !ccline.overstrike;
1683#ifdef CURSOR_SHAPE
1684 ui_cursor_shape(); /* may show different cursor shape */
1685#endif
1686 goto cmdline_not_changed;
1687
1688 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001689 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 {
1691 /* ":lmap" mappings exists, toggle use of mappings. */
1692 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001693#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694 im_set_active(FALSE); /* Disable input method */
1695#endif
1696 if (b_im_ptr != NULL)
1697 {
1698 if (State & LANGMAP)
1699 *b_im_ptr = B_IMODE_LMAP;
1700 else
1701 *b_im_ptr = B_IMODE_NONE;
1702 }
1703 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001704#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 else
1706 {
1707 /* There are no ":lmap" mappings, toggle IM. When
1708 * 'imdisable' is set don't try getting the status, it's
1709 * always off. */
1710 if ((p_imdisable && b_im_ptr != NULL)
1711 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1712 {
1713 im_set_active(FALSE); /* Disable input method */
1714 if (b_im_ptr != NULL)
1715 *b_im_ptr = B_IMODE_NONE;
1716 }
1717 else
1718 {
1719 im_set_active(TRUE); /* Enable input method */
1720 if (b_im_ptr != NULL)
1721 *b_im_ptr = B_IMODE_IM;
1722 }
1723 }
1724#endif
1725 if (b_im_ptr != NULL)
1726 {
1727 if (b_im_ptr == &curbuf->b_p_iminsert)
1728 set_iminsert_global();
1729 else
1730 set_imsearch_global();
1731 }
1732#ifdef CURSOR_SHAPE
1733 ui_cursor_shape(); /* may show different cursor shape */
1734#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001735#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 /* Show/unshow value of 'keymap' in status lines later. */
1737 status_redraw_curbuf();
1738#endif
1739 goto cmdline_not_changed;
1740
1741/* case '@': only in very old vi */
1742 case Ctrl_U:
1743 /* delete all characters left of the cursor */
1744 j = ccline.cmdpos;
1745 ccline.cmdlen -= j;
1746 i = ccline.cmdpos = 0;
1747 while (i < ccline.cmdlen)
1748 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1749 /* Truncate at the end, required for multi-byte chars. */
1750 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001751#ifdef FEAT_SEARCH_EXTRA
1752 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001753 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 redrawcmd();
1756 goto cmdline_changed;
1757
1758#ifdef FEAT_CLIPBOARD
1759 case Ctrl_Y:
1760 /* Copy the modeless selection, if there is one. */
1761 if (clip_star.state != SELECT_CLEARED)
1762 {
1763 if (clip_star.state == SELECT_DONE)
1764 clip_copy_modeless_selection(TRUE);
1765 goto cmdline_not_changed;
1766 }
1767 break;
1768#endif
1769
1770 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1771 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001772 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001773 * ":normal" runs out of characters. */
1774 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001775 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 goto cmdline_not_changed;
1777
1778 gotesc = TRUE; /* will free ccline.cmdbuff after
1779 putting it in history */
1780 goto returncmd; /* back to cmd mode */
1781
1782 case Ctrl_R: /* insert register */
1783#ifdef USE_ON_FLY_SCROLL
1784 dont_scroll = TRUE; /* disallow scrolling here */
1785#endif
1786 putcmdline('"', TRUE);
1787 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001788 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 if (i == Ctrl_O)
1790 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1791 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001792 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001793 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 --no_mapping;
1795#ifdef FEAT_EVAL
1796 /*
1797 * Insert the result of an expression.
1798 * Need to save the current command line, to be able to enter
1799 * a new one...
1800 */
1801 new_cmdpos = -1;
1802 if (c == '=')
1803 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1805 {
1806 beep_flush();
1807 c = ESC;
1808 }
1809 else
1810 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001811 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001813 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815 }
1816#endif
1817 if (c != ESC) /* use ESC to cancel inserting register */
1818 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001819 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001820
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001821#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001822 /* When there was a serious error abort getting the
1823 * command line. */
1824 if (aborting())
1825 {
1826 gotesc = TRUE; /* will free ccline.cmdbuff after
1827 putting it in history */
1828 goto returncmd; /* back to cmd mode */
1829 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 KeyTyped = FALSE; /* Don't do p_wc completion. */
1832#ifdef FEAT_EVAL
1833 if (new_cmdpos >= 0)
1834 {
1835 /* set_cmdline_pos() was used */
1836 if (new_cmdpos > ccline.cmdlen)
1837 ccline.cmdpos = ccline.cmdlen;
1838 else
1839 ccline.cmdpos = new_cmdpos;
1840 }
1841#endif
1842 }
1843 redrawcmd();
1844 goto cmdline_changed;
1845
1846 case Ctrl_D:
1847 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1848 break; /* Use ^D as normal char instead */
1849
1850 redrawcmd();
1851 continue; /* don't do incremental search now */
1852
1853 case K_RIGHT:
1854 case K_S_RIGHT:
1855 case K_C_RIGHT:
1856 do
1857 {
1858 if (ccline.cmdpos >= ccline.cmdlen)
1859 break;
1860 i = cmdline_charsize(ccline.cmdpos);
1861 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1862 break;
1863 ccline.cmdspos += i;
1864#ifdef FEAT_MBYTE
1865 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001866 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 + ccline.cmdpos);
1868 else
1869#endif
1870 ++ccline.cmdpos;
1871 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001872 while ((c == K_S_RIGHT || c == K_C_RIGHT
1873 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1875#ifdef FEAT_MBYTE
1876 if (has_mbyte)
1877 set_cmdspos_cursor();
1878#endif
1879 goto cmdline_not_changed;
1880
1881 case K_LEFT:
1882 case K_S_LEFT:
1883 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001884 if (ccline.cmdpos == 0)
1885 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 do
1887 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 --ccline.cmdpos;
1889#ifdef FEAT_MBYTE
1890 if (has_mbyte) /* move to first byte of char */
1891 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1892 ccline.cmdbuff + ccline.cmdpos);
1893#endif
1894 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1895 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001896 while (ccline.cmdpos > 0
1897 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001898 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1900#ifdef FEAT_MBYTE
1901 if (has_mbyte)
1902 set_cmdspos_cursor();
1903#endif
1904 goto cmdline_not_changed;
1905
1906 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001907 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001908 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaar4770d092006-01-12 23:22:24 +00001910#ifdef FEAT_GUI_W32
1911 /* On Win32 ignore <M-F4>, we get it when closing the window was
1912 * cancelled. */
1913 case K_F4:
1914 if (mod_mask == MOD_MASK_ALT)
1915 {
1916 redrawcmd(); /* somehow the cmdline is cleared */
1917 goto cmdline_not_changed;
1918 }
1919 break;
1920#endif
1921
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922#ifdef FEAT_MOUSE
1923 case K_MIDDLEDRAG:
1924 case K_MIDDLERELEASE:
1925 goto cmdline_not_changed; /* Ignore mouse */
1926
1927 case K_MIDDLEMOUSE:
1928# ifdef FEAT_GUI
1929 /* When GUI is active, also paste when 'mouse' is empty */
1930 if (!gui.in_use)
1931# endif
1932 if (!mouse_has(MOUSE_COMMAND))
1933 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001934# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001936 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001938# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001939 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 redrawcmd();
1941 goto cmdline_changed;
1942
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001943# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001945 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 redrawcmd();
1947 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001948# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949
1950 case K_LEFTDRAG:
1951 case K_LEFTRELEASE:
1952 case K_RIGHTDRAG:
1953 case K_RIGHTRELEASE:
1954 /* Ignore drag and release events when the button-down wasn't
1955 * seen before. */
1956 if (ignore_drag_release)
1957 goto cmdline_not_changed;
1958 /* FALLTHROUGH */
1959 case K_LEFTMOUSE:
1960 case K_RIGHTMOUSE:
1961 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1962 ignore_drag_release = TRUE;
1963 else
1964 ignore_drag_release = FALSE;
1965# ifdef FEAT_GUI
1966 /* When GUI is active, also move when 'mouse' is empty */
1967 if (!gui.in_use)
1968# endif
1969 if (!mouse_has(MOUSE_COMMAND))
1970 goto cmdline_not_changed; /* Ignore mouse */
1971# ifdef FEAT_CLIPBOARD
1972 if (mouse_row < cmdline_row && clip_star.available)
1973 {
1974 int button, is_click, is_drag;
1975
1976 /*
1977 * Handle modeless selection.
1978 */
1979 button = get_mouse_button(KEY2TERMCAP1(c),
1980 &is_click, &is_drag);
1981 if (mouse_model_popup() && button == MOUSE_LEFT
1982 && (mod_mask & MOD_MASK_SHIFT))
1983 {
1984 /* Translate shift-left to right button. */
1985 button = MOUSE_RIGHT;
1986 mod_mask &= ~MOD_MASK_SHIFT;
1987 }
1988 clip_modeless(button, is_click, is_drag);
1989 goto cmdline_not_changed;
1990 }
1991# endif
1992
1993 set_cmdspos();
1994 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1995 ++ccline.cmdpos)
1996 {
1997 i = cmdline_charsize(ccline.cmdpos);
1998 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1999 && mouse_col < ccline.cmdspos % Columns + i)
2000 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002001# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (has_mbyte)
2003 {
2004 /* Count ">" for double-wide char that doesn't fit. */
2005 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002006 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 + ccline.cmdpos) - 1;
2008 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002009# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 ccline.cmdspos += i;
2011 }
2012 goto cmdline_not_changed;
2013
2014 /* Mouse scroll wheel: ignored here */
2015 case K_MOUSEDOWN:
2016 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002017 case K_MOUSELEFT:
2018 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 /* Alternate buttons ignored here */
2020 case K_X1MOUSE:
2021 case K_X1DRAG:
2022 case K_X1RELEASE:
2023 case K_X2MOUSE:
2024 case K_X2DRAG:
2025 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002026 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 goto cmdline_not_changed;
2028
2029#endif /* FEAT_MOUSE */
2030
2031#ifdef FEAT_GUI
2032 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2033 case K_LEFTRELEASE_NM:
2034 goto cmdline_not_changed;
2035
2036 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002037 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
2039 gui_do_scroll();
2040 redrawcmd();
2041 }
2042 goto cmdline_not_changed;
2043
2044 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002045 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002047 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 redrawcmd();
2049 }
2050 goto cmdline_not_changed;
2051#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002052#ifdef FEAT_GUI_TABLINE
2053 case K_TABLINE:
2054 case K_TABMENU:
2055 /* Don't want to change any tabs here. Make sure the same tab
2056 * is still selected. */
2057 if (gui_use_tabline())
2058 gui_mch_set_curtab(tabpage_index(curtab));
2059 goto cmdline_not_changed;
2060#endif
2061
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 case K_SELECT: /* end of Select mode mapping - ignore */
2063 goto cmdline_not_changed;
2064
2065 case Ctrl_B: /* begin of command line */
2066 case K_HOME:
2067 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 case K_S_HOME:
2069 case K_C_HOME:
2070 ccline.cmdpos = 0;
2071 set_cmdspos();
2072 goto cmdline_not_changed;
2073
2074 case Ctrl_E: /* end of command line */
2075 case K_END:
2076 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 case K_S_END:
2078 case K_C_END:
2079 ccline.cmdpos = ccline.cmdlen;
2080 set_cmdspos_cursor();
2081 goto cmdline_not_changed;
2082
2083 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002084 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 break;
2086 goto cmdline_changed;
2087
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002088 case Ctrl_L:
2089#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002090 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002091 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002092#endif
2093
2094 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002095 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 break;
2097 goto cmdline_changed;
2098
2099 case Ctrl_N: /* next match */
2100 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002101 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002103 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2104 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002106 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002109 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 case K_UP:
2111 case K_DOWN:
2112 case K_S_UP:
2113 case K_S_DOWN:
2114 case K_PAGEUP:
2115 case K_KPAGEUP:
2116 case K_PAGEDOWN:
2117 case K_KPAGEDOWN:
2118 if (hislen == 0 || firstc == NUL) /* no history */
2119 goto cmdline_not_changed;
2120
2121 i = hiscnt;
2122
2123 /* save current command string so it can be restored later */
2124 if (lookfor == NULL)
2125 {
2126 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2127 goto cmdline_not_changed;
2128 lookfor[ccline.cmdpos] = NUL;
2129 }
2130
2131 j = (int)STRLEN(lookfor);
2132 for (;;)
2133 {
2134 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002135 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002136 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 if (hiscnt == hislen) /* first time */
2139 hiscnt = hisidx[histype];
2140 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2141 hiscnt = hislen - 1;
2142 else if (hiscnt != hisidx[histype] + 1)
2143 --hiscnt;
2144 else /* at top of list */
2145 {
2146 hiscnt = i;
2147 break;
2148 }
2149 }
2150 else /* one step forwards */
2151 {
2152 /* on last entry, clear the line */
2153 if (hiscnt == hisidx[histype])
2154 {
2155 hiscnt = hislen;
2156 break;
2157 }
2158
2159 /* not on a history line, nothing to do */
2160 if (hiscnt == hislen)
2161 break;
2162 if (hiscnt == hislen - 1) /* wrap around */
2163 hiscnt = 0;
2164 else
2165 ++hiscnt;
2166 }
2167 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2168 {
2169 hiscnt = i;
2170 break;
2171 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002172 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002173 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 || STRNCMP(history[histype][hiscnt].hisstr,
2175 lookfor, (size_t)j) == 0)
2176 break;
2177 }
2178
2179 if (hiscnt != i) /* jumped to other entry */
2180 {
2181 char_u *p;
2182 int len;
2183 int old_firstc;
2184
2185 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002186 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 if (hiscnt == hislen)
2188 p = lookfor; /* back to the old one */
2189 else
2190 p = history[histype][hiscnt].hisstr;
2191
2192 if (histype == HIST_SEARCH
2193 && p != lookfor
2194 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2195 {
2196 /* Correct for the separator character used when
2197 * adding the history entry vs the one used now.
2198 * First loop: count length.
2199 * Second loop: copy the characters. */
2200 for (i = 0; i <= 1; ++i)
2201 {
2202 len = 0;
2203 for (j = 0; p[j] != NUL; ++j)
2204 {
2205 /* Replace old sep with new sep, unless it is
2206 * escaped. */
2207 if (p[j] == old_firstc
2208 && (j == 0 || p[j - 1] != '\\'))
2209 {
2210 if (i > 0)
2211 ccline.cmdbuff[len] = firstc;
2212 }
2213 else
2214 {
2215 /* Escape new sep, unless it is already
2216 * escaped. */
2217 if (p[j] == firstc
2218 && (j == 0 || p[j - 1] != '\\'))
2219 {
2220 if (i > 0)
2221 ccline.cmdbuff[len] = '\\';
2222 ++len;
2223 }
2224 if (i > 0)
2225 ccline.cmdbuff[len] = p[j];
2226 }
2227 ++len;
2228 }
2229 if (i == 0)
2230 {
2231 alloc_cmdbuff(len);
2232 if (ccline.cmdbuff == NULL)
2233 goto returncmd;
2234 }
2235 }
2236 ccline.cmdbuff[len] = NUL;
2237 }
2238 else
2239 {
2240 alloc_cmdbuff((int)STRLEN(p));
2241 if (ccline.cmdbuff == NULL)
2242 goto returncmd;
2243 STRCPY(ccline.cmdbuff, p);
2244 }
2245
2246 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2247 redrawcmd();
2248 goto cmdline_changed;
2249 }
2250 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002251#endif
2252 goto cmdline_not_changed;
2253
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002254#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002255 case Ctrl_G: /* next match */
2256 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002257 if (may_adjust_incsearch_highlighting(
2258 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002259 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002260 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261#endif
2262
2263 case Ctrl_V:
2264 case Ctrl_Q:
2265#ifdef FEAT_MOUSE
2266 ignore_drag_release = TRUE;
2267#endif
2268 putcmdline('^', TRUE);
2269 c = get_literal(); /* get next (two) character(s) */
2270 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002271 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272#ifdef FEAT_MBYTE
2273 /* may need to remove ^ when composing char was typed */
2274 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2275 {
2276 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2277 msg_putchar(' ');
2278 cursorcmd();
2279 }
2280#endif
2281 break;
2282
2283#ifdef FEAT_DIGRAPHS
2284 case Ctrl_K:
2285#ifdef FEAT_MOUSE
2286 ignore_drag_release = TRUE;
2287#endif
2288 putcmdline('?', TRUE);
2289#ifdef USE_ON_FLY_SCROLL
2290 dont_scroll = TRUE; /* disallow scrolling here */
2291#endif
2292 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002293 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 if (c != NUL)
2295 break;
2296
2297 redrawcmd();
2298 goto cmdline_not_changed;
2299#endif /* FEAT_DIGRAPHS */
2300
2301#ifdef FEAT_RIGHTLEFT
2302 case Ctrl__: /* CTRL-_: switch language mode */
2303 if (!p_ari)
2304 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002305# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 if (p_altkeymap)
2307 {
2308 cmd_fkmap = !cmd_fkmap;
2309 if (cmd_fkmap) /* in Farsi always in Insert mode */
2310 ccline.overstrike = FALSE;
2311 }
2312 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002313# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 cmd_hkmap = !cmd_hkmap;
2315 goto cmdline_not_changed;
2316#endif
2317
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002318 case K_PS:
2319 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2320 goto cmdline_changed;
2321
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 default:
2323#ifdef UNIX
2324 if (c == intr_char)
2325 {
2326 gotesc = TRUE; /* will free ccline.cmdbuff after
2327 putting it in history */
2328 goto returncmd; /* back to Normal mode */
2329 }
2330#endif
2331 /*
2332 * Normal character with no special meaning. Just set mod_mask
2333 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2334 * the string <S-Space>. This should only happen after ^V.
2335 */
2336 if (!IS_SPECIAL(c))
2337 mod_mask = 0x0;
2338 break;
2339 }
2340 /*
2341 * End of switch on command line character.
2342 * We come here if we have a normal character.
2343 */
2344
Bram Moolenaarede3e632013-06-23 16:16:19 +02002345 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346#ifdef FEAT_MBYTE
2347 /* Add ABBR_OFF for characters above 0x100, this is
2348 * what check_abbr() expects. */
2349 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2350#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002351 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 goto cmdline_changed;
2353
2354 /*
2355 * put the character in the command line
2356 */
2357 if (IS_SPECIAL(c) || mod_mask != 0)
2358 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2359 else
2360 {
2361#ifdef FEAT_MBYTE
2362 if (has_mbyte)
2363 {
2364 j = (*mb_char2bytes)(c, IObuff);
2365 IObuff[j] = NUL; /* exclude composing chars */
2366 put_on_cmdline(IObuff, j, TRUE);
2367 }
2368 else
2369#endif
2370 {
2371 IObuff[0] = c;
2372 put_on_cmdline(IObuff, 1, TRUE);
2373 }
2374 }
2375 goto cmdline_changed;
2376
2377/*
2378 * This part implements incremental searches for "/" and "?"
2379 * Jump to cmdline_not_changed when a character has been read but the command
2380 * line did not change. Then we only search and redraw if something changed in
2381 * the past.
2382 * Jump to cmdline_changed when the command line did change.
2383 * (Sorry for the goto's, I know it is ugly).
2384 */
2385cmdline_not_changed:
2386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002387 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 continue;
2389#endif
2390
2391cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002392 /* Trigger CmdlineChanged autocommands. */
2393 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002396 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#endif
2398
2399#ifdef FEAT_RIGHTLEFT
2400 if (cmdmsg_rl
2401# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002402 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403# endif
2404 )
2405 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002406 * right-left typing. Not efficient, but it works.
2407 * Do it only when there are no characters left to read
2408 * to avoid useless intermediate redraws. */
2409 if (vpeekc() == NUL)
2410 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411#endif
2412 }
2413
2414returncmd:
2415
2416#ifdef FEAT_RIGHTLEFT
2417 cmdmsg_rl = FALSE;
2418#endif
2419
2420#ifdef FEAT_FKMAP
2421 cmd_fkmap = 0;
2422#endif
2423
2424 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002425 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426
2427#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002428 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429#endif
2430
2431 if (ccline.cmdbuff != NULL)
2432 {
2433 /*
2434 * Put line in history buffer (":" and "=" only when it was typed).
2435 */
2436#ifdef FEAT_CMDHIST
2437 if (ccline.cmdlen && firstc != NUL
2438 && (some_key_typed || histype == HIST_SEARCH))
2439 {
2440 add_to_history(histype, ccline.cmdbuff, TRUE,
2441 histype == HIST_SEARCH ? firstc : NUL);
2442 if (firstc == ':')
2443 {
2444 vim_free(new_last_cmdline);
2445 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2446 }
2447 }
2448#endif
2449
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002450 if (gotesc)
2451 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452 }
2453
2454 /*
2455 * If the screen was shifted up, redraw the whole screen (later).
2456 * If the line is too long, clear it, so ruler and shown command do
2457 * not get printed in the middle of it.
2458 */
2459 msg_check();
2460 msg_scroll = save_msg_scroll;
2461 redir_off = FALSE;
2462
2463 /* When the command line was typed, no need for a wait-return prompt. */
2464 if (some_key_typed)
2465 need_wait_return = FALSE;
2466
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002467 /* Trigger CmdlineLeave autocommands. */
2468 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002469
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002471#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2473 im_save_status(b_im_ptr);
2474 im_set_active(FALSE);
2475#endif
2476#ifdef FEAT_MOUSE
2477 setmouse();
2478#endif
2479#ifdef CURSOR_SHAPE
2480 ui_cursor_shape(); /* may show different cursor shape */
2481#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002482 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002484 {
2485 char_u *p = ccline.cmdbuff;
2486
2487 /* Make ccline empty, getcmdline() may try to use it. */
2488 ccline.cmdbuff = NULL;
2489 return p;
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491}
2492
2493#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2494/*
2495 * Get a command line with a prompt.
2496 * This is prepared to be called recursively from getcmdline() (e.g. by
2497 * f_input() when evaluating an expression from CTRL-R =).
2498 * Returns the command line in allocated memory, or NULL.
2499 */
2500 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002501getcmdline_prompt(
2502 int firstc,
2503 char_u *prompt, /* command line prompt */
2504 int attr, /* attributes for prompt */
2505 int xp_context, /* type of expansion */
2506 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507{
2508 char_u *s;
2509 struct cmdline_info save_ccline;
2510 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002511 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002513 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 ccline.cmdprompt = prompt;
2515 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002516# ifdef FEAT_EVAL
2517 ccline.xp_context = xp_context;
2518 ccline.xp_arg = xp_arg;
2519 ccline.input_fn = (firstc == '@');
2520# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002521 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002523 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002524 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002525 /* Restore msg_col, the prompt from input() may have changed it.
2526 * But only if called recursively and the commandline is therefore being
2527 * restored to an old one; if not, the input() prompt stays on the screen,
2528 * so we need its modified msg_col left intact. */
2529 if (ccline.cmdbuff != NULL)
2530 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531
2532 return s;
2533}
2534#endif
2535
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002536/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002537 * Return TRUE when the text must not be changed and we can't switch to
2538 * another window or buffer. Used when editing the command line, evaluating
2539 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002540 */
2541 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002542text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002543{
2544#ifdef FEAT_CMDWIN
2545 if (cmdwin_type != 0)
2546 return TRUE;
2547#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002548 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549}
2550
2551/*
2552 * Give an error message for a command that isn't allowed while the cmdline
2553 * window is open or editing the cmdline in another way.
2554 */
2555 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002556text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002557{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002558 EMSG(_(get_text_locked_msg()));
2559}
2560
2561 char_u *
2562get_text_locked_msg(void)
2563{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002564#ifdef FEAT_CMDWIN
2565 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002566 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002568 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002569}
2570
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002571/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002572 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2573 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002574 */
2575 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002576curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002577{
2578 if (curbuf_lock > 0)
2579 {
2580 EMSG(_("E788: Not allowed to edit another buffer now"));
2581 return TRUE;
2582 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002583 return allbuf_locked();
2584}
2585
2586/*
2587 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2588 * message.
2589 */
2590 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002591allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002592{
2593 if (allbuf_lock > 0)
2594 {
2595 EMSG(_("E811: Not allowed to change buffer information now"));
2596 return TRUE;
2597 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002598 return FALSE;
2599}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2605 if (cmdline_star > 0) /* showing '*', always 1 position */
2606 return 1;
2607#endif
2608 return ptr2cells(ccline.cmdbuff + idx);
2609}
2610
2611/*
2612 * Compute the offset of the cursor on the command line for the prompt and
2613 * indent.
2614 */
2615 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002616set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002618 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 ccline.cmdspos = 1 + ccline.cmdindent;
2620 else
2621 ccline.cmdspos = 0 + ccline.cmdindent;
2622}
2623
2624/*
2625 * Compute the screen position for the cursor on the command line.
2626 */
2627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002628set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
2630 int i, m, c;
2631
2632 set_cmdspos();
2633 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002634 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002636 if (m < 0) /* overflow, Columns or Rows at weird value */
2637 m = MAXCOL;
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 else
2640 m = MAXCOL;
2641 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2642 {
2643 c = cmdline_charsize(i);
2644#ifdef FEAT_MBYTE
2645 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2646 if (has_mbyte)
2647 correct_cmdspos(i, c);
2648#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002649 /* If the cmdline doesn't fit, show cursor on last visible char.
2650 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if ((ccline.cmdspos += c) >= m)
2652 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 ccline.cmdspos -= c;
2654 break;
2655 }
2656#ifdef FEAT_MBYTE
2657 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002658 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#endif
2660 }
2661}
2662
2663#ifdef FEAT_MBYTE
2664/*
2665 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2666 * character that doesn't fit, so that a ">" must be displayed.
2667 */
2668 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002669correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002671 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2673 && ccline.cmdspos % Columns + cells > Columns)
2674 ccline.cmdspos++;
2675}
2676#endif
2677
2678/*
2679 * Get an Ex command line for the ":" command.
2680 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002682getexline(
2683 int c, /* normally ':', NUL for ":append" */
2684 void *cookie UNUSED,
2685 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 /* When executing a register, remove ':' that's in front of each line. */
2688 if (exec_from_reg && vpeekc() == ':')
2689 (void)vgetc();
2690 return getcmdline(c, 1L, indent);
2691}
2692
2693/*
2694 * Get an Ex command line for Ex mode.
2695 * In Ex mode we only use the OS supplied line editing features and no
2696 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002700getexmodeline(
2701 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002703 void *cookie UNUSED,
2704 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 garray_T line_ga;
2707 char_u *pend;
2708 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002709 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 int escaped = FALSE; /* CTRL-V typed */
2711 int vcol = 0;
2712 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002713 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002714 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 /* Switch cursor on now. This avoids that it happens after the "\n", which
2717 * confuses the system function that computes tabstops. */
2718 cursor_on();
2719
2720 /* always start in column 0; write a newline if necessary */
2721 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002722 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002726 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 if (p_prompt)
2728 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 while (indent-- > 0)
2730 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 }
2733
2734 ga_init2(&line_ga, 1, 30);
2735
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002736 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002738 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002739 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002740 while (indent >= 8)
2741 {
2742 ga_append(&line_ga, TAB);
2743 msg_puts((char_u *)" ");
2744 indent -= 8;
2745 }
2746 while (indent-- > 0)
2747 {
2748 ga_append(&line_ga, ' ');
2749 msg_putchar(' ');
2750 }
2751 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002752 ++no_mapping;
2753 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002754
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 /*
2756 * Get the line, one character at a time.
2757 */
2758 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002761 long sw;
2762 char_u *s;
2763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 if (ga_grow(&line_ga, 40) == FAIL)
2765 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766
Bram Moolenaarba748c82017-02-26 14:00:07 +01002767 /*
2768 * Get one character at a time.
2769 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002770 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002771
2772 /* Check for a ":normal" command and no more characters left. */
2773 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2774 c1 = '\n';
2775 else
2776 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777
2778 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 * Handle line editing.
2780 * Previously this was left to the system, putting the terminal in
2781 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002785 msg_putchar('\n');
2786 break;
2787 }
2788
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002789 if (c1 == K_PS)
2790 {
2791 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2792 goto redraw;
2793 }
2794
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 if (!escaped)
2796 {
2797 /* CR typed means "enter", which is NL */
2798 if (c1 == '\r')
2799 c1 = '\n';
2800
2801 if (c1 == BS || c1 == K_BS
2802 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 if (line_ga.ga_len > 0)
2805 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002806#ifdef FEAT_MBYTE
2807 if (has_mbyte)
2808 {
2809 p = (char_u *)line_ga.ga_data;
2810 p[line_ga.ga_len] = NUL;
2811 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2812 line_ga.ga_len -= len;
2813 }
2814 else
2815#endif
2816 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 goto redraw;
2818 }
2819 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820 }
2821
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 msg_col = startcol;
2825 msg_clr_eos();
2826 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002827 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 }
2829
2830 if (c1 == Ctrl_T)
2831 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002832 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 p = (char_u *)line_ga.ga_data;
2834 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002835 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002836 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002837add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002838 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002840 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002841 p = (char_u *)line_ga.ga_data;
2842 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2844 *s = ' ';
2845 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847redraw:
2848 /* redraw the line */
2849 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002851 p = (char_u *)line_ga.ga_data;
2852 p[line_ga.ga_len] = NUL;
2853 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 msg_putchar(' ');
2860 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002861 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002863 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002865 len = MB_PTR2LEN(p);
2866 msg_outtrans_len(p, len);
2867 vcol += ptr2cells(p);
2868 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
2870 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002871 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002872 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873 continue;
2874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 if (c1 == Ctrl_D)
2877 {
2878 /* Delete one shiftwidth. */
2879 p = (char_u *)line_ga.ga_data;
2880 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002882 if (prev_char == '^')
2883 ex_keep_indent = TRUE;
2884 indent = 0;
2885 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 }
2887 else
2888 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002889 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002890 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002891 if (indent > 0)
2892 {
2893 --indent;
2894 indent -= indent % get_sw_value(curbuf);
2895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002897 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002898 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002899 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2901 --line_ga.ga_len;
2902 }
2903 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905
2906 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2907 {
2908 escaped = TRUE;
2909 continue;
2910 }
2911
2912 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2913 if (IS_SPECIAL(c1))
2914 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002916
2917 if (IS_SPECIAL(c1))
2918 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002919#ifdef FEAT_MBYTE
2920 if (has_mbyte)
2921 len = (*mb_char2bytes)(c1,
2922 (char_u *)line_ga.ga_data + line_ga.ga_len);
2923 else
2924#endif
2925 {
2926 len = 1;
2927 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2928 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002929 if (c1 == '\n')
2930 msg_putchar('\n');
2931 else if (c1 == TAB)
2932 {
2933 /* Don't use chartabsize(), 'ts' can be different */
2934 do
2935 {
2936 msg_putchar(' ');
2937 } while (++vcol % 8);
2938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002942 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002943 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002945 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002946 escaped = FALSE;
2947
2948 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002949 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002950
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002951 /* We are done when a NL is entered, but not when it comes after an
2952 * odd number of backslashes, that results in a NUL. */
2953 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002955 int bcount = 0;
2956
2957 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2958 ++bcount;
2959
2960 if (bcount > 0)
2961 {
2962 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2963 * "\NL", etc. */
2964 line_ga.ga_len -= (bcount + 1) / 2;
2965 pend -= (bcount + 1) / 2;
2966 pend[-1] = '\n';
2967 }
2968
2969 if ((bcount & 1) == 0)
2970 {
2971 --line_ga.ga_len;
2972 --pend;
2973 *pend = NUL;
2974 break;
2975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 }
2977 }
2978
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002979 --no_mapping;
2980 --allow_keys;
2981
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 /* make following messages go to the next line */
2983 msg_didout = FALSE;
2984 msg_col = 0;
2985 if (msg_row < Rows - 1)
2986 ++msg_row;
2987 emsg_on_display = FALSE; /* don't want ui_delay() */
2988
2989 if (got_int)
2990 ga_clear(&line_ga);
2991
2992 return (char_u *)line_ga.ga_data;
2993}
2994
Bram Moolenaare344bea2005-09-01 20:46:49 +00002995# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2996 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997/*
2998 * Return TRUE if ccline.overstrike is on.
2999 */
3000 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003001cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002{
3003 return ccline.overstrike;
3004}
3005
3006/*
3007 * Return TRUE if the cursor is at the end of the cmdline.
3008 */
3009 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003010cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 return (ccline.cmdpos >= ccline.cmdlen);
3013}
3014#endif
3015
Bram Moolenaar9372a112005-12-06 19:59:18 +00003016#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017/*
3018 * Return the virtual column number at the current cursor position.
3019 * This is used by the IM code to obtain the start of the preedit string.
3020 */
3021 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003022cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
3024 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3025 return MAXCOL;
3026
3027# ifdef FEAT_MBYTE
3028 if (has_mbyte)
3029 {
3030 colnr_T col;
3031 int i = 0;
3032
3033 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003034 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035
3036 return col;
3037 }
3038 else
3039# endif
3040 return ccline.cmdpos;
3041}
3042#endif
3043
3044#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3045/*
3046 * If part of the command line is an IM preedit string, redraw it with
3047 * IM feedback attributes. The cursor position is restored after drawing.
3048 */
3049 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003050redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051{
3052 if ((State & CMDLINE)
3053 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003054 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 && !p_imdisable
3056 && im_is_preediting())
3057 {
3058 int cmdpos = 0;
3059 int cmdspos;
3060 int old_row;
3061 int old_col;
3062 colnr_T col;
3063
3064 old_row = msg_row;
3065 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003066 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067
3068# ifdef FEAT_MBYTE
3069 if (has_mbyte)
3070 {
3071 for (col = 0; col < preedit_start_col
3072 && cmdpos < ccline.cmdlen; ++col)
3073 {
3074 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003075 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077 }
3078 else
3079# endif
3080 {
3081 cmdspos += preedit_start_col;
3082 cmdpos += preedit_start_col;
3083 }
3084
3085 msg_row = cmdline_row + (cmdspos / (int)Columns);
3086 msg_col = cmdspos % (int)Columns;
3087 if (msg_row >= Rows)
3088 msg_row = Rows - 1;
3089
3090 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3091 {
3092 int char_len;
3093 int char_attr;
3094
3095 char_attr = im_get_feedback_attr(col);
3096 if (char_attr < 0)
3097 break; /* end of preedit string */
3098
3099# ifdef FEAT_MBYTE
3100 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003101 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 else
3103# endif
3104 char_len = 1;
3105
3106 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3107 cmdpos += char_len;
3108 }
3109
3110 msg_row = old_row;
3111 msg_col = old_col;
3112 }
3113}
3114#endif /* FEAT_XIM && FEAT_GUI_GTK */
3115
3116/*
3117 * Allocate a new command line buffer.
3118 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3119 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3120 */
3121 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003122alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123{
3124 /*
3125 * give some extra space to avoid having to allocate all the time
3126 */
3127 if (len < 80)
3128 len = 100;
3129 else
3130 len += 20;
3131
3132 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3133 ccline.cmdbufflen = len;
3134}
3135
3136/*
3137 * Re-allocate the command line to length len + something extra.
3138 * return FAIL for failure, OK otherwise
3139 */
3140 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003141realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142{
3143 char_u *p;
3144
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003145 if (len < ccline.cmdbufflen)
3146 return OK; /* no need to resize */
3147
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 p = ccline.cmdbuff;
3149 alloc_cmdbuff(len); /* will get some more */
3150 if (ccline.cmdbuff == NULL) /* out of memory */
3151 {
3152 ccline.cmdbuff = p; /* keep the old one */
3153 return FAIL;
3154 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003155 /* There isn't always a NUL after the command, but it may need to be
3156 * there, thus copy up to the NUL and add a NUL. */
3157 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3158 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003160
3161 if (ccline.xpc != NULL
3162 && ccline.xpc->xp_pattern != NULL
3163 && ccline.xpc->xp_context != EXPAND_NOTHING
3164 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3165 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003166 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003167
3168 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3169 * to point into the newly allocated memory. */
3170 if (i >= 0 && i <= ccline.cmdlen)
3171 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3172 }
3173
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 return OK;
3175}
3176
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003177#if defined(FEAT_ARABIC) || defined(PROTO)
3178static char_u *arshape_buf = NULL;
3179
3180# if defined(EXITFREE) || defined(PROTO)
3181 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003182free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003183{
3184 vim_free(arshape_buf);
3185}
3186# endif
3187#endif
3188
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189/*
3190 * Draw part of the cmdline at the current cursor position. But draw stars
3191 * when cmdline_star is TRUE.
3192 */
3193 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003194draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195{
3196#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3197 int i;
3198
3199 if (cmdline_star > 0)
3200 for (i = 0; i < len; ++i)
3201 {
3202 msg_putchar('*');
3203# ifdef FEAT_MBYTE
3204 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003205 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206# endif
3207 }
3208 else
3209#endif
3210#ifdef FEAT_ARABIC
3211 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3212 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 static int buflen = 0;
3214 char_u *p;
3215 int j;
3216 int newlen = 0;
3217 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003218 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 int prev_c = 0;
3220 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003221 int u8c;
3222 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224
3225 /*
3226 * Do arabic shaping into a temporary buffer. This is very
3227 * inefficient!
3228 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003229 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 /* Re-allocate the buffer. We keep it around to avoid a lot of
3232 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003233 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003234 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003235 arshape_buf = alloc(buflen);
3236 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 return; /* out of memory */
3238 }
3239
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003240 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3241 {
3242 /* Prepend a space to draw the leading composing char on. */
3243 arshape_buf[0] = ' ';
3244 newlen = 1;
3245 }
3246
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 for (j = start; j < start + len; j += mb_l)
3248 {
3249 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003250 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003251 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 if (ARABIC_CHAR(u8c))
3253 {
3254 /* Do Arabic shaping. */
3255 if (cmdmsg_rl)
3256 {
3257 /* displaying from right to left */
3258 pc = prev_c;
3259 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003260 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261 if (j + mb_l >= start + len)
3262 nc = NUL;
3263 else
3264 nc = utf_ptr2char(p + mb_l);
3265 }
3266 else
3267 {
3268 /* displaying from left to right */
3269 if (j + mb_l >= start + len)
3270 pc = NUL;
3271 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003272 {
3273 int pcc[MAX_MCO];
3274
3275 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003277 pc1 = pcc[0];
3278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 nc = prev_c;
3280 }
3281 prev_c = u8c;
3282
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003283 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003286 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003288 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3289 if (u8cc[1] != 0)
3290 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003291 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293 }
3294 else
3295 {
3296 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003297 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 newlen += mb_l;
3299 }
3300 }
3301
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003302 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 }
3304 else
3305#endif
3306 msg_outtrans_len(ccline.cmdbuff + start, len);
3307}
3308
3309/*
3310 * Put a character on the command line. Shifts the following text to the
3311 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3312 * "c" must be printable (fit in one display cell)!
3313 */
3314 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003315putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 if (cmd_silent)
3318 return;
3319 msg_no_more = TRUE;
3320 msg_putchar(c);
3321 if (shift)
3322 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3323 msg_no_more = FALSE;
3324 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003325 extra_char = c;
3326 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327}
3328
3329/*
3330 * Undo a putcmdline(c, FALSE).
3331 */
3332 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003333unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334{
3335 if (cmd_silent)
3336 return;
3337 msg_no_more = TRUE;
3338 if (ccline.cmdlen == ccline.cmdpos)
3339 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003340#ifdef FEAT_MBYTE
3341 else if (has_mbyte)
3342 draw_cmdline(ccline.cmdpos,
3343 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3344#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 else
3346 draw_cmdline(ccline.cmdpos, 1);
3347 msg_no_more = FALSE;
3348 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003349 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350}
3351
3352/*
3353 * Put the given string, of the given length, onto the command line.
3354 * If len is -1, then STRLEN() is used to calculate the length.
3355 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3356 * part will be redrawn, otherwise it will not. If this function is called
3357 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3358 * called afterwards.
3359 */
3360 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003361put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362{
3363 int retval;
3364 int i;
3365 int m;
3366 int c;
3367
3368 if (len < 0)
3369 len = (int)STRLEN(str);
3370
3371 /* Check if ccline.cmdbuff needs to be longer */
3372 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003373 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 else
3375 retval = OK;
3376 if (retval == OK)
3377 {
3378 if (!ccline.overstrike)
3379 {
3380 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3381 ccline.cmdbuff + ccline.cmdpos,
3382 (size_t)(ccline.cmdlen - ccline.cmdpos));
3383 ccline.cmdlen += len;
3384 }
3385 else
3386 {
3387#ifdef FEAT_MBYTE
3388 if (has_mbyte)
3389 {
3390 /* Count nr of characters in the new string. */
3391 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003392 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 ++m;
3394 /* Count nr of bytes in cmdline that are overwritten by these
3395 * characters. */
3396 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003397 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 --m;
3399 if (i < ccline.cmdlen)
3400 {
3401 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3402 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3403 ccline.cmdlen += ccline.cmdpos + len - i;
3404 }
3405 else
3406 ccline.cmdlen = ccline.cmdpos + len;
3407 }
3408 else
3409#endif
3410 if (ccline.cmdpos + len > ccline.cmdlen)
3411 ccline.cmdlen = ccline.cmdpos + len;
3412 }
3413 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3414 ccline.cmdbuff[ccline.cmdlen] = NUL;
3415
3416#ifdef FEAT_MBYTE
3417 if (enc_utf8)
3418 {
3419 /* When the inserted text starts with a composing character,
3420 * backup to the character before it. There could be two of them.
3421 */
3422 i = 0;
3423 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3424 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3425 {
3426 i = (*mb_head_off)(ccline.cmdbuff,
3427 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3428 ccline.cmdpos -= i;
3429 len += i;
3430 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3431 }
3432# ifdef FEAT_ARABIC
3433 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3434 {
3435 /* Check the previous character for Arabic combining pair. */
3436 i = (*mb_head_off)(ccline.cmdbuff,
3437 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3438 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3439 + ccline.cmdpos - i), c))
3440 {
3441 ccline.cmdpos -= i;
3442 len += i;
3443 }
3444 else
3445 i = 0;
3446 }
3447# endif
3448 if (i != 0)
3449 {
3450 /* Also backup the cursor position. */
3451 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3452 ccline.cmdspos -= i;
3453 msg_col -= i;
3454 if (msg_col < 0)
3455 {
3456 msg_col += Columns;
3457 --msg_row;
3458 }
3459 }
3460 }
3461#endif
3462
3463 if (redraw && !cmd_silent)
3464 {
3465 msg_no_more = TRUE;
3466 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003467 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3469 /* Avoid clearing the rest of the line too often. */
3470 if (cmdline_row != i || ccline.overstrike)
3471 msg_clr_eos();
3472 msg_no_more = FALSE;
3473 }
3474#ifdef FEAT_FKMAP
3475 /*
3476 * If we are in Farsi command mode, the character input must be in
3477 * Insert mode. So do not advance the cmdpos.
3478 */
3479 if (!cmd_fkmap)
3480#endif
3481 {
3482 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003483 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003485 if (m < 0) /* overflow, Columns or Rows at weird value */
3486 m = MAXCOL;
3487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 else
3489 m = MAXCOL;
3490 for (i = 0; i < len; ++i)
3491 {
3492 c = cmdline_charsize(ccline.cmdpos);
3493#ifdef FEAT_MBYTE
3494 /* count ">" for a double-wide char that doesn't fit. */
3495 if (has_mbyte)
3496 correct_cmdspos(ccline.cmdpos, c);
3497#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003498 /* Stop cursor at the end of the screen, but do increment the
3499 * insert position, so that entering a very long command
3500 * works, even though you can't see it. */
3501 if (ccline.cmdspos + c < m)
3502 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503#ifdef FEAT_MBYTE
3504 if (has_mbyte)
3505 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003506 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (c > len - i - 1)
3508 c = len - i - 1;
3509 ccline.cmdpos += c;
3510 i += c;
3511 }
3512#endif
3513 ++ccline.cmdpos;
3514 }
3515 }
3516 }
3517 if (redraw)
3518 msg_check();
3519 return retval;
3520}
3521
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003522static struct cmdline_info prev_ccline;
3523static int prev_ccline_used = FALSE;
3524
3525/*
3526 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3527 * and overwrite it. But get_cmdline_str() may need it, thus make it
3528 * available globally in prev_ccline.
3529 */
3530 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003531save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003532{
3533 if (!prev_ccline_used)
3534 {
3535 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3536 prev_ccline_used = TRUE;
3537 }
3538 *ccp = prev_ccline;
3539 prev_ccline = ccline;
3540 ccline.cmdbuff = NULL;
3541 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003542 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003543}
3544
3545/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003546 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003547 */
3548 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003549restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003550{
3551 ccline = prev_ccline;
3552 prev_ccline = *ccp;
3553}
3554
Bram Moolenaar5a305422006-04-28 22:38:25 +00003555#if defined(FEAT_EVAL) || defined(PROTO)
3556/*
3557 * Save the command line into allocated memory. Returns a pointer to be
3558 * passed to restore_cmdline_alloc() later.
3559 * Returns NULL when failed.
3560 */
3561 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003562save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003563{
3564 struct cmdline_info *p;
3565
3566 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3567 if (p != NULL)
3568 save_cmdline(p);
3569 return (char_u *)p;
3570}
3571
3572/*
3573 * Restore the command line from the return value of save_cmdline_alloc().
3574 */
3575 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003576restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003577{
3578 if (p != NULL)
3579 {
3580 restore_cmdline((struct cmdline_info *)p);
3581 vim_free(p);
3582 }
3583}
3584#endif
3585
Bram Moolenaar8299df92004-07-10 09:47:34 +00003586/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003587 * Paste a yank register into the command line.
3588 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003589 * insert_reg() can't be used here, because special characters from the
3590 * register contents will be interpreted as commands.
3591 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003592 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003593 */
3594 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003595cmdline_paste(
3596 int regname,
3597 int literally, /* Insert text literally instead of "as typed" */
3598 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003599{
3600 long i;
3601 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003602 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 int allocated;
3604 struct cmdline_info save_ccline;
3605
3606 /* check for valid regname; also accept special characters for CTRL-R in
3607 * the command line */
3608 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003609 && regname != Ctrl_A && regname != Ctrl_L
3610 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003611 return FAIL;
3612
3613 /* A register containing CTRL-R can cause an endless loop. Allow using
3614 * CTRL-C to break the loop. */
3615 line_breakcheck();
3616 if (got_int)
3617 return FAIL;
3618
3619#ifdef FEAT_CLIPBOARD
3620 regname = may_get_selection(regname);
3621#endif
3622
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003623 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003624 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003625 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003626 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003627 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003628 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003629 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630
3631 if (i)
3632 {
3633 /* Got the value of a special register in "arg". */
3634 if (arg == NULL)
3635 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003636
3637 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3638 * part of the word. */
3639 p = arg;
3640 if (p_is && regname == Ctrl_W)
3641 {
3642 char_u *w;
3643 int len;
3644
3645 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003646 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003647 {
3648#ifdef FEAT_MBYTE
3649 if (has_mbyte)
3650 {
3651 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3652 if (!vim_iswordc(mb_ptr2char(w - len)))
3653 break;
3654 w -= len;
3655 }
3656 else
3657#endif
3658 {
3659 if (!vim_iswordc(w[-1]))
3660 break;
3661 --w;
3662 }
3663 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003664 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003665 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3666 p += len;
3667 }
3668
3669 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003670 if (allocated)
3671 vim_free(arg);
3672 return OK;
3673 }
3674
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003675 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003676}
3677
3678/*
3679 * Put a string on the command line.
3680 * When "literally" is TRUE, insert literally.
3681 * When "literally" is FALSE, insert as typed, but don't leave the command
3682 * line.
3683 */
3684 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003685cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003686{
3687 int c, cv;
3688
3689 if (literally)
3690 put_on_cmdline(s, -1, TRUE);
3691 else
3692 while (*s != NUL)
3693 {
3694 cv = *s;
3695 if (cv == Ctrl_V && s[1])
3696 ++s;
3697#ifdef FEAT_MBYTE
3698 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003699 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003700 else
3701#endif
3702 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003703 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3704 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003705#ifdef UNIX
3706 || c == intr_char
3707#endif
3708 || (c == Ctrl_BSL && *s == Ctrl_N))
3709 stuffcharReadbuff(Ctrl_V);
3710 stuffcharReadbuff(c);
3711 }
3712}
3713
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#ifdef FEAT_WILDMENU
3715/*
3716 * Delete characters on the command line, from "from" to the current
3717 * position.
3718 */
3719 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003720cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721{
3722 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3723 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3724 ccline.cmdlen -= ccline.cmdpos - from;
3725 ccline.cmdpos = from;
3726}
3727#endif
3728
3729/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003730 * This function is called when the screen size changes and with incremental
3731 * search and in other situations where the command line may have been
3732 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 */
3734 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003735redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003737 redrawcmdline_ex(TRUE);
3738}
3739
3740 void
3741redrawcmdline_ex(int do_compute_cmdrow)
3742{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 if (cmd_silent)
3744 return;
3745 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003746 if (do_compute_cmdrow)
3747 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 redrawcmd();
3749 cursorcmd();
3750}
3751
3752 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003753redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754{
3755 int i;
3756
3757 if (cmd_silent)
3758 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003759 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 msg_putchar(ccline.cmdfirstc);
3761 if (ccline.cmdprompt != NULL)
3762 {
3763 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3764 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3765 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003766 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 --ccline.cmdindent;
3768 }
3769 else
3770 for (i = ccline.cmdindent; i > 0; --i)
3771 msg_putchar(' ');
3772}
3773
3774/*
3775 * Redraw what is currently on the command line.
3776 */
3777 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003778redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779{
3780 if (cmd_silent)
3781 return;
3782
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003783 /* when 'incsearch' is set there may be no command line while redrawing */
3784 if (ccline.cmdbuff == NULL)
3785 {
3786 windgoto(cmdline_row, 0);
3787 msg_clr_eos();
3788 return;
3789 }
3790
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 msg_start();
3792 redrawcmdprompt();
3793
3794 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3795 msg_no_more = TRUE;
3796 draw_cmdline(0, ccline.cmdlen);
3797 msg_clr_eos();
3798 msg_no_more = FALSE;
3799
3800 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003801 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003802 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803
3804 /*
3805 * An emsg() before may have set msg_scroll. This is used in normal mode,
3806 * in cmdline mode we can reset them now.
3807 */
3808 msg_scroll = FALSE; /* next message overwrites cmdline */
3809
3810 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3811 * in cmdline mode */
3812 skip_redraw = FALSE;
3813}
3814
3815 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003816compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003818 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 cmdline_row = Rows - 1;
3820 else
3821 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003822 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823}
3824
3825 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003826cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827{
3828 if (cmd_silent)
3829 return;
3830
3831#ifdef FEAT_RIGHTLEFT
3832 if (cmdmsg_rl)
3833 {
3834 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3835 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3836 if (msg_row <= 0)
3837 msg_row = Rows - 1;
3838 }
3839 else
3840#endif
3841 {
3842 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3843 msg_col = ccline.cmdspos % (int)Columns;
3844 if (msg_row >= Rows)
3845 msg_row = Rows - 1;
3846 }
3847
3848 windgoto(msg_row, msg_col);
3849#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003850 if (p_imst == IM_ON_THE_SPOT)
3851 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852#endif
3853#ifdef MCH_CURSOR_SHAPE
3854 mch_update_cursor();
3855#endif
3856}
3857
3858 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003859gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860{
3861 msg_start();
3862#ifdef FEAT_RIGHTLEFT
3863 if (cmdmsg_rl)
3864 msg_col = Columns - 1;
3865 else
3866#endif
3867 msg_col = 0; /* always start in column 0 */
3868 if (clr) /* clear the bottom line(s) */
3869 msg_clr_eos(); /* will reset clear_cmdline */
3870 windgoto(cmdline_row, 0);
3871}
3872
3873/*
3874 * Check the word in front of the cursor for an abbreviation.
3875 * Called when the non-id character "c" has been entered.
3876 * When an abbreviation is recognized it is removed from the text with
3877 * backspaces and the replacement string is inserted, followed by "c".
3878 */
3879 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003880ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003882 int spos = 0;
3883
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3885 return FALSE;
3886
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003887 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3888 * Actually accepts any mark. */
3889 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3890 spos++;
3891 if (ccline.cmdlen - spos > 5
3892 && ccline.cmdbuff[spos] == '\''
3893 && ccline.cmdbuff[spos + 2] == ','
3894 && ccline.cmdbuff[spos + 3] == '\'')
3895 spos += 5;
3896 else
3897 /* check abbreviation from the beginning of the commandline */
3898 spos = 0;
3899
3900 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901}
3902
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003903#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3904 static int
3905#ifdef __BORLANDC__
3906_RTLENTRYF
3907#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003908sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003909{
3910 char_u *p1 = *(char_u **)s1;
3911 char_u *p2 = *(char_u **)s2;
3912
3913 if (*p1 != '<' && *p2 == '<') return -1;
3914 if (*p1 == '<' && *p2 != '<') return 1;
3915 return STRCMP(p1, p2);
3916}
3917#endif
3918
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919/*
3920 * Return FAIL if this is not an appropriate context in which to do
3921 * completion of anything, return OK if it is (even if there are no matches).
3922 * For the caller, this means that the character is just passed through like a
3923 * normal character (instead of being expanded). This allows :s/^I^D etc.
3924 */
3925 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003926nextwild(
3927 expand_T *xp,
3928 int type,
3929 int options, /* extra options for ExpandOne() */
3930 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931{
3932 int i, j;
3933 char_u *p1;
3934 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 int difflen;
3936 int v;
3937
3938 if (xp->xp_numfiles == -1)
3939 {
3940 set_expand_context(xp);
3941 cmd_showtail = expand_showtail(xp);
3942 }
3943
3944 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3945 {
3946 beep_flush();
3947 return OK; /* Something illegal on command line */
3948 }
3949 if (xp->xp_context == EXPAND_NOTHING)
3950 {
3951 /* Caller can use the character as a normal char instead */
3952 return FAIL;
3953 }
3954
3955 MSG_PUTS("..."); /* show that we are busy */
3956 out_flush();
3957
3958 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003959 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960
3961 if (type == WILD_NEXT || type == WILD_PREV)
3962 {
3963 /*
3964 * Get next/previous match for a previous expanded pattern.
3965 */
3966 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3967 }
3968 else
3969 {
3970 /*
3971 * Translate string into pattern and expand it.
3972 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003973 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3974 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 p2 = NULL;
3976 else
3977 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003978 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003979 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3980 if (escape)
3981 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003982
3983 if (p_wic)
3984 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003985 p2 = ExpandOne(xp, p1,
3986 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003987 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003989 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 if (p2 != NULL && type == WILD_LONGEST)
3991 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003992 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 if (ccline.cmdbuff[i + j] == '*'
3994 || ccline.cmdbuff[i + j] == '?')
3995 break;
3996 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003997 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 }
3999 }
4000 }
4001
4002 if (p2 != NULL && !got_int)
4003 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004004 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004005 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004007 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 xp->xp_pattern = ccline.cmdbuff + i;
4009 }
4010 else
4011 v = OK;
4012 if (v == OK)
4013 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004014 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4015 &ccline.cmdbuff[ccline.cmdpos],
4016 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4017 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 ccline.cmdlen += difflen;
4019 ccline.cmdpos += difflen;
4020 }
4021 }
4022 vim_free(p2);
4023
4024 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004025 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026
4027 /* When expanding a ":map" command and no matches are found, assume that
4028 * the key is supposed to be inserted literally */
4029 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4030 return FAIL;
4031
4032 if (xp->xp_numfiles <= 0 && p2 == NULL)
4033 beep_flush();
4034 else if (xp->xp_numfiles == 1)
4035 /* free expanded pattern */
4036 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4037
4038 return OK;
4039}
4040
4041/*
4042 * Do wildcard expansion on the string 'str'.
4043 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004044 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 * Return NULL for failure.
4046 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004047 * "orig" is the originally expanded string, copied to allocated memory. It
4048 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4049 * WILD_PREV "orig" should be NULL.
4050 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004051 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4052 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 *
4054 * mode = WILD_FREE: just free previously expanded matches
4055 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4056 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4057 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4058 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4059 * mode = WILD_ALL: return all matches concatenated
4060 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004061 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 *
4063 * options = WILD_LIST_NOTFOUND: list entries without a match
4064 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4065 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4066 * options = WILD_NO_BEEP: Don't beep for multiple matches
4067 * options = WILD_ADD_SLASH: add a slash after directory names
4068 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4069 * options = WILD_SILENT: don't print warning messages
4070 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004071 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 *
4073 * The variables xp->xp_context and xp->xp_backslash must have been set!
4074 */
4075 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004076ExpandOne(
4077 expand_T *xp,
4078 char_u *str,
4079 char_u *orig, /* allocated copy of original of expanded string */
4080 int options,
4081 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082{
4083 char_u *ss = NULL;
4084 static int findex;
4085 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004086 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 int i;
4088 long_u len;
4089 int non_suf_match; /* number without matching suffix */
4090
4091 /*
4092 * first handle the case of using an old match
4093 */
4094 if (mode == WILD_NEXT || mode == WILD_PREV)
4095 {
4096 if (xp->xp_numfiles > 0)
4097 {
4098 if (mode == WILD_PREV)
4099 {
4100 if (findex == -1)
4101 findex = xp->xp_numfiles;
4102 --findex;
4103 }
4104 else /* mode == WILD_NEXT */
4105 ++findex;
4106
4107 /*
4108 * When wrapping around, return the original string, set findex to
4109 * -1.
4110 */
4111 if (findex < 0)
4112 {
4113 if (orig_save == NULL)
4114 findex = xp->xp_numfiles - 1;
4115 else
4116 findex = -1;
4117 }
4118 if (findex >= xp->xp_numfiles)
4119 {
4120 if (orig_save == NULL)
4121 findex = 0;
4122 else
4123 findex = -1;
4124 }
4125#ifdef FEAT_WILDMENU
4126 if (p_wmnu)
4127 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4128 findex, cmd_showtail);
4129#endif
4130 if (findex == -1)
4131 return vim_strsave(orig_save);
4132 return vim_strsave(xp->xp_files[findex]);
4133 }
4134 else
4135 return NULL;
4136 }
4137
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004138 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4140 {
4141 FreeWild(xp->xp_numfiles, xp->xp_files);
4142 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004143 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 findex = 0;
4146
4147 if (mode == WILD_FREE) /* only release file name */
4148 return NULL;
4149
4150 if (xp->xp_numfiles == -1)
4151 {
4152 vim_free(orig_save);
4153 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004154 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155
4156 /*
4157 * Do the expansion.
4158 */
4159 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4160 options) == FAIL)
4161 {
4162#ifdef FNAME_ILLEGAL
4163 /* Illegal file name has been silently skipped. But when there
4164 * are wildcards, the real problem is that there was no match,
4165 * causing the pattern to be added, which has illegal characters.
4166 */
4167 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4168 EMSG2(_(e_nomatch2), str);
4169#endif
4170 }
4171 else if (xp->xp_numfiles == 0)
4172 {
4173 if (!(options & WILD_SILENT))
4174 EMSG2(_(e_nomatch2), str);
4175 }
4176 else
4177 {
4178 /* Escape the matches for use on the command line. */
4179 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4180
4181 /*
4182 * Check for matching suffixes in file names.
4183 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004184 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4185 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 if (xp->xp_numfiles)
4188 non_suf_match = xp->xp_numfiles;
4189 else
4190 non_suf_match = 1;
4191 if ((xp->xp_context == EXPAND_FILES
4192 || xp->xp_context == EXPAND_DIRECTORIES)
4193 && xp->xp_numfiles > 1)
4194 {
4195 /*
4196 * More than one match; check suffix.
4197 * The files will have been sorted on matching suffix in
4198 * expand_wildcards, only need to check the first two.
4199 */
4200 non_suf_match = 0;
4201 for (i = 0; i < 2; ++i)
4202 if (match_suffix(xp->xp_files[i]))
4203 ++non_suf_match;
4204 }
4205 if (non_suf_match != 1)
4206 {
4207 /* Can we ever get here unless it's while expanding
4208 * interactively? If not, we can get rid of this all
4209 * together. Don't really want to wait for this message
4210 * (and possibly have to hit return to continue!).
4211 */
4212 if (!(options & WILD_SILENT))
4213 EMSG(_(e_toomany));
4214 else if (!(options & WILD_NO_BEEP))
4215 beep_flush();
4216 }
4217 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4218 ss = vim_strsave(xp->xp_files[0]);
4219 }
4220 }
4221 }
4222
4223 /* Find longest common part */
4224 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4225 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004226 int mb_len = 1;
4227 int c0, ci;
4228
4229 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004231#ifdef FEAT_MBYTE
4232 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004234 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4235 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4236 }
4237 else
4238#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004239 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004240 for (i = 1; i < xp->xp_numfiles; ++i)
4241 {
4242#ifdef FEAT_MBYTE
4243 if (has_mbyte)
4244 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4245 else
4246#endif
4247 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004248 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004250 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004251 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004253 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 break;
4255 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004256 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 break;
4258 }
4259 if (i < xp->xp_numfiles)
4260 {
4261 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004262 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 break;
4264 }
4265 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004266
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 ss = alloc((unsigned)len + 1);
4268 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004269 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 findex = -1; /* next p_wc gets first one */
4271 }
4272
4273 /* Concatenate all matching names */
4274 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4275 {
4276 len = 0;
4277 for (i = 0; i < xp->xp_numfiles; ++i)
4278 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4279 ss = lalloc(len, TRUE);
4280 if (ss != NULL)
4281 {
4282 *ss = NUL;
4283 for (i = 0; i < xp->xp_numfiles; ++i)
4284 {
4285 STRCAT(ss, xp->xp_files[i]);
4286 if (i != xp->xp_numfiles - 1)
4287 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4288 }
4289 }
4290 }
4291
4292 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4293 ExpandCleanup(xp);
4294
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004295 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004296 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004297 vim_free(orig);
4298
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return ss;
4300}
4301
4302/*
4303 * Prepare an expand structure for use.
4304 */
4305 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004306ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004308 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004309 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004311#ifndef BACKSLASH_IN_FILENAME
4312 xp->xp_shell = FALSE;
4313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 xp->xp_numfiles = -1;
4315 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004316#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4317 xp->xp_arg = NULL;
4318#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004319 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320}
4321
4322/*
4323 * Cleanup an expand structure after use.
4324 */
4325 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004326ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327{
4328 if (xp->xp_numfiles >= 0)
4329 {
4330 FreeWild(xp->xp_numfiles, xp->xp_files);
4331 xp->xp_numfiles = -1;
4332 }
4333}
4334
4335 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004336ExpandEscape(
4337 expand_T *xp,
4338 char_u *str,
4339 int numfiles,
4340 char_u **files,
4341 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
4343 int i;
4344 char_u *p;
4345
4346 /*
4347 * May change home directory back to "~"
4348 */
4349 if (options & WILD_HOME_REPLACE)
4350 tilde_replace(str, numfiles, files);
4351
4352 if (options & WILD_ESCAPE)
4353 {
4354 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004355 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004356 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 || xp->xp_context == EXPAND_BUFFERS
4358 || xp->xp_context == EXPAND_DIRECTORIES)
4359 {
4360 /*
4361 * Insert a backslash into a file name before a space, \, %, #
4362 * and wildmatch characters, except '~'.
4363 */
4364 for (i = 0; i < numfiles; ++i)
4365 {
4366 /* for ":set path=" we need to escape spaces twice */
4367 if (xp->xp_backslash == XP_BS_THREE)
4368 {
4369 p = vim_strsave_escaped(files[i], (char_u *)" ");
4370 if (p != NULL)
4371 {
4372 vim_free(files[i]);
4373 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004374#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 p = vim_strsave_escaped(files[i], (char_u *)" ");
4376 if (p != NULL)
4377 {
4378 vim_free(files[i]);
4379 files[i] = p;
4380 }
4381#endif
4382 }
4383 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004384#ifdef BACKSLASH_IN_FILENAME
4385 p = vim_strsave_fnameescape(files[i], FALSE);
4386#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004387 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 if (p != NULL)
4390 {
4391 vim_free(files[i]);
4392 files[i] = p;
4393 }
4394
4395 /* If 'str' starts with "\~", replace "~" at start of
4396 * files[i] with "\~". */
4397 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004398 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004401
4402 /* If the first file starts with a '+' escape it. Otherwise it
4403 * could be seen as "+cmd". */
4404 if (*files[0] == '+')
4405 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407 else if (xp->xp_context == EXPAND_TAGS)
4408 {
4409 /*
4410 * Insert a backslash before characters in a tag name that
4411 * would terminate the ":tag" command.
4412 */
4413 for (i = 0; i < numfiles; ++i)
4414 {
4415 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4416 if (p != NULL)
4417 {
4418 vim_free(files[i]);
4419 files[i] = p;
4420 }
4421 }
4422 }
4423 }
4424}
4425
4426/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004427 * Escape special characters in "fname" for when used as a file name argument
4428 * after a Vim command, or, when "shell" is non-zero, a shell command.
4429 * Returns the result in allocated memory.
4430 */
4431 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004432vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004433{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004434 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004435#ifdef BACKSLASH_IN_FILENAME
4436 char_u buf[20];
4437 int j = 0;
4438
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004439 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004440 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004441 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004442 buf[j++] = *p;
4443 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004444 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004445#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004446 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4447 if (shell && csh_like_shell() && p != NULL)
4448 {
4449 char_u *s;
4450
4451 /* For csh and similar shells need to put two backslashes before '!'.
4452 * One is taken by Vim, one by the shell. */
4453 s = vim_strsave_escaped(p, (char_u *)"!");
4454 vim_free(p);
4455 p = s;
4456 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004457#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004458
4459 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4460 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004461 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004462 escape_fname(&p);
4463
4464 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004465}
4466
4467/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004468 * Put a backslash before the file name in "pp", which is in allocated memory.
4469 */
4470 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004471escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004472{
4473 char_u *p;
4474
4475 p = alloc((unsigned)(STRLEN(*pp) + 2));
4476 if (p != NULL)
4477 {
4478 p[0] = '\\';
4479 STRCPY(p + 1, *pp);
4480 vim_free(*pp);
4481 *pp = p;
4482 }
4483}
4484
4485/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 * For each file name in files[num_files]:
4487 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4488 */
4489 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004490tilde_replace(
4491 char_u *orig_pat,
4492 int num_files,
4493 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494{
4495 int i;
4496 char_u *p;
4497
4498 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4499 {
4500 for (i = 0; i < num_files; ++i)
4501 {
4502 p = home_replace_save(NULL, files[i]);
4503 if (p != NULL)
4504 {
4505 vim_free(files[i]);
4506 files[i] = p;
4507 }
4508 }
4509 }
4510}
4511
4512/*
4513 * Show all matches for completion on the command line.
4514 * Returns EXPAND_NOTHING when the character that triggered expansion should
4515 * be inserted like a normal character.
4516 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004518showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519{
4520#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4521 int num_files;
4522 char_u **files_found;
4523 int i, j, k;
4524 int maxlen;
4525 int lines;
4526 int columns;
4527 char_u *p;
4528 int lastlen;
4529 int attr;
4530 int showtail;
4531
4532 if (xp->xp_numfiles == -1)
4533 {
4534 set_expand_context(xp);
4535 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4536 &num_files, &files_found);
4537 showtail = expand_showtail(xp);
4538 if (i != EXPAND_OK)
4539 return i;
4540
4541 }
4542 else
4543 {
4544 num_files = xp->xp_numfiles;
4545 files_found = xp->xp_files;
4546 showtail = cmd_showtail;
4547 }
4548
4549#ifdef FEAT_WILDMENU
4550 if (!wildmenu)
4551 {
4552#endif
4553 msg_didany = FALSE; /* lines_left will be set */
4554 msg_start(); /* prepare for paging */
4555 msg_putchar('\n');
4556 out_flush();
4557 cmdline_row = msg_row;
4558 msg_didany = FALSE; /* lines_left will be set again */
4559 msg_start(); /* prepare for paging */
4560#ifdef FEAT_WILDMENU
4561 }
4562#endif
4563
4564 if (got_int)
4565 got_int = FALSE; /* only int. the completion, not the cmd line */
4566#ifdef FEAT_WILDMENU
4567 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004568 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569#endif
4570 else
4571 {
4572 /* find the length of the longest file name */
4573 maxlen = 0;
4574 for (i = 0; i < num_files; ++i)
4575 {
4576 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004577 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 || xp->xp_context == EXPAND_BUFFERS))
4579 {
4580 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4581 j = vim_strsize(NameBuff);
4582 }
4583 else
4584 j = vim_strsize(L_SHOWFILE(i));
4585 if (j > maxlen)
4586 maxlen = j;
4587 }
4588
4589 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4590 lines = num_files;
4591 else
4592 {
4593 /* compute the number of columns and lines for the listing */
4594 maxlen += 2; /* two spaces between file names */
4595 columns = ((int)Columns + 2) / maxlen;
4596 if (columns < 1)
4597 columns = 1;
4598 lines = (num_files + columns - 1) / columns;
4599 }
4600
Bram Moolenaar8820b482017-03-16 17:23:31 +01004601 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
4603 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4604 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004605 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 msg_clr_eos();
4607 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004608 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 }
4610
4611 /* list the files line by line */
4612 for (i = 0; i < lines; ++i)
4613 {
4614 lastlen = 999;
4615 for (k = i; k < num_files; k += lines)
4616 {
4617 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4618 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004619 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 p = files_found[k] + STRLEN(files_found[k]) + 1;
4621 msg_advance(maxlen + 1);
4622 msg_puts(p);
4623 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004624 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626 }
4627 for (j = maxlen - lastlen; --j >= 0; )
4628 msg_putchar(' ');
4629 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004630 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 || xp->xp_context == EXPAND_BUFFERS)
4632 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004633 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004634 if (xp->xp_numfiles != -1)
4635 {
4636 char_u *halved_slash;
4637 char_u *exp_path;
4638
4639 /* Expansion was done before and special characters
4640 * were escaped, need to halve backslashes. Also
4641 * $HOME has been replaced with ~/. */
4642 exp_path = expand_env_save_opt(files_found[k], TRUE);
4643 halved_slash = backslash_halve_save(
4644 exp_path != NULL ? exp_path : files_found[k]);
4645 j = mch_isdir(halved_slash != NULL ? halved_slash
4646 : files_found[k]);
4647 vim_free(exp_path);
4648 vim_free(halved_slash);
4649 }
4650 else
4651 /* Expansion was done here, file names are literal. */
4652 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 if (showtail)
4654 p = L_SHOWFILE(k);
4655 else
4656 {
4657 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4658 TRUE);
4659 p = NameBuff;
4660 }
4661 }
4662 else
4663 {
4664 j = FALSE;
4665 p = L_SHOWFILE(k);
4666 }
4667 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4668 }
4669 if (msg_col > 0) /* when not wrapped around */
4670 {
4671 msg_clr_eos();
4672 msg_putchar('\n');
4673 }
4674 out_flush(); /* show one line at a time */
4675 if (got_int)
4676 {
4677 got_int = FALSE;
4678 break;
4679 }
4680 }
4681
4682 /*
4683 * we redraw the command below the lines that we have just listed
4684 * This is a bit tricky, but it saves a lot of screen updating.
4685 */
4686 cmdline_row = msg_row; /* will put it back later */
4687 }
4688
4689 if (xp->xp_numfiles == -1)
4690 FreeWild(num_files, files_found);
4691
4692 return EXPAND_OK;
4693}
4694
4695/*
4696 * Private gettail for showmatches() (and win_redr_status_matches()):
4697 * Find tail of file name path, but ignore trailing "/".
4698 */
4699 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004700sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701{
4702 char_u *p;
4703 char_u *t = s;
4704 int had_sep = FALSE;
4705
4706 for (p = s; *p != NUL; )
4707 {
4708 if (vim_ispathsep(*p)
4709#ifdef BACKSLASH_IN_FILENAME
4710 && !rem_backslash(p)
4711#endif
4712 )
4713 had_sep = TRUE;
4714 else if (had_sep)
4715 {
4716 t = p;
4717 had_sep = FALSE;
4718 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004719 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 }
4721 return t;
4722}
4723
4724/*
4725 * Return TRUE if we only need to show the tail of completion matches.
4726 * When not completing file names or there is a wildcard in the path FALSE is
4727 * returned.
4728 */
4729 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004730expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731{
4732 char_u *s;
4733 char_u *end;
4734
4735 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004736 if (xp->xp_context != EXPAND_FILES
4737 && xp->xp_context != EXPAND_SHELLCMD
4738 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 return FALSE;
4740
4741 end = gettail(xp->xp_pattern);
4742 if (end == xp->xp_pattern) /* there is no path separator */
4743 return FALSE;
4744
4745 for (s = xp->xp_pattern; s < end; s++)
4746 {
4747 /* Skip escaped wildcards. Only when the backslash is not a path
4748 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4749 if (rem_backslash(s))
4750 ++s;
4751 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4752 return FALSE;
4753 }
4754 return TRUE;
4755}
4756
4757/*
4758 * Prepare a string for expansion.
4759 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004760 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 * When expanding other names: The string will be used with regcomp(). Copy
4762 * the name into allocated memory and prepend "^".
4763 */
4764 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004765addstar(
4766 char_u *fname,
4767 int len,
4768 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769{
4770 char_u *retval;
4771 int i, j;
4772 int new_len;
4773 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004774 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004776 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004777 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004778 && context != EXPAND_SHELLCMD
4779 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 {
4781 /*
4782 * Matching will be done internally (on something other than files).
4783 * So we convert the file-matching-type wildcards into our kind for
4784 * use with vim_regcomp(). First work out how long it will be:
4785 */
4786
4787 /* For help tags the translation is done in find_help_tags().
4788 * For a tag pattern starting with "/" no translation is needed. */
4789 if (context == EXPAND_HELP
4790 || context == EXPAND_COLORS
4791 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004792 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004793 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004794 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004795 || ((context == EXPAND_TAGS_LISTFILES
4796 || context == EXPAND_TAGS)
4797 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 retval = vim_strnsave(fname, len);
4799 else
4800 {
4801 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4802 for (i = 0; i < len; i++)
4803 {
4804 if (fname[i] == '*' || fname[i] == '~')
4805 new_len++; /* '*' needs to be replaced by ".*"
4806 '~' needs to be replaced by "\~" */
4807
4808 /* Buffer names are like file names. "." should be literal */
4809 if (context == EXPAND_BUFFERS && fname[i] == '.')
4810 new_len++; /* "." becomes "\." */
4811
4812 /* Custom expansion takes care of special things, match
4813 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004814 if ((context == EXPAND_USER_DEFINED
4815 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 new_len++; /* '\' becomes "\\" */
4817 }
4818 retval = alloc(new_len);
4819 if (retval != NULL)
4820 {
4821 retval[0] = '^';
4822 j = 1;
4823 for (i = 0; i < len; i++, j++)
4824 {
4825 /* Skip backslash. But why? At least keep it for custom
4826 * expansion. */
4827 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004828 && context != EXPAND_USER_LIST
4829 && fname[i] == '\\'
4830 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 break;
4832
4833 switch (fname[i])
4834 {
4835 case '*': retval[j++] = '.';
4836 break;
4837 case '~': retval[j++] = '\\';
4838 break;
4839 case '?': retval[j] = '.';
4840 continue;
4841 case '.': if (context == EXPAND_BUFFERS)
4842 retval[j++] = '\\';
4843 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004844 case '\\': if (context == EXPAND_USER_DEFINED
4845 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 retval[j++] = '\\';
4847 break;
4848 }
4849 retval[j] = fname[i];
4850 }
4851 retval[j] = NUL;
4852 }
4853 }
4854 }
4855 else
4856 {
4857 retval = alloc(len + 4);
4858 if (retval != NULL)
4859 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004860 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004863 * Don't add a star to *, ~, ~user, $var or `cmd`.
4864 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 * ~ would be at the start of the file name, but not the tail.
4866 * $ could be anywhere in the tail.
4867 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004868 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 */
4870 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004871 ends_in_star = (len > 0 && retval[len - 1] == '*');
4872#ifndef BACKSLASH_IN_FILENAME
4873 for (i = len - 2; i >= 0; --i)
4874 {
4875 if (retval[i] != '\\')
4876 break;
4877 ends_in_star = !ends_in_star;
4878 }
4879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004881 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 && vim_strchr(tail, '$') == NULL
4883 && vim_strchr(retval, '`') == NULL)
4884 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004885 else if (len > 0 && retval[len - 1] == '$')
4886 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 retval[len] = NUL;
4888 }
4889 }
4890 return retval;
4891}
4892
4893/*
4894 * Must parse the command line so far to work out what context we are in.
4895 * Completion can then be done based on that context.
4896 * This routine sets the variables:
4897 * xp->xp_pattern The start of the pattern to be expanded within
4898 * the command line (ends at the cursor).
4899 * xp->xp_context The type of thing to expand. Will be one of:
4900 *
4901 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4902 * the command line, like an unknown command. Caller
4903 * should beep.
4904 * EXPAND_NOTHING Unrecognised context for completion, use char like
4905 * a normal char, rather than for completion. eg
4906 * :s/^I/
4907 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4908 * it.
4909 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4910 * EXPAND_FILES After command with XFILE set, or after setting
4911 * with P_EXPAND set. eg :e ^I, :w>>^I
4912 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4913 * when we know only directories are of interest. eg
4914 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004915 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4917 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4918 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4919 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4920 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4921 * EXPAND_EVENTS Complete event names
4922 * EXPAND_SYNTAX Complete :syntax command arguments
4923 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4924 * EXPAND_AUGROUP Complete autocommand group names
4925 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4926 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4927 * eg :unmap a^I , :cunab x^I
4928 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4929 * eg :call sub^I
4930 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4931 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4932 * names in expressions, eg :while s^I
4933 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004934 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 */
4936 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004937set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004939 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 if (ccline.cmdfirstc != ':'
4941#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004942 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004943 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945 )
4946 {
4947 xp->xp_context = EXPAND_NOTHING;
4948 return;
4949 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004950 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951}
4952
4953 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004954set_cmd_context(
4955 expand_T *xp,
4956 char_u *str, /* start of command line */
4957 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004958 int col, /* position of cursor */
4959 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
4961 int old_char = NUL;
4962 char_u *nextcomm;
4963
4964 /*
4965 * Avoid a UMR warning from Purify, only save the character if it has been
4966 * written before.
4967 */
4968 if (col < len)
4969 old_char = str[col];
4970 str[col] = NUL;
4971 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004972
4973#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004974 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004975 {
4976# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004977 /* pass CMD_SIZE because there is no real command */
4978 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004979# endif
4980 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004981 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004982 {
4983 xp->xp_context = ccline.xp_context;
4984 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004985# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004986 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004987# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004988 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004989 else
4990#endif
4991 while (nextcomm != NULL)
4992 nextcomm = set_one_cmd_context(xp, nextcomm);
4993
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004994 /* Store the string here so that call_user_expand_func() can get to them
4995 * easily. */
4996 xp->xp_line = str;
4997 xp->xp_col = col;
4998
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 str[col] = old_char;
5000}
5001
5002/*
5003 * Expand the command line "str" from context "xp".
5004 * "xp" must have been set by set_cmd_context().
5005 * xp->xp_pattern points into "str", to where the text that is to be expanded
5006 * starts.
5007 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
5008 * cursor.
5009 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5010 * key that triggered expansion literally.
5011 * Returns EXPAND_OK otherwise.
5012 */
5013 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005014expand_cmdline(
5015 expand_T *xp,
5016 char_u *str, /* start of command line */
5017 int col, /* position of cursor */
5018 int *matchcount, /* return: nr of matches */
5019 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020{
5021 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005022 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5025 {
5026 beep_flush();
5027 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5028 }
5029 if (xp->xp_context == EXPAND_NOTHING)
5030 {
5031 /* Caller can use the character as a normal char instead */
5032 return EXPAND_NOTHING;
5033 }
5034
5035 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005036 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5037 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 if (file_str == NULL)
5039 return EXPAND_UNSUCCESSFUL;
5040
Bram Moolenaar94950a92010-12-02 16:01:29 +01005041 if (p_wic)
5042 options += WILD_ICASE;
5043
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005045 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
5047 *matchcount = 0;
5048 *matches = NULL;
5049 }
5050 vim_free(file_str);
5051
5052 return EXPAND_OK;
5053}
5054
5055#ifdef FEAT_MULTI_LANG
5056/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005057 * Cleanup matches for help tags:
5058 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5059 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005061static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005064cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
5066 int i, j;
5067 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005068 char_u buf[4];
5069 char_u *p = buf;
5070
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005071 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005072 {
5073 *p++ = '@';
5074 *p++ = p_hlg[0];
5075 *p++ = p_hlg[1];
5076 }
5077 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
5079 for (i = 0; i < num_file; ++i)
5080 {
5081 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005082 if (len <= 0)
5083 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005084 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
5086 /* Sorting on priority means the same item in another language may
5087 * be anywhere. Search all items for a match up to the "@en". */
5088 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005089 if (j != i && (int)STRLEN(file[j]) == len + 3
5090 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005093 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 file[i][len] = NUL;
5095 }
5096 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005097
5098 if (*buf != NUL)
5099 for (i = 0; i < num_file; ++i)
5100 {
5101 len = (int)STRLEN(file[i]) - 3;
5102 if (len <= 0)
5103 continue;
5104 if (STRCMP(file[i] + len, buf) == 0)
5105 {
5106 /* remove the default language */
5107 file[i][len] = NUL;
5108 }
5109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110}
5111#endif
5112
5113/*
5114 * Do the expansion based on xp->xp_context and "pat".
5115 */
5116 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005117ExpandFromContext(
5118 expand_T *xp,
5119 char_u *pat,
5120 int *num_file,
5121 char_u ***file,
5122 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123{
5124#ifdef FEAT_CMDL_COMPL
5125 regmatch_T regmatch;
5126#endif
5127 int ret;
5128 int flags;
5129
5130 flags = EW_DIR; /* include directories */
5131 if (options & WILD_LIST_NOTFOUND)
5132 flags |= EW_NOTFOUND;
5133 if (options & WILD_ADD_SLASH)
5134 flags |= EW_ADDSLASH;
5135 if (options & WILD_KEEP_ALL)
5136 flags |= EW_KEEPALL;
5137 if (options & WILD_SILENT)
5138 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005139 if (options & WILD_ALLLINKS)
5140 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005142 if (xp->xp_context == EXPAND_FILES
5143 || xp->xp_context == EXPAND_DIRECTORIES
5144 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
5146 /*
5147 * Expand file or directory names.
5148 */
5149 int free_pat = FALSE;
5150 int i;
5151
5152 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5153 * space */
5154 if (xp->xp_backslash != XP_BS_NONE)
5155 {
5156 free_pat = TRUE;
5157 pat = vim_strsave(pat);
5158 for (i = 0; pat[i]; ++i)
5159 if (pat[i] == '\\')
5160 {
5161 if (xp->xp_backslash == XP_BS_THREE
5162 && pat[i + 1] == '\\'
5163 && pat[i + 2] == '\\'
5164 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005165 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 if (xp->xp_backslash == XP_BS_ONE
5167 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005168 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 }
5171
5172 if (xp->xp_context == EXPAND_FILES)
5173 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005174 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5175 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 else
5177 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005178 if (options & WILD_ICASE)
5179 flags |= EW_ICASE;
5180
Bram Moolenaard7834d32009-12-02 16:14:36 +00005181 /* Expand wildcards, supporting %:h and the like. */
5182 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 if (free_pat)
5184 vim_free(pat);
5185 return ret;
5186 }
5187
5188 *file = (char_u **)"";
5189 *num_file = 0;
5190 if (xp->xp_context == EXPAND_HELP)
5191 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005192 /* With an empty argument we would get all the help tags, which is
5193 * very slow. Get matches for "help" instead. */
5194 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5195 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
5197#ifdef FEAT_MULTI_LANG
5198 cleanup_help_tags(*num_file, *file);
5199#endif
5200 return OK;
5201 }
5202 return FAIL;
5203 }
5204
5205#ifndef FEAT_CMDL_COMPL
5206 return FAIL;
5207#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005208 if (xp->xp_context == EXPAND_SHELLCMD)
5209 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 if (xp->xp_context == EXPAND_OLD_SETTING)
5211 return ExpandOldSetting(num_file, file);
5212 if (xp->xp_context == EXPAND_BUFFERS)
5213 return ExpandBufnames(pat, num_file, file, options);
5214 if (xp->xp_context == EXPAND_TAGS
5215 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5216 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5217 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005218 {
5219 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005220 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5221 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005224 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005225 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005226 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005227 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005228 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005229 {
5230 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005231 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005232 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005233 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005234 {
5235 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005236 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005237 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005238# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5239 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005240 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005241# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005242 if (xp->xp_context == EXPAND_PACKADD)
5243 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5246 if (regmatch.regprog == NULL)
5247 return FAIL;
5248
5249 /* set ignore-case according to p_ic, p_scs and pat */
5250 regmatch.rm_ic = ignorecase(pat);
5251
5252 if (xp->xp_context == EXPAND_SETTINGS
5253 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5254 ret = ExpandSettings(xp, &regmatch, num_file, file);
5255 else if (xp->xp_context == EXPAND_MAPPINGS)
5256 ret = ExpandMappings(&regmatch, num_file, file);
5257# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5258 else if (xp->xp_context == EXPAND_USER_DEFINED)
5259 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5260# endif
5261 else
5262 {
5263 static struct expgen
5264 {
5265 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005266 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005268 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 } tab[] =
5270 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005271 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5272 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005273 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005274 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005275#ifdef FEAT_CMDHIST
5276 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005279 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005280 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005281 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5282 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5283 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284#endif
5285#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005286 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5287 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5288 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5289 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290#endif
5291#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005292 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5293 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294#endif
5295#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005296 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005298#ifdef FEAT_PROFILE
5299 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5300#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005301 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005302 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5303 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005304#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005305 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005306#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005307#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005308 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005309#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005310#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005311 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5314 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005315 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5316 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005318 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005319 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005320 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 };
5322 int i;
5323
5324 /*
5325 * Find a context in the table and call the ExpandGeneric() with the
5326 * right function to do the expansion.
5327 */
5328 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005329 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 if (xp->xp_context == tab[i].context)
5331 {
5332 if (tab[i].ic)
5333 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005334 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005335 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 break;
5337 }
5338 }
5339
Bram Moolenaar473de612013-06-08 18:19:48 +02005340 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341
5342 return ret;
5343#endif /* FEAT_CMDL_COMPL */
5344}
5345
5346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5347/*
5348 * Expand a list of names.
5349 *
5350 * Generic function for command line completion. It calls a function to
5351 * obtain strings, one by one. The strings are matched against a regexp
5352 * program. Matching strings are copied into an array, which is returned.
5353 *
5354 * Returns OK when no problems encountered, FAIL for error (out of memory).
5355 */
5356 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005357ExpandGeneric(
5358 expand_T *xp,
5359 regmatch_T *regmatch,
5360 int *num_file,
5361 char_u ***file,
5362 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005364 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365{
5366 int i;
5367 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005368 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 char_u *str;
5370
5371 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005372 * round == 0: count the number of matching names
5373 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005375 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 for (i = 0; ; ++i)
5378 {
5379 str = (*func)(xp, i);
5380 if (str == NULL) /* end of list */
5381 break;
5382 if (*str == NUL) /* skip empty strings */
5383 continue;
5384
5385 if (vim_regexec(regmatch, str, (colnr_T)0))
5386 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005387 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005389 if (escaped)
5390 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5391 else
5392 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 (*file)[count] = str;
5394#ifdef FEAT_MENU
5395 if (func == get_menu_names && str != NULL)
5396 {
5397 /* test for separator added by get_menu_names() */
5398 str += STRLEN(str) - 1;
5399 if (*str == '\001')
5400 *str = '.';
5401 }
5402#endif
5403 }
5404 ++count;
5405 }
5406 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005407 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 if (count == 0)
5410 return OK;
5411 *num_file = count;
5412 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5413 if (*file == NULL)
5414 {
5415 *file = (char_u **)"";
5416 return FAIL;
5417 }
5418 count = 0;
5419 }
5420 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005421
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005422 /* Sort the results. Keep menu's in the specified order. */
5423 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005424 {
5425 if (xp->xp_context == EXPAND_EXPRESSION
5426 || xp->xp_context == EXPAND_FUNCTIONS
5427 || xp->xp_context == EXPAND_USER_FUNC)
5428 /* <SNR> functions should be sorted to the end. */
5429 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5430 sort_func_compare);
5431 else
5432 sort_strings(*file, *num_file);
5433 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005434
Bram Moolenaar4f688582007-07-24 12:34:30 +00005435#ifdef FEAT_CMDL_COMPL
5436 /* Reset the variables used for special highlight names expansion, so that
5437 * they don't show up when getting normal highlight names by ID. */
5438 reset_expand_highlight();
5439#endif
5440
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 return OK;
5442}
5443
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005444/*
5445 * Complete a shell command.
5446 * Returns FAIL or OK;
5447 */
5448 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005449expand_shellcmd(
5450 char_u *filepat, /* pattern to match with command names */
5451 int *num_file, /* return: number of matches */
5452 char_u ***file, /* return: array with matches */
5453 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005454{
5455 char_u *pat;
5456 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005457 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005458 int mustfree = FALSE;
5459 garray_T ga;
5460 char_u *buf = alloc(MAXPATHL);
5461 size_t l;
5462 char_u *s, *e;
5463 int flags = flagsarg;
5464 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005465 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005466 hashtab_T found_ht;
5467 hashitem_T *hi;
5468 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005469
5470 if (buf == NULL)
5471 return FAIL;
5472
5473 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5474 * space */
5475 pat = vim_strsave(filepat);
5476 for (i = 0; pat[i]; ++i)
5477 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005478 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005479
Bram Moolenaarb5971142015-03-21 17:32:19 +01005480 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005481
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005482 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5483 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005484 path = (char_u *)".";
5485 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005486 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005487 /* For an absolute name we don't use $PATH. */
5488 if (!mch_isFullName(pat))
5489 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005490 if (path == NULL)
5491 path = (char_u *)"";
5492 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005493
5494 /*
5495 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005496 * collect them in "ga". When "." is not in $PATH also expand for the
5497 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005498 */
5499 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005500 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005501 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005502 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005503#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005504 e = vim_strchr(s, ';');
5505#else
5506 e = vim_strchr(s, ':');
5507#endif
5508 if (e == NULL)
5509 e = s + STRLEN(s);
5510
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005511 if (*s == NUL)
5512 {
5513 if (did_curdir)
5514 break;
5515 // Find directories in the current directory, path is empty.
5516 did_curdir = TRUE;
5517 flags |= EW_DIR;
5518 }
5519 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5520 {
5521 did_curdir = TRUE;
5522 flags |= EW_DIR;
5523 }
5524 else
5525 // Do not match directories inside a $PATH item.
5526 flags &= ~EW_DIR;
5527
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005528 l = e - s;
5529 if (l > MAXPATHL - 5)
5530 break;
5531 vim_strncpy(buf, s, l);
5532 add_pathsep(buf);
5533 l = STRLEN(buf);
5534 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5535
5536 /* Expand matches in one directory of $PATH. */
5537 ret = expand_wildcards(1, &buf, num_file, file, flags);
5538 if (ret == OK)
5539 {
5540 if (ga_grow(&ga, *num_file) == FAIL)
5541 FreeWild(*num_file, *file);
5542 else
5543 {
5544 for (i = 0; i < *num_file; ++i)
5545 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005546 char_u *name = (*file)[i];
5547
5548 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005549 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005550 // Check if this name was already found.
5551 hash = hash_hash(name + l);
5552 hi = hash_lookup(&found_ht, name + l, hash);
5553 if (HASHITEM_EMPTY(hi))
5554 {
5555 // Remove the path that was prepended.
5556 STRMOVE(name, name + l);
5557 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5558 hash_add_item(&found_ht, hi, name, hash);
5559 name = NULL;
5560 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005561 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005562 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005563 }
5564 vim_free(*file);
5565 }
5566 }
5567 if (*e != NUL)
5568 ++e;
5569 }
5570 *file = ga.ga_data;
5571 *num_file = ga.ga_len;
5572
5573 vim_free(buf);
5574 vim_free(pat);
5575 if (mustfree)
5576 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005577 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005578 return OK;
5579}
5580
5581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5583/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005584 * Call "user_expand_func()" to invoke a user defined Vim script function and
5585 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005587 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005589 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005590 expand_T *xp,
5591 int *num_file,
5592 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005594 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005595 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005596 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005597 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005598 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005599 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600
Bram Moolenaarb7515462013-06-29 12:58:33 +02005601 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005602 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 *num_file = 0;
5604 *file = NULL;
5605
Bram Moolenaarb7515462013-06-29 12:58:33 +02005606 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005607 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005608 keep = ccline.cmdbuff[ccline.cmdlen];
5609 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005610 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005611
Bram Moolenaarffa96842018-06-12 22:05:14 +02005612 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5613
5614 args[0].v_type = VAR_STRING;
5615 args[0].vval.v_string = pat;
5616 args[1].v_type = VAR_STRING;
5617 args[1].vval.v_string = xp->xp_line;
5618 args[2].v_type = VAR_NUMBER;
5619 args[2].vval.v_number = xp->xp_col;
5620 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005622 /* Save the cmdline, we don't know what the function may do. */
5623 save_ccline = ccline;
5624 ccline.cmdbuff = NULL;
5625 ccline.cmdprompt = NULL;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005626 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005627
Bram Moolenaarded27a12018-08-01 19:06:03 +02005628 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005629
5630 ccline = save_ccline;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005631 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005632 if (ccline.cmdbuff != NULL)
5633 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005634
Bram Moolenaarffa96842018-06-12 22:05:14 +02005635 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005636 return ret;
5637}
5638
5639/*
5640 * Expand names with a function defined by the user.
5641 */
5642 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005643ExpandUserDefined(
5644 expand_T *xp,
5645 regmatch_T *regmatch,
5646 int *num_file,
5647 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005648{
5649 char_u *retstr;
5650 char_u *s;
5651 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005652 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005653 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005654 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005655
5656 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5657 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 return FAIL;
5659
5660 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005661 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 e = vim_strchr(s, '\n');
5664 if (e == NULL)
5665 e = s + STRLEN(s);
5666 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005667 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005669 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5670 *e = keep;
5671
5672 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005674 if (ga_grow(&ga, 1) == FAIL)
5675 break;
5676 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5677 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 }
5679
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 if (*e != NUL)
5681 ++e;
5682 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005683 vim_free(retstr);
5684 *file = ga.ga_data;
5685 *num_file = ga.ga_len;
5686 return OK;
5687}
5688
5689/*
5690 * Expand names with a list returned by a function defined by the user.
5691 */
5692 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693ExpandUserList(
5694 expand_T *xp,
5695 int *num_file,
5696 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005697{
5698 list_T *retlist;
5699 listitem_T *li;
5700 garray_T ga;
5701
5702 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5703 if (retlist == NULL)
5704 return FAIL;
5705
5706 ga_init2(&ga, (int)sizeof(char *), 3);
5707 /* Loop over the items in the list. */
5708 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5709 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005710 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5711 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005712
5713 if (ga_grow(&ga, 1) == FAIL)
5714 break;
5715
5716 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005717 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005718 ++ga.ga_len;
5719 }
5720 list_unref(retlist);
5721
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 *file = ga.ga_data;
5723 *num_file = ga.ga_len;
5724 return OK;
5725}
5726#endif
5727
5728/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005729 * Expand color scheme, compiler or filetype names.
5730 * Search from 'runtimepath':
5731 * 'runtimepath'/{dirnames}/{pat}.vim
5732 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5733 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5734 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5735 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005736 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 */
5738 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005739ExpandRTDir(
5740 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005741 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005742 int *num_file,
5743 char_u ***file,
5744 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 char_u *s;
5747 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005748 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005750 int i;
5751 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752
5753 *num_file = 0;
5754 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005755 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005756 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005757
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005758 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005760 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5761 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005763 ga_clear_strings(&ga);
5764 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005766 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005767 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005768 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005770
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005771 if (flags & DIP_START) {
5772 for (i = 0; dirnames[i] != NULL; ++i)
5773 {
5774 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5775 if (s == NULL)
5776 {
5777 ga_clear_strings(&ga);
5778 return FAIL;
5779 }
5780 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5781 globpath(p_pp, s, &ga, 0);
5782 vim_free(s);
5783 }
5784 }
5785
5786 if (flags & DIP_OPT) {
5787 for (i = 0; dirnames[i] != NULL; ++i)
5788 {
5789 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5790 if (s == NULL)
5791 {
5792 ga_clear_strings(&ga);
5793 return FAIL;
5794 }
5795 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5796 globpath(p_pp, s, &ga, 0);
5797 vim_free(s);
5798 }
5799 }
5800
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005801 for (i = 0; i < ga.ga_len; ++i)
5802 {
5803 match = ((char_u **)ga.ga_data)[i];
5804 s = match;
5805 e = s + STRLEN(s);
5806 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5807 {
5808 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005809 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005810 if (s < match || vim_ispathsep(*s))
5811 break;
5812 ++s;
5813 *e = NUL;
5814 mch_memmove(match, s, e - s + 1);
5815 }
5816 }
5817
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005818 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005819 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005820
5821 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005822 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005823 remove_duplicates(&ga);
5824
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 *file = ga.ga_data;
5826 *num_file = ga.ga_len;
5827 return OK;
5828}
5829
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005830/*
5831 * Expand loadplugin names:
5832 * 'packpath'/pack/ * /opt/{pat}
5833 */
5834 static int
5835ExpandPackAddDir(
5836 char_u *pat,
5837 int *num_file,
5838 char_u ***file)
5839{
5840 char_u *s;
5841 char_u *e;
5842 char_u *match;
5843 garray_T ga;
5844 int i;
5845 int pat_len;
5846
5847 *num_file = 0;
5848 *file = NULL;
5849 pat_len = (int)STRLEN(pat);
5850 ga_init2(&ga, (int)sizeof(char *), 10);
5851
5852 s = alloc((unsigned)(pat_len + 26));
5853 if (s == NULL)
5854 {
5855 ga_clear_strings(&ga);
5856 return FAIL;
5857 }
5858 sprintf((char *)s, "pack/*/opt/%s*", pat);
5859 globpath(p_pp, s, &ga, 0);
5860 vim_free(s);
5861
5862 for (i = 0; i < ga.ga_len; ++i)
5863 {
5864 match = ((char_u **)ga.ga_data)[i];
5865 s = gettail(match);
5866 e = s + STRLEN(s);
5867 mch_memmove(match, s, e - s + 1);
5868 }
5869
5870 if (ga.ga_len == 0)
5871 return FAIL;
5872
5873 /* Sort and remove duplicates which can happen when specifying multiple
5874 * directories in dirnames. */
5875 remove_duplicates(&ga);
5876
5877 *file = ga.ga_data;
5878 *num_file = ga.ga_len;
5879 return OK;
5880}
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882#endif
5883
5884#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5885/*
5886 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005887 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005889 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005890globpath(
5891 char_u *path,
5892 char_u *file,
5893 garray_T *ga,
5894 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 expand_T xpc;
5897 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 int num_p;
5900 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901
5902 buf = alloc(MAXPATHL);
5903 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005904 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005906 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005908
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 /* Loop over all entries in {path}. */
5910 while (*path != NUL)
5911 {
5912 /* Copy one item of the path to buf[] and concatenate the file name. */
5913 copy_option_part(&path, buf, MAXPATHL, ",");
5914 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5915 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005916# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005917 /* Using the platform's path separator (\) makes vim incorrectly
5918 * treat it as an escape character, use '/' instead. */
5919 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5920 STRCAT(buf, "/");
5921# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005923# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005925 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5926 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005928 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005930 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 for (i = 0; i < num_p; ++i)
5933 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005934 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005935 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005936 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005939
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 FreeWild(num_p, p);
5941 }
5942 }
5943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
5945 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946}
5947
5948#endif
5949
5950#if defined(FEAT_CMDHIST) || defined(PROTO)
5951
5952/*********************************
5953 * Command line history stuff *
5954 *********************************/
5955
5956/*
5957 * Translate a history character to the associated type number.
5958 */
5959 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005960hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 if (c == ':')
5963 return HIST_CMD;
5964 if (c == '=')
5965 return HIST_EXPR;
5966 if (c == '@')
5967 return HIST_INPUT;
5968 if (c == '>')
5969 return HIST_DEBUG;
5970 return HIST_SEARCH; /* must be '?' or '/' */
5971}
5972
5973/*
5974 * Table of history names.
5975 * These names are used in :history and various hist...() functions.
5976 * It is sufficient to give the significant prefix of a history name.
5977 */
5978
5979static char *(history_names[]) =
5980{
5981 "cmd",
5982 "search",
5983 "expr",
5984 "input",
5985 "debug",
5986 NULL
5987};
5988
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005989#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5990/*
5991 * Function given to ExpandGeneric() to obtain the possible first
5992 * arguments of the ":history command.
5993 */
5994 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005995get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005996{
5997 static char_u compl[2] = { NUL, NUL };
5998 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005999 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02006000 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
6001
6002 if (idx < short_names_count)
6003 {
6004 compl[0] = (char_u)short_names[idx];
6005 return compl;
6006 }
6007 if (idx < short_names_count + history_name_count)
6008 return (char_u *)history_names[idx - short_names_count];
6009 if (idx == short_names_count + history_name_count)
6010 return (char_u *)"all";
6011 return NULL;
6012}
6013#endif
6014
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015/*
6016 * init_history() - Initialize the command line history.
6017 * Also used to re-allocate the history when the size changes.
6018 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006019 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006020init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021{
6022 int newlen; /* new length of history table */
6023 histentry_T *temp;
6024 int i;
6025 int j;
6026 int type;
6027
6028 /*
6029 * If size of history table changed, reallocate it
6030 */
6031 newlen = (int)p_hi;
6032 if (newlen != hislen) /* history length changed */
6033 {
6034 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6035 {
6036 if (newlen)
6037 {
6038 temp = (histentry_T *)lalloc(
6039 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6040 if (temp == NULL) /* out of memory! */
6041 {
6042 if (type == 0) /* first one: just keep the old length */
6043 {
6044 newlen = hislen;
6045 break;
6046 }
6047 /* Already changed one table, now we can only have zero
6048 * length for all tables. */
6049 newlen = 0;
6050 type = -1;
6051 continue;
6052 }
6053 }
6054 else
6055 temp = NULL;
6056 if (newlen == 0 || temp != NULL)
6057 {
6058 if (hisidx[type] < 0) /* there are no entries yet */
6059 {
6060 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006061 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 }
6063 else if (newlen > hislen) /* array becomes bigger */
6064 {
6065 for (i = 0; i <= hisidx[type]; ++i)
6066 temp[i] = history[type][i];
6067 j = i;
6068 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006069 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070 for ( ; j < hislen; ++i, ++j)
6071 temp[i] = history[type][j];
6072 }
6073 else /* array becomes smaller or 0 */
6074 {
6075 j = hisidx[type];
6076 for (i = newlen - 1; ; --i)
6077 {
6078 if (i >= 0) /* copy newest entries */
6079 temp[i] = history[type][j];
6080 else /* remove older entries */
6081 vim_free(history[type][j].hisstr);
6082 if (--j < 0)
6083 j = hislen - 1;
6084 if (j == hisidx[type])
6085 break;
6086 }
6087 hisidx[type] = newlen - 1;
6088 }
6089 vim_free(history[type]);
6090 history[type] = temp;
6091 }
6092 }
6093 hislen = newlen;
6094 }
6095}
6096
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006097 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006098clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006099{
6100 hisptr->hisnum = 0;
6101 hisptr->viminfo = FALSE;
6102 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006103 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006104}
6105
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106/*
6107 * Check if command line 'str' is already in history.
6108 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6109 */
6110 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006111in_history(
6112 int type,
6113 char_u *str,
6114 int move_to_front, /* Move the entry to the front if it exists */
6115 int sep,
6116 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 int i;
6119 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006120 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121
6122 if (hisidx[type] < 0)
6123 return FALSE;
6124 i = hisidx[type];
6125 do
6126 {
6127 if (history[type][i].hisstr == NULL)
6128 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006129
6130 /* For search history, check that the separator character matches as
6131 * well. */
6132 p = history[type][i].hisstr;
6133 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006134 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006135 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 {
6137 if (!move_to_front)
6138 return TRUE;
6139 last_i = i;
6140 break;
6141 }
6142 if (--i < 0)
6143 i = hislen - 1;
6144 } while (i != hisidx[type]);
6145
6146 if (last_i >= 0)
6147 {
6148 str = history[type][i].hisstr;
6149 while (i != hisidx[type])
6150 {
6151 if (++i >= hislen)
6152 i = 0;
6153 history[type][last_i] = history[type][i];
6154 last_i = i;
6155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006157 history[type][i].viminfo = FALSE;
6158 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006159 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 return TRUE;
6161 }
6162 return FALSE;
6163}
6164
6165/*
6166 * Convert history name (from table above) to its HIST_ equivalent.
6167 * When "name" is empty, return "cmd" history.
6168 * Returns -1 for unknown history name.
6169 */
6170 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006171get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 int i;
6174 int len = (int)STRLEN(name);
6175
6176 /* No argument: use current history. */
6177 if (len == 0)
6178 return hist_char2type(ccline.cmdfirstc);
6179
6180 for (i = 0; history_names[i] != NULL; ++i)
6181 if (STRNICMP(name, history_names[i], len) == 0)
6182 return i;
6183
6184 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6185 return hist_char2type(name[0]);
6186
6187 return -1;
6188}
6189
6190static int last_maptick = -1; /* last seen maptick */
6191
6192/*
6193 * Add the given string to the given history. If the string is already in the
6194 * history then it is moved to the front. "histype" may be one of he HIST_
6195 * values.
6196 */
6197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006198add_to_history(
6199 int histype,
6200 char_u *new_entry,
6201 int in_map, /* consider maptick when inside a mapping */
6202 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 histentry_T *hisptr;
6205 int len;
6206
6207 if (hislen == 0) /* no history */
6208 return;
6209
Bram Moolenaara939e432013-11-09 05:30:26 +01006210 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6211 return;
6212
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 /*
6214 * Searches inside the same mapping overwrite each other, so that only
6215 * the last line is kept. Be careful not to remove a line that was moved
6216 * down, only lines that were added.
6217 */
6218 if (histype == HIST_SEARCH && in_map)
6219 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006220 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
6222 /* Current line is from the same mapping, remove it */
6223 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6224 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006225 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 --hisnum[histype];
6227 if (--hisidx[HIST_SEARCH] < 0)
6228 hisidx[HIST_SEARCH] = hislen - 1;
6229 }
6230 last_maptick = -1;
6231 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006232 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 {
6234 if (++hisidx[histype] == hislen)
6235 hisidx[histype] = 0;
6236 hisptr = &history[histype][hisidx[histype]];
6237 vim_free(hisptr->hisstr);
6238
6239 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006240 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6242 if (hisptr->hisstr != NULL)
6243 hisptr->hisstr[len + 1] = sep;
6244
6245 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006246 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006247 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006248 if (histype == HIST_SEARCH && in_map)
6249 last_maptick = maptick;
6250 }
6251}
6252
6253#if defined(FEAT_EVAL) || defined(PROTO)
6254
6255/*
6256 * Get identifier of newest history entry.
6257 * "histype" may be one of the HIST_ values.
6258 */
6259 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006260get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6263 || hisidx[histype] < 0)
6264 return -1;
6265
6266 return history[histype][hisidx[histype]].hisnum;
6267}
6268
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006270 * Calculate history index from a number:
6271 * num > 0: seen as identifying number of a history entry
6272 * num < 0: relative position in history wrt newest entry
6273 * "histype" may be one of the HIST_ values.
6274 */
6275 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006276calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277{
6278 int i;
6279 histentry_T *hist;
6280 int wrapped = FALSE;
6281
6282 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6283 || (i = hisidx[histype]) < 0 || num == 0)
6284 return -1;
6285
6286 hist = history[histype];
6287 if (num > 0)
6288 {
6289 while (hist[i].hisnum > num)
6290 if (--i < 0)
6291 {
6292 if (wrapped)
6293 break;
6294 i += hislen;
6295 wrapped = TRUE;
6296 }
6297 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6298 return i;
6299 }
6300 else if (-num <= hislen)
6301 {
6302 i += num + 1;
6303 if (i < 0)
6304 i += hislen;
6305 if (hist[i].hisstr != NULL)
6306 return i;
6307 }
6308 return -1;
6309}
6310
6311/*
6312 * Get a history entry by its index.
6313 * "histype" may be one of the HIST_ values.
6314 */
6315 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 idx = calc_hist_idx(histype, idx);
6319 if (idx >= 0)
6320 return history[histype][idx].hisstr;
6321 else
6322 return (char_u *)"";
6323}
6324
6325/*
6326 * Clear all entries of a history.
6327 * "histype" may be one of the HIST_ values.
6328 */
6329 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006330clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 int i;
6333 histentry_T *hisptr;
6334
6335 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6336 {
6337 hisptr = history[histype];
6338 for (i = hislen; i--;)
6339 {
6340 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006341 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006342 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 }
6344 hisidx[histype] = -1; /* mark history as cleared */
6345 hisnum[histype] = 0; /* reset identifier counter */
6346 return OK;
6347 }
6348 return FAIL;
6349}
6350
6351/*
6352 * Remove all entries matching {str} from a history.
6353 * "histype" may be one of the HIST_ values.
6354 */
6355 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006356del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357{
6358 regmatch_T regmatch;
6359 histentry_T *hisptr;
6360 int idx;
6361 int i;
6362 int last;
6363 int found = FALSE;
6364
6365 regmatch.regprog = NULL;
6366 regmatch.rm_ic = FALSE; /* always match case */
6367 if (hislen != 0
6368 && histype >= 0
6369 && histype < HIST_COUNT
6370 && *str != NUL
6371 && (idx = hisidx[histype]) >= 0
6372 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6373 != NULL)
6374 {
6375 i = last = idx;
6376 do
6377 {
6378 hisptr = &history[histype][i];
6379 if (hisptr->hisstr == NULL)
6380 break;
6381 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6382 {
6383 found = TRUE;
6384 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006385 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 }
6387 else
6388 {
6389 if (i != last)
6390 {
6391 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006392 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 }
6394 if (--last < 0)
6395 last += hislen;
6396 }
6397 if (--i < 0)
6398 i += hislen;
6399 } while (i != idx);
6400 if (history[histype][idx].hisstr == NULL)
6401 hisidx[histype] = -1;
6402 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006403 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404 return found;
6405}
6406
6407/*
6408 * Remove an indexed entry from a history.
6409 * "histype" may be one of the HIST_ values.
6410 */
6411 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006412del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413{
6414 int i, j;
6415
6416 i = calc_hist_idx(histype, idx);
6417 if (i < 0)
6418 return FALSE;
6419 idx = hisidx[histype];
6420 vim_free(history[histype][i].hisstr);
6421
6422 /* When deleting the last added search string in a mapping, reset
6423 * last_maptick, so that the last added search string isn't deleted again.
6424 */
6425 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6426 last_maptick = -1;
6427
6428 while (i != idx)
6429 {
6430 j = (i + 1) % hislen;
6431 history[histype][i] = history[histype][j];
6432 i = j;
6433 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006434 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006435 if (--i < 0)
6436 i += hislen;
6437 hisidx[histype] = i;
6438 return TRUE;
6439}
6440
6441#endif /* FEAT_EVAL */
6442
6443#if defined(FEAT_CRYPT) || defined(PROTO)
6444/*
6445 * Very specific function to remove the value in ":set key=val" from the
6446 * history.
6447 */
6448 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006449remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450{
6451 char_u *p;
6452 int i;
6453
6454 i = hisidx[HIST_CMD];
6455 if (i < 0)
6456 return;
6457 p = history[HIST_CMD][i].hisstr;
6458 if (p != NULL)
6459 for ( ; *p; ++p)
6460 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6461 {
6462 p = vim_strchr(p + 3, '=');
6463 if (p == NULL)
6464 break;
6465 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006466 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 if (p[i] == '\\' && p[i + 1])
6468 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006469 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470 --p;
6471 }
6472}
6473#endif
6474
6475#endif /* FEAT_CMDHIST */
6476
Bram Moolenaar064154c2016-03-19 22:50:43 +01006477#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006478/*
6479 * Get pointer to the command line info to use. cmdline_paste() may clear
6480 * ccline and put the previous value in prev_ccline.
6481 */
6482 static struct cmdline_info *
6483get_ccline_ptr(void)
6484{
6485 if ((State & CMDLINE) == 0)
6486 return NULL;
6487 if (ccline.cmdbuff != NULL)
6488 return &ccline;
6489 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6490 return &prev_ccline;
6491 return NULL;
6492}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006493#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006494
Bram Moolenaar064154c2016-03-19 22:50:43 +01006495#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006496/*
6497 * Get the current command line in allocated memory.
6498 * Only works when the command line is being edited.
6499 * Returns NULL when something is wrong.
6500 */
6501 char_u *
6502get_cmdline_str(void)
6503{
6504 struct cmdline_info *p = get_ccline_ptr();
6505
6506 if (p == NULL)
6507 return NULL;
6508 return vim_strnsave(p->cmdbuff, p->cmdlen);
6509}
6510
6511/*
6512 * Get the current command line position, counted in bytes.
6513 * Zero is the first position.
6514 * Only works when the command line is being edited.
6515 * Returns -1 when something is wrong.
6516 */
6517 int
6518get_cmdline_pos(void)
6519{
6520 struct cmdline_info *p = get_ccline_ptr();
6521
6522 if (p == NULL)
6523 return -1;
6524 return p->cmdpos;
6525}
6526
6527/*
6528 * Set the command line byte position to "pos". Zero is the first position.
6529 * Only works when the command line is being edited.
6530 * Returns 1 when failed, 0 when OK.
6531 */
6532 int
6533set_cmdline_pos(
6534 int pos)
6535{
6536 struct cmdline_info *p = get_ccline_ptr();
6537
6538 if (p == NULL)
6539 return 1;
6540
6541 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6542 * changed the command line. */
6543 if (pos < 0)
6544 new_cmdpos = 0;
6545 else
6546 new_cmdpos = pos;
6547 return 0;
6548}
6549#endif
6550
6551#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6552/*
6553 * Get the current command-line type.
6554 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6555 * Only works when the command line is being edited.
6556 * Returns NUL when something is wrong.
6557 */
6558 int
6559get_cmdline_type(void)
6560{
6561 struct cmdline_info *p = get_ccline_ptr();
6562
6563 if (p == NULL)
6564 return NUL;
6565 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006566 return
6567# ifdef FEAT_EVAL
6568 (p->input_fn) ? '@' :
6569# endif
6570 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006571 return p->cmdfirstc;
6572}
6573#endif
6574
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6576/*
6577 * Get indices "num1,num2" that specify a range within a list (not a range of
6578 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6579 * Returns OK if parsed successfully, otherwise FAIL.
6580 */
6581 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006582get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583{
6584 int len;
6585 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006586 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587
6588 *str = skipwhite(*str);
6589 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6590 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006591 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 *str += len;
6593 *num1 = (int)num;
6594 first = TRUE;
6595 }
6596 *str = skipwhite(*str);
6597 if (**str == ',') /* parse "to" part of range */
6598 {
6599 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006600 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (len > 0)
6602 {
6603 *num2 = (int)num;
6604 *str = skipwhite(*str + len);
6605 }
6606 else if (!first) /* no number given at all */
6607 return FAIL;
6608 }
6609 else if (first) /* only one number given */
6610 *num2 = *num1;
6611 return OK;
6612}
6613#endif
6614
6615#if defined(FEAT_CMDHIST) || defined(PROTO)
6616/*
6617 * :history command - print a history
6618 */
6619 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006620ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 histentry_T *hist;
6623 int histype1 = HIST_CMD;
6624 int histype2 = HIST_CMD;
6625 int hisidx1 = 1;
6626 int hisidx2 = -1;
6627 int idx;
6628 int i, j, k;
6629 char_u *end;
6630 char_u *arg = eap->arg;
6631
6632 if (hislen == 0)
6633 {
6634 MSG(_("'history' option is zero"));
6635 return;
6636 }
6637
6638 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6639 {
6640 end = arg;
6641 while (ASCII_ISALPHA(*end)
6642 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6643 end++;
6644 i = *end;
6645 *end = NUL;
6646 histype1 = get_histtype(arg);
6647 if (histype1 == -1)
6648 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006649 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 {
6651 histype1 = 0;
6652 histype2 = HIST_COUNT-1;
6653 }
6654 else
6655 {
6656 *end = i;
6657 EMSG(_(e_trailing));
6658 return;
6659 }
6660 }
6661 else
6662 histype2 = histype1;
6663 *end = i;
6664 }
6665 else
6666 end = arg;
6667 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6668 {
6669 EMSG(_(e_trailing));
6670 return;
6671 }
6672
6673 for (; !got_int && histype1 <= histype2; ++histype1)
6674 {
6675 STRCPY(IObuff, "\n # ");
6676 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6677 MSG_PUTS_TITLE(IObuff);
6678 idx = hisidx[histype1];
6679 hist = history[histype1];
6680 j = hisidx1;
6681 k = hisidx2;
6682 if (j < 0)
6683 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6684 if (k < 0)
6685 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6686 if (idx >= 0 && j <= k)
6687 for (i = idx + 1; !got_int; ++i)
6688 {
6689 if (i == hislen)
6690 i = 0;
6691 if (hist[i].hisstr != NULL
6692 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6693 {
6694 msg_putchar('\n');
6695 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6696 hist[i].hisnum);
6697 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6698 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006699 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 else
6701 STRCAT(IObuff, hist[i].hisstr);
6702 msg_outtrans(IObuff);
6703 out_flush();
6704 }
6705 if (i == idx)
6706 break;
6707 }
6708 }
6709}
6710#endif
6711
6712#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006713/*
6714 * Buffers for history read from a viminfo file. Only valid while reading.
6715 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006716static histentry_T *viminfo_history[HIST_COUNT] =
6717 {NULL, NULL, NULL, NULL, NULL};
6718static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6719static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720static int viminfo_add_at_front = FALSE;
6721
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006722static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723
6724/*
6725 * Translate a history type number to the associated character.
6726 */
6727 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006728hist_type2char(
6729 int type,
6730 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 if (type == HIST_CMD)
6733 return ':';
6734 if (type == HIST_SEARCH)
6735 {
6736 if (use_question)
6737 return '?';
6738 else
6739 return '/';
6740 }
6741 if (type == HIST_EXPR)
6742 return '=';
6743 return '@';
6744}
6745
6746/*
6747 * Prepare for reading the history from the viminfo file.
6748 * This allocates history arrays to store the read history lines.
6749 */
6750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006751prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752{
6753 int i;
6754 int num;
6755 int type;
6756 int len;
6757
6758 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006759 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (asklen > hislen)
6761 asklen = hislen;
6762
6763 for (type = 0; type < HIST_COUNT; ++type)
6764 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006765 /* Count the number of empty spaces in the history list. Entries read
6766 * from viminfo previously are also considered empty. If there are
6767 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006769 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006770 num++;
6771 len = asklen;
6772 if (num > len)
6773 len = num;
6774 if (len <= 0)
6775 viminfo_history[type] = NULL;
6776 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 viminfo_history[type] = (histentry_T *)lalloc(
6778 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 if (viminfo_history[type] == NULL)
6780 len = 0;
6781 viminfo_hislen[type] = len;
6782 viminfo_hisidx[type] = 0;
6783 }
6784}
6785
6786/*
6787 * Accept a line from the viminfo, store it in the history array when it's
6788 * new.
6789 */
6790 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006791read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792{
6793 int type;
6794 long_u len;
6795 char_u *val;
6796 char_u *p;
6797
6798 type = hist_char2type(virp->vir_line[0]);
6799 if (viminfo_hisidx[type] < viminfo_hislen[type])
6800 {
6801 val = viminfo_readstring(virp, 1, TRUE);
6802 if (val != NULL && *val != NUL)
6803 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006804 int sep = (*val == ' ' ? NUL : *val);
6805
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006807 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 {
6809 /* Need to re-allocate to append the separator byte. */
6810 len = STRLEN(val);
6811 p = lalloc(len + 2, TRUE);
6812 if (p != NULL)
6813 {
6814 if (type == HIST_SEARCH)
6815 {
6816 /* Search entry: Move the separator from the first
6817 * column to after the NUL. */
6818 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006819 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821 else
6822 {
6823 /* Not a search entry: No separator in the viminfo
6824 * file, add a NUL separator. */
6825 mch_memmove(p, val, (size_t)len + 1);
6826 p[len + 1] = NUL;
6827 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006828 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6829 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006830 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6831 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006832 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 }
6834 }
6835 }
6836 vim_free(val);
6837 }
6838 return viminfo_readline(virp);
6839}
6840
Bram Moolenaar07219f92013-04-14 23:19:36 +02006841/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842 * Accept a new style history line from the viminfo, store it in the history
6843 * array when it's new.
6844 */
6845 void
6846handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006847 garray_T *values,
6848 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006849{
6850 int type;
6851 long_u len;
6852 char_u *val;
6853 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006854 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006855
6856 /* Check the format:
6857 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006858 if (values->ga_len < 4
6859 || vp[0].bv_type != BVAL_NR
6860 || vp[1].bv_type != BVAL_NR
6861 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6862 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006863 return;
6864
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006865 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006866 if (type >= HIST_COUNT)
6867 return;
6868 if (viminfo_hisidx[type] < viminfo_hislen[type])
6869 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006870 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006871 if (val != NULL && *val != NUL)
6872 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006873 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6874 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006875 int idx;
6876 int overwrite = FALSE;
6877
6878 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6879 {
6880 /* If lines were written by an older Vim we need to avoid
6881 * getting duplicates. See if the entry already exists. */
6882 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6883 {
6884 p = viminfo_history[type][idx].hisstr;
6885 if (STRCMP(val, p) == 0
6886 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6887 {
6888 overwrite = TRUE;
6889 break;
6890 }
6891 }
6892
6893 if (!overwrite)
6894 {
6895 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006896 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006897 p = lalloc(len + 2, TRUE);
6898 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006899 else
6900 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006901 if (p != NULL)
6902 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006903 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006904 if (!overwrite)
6905 {
6906 mch_memmove(p, val, (size_t)len + 1);
6907 /* Put the separator after the NUL. */
6908 p[len + 1] = sep;
6909 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006910 viminfo_history[type][idx].hisnum = 0;
6911 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006912 viminfo_hisidx[type]++;
6913 }
6914 }
6915 }
6916 }
6917 }
6918}
6919
6920/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006921 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006922 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006923 static void
6924concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
6926 int idx;
6927 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006928
6929 idx = hisidx[type] + viminfo_hisidx[type];
6930 if (idx >= hislen)
6931 idx -= hislen;
6932 else if (idx < 0)
6933 idx = hislen - 1;
6934 if (viminfo_add_at_front)
6935 hisidx[type] = idx;
6936 else
6937 {
6938 if (hisidx[type] == -1)
6939 hisidx[type] = hislen - 1;
6940 do
6941 {
6942 if (history[type][idx].hisstr != NULL
6943 || history[type][idx].viminfo)
6944 break;
6945 if (++idx == hislen)
6946 idx = 0;
6947 } while (idx != hisidx[type]);
6948 if (idx != hisidx[type] && --idx < 0)
6949 idx = hislen - 1;
6950 }
6951 for (i = 0; i < viminfo_hisidx[type]; i++)
6952 {
6953 vim_free(history[type][idx].hisstr);
6954 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6955 history[type][idx].viminfo = TRUE;
6956 history[type][idx].time_set = viminfo_history[type][i].time_set;
6957 if (--idx < 0)
6958 idx = hislen - 1;
6959 }
6960 idx += 1;
6961 idx %= hislen;
6962 for (i = 0; i < viminfo_hisidx[type]; i++)
6963 {
6964 history[type][idx++].hisnum = ++hisnum[type];
6965 idx %= hislen;
6966 }
6967}
6968
6969#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6970 static int
6971#ifdef __BORLANDC__
6972_RTLENTRYF
6973#endif
6974sort_hist(const void *s1, const void *s2)
6975{
6976 histentry_T *p1 = *(histentry_T **)s1;
6977 histentry_T *p2 = *(histentry_T **)s2;
6978
6979 if (p1->time_set < p2->time_set) return -1;
6980 if (p1->time_set > p2->time_set) return 1;
6981 return 0;
6982}
6983#endif
6984
6985/*
6986 * Merge history lines from viminfo and lines typed in this Vim based on the
6987 * timestamp;
6988 */
6989 static void
6990merge_history(int type)
6991{
6992 int max_len;
6993 histentry_T **tot_hist;
6994 histentry_T *new_hist;
6995 int i;
6996 int len;
6997
6998 /* Make one long list with all entries. */
6999 max_len = hislen + viminfo_hisidx[type];
7000 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
7001 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
7002 if (tot_hist == NULL || new_hist == NULL)
7003 {
7004 vim_free(tot_hist);
7005 vim_free(new_hist);
7006 return;
7007 }
7008 for (i = 0; i < viminfo_hisidx[type]; i++)
7009 tot_hist[i] = &viminfo_history[type][i];
7010 len = i;
7011 for (i = 0; i < hislen; i++)
7012 if (history[type][i].hisstr != NULL)
7013 tot_hist[len++] = &history[type][i];
7014
7015 /* Sort the list on timestamp. */
7016 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7017
7018 /* Keep the newest ones. */
7019 for (i = 0; i < hislen; i++)
7020 {
7021 if (i < len)
7022 {
7023 new_hist[i] = *tot_hist[i];
7024 tot_hist[i]->hisstr = NULL;
7025 if (new_hist[i].hisnum == 0)
7026 new_hist[i].hisnum = ++hisnum[type];
7027 }
7028 else
7029 clear_hist_entry(&new_hist[i]);
7030 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007031 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007032
7033 /* Free what is not kept. */
7034 for (i = 0; i < viminfo_hisidx[type]; i++)
7035 vim_free(viminfo_history[type][i].hisstr);
7036 for (i = 0; i < hislen; i++)
7037 vim_free(history[type][i].hisstr);
7038 vim_free(history[type]);
7039 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007040 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007041}
7042
7043/*
7044 * Finish reading history lines from viminfo. Not used when writing viminfo.
7045 */
7046 void
7047finish_viminfo_history(vir_T *virp)
7048{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007050 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051
7052 for (type = 0; type < HIST_COUNT; ++type)
7053 {
7054 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007055 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007056
7057 if (merge)
7058 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007060 concat_history(type);
7061
Bram Moolenaard23a8232018-02-10 18:45:26 +01007062 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007063 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 }
7065}
7066
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007067/*
7068 * Write history to viminfo file in "fp".
7069 * When "merge" is TRUE merge history lines with a previously read viminfo
7070 * file, data is in viminfo_history[].
7071 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007074write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075{
7076 int i;
7077 int type;
7078 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007079 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080
7081 init_history();
7082 if (hislen == 0)
7083 return;
7084 for (type = 0; type < HIST_COUNT; ++type)
7085 {
7086 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7087 if (num_saved == 0)
7088 continue;
7089 if (num_saved < 0) /* Use default */
7090 num_saved = hislen;
7091 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7092 type == HIST_CMD ? _("Command Line") :
7093 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007094 type == HIST_EXPR ? _("Expression") :
7095 type == HIST_INPUT ? _("Input Line") :
7096 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097 if (num_saved > hislen)
7098 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007099
7100 /*
7101 * Merge typed and viminfo history:
7102 * round 1: history of typed commands.
7103 * round 2: history from recently read viminfo.
7104 */
7105 for (round = 1; round <= 2; ++round)
7106 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007107 if (round == 1)
7108 /* start at newest entry, somewhere in the list */
7109 i = hisidx[type];
7110 else if (viminfo_hisidx[type] > 0)
7111 /* start at newest entry, first in the list */
7112 i = 0;
7113 else
7114 /* empty list */
7115 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007116 if (i >= 0)
7117 while (num_saved > 0
7118 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007120 char_u *p;
7121 time_t timestamp;
7122 int c = NUL;
7123
7124 if (round == 1)
7125 {
7126 p = history[type][i].hisstr;
7127 timestamp = history[type][i].time_set;
7128 }
7129 else
7130 {
7131 p = viminfo_history[type] == NULL ? NULL
7132 : viminfo_history[type][i].hisstr;
7133 timestamp = viminfo_history[type] == NULL ? 0
7134 : viminfo_history[type][i].time_set;
7135 }
7136
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007137 if (p != NULL && (round == 2
7138 || !merge
7139 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007141 --num_saved;
7142 fputc(hist_type2char(type, TRUE), fp);
7143 /* For the search history: put the separator in the
7144 * second column; use a space if there isn't one. */
7145 if (type == HIST_SEARCH)
7146 {
7147 c = p[STRLEN(p) + 1];
7148 putc(c == NUL ? ' ' : c, fp);
7149 }
7150 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007151
7152 {
7153 char cbuf[NUMBUFLEN];
7154
7155 /* New style history with a bar line. Format:
7156 * |{bartype},{histtype},{timestamp},{separator},"text" */
7157 if (c == NUL)
7158 cbuf[0] = NUL;
7159 else
7160 sprintf(cbuf, "%d", c);
7161 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7162 type, (long)timestamp, cbuf);
7163 barline_writestring(fp, p, LSIZE - 20);
7164 putc('\n', fp);
7165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007167 if (round == 1)
7168 {
7169 /* Decrement index, loop around and stop when back at
7170 * the start. */
7171 if (--i < 0)
7172 i = hislen - 1;
7173 if (i == hisidx[type])
7174 break;
7175 }
7176 else
7177 {
7178 /* Increment index. Stop at the end in the while. */
7179 ++i;
7180 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007182 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007183 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007184 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007185 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007186 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007187 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188 }
7189}
7190#endif /* FEAT_VIMINFO */
7191
7192#if defined(FEAT_FKMAP) || defined(PROTO)
7193/*
7194 * Write a character at the current cursor+offset position.
7195 * It is directly written into the command buffer block.
7196 */
7197 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007198cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199{
7200 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7201 {
7202 EMSG(_("E198: cmd_pchar beyond the command length"));
7203 return;
7204 }
7205 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7206 ccline.cmdbuff[ccline.cmdlen] = NUL;
7207}
7208
7209 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007210cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211{
7212 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7213 {
7214 /* EMSG(_("cmd_gchar beyond the command length")); */
7215 return NUL;
7216 }
7217 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7218}
7219#endif
7220
7221#if defined(FEAT_CMDWIN) || defined(PROTO)
7222/*
7223 * Open a window on the current command line and history. Allow editing in
7224 * the window. Returns when the window is closed.
7225 * Returns:
7226 * CR if the command is to be executed
7227 * Ctrl_C if it is to be abandoned
7228 * K_IGNORE if editing continues
7229 */
7230 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007231open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232{
7233 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007234 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007236 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 win_T *wp;
7238 int i;
7239 linenr_T lnum;
7240 int histtype;
7241 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 int save_restart_edit = restart_edit;
7243 int save_State = State;
7244 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007245#ifdef FEAT_RIGHTLEFT
7246 int save_cmdmsg_rl = cmdmsg_rl;
7247#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007248#ifdef FEAT_FOLDING
7249 int save_KeyTyped;
7250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
7252 /* Can't do this recursively. Can't do it when typing a password. */
7253 if (cmdwin_type != 0
7254# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7255 || cmdline_star > 0
7256# endif
7257 )
7258 {
7259 beep_flush();
7260 return K_IGNORE;
7261 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007262 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263
7264 /* Save current window sizes. */
7265 win_size_save(&winsizes);
7266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007268 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007269
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007270 /* don't use a new tab page */
7271 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007272 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007273
Bram Moolenaar071d4272004-06-13 20:20:40 +00007274 /* Create a window for the command-line buffer. */
7275 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7276 {
7277 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007278 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 return K_IGNORE;
7280 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007281 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
7283 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007284 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007285 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007288#ifdef FEAT_FOLDING
7289 curwin->w_p_fen = FALSE;
7290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007292 curwin->w_p_rl = cmdmsg_rl;
7293 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007295 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007298 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007299 /* But don't allow switching to another buffer. */
7300 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
Bram Moolenaar46152342005-09-07 21:18:43 +00007302 /* Showing the prompt may have set need_wait_return, reset it. */
7303 need_wait_return = FALSE;
7304
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007305 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7307 {
7308 if (p_wc == TAB)
7309 {
7310 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7311 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7312 }
7313 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7314 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007315 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007317 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7318 * sets 'textwidth' to 78). */
7319 curbuf->b_p_tw = 0;
7320
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321 /* Fill the buffer with the history. */
7322 init_history();
7323 if (hislen > 0)
7324 {
7325 i = hisidx[histtype];
7326 if (i >= 0)
7327 {
7328 lnum = 0;
7329 do
7330 {
7331 if (++i == hislen)
7332 i = 0;
7333 if (history[histtype][i].hisstr != NULL)
7334 ml_append(lnum++, history[histtype][i].hisstr,
7335 (colnr_T)0, FALSE);
7336 }
7337 while (i != hisidx[histtype]);
7338 }
7339 }
7340
7341 /* Replace the empty last line with the current command-line and put the
7342 * cursor there. */
7343 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7344 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7345 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007346 changed_line_abv_curs();
7347 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007348 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
7350 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007351 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352
7353 /* No Ex mode here! */
7354 exmode_active = 0;
7355
7356 State = NORMAL;
7357# ifdef FEAT_MOUSE
7358 setmouse();
7359# endif
7360
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007362 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007363 if (restart_edit != 0) /* autocmd with ":startinsert" */
7364 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007365
7366 i = RedrawingDisabled;
7367 RedrawingDisabled = 0;
7368
7369 /*
7370 * Call the main loop until <CR> or CTRL-C is typed.
7371 */
7372 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007373 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374
7375 RedrawingDisabled = i;
7376
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007377# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007378 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007379# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007380
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007382 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007383
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007384# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007385 /* Restore KeyTyped in case it is modified by autocommands */
7386 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387# endif
7388
Bram Moolenaarccc18222007-05-10 18:25:20 +00007389 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007390 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 cmdwin_type = 0;
7392
7393 exmode_active = save_exmode;
7394
Bram Moolenaarf9821062008-06-20 16:31:07 +00007395 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007397 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 {
7399 cmdwin_result = Ctrl_C;
7400 EMSG(_("E199: Active window or buffer deleted"));
7401 }
7402 else
7403 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007404# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 /* autocmds may abort script processing */
7406 if (aborting() && cmdwin_result != K_IGNORE)
7407 cmdwin_result = Ctrl_C;
7408# endif
7409 /* Set the new command line from the cmdline buffer. */
7410 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007411 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007413 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7414
7415 if (histtype == HIST_CMD)
7416 {
7417 /* Execute the command directly. */
7418 ccline.cmdbuff = vim_strsave((char_u *)p);
7419 cmdwin_result = CAR;
7420 }
7421 else
7422 {
7423 /* First need to cancel what we were doing. */
7424 ccline.cmdbuff = NULL;
7425 stuffcharReadbuff(':');
7426 stuffReadbuff((char_u *)p);
7427 stuffcharReadbuff(CAR);
7428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 }
7430 else if (cmdwin_result == K_XF2) /* :qa typed */
7431 {
7432 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7433 cmdwin_result = CAR;
7434 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007435 else if (cmdwin_result == Ctrl_C)
7436 {
7437 /* :q or :close, don't execute any command
7438 * and don't modify the cmd window. */
7439 ccline.cmdbuff = NULL;
7440 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007441 else
7442 ccline.cmdbuff = vim_strsave(ml_get_curline());
7443 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007444 {
7445 ccline.cmdbuff = vim_strsave((char_u *)"");
7446 ccline.cmdlen = 0;
7447 ccline.cmdbufflen = 1;
7448 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 else
7452 {
7453 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7454 ccline.cmdbufflen = ccline.cmdlen + 1;
7455 ccline.cmdpos = curwin->w_cursor.col;
7456 if (ccline.cmdpos > ccline.cmdlen)
7457 ccline.cmdpos = ccline.cmdlen;
7458 if (cmdwin_result == K_IGNORE)
7459 {
7460 set_cmdspos_cursor();
7461 redrawcmd();
7462 }
7463 }
7464
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007466 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007467# ifdef FEAT_CONCEAL
7468 /* Avoid command-line window first character being concealed. */
7469 curwin->w_p_cole = 0;
7470# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007471 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007472 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 win_goto(old_curwin);
7474 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007475
7476 /* win_close() may have already wiped the buffer when 'bh' is
7477 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007478 if (bufref_valid(&bufref))
7479 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480
7481 /* Restore window sizes. */
7482 win_size_restore(&winsizes);
7483
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007484 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 }
7486
7487 ga_clear(&winsizes);
7488 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007489# ifdef FEAT_RIGHTLEFT
7490 cmdmsg_rl = save_cmdmsg_rl;
7491# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492
7493 State = save_State;
7494# ifdef FEAT_MOUSE
7495 setmouse();
7496# endif
7497
7498 return cmdwin_result;
7499}
7500#endif /* FEAT_CMDWIN */
7501
7502/*
7503 * Used for commands that either take a simple command string argument, or:
7504 * cmd << endmarker
7505 * {script}
7506 * endmarker
7507 * Returns a pointer to allocated memory with {script} or NULL.
7508 */
7509 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007510script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511{
7512 char_u *theline;
7513 char *end_pattern = NULL;
7514 char dot[] = ".";
7515 garray_T ga;
7516
7517 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7518 return NULL;
7519
7520 ga_init2(&ga, 1, 0x400);
7521
7522 if (cmd[2] != NUL)
7523 end_pattern = (char *)skipwhite(cmd + 2);
7524 else
7525 end_pattern = dot;
7526
7527 for (;;)
7528 {
7529 theline = eap->getline(
7530#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007531 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532#endif
7533 NUL, eap->cookie, 0);
7534
7535 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007536 {
7537 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540
7541 ga_concat(&ga, theline);
7542 ga_append(&ga, '\n');
7543 vim_free(theline);
7544 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007545 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546
7547 return (char_u *)ga.ga_data;
7548}