blob: 52eda33c736daac1ac29bcc584222bfd31c0c168 [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 Moolenaar438d1762018-09-30 17:11:48 +020047// 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().
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000050static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar438d1762018-09-30 17:11:48 +020052static int cmd_showtail; /* Only show path tail in lists ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef FEAT_EVAL
55static int new_cmdpos; /* position set by set_cmdline_pos() */
56#endif
57
Bram Moolenaara92522f2017-07-15 15:21:38 +020058static int extra_char = NUL; /* extra character to display when redrawing
59 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020060static int extra_char_shift;
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_CMDHIST
63typedef struct hist_entry
64{
65 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020066 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020068 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069} histentry_T;
70
71static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
72static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
73static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
74 /* identifying (unique) number of newest history entry */
75static int hislen = 0; /* actual length of history tables */
76
Bram Moolenaard25c16e2016-01-29 22:13:30 +010077static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
80#ifdef FEAT_RIGHTLEFT
81static int cmd_hkmap = 0; /* Hebrew mapping during command line */
82#endif
83
Bram Moolenaar438d1762018-09-30 17:11:48 +020084static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010085static int cmdline_charsize(int idx);
86static void set_cmdspos(void);
87static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010088static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static void alloc_cmdbuff(int len);
90static int realloc_cmdbuff(int len);
91static void draw_cmdline(int start, int len);
92static void save_cmdline(struct cmdline_info *ccp);
93static void restore_cmdline(struct cmdline_info *ccp);
94static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void redrawcmdprompt(void);
99static void cursorcmd(void);
100static int ccheck_abbr(int);
101static int nextwild(expand_T *xp, int type, int options, int escape);
102static void escape_fname(char_u **pp);
103static int showmatches(expand_T *xp, int wildmenu);
104static void set_expand_context(expand_T *xp);
105static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
106static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100109static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100110static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200111# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100115static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
116static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200119#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100120static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200124static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200127#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
128static int
129#ifdef __BORLANDC__
130_RTLENTRYF
131#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100132sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#endif
134
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200135
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200136 static void
137trigger_cmd_autocmd(int typechar, int evt)
138{
139 char_u typestr[2];
140
141 typestr[0] = typechar;
142 typestr[1] = NUL;
143 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
144}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200147 * Abandon the command line.
148 */
149 static void
150abandon_cmdline(void)
151{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100152 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200153 if (msg_scrolled == 0)
154 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100155 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200156 redraw_cmdline = TRUE;
157}
158
Bram Moolenaaree219b02017-12-17 14:55:01 +0100159#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200160/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100161 * Guess that the pattern matches everything. Only finds specific cases, such
162 * as a trailing \|, which can happen while typing a pattern.
163 */
164 static int
165empty_pattern(char_u *p)
166{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100167 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100168
169 /* remove trailing \v and the like */
170 while (n >= 2 && p[n - 2] == '\\'
171 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
172 n -= 2;
173 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
174}
175
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200176// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200177typedef struct {
178 colnr_T vs_curswant;
179 colnr_T vs_leftcol;
180 linenr_T vs_topline;
181# ifdef FEAT_DIFF
182 int vs_topfill;
183# endif
184 linenr_T vs_botline;
185 linenr_T vs_empty_rows;
186} viewstate_T;
187
188 static void
189save_viewstate(viewstate_T *vs)
190{
191 vs->vs_curswant = curwin->w_curswant;
192 vs->vs_leftcol = curwin->w_leftcol;
193 vs->vs_topline = curwin->w_topline;
194# ifdef FEAT_DIFF
195 vs->vs_topfill = curwin->w_topfill;
196# endif
197 vs->vs_botline = curwin->w_botline;
198 vs->vs_empty_rows = curwin->w_empty_rows;
199}
200
201 static void
202restore_viewstate(viewstate_T *vs)
203{
204 curwin->w_curswant = vs->vs_curswant;
205 curwin->w_leftcol = vs->vs_leftcol;
206 curwin->w_topline = vs->vs_topline;
207# ifdef FEAT_DIFF
208 curwin->w_topfill = vs->vs_topfill;
209# endif
210 curwin->w_botline = vs->vs_botline;
211 curwin->w_empty_rows = vs->vs_empty_rows;
212}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200213
214// Struct to store the state of 'incsearch' highlighting.
215typedef struct {
216 pos_T search_start; // where 'incsearch' starts searching
217 pos_T save_cursor;
218 viewstate_T init_viewstate;
219 viewstate_T old_viewstate;
220 pos_T match_start;
221 pos_T match_end;
222 int did_incsearch;
223 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200224 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200225} incsearch_state_T;
226
227 static void
228init_incsearch_state(incsearch_state_T *is_state)
229{
230 is_state->match_start = curwin->w_cursor;
231 is_state->did_incsearch = FALSE;
232 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200233 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200234 CLEAR_POS(&is_state->match_end);
235 is_state->save_cursor = curwin->w_cursor; // may be restored later
236 is_state->search_start = curwin->w_cursor;
237 save_viewstate(&is_state->init_viewstate);
238 save_viewstate(&is_state->old_viewstate);
239}
240
241/*
242 * First move cursor to end of match, then to the start. This
243 * moves the whole match onto the screen when 'nowrap' is set.
244 */
245 static void
246set_search_match(pos_T *t)
247{
248 t->lnum += search_match_lines;
249 t->col = search_match_endcol;
250 if (t->lnum > curbuf->b_ml.ml_line_count)
251 {
252 t->lnum = curbuf->b_ml.ml_line_count;
253 coladvance((colnr_T)MAXCOL);
254 }
255}
256
257/*
258 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200259 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200260 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200261 */
262 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200263do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
264 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200265{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200266 char_u *cmd;
267 cmdmod_T save_cmdmod = cmdmod;
268 char_u *p;
269 int delim_optional = FALSE;
270 int delim;
271 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100272 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200273 exarg_T ea;
274 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200275 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200277 *skiplen = 0;
278 *patlen = ccline.cmdlen;
279
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 if (!p_is || cmd_silent)
281 return FALSE;
282
283 // by default search all lines
284 search_first_line = 0;
285 search_last_line = MAXLNUM;
286
287 if (firstc == '/' || firstc == '?')
288 return TRUE;
289 if (firstc != ':')
290 return FALSE;
291
292 vim_memset(&ea, 0, sizeof(ea));
293 ea.line1 = 1;
294 ea.line2 = 1;
295 ea.cmd = ccline.cmdbuff;
296 ea.addr_type = ADDR_LINES;
297
298 parse_command_modifiers(&ea, &dummy, TRUE);
299 cmdmod = save_cmdmod;
300
301 cmd = skip_range(ea.cmd, NULL);
302 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
303 return FALSE;
304
305 // Skip over "substitute" to find the pattern separator.
306 for (p = cmd; ASCII_ISALPHA(*p); ++p)
307 ;
308 if (*skipwhite(p) == NUL)
309 return FALSE;
310
311 if (STRNCMP(cmd, "substitute", p - cmd) == 0
312 || STRNCMP(cmd, "smagic", p - cmd) == 0
313 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
314 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200315 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200316 if (*cmd == 's' && cmd[1] == 'm')
317 p_magic = TRUE;
318 else if (*cmd == 's' && cmd[1] == 'n')
319 p_magic = FALSE;
320 }
321 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
322 {
323 // skip over flags
324 while (ASCII_ISALPHA(*(p = skipwhite(p))))
325 ++p;
326 if (*p == NUL)
327 return FALSE;
328 }
329 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
330 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
331 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
332 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
333 || STRNCMP(cmd, "global", p - cmd) == 0)
334 {
335 // skip over "!"
336 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200337 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200338 p++;
339 if (*skipwhite(p) == NUL)
340 return FALSE;
341 }
342 if (*cmd != 'g')
343 delim_optional = TRUE;
344 }
345 else
346 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200347
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200348 p = skipwhite(p);
349 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
350 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200351
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200352 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200353
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200354 if (end == p && !use_last_pat)
355 return FALSE;
356
357 // Don't do 'hlsearch' highlighting if the pattern matches everything.
358 if (!use_last_pat)
359 {
360 char c = *end;
361 int empty;
362
363 *end = NUL;
364 empty = empty_pattern(p);
365 *end = c;
366 if (empty)
367 return FALSE;
368 }
369
370 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200371 *skiplen = (int)(p - ccline.cmdbuff);
372 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200373
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200374 // parse the address range
375 save_cursor = curwin->w_cursor;
376 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200377 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200378 if (ea.addr_count > 0)
379 {
380 // Allow for reverse match.
381 if (ea.line2 < ea.line1)
382 {
383 search_first_line = ea.line2;
384 search_last_line = ea.line1;
385 }
386 else
387 {
388 search_first_line = ea.line1;
389 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200390 }
391 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200392 else if (cmd[0] == 's' && cmd[1] != 'o')
393 {
394 // :s defaults to the current line
395 search_first_line = curwin->w_cursor.lnum;
396 search_last_line = curwin->w_cursor.lnum;
397 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200398
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200399 curwin->w_cursor = save_cursor;
400 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200401}
402
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200403 static void
404finish_incsearch_highlighting(
405 int gotesc,
406 incsearch_state_T *is_state,
407 int call_update_screen)
408{
409 if (is_state->did_incsearch)
410 {
411 is_state->did_incsearch = FALSE;
412 if (gotesc)
413 curwin->w_cursor = is_state->save_cursor;
414 else
415 {
416 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
417 {
418 // put the '" mark at the original position
419 curwin->w_cursor = is_state->save_cursor;
420 setpcmark();
421 }
422 curwin->w_cursor = is_state->search_start;
423 }
424 restore_viewstate(&is_state->old_viewstate);
425 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200426
427 // by default search all lines
428 search_first_line = 0;
429 search_last_line = MAXLNUM;
430
431 p_magic = is_state->magic_save;
432
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200433 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200434 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200435 if (call_update_screen)
436 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200437 }
438}
439
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200440/*
441 * Do 'incsearch' highlighting if desired.
442 */
443 static void
444may_do_incsearch_highlighting(
445 int firstc,
446 long count,
447 incsearch_state_T *is_state)
448{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200449 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200450 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200451 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200452#ifdef FEAT_RELTIME
453 proftime_T tm;
454#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200455 int next_char;
456 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457
Bram Moolenaar198cb662018-09-06 21:44:17 +0200458 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100459 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200460 save_last_search_pattern();
461
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200462 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200463 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200464 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200465 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200467 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200468
469 // If there is a character waiting, search and redraw later.
470 if (char_avail())
471 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200472 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473 is_state->incsearch_postponed = TRUE;
474 return;
475 }
476 is_state->incsearch_postponed = FALSE;
477
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200478 if (search_first_line == 0)
479 // start at the original cursor position
480 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100481 else if (search_first_line > curbuf->b_ml.ml_line_count)
482 {
483 // start after the last line
484 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
485 curwin->w_cursor.col = MAXCOL;
486 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200487 else
488 {
489 // start at the first line in the range
490 curwin->w_cursor.lnum = search_first_line;
491 curwin->w_cursor.col = 0;
492 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200493
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200494 // Use the previous pattern for ":s//".
495 next_char = ccline.cmdbuff[skiplen + patlen];
496 use_last_pat = patlen == 0 && skiplen > 0
497 && ccline.cmdbuff[skiplen - 1] == next_char;
498
499 // If there is no pattern, don't do anything.
500 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200501 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200502 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200503 set_no_hlsearch(TRUE); // turn off previous highlight
504 redraw_all_later(SOME_VALID);
505 }
506 else
507 {
508 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
509
510 cursor_off(); // so the user knows we're busy
511 out_flush();
512 ++emsg_off; // so it doesn't beep if bad expr
513#ifdef FEAT_RELTIME
514 // Set the time limit to half a second.
515 profile_setlimit(500L, &tm);
516#endif
517 if (!p_hls)
518 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200519 if (search_first_line != 0)
520 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200521 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200522 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200523 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200524#ifdef FEAT_RELTIME
525 &tm, NULL
526#else
527 NULL, NULL
528#endif
529 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200530 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200531 --emsg_off;
532
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200533 if (curwin->w_cursor.lnum < search_first_line
534 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200535 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200536 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200537 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200538 curwin->w_cursor = is_state->search_start;
539 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200540
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200541 // if interrupted while searching, behave like it failed
542 if (got_int)
543 {
544 (void)vpeekc(); // remove <C-C> from input stream
545 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200546 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547 }
548 else if (char_avail())
549 // cancelled searching because a char was typed
550 is_state->incsearch_postponed = TRUE;
551 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200552 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200553 highlight_match = TRUE; // highlight position
554 else
555 highlight_match = FALSE; // remove highlight
556
557 // First restore the old curwin values, so the screen is positioned in the
558 // same way as the actual search command.
559 restore_viewstate(&is_state->old_viewstate);
560 changed_cline_bef_curs();
561 update_topline();
562
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200563 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 {
565 pos_T save_pos = curwin->w_cursor;
566
567 is_state->match_start = curwin->w_cursor;
568 set_search_match(&curwin->w_cursor);
569 validate_cursor();
570 end_pos = curwin->w_cursor;
571 is_state->match_end = end_pos;
572 curwin->w_cursor = save_pos;
573 }
574 else
575 end_pos = curwin->w_cursor; // shutup gcc 4
576
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200577 // Disable 'hlsearch' highlighting if the pattern matches everything.
578 // Avoids a flash when typing "foo\|".
579 if (!use_last_pat)
580 {
581 next_char = ccline.cmdbuff[skiplen + patlen];
582 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200583 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
584 {
585 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200586 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200587 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200588 ccline.cmdbuff[skiplen + patlen] = next_char;
589 }
590
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 validate_cursor();
592 // May redraw the status line to show the cursor position.
593 if (p_ru && curwin->w_status_height > 0)
594 curwin->w_redr_status = TRUE;
595
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 restore_last_search_pattern();
598
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200599 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
600 // end of the pattern, e.g. for ":s/pat/".
601 if (ccline.cmdbuff[skiplen + patlen] != NUL)
602 curwin->w_cursor = is_state->search_start;
603 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 curwin->w_cursor = end_pos;
605
606 msg_starthere();
607 redrawcmdline();
608 is_state->did_incsearch = TRUE;
609}
610
611/*
612 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
613 * or previous match.
614 * Returns FAIL when jumping to cmdline_not_changed;
615 */
616 static int
617may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200618 int firstc,
619 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 pos_T t;
625 char_u *pat;
626 int search_flags = SEARCH_NOOF;
627 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629
Bram Moolenaar198cb662018-09-06 21:44:17 +0200630 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100631 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200632 save_last_search_pattern();
633
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200634 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200635 {
636 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200637 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200638 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200639 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200640 {
641 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200642 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200643 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200644
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200645 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200646 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200647 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200648 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200649 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200650 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200652 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 cursor_off();
655 out_flush();
656 if (c == Ctrl_G)
657 {
658 t = is_state->match_end;
659 if (LT_POS(is_state->match_start, is_state->match_end))
660 // Start searching at the end of the match not at the beginning of
661 // the next column.
662 (void)decl(&t);
663 search_flags += SEARCH_COL;
664 }
665 else
666 t = is_state->match_start;
667 if (!p_hls)
668 search_flags += SEARCH_KEEP;
669 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200670 save = pat[patlen];
671 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100672 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200673 c == Ctrl_G ? FORWARD : BACKWARD,
674 pat, count, search_flags,
675 RE_SEARCH, 0, NULL, NULL);
676 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678 if (i)
679 {
680 is_state->search_start = is_state->match_start;
681 is_state->match_end = t;
682 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200683 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 {
685 // Move just before the current match, so that when nv_search
686 // finishes the cursor will be put back on the match.
687 is_state->search_start = t;
688 (void)decl(&is_state->search_start);
689 }
690 else if (c == Ctrl_G && firstc == '?')
691 {
692 // Move just after the current match, so that when nv_search
693 // finishes the cursor will be put back on the match.
694 is_state->search_start = t;
695 (void)incl(&is_state->search_start);
696 }
697 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
698 {
699 // wrap around
700 is_state->search_start = t;
701 if (firstc == '?')
702 (void)incl(&is_state->search_start);
703 else
704 (void)decl(&is_state->search_start);
705 }
706
707 set_search_match(&is_state->match_end);
708 curwin->w_cursor = is_state->match_start;
709 changed_cline_bef_curs();
710 update_topline();
711 validate_cursor();
712 highlight_match = TRUE;
713 save_viewstate(&is_state->old_viewstate);
714 update_screen(NOT_VALID);
715 redrawcmdline();
716 }
717 else
718 vim_beep(BO_ERROR);
719 restore_last_search_pattern();
720 return FAIL;
721}
722
723/*
724 * When CTRL-L typed: add character from the match to the pattern.
725 * May set "*c" to the added character.
726 * Return OK when jumping to cmdline_not_changed.
727 */
728 static int
729may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
730{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200731 int skiplen, patlen;
732
Bram Moolenaar198cb662018-09-06 21:44:17 +0200733 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100734 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200735 save_last_search_pattern();
736
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200737 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200738 {
739 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200740 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200741 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100742 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200743
744 // Add a character from under the cursor for 'incsearch'.
745 if (is_state->did_incsearch)
746 {
747 curwin->w_cursor = is_state->match_end;
748 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
749 {
750 *c = gchar_cursor();
751
752 // If 'ignorecase' and 'smartcase' are set and the
753 // command line has no uppercase characters, convert
754 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200755 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200756 *c = MB_TOLOWER(*c);
757 if (*c != NUL)
758 {
759 if (*c == firstc || vim_strchr((char_u *)(
760 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
761 {
762 // put a backslash before special characters
763 stuffcharReadbuff(*c);
764 *c = '\\';
765 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 // add any composing characters
767 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
768 {
769 int save_c = *c;
770
771 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
772 {
773 curwin->w_cursor.col += mb_char2len(*c);
774 *c = gchar_cursor();
775 stuffcharReadbuff(*c);
776 }
777 *c = save_c;
778 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200779 return FAIL;
780 }
781 }
782 }
783 return OK;
784}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200785#endif
786
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200787 void
788cmdline_init(void)
789{
790 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
791}
792
Bram Moolenaar66216052017-12-16 16:33:44 +0100793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * getcmdline() - accept a command line starting with firstc.
795 *
796 * firstc == ':' get ":" command line.
797 * firstc == '/' or '?' get search pattern
798 * firstc == '=' get expression
799 * firstc == '@' get text for input() function
800 * firstc == '>' get text for debug mode
801 * firstc == NUL get text for :insert command
802 * firstc == -1 like NUL, and break on CTRL-C
803 *
804 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
805 * command line.
806 *
807 * Careful: getcmdline() can be called recursively!
808 *
809 * Return pointer to allocated string if there is a commandline, NULL
810 * otherwise.
811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100813getcmdline(
814 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200815 long count, // only used for incremental search
816 int indent) // indent for inside conditionals
817{
818 return getcmdline_int(firstc, count, indent, TRUE);
819}
820
821 static char_u *
822getcmdline_int(
823 int firstc,
824 long count UNUSED, // only used for incremental search
825 int indent, // indent for inside conditionals
826 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 int c;
829 int i;
830 int j;
831 int gotesc = FALSE; /* TRUE when <ESC> just typed */
832 int do_abbr; /* when TRUE check for abbr. */
833#ifdef FEAT_CMDHIST
834 char_u *lookfor = NULL; /* string to match */
835 int hiscnt; /* current history line in use */
836 int histype; /* history type to be used */
837#endif
838#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200839 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#endif
841 int did_wild_list = FALSE; /* did wild_list() recently */
842 int wim_index = 0; /* index in wim_flags[] */
843 int res;
844 int save_msg_scroll = msg_scroll;
845 int save_State = State; /* remember State when called */
846 int some_key_typed = FALSE; /* one of the keys was typed */
847#ifdef FEAT_MOUSE
848 /* mouse drag and release events are ignored, unless they are
849 * preceded with a mouse down event */
850 int ignore_drag_release = TRUE;
851#endif
852#ifdef FEAT_EVAL
853 int break_ctrl_c = FALSE;
854#endif
855 expand_T xpc;
856 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000857 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200858 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200859 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaar438d1762018-09-30 17:11:48 +0200861 if (ccline.cmdbuff != NULL)
862 {
863 // Being called recursively. Since ccline is global, we need to save
864 // the current buffer and restore it when returning.
865 save_cmdline(&save_ccline);
866 did_save_ccline = TRUE;
867 }
868 if (init_ccline)
869 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_EVAL
872 if (firstc == -1)
873 {
874 firstc = NUL;
875 break_ctrl_c = TRUE;
876 }
877#endif
878#ifdef FEAT_RIGHTLEFT
879 /* start without Hebrew mapping for a command line */
880 if (firstc == ':' || firstc == '=' || firstc == '>')
881 cmd_hkmap = 0;
882#endif
883
884 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200887 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#endif
889
890 /*
891 * set some variables for redrawcmd()
892 */
893 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000894 ccline.cmdindent = (firstc > 0 ? indent : 0);
895
896 /* alloc initial ccline.cmdbuff */
897 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200899 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 ccline.cmdlen = ccline.cmdpos = 0;
901 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100902 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000904 /* autoindent for :insert and :append */
905 if (firstc <= 0)
906 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200907 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000908 ccline.cmdbuff[indent] = NUL;
909 ccline.cmdpos = indent;
910 ccline.cmdspos = indent;
911 ccline.cmdlen = indent;
912 }
913
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000915 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917#ifdef FEAT_RIGHTLEFT
918 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
919 && (firstc == '/' || firstc == '?'))
920 cmdmsg_rl = TRUE;
921 else
922 cmdmsg_rl = FALSE;
923#endif
924
925 redir_off = TRUE; /* don't redirect the typed command */
926 if (!cmd_silent)
927 {
928 i = msg_scrolled;
929 msg_scrolled = 0; /* avoid wait_return message */
930 gotocmdline(TRUE);
931 msg_scrolled += i;
932 redrawcmdprompt(); /* draw prompt or indent */
933 set_cmdspos();
934 }
935 xpc.xp_context = EXPAND_NOTHING;
936 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000937#ifndef BACKSLASH_IN_FILENAME
938 xpc.xp_shell = FALSE;
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000941#if defined(FEAT_EVAL)
942 if (ccline.input_fn)
943 {
944 xpc.xp_context = ccline.xp_context;
945 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000946# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000947 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000948# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000949 }
950#endif
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 /*
953 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
954 * doing ":@0" when register 0 doesn't contain a CR.
955 */
956 msg_scroll = FALSE;
957
958 State = CMDLINE;
959
960 if (firstc == '/' || firstc == '?' || firstc == '@')
961 {
962 /* Use ":lmap" mappings for search pattern and input(). */
963 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
964 b_im_ptr = &curbuf->b_p_iminsert;
965 else
966 b_im_ptr = &curbuf->b_p_imsearch;
967 if (*b_im_ptr == B_IMODE_LMAP)
968 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100969#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 im_set_active(*b_im_ptr == B_IMODE_IM);
971#endif
972 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100973#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 else if (p_imcmdline)
975 im_set_active(TRUE);
976#endif
977
978#ifdef FEAT_MOUSE
979 setmouse();
980#endif
981#ifdef CURSOR_SHAPE
982 ui_cursor_shape(); /* may show different cursor shape */
983#endif
984
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000985 /* When inside an autocommand for writing "exiting" may be set and
986 * terminal mode set to cooked. Need to set raw mode here then. */
987 settmode(TMODE_RAW);
988
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200989 /* Trigger CmdlineEnter autocommands. */
990 cmdline_type = firstc == NUL ? '-' : firstc;
991 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_CMDHIST
994 init_history();
995 hiscnt = hislen; /* set hiscnt to impossible history value */
996 histype = hist_char2type(firstc);
997#endif
998
999#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +00001000 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#endif
1002
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001003 /* If something above caused an error, reset the flags, we do want to type
1004 * and execute commands. Display may be messed up a bit. */
1005 if (did_emsg)
1006 redrawcmd();
1007 did_emsg = FALSE;
1008 got_int = FALSE;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * Collect the command string, handling editing keys.
1012 */
1013 for (;;)
1014 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001015 redir_off = TRUE; /* Don't redirect the typed command.
1016 Repeated, because a ":redir" inside
1017 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#ifdef USE_ON_FLY_SCROLL
1019 dont_scroll = FALSE; /* allow scrolling here */
1020#endif
1021 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1022
Bram Moolenaar72532d32018-04-07 19:09:09 +02001023 did_emsg = FALSE; /* There can't really be a reason why an error
1024 that occurs while typing a command should
1025 cause the command not to be executed. */
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001028
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001029 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1030 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001031 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001032 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001033 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001034
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 if (KeyTyped)
1036 {
1037 some_key_typed = TRUE;
1038#ifdef FEAT_RIGHTLEFT
1039 if (cmd_hkmap)
1040 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 if (cmdmsg_rl && !KeyStuffed)
1042 {
1043 /* Invert horizontal movements and operations. Only when
1044 * typed by the user directly, not when the result of a
1045 * mapping. */
1046 switch (c)
1047 {
1048 case K_RIGHT: c = K_LEFT; break;
1049 case K_S_RIGHT: c = K_S_LEFT; break;
1050 case K_C_RIGHT: c = K_C_LEFT; break;
1051 case K_LEFT: c = K_RIGHT; break;
1052 case K_S_LEFT: c = K_S_RIGHT; break;
1053 case K_C_LEFT: c = K_C_RIGHT; break;
1054 }
1055 }
1056#endif
1057 }
1058
1059 /*
1060 * Ignore got_int when CTRL-C was typed here.
1061 * Don't ignore it in :global, we really need to break then, e.g., for
1062 * ":g/pat/normal /pat" (without the <CR>).
1063 * Don't ignore it for the input() function.
1064 */
1065 if ((c == Ctrl_C
1066#ifdef UNIX
1067 || c == intr_char
1068#endif
1069 )
1070#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1071 && firstc != '@'
1072#endif
1073#ifdef FEAT_EVAL
1074 && !break_ctrl_c
1075#endif
1076 && !global_busy)
1077 got_int = FALSE;
1078
1079#ifdef FEAT_CMDHIST
1080 /* free old command line when finished moving around in the history
1081 * list */
1082 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001083 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001084 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 && c != K_PAGEDOWN && c != K_PAGEUP
1086 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001087 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001089 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090#endif
1091
1092 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001093 * When there are matching completions to select <S-Tab> works like
1094 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001096 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 c = Ctrl_P;
1098
1099#ifdef FEAT_WILDMENU
1100 /* Special translations for 'wildmenu' */
1101 if (did_wild_list && p_wmnu)
1102 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001103 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001105 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 c = Ctrl_N;
1107 }
1108 /* Hitting CR after "emenu Name.": complete submenu */
1109 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1110 && ccline.cmdpos > 1
1111 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1112 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1113 && (c == '\n' || c == '\r' || c == K_KENTER))
1114 c = K_DOWN;
1115#endif
1116
1117 /* free expanded names when finished walking through matches */
1118 if (xpc.xp_numfiles != -1
1119 && !(c == p_wc && KeyTyped) && c != p_wcm
1120 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1121 && c != Ctrl_L)
1122 {
1123 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1124 did_wild_list = FALSE;
1125#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001126 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127#endif
1128 xpc.xp_context = EXPAND_NOTHING;
1129 wim_index = 0;
1130#ifdef FEAT_WILDMENU
1131 if (p_wmnu && wild_menu_showing != 0)
1132 {
1133 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001134 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001135
1136 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001137 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138
1139 if (wild_menu_showing == WM_SCROLLED)
1140 {
1141 /* Entered command line, move it up */
1142 cmdline_row--;
1143 redrawcmd();
1144 }
1145 else if (save_p_ls != -1)
1146 {
1147 /* restore 'laststatus' and 'winminheight' */
1148 p_ls = save_p_ls;
1149 p_wmh = save_p_wmh;
1150 last_status(FALSE);
1151 update_screen(VALID); /* redraw the screen NOW */
1152 redrawcmd();
1153 save_p_ls = -1;
1154 }
1155 else
1156 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 redraw_statuslines();
1159 }
1160 KeyTyped = skt;
1161 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001162 if (ccline.input_fn)
1163 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 }
1165#endif
1166 }
1167
1168#ifdef FEAT_WILDMENU
1169 /* Special translations for 'wildmenu' */
1170 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1171 {
1172 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001173 if (c == K_DOWN && ccline.cmdpos > 0
1174 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001176 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 {
1178 /* Hitting <Up>: Remove one submenu name in front of the
1179 * cursor */
1180 int found = FALSE;
1181
1182 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1183 i = 0;
1184 while (--j > 0)
1185 {
1186 /* check for start of menu name */
1187 if (ccline.cmdbuff[j] == ' '
1188 && ccline.cmdbuff[j - 1] != '\\')
1189 {
1190 i = j + 1;
1191 break;
1192 }
1193 /* check for start of submenu name */
1194 if (ccline.cmdbuff[j] == '.'
1195 && ccline.cmdbuff[j - 1] != '\\')
1196 {
1197 if (found)
1198 {
1199 i = j + 1;
1200 break;
1201 }
1202 else
1203 found = TRUE;
1204 }
1205 }
1206 if (i > 0)
1207 cmdline_del(i);
1208 c = p_wc;
1209 xpc.xp_context = EXPAND_NOTHING;
1210 }
1211 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001212 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001213 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001214 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 {
1216 char_u upseg[5];
1217
1218 upseg[0] = PATHSEP;
1219 upseg[1] = '.';
1220 upseg[2] = '.';
1221 upseg[3] = PATHSEP;
1222 upseg[4] = NUL;
1223
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001224 if (c == K_DOWN
1225 && ccline.cmdpos > 0
1226 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1227 && (ccline.cmdpos < 3
1228 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1230 {
1231 /* go down a directory */
1232 c = p_wc;
1233 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001234 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 {
1236 /* If in a direct ancestor, strip off one ../ to go down */
1237 int found = FALSE;
1238
1239 j = ccline.cmdpos;
1240 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1241 while (--j > i)
1242 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001243 if (has_mbyte)
1244 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (vim_ispathsep(ccline.cmdbuff[j]))
1246 {
1247 found = TRUE;
1248 break;
1249 }
1250 }
1251 if (found
1252 && ccline.cmdbuff[j - 1] == '.'
1253 && ccline.cmdbuff[j - 2] == '.'
1254 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1255 {
1256 cmdline_del(j - 2);
1257 c = p_wc;
1258 }
1259 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001260 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 /* go up a directory */
1263 int found = FALSE;
1264
1265 j = ccline.cmdpos - 1;
1266 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1267 while (--j > i)
1268 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 if (has_mbyte)
1270 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 if (vim_ispathsep(ccline.cmdbuff[j])
1272#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001273 && vim_strchr((char_u *)" *?[{`$%#",
1274 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275#endif
1276 )
1277 {
1278 if (found)
1279 {
1280 i = j + 1;
1281 break;
1282 }
1283 else
1284 found = TRUE;
1285 }
1286 }
1287
1288 if (!found)
1289 j = i;
1290 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1291 j += 4;
1292 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1293 && j == i)
1294 j += 3;
1295 else
1296 j = 0;
1297 if (j > 0)
1298 {
1299 /* TODO this is only for DOS/UNIX systems - need to put in
1300 * machine-specific stuff here and in upseg init */
1301 cmdline_del(j);
1302 put_on_cmdline(upseg + 1, 3, FALSE);
1303 }
1304 else if (ccline.cmdpos > i)
1305 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001306
1307 /* Now complete in the new directory. Set KeyTyped in case the
1308 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001310 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
1314#endif /* FEAT_WILDMENU */
1315
1316 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1317 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1318 if (c == Ctrl_BSL)
1319 {
1320 ++no_mapping;
1321 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001322 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 --no_mapping;
1324 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001325 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1326 * is in a mapping. */
1327 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001328 || (ccline.cmdfirstc == '=' && KeyTyped)
1329#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001330 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001331#endif
1332 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 {
1334 vungetc(c);
1335 c = Ctrl_BSL;
1336 }
1337#ifdef FEAT_EVAL
1338 else if (c == 'e')
1339 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001340 char_u *p = NULL;
1341 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342
1343 /*
1344 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001345 * Need to save and restore the current command line, to be
1346 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 */
1348 if (ccline.cmdpos == ccline.cmdlen)
1349 new_cmdpos = 99999; /* keep it at the end */
1350 else
1351 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001352
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 if (c == '=')
1355 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001356 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001357 * to avoid nasty things like going to another buffer when
1358 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001359 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001361 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001362
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001363 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001365 len = (int)STRLEN(p);
1366 if (realloc_cmdbuff(len + 1) == OK)
1367 {
1368 ccline.cmdlen = len;
1369 STRCPY(ccline.cmdbuff, p);
1370 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001372 /* Restore the cursor or use the position set with
1373 * set_cmdline_pos(). */
1374 if (new_cmdpos > ccline.cmdlen)
1375 ccline.cmdpos = ccline.cmdlen;
1376 else
1377 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001379 KeyTyped = FALSE; /* Don't do p_wc completion. */
1380 redrawcmd();
1381 goto cmdline_changed;
1382 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001383 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385 }
1386 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001387 got_int = FALSE; /* don't abandon the command line */
1388 did_emsg = FALSE;
1389 emsg_on_display = FALSE;
1390 redrawcmd();
1391 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 }
1393#endif
1394 else
1395 {
1396 if (c == Ctrl_G && p_im && restart_edit == 0)
1397 restart_edit = 'a';
1398 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1399 in history */
1400 goto returncmd; /* back to Normal mode */
1401 }
1402 }
1403
1404#ifdef FEAT_CMDWIN
1405 if (c == cedit_key || c == K_CMDWIN)
1406 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001407 if (ex_normal_busy == 0 && got_int == FALSE)
1408 {
1409 /*
1410 * Open a window to edit the command line (and history).
1411 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001412 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001413 some_key_typed = TRUE;
1414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 }
1416# ifdef FEAT_DIGRAPHS
1417 else
1418# endif
1419#endif
1420#ifdef FEAT_DIGRAPHS
1421 c = do_digraph(c);
1422#endif
1423
1424 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1425 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1426 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001427 /* In Ex mode a backslash escapes a newline. */
1428 if (exmode_active
1429 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001430 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001431 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001432 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001434 if (c == K_KENTER)
1435 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001437 else
1438 {
1439 gotesc = FALSE; /* Might have typed ESC previously, don't
1440 truncate the cmdline now. */
1441 if (ccheck_abbr(c + ABBR_OFF))
1442 goto cmdline_changed;
1443 if (!cmd_silent)
1444 {
1445 windgoto(msg_row, 0);
1446 out_flush();
1447 }
1448 break;
1449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451
1452 /*
1453 * Completion for 'wildchar' or 'wildcharm' key.
1454 * - hitting <ESC> twice means: abandon command line.
1455 * - wildcard expansion is only done when the 'wildchar' key is really
1456 * typed, not when it comes from a macro
1457 */
1458 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1459 {
1460 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1461 {
1462 /* if 'wildmode' contains "list" may still need to list */
1463 if (xpc.xp_numfiles > 1
1464 && !did_wild_list
1465 && (wim_flags[wim_index] & WIM_LIST))
1466 {
1467 (void)showmatches(&xpc, FALSE);
1468 redrawcmd();
1469 did_wild_list = TRUE;
1470 }
1471 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001472 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1473 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001475 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1476 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 else
1478 res = OK; /* don't insert 'wildchar' now */
1479 }
1480 else /* typed p_wc first time */
1481 {
1482 wim_index = 0;
1483 j = ccline.cmdpos;
1484 /* if 'wildmode' first contains "longest", get longest
1485 * common part */
1486 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001487 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1488 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001490 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1491 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 /* if interrupted while completing, behave like it failed */
1494 if (got_int)
1495 {
1496 (void)vpeekc(); /* remove <C-C> from input stream */
1497 got_int = FALSE; /* don't abandon the command line */
1498 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1499#ifdef FEAT_WILDMENU
1500 xpc.xp_context = EXPAND_NOTHING;
1501#endif
1502 goto cmdline_changed;
1503 }
1504
1505 /* when more than one match, and 'wildmode' first contains
1506 * "list", or no change and 'wildmode' contains "longest,list",
1507 * list all matches */
1508 if (res == OK && xpc.xp_numfiles > 1)
1509 {
1510 /* a "longest" that didn't do anything is skipped (but not
1511 * "list:longest") */
1512 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1513 wim_index = 1;
1514 if ((wim_flags[wim_index] & WIM_LIST)
1515#ifdef FEAT_WILDMENU
1516 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1517#endif
1518 )
1519 {
1520 if (!(wim_flags[0] & WIM_LONGEST))
1521 {
1522#ifdef FEAT_WILDMENU
1523 int p_wmnu_save = p_wmnu;
1524 p_wmnu = 0;
1525#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001526 /* remove match */
1527 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528#ifdef FEAT_WILDMENU
1529 p_wmnu = p_wmnu_save;
1530#endif
1531 }
1532#ifdef FEAT_WILDMENU
1533 (void)showmatches(&xpc, p_wmnu
1534 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1535#else
1536 (void)showmatches(&xpc, FALSE);
1537#endif
1538 redrawcmd();
1539 did_wild_list = TRUE;
1540 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001541 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1542 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001544 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1545 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 }
1547 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001548 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550#ifdef FEAT_WILDMENU
1551 else if (xpc.xp_numfiles == -1)
1552 xpc.xp_context = EXPAND_NOTHING;
1553#endif
1554 }
1555 if (wim_index < 3)
1556 ++wim_index;
1557 if (c == ESC)
1558 gotesc = TRUE;
1559 if (res == OK)
1560 goto cmdline_changed;
1561 }
1562
1563 gotesc = FALSE;
1564
1565 /* <S-Tab> goes to last match, in a clumsy way */
1566 if (c == K_S_TAB && KeyTyped)
1567 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001568 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1569 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1570 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 goto cmdline_changed;
1572 }
1573
1574 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1575 c = NL;
1576
1577 do_abbr = TRUE; /* default: check for abbreviation */
1578
1579 /*
1580 * Big switch for a typed command line character.
1581 */
1582 switch (c)
1583 {
1584 case K_BS:
1585 case Ctrl_H:
1586 case K_DEL:
1587 case K_KDEL:
1588 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (c == K_KDEL)
1590 c = K_DEL;
1591
1592 /*
1593 * delete current character is the same as backspace on next
1594 * character, except at end of line
1595 */
1596 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1597 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (has_mbyte && c == K_DEL)
1599 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1600 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 if (ccline.cmdpos > 0)
1602 {
1603 char_u *p;
1604
1605 j = ccline.cmdpos;
1606 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 if (has_mbyte)
1608 {
1609 p = mb_prevptr(ccline.cmdbuff, p);
1610 if (c == Ctrl_W)
1611 {
1612 while (p > ccline.cmdbuff && vim_isspace(*p))
1613 p = mb_prevptr(ccline.cmdbuff, p);
1614 i = mb_get_class(p);
1615 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1616 p = mb_prevptr(ccline.cmdbuff, p);
1617 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001618 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
1620 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001621 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 {
1623 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1624 --p;
1625 i = vim_iswordc(p[-1]);
1626 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1627 && vim_iswordc(p[-1]) == i)
1628 --p;
1629 }
1630 else
1631 --p;
1632 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1633 ccline.cmdlen -= j - ccline.cmdpos;
1634 i = ccline.cmdpos;
1635 while (i < ccline.cmdlen)
1636 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1637
1638 /* Truncate at the end, required for multi-byte chars. */
1639 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001640#ifdef FEAT_SEARCH_EXTRA
1641 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001642 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001643 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001644 /* save view settings, so that the screen
1645 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001646 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001647 }
1648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 redrawcmd();
1650 }
1651 else if (ccline.cmdlen == 0 && c != Ctrl_W
1652 && ccline.cmdprompt == NULL && indent == 0)
1653 {
1654 /* In ex and debug mode it doesn't make sense to return. */
1655 if (exmode_active
1656#ifdef FEAT_EVAL
1657 || ccline.cmdfirstc == '>'
1658#endif
1659 )
1660 goto cmdline_not_changed;
1661
Bram Moolenaard23a8232018-02-10 18:45:26 +01001662 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 if (!cmd_silent)
1664 {
1665#ifdef FEAT_RIGHTLEFT
1666 if (cmdmsg_rl)
1667 msg_col = Columns;
1668 else
1669#endif
1670 msg_col = 0;
1671 msg_putchar(' '); /* delete ':' */
1672 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001673#ifdef FEAT_SEARCH_EXTRA
1674 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001675 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 redraw_cmdline = TRUE;
1678 goto returncmd; /* back to cmd mode */
1679 }
1680 goto cmdline_changed;
1681
1682 case K_INS:
1683 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 ccline.overstrike = !ccline.overstrike;
1685#ifdef CURSOR_SHAPE
1686 ui_cursor_shape(); /* may show different cursor shape */
1687#endif
1688 goto cmdline_not_changed;
1689
1690 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001691 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 {
1693 /* ":lmap" mappings exists, toggle use of mappings. */
1694 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001695#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 im_set_active(FALSE); /* Disable input method */
1697#endif
1698 if (b_im_ptr != NULL)
1699 {
1700 if (State & LANGMAP)
1701 *b_im_ptr = B_IMODE_LMAP;
1702 else
1703 *b_im_ptr = B_IMODE_NONE;
1704 }
1705 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001706#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 else
1708 {
1709 /* There are no ":lmap" mappings, toggle IM. When
1710 * 'imdisable' is set don't try getting the status, it's
1711 * always off. */
1712 if ((p_imdisable && b_im_ptr != NULL)
1713 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1714 {
1715 im_set_active(FALSE); /* Disable input method */
1716 if (b_im_ptr != NULL)
1717 *b_im_ptr = B_IMODE_NONE;
1718 }
1719 else
1720 {
1721 im_set_active(TRUE); /* Enable input method */
1722 if (b_im_ptr != NULL)
1723 *b_im_ptr = B_IMODE_IM;
1724 }
1725 }
1726#endif
1727 if (b_im_ptr != NULL)
1728 {
1729 if (b_im_ptr == &curbuf->b_p_iminsert)
1730 set_iminsert_global();
1731 else
1732 set_imsearch_global();
1733 }
1734#ifdef CURSOR_SHAPE
1735 ui_cursor_shape(); /* may show different cursor shape */
1736#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001737#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 /* Show/unshow value of 'keymap' in status lines later. */
1739 status_redraw_curbuf();
1740#endif
1741 goto cmdline_not_changed;
1742
1743/* case '@': only in very old vi */
1744 case Ctrl_U:
1745 /* delete all characters left of the cursor */
1746 j = ccline.cmdpos;
1747 ccline.cmdlen -= j;
1748 i = ccline.cmdpos = 0;
1749 while (i < ccline.cmdlen)
1750 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1751 /* Truncate at the end, required for multi-byte chars. */
1752 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001753#ifdef FEAT_SEARCH_EXTRA
1754 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001755 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 redrawcmd();
1758 goto cmdline_changed;
1759
1760#ifdef FEAT_CLIPBOARD
1761 case Ctrl_Y:
1762 /* Copy the modeless selection, if there is one. */
1763 if (clip_star.state != SELECT_CLEARED)
1764 {
1765 if (clip_star.state == SELECT_DONE)
1766 clip_copy_modeless_selection(TRUE);
1767 goto cmdline_not_changed;
1768 }
1769 break;
1770#endif
1771
1772 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1773 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001774 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001775 * ":normal" runs out of characters. */
1776 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001777 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 goto cmdline_not_changed;
1779
1780 gotesc = TRUE; /* will free ccline.cmdbuff after
1781 putting it in history */
1782 goto returncmd; /* back to cmd mode */
1783
1784 case Ctrl_R: /* insert register */
1785#ifdef USE_ON_FLY_SCROLL
1786 dont_scroll = TRUE; /* disallow scrolling here */
1787#endif
1788 putcmdline('"', TRUE);
1789 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001790 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (i == Ctrl_O)
1792 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1793 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001794 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001795 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 --no_mapping;
1797#ifdef FEAT_EVAL
1798 /*
1799 * Insert the result of an expression.
1800 * Need to save the current command line, to be able to enter
1801 * a new one...
1802 */
1803 new_cmdpos = -1;
1804 if (c == '=')
1805 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001806 if (ccline.cmdfirstc == '=' // can't do this recursively
1807 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
1809 beep_flush();
1810 c = ESC;
1811 }
1812 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815#endif
1816 if (c != ESC) /* use ESC to cancel inserting register */
1817 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001818 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001819
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001820#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001821 /* When there was a serious error abort getting the
1822 * command line. */
1823 if (aborting())
1824 {
1825 gotesc = TRUE; /* will free ccline.cmdbuff after
1826 putting it in history */
1827 goto returncmd; /* back to cmd mode */
1828 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 KeyTyped = FALSE; /* Don't do p_wc completion. */
1831#ifdef FEAT_EVAL
1832 if (new_cmdpos >= 0)
1833 {
1834 /* set_cmdline_pos() was used */
1835 if (new_cmdpos > ccline.cmdlen)
1836 ccline.cmdpos = ccline.cmdlen;
1837 else
1838 ccline.cmdpos = new_cmdpos;
1839 }
1840#endif
1841 }
1842 redrawcmd();
1843 goto cmdline_changed;
1844
1845 case Ctrl_D:
1846 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1847 break; /* Use ^D as normal char instead */
1848
1849 redrawcmd();
1850 continue; /* don't do incremental search now */
1851
1852 case K_RIGHT:
1853 case K_S_RIGHT:
1854 case K_C_RIGHT:
1855 do
1856 {
1857 if (ccline.cmdpos >= ccline.cmdlen)
1858 break;
1859 i = cmdline_charsize(ccline.cmdpos);
1860 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1861 break;
1862 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001864 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 + ccline.cmdpos);
1866 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 ++ccline.cmdpos;
1868 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001869 while ((c == K_S_RIGHT || c == K_C_RIGHT
1870 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (has_mbyte)
1873 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 goto cmdline_not_changed;
1875
1876 case K_LEFT:
1877 case K_S_LEFT:
1878 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001879 if (ccline.cmdpos == 0)
1880 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 do
1882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 if (has_mbyte) /* move to first byte of char */
1885 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1886 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1888 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001889 while (ccline.cmdpos > 0
1890 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001891 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 if (has_mbyte)
1894 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 goto cmdline_not_changed;
1896
1897 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001898 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001899 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
Bram Moolenaar4f974752019-02-17 17:44:42 +01001901#ifdef FEAT_GUI_MSWIN
1902 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1903 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001904 case K_F4:
1905 if (mod_mask == MOD_MASK_ALT)
1906 {
1907 redrawcmd(); /* somehow the cmdline is cleared */
1908 goto cmdline_not_changed;
1909 }
1910 break;
1911#endif
1912
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#ifdef FEAT_MOUSE
1914 case K_MIDDLEDRAG:
1915 case K_MIDDLERELEASE:
1916 goto cmdline_not_changed; /* Ignore mouse */
1917
1918 case K_MIDDLEMOUSE:
1919# ifdef FEAT_GUI
1920 /* When GUI is active, also paste when 'mouse' is empty */
1921 if (!gui.in_use)
1922# endif
1923 if (!mouse_has(MOUSE_COMMAND))
1924 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001925# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001927 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001929# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001930 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 redrawcmd();
1932 goto cmdline_changed;
1933
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001934# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001936 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 redrawcmd();
1938 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001939# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941 case K_LEFTDRAG:
1942 case K_LEFTRELEASE:
1943 case K_RIGHTDRAG:
1944 case K_RIGHTRELEASE:
1945 /* Ignore drag and release events when the button-down wasn't
1946 * seen before. */
1947 if (ignore_drag_release)
1948 goto cmdline_not_changed;
1949 /* FALLTHROUGH */
1950 case K_LEFTMOUSE:
1951 case K_RIGHTMOUSE:
1952 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1953 ignore_drag_release = TRUE;
1954 else
1955 ignore_drag_release = FALSE;
1956# ifdef FEAT_GUI
1957 /* When GUI is active, also move when 'mouse' is empty */
1958 if (!gui.in_use)
1959# endif
1960 if (!mouse_has(MOUSE_COMMAND))
1961 goto cmdline_not_changed; /* Ignore mouse */
1962# ifdef FEAT_CLIPBOARD
1963 if (mouse_row < cmdline_row && clip_star.available)
1964 {
1965 int button, is_click, is_drag;
1966
1967 /*
1968 * Handle modeless selection.
1969 */
1970 button = get_mouse_button(KEY2TERMCAP1(c),
1971 &is_click, &is_drag);
1972 if (mouse_model_popup() && button == MOUSE_LEFT
1973 && (mod_mask & MOD_MASK_SHIFT))
1974 {
1975 /* Translate shift-left to right button. */
1976 button = MOUSE_RIGHT;
1977 mod_mask &= ~MOD_MASK_SHIFT;
1978 }
1979 clip_modeless(button, is_click, is_drag);
1980 goto cmdline_not_changed;
1981 }
1982# endif
1983
1984 set_cmdspos();
1985 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1986 ++ccline.cmdpos)
1987 {
1988 i = cmdline_charsize(ccline.cmdpos);
1989 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1990 && mouse_col < ccline.cmdspos % Columns + i)
1991 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (has_mbyte)
1993 {
1994 /* Count ">" for double-wide char that doesn't fit. */
1995 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001996 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 + ccline.cmdpos) - 1;
1998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 ccline.cmdspos += i;
2000 }
2001 goto cmdline_not_changed;
2002
2003 /* Mouse scroll wheel: ignored here */
2004 case K_MOUSEDOWN:
2005 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002006 case K_MOUSELEFT:
2007 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 /* Alternate buttons ignored here */
2009 case K_X1MOUSE:
2010 case K_X1DRAG:
2011 case K_X1RELEASE:
2012 case K_X2MOUSE:
2013 case K_X2DRAG:
2014 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002015 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 goto cmdline_not_changed;
2017
2018#endif /* FEAT_MOUSE */
2019
2020#ifdef FEAT_GUI
2021 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2022 case K_LEFTRELEASE_NM:
2023 goto cmdline_not_changed;
2024
2025 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002026 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
2028 gui_do_scroll();
2029 redrawcmd();
2030 }
2031 goto cmdline_not_changed;
2032
2033 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002034 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002036 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037 redrawcmd();
2038 }
2039 goto cmdline_not_changed;
2040#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002041#ifdef FEAT_GUI_TABLINE
2042 case K_TABLINE:
2043 case K_TABMENU:
2044 /* Don't want to change any tabs here. Make sure the same tab
2045 * is still selected. */
2046 if (gui_use_tabline())
2047 gui_mch_set_curtab(tabpage_index(curtab));
2048 goto cmdline_not_changed;
2049#endif
2050
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 case K_SELECT: /* end of Select mode mapping - ignore */
2052 goto cmdline_not_changed;
2053
2054 case Ctrl_B: /* begin of command line */
2055 case K_HOME:
2056 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 case K_S_HOME:
2058 case K_C_HOME:
2059 ccline.cmdpos = 0;
2060 set_cmdspos();
2061 goto cmdline_not_changed;
2062
2063 case Ctrl_E: /* end of command line */
2064 case K_END:
2065 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 case K_S_END:
2067 case K_C_END:
2068 ccline.cmdpos = ccline.cmdlen;
2069 set_cmdspos_cursor();
2070 goto cmdline_not_changed;
2071
2072 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002073 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 break;
2075 goto cmdline_changed;
2076
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002077 case Ctrl_L:
2078#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002079 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002080 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002081#endif
2082
2083 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002084 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 break;
2086 goto cmdline_changed;
2087
2088 case Ctrl_N: /* next match */
2089 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002090 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002092 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2093 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002095 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002098 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 case K_UP:
2100 case K_DOWN:
2101 case K_S_UP:
2102 case K_S_DOWN:
2103 case K_PAGEUP:
2104 case K_KPAGEUP:
2105 case K_PAGEDOWN:
2106 case K_KPAGEDOWN:
2107 if (hislen == 0 || firstc == NUL) /* no history */
2108 goto cmdline_not_changed;
2109
2110 i = hiscnt;
2111
2112 /* save current command string so it can be restored later */
2113 if (lookfor == NULL)
2114 {
2115 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2116 goto cmdline_not_changed;
2117 lookfor[ccline.cmdpos] = NUL;
2118 }
2119
2120 j = (int)STRLEN(lookfor);
2121 for (;;)
2122 {
2123 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002124 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002125 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 {
2127 if (hiscnt == hislen) /* first time */
2128 hiscnt = hisidx[histype];
2129 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2130 hiscnt = hislen - 1;
2131 else if (hiscnt != hisidx[histype] + 1)
2132 --hiscnt;
2133 else /* at top of list */
2134 {
2135 hiscnt = i;
2136 break;
2137 }
2138 }
2139 else /* one step forwards */
2140 {
2141 /* on last entry, clear the line */
2142 if (hiscnt == hisidx[histype])
2143 {
2144 hiscnt = hislen;
2145 break;
2146 }
2147
2148 /* not on a history line, nothing to do */
2149 if (hiscnt == hislen)
2150 break;
2151 if (hiscnt == hislen - 1) /* wrap around */
2152 hiscnt = 0;
2153 else
2154 ++hiscnt;
2155 }
2156 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2157 {
2158 hiscnt = i;
2159 break;
2160 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002161 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002162 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 || STRNCMP(history[histype][hiscnt].hisstr,
2164 lookfor, (size_t)j) == 0)
2165 break;
2166 }
2167
2168 if (hiscnt != i) /* jumped to other entry */
2169 {
2170 char_u *p;
2171 int len;
2172 int old_firstc;
2173
Bram Moolenaar438d1762018-09-30 17:11:48 +02002174 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002175 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (hiscnt == hislen)
2177 p = lookfor; /* back to the old one */
2178 else
2179 p = history[histype][hiscnt].hisstr;
2180
2181 if (histype == HIST_SEARCH
2182 && p != lookfor
2183 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2184 {
2185 /* Correct for the separator character used when
2186 * adding the history entry vs the one used now.
2187 * First loop: count length.
2188 * Second loop: copy the characters. */
2189 for (i = 0; i <= 1; ++i)
2190 {
2191 len = 0;
2192 for (j = 0; p[j] != NUL; ++j)
2193 {
2194 /* Replace old sep with new sep, unless it is
2195 * escaped. */
2196 if (p[j] == old_firstc
2197 && (j == 0 || p[j - 1] != '\\'))
2198 {
2199 if (i > 0)
2200 ccline.cmdbuff[len] = firstc;
2201 }
2202 else
2203 {
2204 /* Escape new sep, unless it is already
2205 * escaped. */
2206 if (p[j] == firstc
2207 && (j == 0 || p[j - 1] != '\\'))
2208 {
2209 if (i > 0)
2210 ccline.cmdbuff[len] = '\\';
2211 ++len;
2212 }
2213 if (i > 0)
2214 ccline.cmdbuff[len] = p[j];
2215 }
2216 ++len;
2217 }
2218 if (i == 0)
2219 {
2220 alloc_cmdbuff(len);
2221 if (ccline.cmdbuff == NULL)
2222 goto returncmd;
2223 }
2224 }
2225 ccline.cmdbuff[len] = NUL;
2226 }
2227 else
2228 {
2229 alloc_cmdbuff((int)STRLEN(p));
2230 if (ccline.cmdbuff == NULL)
2231 goto returncmd;
2232 STRCPY(ccline.cmdbuff, p);
2233 }
2234
2235 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2236 redrawcmd();
2237 goto cmdline_changed;
2238 }
2239 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002240#endif
2241 goto cmdline_not_changed;
2242
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002243#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002244 case Ctrl_G: /* next match */
2245 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002246 if (may_adjust_incsearch_highlighting(
2247 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002248 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#endif
2251
2252 case Ctrl_V:
2253 case Ctrl_Q:
2254#ifdef FEAT_MOUSE
2255 ignore_drag_release = TRUE;
2256#endif
2257 putcmdline('^', TRUE);
2258 c = get_literal(); /* get next (two) character(s) */
2259 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002260 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 /* may need to remove ^ when composing char was typed */
2262 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2263 {
2264 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2265 msg_putchar(' ');
2266 cursorcmd();
2267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 break;
2269
2270#ifdef FEAT_DIGRAPHS
2271 case Ctrl_K:
2272#ifdef FEAT_MOUSE
2273 ignore_drag_release = TRUE;
2274#endif
2275 putcmdline('?', TRUE);
2276#ifdef USE_ON_FLY_SCROLL
2277 dont_scroll = TRUE; /* disallow scrolling here */
2278#endif
2279 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002280 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 if (c != NUL)
2282 break;
2283
2284 redrawcmd();
2285 goto cmdline_not_changed;
2286#endif /* FEAT_DIGRAPHS */
2287
2288#ifdef FEAT_RIGHTLEFT
2289 case Ctrl__: /* CTRL-_: switch language mode */
2290 if (!p_ari)
2291 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002292 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 goto cmdline_not_changed;
2294#endif
2295
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002296 case K_PS:
2297 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2298 goto cmdline_changed;
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 default:
2301#ifdef UNIX
2302 if (c == intr_char)
2303 {
2304 gotesc = TRUE; /* will free ccline.cmdbuff after
2305 putting it in history */
2306 goto returncmd; /* back to Normal mode */
2307 }
2308#endif
2309 /*
2310 * Normal character with no special meaning. Just set mod_mask
2311 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2312 * the string <S-Space>. This should only happen after ^V.
2313 */
2314 if (!IS_SPECIAL(c))
2315 mod_mask = 0x0;
2316 break;
2317 }
2318 /*
2319 * End of switch on command line character.
2320 * We come here if we have a normal character.
2321 */
2322
Bram Moolenaar13505972019-01-24 15:04:48 +01002323 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2324 && (ccheck_abbr(
2325 // Add ABBR_OFF for characters above 0x100, this is
2326 // what check_abbr() expects.
2327 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2328 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 goto cmdline_changed;
2330
2331 /*
2332 * put the character in the command line
2333 */
2334 if (IS_SPECIAL(c) || mod_mask != 0)
2335 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2336 else
2337 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 if (has_mbyte)
2339 {
2340 j = (*mb_char2bytes)(c, IObuff);
2341 IObuff[j] = NUL; /* exclude composing chars */
2342 put_on_cmdline(IObuff, j, TRUE);
2343 }
2344 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
2346 IObuff[0] = c;
2347 put_on_cmdline(IObuff, 1, TRUE);
2348 }
2349 }
2350 goto cmdline_changed;
2351
2352/*
2353 * This part implements incremental searches for "/" and "?"
2354 * Jump to cmdline_not_changed when a character has been read but the command
2355 * line did not change. Then we only search and redraw if something changed in
2356 * the past.
2357 * Jump to cmdline_changed when the command line did change.
2358 * (Sorry for the goto's, I know it is ugly).
2359 */
2360cmdline_not_changed:
2361#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002362 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 continue;
2364#endif
2365
2366cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002367 /* Trigger CmdlineChanged autocommands. */
2368 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002369
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002371 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372#endif
2373
2374#ifdef FEAT_RIGHTLEFT
2375 if (cmdmsg_rl
2376# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002377 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378# endif
2379 )
2380 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002381 * right-left typing. Not efficient, but it works.
2382 * Do it only when there are no characters left to read
2383 * to avoid useless intermediate redraws. */
2384 if (vpeekc() == NUL)
2385 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386#endif
2387 }
2388
2389returncmd:
2390
2391#ifdef FEAT_RIGHTLEFT
2392 cmdmsg_rl = FALSE;
2393#endif
2394
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002396 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002399 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400#endif
2401
2402 if (ccline.cmdbuff != NULL)
2403 {
2404 /*
2405 * Put line in history buffer (":" and "=" only when it was typed).
2406 */
2407#ifdef FEAT_CMDHIST
2408 if (ccline.cmdlen && firstc != NUL
2409 && (some_key_typed || histype == HIST_SEARCH))
2410 {
2411 add_to_history(histype, ccline.cmdbuff, TRUE,
2412 histype == HIST_SEARCH ? firstc : NUL);
2413 if (firstc == ':')
2414 {
2415 vim_free(new_last_cmdline);
2416 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2417 }
2418 }
2419#endif
2420
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002421 if (gotesc)
2422 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 }
2424
2425 /*
2426 * If the screen was shifted up, redraw the whole screen (later).
2427 * If the line is too long, clear it, so ruler and shown command do
2428 * not get printed in the middle of it.
2429 */
2430 msg_check();
2431 msg_scroll = save_msg_scroll;
2432 redir_off = FALSE;
2433
2434 /* When the command line was typed, no need for a wait-return prompt. */
2435 if (some_key_typed)
2436 need_wait_return = FALSE;
2437
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002438 /* Trigger CmdlineLeave autocommands. */
2439 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002442#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2444 im_save_status(b_im_ptr);
2445 im_set_active(FALSE);
2446#endif
2447#ifdef FEAT_MOUSE
2448 setmouse();
2449#endif
2450#ifdef CURSOR_SHAPE
2451 ui_cursor_shape(); /* may show different cursor shape */
2452#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002453 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
Bram Moolenaar438d1762018-09-30 17:11:48 +02002455theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002456 {
2457 char_u *p = ccline.cmdbuff;
2458
Bram Moolenaar438d1762018-09-30 17:11:48 +02002459 if (did_save_ccline)
2460 restore_cmdline(&save_ccline);
2461 else
2462 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002463 return p;
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465}
2466
2467#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2468/*
2469 * Get a command line with a prompt.
2470 * This is prepared to be called recursively from getcmdline() (e.g. by
2471 * f_input() when evaluating an expression from CTRL-R =).
2472 * Returns the command line in allocated memory, or NULL.
2473 */
2474 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002475getcmdline_prompt(
2476 int firstc,
2477 char_u *prompt, /* command line prompt */
2478 int attr, /* attributes for prompt */
2479 int xp_context, /* type of expansion */
2480 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 char_u *s;
2483 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002484 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002486 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487
Bram Moolenaar438d1762018-09-30 17:11:48 +02002488 if (ccline.cmdbuff != NULL)
2489 {
2490 // Save the values of the current cmdline and restore them below.
2491 save_cmdline(&save_ccline);
2492 did_save_ccline = TRUE;
2493 }
2494
2495 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 ccline.cmdprompt = prompt;
2497 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002498# ifdef FEAT_EVAL
2499 ccline.xp_context = xp_context;
2500 ccline.xp_arg = xp_arg;
2501 ccline.input_fn = (firstc == '@');
2502# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002503 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002504 s = getcmdline_int(firstc, 1L, 0, FALSE);
2505
2506 if (did_save_ccline)
2507 restore_cmdline(&save_ccline);
2508
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002509 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002510 /* Restore msg_col, the prompt from input() may have changed it.
2511 * But only if called recursively and the commandline is therefore being
2512 * restored to an old one; if not, the input() prompt stays on the screen,
2513 * so we need its modified msg_col left intact. */
2514 if (ccline.cmdbuff != NULL)
2515 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516
2517 return s;
2518}
2519#endif
2520
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002521/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002522 * Return TRUE when the text must not be changed and we can't switch to
2523 * another window or buffer. Used when editing the command line, evaluating
2524 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002525 */
2526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002527text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002528{
2529#ifdef FEAT_CMDWIN
2530 if (cmdwin_type != 0)
2531 return TRUE;
2532#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002533 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002534}
2535
2536/*
2537 * Give an error message for a command that isn't allowed while the cmdline
2538 * window is open or editing the cmdline in another way.
2539 */
2540 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002541text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002542{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002543 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002544}
2545
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002546 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002547get_text_locked_msg(void)
2548{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549#ifdef FEAT_CMDWIN
2550 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002551 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002552#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002553 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002554}
2555
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002556/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002557 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2558 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002559 */
2560 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002561curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002562{
2563 if (curbuf_lock > 0)
2564 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002565 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002566 return TRUE;
2567 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002568 return allbuf_locked();
2569}
2570
2571/*
2572 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2573 * message.
2574 */
2575 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002576allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002577{
2578 if (allbuf_lock > 0)
2579 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002580 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002581 return TRUE;
2582 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002583 return FALSE;
2584}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002587cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2590 if (cmdline_star > 0) /* showing '*', always 1 position */
2591 return 1;
2592#endif
2593 return ptr2cells(ccline.cmdbuff + idx);
2594}
2595
2596/*
2597 * Compute the offset of the cursor on the command line for the prompt and
2598 * indent.
2599 */
2600 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002601set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002603 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 ccline.cmdspos = 1 + ccline.cmdindent;
2605 else
2606 ccline.cmdspos = 0 + ccline.cmdindent;
2607}
2608
2609/*
2610 * Compute the screen position for the cursor on the command line.
2611 */
2612 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002613set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 int i, m, c;
2616
2617 set_cmdspos();
2618 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002619 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002621 if (m < 0) /* overflow, Columns or Rows at weird value */
2622 m = MAXCOL;
2623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 else
2625 m = MAXCOL;
2626 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2627 {
2628 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2630 if (has_mbyte)
2631 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002632 /* If the cmdline doesn't fit, show cursor on last visible char.
2633 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 if ((ccline.cmdspos += c) >= m)
2635 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 ccline.cmdspos -= c;
2637 break;
2638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002640 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 }
2642}
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644/*
2645 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2646 * character that doesn't fit, so that a ">" must be displayed.
2647 */
2648 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002649correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002651 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2653 && ccline.cmdspos % Columns + cells > Columns)
2654 ccline.cmdspos++;
2655}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657/*
2658 * Get an Ex command line for the ":" command.
2659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002661getexline(
2662 int c, /* normally ':', NUL for ":append" */
2663 void *cookie UNUSED,
2664 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665{
2666 /* When executing a register, remove ':' that's in front of each line. */
2667 if (exec_from_reg && vpeekc() == ':')
2668 (void)vgetc();
2669 return getcmdline(c, 1L, indent);
2670}
2671
2672/*
2673 * Get an Ex command line for Ex mode.
2674 * In Ex mode we only use the OS supplied line editing features and no
2675 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002676 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002679getexmodeline(
2680 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002682 void *cookie UNUSED,
2683 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002685 garray_T line_ga;
2686 char_u *pend;
2687 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002688 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002689 int escaped = FALSE; /* CTRL-V typed */
2690 int vcol = 0;
2691 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002692 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002693 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694
2695 /* Switch cursor on now. This avoids that it happens after the "\n", which
2696 * confuses the system function that computes tabstops. */
2697 cursor_on();
2698
2699 /* always start in column 0; write a newline if necessary */
2700 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002701 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002703 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002705 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 if (p_prompt)
2707 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 while (indent-- > 0)
2709 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
2712
2713 ga_init2(&line_ga, 1, 30);
2714
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002715 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002716 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002717 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002718 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002719 while (indent >= 8)
2720 {
2721 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002722 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002723 indent -= 8;
2724 }
2725 while (indent-- > 0)
2726 {
2727 ga_append(&line_ga, ' ');
2728 msg_putchar(' ');
2729 }
2730 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 ++no_mapping;
2732 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002733
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 /*
2735 * Get the line, one character at a time.
2736 */
2737 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002740 long sw;
2741 char_u *s;
2742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 if (ga_grow(&line_ga, 40) == FAIL)
2744 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745
Bram Moolenaarba748c82017-02-26 14:00:07 +01002746 /*
2747 * Get one character at a time.
2748 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002749 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002750
2751 /* Check for a ":normal" command and no more characters left. */
2752 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2753 c1 = '\n';
2754 else
2755 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756
2757 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 * Handle line editing.
2759 * Previously this was left to the system, putting the terminal in
2760 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002764 msg_putchar('\n');
2765 break;
2766 }
2767
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002768 if (c1 == K_PS)
2769 {
2770 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2771 goto redraw;
2772 }
2773
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002774 if (!escaped)
2775 {
2776 /* CR typed means "enter", which is NL */
2777 if (c1 == '\r')
2778 c1 = '\n';
2779
2780 if (c1 == BS || c1 == K_BS
2781 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 if (line_ga.ga_len > 0)
2784 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002785 if (has_mbyte)
2786 {
2787 p = (char_u *)line_ga.ga_data;
2788 p[line_ga.ga_len] = NUL;
2789 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2790 line_ga.ga_len -= len;
2791 }
2792 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002793 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002794 goto redraw;
2795 }
2796 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797 }
2798
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002799 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 msg_col = startcol;
2802 msg_clr_eos();
2803 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002804 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002805 }
2806
2807 if (c1 == Ctrl_T)
2808 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002809 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810 p = (char_u *)line_ga.ga_data;
2811 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002812 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002813 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002814add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002815 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002817 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002818 p = (char_u *)line_ga.ga_data;
2819 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2821 *s = ' ';
2822 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824redraw:
2825 /* redraw the line */
2826 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002828 p = (char_u *)line_ga.ga_data;
2829 p[line_ga.ga_len] = NUL;
2830 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002834 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002836 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002837 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002841 len = MB_PTR2LEN(p);
2842 msg_outtrans_len(p, len);
2843 vcol += ptr2cells(p);
2844 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002847 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002848 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 continue;
2850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 if (c1 == Ctrl_D)
2853 {
2854 /* Delete one shiftwidth. */
2855 p = (char_u *)line_ga.ga_data;
2856 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002858 if (prev_char == '^')
2859 ex_keep_indent = TRUE;
2860 indent = 0;
2861 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862 }
2863 else
2864 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002866 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002867 if (indent > 0)
2868 {
2869 --indent;
2870 indent -= indent % get_sw_value(curbuf);
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002873 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002875 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2877 --line_ga.ga_len;
2878 }
2879 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881
2882 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2883 {
2884 escaped = TRUE;
2885 continue;
2886 }
2887
2888 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2889 if (IS_SPECIAL(c1))
2890 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002892
2893 if (IS_SPECIAL(c1))
2894 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002895 if (has_mbyte)
2896 len = (*mb_char2bytes)(c1,
2897 (char_u *)line_ga.ga_data + line_ga.ga_len);
2898 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002899 {
2900 len = 1;
2901 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2902 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903 if (c1 == '\n')
2904 msg_putchar('\n');
2905 else if (c1 == TAB)
2906 {
2907 /* Don't use chartabsize(), 'ts' can be different */
2908 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002910 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002914 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002915 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002916 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002918 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002919 escaped = FALSE;
2920
2921 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002922 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002923
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002924 /* We are done when a NL is entered, but not when it comes after an
2925 * odd number of backslashes, that results in a NUL. */
2926 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002928 int bcount = 0;
2929
2930 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2931 ++bcount;
2932
2933 if (bcount > 0)
2934 {
2935 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2936 * "\NL", etc. */
2937 line_ga.ga_len -= (bcount + 1) / 2;
2938 pend -= (bcount + 1) / 2;
2939 pend[-1] = '\n';
2940 }
2941
2942 if ((bcount & 1) == 0)
2943 {
2944 --line_ga.ga_len;
2945 --pend;
2946 *pend = NUL;
2947 break;
2948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950 }
2951
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002952 --no_mapping;
2953 --allow_keys;
2954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 /* make following messages go to the next line */
2956 msg_didout = FALSE;
2957 msg_col = 0;
2958 if (msg_row < Rows - 1)
2959 ++msg_row;
2960 emsg_on_display = FALSE; /* don't want ui_delay() */
2961
2962 if (got_int)
2963 ga_clear(&line_ga);
2964
2965 return (char_u *)line_ga.ga_data;
2966}
2967
Bram Moolenaare344bea2005-09-01 20:46:49 +00002968# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2969 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970/*
2971 * Return TRUE if ccline.overstrike is on.
2972 */
2973 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002974cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 return ccline.overstrike;
2977}
2978
2979/*
2980 * Return TRUE if the cursor is at the end of the cmdline.
2981 */
2982 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002983cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 return (ccline.cmdpos >= ccline.cmdlen);
2986}
2987#endif
2988
Bram Moolenaar9372a112005-12-06 19:59:18 +00002989#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
2991 * Return the virtual column number at the current cursor position.
2992 * This is used by the IM code to obtain the start of the preedit string.
2993 */
2994 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2998 return MAXCOL;
2999
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 if (has_mbyte)
3001 {
3002 colnr_T col;
3003 int i = 0;
3004
3005 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003006 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007
3008 return col;
3009 }
3010 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 return ccline.cmdpos;
3012}
3013#endif
3014
3015#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3016/*
3017 * If part of the command line is an IM preedit string, redraw it with
3018 * IM feedback attributes. The cursor position is restored after drawing.
3019 */
3020 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003021redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022{
3023 if ((State & CMDLINE)
3024 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003025 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 && !p_imdisable
3027 && im_is_preediting())
3028 {
3029 int cmdpos = 0;
3030 int cmdspos;
3031 int old_row;
3032 int old_col;
3033 colnr_T col;
3034
3035 old_row = msg_row;
3036 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003037 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 if (has_mbyte)
3040 {
3041 for (col = 0; col < preedit_start_col
3042 && cmdpos < ccline.cmdlen; ++col)
3043 {
3044 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003045 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047 }
3048 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
3050 cmdspos += preedit_start_col;
3051 cmdpos += preedit_start_col;
3052 }
3053
3054 msg_row = cmdline_row + (cmdspos / (int)Columns);
3055 msg_col = cmdspos % (int)Columns;
3056 if (msg_row >= Rows)
3057 msg_row = Rows - 1;
3058
3059 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3060 {
3061 int char_len;
3062 int char_attr;
3063
3064 char_attr = im_get_feedback_attr(col);
3065 if (char_attr < 0)
3066 break; /* end of preedit string */
3067
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003069 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 char_len = 1;
3072
3073 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3074 cmdpos += char_len;
3075 }
3076
3077 msg_row = old_row;
3078 msg_col = old_col;
3079 }
3080}
3081#endif /* FEAT_XIM && FEAT_GUI_GTK */
3082
3083/*
3084 * Allocate a new command line buffer.
3085 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 */
3087 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003088alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089{
3090 /*
3091 * give some extra space to avoid having to allocate all the time
3092 */
3093 if (len < 80)
3094 len = 100;
3095 else
3096 len += 20;
3097
3098 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3099 ccline.cmdbufflen = len;
3100}
3101
3102/*
3103 * Re-allocate the command line to length len + something extra.
3104 * return FAIL for failure, OK otherwise
3105 */
3106 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003107realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
3109 char_u *p;
3110
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003111 if (len < ccline.cmdbufflen)
3112 return OK; /* no need to resize */
3113
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 p = ccline.cmdbuff;
3115 alloc_cmdbuff(len); /* will get some more */
3116 if (ccline.cmdbuff == NULL) /* out of memory */
3117 {
3118 ccline.cmdbuff = p; /* keep the old one */
3119 return FAIL;
3120 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003121 /* There isn't always a NUL after the command, but it may need to be
3122 * there, thus copy up to the NUL and add a NUL. */
3123 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3124 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003126
3127 if (ccline.xpc != NULL
3128 && ccline.xpc->xp_pattern != NULL
3129 && ccline.xpc->xp_context != EXPAND_NOTHING
3130 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3131 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003132 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003133
3134 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3135 * to point into the newly allocated memory. */
3136 if (i >= 0 && i <= ccline.cmdlen)
3137 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3138 }
3139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 return OK;
3141}
3142
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003143#if defined(FEAT_ARABIC) || defined(PROTO)
3144static char_u *arshape_buf = NULL;
3145
3146# if defined(EXITFREE) || defined(PROTO)
3147 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003149{
3150 vim_free(arshape_buf);
3151}
3152# endif
3153#endif
3154
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155/*
3156 * Draw part of the cmdline at the current cursor position. But draw stars
3157 * when cmdline_star is TRUE.
3158 */
3159 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003160draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161{
3162#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3163 int i;
3164
3165 if (cmdline_star > 0)
3166 for (i = 0; i < len; ++i)
3167 {
3168 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003170 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 }
3172 else
3173#endif
3174#ifdef FEAT_ARABIC
3175 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3176 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 static int buflen = 0;
3178 char_u *p;
3179 int j;
3180 int newlen = 0;
3181 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003182 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int prev_c = 0;
3184 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003185 int u8c;
3186 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188
3189 /*
3190 * Do arabic shaping into a temporary buffer. This is very
3191 * inefficient!
3192 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003193 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 {
3195 /* Re-allocate the buffer. We keep it around to avoid a lot of
3196 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003197 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003198 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003199 arshape_buf = alloc(buflen);
3200 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 return; /* out of memory */
3202 }
3203
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003204 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3205 {
3206 /* Prepend a space to draw the leading composing char on. */
3207 arshape_buf[0] = ' ';
3208 newlen = 1;
3209 }
3210
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 for (j = start; j < start + len; j += mb_l)
3212 {
3213 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003214 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003215 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 if (ARABIC_CHAR(u8c))
3217 {
3218 /* Do Arabic shaping. */
3219 if (cmdmsg_rl)
3220 {
3221 /* displaying from right to left */
3222 pc = prev_c;
3223 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003224 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (j + mb_l >= start + len)
3226 nc = NUL;
3227 else
3228 nc = utf_ptr2char(p + mb_l);
3229 }
3230 else
3231 {
3232 /* displaying from left to right */
3233 if (j + mb_l >= start + len)
3234 pc = NUL;
3235 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003236 {
3237 int pcc[MAX_MCO];
3238
3239 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003241 pc1 = pcc[0];
3242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 nc = prev_c;
3244 }
3245 prev_c = u8c;
3246
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003247 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003249 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003250 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003252 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3253 if (u8cc[1] != 0)
3254 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003255 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257 }
3258 else
3259 {
3260 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 newlen += mb_l;
3263 }
3264 }
3265
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003266 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 }
3268 else
3269#endif
3270 msg_outtrans_len(ccline.cmdbuff + start, len);
3271}
3272
3273/*
3274 * Put a character on the command line. Shifts the following text to the
3275 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3276 * "c" must be printable (fit in one display cell)!
3277 */
3278 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003279putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280{
3281 if (cmd_silent)
3282 return;
3283 msg_no_more = TRUE;
3284 msg_putchar(c);
3285 if (shift)
3286 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3287 msg_no_more = FALSE;
3288 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003289 extra_char = c;
3290 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291}
3292
3293/*
3294 * Undo a putcmdline(c, FALSE).
3295 */
3296 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003297unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298{
3299 if (cmd_silent)
3300 return;
3301 msg_no_more = TRUE;
3302 if (ccline.cmdlen == ccline.cmdpos)
3303 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003304 else if (has_mbyte)
3305 draw_cmdline(ccline.cmdpos,
3306 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
3308 draw_cmdline(ccline.cmdpos, 1);
3309 msg_no_more = FALSE;
3310 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003311 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313
3314/*
3315 * Put the given string, of the given length, onto the command line.
3316 * If len is -1, then STRLEN() is used to calculate the length.
3317 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3318 * part will be redrawn, otherwise it will not. If this function is called
3319 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3320 * called afterwards.
3321 */
3322 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003323put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324{
3325 int retval;
3326 int i;
3327 int m;
3328 int c;
3329
3330 if (len < 0)
3331 len = (int)STRLEN(str);
3332
3333 /* Check if ccline.cmdbuff needs to be longer */
3334 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003335 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 else
3337 retval = OK;
3338 if (retval == OK)
3339 {
3340 if (!ccline.overstrike)
3341 {
3342 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3343 ccline.cmdbuff + ccline.cmdpos,
3344 (size_t)(ccline.cmdlen - ccline.cmdpos));
3345 ccline.cmdlen += len;
3346 }
3347 else
3348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (has_mbyte)
3350 {
3351 /* Count nr of characters in the new string. */
3352 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003353 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 ++m;
3355 /* Count nr of bytes in cmdline that are overwritten by these
3356 * characters. */
3357 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003358 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 --m;
3360 if (i < ccline.cmdlen)
3361 {
3362 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3363 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3364 ccline.cmdlen += ccline.cmdpos + len - i;
3365 }
3366 else
3367 ccline.cmdlen = ccline.cmdpos + len;
3368 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003369 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 ccline.cmdlen = ccline.cmdpos + len;
3371 }
3372 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3373 ccline.cmdbuff[ccline.cmdlen] = NUL;
3374
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 if (enc_utf8)
3376 {
3377 /* When the inserted text starts with a composing character,
3378 * backup to the character before it. There could be two of them.
3379 */
3380 i = 0;
3381 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3382 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3383 {
3384 i = (*mb_head_off)(ccline.cmdbuff,
3385 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3386 ccline.cmdpos -= i;
3387 len += i;
3388 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3389 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003390#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3392 {
3393 /* Check the previous character for Arabic combining pair. */
3394 i = (*mb_head_off)(ccline.cmdbuff,
3395 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3396 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3397 + ccline.cmdpos - i), c))
3398 {
3399 ccline.cmdpos -= i;
3400 len += i;
3401 }
3402 else
3403 i = 0;
3404 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 if (i != 0)
3407 {
3408 /* Also backup the cursor position. */
3409 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3410 ccline.cmdspos -= i;
3411 msg_col -= i;
3412 if (msg_col < 0)
3413 {
3414 msg_col += Columns;
3415 --msg_row;
3416 }
3417 }
3418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419
3420 if (redraw && !cmd_silent)
3421 {
3422 msg_no_more = TRUE;
3423 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003424 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3426 /* Avoid clearing the rest of the line too often. */
3427 if (cmdline_row != i || ccline.overstrike)
3428 msg_clr_eos();
3429 msg_no_more = FALSE;
3430 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003431 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003433 m = Columns * Rows;
3434 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003436 }
3437 else
3438 m = MAXCOL;
3439 for (i = 0; i < len; ++i)
3440 {
3441 c = cmdline_charsize(ccline.cmdpos);
3442 /* count ">" for a double-wide char that doesn't fit. */
3443 if (has_mbyte)
3444 correct_cmdspos(ccline.cmdpos, c);
3445 /* Stop cursor at the end of the screen, but do increment the
3446 * insert position, so that entering a very long command
3447 * works, even though you can't see it. */
3448 if (ccline.cmdspos + c < m)
3449 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003450
Bram Moolenaar14184a32019-02-16 15:10:30 +01003451 if (has_mbyte)
3452 {
3453 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3454 if (c > len - i - 1)
3455 c = len - i - 1;
3456 ccline.cmdpos += c;
3457 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003459 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 }
3461 }
3462 if (redraw)
3463 msg_check();
3464 return retval;
3465}
3466
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003467static struct cmdline_info prev_ccline;
3468static int prev_ccline_used = FALSE;
3469
3470/*
3471 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3472 * and overwrite it. But get_cmdline_str() may need it, thus make it
3473 * available globally in prev_ccline.
3474 */
3475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003476save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003477{
3478 if (!prev_ccline_used)
3479 {
3480 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3481 prev_ccline_used = TRUE;
3482 }
3483 *ccp = prev_ccline;
3484 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003485 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003486}
3487
3488/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003489 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003490 */
3491 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003492restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003493{
3494 ccline = prev_ccline;
3495 prev_ccline = *ccp;
3496}
3497
Bram Moolenaar8299df92004-07-10 09:47:34 +00003498/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003499 * Paste a yank register into the command line.
3500 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003501 * insert_reg() can't be used here, because special characters from the
3502 * register contents will be interpreted as commands.
3503 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003504 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003505 */
3506 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507cmdline_paste(
3508 int regname,
3509 int literally, /* Insert text literally instead of "as typed" */
3510 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003511{
3512 long i;
3513 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003514 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003515 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003516
3517 /* check for valid regname; also accept special characters for CTRL-R in
3518 * the command line */
3519 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003520 && regname != Ctrl_A && regname != Ctrl_L
3521 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003522 return FAIL;
3523
3524 /* A register containing CTRL-R can cause an endless loop. Allow using
3525 * CTRL-C to break the loop. */
3526 line_breakcheck();
3527 if (got_int)
3528 return FAIL;
3529
3530#ifdef FEAT_CLIPBOARD
3531 regname = may_get_selection(regname);
3532#endif
3533
Bram Moolenaar438d1762018-09-30 17:11:48 +02003534 // Need to set "textlock" to avoid nasty things like going to another
3535 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003536 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003537 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003538 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003539
3540 if (i)
3541 {
3542 /* Got the value of a special register in "arg". */
3543 if (arg == NULL)
3544 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003545
3546 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3547 * part of the word. */
3548 p = arg;
3549 if (p_is && regname == Ctrl_W)
3550 {
3551 char_u *w;
3552 int len;
3553
3554 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003555 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003556 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003557 if (has_mbyte)
3558 {
3559 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3560 if (!vim_iswordc(mb_ptr2char(w - len)))
3561 break;
3562 w -= len;
3563 }
3564 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003565 {
3566 if (!vim_iswordc(w[-1]))
3567 break;
3568 --w;
3569 }
3570 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003571 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003572 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3573 p += len;
3574 }
3575
3576 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003577 if (allocated)
3578 vim_free(arg);
3579 return OK;
3580 }
3581
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003582 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003583}
3584
3585/*
3586 * Put a string on the command line.
3587 * When "literally" is TRUE, insert literally.
3588 * When "literally" is FALSE, insert as typed, but don't leave the command
3589 * line.
3590 */
3591 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003592cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003593{
3594 int c, cv;
3595
3596 if (literally)
3597 put_on_cmdline(s, -1, TRUE);
3598 else
3599 while (*s != NUL)
3600 {
3601 cv = *s;
3602 if (cv == Ctrl_V && s[1])
3603 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003604 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003605 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003607 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003608 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3609 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610#ifdef UNIX
3611 || c == intr_char
3612#endif
3613 || (c == Ctrl_BSL && *s == Ctrl_N))
3614 stuffcharReadbuff(Ctrl_V);
3615 stuffcharReadbuff(c);
3616 }
3617}
3618
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619#ifdef FEAT_WILDMENU
3620/*
3621 * Delete characters on the command line, from "from" to the current
3622 * position.
3623 */
3624 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003625cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626{
3627 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3628 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3629 ccline.cmdlen -= ccline.cmdpos - from;
3630 ccline.cmdpos = from;
3631}
3632#endif
3633
3634/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003635 * This function is called when the screen size changes and with incremental
3636 * search and in other situations where the command line may have been
3637 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 */
3639 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003640redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003642 redrawcmdline_ex(TRUE);
3643}
3644
3645 void
3646redrawcmdline_ex(int do_compute_cmdrow)
3647{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 if (cmd_silent)
3649 return;
3650 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003651 if (do_compute_cmdrow)
3652 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 redrawcmd();
3654 cursorcmd();
3655}
3656
3657 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003658redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659{
3660 int i;
3661
3662 if (cmd_silent)
3663 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003664 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 msg_putchar(ccline.cmdfirstc);
3666 if (ccline.cmdprompt != NULL)
3667 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003668 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3670 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003671 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 --ccline.cmdindent;
3673 }
3674 else
3675 for (i = ccline.cmdindent; i > 0; --i)
3676 msg_putchar(' ');
3677}
3678
3679/*
3680 * Redraw what is currently on the command line.
3681 */
3682 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003683redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684{
3685 if (cmd_silent)
3686 return;
3687
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003688 /* when 'incsearch' is set there may be no command line while redrawing */
3689 if (ccline.cmdbuff == NULL)
3690 {
3691 windgoto(cmdline_row, 0);
3692 msg_clr_eos();
3693 return;
3694 }
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 msg_start();
3697 redrawcmdprompt();
3698
3699 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3700 msg_no_more = TRUE;
3701 draw_cmdline(0, ccline.cmdlen);
3702 msg_clr_eos();
3703 msg_no_more = FALSE;
3704
3705 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003706 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003707 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708
3709 /*
3710 * An emsg() before may have set msg_scroll. This is used in normal mode,
3711 * in cmdline mode we can reset them now.
3712 */
3713 msg_scroll = FALSE; /* next message overwrites cmdline */
3714
3715 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3716 * in cmdline mode */
3717 skip_redraw = FALSE;
3718}
3719
3720 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003721compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003723 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 cmdline_row = Rows - 1;
3725 else
3726 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003727 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728}
3729
3730 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003731cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732{
3733 if (cmd_silent)
3734 return;
3735
3736#ifdef FEAT_RIGHTLEFT
3737 if (cmdmsg_rl)
3738 {
3739 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3740 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3741 if (msg_row <= 0)
3742 msg_row = Rows - 1;
3743 }
3744 else
3745#endif
3746 {
3747 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3748 msg_col = ccline.cmdspos % (int)Columns;
3749 if (msg_row >= Rows)
3750 msg_row = Rows - 1;
3751 }
3752
3753 windgoto(msg_row, msg_col);
3754#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003755 if (p_imst == IM_ON_THE_SPOT)
3756 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757#endif
3758#ifdef MCH_CURSOR_SHAPE
3759 mch_update_cursor();
3760#endif
3761}
3762
3763 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003764gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
3766 msg_start();
3767#ifdef FEAT_RIGHTLEFT
3768 if (cmdmsg_rl)
3769 msg_col = Columns - 1;
3770 else
3771#endif
3772 msg_col = 0; /* always start in column 0 */
3773 if (clr) /* clear the bottom line(s) */
3774 msg_clr_eos(); /* will reset clear_cmdline */
3775 windgoto(cmdline_row, 0);
3776}
3777
3778/*
3779 * Check the word in front of the cursor for an abbreviation.
3780 * Called when the non-id character "c" has been entered.
3781 * When an abbreviation is recognized it is removed from the text with
3782 * backspaces and the replacement string is inserted, followed by "c".
3783 */
3784 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003785ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003787 int spos = 0;
3788
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3790 return FALSE;
3791
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003792 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3793 * Actually accepts any mark. */
3794 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3795 spos++;
3796 if (ccline.cmdlen - spos > 5
3797 && ccline.cmdbuff[spos] == '\''
3798 && ccline.cmdbuff[spos + 2] == ','
3799 && ccline.cmdbuff[spos + 3] == '\'')
3800 spos += 5;
3801 else
3802 /* check abbreviation from the beginning of the commandline */
3803 spos = 0;
3804
3805 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806}
3807
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003808#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3809 static int
3810#ifdef __BORLANDC__
3811_RTLENTRYF
3812#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003813sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003814{
3815 char_u *p1 = *(char_u **)s1;
3816 char_u *p2 = *(char_u **)s2;
3817
3818 if (*p1 != '<' && *p2 == '<') return -1;
3819 if (*p1 == '<' && *p2 != '<') return 1;
3820 return STRCMP(p1, p2);
3821}
3822#endif
3823
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824/*
3825 * Return FAIL if this is not an appropriate context in which to do
3826 * completion of anything, return OK if it is (even if there are no matches).
3827 * For the caller, this means that the character is just passed through like a
3828 * normal character (instead of being expanded). This allows :s/^I^D etc.
3829 */
3830 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003831nextwild(
3832 expand_T *xp,
3833 int type,
3834 int options, /* extra options for ExpandOne() */
3835 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
3837 int i, j;
3838 char_u *p1;
3839 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 int difflen;
3841 int v;
3842
3843 if (xp->xp_numfiles == -1)
3844 {
3845 set_expand_context(xp);
3846 cmd_showtail = expand_showtail(xp);
3847 }
3848
3849 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3850 {
3851 beep_flush();
3852 return OK; /* Something illegal on command line */
3853 }
3854 if (xp->xp_context == EXPAND_NOTHING)
3855 {
3856 /* Caller can use the character as a normal char instead */
3857 return FAIL;
3858 }
3859
Bram Moolenaar32526b32019-01-19 17:43:09 +01003860 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 out_flush();
3862
3863 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003864 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865
3866 if (type == WILD_NEXT || type == WILD_PREV)
3867 {
3868 /*
3869 * Get next/previous match for a previous expanded pattern.
3870 */
3871 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3872 }
3873 else
3874 {
3875 /*
3876 * Translate string into pattern and expand it.
3877 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003878 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3879 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 p2 = NULL;
3881 else
3882 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003883 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003884 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3885 if (escape)
3886 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003887
3888 if (p_wic)
3889 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003890 p2 = ExpandOne(xp, p1,
3891 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003892 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003894 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (p2 != NULL && type == WILD_LONGEST)
3896 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003897 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (ccline.cmdbuff[i + j] == '*'
3899 || ccline.cmdbuff[i + j] == '?')
3900 break;
3901 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003902 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 }
3904 }
3905 }
3906
3907 if (p2 != NULL && !got_int)
3908 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003909 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003910 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003912 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 xp->xp_pattern = ccline.cmdbuff + i;
3914 }
3915 else
3916 v = OK;
3917 if (v == OK)
3918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003919 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3920 &ccline.cmdbuff[ccline.cmdpos],
3921 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3922 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 ccline.cmdlen += difflen;
3924 ccline.cmdpos += difflen;
3925 }
3926 }
3927 vim_free(p2);
3928
3929 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003930 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931
3932 /* When expanding a ":map" command and no matches are found, assume that
3933 * the key is supposed to be inserted literally */
3934 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3935 return FAIL;
3936
3937 if (xp->xp_numfiles <= 0 && p2 == NULL)
3938 beep_flush();
3939 else if (xp->xp_numfiles == 1)
3940 /* free expanded pattern */
3941 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3942
3943 return OK;
3944}
3945
3946/*
3947 * Do wildcard expansion on the string 'str'.
3948 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003949 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 * Return NULL for failure.
3951 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003952 * "orig" is the originally expanded string, copied to allocated memory. It
3953 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3954 * WILD_PREV "orig" should be NULL.
3955 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003956 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3957 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 *
3959 * mode = WILD_FREE: just free previously expanded matches
3960 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3961 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3962 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3963 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3964 * mode = WILD_ALL: return all matches concatenated
3965 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003966 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 *
3968 * options = WILD_LIST_NOTFOUND: list entries without a match
3969 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3970 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3971 * options = WILD_NO_BEEP: Don't beep for multiple matches
3972 * options = WILD_ADD_SLASH: add a slash after directory names
3973 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3974 * options = WILD_SILENT: don't print warning messages
3975 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003976 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 *
3978 * The variables xp->xp_context and xp->xp_backslash must have been set!
3979 */
3980 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003981ExpandOne(
3982 expand_T *xp,
3983 char_u *str,
3984 char_u *orig, /* allocated copy of original of expanded string */
3985 int options,
3986 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987{
3988 char_u *ss = NULL;
3989 static int findex;
3990 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003991 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 int i;
3993 long_u len;
3994 int non_suf_match; /* number without matching suffix */
3995
3996 /*
3997 * first handle the case of using an old match
3998 */
3999 if (mode == WILD_NEXT || mode == WILD_PREV)
4000 {
4001 if (xp->xp_numfiles > 0)
4002 {
4003 if (mode == WILD_PREV)
4004 {
4005 if (findex == -1)
4006 findex = xp->xp_numfiles;
4007 --findex;
4008 }
4009 else /* mode == WILD_NEXT */
4010 ++findex;
4011
4012 /*
4013 * When wrapping around, return the original string, set findex to
4014 * -1.
4015 */
4016 if (findex < 0)
4017 {
4018 if (orig_save == NULL)
4019 findex = xp->xp_numfiles - 1;
4020 else
4021 findex = -1;
4022 }
4023 if (findex >= xp->xp_numfiles)
4024 {
4025 if (orig_save == NULL)
4026 findex = 0;
4027 else
4028 findex = -1;
4029 }
4030#ifdef FEAT_WILDMENU
4031 if (p_wmnu)
4032 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4033 findex, cmd_showtail);
4034#endif
4035 if (findex == -1)
4036 return vim_strsave(orig_save);
4037 return vim_strsave(xp->xp_files[findex]);
4038 }
4039 else
4040 return NULL;
4041 }
4042
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004043 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4045 {
4046 FreeWild(xp->xp_numfiles, xp->xp_files);
4047 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004048 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 findex = 0;
4051
4052 if (mode == WILD_FREE) /* only release file name */
4053 return NULL;
4054
4055 if (xp->xp_numfiles == -1)
4056 {
4057 vim_free(orig_save);
4058 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004059 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
4061 /*
4062 * Do the expansion.
4063 */
4064 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4065 options) == FAIL)
4066 {
4067#ifdef FNAME_ILLEGAL
4068 /* Illegal file name has been silently skipped. But when there
4069 * are wildcards, the real problem is that there was no match,
4070 * causing the pattern to be added, which has illegal characters.
4071 */
4072 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004073 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074#endif
4075 }
4076 else if (xp->xp_numfiles == 0)
4077 {
4078 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004079 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 }
4081 else
4082 {
4083 /* Escape the matches for use on the command line. */
4084 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4085
4086 /*
4087 * Check for matching suffixes in file names.
4088 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004089 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4090 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
4092 if (xp->xp_numfiles)
4093 non_suf_match = xp->xp_numfiles;
4094 else
4095 non_suf_match = 1;
4096 if ((xp->xp_context == EXPAND_FILES
4097 || xp->xp_context == EXPAND_DIRECTORIES)
4098 && xp->xp_numfiles > 1)
4099 {
4100 /*
4101 * More than one match; check suffix.
4102 * The files will have been sorted on matching suffix in
4103 * expand_wildcards, only need to check the first two.
4104 */
4105 non_suf_match = 0;
4106 for (i = 0; i < 2; ++i)
4107 if (match_suffix(xp->xp_files[i]))
4108 ++non_suf_match;
4109 }
4110 if (non_suf_match != 1)
4111 {
4112 /* Can we ever get here unless it's while expanding
4113 * interactively? If not, we can get rid of this all
4114 * together. Don't really want to wait for this message
4115 * (and possibly have to hit return to continue!).
4116 */
4117 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004118 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 else if (!(options & WILD_NO_BEEP))
4120 beep_flush();
4121 }
4122 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4123 ss = vim_strsave(xp->xp_files[0]);
4124 }
4125 }
4126 }
4127
4128 /* Find longest common part */
4129 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4130 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004131 int mb_len = 1;
4132 int c0, ci;
4133
4134 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004136 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004138 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4139 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4140 }
4141 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004142 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004143 for (i = 1; i < xp->xp_numfiles; ++i)
4144 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004145 if (has_mbyte)
4146 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4147 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004148 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004149 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004151 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004152 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004154 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 break;
4156 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004157 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159 }
4160 if (i < xp->xp_numfiles)
4161 {
4162 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004163 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 break;
4165 }
4166 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004167
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 ss = alloc((unsigned)len + 1);
4169 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004170 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 findex = -1; /* next p_wc gets first one */
4172 }
4173
4174 /* Concatenate all matching names */
4175 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4176 {
4177 len = 0;
4178 for (i = 0; i < xp->xp_numfiles; ++i)
4179 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4180 ss = lalloc(len, TRUE);
4181 if (ss != NULL)
4182 {
4183 *ss = NUL;
4184 for (i = 0; i < xp->xp_numfiles; ++i)
4185 {
4186 STRCAT(ss, xp->xp_files[i]);
4187 if (i != xp->xp_numfiles - 1)
4188 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4189 }
4190 }
4191 }
4192
4193 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4194 ExpandCleanup(xp);
4195
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004196 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004197 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004198 vim_free(orig);
4199
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 return ss;
4201}
4202
4203/*
4204 * Prepare an expand structure for use.
4205 */
4206 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004207ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004209 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004210 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004212#ifndef BACKSLASH_IN_FILENAME
4213 xp->xp_shell = FALSE;
4214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 xp->xp_numfiles = -1;
4216 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004217#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4218 xp->xp_arg = NULL;
4219#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004220 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221}
4222
4223/*
4224 * Cleanup an expand structure after use.
4225 */
4226 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004227ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228{
4229 if (xp->xp_numfiles >= 0)
4230 {
4231 FreeWild(xp->xp_numfiles, xp->xp_files);
4232 xp->xp_numfiles = -1;
4233 }
4234}
4235
4236 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004237ExpandEscape(
4238 expand_T *xp,
4239 char_u *str,
4240 int numfiles,
4241 char_u **files,
4242 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243{
4244 int i;
4245 char_u *p;
4246
4247 /*
4248 * May change home directory back to "~"
4249 */
4250 if (options & WILD_HOME_REPLACE)
4251 tilde_replace(str, numfiles, files);
4252
4253 if (options & WILD_ESCAPE)
4254 {
4255 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004256 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004257 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 || xp->xp_context == EXPAND_BUFFERS
4259 || xp->xp_context == EXPAND_DIRECTORIES)
4260 {
4261 /*
4262 * Insert a backslash into a file name before a space, \, %, #
4263 * and wildmatch characters, except '~'.
4264 */
4265 for (i = 0; i < numfiles; ++i)
4266 {
4267 /* for ":set path=" we need to escape spaces twice */
4268 if (xp->xp_backslash == XP_BS_THREE)
4269 {
4270 p = vim_strsave_escaped(files[i], (char_u *)" ");
4271 if (p != NULL)
4272 {
4273 vim_free(files[i]);
4274 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004275#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 p = vim_strsave_escaped(files[i], (char_u *)" ");
4277 if (p != NULL)
4278 {
4279 vim_free(files[i]);
4280 files[i] = p;
4281 }
4282#endif
4283 }
4284 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004285#ifdef BACKSLASH_IN_FILENAME
4286 p = vim_strsave_fnameescape(files[i], FALSE);
4287#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004288 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (p != NULL)
4291 {
4292 vim_free(files[i]);
4293 files[i] = p;
4294 }
4295
4296 /* If 'str' starts with "\~", replace "~" at start of
4297 * files[i] with "\~". */
4298 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004299 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004302
4303 /* If the first file starts with a '+' escape it. Otherwise it
4304 * could be seen as "+cmd". */
4305 if (*files[0] == '+')
4306 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 }
4308 else if (xp->xp_context == EXPAND_TAGS)
4309 {
4310 /*
4311 * Insert a backslash before characters in a tag name that
4312 * would terminate the ":tag" command.
4313 */
4314 for (i = 0; i < numfiles; ++i)
4315 {
4316 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4317 if (p != NULL)
4318 {
4319 vim_free(files[i]);
4320 files[i] = p;
4321 }
4322 }
4323 }
4324 }
4325}
4326
4327/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004328 * Escape special characters in "fname" for when used as a file name argument
4329 * after a Vim command, or, when "shell" is non-zero, a shell command.
4330 * Returns the result in allocated memory.
4331 */
4332 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004333vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004334{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004335 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004336#ifdef BACKSLASH_IN_FILENAME
4337 char_u buf[20];
4338 int j = 0;
4339
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004340 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004341 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004342 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004343 buf[j++] = *p;
4344 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004345 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004346#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004347 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4348 if (shell && csh_like_shell() && p != NULL)
4349 {
4350 char_u *s;
4351
4352 /* For csh and similar shells need to put two backslashes before '!'.
4353 * One is taken by Vim, one by the shell. */
4354 s = vim_strsave_escaped(p, (char_u *)"!");
4355 vim_free(p);
4356 p = s;
4357 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004358#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004359
4360 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4361 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004362 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004363 escape_fname(&p);
4364
4365 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004366}
4367
4368/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004369 * Put a backslash before the file name in "pp", which is in allocated memory.
4370 */
4371 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004372escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004373{
4374 char_u *p;
4375
4376 p = alloc((unsigned)(STRLEN(*pp) + 2));
4377 if (p != NULL)
4378 {
4379 p[0] = '\\';
4380 STRCPY(p + 1, *pp);
4381 vim_free(*pp);
4382 *pp = p;
4383 }
4384}
4385
4386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 * For each file name in files[num_files]:
4388 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4389 */
4390 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004391tilde_replace(
4392 char_u *orig_pat,
4393 int num_files,
4394 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396 int i;
4397 char_u *p;
4398
4399 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4400 {
4401 for (i = 0; i < num_files; ++i)
4402 {
4403 p = home_replace_save(NULL, files[i]);
4404 if (p != NULL)
4405 {
4406 vim_free(files[i]);
4407 files[i] = p;
4408 }
4409 }
4410 }
4411}
4412
4413/*
4414 * Show all matches for completion on the command line.
4415 * Returns EXPAND_NOTHING when the character that triggered expansion should
4416 * be inserted like a normal character.
4417 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004419showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420{
4421#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4422 int num_files;
4423 char_u **files_found;
4424 int i, j, k;
4425 int maxlen;
4426 int lines;
4427 int columns;
4428 char_u *p;
4429 int lastlen;
4430 int attr;
4431 int showtail;
4432
4433 if (xp->xp_numfiles == -1)
4434 {
4435 set_expand_context(xp);
4436 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4437 &num_files, &files_found);
4438 showtail = expand_showtail(xp);
4439 if (i != EXPAND_OK)
4440 return i;
4441
4442 }
4443 else
4444 {
4445 num_files = xp->xp_numfiles;
4446 files_found = xp->xp_files;
4447 showtail = cmd_showtail;
4448 }
4449
4450#ifdef FEAT_WILDMENU
4451 if (!wildmenu)
4452 {
4453#endif
4454 msg_didany = FALSE; /* lines_left will be set */
4455 msg_start(); /* prepare for paging */
4456 msg_putchar('\n');
4457 out_flush();
4458 cmdline_row = msg_row;
4459 msg_didany = FALSE; /* lines_left will be set again */
4460 msg_start(); /* prepare for paging */
4461#ifdef FEAT_WILDMENU
4462 }
4463#endif
4464
4465 if (got_int)
4466 got_int = FALSE; /* only int. the completion, not the cmd line */
4467#ifdef FEAT_WILDMENU
4468 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004469 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#endif
4471 else
4472 {
4473 /* find the length of the longest file name */
4474 maxlen = 0;
4475 for (i = 0; i < num_files; ++i)
4476 {
4477 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004478 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 || xp->xp_context == EXPAND_BUFFERS))
4480 {
4481 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4482 j = vim_strsize(NameBuff);
4483 }
4484 else
4485 j = vim_strsize(L_SHOWFILE(i));
4486 if (j > maxlen)
4487 maxlen = j;
4488 }
4489
4490 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4491 lines = num_files;
4492 else
4493 {
4494 /* compute the number of columns and lines for the listing */
4495 maxlen += 2; /* two spaces between file names */
4496 columns = ((int)Columns + 2) / maxlen;
4497 if (columns < 1)
4498 columns = 1;
4499 lines = (num_files + columns - 1) / columns;
4500 }
4501
Bram Moolenaar8820b482017-03-16 17:23:31 +01004502 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4505 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004506 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 msg_clr_eos();
4508 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004509 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 }
4511
4512 /* list the files line by line */
4513 for (i = 0; i < lines; ++i)
4514 {
4515 lastlen = 999;
4516 for (k = i; k < num_files; k += lines)
4517 {
4518 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4519 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004520 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 p = files_found[k] + STRLEN(files_found[k]) + 1;
4522 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004523 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004525 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 break;
4527 }
4528 for (j = maxlen - lastlen; --j >= 0; )
4529 msg_putchar(' ');
4530 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004531 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 || xp->xp_context == EXPAND_BUFFERS)
4533 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004534 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004535 if (xp->xp_numfiles != -1)
4536 {
4537 char_u *halved_slash;
4538 char_u *exp_path;
4539
4540 /* Expansion was done before and special characters
4541 * were escaped, need to halve backslashes. Also
4542 * $HOME has been replaced with ~/. */
4543 exp_path = expand_env_save_opt(files_found[k], TRUE);
4544 halved_slash = backslash_halve_save(
4545 exp_path != NULL ? exp_path : files_found[k]);
4546 j = mch_isdir(halved_slash != NULL ? halved_slash
4547 : files_found[k]);
4548 vim_free(exp_path);
4549 vim_free(halved_slash);
4550 }
4551 else
4552 /* Expansion was done here, file names are literal. */
4553 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (showtail)
4555 p = L_SHOWFILE(k);
4556 else
4557 {
4558 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4559 TRUE);
4560 p = NameBuff;
4561 }
4562 }
4563 else
4564 {
4565 j = FALSE;
4566 p = L_SHOWFILE(k);
4567 }
4568 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4569 }
4570 if (msg_col > 0) /* when not wrapped around */
4571 {
4572 msg_clr_eos();
4573 msg_putchar('\n');
4574 }
4575 out_flush(); /* show one line at a time */
4576 if (got_int)
4577 {
4578 got_int = FALSE;
4579 break;
4580 }
4581 }
4582
4583 /*
4584 * we redraw the command below the lines that we have just listed
4585 * This is a bit tricky, but it saves a lot of screen updating.
4586 */
4587 cmdline_row = msg_row; /* will put it back later */
4588 }
4589
4590 if (xp->xp_numfiles == -1)
4591 FreeWild(num_files, files_found);
4592
4593 return EXPAND_OK;
4594}
4595
4596/*
4597 * Private gettail for showmatches() (and win_redr_status_matches()):
4598 * Find tail of file name path, but ignore trailing "/".
4599 */
4600 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004601sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 char_u *p;
4604 char_u *t = s;
4605 int had_sep = FALSE;
4606
4607 for (p = s; *p != NUL; )
4608 {
4609 if (vim_ispathsep(*p)
4610#ifdef BACKSLASH_IN_FILENAME
4611 && !rem_backslash(p)
4612#endif
4613 )
4614 had_sep = TRUE;
4615 else if (had_sep)
4616 {
4617 t = p;
4618 had_sep = FALSE;
4619 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004620 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 }
4622 return t;
4623}
4624
4625/*
4626 * Return TRUE if we only need to show the tail of completion matches.
4627 * When not completing file names or there is a wildcard in the path FALSE is
4628 * returned.
4629 */
4630 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004631expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632{
4633 char_u *s;
4634 char_u *end;
4635
4636 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004637 if (xp->xp_context != EXPAND_FILES
4638 && xp->xp_context != EXPAND_SHELLCMD
4639 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 return FALSE;
4641
4642 end = gettail(xp->xp_pattern);
4643 if (end == xp->xp_pattern) /* there is no path separator */
4644 return FALSE;
4645
4646 for (s = xp->xp_pattern; s < end; s++)
4647 {
4648 /* Skip escaped wildcards. Only when the backslash is not a path
4649 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4650 if (rem_backslash(s))
4651 ++s;
4652 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4653 return FALSE;
4654 }
4655 return TRUE;
4656}
4657
4658/*
4659 * Prepare a string for expansion.
4660 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004661 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 * When expanding other names: The string will be used with regcomp(). Copy
4663 * the name into allocated memory and prepend "^".
4664 */
4665 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004666addstar(
4667 char_u *fname,
4668 int len,
4669 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670{
4671 char_u *retval;
4672 int i, j;
4673 int new_len;
4674 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004675 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004677 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004678 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004679 && context != EXPAND_SHELLCMD
4680 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 {
4682 /*
4683 * Matching will be done internally (on something other than files).
4684 * So we convert the file-matching-type wildcards into our kind for
4685 * use with vim_regcomp(). First work out how long it will be:
4686 */
4687
4688 /* For help tags the translation is done in find_help_tags().
4689 * For a tag pattern starting with "/" no translation is needed. */
4690 if (context == EXPAND_HELP
4691 || context == EXPAND_COLORS
4692 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004693 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004694 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004695 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004696 || ((context == EXPAND_TAGS_LISTFILES
4697 || context == EXPAND_TAGS)
4698 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 retval = vim_strnsave(fname, len);
4700 else
4701 {
4702 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4703 for (i = 0; i < len; i++)
4704 {
4705 if (fname[i] == '*' || fname[i] == '~')
4706 new_len++; /* '*' needs to be replaced by ".*"
4707 '~' needs to be replaced by "\~" */
4708
4709 /* Buffer names are like file names. "." should be literal */
4710 if (context == EXPAND_BUFFERS && fname[i] == '.')
4711 new_len++; /* "." becomes "\." */
4712
4713 /* Custom expansion takes care of special things, match
4714 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004715 if ((context == EXPAND_USER_DEFINED
4716 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 new_len++; /* '\' becomes "\\" */
4718 }
4719 retval = alloc(new_len);
4720 if (retval != NULL)
4721 {
4722 retval[0] = '^';
4723 j = 1;
4724 for (i = 0; i < len; i++, j++)
4725 {
4726 /* Skip backslash. But why? At least keep it for custom
4727 * expansion. */
4728 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004729 && context != EXPAND_USER_LIST
4730 && fname[i] == '\\'
4731 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 break;
4733
4734 switch (fname[i])
4735 {
4736 case '*': retval[j++] = '.';
4737 break;
4738 case '~': retval[j++] = '\\';
4739 break;
4740 case '?': retval[j] = '.';
4741 continue;
4742 case '.': if (context == EXPAND_BUFFERS)
4743 retval[j++] = '\\';
4744 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004745 case '\\': if (context == EXPAND_USER_DEFINED
4746 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 retval[j++] = '\\';
4748 break;
4749 }
4750 retval[j] = fname[i];
4751 }
4752 retval[j] = NUL;
4753 }
4754 }
4755 }
4756 else
4757 {
4758 retval = alloc(len + 4);
4759 if (retval != NULL)
4760 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004761 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
4763 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004764 * Don't add a star to *, ~, ~user, $var or `cmd`.
4765 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 * ~ would be at the start of the file name, but not the tail.
4767 * $ could be anywhere in the tail.
4768 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004769 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 */
4771 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004772 ends_in_star = (len > 0 && retval[len - 1] == '*');
4773#ifndef BACKSLASH_IN_FILENAME
4774 for (i = len - 2; i >= 0; --i)
4775 {
4776 if (retval[i] != '\\')
4777 break;
4778 ends_in_star = !ends_in_star;
4779 }
4780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004782 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 && vim_strchr(tail, '$') == NULL
4784 && vim_strchr(retval, '`') == NULL)
4785 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004786 else if (len > 0 && retval[len - 1] == '$')
4787 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 retval[len] = NUL;
4789 }
4790 }
4791 return retval;
4792}
4793
4794/*
4795 * Must parse the command line so far to work out what context we are in.
4796 * Completion can then be done based on that context.
4797 * This routine sets the variables:
4798 * xp->xp_pattern The start of the pattern to be expanded within
4799 * the command line (ends at the cursor).
4800 * xp->xp_context The type of thing to expand. Will be one of:
4801 *
4802 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4803 * the command line, like an unknown command. Caller
4804 * should beep.
4805 * EXPAND_NOTHING Unrecognised context for completion, use char like
4806 * a normal char, rather than for completion. eg
4807 * :s/^I/
4808 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4809 * it.
4810 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4811 * EXPAND_FILES After command with XFILE set, or after setting
4812 * with P_EXPAND set. eg :e ^I, :w>>^I
4813 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4814 * when we know only directories are of interest. eg
4815 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004816 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4818 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4819 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4820 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4821 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4822 * EXPAND_EVENTS Complete event names
4823 * EXPAND_SYNTAX Complete :syntax command arguments
4824 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4825 * EXPAND_AUGROUP Complete autocommand group names
4826 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4827 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4828 * eg :unmap a^I , :cunab x^I
4829 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4830 * eg :call sub^I
4831 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4832 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4833 * names in expressions, eg :while s^I
4834 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004835 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
4837 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004838set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004840 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (ccline.cmdfirstc != ':'
4842#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004843 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004844 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845#endif
4846 )
4847 {
4848 xp->xp_context = EXPAND_NOTHING;
4849 return;
4850 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004851 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852}
4853
4854 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004855set_cmd_context(
4856 expand_T *xp,
4857 char_u *str, /* start of command line */
4858 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004859 int col, /* position of cursor */
4860 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861{
4862 int old_char = NUL;
4863 char_u *nextcomm;
4864
4865 /*
4866 * Avoid a UMR warning from Purify, only save the character if it has been
4867 * written before.
4868 */
4869 if (col < len)
4870 old_char = str[col];
4871 str[col] = NUL;
4872 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004873
4874#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004875 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004876 {
4877# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004878 /* pass CMD_SIZE because there is no real command */
4879 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004880# endif
4881 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004882 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004883 {
4884 xp->xp_context = ccline.xp_context;
4885 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004886# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004887 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004888# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004889 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004890 else
4891#endif
4892 while (nextcomm != NULL)
4893 nextcomm = set_one_cmd_context(xp, nextcomm);
4894
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004895 /* Store the string here so that call_user_expand_func() can get to them
4896 * easily. */
4897 xp->xp_line = str;
4898 xp->xp_col = col;
4899
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 str[col] = old_char;
4901}
4902
4903/*
4904 * Expand the command line "str" from context "xp".
4905 * "xp" must have been set by set_cmd_context().
4906 * xp->xp_pattern points into "str", to where the text that is to be expanded
4907 * starts.
4908 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4909 * cursor.
4910 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4911 * key that triggered expansion literally.
4912 * Returns EXPAND_OK otherwise.
4913 */
4914 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004915expand_cmdline(
4916 expand_T *xp,
4917 char_u *str, /* start of command line */
4918 int col, /* position of cursor */
4919 int *matchcount, /* return: nr of matches */
4920 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921{
4922 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004923 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924
4925 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4926 {
4927 beep_flush();
4928 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4929 }
4930 if (xp->xp_context == EXPAND_NOTHING)
4931 {
4932 /* Caller can use the character as a normal char instead */
4933 return EXPAND_NOTHING;
4934 }
4935
4936 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004937 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4938 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 if (file_str == NULL)
4940 return EXPAND_UNSUCCESSFUL;
4941
Bram Moolenaar94950a92010-12-02 16:01:29 +01004942 if (p_wic)
4943 options += WILD_ICASE;
4944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004946 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 {
4948 *matchcount = 0;
4949 *matches = NULL;
4950 }
4951 vim_free(file_str);
4952
4953 return EXPAND_OK;
4954}
4955
4956#ifdef FEAT_MULTI_LANG
4957/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004958 * Cleanup matches for help tags:
4959 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4960 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004963cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964{
4965 int i, j;
4966 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004967 char_u buf[4];
4968 char_u *p = buf;
4969
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004970 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004971 {
4972 *p++ = '@';
4973 *p++ = p_hlg[0];
4974 *p++ = p_hlg[1];
4975 }
4976 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 for (i = 0; i < num_file; ++i)
4979 {
4980 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004981 if (len <= 0)
4982 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004983 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
4985 /* Sorting on priority means the same item in another language may
4986 * be anywhere. Search all items for a match up to the "@en". */
4987 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004988 if (j != i && (int)STRLEN(file[j]) == len + 3
4989 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 break;
4991 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004992 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 file[i][len] = NUL;
4994 }
4995 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004996
4997 if (*buf != NUL)
4998 for (i = 0; i < num_file; ++i)
4999 {
5000 len = (int)STRLEN(file[i]) - 3;
5001 if (len <= 0)
5002 continue;
5003 if (STRCMP(file[i] + len, buf) == 0)
5004 {
5005 /* remove the default language */
5006 file[i][len] = NUL;
5007 }
5008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009}
5010#endif
5011
5012/*
5013 * Do the expansion based on xp->xp_context and "pat".
5014 */
5015 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005016ExpandFromContext(
5017 expand_T *xp,
5018 char_u *pat,
5019 int *num_file,
5020 char_u ***file,
5021 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022{
5023#ifdef FEAT_CMDL_COMPL
5024 regmatch_T regmatch;
5025#endif
5026 int ret;
5027 int flags;
5028
5029 flags = EW_DIR; /* include directories */
5030 if (options & WILD_LIST_NOTFOUND)
5031 flags |= EW_NOTFOUND;
5032 if (options & WILD_ADD_SLASH)
5033 flags |= EW_ADDSLASH;
5034 if (options & WILD_KEEP_ALL)
5035 flags |= EW_KEEPALL;
5036 if (options & WILD_SILENT)
5037 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005038 if (options & WILD_ALLLINKS)
5039 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005041 if (xp->xp_context == EXPAND_FILES
5042 || xp->xp_context == EXPAND_DIRECTORIES
5043 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 /*
5046 * Expand file or directory names.
5047 */
5048 int free_pat = FALSE;
5049 int i;
5050
5051 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5052 * space */
5053 if (xp->xp_backslash != XP_BS_NONE)
5054 {
5055 free_pat = TRUE;
5056 pat = vim_strsave(pat);
5057 for (i = 0; pat[i]; ++i)
5058 if (pat[i] == '\\')
5059 {
5060 if (xp->xp_backslash == XP_BS_THREE
5061 && pat[i + 1] == '\\'
5062 && pat[i + 2] == '\\'
5063 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005064 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 if (xp->xp_backslash == XP_BS_ONE
5066 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005067 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 }
5070
5071 if (xp->xp_context == EXPAND_FILES)
5072 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005073 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5074 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 else
5076 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005077 if (options & WILD_ICASE)
5078 flags |= EW_ICASE;
5079
Bram Moolenaard7834d32009-12-02 16:14:36 +00005080 /* Expand wildcards, supporting %:h and the like. */
5081 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 if (free_pat)
5083 vim_free(pat);
5084 return ret;
5085 }
5086
5087 *file = (char_u **)"";
5088 *num_file = 0;
5089 if (xp->xp_context == EXPAND_HELP)
5090 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005091 /* With an empty argument we would get all the help tags, which is
5092 * very slow. Get matches for "help" instead. */
5093 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5094 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
5096#ifdef FEAT_MULTI_LANG
5097 cleanup_help_tags(*num_file, *file);
5098#endif
5099 return OK;
5100 }
5101 return FAIL;
5102 }
5103
5104#ifndef FEAT_CMDL_COMPL
5105 return FAIL;
5106#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005107 if (xp->xp_context == EXPAND_SHELLCMD)
5108 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (xp->xp_context == EXPAND_OLD_SETTING)
5110 return ExpandOldSetting(num_file, file);
5111 if (xp->xp_context == EXPAND_BUFFERS)
5112 return ExpandBufnames(pat, num_file, file, options);
5113 if (xp->xp_context == EXPAND_TAGS
5114 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5115 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5116 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005117 {
5118 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005119 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5120 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005121 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005123 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005124 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005125 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005126 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005127 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005128 {
5129 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005130 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005131 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005132 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005133 {
5134 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005135 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005136 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005137# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5138 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005139 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005140# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005141 if (xp->xp_context == EXPAND_PACKADD)
5142 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5145 if (regmatch.regprog == NULL)
5146 return FAIL;
5147
5148 /* set ignore-case according to p_ic, p_scs and pat */
5149 regmatch.rm_ic = ignorecase(pat);
5150
5151 if (xp->xp_context == EXPAND_SETTINGS
5152 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5153 ret = ExpandSettings(xp, &regmatch, num_file, file);
5154 else if (xp->xp_context == EXPAND_MAPPINGS)
5155 ret = ExpandMappings(&regmatch, num_file, file);
5156# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5157 else if (xp->xp_context == EXPAND_USER_DEFINED)
5158 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5159# endif
5160 else
5161 {
5162 static struct expgen
5163 {
5164 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005165 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005167 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 } tab[] =
5169 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005170 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5171 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005172 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005173 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005174#ifdef FEAT_CMDHIST
5175 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005178 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005179 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005180 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5181 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5182 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
5184#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005185 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5186 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5187 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5188 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189#endif
5190#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005191 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5192 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#endif
5194#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005195 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005197#ifdef FEAT_PROFILE
5198 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5199#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005200 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005201 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5202 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005203#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005204 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005205#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005206#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005207 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005208#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005209#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005210 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005211#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005212#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5214 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005217 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005218 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 };
5220 int i;
5221
5222 /*
5223 * Find a context in the table and call the ExpandGeneric() with the
5224 * right function to do the expansion.
5225 */
5226 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005227 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (xp->xp_context == tab[i].context)
5229 {
5230 if (tab[i].ic)
5231 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005232 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005233 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 break;
5235 }
5236 }
5237
Bram Moolenaar473de612013-06-08 18:19:48 +02005238 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239
5240 return ret;
5241#endif /* FEAT_CMDL_COMPL */
5242}
5243
5244#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5245/*
5246 * Expand a list of names.
5247 *
5248 * Generic function for command line completion. It calls a function to
5249 * obtain strings, one by one. The strings are matched against a regexp
5250 * program. Matching strings are copied into an array, which is returned.
5251 *
5252 * Returns OK when no problems encountered, FAIL for error (out of memory).
5253 */
5254 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005255ExpandGeneric(
5256 expand_T *xp,
5257 regmatch_T *regmatch,
5258 int *num_file,
5259 char_u ***file,
5260 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005262 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263{
5264 int i;
5265 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005266 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 char_u *str;
5268
5269 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005270 * round == 0: count the number of matching names
5271 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005273 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
5275 for (i = 0; ; ++i)
5276 {
5277 str = (*func)(xp, i);
5278 if (str == NULL) /* end of list */
5279 break;
5280 if (*str == NUL) /* skip empty strings */
5281 continue;
5282
5283 if (vim_regexec(regmatch, str, (colnr_T)0))
5284 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005285 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005287 if (escaped)
5288 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5289 else
5290 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 (*file)[count] = str;
5292#ifdef FEAT_MENU
5293 if (func == get_menu_names && str != NULL)
5294 {
5295 /* test for separator added by get_menu_names() */
5296 str += STRLEN(str) - 1;
5297 if (*str == '\001')
5298 *str = '.';
5299 }
5300#endif
5301 }
5302 ++count;
5303 }
5304 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005305 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 {
5307 if (count == 0)
5308 return OK;
5309 *num_file = count;
5310 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5311 if (*file == NULL)
5312 {
5313 *file = (char_u **)"";
5314 return FAIL;
5315 }
5316 count = 0;
5317 }
5318 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005319
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005320 /* Sort the results. Keep menu's in the specified order. */
5321 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005322 {
5323 if (xp->xp_context == EXPAND_EXPRESSION
5324 || xp->xp_context == EXPAND_FUNCTIONS
5325 || xp->xp_context == EXPAND_USER_FUNC)
5326 /* <SNR> functions should be sorted to the end. */
5327 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5328 sort_func_compare);
5329 else
5330 sort_strings(*file, *num_file);
5331 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005332
Bram Moolenaar4f688582007-07-24 12:34:30 +00005333#ifdef FEAT_CMDL_COMPL
5334 /* Reset the variables used for special highlight names expansion, so that
5335 * they don't show up when getting normal highlight names by ID. */
5336 reset_expand_highlight();
5337#endif
5338
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 return OK;
5340}
5341
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005342/*
5343 * Complete a shell command.
5344 * Returns FAIL or OK;
5345 */
5346 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005347expand_shellcmd(
5348 char_u *filepat, /* pattern to match with command names */
5349 int *num_file, /* return: number of matches */
5350 char_u ***file, /* return: array with matches */
5351 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005352{
5353 char_u *pat;
5354 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005355 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005356 int mustfree = FALSE;
5357 garray_T ga;
5358 char_u *buf = alloc(MAXPATHL);
5359 size_t l;
5360 char_u *s, *e;
5361 int flags = flagsarg;
5362 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005363 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005364 hashtab_T found_ht;
5365 hashitem_T *hi;
5366 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005367
5368 if (buf == NULL)
5369 return FAIL;
5370
5371 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5372 * space */
5373 pat = vim_strsave(filepat);
5374 for (i = 0; pat[i]; ++i)
5375 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005376 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005377
Bram Moolenaarb5971142015-03-21 17:32:19 +01005378 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005379
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005380 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5381 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005382 path = (char_u *)".";
5383 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005384 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005385 /* For an absolute name we don't use $PATH. */
5386 if (!mch_isFullName(pat))
5387 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005388 if (path == NULL)
5389 path = (char_u *)"";
5390 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005391
5392 /*
5393 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005394 * collect them in "ga". When "." is not in $PATH also expand for the
5395 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005396 */
5397 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005398 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005399 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005400 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005401#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005402 e = vim_strchr(s, ';');
5403#else
5404 e = vim_strchr(s, ':');
5405#endif
5406 if (e == NULL)
5407 e = s + STRLEN(s);
5408
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005409 if (*s == NUL)
5410 {
5411 if (did_curdir)
5412 break;
5413 // Find directories in the current directory, path is empty.
5414 did_curdir = TRUE;
5415 flags |= EW_DIR;
5416 }
5417 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5418 {
5419 did_curdir = TRUE;
5420 flags |= EW_DIR;
5421 }
5422 else
5423 // Do not match directories inside a $PATH item.
5424 flags &= ~EW_DIR;
5425
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005426 l = e - s;
5427 if (l > MAXPATHL - 5)
5428 break;
5429 vim_strncpy(buf, s, l);
5430 add_pathsep(buf);
5431 l = STRLEN(buf);
5432 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5433
5434 /* Expand matches in one directory of $PATH. */
5435 ret = expand_wildcards(1, &buf, num_file, file, flags);
5436 if (ret == OK)
5437 {
5438 if (ga_grow(&ga, *num_file) == FAIL)
5439 FreeWild(*num_file, *file);
5440 else
5441 {
5442 for (i = 0; i < *num_file; ++i)
5443 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005444 char_u *name = (*file)[i];
5445
5446 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005447 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005448 // Check if this name was already found.
5449 hash = hash_hash(name + l);
5450 hi = hash_lookup(&found_ht, name + l, hash);
5451 if (HASHITEM_EMPTY(hi))
5452 {
5453 // Remove the path that was prepended.
5454 STRMOVE(name, name + l);
5455 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5456 hash_add_item(&found_ht, hi, name, hash);
5457 name = NULL;
5458 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005459 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005460 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005461 }
5462 vim_free(*file);
5463 }
5464 }
5465 if (*e != NUL)
5466 ++e;
5467 }
5468 *file = ga.ga_data;
5469 *num_file = ga.ga_len;
5470
5471 vim_free(buf);
5472 vim_free(pat);
5473 if (mustfree)
5474 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005475 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005476 return OK;
5477}
5478
5479
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5481/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005482 * Call "user_expand_func()" to invoke a user defined Vim script function and
5483 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005485 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005486call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005487 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005488 expand_T *xp,
5489 int *num_file,
5490 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005492 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005493 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005494 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005495 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005496 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497
Bram Moolenaarb7515462013-06-29 12:58:33 +02005498 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005499 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 *num_file = 0;
5501 *file = NULL;
5502
Bram Moolenaarb7515462013-06-29 12:58:33 +02005503 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005504 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005505 keep = ccline.cmdbuff[ccline.cmdlen];
5506 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005507 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005508
Bram Moolenaarffa96842018-06-12 22:05:14 +02005509 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5510
5511 args[0].v_type = VAR_STRING;
5512 args[0].vval.v_string = pat;
5513 args[1].v_type = VAR_STRING;
5514 args[1].vval.v_string = xp->xp_line;
5515 args[2].v_type = VAR_NUMBER;
5516 args[2].vval.v_number = xp->xp_col;
5517 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005519 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005520
Bram Moolenaarded27a12018-08-01 19:06:03 +02005521 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005522
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005523 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005524 if (ccline.cmdbuff != NULL)
5525 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005526
Bram Moolenaarffa96842018-06-12 22:05:14 +02005527 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005528 return ret;
5529}
5530
5531/*
5532 * Expand names with a function defined by the user.
5533 */
5534 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535ExpandUserDefined(
5536 expand_T *xp,
5537 regmatch_T *regmatch,
5538 int *num_file,
5539 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005540{
5541 char_u *retstr;
5542 char_u *s;
5543 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005544 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005545 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005546 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005547
5548 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5549 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return FAIL;
5551
5552 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005553 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
5555 e = vim_strchr(s, '\n');
5556 if (e == NULL)
5557 e = s + STRLEN(s);
5558 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005559 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005561 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5562 *e = keep;
5563
5564 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005566 if (ga_grow(&ga, 1) == FAIL)
5567 break;
5568 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5569 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 if (*e != NUL)
5573 ++e;
5574 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005575 vim_free(retstr);
5576 *file = ga.ga_data;
5577 *num_file = ga.ga_len;
5578 return OK;
5579}
5580
5581/*
5582 * Expand names with a list returned by a function defined by the user.
5583 */
5584 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005585ExpandUserList(
5586 expand_T *xp,
5587 int *num_file,
5588 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005589{
5590 list_T *retlist;
5591 listitem_T *li;
5592 garray_T ga;
5593
5594 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5595 if (retlist == NULL)
5596 return FAIL;
5597
5598 ga_init2(&ga, (int)sizeof(char *), 3);
5599 /* Loop over the items in the list. */
5600 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5601 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005602 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5603 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005604
5605 if (ga_grow(&ga, 1) == FAIL)
5606 break;
5607
5608 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005609 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005610 ++ga.ga_len;
5611 }
5612 list_unref(retlist);
5613
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 *file = ga.ga_data;
5615 *num_file = ga.ga_len;
5616 return OK;
5617}
5618#endif
5619
5620/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005621 * Expand color scheme, compiler or filetype names.
5622 * Search from 'runtimepath':
5623 * 'runtimepath'/{dirnames}/{pat}.vim
5624 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5625 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5626 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5627 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005628 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 */
5630 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005631ExpandRTDir(
5632 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005633 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634 int *num_file,
5635 char_u ***file,
5636 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 char_u *s;
5639 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005640 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005642 int i;
5643 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644
5645 *num_file = 0;
5646 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005647 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005648 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005650 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005652 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5653 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005655 ga_clear_strings(&ga);
5656 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005658 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005659 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005660 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005662
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005663 if (flags & DIP_START) {
5664 for (i = 0; dirnames[i] != NULL; ++i)
5665 {
5666 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5667 if (s == NULL)
5668 {
5669 ga_clear_strings(&ga);
5670 return FAIL;
5671 }
5672 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5673 globpath(p_pp, s, &ga, 0);
5674 vim_free(s);
5675 }
5676 }
5677
5678 if (flags & DIP_OPT) {
5679 for (i = 0; dirnames[i] != NULL; ++i)
5680 {
5681 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5682 if (s == NULL)
5683 {
5684 ga_clear_strings(&ga);
5685 return FAIL;
5686 }
5687 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5688 globpath(p_pp, s, &ga, 0);
5689 vim_free(s);
5690 }
5691 }
5692
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005693 for (i = 0; i < ga.ga_len; ++i)
5694 {
5695 match = ((char_u **)ga.ga_data)[i];
5696 s = match;
5697 e = s + STRLEN(s);
5698 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5699 {
5700 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005701 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005702 if (s < match || vim_ispathsep(*s))
5703 break;
5704 ++s;
5705 *e = NUL;
5706 mch_memmove(match, s, e - s + 1);
5707 }
5708 }
5709
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005710 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005711 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005712
5713 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005714 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005715 remove_duplicates(&ga);
5716
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 *file = ga.ga_data;
5718 *num_file = ga.ga_len;
5719 return OK;
5720}
5721
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005722/*
5723 * Expand loadplugin names:
5724 * 'packpath'/pack/ * /opt/{pat}
5725 */
5726 static int
5727ExpandPackAddDir(
5728 char_u *pat,
5729 int *num_file,
5730 char_u ***file)
5731{
5732 char_u *s;
5733 char_u *e;
5734 char_u *match;
5735 garray_T ga;
5736 int i;
5737 int pat_len;
5738
5739 *num_file = 0;
5740 *file = NULL;
5741 pat_len = (int)STRLEN(pat);
5742 ga_init2(&ga, (int)sizeof(char *), 10);
5743
5744 s = alloc((unsigned)(pat_len + 26));
5745 if (s == NULL)
5746 {
5747 ga_clear_strings(&ga);
5748 return FAIL;
5749 }
5750 sprintf((char *)s, "pack/*/opt/%s*", pat);
5751 globpath(p_pp, s, &ga, 0);
5752 vim_free(s);
5753
5754 for (i = 0; i < ga.ga_len; ++i)
5755 {
5756 match = ((char_u **)ga.ga_data)[i];
5757 s = gettail(match);
5758 e = s + STRLEN(s);
5759 mch_memmove(match, s, e - s + 1);
5760 }
5761
5762 if (ga.ga_len == 0)
5763 return FAIL;
5764
5765 /* Sort and remove duplicates which can happen when specifying multiple
5766 * directories in dirnames. */
5767 remove_duplicates(&ga);
5768
5769 *file = ga.ga_data;
5770 *num_file = ga.ga_len;
5771 return OK;
5772}
5773
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774#endif
5775
5776#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5777/*
5778 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005779 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005781 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005782globpath(
5783 char_u *path,
5784 char_u *file,
5785 garray_T *ga,
5786 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 expand_T xpc;
5789 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 int num_p;
5792 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 buf = alloc(MAXPATHL);
5795 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005796 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005798 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 /* Loop over all entries in {path}. */
5802 while (*path != NUL)
5803 {
5804 /* Copy one item of the path to buf[] and concatenate the file name. */
5805 copy_option_part(&path, buf, MAXPATHL, ",");
5806 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5807 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005808# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005809 /* Using the platform's path separator (\) makes vim incorrectly
5810 * treat it as an escape character, use '/' instead. */
5811 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5812 STRCAT(buf, "/");
5813# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005817 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5818 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005820 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005822 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 for (i = 0; i < num_p; ++i)
5825 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005826 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005827 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005828 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005831
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 FreeWild(num_p, p);
5833 }
5834 }
5835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836
5837 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838}
5839
5840#endif
5841
5842#if defined(FEAT_CMDHIST) || defined(PROTO)
5843
5844/*********************************
5845 * Command line history stuff *
5846 *********************************/
5847
5848/*
5849 * Translate a history character to the associated type number.
5850 */
5851 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005852hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
5854 if (c == ':')
5855 return HIST_CMD;
5856 if (c == '=')
5857 return HIST_EXPR;
5858 if (c == '@')
5859 return HIST_INPUT;
5860 if (c == '>')
5861 return HIST_DEBUG;
5862 return HIST_SEARCH; /* must be '?' or '/' */
5863}
5864
5865/*
5866 * Table of history names.
5867 * These names are used in :history and various hist...() functions.
5868 * It is sufficient to give the significant prefix of a history name.
5869 */
5870
5871static char *(history_names[]) =
5872{
5873 "cmd",
5874 "search",
5875 "expr",
5876 "input",
5877 "debug",
5878 NULL
5879};
5880
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005881#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5882/*
5883 * Function given to ExpandGeneric() to obtain the possible first
5884 * arguments of the ":history command.
5885 */
5886 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005887get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005888{
5889 static char_u compl[2] = { NUL, NUL };
5890 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005891 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005892 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5893
5894 if (idx < short_names_count)
5895 {
5896 compl[0] = (char_u)short_names[idx];
5897 return compl;
5898 }
5899 if (idx < short_names_count + history_name_count)
5900 return (char_u *)history_names[idx - short_names_count];
5901 if (idx == short_names_count + history_name_count)
5902 return (char_u *)"all";
5903 return NULL;
5904}
5905#endif
5906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907/*
5908 * init_history() - Initialize the command line history.
5909 * Also used to re-allocate the history when the size changes.
5910 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005911 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 int newlen; /* new length of history table */
5915 histentry_T *temp;
5916 int i;
5917 int j;
5918 int type;
5919
5920 /*
5921 * If size of history table changed, reallocate it
5922 */
5923 newlen = (int)p_hi;
5924 if (newlen != hislen) /* history length changed */
5925 {
5926 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5927 {
5928 if (newlen)
5929 {
5930 temp = (histentry_T *)lalloc(
5931 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5932 if (temp == NULL) /* out of memory! */
5933 {
5934 if (type == 0) /* first one: just keep the old length */
5935 {
5936 newlen = hislen;
5937 break;
5938 }
5939 /* Already changed one table, now we can only have zero
5940 * length for all tables. */
5941 newlen = 0;
5942 type = -1;
5943 continue;
5944 }
5945 }
5946 else
5947 temp = NULL;
5948 if (newlen == 0 || temp != NULL)
5949 {
5950 if (hisidx[type] < 0) /* there are no entries yet */
5951 {
5952 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005953 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 }
5955 else if (newlen > hislen) /* array becomes bigger */
5956 {
5957 for (i = 0; i <= hisidx[type]; ++i)
5958 temp[i] = history[type][i];
5959 j = i;
5960 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005961 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 for ( ; j < hislen; ++i, ++j)
5963 temp[i] = history[type][j];
5964 }
5965 else /* array becomes smaller or 0 */
5966 {
5967 j = hisidx[type];
5968 for (i = newlen - 1; ; --i)
5969 {
5970 if (i >= 0) /* copy newest entries */
5971 temp[i] = history[type][j];
5972 else /* remove older entries */
5973 vim_free(history[type][j].hisstr);
5974 if (--j < 0)
5975 j = hislen - 1;
5976 if (j == hisidx[type])
5977 break;
5978 }
5979 hisidx[type] = newlen - 1;
5980 }
5981 vim_free(history[type]);
5982 history[type] = temp;
5983 }
5984 }
5985 hislen = newlen;
5986 }
5987}
5988
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005990clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005991{
5992 hisptr->hisnum = 0;
5993 hisptr->viminfo = FALSE;
5994 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005995 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005996}
5997
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998/*
5999 * Check if command line 'str' is already in history.
6000 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6001 */
6002 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006003in_history(
6004 int type,
6005 char_u *str,
6006 int move_to_front, /* Move the entry to the front if it exists */
6007 int sep,
6008 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009{
6010 int i;
6011 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006012 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013
6014 if (hisidx[type] < 0)
6015 return FALSE;
6016 i = hisidx[type];
6017 do
6018 {
6019 if (history[type][i].hisstr == NULL)
6020 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006021
6022 /* For search history, check that the separator character matches as
6023 * well. */
6024 p = history[type][i].hisstr;
6025 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006026 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006027 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 {
6029 if (!move_to_front)
6030 return TRUE;
6031 last_i = i;
6032 break;
6033 }
6034 if (--i < 0)
6035 i = hislen - 1;
6036 } while (i != hisidx[type]);
6037
6038 if (last_i >= 0)
6039 {
6040 str = history[type][i].hisstr;
6041 while (i != hisidx[type])
6042 {
6043 if (++i >= hislen)
6044 i = 0;
6045 history[type][last_i] = history[type][i];
6046 last_i = i;
6047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006049 history[type][i].viminfo = FALSE;
6050 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006051 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 return TRUE;
6053 }
6054 return FALSE;
6055}
6056
6057/*
6058 * Convert history name (from table above) to its HIST_ equivalent.
6059 * When "name" is empty, return "cmd" history.
6060 * Returns -1 for unknown history name.
6061 */
6062 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006063get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006064{
6065 int i;
6066 int len = (int)STRLEN(name);
6067
6068 /* No argument: use current history. */
6069 if (len == 0)
6070 return hist_char2type(ccline.cmdfirstc);
6071
6072 for (i = 0; history_names[i] != NULL; ++i)
6073 if (STRNICMP(name, history_names[i], len) == 0)
6074 return i;
6075
6076 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6077 return hist_char2type(name[0]);
6078
6079 return -1;
6080}
6081
6082static int last_maptick = -1; /* last seen maptick */
6083
6084/*
6085 * Add the given string to the given history. If the string is already in the
6086 * history then it is moved to the front. "histype" may be one of he HIST_
6087 * values.
6088 */
6089 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006090add_to_history(
6091 int histype,
6092 char_u *new_entry,
6093 int in_map, /* consider maptick when inside a mapping */
6094 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095{
6096 histentry_T *hisptr;
6097 int len;
6098
6099 if (hislen == 0) /* no history */
6100 return;
6101
Bram Moolenaara939e432013-11-09 05:30:26 +01006102 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6103 return;
6104
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 /*
6106 * Searches inside the same mapping overwrite each other, so that only
6107 * the last line is kept. Be careful not to remove a line that was moved
6108 * down, only lines that were added.
6109 */
6110 if (histype == HIST_SEARCH && in_map)
6111 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006112 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 {
6114 /* Current line is from the same mapping, remove it */
6115 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6116 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006117 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 --hisnum[histype];
6119 if (--hisidx[HIST_SEARCH] < 0)
6120 hisidx[HIST_SEARCH] = hislen - 1;
6121 }
6122 last_maptick = -1;
6123 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006124 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 {
6126 if (++hisidx[histype] == hislen)
6127 hisidx[histype] = 0;
6128 hisptr = &history[histype][hisidx[histype]];
6129 vim_free(hisptr->hisstr);
6130
6131 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006132 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6134 if (hisptr->hisstr != NULL)
6135 hisptr->hisstr[len + 1] = sep;
6136
6137 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006138 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006139 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (histype == HIST_SEARCH && in_map)
6141 last_maptick = maptick;
6142 }
6143}
6144
6145#if defined(FEAT_EVAL) || defined(PROTO)
6146
6147/*
6148 * Get identifier of newest history entry.
6149 * "histype" may be one of the HIST_ values.
6150 */
6151 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153{
6154 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6155 || hisidx[histype] < 0)
6156 return -1;
6157
6158 return history[histype][hisidx[histype]].hisnum;
6159}
6160
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006161/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 * Calculate history index from a number:
6163 * num > 0: seen as identifying number of a history entry
6164 * num < 0: relative position in history wrt newest entry
6165 * "histype" may be one of the HIST_ values.
6166 */
6167 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006168calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169{
6170 int i;
6171 histentry_T *hist;
6172 int wrapped = FALSE;
6173
6174 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6175 || (i = hisidx[histype]) < 0 || num == 0)
6176 return -1;
6177
6178 hist = history[histype];
6179 if (num > 0)
6180 {
6181 while (hist[i].hisnum > num)
6182 if (--i < 0)
6183 {
6184 if (wrapped)
6185 break;
6186 i += hislen;
6187 wrapped = TRUE;
6188 }
6189 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6190 return i;
6191 }
6192 else if (-num <= hislen)
6193 {
6194 i += num + 1;
6195 if (i < 0)
6196 i += hislen;
6197 if (hist[i].hisstr != NULL)
6198 return i;
6199 }
6200 return -1;
6201}
6202
6203/*
6204 * Get a history entry by its index.
6205 * "histype" may be one of the HIST_ values.
6206 */
6207 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006208get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 idx = calc_hist_idx(histype, idx);
6211 if (idx >= 0)
6212 return history[histype][idx].hisstr;
6213 else
6214 return (char_u *)"";
6215}
6216
6217/*
6218 * Clear all entries of a history.
6219 * "histype" may be one of the HIST_ values.
6220 */
6221 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006222clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223{
6224 int i;
6225 histentry_T *hisptr;
6226
6227 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6228 {
6229 hisptr = history[histype];
6230 for (i = hislen; i--;)
6231 {
6232 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006233 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006234 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236 hisidx[histype] = -1; /* mark history as cleared */
6237 hisnum[histype] = 0; /* reset identifier counter */
6238 return OK;
6239 }
6240 return FAIL;
6241}
6242
6243/*
6244 * Remove all entries matching {str} from a history.
6245 * "histype" may be one of the HIST_ values.
6246 */
6247 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006248del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249{
6250 regmatch_T regmatch;
6251 histentry_T *hisptr;
6252 int idx;
6253 int i;
6254 int last;
6255 int found = FALSE;
6256
6257 regmatch.regprog = NULL;
6258 regmatch.rm_ic = FALSE; /* always match case */
6259 if (hislen != 0
6260 && histype >= 0
6261 && histype < HIST_COUNT
6262 && *str != NUL
6263 && (idx = hisidx[histype]) >= 0
6264 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6265 != NULL)
6266 {
6267 i = last = idx;
6268 do
6269 {
6270 hisptr = &history[histype][i];
6271 if (hisptr->hisstr == NULL)
6272 break;
6273 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6274 {
6275 found = TRUE;
6276 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006277 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 }
6279 else
6280 {
6281 if (i != last)
6282 {
6283 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006284 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 }
6286 if (--last < 0)
6287 last += hislen;
6288 }
6289 if (--i < 0)
6290 i += hislen;
6291 } while (i != idx);
6292 if (history[histype][idx].hisstr == NULL)
6293 hisidx[histype] = -1;
6294 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006295 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296 return found;
6297}
6298
6299/*
6300 * Remove an indexed entry from a history.
6301 * "histype" may be one of the HIST_ values.
6302 */
6303 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006304del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305{
6306 int i, j;
6307
6308 i = calc_hist_idx(histype, idx);
6309 if (i < 0)
6310 return FALSE;
6311 idx = hisidx[histype];
6312 vim_free(history[histype][i].hisstr);
6313
6314 /* When deleting the last added search string in a mapping, reset
6315 * last_maptick, so that the last added search string isn't deleted again.
6316 */
6317 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6318 last_maptick = -1;
6319
6320 while (i != idx)
6321 {
6322 j = (i + 1) % hislen;
6323 history[histype][i] = history[histype][j];
6324 i = j;
6325 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006326 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 if (--i < 0)
6328 i += hislen;
6329 hisidx[histype] = i;
6330 return TRUE;
6331}
6332
6333#endif /* FEAT_EVAL */
6334
6335#if defined(FEAT_CRYPT) || defined(PROTO)
6336/*
6337 * Very specific function to remove the value in ":set key=val" from the
6338 * history.
6339 */
6340 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006341remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006342{
6343 char_u *p;
6344 int i;
6345
6346 i = hisidx[HIST_CMD];
6347 if (i < 0)
6348 return;
6349 p = history[HIST_CMD][i].hisstr;
6350 if (p != NULL)
6351 for ( ; *p; ++p)
6352 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6353 {
6354 p = vim_strchr(p + 3, '=');
6355 if (p == NULL)
6356 break;
6357 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006358 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359 if (p[i] == '\\' && p[i + 1])
6360 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006361 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 --p;
6363 }
6364}
6365#endif
6366
6367#endif /* FEAT_CMDHIST */
6368
Bram Moolenaar064154c2016-03-19 22:50:43 +01006369#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006370/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006371 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006372 * ccline and put the previous value in prev_ccline.
6373 */
6374 static struct cmdline_info *
6375get_ccline_ptr(void)
6376{
6377 if ((State & CMDLINE) == 0)
6378 return NULL;
6379 if (ccline.cmdbuff != NULL)
6380 return &ccline;
6381 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6382 return &prev_ccline;
6383 return NULL;
6384}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006385#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006386
Bram Moolenaar064154c2016-03-19 22:50:43 +01006387#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006388/*
6389 * Get the current command line in allocated memory.
6390 * Only works when the command line is being edited.
6391 * Returns NULL when something is wrong.
6392 */
6393 char_u *
6394get_cmdline_str(void)
6395{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006396 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006397
Bram Moolenaaree91c332018-09-25 22:27:35 +02006398 if (cmdline_star > 0)
6399 return NULL;
6400 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006401 if (p == NULL)
6402 return NULL;
6403 return vim_strnsave(p->cmdbuff, p->cmdlen);
6404}
6405
6406/*
6407 * Get the current command line position, counted in bytes.
6408 * Zero is the first position.
6409 * Only works when the command line is being edited.
6410 * Returns -1 when something is wrong.
6411 */
6412 int
6413get_cmdline_pos(void)
6414{
6415 struct cmdline_info *p = get_ccline_ptr();
6416
6417 if (p == NULL)
6418 return -1;
6419 return p->cmdpos;
6420}
6421
6422/*
6423 * Set the command line byte position to "pos". Zero is the first position.
6424 * Only works when the command line is being edited.
6425 * Returns 1 when failed, 0 when OK.
6426 */
6427 int
6428set_cmdline_pos(
6429 int pos)
6430{
6431 struct cmdline_info *p = get_ccline_ptr();
6432
6433 if (p == NULL)
6434 return 1;
6435
6436 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6437 * changed the command line. */
6438 if (pos < 0)
6439 new_cmdpos = 0;
6440 else
6441 new_cmdpos = pos;
6442 return 0;
6443}
6444#endif
6445
6446#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6447/*
6448 * Get the current command-line type.
6449 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6450 * Only works when the command line is being edited.
6451 * Returns NUL when something is wrong.
6452 */
6453 int
6454get_cmdline_type(void)
6455{
6456 struct cmdline_info *p = get_ccline_ptr();
6457
6458 if (p == NULL)
6459 return NUL;
6460 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006461 return
6462# ifdef FEAT_EVAL
6463 (p->input_fn) ? '@' :
6464# endif
6465 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006466 return p->cmdfirstc;
6467}
6468#endif
6469
Bram Moolenaar071d4272004-06-13 20:20:40 +00006470#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6471/*
6472 * Get indices "num1,num2" that specify a range within a list (not a range of
6473 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6474 * Returns OK if parsed successfully, otherwise FAIL.
6475 */
6476 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006477get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478{
6479 int len;
6480 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006481 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483 *str = skipwhite(*str);
6484 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6485 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006486 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 *str += len;
6488 *num1 = (int)num;
6489 first = TRUE;
6490 }
6491 *str = skipwhite(*str);
6492 if (**str == ',') /* parse "to" part of range */
6493 {
6494 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006495 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (len > 0)
6497 {
6498 *num2 = (int)num;
6499 *str = skipwhite(*str + len);
6500 }
6501 else if (!first) /* no number given at all */
6502 return FAIL;
6503 }
6504 else if (first) /* only one number given */
6505 *num2 = *num1;
6506 return OK;
6507}
6508#endif
6509
6510#if defined(FEAT_CMDHIST) || defined(PROTO)
6511/*
6512 * :history command - print a history
6513 */
6514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006515ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 histentry_T *hist;
6518 int histype1 = HIST_CMD;
6519 int histype2 = HIST_CMD;
6520 int hisidx1 = 1;
6521 int hisidx2 = -1;
6522 int idx;
6523 int i, j, k;
6524 char_u *end;
6525 char_u *arg = eap->arg;
6526
6527 if (hislen == 0)
6528 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006529 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 return;
6531 }
6532
6533 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6534 {
6535 end = arg;
6536 while (ASCII_ISALPHA(*end)
6537 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6538 end++;
6539 i = *end;
6540 *end = NUL;
6541 histype1 = get_histtype(arg);
6542 if (histype1 == -1)
6543 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006544 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 {
6546 histype1 = 0;
6547 histype2 = HIST_COUNT-1;
6548 }
6549 else
6550 {
6551 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006552 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006553 return;
6554 }
6555 }
6556 else
6557 histype2 = histype1;
6558 *end = i;
6559 }
6560 else
6561 end = arg;
6562 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6563 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006564 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 return;
6566 }
6567
6568 for (; !got_int && histype1 <= histype2; ++histype1)
6569 {
6570 STRCPY(IObuff, "\n # ");
6571 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006572 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 idx = hisidx[histype1];
6574 hist = history[histype1];
6575 j = hisidx1;
6576 k = hisidx2;
6577 if (j < 0)
6578 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6579 if (k < 0)
6580 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6581 if (idx >= 0 && j <= k)
6582 for (i = idx + 1; !got_int; ++i)
6583 {
6584 if (i == hislen)
6585 i = 0;
6586 if (hist[i].hisstr != NULL
6587 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6588 {
6589 msg_putchar('\n');
6590 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6591 hist[i].hisnum);
6592 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6593 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006594 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 else
6596 STRCAT(IObuff, hist[i].hisstr);
6597 msg_outtrans(IObuff);
6598 out_flush();
6599 }
6600 if (i == idx)
6601 break;
6602 }
6603 }
6604}
6605#endif
6606
6607#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006608/*
6609 * Buffers for history read from a viminfo file. Only valid while reading.
6610 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006611static histentry_T *viminfo_history[HIST_COUNT] =
6612 {NULL, NULL, NULL, NULL, NULL};
6613static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6614static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615static int viminfo_add_at_front = FALSE;
6616
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617/*
6618 * Translate a history type number to the associated character.
6619 */
6620 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006621hist_type2char(
6622 int type,
6623 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624{
6625 if (type == HIST_CMD)
6626 return ':';
6627 if (type == HIST_SEARCH)
6628 {
6629 if (use_question)
6630 return '?';
6631 else
6632 return '/';
6633 }
6634 if (type == HIST_EXPR)
6635 return '=';
6636 return '@';
6637}
6638
6639/*
6640 * Prepare for reading the history from the viminfo file.
6641 * This allocates history arrays to store the read history lines.
6642 */
6643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006644prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645{
6646 int i;
6647 int num;
6648 int type;
6649 int len;
6650
6651 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006652 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 if (asklen > hislen)
6654 asklen = hislen;
6655
6656 for (type = 0; type < HIST_COUNT; ++type)
6657 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006658 /* Count the number of empty spaces in the history list. Entries read
6659 * from viminfo previously are also considered empty. If there are
6660 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006662 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 num++;
6664 len = asklen;
6665 if (num > len)
6666 len = num;
6667 if (len <= 0)
6668 viminfo_history[type] = NULL;
6669 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006670 viminfo_history[type] = (histentry_T *)lalloc(
6671 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (viminfo_history[type] == NULL)
6673 len = 0;
6674 viminfo_hislen[type] = len;
6675 viminfo_hisidx[type] = 0;
6676 }
6677}
6678
6679/*
6680 * Accept a line from the viminfo, store it in the history array when it's
6681 * new.
6682 */
6683 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006684read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 int type;
6687 long_u len;
6688 char_u *val;
6689 char_u *p;
6690
6691 type = hist_char2type(virp->vir_line[0]);
6692 if (viminfo_hisidx[type] < viminfo_hislen[type])
6693 {
6694 val = viminfo_readstring(virp, 1, TRUE);
6695 if (val != NULL && *val != NUL)
6696 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006697 int sep = (*val == ' ' ? NUL : *val);
6698
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006700 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
6702 /* Need to re-allocate to append the separator byte. */
6703 len = STRLEN(val);
6704 p = lalloc(len + 2, TRUE);
6705 if (p != NULL)
6706 {
6707 if (type == HIST_SEARCH)
6708 {
6709 /* Search entry: Move the separator from the first
6710 * column to after the NUL. */
6711 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006712 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714 else
6715 {
6716 /* Not a search entry: No separator in the viminfo
6717 * file, add a NUL separator. */
6718 mch_memmove(p, val, (size_t)len + 1);
6719 p[len + 1] = NUL;
6720 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006721 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6722 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006723 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6724 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006725 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 }
6727 }
6728 }
6729 vim_free(val);
6730 }
6731 return viminfo_readline(virp);
6732}
6733
Bram Moolenaar07219f92013-04-14 23:19:36 +02006734/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006735 * Accept a new style history line from the viminfo, store it in the history
6736 * array when it's new.
6737 */
6738 void
6739handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006740 garray_T *values,
6741 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006742{
6743 int type;
6744 long_u len;
6745 char_u *val;
6746 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006747 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006748
6749 /* Check the format:
6750 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006751 if (values->ga_len < 4
6752 || vp[0].bv_type != BVAL_NR
6753 || vp[1].bv_type != BVAL_NR
6754 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6755 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006756 return;
6757
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006758 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006759 if (type >= HIST_COUNT)
6760 return;
6761 if (viminfo_hisidx[type] < viminfo_hislen[type])
6762 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006763 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006764 if (val != NULL && *val != NUL)
6765 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006766 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6767 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006768 int idx;
6769 int overwrite = FALSE;
6770
6771 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6772 {
6773 /* If lines were written by an older Vim we need to avoid
6774 * getting duplicates. See if the entry already exists. */
6775 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6776 {
6777 p = viminfo_history[type][idx].hisstr;
6778 if (STRCMP(val, p) == 0
6779 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6780 {
6781 overwrite = TRUE;
6782 break;
6783 }
6784 }
6785
6786 if (!overwrite)
6787 {
6788 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006789 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006790 p = lalloc(len + 2, TRUE);
6791 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006792 else
6793 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006794 if (p != NULL)
6795 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006796 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006797 if (!overwrite)
6798 {
6799 mch_memmove(p, val, (size_t)len + 1);
6800 /* Put the separator after the NUL. */
6801 p[len + 1] = sep;
6802 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006803 viminfo_history[type][idx].hisnum = 0;
6804 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006805 viminfo_hisidx[type]++;
6806 }
6807 }
6808 }
6809 }
6810 }
6811}
6812
6813/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006814 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006815 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006816 static void
6817concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 int idx;
6820 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006821
6822 idx = hisidx[type] + viminfo_hisidx[type];
6823 if (idx >= hislen)
6824 idx -= hislen;
6825 else if (idx < 0)
6826 idx = hislen - 1;
6827 if (viminfo_add_at_front)
6828 hisidx[type] = idx;
6829 else
6830 {
6831 if (hisidx[type] == -1)
6832 hisidx[type] = hislen - 1;
6833 do
6834 {
6835 if (history[type][idx].hisstr != NULL
6836 || history[type][idx].viminfo)
6837 break;
6838 if (++idx == hislen)
6839 idx = 0;
6840 } while (idx != hisidx[type]);
6841 if (idx != hisidx[type] && --idx < 0)
6842 idx = hislen - 1;
6843 }
6844 for (i = 0; i < viminfo_hisidx[type]; i++)
6845 {
6846 vim_free(history[type][idx].hisstr);
6847 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6848 history[type][idx].viminfo = TRUE;
6849 history[type][idx].time_set = viminfo_history[type][i].time_set;
6850 if (--idx < 0)
6851 idx = hislen - 1;
6852 }
6853 idx += 1;
6854 idx %= hislen;
6855 for (i = 0; i < viminfo_hisidx[type]; i++)
6856 {
6857 history[type][idx++].hisnum = ++hisnum[type];
6858 idx %= hislen;
6859 }
6860}
6861
6862#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6863 static int
6864#ifdef __BORLANDC__
6865_RTLENTRYF
6866#endif
6867sort_hist(const void *s1, const void *s2)
6868{
6869 histentry_T *p1 = *(histentry_T **)s1;
6870 histentry_T *p2 = *(histentry_T **)s2;
6871
6872 if (p1->time_set < p2->time_set) return -1;
6873 if (p1->time_set > p2->time_set) return 1;
6874 return 0;
6875}
6876#endif
6877
6878/*
6879 * Merge history lines from viminfo and lines typed in this Vim based on the
6880 * timestamp;
6881 */
6882 static void
6883merge_history(int type)
6884{
6885 int max_len;
6886 histentry_T **tot_hist;
6887 histentry_T *new_hist;
6888 int i;
6889 int len;
6890
6891 /* Make one long list with all entries. */
6892 max_len = hislen + viminfo_hisidx[type];
6893 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6894 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6895 if (tot_hist == NULL || new_hist == NULL)
6896 {
6897 vim_free(tot_hist);
6898 vim_free(new_hist);
6899 return;
6900 }
6901 for (i = 0; i < viminfo_hisidx[type]; i++)
6902 tot_hist[i] = &viminfo_history[type][i];
6903 len = i;
6904 for (i = 0; i < hislen; i++)
6905 if (history[type][i].hisstr != NULL)
6906 tot_hist[len++] = &history[type][i];
6907
6908 /* Sort the list on timestamp. */
6909 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6910
6911 /* Keep the newest ones. */
6912 for (i = 0; i < hislen; i++)
6913 {
6914 if (i < len)
6915 {
6916 new_hist[i] = *tot_hist[i];
6917 tot_hist[i]->hisstr = NULL;
6918 if (new_hist[i].hisnum == 0)
6919 new_hist[i].hisnum = ++hisnum[type];
6920 }
6921 else
6922 clear_hist_entry(&new_hist[i]);
6923 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006924 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006925
6926 /* Free what is not kept. */
6927 for (i = 0; i < viminfo_hisidx[type]; i++)
6928 vim_free(viminfo_history[type][i].hisstr);
6929 for (i = 0; i < hislen; i++)
6930 vim_free(history[type][i].hisstr);
6931 vim_free(history[type]);
6932 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006933 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006934}
6935
6936/*
6937 * Finish reading history lines from viminfo. Not used when writing viminfo.
6938 */
6939 void
6940finish_viminfo_history(vir_T *virp)
6941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006943 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944
6945 for (type = 0; type < HIST_COUNT; ++type)
6946 {
6947 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006948 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006949
6950 if (merge)
6951 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006953 concat_history(type);
6954
Bram Moolenaard23a8232018-02-10 18:45:26 +01006955 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006956 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 }
6958}
6959
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006960/*
6961 * Write history to viminfo file in "fp".
6962 * When "merge" is TRUE merge history lines with a previously read viminfo
6963 * file, data is in viminfo_history[].
6964 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006967write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968{
6969 int i;
6970 int type;
6971 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006972 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973
6974 init_history();
6975 if (hislen == 0)
6976 return;
6977 for (type = 0; type < HIST_COUNT; ++type)
6978 {
6979 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6980 if (num_saved == 0)
6981 continue;
6982 if (num_saved < 0) /* Use default */
6983 num_saved = hislen;
6984 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6985 type == HIST_CMD ? _("Command Line") :
6986 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006987 type == HIST_EXPR ? _("Expression") :
6988 type == HIST_INPUT ? _("Input Line") :
6989 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 if (num_saved > hislen)
6991 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006992
6993 /*
6994 * Merge typed and viminfo history:
6995 * round 1: history of typed commands.
6996 * round 2: history from recently read viminfo.
6997 */
6998 for (round = 1; round <= 2; ++round)
6999 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007000 if (round == 1)
7001 /* start at newest entry, somewhere in the list */
7002 i = hisidx[type];
7003 else if (viminfo_hisidx[type] > 0)
7004 /* start at newest entry, first in the list */
7005 i = 0;
7006 else
7007 /* empty list */
7008 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007009 if (i >= 0)
7010 while (num_saved > 0
7011 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007013 char_u *p;
7014 time_t timestamp;
7015 int c = NUL;
7016
7017 if (round == 1)
7018 {
7019 p = history[type][i].hisstr;
7020 timestamp = history[type][i].time_set;
7021 }
7022 else
7023 {
7024 p = viminfo_history[type] == NULL ? NULL
7025 : viminfo_history[type][i].hisstr;
7026 timestamp = viminfo_history[type] == NULL ? 0
7027 : viminfo_history[type][i].time_set;
7028 }
7029
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007030 if (p != NULL && (round == 2
7031 || !merge
7032 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007034 --num_saved;
7035 fputc(hist_type2char(type, TRUE), fp);
7036 /* For the search history: put the separator in the
7037 * second column; use a space if there isn't one. */
7038 if (type == HIST_SEARCH)
7039 {
7040 c = p[STRLEN(p) + 1];
7041 putc(c == NUL ? ' ' : c, fp);
7042 }
7043 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007044
7045 {
7046 char cbuf[NUMBUFLEN];
7047
7048 /* New style history with a bar line. Format:
7049 * |{bartype},{histtype},{timestamp},{separator},"text" */
7050 if (c == NUL)
7051 cbuf[0] = NUL;
7052 else
7053 sprintf(cbuf, "%d", c);
7054 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7055 type, (long)timestamp, cbuf);
7056 barline_writestring(fp, p, LSIZE - 20);
7057 putc('\n', fp);
7058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007060 if (round == 1)
7061 {
7062 /* Decrement index, loop around and stop when back at
7063 * the start. */
7064 if (--i < 0)
7065 i = hislen - 1;
7066 if (i == hisidx[type])
7067 break;
7068 }
7069 else
7070 {
7071 /* Increment index. Stop at the end in the while. */
7072 ++i;
7073 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007075 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007076 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007077 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007078 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007079 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007080 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 }
7082}
7083#endif /* FEAT_VIMINFO */
7084
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085#if defined(FEAT_CMDWIN) || defined(PROTO)
7086/*
7087 * Open a window on the current command line and history. Allow editing in
7088 * the window. Returns when the window is closed.
7089 * Returns:
7090 * CR if the command is to be executed
7091 * Ctrl_C if it is to be abandoned
7092 * K_IGNORE if editing continues
7093 */
7094 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007095open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007097 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007099 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007100 win_T *wp;
7101 int i;
7102 linenr_T lnum;
7103 int histtype;
7104 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 int save_restart_edit = restart_edit;
7106 int save_State = State;
7107 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007108#ifdef FEAT_RIGHTLEFT
7109 int save_cmdmsg_rl = cmdmsg_rl;
7110#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007111#ifdef FEAT_FOLDING
7112 int save_KeyTyped;
7113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114
7115 /* Can't do this recursively. Can't do it when typing a password. */
7116 if (cmdwin_type != 0
7117# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7118 || cmdline_star > 0
7119# endif
7120 )
7121 {
7122 beep_flush();
7123 return K_IGNORE;
7124 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007125 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
7127 /* Save current window sizes. */
7128 win_size_save(&winsizes);
7129
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007131 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007132
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007133#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007134 // When using completion in Insert mode with <C-R>=<C-F> one can open the
7135 // command line window, but we don't want the popup menu then.
7136 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007137#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007138
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007139 /* don't use a new tab page */
7140 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007141 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007142
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 /* Create a window for the command-line buffer. */
7144 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7145 {
7146 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007147 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007148 return K_IGNORE;
7149 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007150 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151
7152 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007153 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007154 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007157#ifdef FEAT_FOLDING
7158 curwin->w_p_fen = FALSE;
7159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007161 curwin->w_p_rl = cmdmsg_rl;
7162 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007164 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007167 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007168 /* But don't allow switching to another buffer. */
7169 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170
Bram Moolenaar46152342005-09-07 21:18:43 +00007171 /* Showing the prompt may have set need_wait_return, reset it. */
7172 need_wait_return = FALSE;
7173
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007174 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7176 {
7177 if (p_wc == TAB)
7178 {
7179 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7180 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7181 }
7182 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7183 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007184 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007186 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7187 * sets 'textwidth' to 78). */
7188 curbuf->b_p_tw = 0;
7189
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190 /* Fill the buffer with the history. */
7191 init_history();
7192 if (hislen > 0)
7193 {
7194 i = hisidx[histtype];
7195 if (i >= 0)
7196 {
7197 lnum = 0;
7198 do
7199 {
7200 if (++i == hislen)
7201 i = 0;
7202 if (history[histtype][i].hisstr != NULL)
7203 ml_append(lnum++, history[histtype][i].hisstr,
7204 (colnr_T)0, FALSE);
7205 }
7206 while (i != hisidx[histtype]);
7207 }
7208 }
7209
7210 /* Replace the empty last line with the current command-line and put the
7211 * cursor there. */
7212 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7213 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7214 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007215 changed_line_abv_curs();
7216 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007217 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 /* No Ex mode here! */
7220 exmode_active = 0;
7221
7222 State = NORMAL;
7223# ifdef FEAT_MOUSE
7224 setmouse();
7225# endif
7226
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007228 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007229 if (restart_edit != 0) /* autocmd with ":startinsert" */
7230 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231
7232 i = RedrawingDisabled;
7233 RedrawingDisabled = 0;
7234
7235 /*
7236 * Call the main loop until <CR> or CTRL-C is typed.
7237 */
7238 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007239 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240
7241 RedrawingDisabled = i;
7242
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007243# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007244 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007245# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007248 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007249
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007250# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007251 /* Restore KeyTyped in case it is modified by autocommands */
7252 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253# endif
7254
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 exmode_active = save_exmode;
7257
Bram Moolenaarf9821062008-06-20 16:31:07 +00007258 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007260 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261 {
7262 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007263 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 }
7265 else
7266 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007267# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268 /* autocmds may abort script processing */
7269 if (aborting() && cmdwin_result != K_IGNORE)
7270 cmdwin_result = Ctrl_C;
7271# endif
7272 /* Set the new command line from the cmdline buffer. */
7273 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007274 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007276 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7277
7278 if (histtype == HIST_CMD)
7279 {
7280 /* Execute the command directly. */
7281 ccline.cmdbuff = vim_strsave((char_u *)p);
7282 cmdwin_result = CAR;
7283 }
7284 else
7285 {
7286 /* First need to cancel what we were doing. */
7287 ccline.cmdbuff = NULL;
7288 stuffcharReadbuff(':');
7289 stuffReadbuff((char_u *)p);
7290 stuffcharReadbuff(CAR);
7291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 }
7293 else if (cmdwin_result == K_XF2) /* :qa typed */
7294 {
7295 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7296 cmdwin_result = CAR;
7297 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007298 else if (cmdwin_result == Ctrl_C)
7299 {
7300 /* :q or :close, don't execute any command
7301 * and don't modify the cmd window. */
7302 ccline.cmdbuff = NULL;
7303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007304 else
7305 ccline.cmdbuff = vim_strsave(ml_get_curline());
7306 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007307 {
7308 ccline.cmdbuff = vim_strsave((char_u *)"");
7309 ccline.cmdlen = 0;
7310 ccline.cmdbufflen = 1;
7311 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007314 else
7315 {
7316 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7317 ccline.cmdbufflen = ccline.cmdlen + 1;
7318 ccline.cmdpos = curwin->w_cursor.col;
7319 if (ccline.cmdpos > ccline.cmdlen)
7320 ccline.cmdpos = ccline.cmdlen;
7321 if (cmdwin_result == K_IGNORE)
7322 {
7323 set_cmdspos_cursor();
7324 redrawcmd();
7325 }
7326 }
7327
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007329 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007330# ifdef FEAT_CONCEAL
7331 /* Avoid command-line window first character being concealed. */
7332 curwin->w_p_cole = 0;
7333# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007335 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 win_goto(old_curwin);
7337 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007338
7339 /* win_close() may have already wiped the buffer when 'bh' is
7340 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007341 if (bufref_valid(&bufref))
7342 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343
7344 /* Restore window sizes. */
7345 win_size_restore(&winsizes);
7346
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007347 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349
7350 ga_clear(&winsizes);
7351 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007352# ifdef FEAT_RIGHTLEFT
7353 cmdmsg_rl = save_cmdmsg_rl;
7354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
7356 State = save_State;
7357# ifdef FEAT_MOUSE
7358 setmouse();
7359# endif
7360
7361 return cmdwin_result;
7362}
7363#endif /* FEAT_CMDWIN */
7364
7365/*
7366 * Used for commands that either take a simple command string argument, or:
7367 * cmd << endmarker
7368 * {script}
7369 * endmarker
7370 * Returns a pointer to allocated memory with {script} or NULL.
7371 */
7372 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007373script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374{
7375 char_u *theline;
7376 char *end_pattern = NULL;
7377 char dot[] = ".";
7378 garray_T ga;
7379
7380 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7381 return NULL;
7382
7383 ga_init2(&ga, 1, 0x400);
7384
7385 if (cmd[2] != NUL)
7386 end_pattern = (char *)skipwhite(cmd + 2);
7387 else
7388 end_pattern = dot;
7389
7390 for (;;)
7391 {
7392 theline = eap->getline(
7393#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007394 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395#endif
7396 NUL, eap->cookie, 0);
7397
7398 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007399 {
7400 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 ga_concat(&ga, theline);
7405 ga_append(&ga, '\n');
7406 vim_free(theline);
7407 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007408 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409
7410 return (char_u *)ga.ga_data;
7411}