blob: b2b6f534a260da61a09d96d0508557c4a752d030 [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 Moolenaarac9fb182019-04-27 13:04:13 +0200114# if 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;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200748 *c = gchar_cursor();
749 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200750 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751 // If 'ignorecase' and 'smartcase' are set and the
752 // command line has no uppercase characters, convert
753 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200754 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200755 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200756 if (*c == firstc || vim_strchr((char_u *)(
757 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200758 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200759 // put a backslash before special characters
760 stuffcharReadbuff(*c);
761 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200762 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200763 // add any composing characters
764 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
765 {
766 int save_c = *c;
767
768 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
769 {
770 curwin->w_cursor.col += mb_char2len(*c);
771 *c = gchar_cursor();
772 stuffcharReadbuff(*c);
773 }
774 *c = save_c;
775 }
776 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200777 }
778 }
779 return OK;
780}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200781#endif
782
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200783 void
784cmdline_init(void)
785{
786 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
787}
788
Bram Moolenaar66216052017-12-16 16:33:44 +0100789/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 * getcmdline() - accept a command line starting with firstc.
791 *
792 * firstc == ':' get ":" command line.
793 * firstc == '/' or '?' get search pattern
794 * firstc == '=' get expression
795 * firstc == '@' get text for input() function
796 * firstc == '>' get text for debug mode
797 * firstc == NUL get text for :insert command
798 * firstc == -1 like NUL, and break on CTRL-C
799 *
800 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
801 * command line.
802 *
803 * Careful: getcmdline() can be called recursively!
804 *
805 * Return pointer to allocated string if there is a commandline, NULL
806 * otherwise.
807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100809getcmdline(
810 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200811 long count, // only used for incremental search
812 int indent) // indent for inside conditionals
813{
814 return getcmdline_int(firstc, count, indent, TRUE);
815}
816
817 static char_u *
818getcmdline_int(
819 int firstc,
820 long count UNUSED, // only used for incremental search
821 int indent, // indent for inside conditionals
822 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
824 int c;
825 int i;
826 int j;
827 int gotesc = FALSE; /* TRUE when <ESC> just typed */
828 int do_abbr; /* when TRUE check for abbr. */
829#ifdef FEAT_CMDHIST
830 char_u *lookfor = NULL; /* string to match */
831 int hiscnt; /* current history line in use */
832 int histype; /* history type to be used */
833#endif
834#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200835 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#endif
837 int did_wild_list = FALSE; /* did wild_list() recently */
838 int wim_index = 0; /* index in wim_flags[] */
839 int res;
840 int save_msg_scroll = msg_scroll;
841 int save_State = State; /* remember State when called */
842 int some_key_typed = FALSE; /* one of the keys was typed */
843#ifdef FEAT_MOUSE
844 /* mouse drag and release events are ignored, unless they are
845 * preceded with a mouse down event */
846 int ignore_drag_release = TRUE;
847#endif
848#ifdef FEAT_EVAL
849 int break_ctrl_c = FALSE;
850#endif
851 expand_T xpc;
852 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000853 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200854 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200855 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856
Bram Moolenaar438d1762018-09-30 17:11:48 +0200857 if (ccline.cmdbuff != NULL)
858 {
859 // Being called recursively. Since ccline is global, we need to save
860 // the current buffer and restore it when returning.
861 save_cmdline(&save_ccline);
862 did_save_ccline = TRUE;
863 }
864 if (init_ccline)
865 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
866
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#ifdef FEAT_EVAL
868 if (firstc == -1)
869 {
870 firstc = NUL;
871 break_ctrl_c = TRUE;
872 }
873#endif
874#ifdef FEAT_RIGHTLEFT
875 /* start without Hebrew mapping for a command line */
876 if (firstc == ':' || firstc == '=' || firstc == '>')
877 cmd_hkmap = 0;
878#endif
879
880 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200881
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200883 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884#endif
885
886 /*
887 * set some variables for redrawcmd()
888 */
889 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000890 ccline.cmdindent = (firstc > 0 ? indent : 0);
891
892 /* alloc initial ccline.cmdbuff */
893 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200895 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 ccline.cmdlen = ccline.cmdpos = 0;
897 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100898 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000900 /* autoindent for :insert and :append */
901 if (firstc <= 0)
902 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200903 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000904 ccline.cmdbuff[indent] = NUL;
905 ccline.cmdpos = indent;
906 ccline.cmdspos = indent;
907 ccline.cmdlen = indent;
908 }
909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000911 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913#ifdef FEAT_RIGHTLEFT
914 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
915 && (firstc == '/' || firstc == '?'))
916 cmdmsg_rl = TRUE;
917 else
918 cmdmsg_rl = FALSE;
919#endif
920
921 redir_off = TRUE; /* don't redirect the typed command */
922 if (!cmd_silent)
923 {
924 i = msg_scrolled;
925 msg_scrolled = 0; /* avoid wait_return message */
926 gotocmdline(TRUE);
927 msg_scrolled += i;
928 redrawcmdprompt(); /* draw prompt or indent */
929 set_cmdspos();
930 }
931 xpc.xp_context = EXPAND_NOTHING;
932 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000933#ifndef BACKSLASH_IN_FILENAME
934 xpc.xp_shell = FALSE;
935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000937#if defined(FEAT_EVAL)
938 if (ccline.input_fn)
939 {
940 xpc.xp_context = ccline.xp_context;
941 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200942# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000943 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000944# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000945 }
946#endif
947
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 /*
949 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
950 * doing ":@0" when register 0 doesn't contain a CR.
951 */
952 msg_scroll = FALSE;
953
954 State = CMDLINE;
955
956 if (firstc == '/' || firstc == '?' || firstc == '@')
957 {
958 /* Use ":lmap" mappings for search pattern and input(). */
959 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
960 b_im_ptr = &curbuf->b_p_iminsert;
961 else
962 b_im_ptr = &curbuf->b_p_imsearch;
963 if (*b_im_ptr == B_IMODE_LMAP)
964 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100965#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 im_set_active(*b_im_ptr == B_IMODE_IM);
967#endif
968 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100969#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 else if (p_imcmdline)
971 im_set_active(TRUE);
972#endif
973
974#ifdef FEAT_MOUSE
975 setmouse();
976#endif
977#ifdef CURSOR_SHAPE
978 ui_cursor_shape(); /* may show different cursor shape */
979#endif
980
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000981 /* When inside an autocommand for writing "exiting" may be set and
982 * terminal mode set to cooked. Need to set raw mode here then. */
983 settmode(TMODE_RAW);
984
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200985 /* Trigger CmdlineEnter autocommands. */
986 cmdline_type = firstc == NUL ? '-' : firstc;
987 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989#ifdef FEAT_CMDHIST
990 init_history();
991 hiscnt = hislen; /* set hiscnt to impossible history value */
992 histype = hist_char2type(firstc);
993#endif
994
995#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000996 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997#endif
998
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200999 /* If something above caused an error, reset the flags, we do want to type
1000 * and execute commands. Display may be messed up a bit. */
1001 if (did_emsg)
1002 redrawcmd();
1003 did_emsg = FALSE;
1004 got_int = FALSE;
1005
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 /*
1007 * Collect the command string, handling editing keys.
1008 */
1009 for (;;)
1010 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001011 redir_off = TRUE; /* Don't redirect the typed command.
1012 Repeated, because a ":redir" inside
1013 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014#ifdef USE_ON_FLY_SCROLL
1015 dont_scroll = FALSE; /* allow scrolling here */
1016#endif
1017 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1018
Bram Moolenaar72532d32018-04-07 19:09:09 +02001019 did_emsg = FALSE; /* There can't really be a reason why an error
1020 that occurs while typing a command should
1021 cause the command not to be executed. */
1022
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001024
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001025 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1026 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001027 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001028 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001029 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (KeyTyped)
1032 {
1033 some_key_typed = TRUE;
1034#ifdef FEAT_RIGHTLEFT
1035 if (cmd_hkmap)
1036 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (cmdmsg_rl && !KeyStuffed)
1038 {
1039 /* Invert horizontal movements and operations. Only when
1040 * typed by the user directly, not when the result of a
1041 * mapping. */
1042 switch (c)
1043 {
1044 case K_RIGHT: c = K_LEFT; break;
1045 case K_S_RIGHT: c = K_S_LEFT; break;
1046 case K_C_RIGHT: c = K_C_LEFT; break;
1047 case K_LEFT: c = K_RIGHT; break;
1048 case K_S_LEFT: c = K_S_RIGHT; break;
1049 case K_C_LEFT: c = K_C_RIGHT; break;
1050 }
1051 }
1052#endif
1053 }
1054
1055 /*
1056 * Ignore got_int when CTRL-C was typed here.
1057 * Don't ignore it in :global, we really need to break then, e.g., for
1058 * ":g/pat/normal /pat" (without the <CR>).
1059 * Don't ignore it for the input() function.
1060 */
1061 if ((c == Ctrl_C
1062#ifdef UNIX
1063 || c == intr_char
1064#endif
1065 )
1066#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1067 && firstc != '@'
1068#endif
1069#ifdef FEAT_EVAL
1070 && !break_ctrl_c
1071#endif
1072 && !global_busy)
1073 got_int = FALSE;
1074
1075#ifdef FEAT_CMDHIST
1076 /* free old command line when finished moving around in the history
1077 * list */
1078 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001079 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001080 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 && c != K_PAGEDOWN && c != K_PAGEUP
1082 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001083 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001085 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086#endif
1087
1088 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001089 * When there are matching completions to select <S-Tab> works like
1090 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001092 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 c = Ctrl_P;
1094
1095#ifdef FEAT_WILDMENU
1096 /* Special translations for 'wildmenu' */
1097 if (did_wild_list && p_wmnu)
1098 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001099 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001101 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 c = Ctrl_N;
1103 }
1104 /* Hitting CR after "emenu Name.": complete submenu */
1105 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1106 && ccline.cmdpos > 1
1107 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1108 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1109 && (c == '\n' || c == '\r' || c == K_KENTER))
1110 c = K_DOWN;
1111#endif
1112
1113 /* free expanded names when finished walking through matches */
1114 if (xpc.xp_numfiles != -1
1115 && !(c == p_wc && KeyTyped) && c != p_wcm
1116 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1117 && c != Ctrl_L)
1118 {
1119 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1120 did_wild_list = FALSE;
1121#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001122 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123#endif
1124 xpc.xp_context = EXPAND_NOTHING;
1125 wim_index = 0;
1126#ifdef FEAT_WILDMENU
1127 if (p_wmnu && wild_menu_showing != 0)
1128 {
1129 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001130 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001131
1132 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001133 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134
1135 if (wild_menu_showing == WM_SCROLLED)
1136 {
1137 /* Entered command line, move it up */
1138 cmdline_row--;
1139 redrawcmd();
1140 }
1141 else if (save_p_ls != -1)
1142 {
1143 /* restore 'laststatus' and 'winminheight' */
1144 p_ls = save_p_ls;
1145 p_wmh = save_p_wmh;
1146 last_status(FALSE);
1147 update_screen(VALID); /* redraw the screen NOW */
1148 redrawcmd();
1149 save_p_ls = -1;
1150 }
1151 else
1152 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 redraw_statuslines();
1155 }
1156 KeyTyped = skt;
1157 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001158 if (ccline.input_fn)
1159 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 }
1161#endif
1162 }
1163
1164#ifdef FEAT_WILDMENU
1165 /* Special translations for 'wildmenu' */
1166 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1167 {
1168 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001169 if (c == K_DOWN && ccline.cmdpos > 0
1170 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001172 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 /* Hitting <Up>: Remove one submenu name in front of the
1175 * cursor */
1176 int found = FALSE;
1177
1178 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1179 i = 0;
1180 while (--j > 0)
1181 {
1182 /* check for start of menu name */
1183 if (ccline.cmdbuff[j] == ' '
1184 && ccline.cmdbuff[j - 1] != '\\')
1185 {
1186 i = j + 1;
1187 break;
1188 }
1189 /* check for start of submenu name */
1190 if (ccline.cmdbuff[j] == '.'
1191 && ccline.cmdbuff[j - 1] != '\\')
1192 {
1193 if (found)
1194 {
1195 i = j + 1;
1196 break;
1197 }
1198 else
1199 found = TRUE;
1200 }
1201 }
1202 if (i > 0)
1203 cmdline_del(i);
1204 c = p_wc;
1205 xpc.xp_context = EXPAND_NOTHING;
1206 }
1207 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001208 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001209 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001210 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 {
1212 char_u upseg[5];
1213
1214 upseg[0] = PATHSEP;
1215 upseg[1] = '.';
1216 upseg[2] = '.';
1217 upseg[3] = PATHSEP;
1218 upseg[4] = NUL;
1219
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001220 if (c == K_DOWN
1221 && ccline.cmdpos > 0
1222 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1223 && (ccline.cmdpos < 3
1224 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1226 {
1227 /* go down a directory */
1228 c = p_wc;
1229 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001230 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 {
1232 /* If in a direct ancestor, strip off one ../ to go down */
1233 int found = FALSE;
1234
1235 j = ccline.cmdpos;
1236 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1237 while (--j > i)
1238 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001239 if (has_mbyte)
1240 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 if (vim_ispathsep(ccline.cmdbuff[j]))
1242 {
1243 found = TRUE;
1244 break;
1245 }
1246 }
1247 if (found
1248 && ccline.cmdbuff[j - 1] == '.'
1249 && ccline.cmdbuff[j - 2] == '.'
1250 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1251 {
1252 cmdline_del(j - 2);
1253 c = p_wc;
1254 }
1255 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001256 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 /* go up a directory */
1259 int found = FALSE;
1260
1261 j = ccline.cmdpos - 1;
1262 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1263 while (--j > i)
1264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 if (has_mbyte)
1266 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 if (vim_ispathsep(ccline.cmdbuff[j])
1268#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001269 && vim_strchr((char_u *)" *?[{`$%#",
1270 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271#endif
1272 )
1273 {
1274 if (found)
1275 {
1276 i = j + 1;
1277 break;
1278 }
1279 else
1280 found = TRUE;
1281 }
1282 }
1283
1284 if (!found)
1285 j = i;
1286 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1287 j += 4;
1288 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1289 && j == i)
1290 j += 3;
1291 else
1292 j = 0;
1293 if (j > 0)
1294 {
1295 /* TODO this is only for DOS/UNIX systems - need to put in
1296 * machine-specific stuff here and in upseg init */
1297 cmdline_del(j);
1298 put_on_cmdline(upseg + 1, 3, FALSE);
1299 }
1300 else if (ccline.cmdpos > i)
1301 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001302
1303 /* Now complete in the new directory. Set KeyTyped in case the
1304 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001306 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309
1310#endif /* FEAT_WILDMENU */
1311
1312 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1313 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1314 if (c == Ctrl_BSL)
1315 {
1316 ++no_mapping;
1317 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001318 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 --no_mapping;
1320 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001321 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1322 * is in a mapping. */
1323 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001324 || (ccline.cmdfirstc == '=' && KeyTyped)
1325#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001326 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001327#endif
1328 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 {
1330 vungetc(c);
1331 c = Ctrl_BSL;
1332 }
1333#ifdef FEAT_EVAL
1334 else if (c == 'e')
1335 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001336 char_u *p = NULL;
1337 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338
1339 /*
1340 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001341 * Need to save and restore the current command line, to be
1342 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 */
1344 if (ccline.cmdpos == ccline.cmdlen)
1345 new_cmdpos = 99999; /* keep it at the end */
1346 else
1347 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001348
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 if (c == '=')
1351 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001352 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001353 * to avoid nasty things like going to another buffer when
1354 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001355 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001357 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001358
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001359 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001361 len = (int)STRLEN(p);
1362 if (realloc_cmdbuff(len + 1) == OK)
1363 {
1364 ccline.cmdlen = len;
1365 STRCPY(ccline.cmdbuff, p);
1366 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001368 /* Restore the cursor or use the position set with
1369 * set_cmdline_pos(). */
1370 if (new_cmdpos > ccline.cmdlen)
1371 ccline.cmdpos = ccline.cmdlen;
1372 else
1373 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001375 KeyTyped = FALSE; /* Don't do p_wc completion. */
1376 redrawcmd();
1377 goto cmdline_changed;
1378 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001379 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381 }
1382 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001383 got_int = FALSE; /* don't abandon the command line */
1384 did_emsg = FALSE;
1385 emsg_on_display = FALSE;
1386 redrawcmd();
1387 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389#endif
1390 else
1391 {
1392 if (c == Ctrl_G && p_im && restart_edit == 0)
1393 restart_edit = 'a';
1394 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1395 in history */
1396 goto returncmd; /* back to Normal mode */
1397 }
1398 }
1399
1400#ifdef FEAT_CMDWIN
1401 if (c == cedit_key || c == K_CMDWIN)
1402 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001403 if (ex_normal_busy == 0 && got_int == FALSE)
1404 {
1405 /*
1406 * Open a window to edit the command line (and history).
1407 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001408 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001409 some_key_typed = TRUE;
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412# ifdef FEAT_DIGRAPHS
1413 else
1414# endif
1415#endif
1416#ifdef FEAT_DIGRAPHS
1417 c = do_digraph(c);
1418#endif
1419
1420 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1421 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1422 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001423 /* In Ex mode a backslash escapes a newline. */
1424 if (exmode_active
1425 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001426 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001427 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001428 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001430 if (c == K_KENTER)
1431 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001433 else
1434 {
1435 gotesc = FALSE; /* Might have typed ESC previously, don't
1436 truncate the cmdline now. */
1437 if (ccheck_abbr(c + ABBR_OFF))
1438 goto cmdline_changed;
1439 if (!cmd_silent)
1440 {
1441 windgoto(msg_row, 0);
1442 out_flush();
1443 }
1444 break;
1445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 }
1447
1448 /*
1449 * Completion for 'wildchar' or 'wildcharm' key.
1450 * - hitting <ESC> twice means: abandon command line.
1451 * - wildcard expansion is only done when the 'wildchar' key is really
1452 * typed, not when it comes from a macro
1453 */
1454 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1455 {
1456 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1457 {
1458 /* if 'wildmode' contains "list" may still need to list */
1459 if (xpc.xp_numfiles > 1
1460 && !did_wild_list
1461 && (wim_flags[wim_index] & WIM_LIST))
1462 {
1463 (void)showmatches(&xpc, FALSE);
1464 redrawcmd();
1465 did_wild_list = TRUE;
1466 }
1467 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001468 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1469 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001471 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1472 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 else
1474 res = OK; /* don't insert 'wildchar' now */
1475 }
1476 else /* typed p_wc first time */
1477 {
1478 wim_index = 0;
1479 j = ccline.cmdpos;
1480 /* if 'wildmode' first contains "longest", get longest
1481 * common part */
1482 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001483 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1484 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001486 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1487 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 /* if interrupted while completing, behave like it failed */
1490 if (got_int)
1491 {
1492 (void)vpeekc(); /* remove <C-C> from input stream */
1493 got_int = FALSE; /* don't abandon the command line */
1494 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1495#ifdef FEAT_WILDMENU
1496 xpc.xp_context = EXPAND_NOTHING;
1497#endif
1498 goto cmdline_changed;
1499 }
1500
1501 /* when more than one match, and 'wildmode' first contains
1502 * "list", or no change and 'wildmode' contains "longest,list",
1503 * list all matches */
1504 if (res == OK && xpc.xp_numfiles > 1)
1505 {
1506 /* a "longest" that didn't do anything is skipped (but not
1507 * "list:longest") */
1508 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1509 wim_index = 1;
1510 if ((wim_flags[wim_index] & WIM_LIST)
1511#ifdef FEAT_WILDMENU
1512 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1513#endif
1514 )
1515 {
1516 if (!(wim_flags[0] & WIM_LONGEST))
1517 {
1518#ifdef FEAT_WILDMENU
1519 int p_wmnu_save = p_wmnu;
1520 p_wmnu = 0;
1521#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001522 /* remove match */
1523 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#ifdef FEAT_WILDMENU
1525 p_wmnu = p_wmnu_save;
1526#endif
1527 }
1528#ifdef FEAT_WILDMENU
1529 (void)showmatches(&xpc, p_wmnu
1530 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1531#else
1532 (void)showmatches(&xpc, FALSE);
1533#endif
1534 redrawcmd();
1535 did_wild_list = TRUE;
1536 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001537 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1538 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001540 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1541 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 }
1543 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001544 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 }
1546#ifdef FEAT_WILDMENU
1547 else if (xpc.xp_numfiles == -1)
1548 xpc.xp_context = EXPAND_NOTHING;
1549#endif
1550 }
1551 if (wim_index < 3)
1552 ++wim_index;
1553 if (c == ESC)
1554 gotesc = TRUE;
1555 if (res == OK)
1556 goto cmdline_changed;
1557 }
1558
1559 gotesc = FALSE;
1560
1561 /* <S-Tab> goes to last match, in a clumsy way */
1562 if (c == K_S_TAB && KeyTyped)
1563 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001564 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1565 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1566 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 goto cmdline_changed;
1568 }
1569
1570 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1571 c = NL;
1572
1573 do_abbr = TRUE; /* default: check for abbreviation */
1574
1575 /*
1576 * Big switch for a typed command line character.
1577 */
1578 switch (c)
1579 {
1580 case K_BS:
1581 case Ctrl_H:
1582 case K_DEL:
1583 case K_KDEL:
1584 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 if (c == K_KDEL)
1586 c = K_DEL;
1587
1588 /*
1589 * delete current character is the same as backspace on next
1590 * character, except at end of line
1591 */
1592 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1593 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (has_mbyte && c == K_DEL)
1595 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1596 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (ccline.cmdpos > 0)
1598 {
1599 char_u *p;
1600
1601 j = ccline.cmdpos;
1602 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 if (has_mbyte)
1604 {
1605 p = mb_prevptr(ccline.cmdbuff, p);
1606 if (c == Ctrl_W)
1607 {
1608 while (p > ccline.cmdbuff && vim_isspace(*p))
1609 p = mb_prevptr(ccline.cmdbuff, p);
1610 i = mb_get_class(p);
1611 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1612 p = mb_prevptr(ccline.cmdbuff, p);
1613 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001614 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001617 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 {
1619 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1620 --p;
1621 i = vim_iswordc(p[-1]);
1622 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1623 && vim_iswordc(p[-1]) == i)
1624 --p;
1625 }
1626 else
1627 --p;
1628 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1629 ccline.cmdlen -= j - ccline.cmdpos;
1630 i = ccline.cmdpos;
1631 while (i < ccline.cmdlen)
1632 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1633
1634 /* Truncate at the end, required for multi-byte chars. */
1635 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001636#ifdef FEAT_SEARCH_EXTRA
1637 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001638 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001639 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001640 /* save view settings, so that the screen
1641 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001642 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001643 }
1644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 redrawcmd();
1646 }
1647 else if (ccline.cmdlen == 0 && c != Ctrl_W
1648 && ccline.cmdprompt == NULL && indent == 0)
1649 {
1650 /* In ex and debug mode it doesn't make sense to return. */
1651 if (exmode_active
1652#ifdef FEAT_EVAL
1653 || ccline.cmdfirstc == '>'
1654#endif
1655 )
1656 goto cmdline_not_changed;
1657
Bram Moolenaard23a8232018-02-10 18:45:26 +01001658 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 if (!cmd_silent)
1660 {
1661#ifdef FEAT_RIGHTLEFT
1662 if (cmdmsg_rl)
1663 msg_col = Columns;
1664 else
1665#endif
1666 msg_col = 0;
1667 msg_putchar(' '); /* delete ':' */
1668 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001669#ifdef FEAT_SEARCH_EXTRA
1670 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001671 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001672#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 redraw_cmdline = TRUE;
1674 goto returncmd; /* back to cmd mode */
1675 }
1676 goto cmdline_changed;
1677
1678 case K_INS:
1679 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 ccline.overstrike = !ccline.overstrike;
1681#ifdef CURSOR_SHAPE
1682 ui_cursor_shape(); /* may show different cursor shape */
1683#endif
1684 goto cmdline_not_changed;
1685
1686 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001687 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 {
1689 /* ":lmap" mappings exists, toggle use of mappings. */
1690 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001691#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 im_set_active(FALSE); /* Disable input method */
1693#endif
1694 if (b_im_ptr != NULL)
1695 {
1696 if (State & LANGMAP)
1697 *b_im_ptr = B_IMODE_LMAP;
1698 else
1699 *b_im_ptr = B_IMODE_NONE;
1700 }
1701 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001702#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 else
1704 {
1705 /* There are no ":lmap" mappings, toggle IM. When
1706 * 'imdisable' is set don't try getting the status, it's
1707 * always off. */
1708 if ((p_imdisable && b_im_ptr != NULL)
1709 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1710 {
1711 im_set_active(FALSE); /* Disable input method */
1712 if (b_im_ptr != NULL)
1713 *b_im_ptr = B_IMODE_NONE;
1714 }
1715 else
1716 {
1717 im_set_active(TRUE); /* Enable input method */
1718 if (b_im_ptr != NULL)
1719 *b_im_ptr = B_IMODE_IM;
1720 }
1721 }
1722#endif
1723 if (b_im_ptr != NULL)
1724 {
1725 if (b_im_ptr == &curbuf->b_p_iminsert)
1726 set_iminsert_global();
1727 else
1728 set_imsearch_global();
1729 }
1730#ifdef CURSOR_SHAPE
1731 ui_cursor_shape(); /* may show different cursor shape */
1732#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001733#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 /* Show/unshow value of 'keymap' in status lines later. */
1735 status_redraw_curbuf();
1736#endif
1737 goto cmdline_not_changed;
1738
1739/* case '@': only in very old vi */
1740 case Ctrl_U:
1741 /* delete all characters left of the cursor */
1742 j = ccline.cmdpos;
1743 ccline.cmdlen -= j;
1744 i = ccline.cmdpos = 0;
1745 while (i < ccline.cmdlen)
1746 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1747 /* Truncate at the end, required for multi-byte chars. */
1748 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001749#ifdef FEAT_SEARCH_EXTRA
1750 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001751 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 redrawcmd();
1754 goto cmdline_changed;
1755
1756#ifdef FEAT_CLIPBOARD
1757 case Ctrl_Y:
1758 /* Copy the modeless selection, if there is one. */
1759 if (clip_star.state != SELECT_CLEARED)
1760 {
1761 if (clip_star.state == SELECT_DONE)
1762 clip_copy_modeless_selection(TRUE);
1763 goto cmdline_not_changed;
1764 }
1765 break;
1766#endif
1767
1768 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1769 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001770 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001771 * ":normal" runs out of characters. */
1772 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001773 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 goto cmdline_not_changed;
1775
1776 gotesc = TRUE; /* will free ccline.cmdbuff after
1777 putting it in history */
1778 goto returncmd; /* back to cmd mode */
1779
1780 case Ctrl_R: /* insert register */
1781#ifdef USE_ON_FLY_SCROLL
1782 dont_scroll = TRUE; /* disallow scrolling here */
1783#endif
1784 putcmdline('"', TRUE);
1785 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001786 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 if (i == Ctrl_O)
1788 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1789 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001790 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001791 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 --no_mapping;
1793#ifdef FEAT_EVAL
1794 /*
1795 * Insert the result of an expression.
1796 * Need to save the current command line, to be able to enter
1797 * a new one...
1798 */
1799 new_cmdpos = -1;
1800 if (c == '=')
1801 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001802 if (ccline.cmdfirstc == '=' // can't do this recursively
1803 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 {
1805 beep_flush();
1806 c = ESC;
1807 }
1808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 }
1811#endif
1812 if (c != ESC) /* use ESC to cancel inserting register */
1813 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001814 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001815
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001816#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001817 /* When there was a serious error abort getting the
1818 * command line. */
1819 if (aborting())
1820 {
1821 gotesc = TRUE; /* will free ccline.cmdbuff after
1822 putting it in history */
1823 goto returncmd; /* back to cmd mode */
1824 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 KeyTyped = FALSE; /* Don't do p_wc completion. */
1827#ifdef FEAT_EVAL
1828 if (new_cmdpos >= 0)
1829 {
1830 /* set_cmdline_pos() was used */
1831 if (new_cmdpos > ccline.cmdlen)
1832 ccline.cmdpos = ccline.cmdlen;
1833 else
1834 ccline.cmdpos = new_cmdpos;
1835 }
1836#endif
1837 }
1838 redrawcmd();
1839 goto cmdline_changed;
1840
1841 case Ctrl_D:
1842 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1843 break; /* Use ^D as normal char instead */
1844
1845 redrawcmd();
1846 continue; /* don't do incremental search now */
1847
1848 case K_RIGHT:
1849 case K_S_RIGHT:
1850 case K_C_RIGHT:
1851 do
1852 {
1853 if (ccline.cmdpos >= ccline.cmdlen)
1854 break;
1855 i = cmdline_charsize(ccline.cmdpos);
1856 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1857 break;
1858 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001860 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 + ccline.cmdpos);
1862 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 ++ccline.cmdpos;
1864 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001865 while ((c == K_S_RIGHT || c == K_C_RIGHT
1866 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (has_mbyte)
1869 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 goto cmdline_not_changed;
1871
1872 case K_LEFT:
1873 case K_S_LEFT:
1874 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001875 if (ccline.cmdpos == 0)
1876 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 do
1878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (has_mbyte) /* move to first byte of char */
1881 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1882 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1884 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001885 while (ccline.cmdpos > 0
1886 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001887 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (has_mbyte)
1890 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 goto cmdline_not_changed;
1892
1893 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001894 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001895 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896
Bram Moolenaar4f974752019-02-17 17:44:42 +01001897#ifdef FEAT_GUI_MSWIN
1898 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1899 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001900 case K_F4:
1901 if (mod_mask == MOD_MASK_ALT)
1902 {
1903 redrawcmd(); /* somehow the cmdline is cleared */
1904 goto cmdline_not_changed;
1905 }
1906 break;
1907#endif
1908
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909#ifdef FEAT_MOUSE
1910 case K_MIDDLEDRAG:
1911 case K_MIDDLERELEASE:
1912 goto cmdline_not_changed; /* Ignore mouse */
1913
1914 case K_MIDDLEMOUSE:
1915# ifdef FEAT_GUI
1916 /* When GUI is active, also paste when 'mouse' is empty */
1917 if (!gui.in_use)
1918# endif
1919 if (!mouse_has(MOUSE_COMMAND))
1920 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001921# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001923 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001925# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001926 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 redrawcmd();
1928 goto cmdline_changed;
1929
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001930# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001932 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 redrawcmd();
1934 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001935# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937 case K_LEFTDRAG:
1938 case K_LEFTRELEASE:
1939 case K_RIGHTDRAG:
1940 case K_RIGHTRELEASE:
1941 /* Ignore drag and release events when the button-down wasn't
1942 * seen before. */
1943 if (ignore_drag_release)
1944 goto cmdline_not_changed;
1945 /* FALLTHROUGH */
1946 case K_LEFTMOUSE:
1947 case K_RIGHTMOUSE:
1948 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1949 ignore_drag_release = TRUE;
1950 else
1951 ignore_drag_release = FALSE;
1952# ifdef FEAT_GUI
1953 /* When GUI is active, also move when 'mouse' is empty */
1954 if (!gui.in_use)
1955# endif
1956 if (!mouse_has(MOUSE_COMMAND))
1957 goto cmdline_not_changed; /* Ignore mouse */
1958# ifdef FEAT_CLIPBOARD
1959 if (mouse_row < cmdline_row && clip_star.available)
1960 {
1961 int button, is_click, is_drag;
1962
1963 /*
1964 * Handle modeless selection.
1965 */
1966 button = get_mouse_button(KEY2TERMCAP1(c),
1967 &is_click, &is_drag);
1968 if (mouse_model_popup() && button == MOUSE_LEFT
1969 && (mod_mask & MOD_MASK_SHIFT))
1970 {
1971 /* Translate shift-left to right button. */
1972 button = MOUSE_RIGHT;
1973 mod_mask &= ~MOD_MASK_SHIFT;
1974 }
1975 clip_modeless(button, is_click, is_drag);
1976 goto cmdline_not_changed;
1977 }
1978# endif
1979
1980 set_cmdspos();
1981 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1982 ++ccline.cmdpos)
1983 {
1984 i = cmdline_charsize(ccline.cmdpos);
1985 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1986 && mouse_col < ccline.cmdspos % Columns + i)
1987 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 if (has_mbyte)
1989 {
1990 /* Count ">" for double-wide char that doesn't fit. */
1991 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001992 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 + ccline.cmdpos) - 1;
1994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 ccline.cmdspos += i;
1996 }
1997 goto cmdline_not_changed;
1998
1999 /* Mouse scroll wheel: ignored here */
2000 case K_MOUSEDOWN:
2001 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002002 case K_MOUSELEFT:
2003 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 /* Alternate buttons ignored here */
2005 case K_X1MOUSE:
2006 case K_X1DRAG:
2007 case K_X1RELEASE:
2008 case K_X2MOUSE:
2009 case K_X2DRAG:
2010 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002011 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 goto cmdline_not_changed;
2013
2014#endif /* FEAT_MOUSE */
2015
2016#ifdef FEAT_GUI
2017 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2018 case K_LEFTRELEASE_NM:
2019 goto cmdline_not_changed;
2020
2021 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002022 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
2024 gui_do_scroll();
2025 redrawcmd();
2026 }
2027 goto cmdline_not_changed;
2028
2029 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002030 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002032 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 redrawcmd();
2034 }
2035 goto cmdline_not_changed;
2036#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002037#ifdef FEAT_GUI_TABLINE
2038 case K_TABLINE:
2039 case K_TABMENU:
2040 /* Don't want to change any tabs here. Make sure the same tab
2041 * is still selected. */
2042 if (gui_use_tabline())
2043 gui_mch_set_curtab(tabpage_index(curtab));
2044 goto cmdline_not_changed;
2045#endif
2046
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 case K_SELECT: /* end of Select mode mapping - ignore */
2048 goto cmdline_not_changed;
2049
2050 case Ctrl_B: /* begin of command line */
2051 case K_HOME:
2052 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 case K_S_HOME:
2054 case K_C_HOME:
2055 ccline.cmdpos = 0;
2056 set_cmdspos();
2057 goto cmdline_not_changed;
2058
2059 case Ctrl_E: /* end of command line */
2060 case K_END:
2061 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 case K_S_END:
2063 case K_C_END:
2064 ccline.cmdpos = ccline.cmdlen;
2065 set_cmdspos_cursor();
2066 goto cmdline_not_changed;
2067
2068 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002069 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 break;
2071 goto cmdline_changed;
2072
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002073 case Ctrl_L:
2074#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002075 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002076 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002077#endif
2078
2079 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002080 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 break;
2082 goto cmdline_changed;
2083
2084 case Ctrl_N: /* next match */
2085 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002086 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002088 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2089 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002091 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002094 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 case K_UP:
2096 case K_DOWN:
2097 case K_S_UP:
2098 case K_S_DOWN:
2099 case K_PAGEUP:
2100 case K_KPAGEUP:
2101 case K_PAGEDOWN:
2102 case K_KPAGEDOWN:
2103 if (hislen == 0 || firstc == NUL) /* no history */
2104 goto cmdline_not_changed;
2105
2106 i = hiscnt;
2107
2108 /* save current command string so it can be restored later */
2109 if (lookfor == NULL)
2110 {
2111 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2112 goto cmdline_not_changed;
2113 lookfor[ccline.cmdpos] = NUL;
2114 }
2115
2116 j = (int)STRLEN(lookfor);
2117 for (;;)
2118 {
2119 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002120 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002121 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 {
2123 if (hiscnt == hislen) /* first time */
2124 hiscnt = hisidx[histype];
2125 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2126 hiscnt = hislen - 1;
2127 else if (hiscnt != hisidx[histype] + 1)
2128 --hiscnt;
2129 else /* at top of list */
2130 {
2131 hiscnt = i;
2132 break;
2133 }
2134 }
2135 else /* one step forwards */
2136 {
2137 /* on last entry, clear the line */
2138 if (hiscnt == hisidx[histype])
2139 {
2140 hiscnt = hislen;
2141 break;
2142 }
2143
2144 /* not on a history line, nothing to do */
2145 if (hiscnt == hislen)
2146 break;
2147 if (hiscnt == hislen - 1) /* wrap around */
2148 hiscnt = 0;
2149 else
2150 ++hiscnt;
2151 }
2152 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2153 {
2154 hiscnt = i;
2155 break;
2156 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002157 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002158 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 || STRNCMP(history[histype][hiscnt].hisstr,
2160 lookfor, (size_t)j) == 0)
2161 break;
2162 }
2163
2164 if (hiscnt != i) /* jumped to other entry */
2165 {
2166 char_u *p;
2167 int len;
2168 int old_firstc;
2169
Bram Moolenaar438d1762018-09-30 17:11:48 +02002170 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002171 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 if (hiscnt == hislen)
2173 p = lookfor; /* back to the old one */
2174 else
2175 p = history[histype][hiscnt].hisstr;
2176
2177 if (histype == HIST_SEARCH
2178 && p != lookfor
2179 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2180 {
2181 /* Correct for the separator character used when
2182 * adding the history entry vs the one used now.
2183 * First loop: count length.
2184 * Second loop: copy the characters. */
2185 for (i = 0; i <= 1; ++i)
2186 {
2187 len = 0;
2188 for (j = 0; p[j] != NUL; ++j)
2189 {
2190 /* Replace old sep with new sep, unless it is
2191 * escaped. */
2192 if (p[j] == old_firstc
2193 && (j == 0 || p[j - 1] != '\\'))
2194 {
2195 if (i > 0)
2196 ccline.cmdbuff[len] = firstc;
2197 }
2198 else
2199 {
2200 /* Escape new sep, unless it is already
2201 * escaped. */
2202 if (p[j] == firstc
2203 && (j == 0 || p[j - 1] != '\\'))
2204 {
2205 if (i > 0)
2206 ccline.cmdbuff[len] = '\\';
2207 ++len;
2208 }
2209 if (i > 0)
2210 ccline.cmdbuff[len] = p[j];
2211 }
2212 ++len;
2213 }
2214 if (i == 0)
2215 {
2216 alloc_cmdbuff(len);
2217 if (ccline.cmdbuff == NULL)
2218 goto returncmd;
2219 }
2220 }
2221 ccline.cmdbuff[len] = NUL;
2222 }
2223 else
2224 {
2225 alloc_cmdbuff((int)STRLEN(p));
2226 if (ccline.cmdbuff == NULL)
2227 goto returncmd;
2228 STRCPY(ccline.cmdbuff, p);
2229 }
2230
2231 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2232 redrawcmd();
2233 goto cmdline_changed;
2234 }
2235 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002236#endif
2237 goto cmdline_not_changed;
2238
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002239#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002240 case Ctrl_G: /* next match */
2241 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002242 if (may_adjust_incsearch_highlighting(
2243 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002244 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002245 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246#endif
2247
2248 case Ctrl_V:
2249 case Ctrl_Q:
2250#ifdef FEAT_MOUSE
2251 ignore_drag_release = TRUE;
2252#endif
2253 putcmdline('^', TRUE);
2254 c = get_literal(); /* get next (two) character(s) */
2255 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002256 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 /* may need to remove ^ when composing char was typed */
2258 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2259 {
2260 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2261 msg_putchar(' ');
2262 cursorcmd();
2263 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 break;
2265
2266#ifdef FEAT_DIGRAPHS
2267 case Ctrl_K:
2268#ifdef FEAT_MOUSE
2269 ignore_drag_release = TRUE;
2270#endif
2271 putcmdline('?', TRUE);
2272#ifdef USE_ON_FLY_SCROLL
2273 dont_scroll = TRUE; /* disallow scrolling here */
2274#endif
2275 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002276 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 if (c != NUL)
2278 break;
2279
2280 redrawcmd();
2281 goto cmdline_not_changed;
2282#endif /* FEAT_DIGRAPHS */
2283
2284#ifdef FEAT_RIGHTLEFT
2285 case Ctrl__: /* CTRL-_: switch language mode */
2286 if (!p_ari)
2287 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002288 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 goto cmdline_not_changed;
2290#endif
2291
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002292 case K_PS:
2293 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2294 goto cmdline_changed;
2295
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 default:
2297#ifdef UNIX
2298 if (c == intr_char)
2299 {
2300 gotesc = TRUE; /* will free ccline.cmdbuff after
2301 putting it in history */
2302 goto returncmd; /* back to Normal mode */
2303 }
2304#endif
2305 /*
2306 * Normal character with no special meaning. Just set mod_mask
2307 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2308 * the string <S-Space>. This should only happen after ^V.
2309 */
2310 if (!IS_SPECIAL(c))
2311 mod_mask = 0x0;
2312 break;
2313 }
2314 /*
2315 * End of switch on command line character.
2316 * We come here if we have a normal character.
2317 */
2318
Bram Moolenaar13505972019-01-24 15:04:48 +01002319 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2320 && (ccheck_abbr(
2321 // Add ABBR_OFF for characters above 0x100, this is
2322 // what check_abbr() expects.
2323 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2324 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 goto cmdline_changed;
2326
2327 /*
2328 * put the character in the command line
2329 */
2330 if (IS_SPECIAL(c) || mod_mask != 0)
2331 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2332 else
2333 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 if (has_mbyte)
2335 {
2336 j = (*mb_char2bytes)(c, IObuff);
2337 IObuff[j] = NUL; /* exclude composing chars */
2338 put_on_cmdline(IObuff, j, TRUE);
2339 }
2340 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
2342 IObuff[0] = c;
2343 put_on_cmdline(IObuff, 1, TRUE);
2344 }
2345 }
2346 goto cmdline_changed;
2347
2348/*
2349 * This part implements incremental searches for "/" and "?"
2350 * Jump to cmdline_not_changed when a character has been read but the command
2351 * line did not change. Then we only search and redraw if something changed in
2352 * the past.
2353 * Jump to cmdline_changed when the command line did change.
2354 * (Sorry for the goto's, I know it is ugly).
2355 */
2356cmdline_not_changed:
2357#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002358 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 continue;
2360#endif
2361
2362cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002363 /* Trigger CmdlineChanged autocommands. */
2364 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002367 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368#endif
2369
2370#ifdef FEAT_RIGHTLEFT
2371 if (cmdmsg_rl
2372# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002373 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374# endif
2375 )
2376 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002377 * right-left typing. Not efficient, but it works.
2378 * Do it only when there are no characters left to read
2379 * to avoid useless intermediate redraws. */
2380 if (vpeekc() == NUL)
2381 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382#endif
2383 }
2384
2385returncmd:
2386
2387#ifdef FEAT_RIGHTLEFT
2388 cmdmsg_rl = FALSE;
2389#endif
2390
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002392 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
2394#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002395 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396#endif
2397
2398 if (ccline.cmdbuff != NULL)
2399 {
2400 /*
2401 * Put line in history buffer (":" and "=" only when it was typed).
2402 */
2403#ifdef FEAT_CMDHIST
2404 if (ccline.cmdlen && firstc != NUL
2405 && (some_key_typed || histype == HIST_SEARCH))
2406 {
2407 add_to_history(histype, ccline.cmdbuff, TRUE,
2408 histype == HIST_SEARCH ? firstc : NUL);
2409 if (firstc == ':')
2410 {
2411 vim_free(new_last_cmdline);
2412 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2413 }
2414 }
2415#endif
2416
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002417 if (gotesc)
2418 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420
2421 /*
2422 * If the screen was shifted up, redraw the whole screen (later).
2423 * If the line is too long, clear it, so ruler and shown command do
2424 * not get printed in the middle of it.
2425 */
2426 msg_check();
2427 msg_scroll = save_msg_scroll;
2428 redir_off = FALSE;
2429
2430 /* When the command line was typed, no need for a wait-return prompt. */
2431 if (some_key_typed)
2432 need_wait_return = FALSE;
2433
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002434 /* Trigger CmdlineLeave autocommands. */
2435 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002436
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002438#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2440 im_save_status(b_im_ptr);
2441 im_set_active(FALSE);
2442#endif
2443#ifdef FEAT_MOUSE
2444 setmouse();
2445#endif
2446#ifdef CURSOR_SHAPE
2447 ui_cursor_shape(); /* may show different cursor shape */
2448#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002449 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450
Bram Moolenaar438d1762018-09-30 17:11:48 +02002451theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002452 {
2453 char_u *p = ccline.cmdbuff;
2454
Bram Moolenaar438d1762018-09-30 17:11:48 +02002455 if (did_save_ccline)
2456 restore_cmdline(&save_ccline);
2457 else
2458 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002459 return p;
2460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461}
2462
2463#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2464/*
2465 * Get a command line with a prompt.
2466 * This is prepared to be called recursively from getcmdline() (e.g. by
2467 * f_input() when evaluating an expression from CTRL-R =).
2468 * Returns the command line in allocated memory, or NULL.
2469 */
2470 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002471getcmdline_prompt(
2472 int firstc,
2473 char_u *prompt, /* command line prompt */
2474 int attr, /* attributes for prompt */
2475 int xp_context, /* type of expansion */
2476 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 char_u *s;
2479 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002480 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002482 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483
Bram Moolenaar438d1762018-09-30 17:11:48 +02002484 if (ccline.cmdbuff != NULL)
2485 {
2486 // Save the values of the current cmdline and restore them below.
2487 save_cmdline(&save_ccline);
2488 did_save_ccline = TRUE;
2489 }
2490
2491 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 ccline.cmdprompt = prompt;
2493 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002494# ifdef FEAT_EVAL
2495 ccline.xp_context = xp_context;
2496 ccline.xp_arg = xp_arg;
2497 ccline.input_fn = (firstc == '@');
2498# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002499 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002500 s = getcmdline_int(firstc, 1L, 0, FALSE);
2501
2502 if (did_save_ccline)
2503 restore_cmdline(&save_ccline);
2504
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002505 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002506 /* Restore msg_col, the prompt from input() may have changed it.
2507 * But only if called recursively and the commandline is therefore being
2508 * restored to an old one; if not, the input() prompt stays on the screen,
2509 * so we need its modified msg_col left intact. */
2510 if (ccline.cmdbuff != NULL)
2511 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512
2513 return s;
2514}
2515#endif
2516
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002517/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002518 * Return TRUE when the text must not be changed and we can't switch to
2519 * another window or buffer. Used when editing the command line, evaluating
2520 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002521 */
2522 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002523text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002524{
2525#ifdef FEAT_CMDWIN
2526 if (cmdwin_type != 0)
2527 return TRUE;
2528#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002529 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002530}
2531
2532/*
2533 * Give an error message for a command that isn't allowed while the cmdline
2534 * window is open or editing the cmdline in another way.
2535 */
2536 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002537text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002538{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002539 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002540}
2541
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002542 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002543get_text_locked_msg(void)
2544{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002545#ifdef FEAT_CMDWIN
2546 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002547 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002548#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002549 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002550}
2551
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002552/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002553 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2554 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002555 */
2556 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002557curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002558{
2559 if (curbuf_lock > 0)
2560 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002561 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002562 return TRUE;
2563 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002564 return allbuf_locked();
2565}
2566
2567/*
2568 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2569 * message.
2570 */
2571 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002572allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002573{
2574 if (allbuf_lock > 0)
2575 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002576 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002577 return TRUE;
2578 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002579 return FALSE;
2580}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002581
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002583cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584{
2585#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2586 if (cmdline_star > 0) /* showing '*', always 1 position */
2587 return 1;
2588#endif
2589 return ptr2cells(ccline.cmdbuff + idx);
2590}
2591
2592/*
2593 * Compute the offset of the cursor on the command line for the prompt and
2594 * indent.
2595 */
2596 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002597set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002599 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 ccline.cmdspos = 1 + ccline.cmdindent;
2601 else
2602 ccline.cmdspos = 0 + ccline.cmdindent;
2603}
2604
2605/*
2606 * Compute the screen position for the cursor on the command line.
2607 */
2608 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002609set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610{
2611 int i, m, c;
2612
2613 set_cmdspos();
2614 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002615 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002617 if (m < 0) /* overflow, Columns or Rows at weird value */
2618 m = MAXCOL;
2619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 else
2621 m = MAXCOL;
2622 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2623 {
2624 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2626 if (has_mbyte)
2627 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002628 /* If the cmdline doesn't fit, show cursor on last visible char.
2629 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 if ((ccline.cmdspos += c) >= m)
2631 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 ccline.cmdspos -= c;
2633 break;
2634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002636 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 }
2638}
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640/*
2641 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2642 * character that doesn't fit, so that a ">" must be displayed.
2643 */
2644 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002645correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002647 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2649 && ccline.cmdspos % Columns + cells > Columns)
2650 ccline.cmdspos++;
2651}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653/*
2654 * Get an Ex command line for the ":" command.
2655 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002657getexline(
2658 int c, /* normally ':', NUL for ":append" */
2659 void *cookie UNUSED,
2660 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
2662 /* When executing a register, remove ':' that's in front of each line. */
2663 if (exec_from_reg && vpeekc() == ':')
2664 (void)vgetc();
2665 return getcmdline(c, 1L, indent);
2666}
2667
2668/*
2669 * Get an Ex command line for Ex mode.
2670 * In Ex mode we only use the OS supplied line editing features and no
2671 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002675getexmodeline(
2676 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002678 void *cookie UNUSED,
2679 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 garray_T line_ga;
2682 char_u *pend;
2683 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002684 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002685 int escaped = FALSE; /* CTRL-V typed */
2686 int vcol = 0;
2687 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002688 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002689 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 /* Switch cursor on now. This avoids that it happens after the "\n", which
2692 * confuses the system function that computes tabstops. */
2693 cursor_on();
2694
2695 /* always start in column 0; write a newline if necessary */
2696 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002699 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002701 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 if (p_prompt)
2703 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 while (indent-- > 0)
2705 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 }
2708
2709 ga_init2(&line_ga, 1, 30);
2710
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002711 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002712 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002713 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002714 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002715 while (indent >= 8)
2716 {
2717 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002718 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002719 indent -= 8;
2720 }
2721 while (indent-- > 0)
2722 {
2723 ga_append(&line_ga, ' ');
2724 msg_putchar(' ');
2725 }
2726 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002727 ++no_mapping;
2728 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002729
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 /*
2731 * Get the line, one character at a time.
2732 */
2733 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002734 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002736 long sw;
2737 char_u *s;
2738
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 if (ga_grow(&line_ga, 40) == FAIL)
2740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
Bram Moolenaarba748c82017-02-26 14:00:07 +01002742 /*
2743 * Get one character at a time.
2744 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002745 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002746
2747 /* Check for a ":normal" command and no more characters left. */
2748 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2749 c1 = '\n';
2750 else
2751 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752
2753 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 * Handle line editing.
2755 * Previously this was left to the system, putting the terminal in
2756 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002760 msg_putchar('\n');
2761 break;
2762 }
2763
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002764 if (c1 == K_PS)
2765 {
2766 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2767 goto redraw;
2768 }
2769
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002770 if (!escaped)
2771 {
2772 /* CR typed means "enter", which is NL */
2773 if (c1 == '\r')
2774 c1 = '\n';
2775
2776 if (c1 == BS || c1 == K_BS
2777 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002779 if (line_ga.ga_len > 0)
2780 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002781 if (has_mbyte)
2782 {
2783 p = (char_u *)line_ga.ga_data;
2784 p[line_ga.ga_len] = NUL;
2785 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2786 line_ga.ga_len -= len;
2787 }
2788 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002789 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 goto redraw;
2791 }
2792 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 }
2794
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002796 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002797 msg_col = startcol;
2798 msg_clr_eos();
2799 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002800 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801 }
2802
2803 if (c1 == Ctrl_T)
2804 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002805 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806 p = (char_u *)line_ga.ga_data;
2807 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002808 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002809 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002811 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002813 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002814 p = (char_u *)line_ga.ga_data;
2815 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2817 *s = ' ';
2818 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002820redraw:
2821 /* redraw the line */
2822 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002823 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002824 p = (char_u *)line_ga.ga_data;
2825 p[line_ga.ga_len] = NUL;
2826 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002832 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002833 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002837 len = MB_PTR2LEN(p);
2838 msg_outtrans_len(p, len);
2839 vcol += ptr2cells(p);
2840 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
2842 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002844 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 continue;
2846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 if (c1 == Ctrl_D)
2849 {
2850 /* Delete one shiftwidth. */
2851 p = (char_u *)line_ga.ga_data;
2852 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 if (prev_char == '^')
2855 ex_keep_indent = TRUE;
2856 indent = 0;
2857 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
2859 else
2860 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002862 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002863 if (indent > 0)
2864 {
2865 --indent;
2866 indent -= indent % get_sw_value(curbuf);
2867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002869 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002871 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002872 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2873 --line_ga.ga_len;
2874 }
2875 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877
2878 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2879 {
2880 escaped = TRUE;
2881 continue;
2882 }
2883
2884 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2885 if (IS_SPECIAL(c1))
2886 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002888
2889 if (IS_SPECIAL(c1))
2890 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002891 if (has_mbyte)
2892 len = (*mb_char2bytes)(c1,
2893 (char_u *)line_ga.ga_data + line_ga.ga_len);
2894 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002895 {
2896 len = 1;
2897 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2898 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002899 if (c1 == '\n')
2900 msg_putchar('\n');
2901 else if (c1 == TAB)
2902 {
2903 /* Don't use chartabsize(), 'ts' can be different */
2904 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002906 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002907 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002910 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002911 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002912 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002914 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915 escaped = FALSE;
2916
2917 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002918 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002919
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002920 /* We are done when a NL is entered, but not when it comes after an
2921 * odd number of backslashes, that results in a NUL. */
2922 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002924 int bcount = 0;
2925
2926 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2927 ++bcount;
2928
2929 if (bcount > 0)
2930 {
2931 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2932 * "\NL", etc. */
2933 line_ga.ga_len -= (bcount + 1) / 2;
2934 pend -= (bcount + 1) / 2;
2935 pend[-1] = '\n';
2936 }
2937
2938 if ((bcount & 1) == 0)
2939 {
2940 --line_ga.ga_len;
2941 --pend;
2942 *pend = NUL;
2943 break;
2944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 }
2946 }
2947
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002948 --no_mapping;
2949 --allow_keys;
2950
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 /* make following messages go to the next line */
2952 msg_didout = FALSE;
2953 msg_col = 0;
2954 if (msg_row < Rows - 1)
2955 ++msg_row;
2956 emsg_on_display = FALSE; /* don't want ui_delay() */
2957
2958 if (got_int)
2959 ga_clear(&line_ga);
2960
2961 return (char_u *)line_ga.ga_data;
2962}
2963
Bram Moolenaare344bea2005-09-01 20:46:49 +00002964# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2965 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966/*
2967 * Return TRUE if ccline.overstrike is on.
2968 */
2969 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002970cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971{
2972 return ccline.overstrike;
2973}
2974
2975/*
2976 * Return TRUE if the cursor is at the end of the cmdline.
2977 */
2978 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002979cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980{
2981 return (ccline.cmdpos >= ccline.cmdlen);
2982}
2983#endif
2984
Bram Moolenaar9372a112005-12-06 19:59:18 +00002985#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986/*
2987 * Return the virtual column number at the current cursor position.
2988 * This is used by the IM code to obtain the start of the preedit string.
2989 */
2990 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002991cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992{
2993 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2994 return MAXCOL;
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 if (has_mbyte)
2997 {
2998 colnr_T col;
2999 int i = 0;
3000
3001 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003002 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 return col;
3005 }
3006 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 return ccline.cmdpos;
3008}
3009#endif
3010
3011#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3012/*
3013 * If part of the command line is an IM preedit string, redraw it with
3014 * IM feedback attributes. The cursor position is restored after drawing.
3015 */
3016 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003017redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018{
3019 if ((State & CMDLINE)
3020 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003021 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 && !p_imdisable
3023 && im_is_preediting())
3024 {
3025 int cmdpos = 0;
3026 int cmdspos;
3027 int old_row;
3028 int old_col;
3029 colnr_T col;
3030
3031 old_row = msg_row;
3032 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003033 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 if (has_mbyte)
3036 {
3037 for (col = 0; col < preedit_start_col
3038 && cmdpos < ccline.cmdlen; ++col)
3039 {
3040 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003041 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 }
3043 }
3044 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 {
3046 cmdspos += preedit_start_col;
3047 cmdpos += preedit_start_col;
3048 }
3049
3050 msg_row = cmdline_row + (cmdspos / (int)Columns);
3051 msg_col = cmdspos % (int)Columns;
3052 if (msg_row >= Rows)
3053 msg_row = Rows - 1;
3054
3055 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3056 {
3057 int char_len;
3058 int char_attr;
3059
3060 char_attr = im_get_feedback_attr(col);
3061 if (char_attr < 0)
3062 break; /* end of preedit string */
3063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003065 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 char_len = 1;
3068
3069 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3070 cmdpos += char_len;
3071 }
3072
3073 msg_row = old_row;
3074 msg_col = old_col;
3075 }
3076}
3077#endif /* FEAT_XIM && FEAT_GUI_GTK */
3078
3079/*
3080 * Allocate a new command line buffer.
3081 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 */
3083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003084alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 /*
3087 * give some extra space to avoid having to allocate all the time
3088 */
3089 if (len < 80)
3090 len = 100;
3091 else
3092 len += 20;
3093
3094 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3095 ccline.cmdbufflen = len;
3096}
3097
3098/*
3099 * Re-allocate the command line to length len + something extra.
3100 * return FAIL for failure, OK otherwise
3101 */
3102 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003103realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104{
3105 char_u *p;
3106
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003107 if (len < ccline.cmdbufflen)
3108 return OK; /* no need to resize */
3109
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 p = ccline.cmdbuff;
3111 alloc_cmdbuff(len); /* will get some more */
3112 if (ccline.cmdbuff == NULL) /* out of memory */
3113 {
3114 ccline.cmdbuff = p; /* keep the old one */
3115 return FAIL;
3116 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003117 /* There isn't always a NUL after the command, but it may need to be
3118 * there, thus copy up to the NUL and add a NUL. */
3119 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3120 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003122
3123 if (ccline.xpc != NULL
3124 && ccline.xpc->xp_pattern != NULL
3125 && ccline.xpc->xp_context != EXPAND_NOTHING
3126 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3127 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003128 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003129
3130 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3131 * to point into the newly allocated memory. */
3132 if (i >= 0 && i <= ccline.cmdlen)
3133 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3134 }
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 return OK;
3137}
3138
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003139#if defined(FEAT_ARABIC) || defined(PROTO)
3140static char_u *arshape_buf = NULL;
3141
3142# if defined(EXITFREE) || defined(PROTO)
3143 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003144free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003145{
3146 vim_free(arshape_buf);
3147}
3148# endif
3149#endif
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151/*
3152 * Draw part of the cmdline at the current cursor position. But draw stars
3153 * when cmdline_star is TRUE.
3154 */
3155 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003156draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157{
3158#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3159 int i;
3160
3161 if (cmdline_star > 0)
3162 for (i = 0; i < len; ++i)
3163 {
3164 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003166 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 }
3168 else
3169#endif
3170#ifdef FEAT_ARABIC
3171 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3172 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 static int buflen = 0;
3174 char_u *p;
3175 int j;
3176 int newlen = 0;
3177 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003178 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 int prev_c = 0;
3180 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003181 int u8c;
3182 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184
3185 /*
3186 * Do arabic shaping into a temporary buffer. This is very
3187 * inefficient!
3188 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003189 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
3191 /* Re-allocate the buffer. We keep it around to avoid a lot of
3192 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003193 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003194 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003195 arshape_buf = alloc(buflen);
3196 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 return; /* out of memory */
3198 }
3199
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003200 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3201 {
3202 /* Prepend a space to draw the leading composing char on. */
3203 arshape_buf[0] = ' ';
3204 newlen = 1;
3205 }
3206
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 for (j = start; j < start + len; j += mb_l)
3208 {
3209 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003210 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003211 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 if (ARABIC_CHAR(u8c))
3213 {
3214 /* Do Arabic shaping. */
3215 if (cmdmsg_rl)
3216 {
3217 /* displaying from right to left */
3218 pc = prev_c;
3219 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003220 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 if (j + mb_l >= start + len)
3222 nc = NUL;
3223 else
3224 nc = utf_ptr2char(p + mb_l);
3225 }
3226 else
3227 {
3228 /* displaying from left to right */
3229 if (j + mb_l >= start + len)
3230 pc = NUL;
3231 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003232 {
3233 int pcc[MAX_MCO];
3234
3235 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003237 pc1 = pcc[0];
3238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 nc = prev_c;
3240 }
3241 prev_c = u8c;
3242
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003243 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003246 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003248 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3249 if (u8cc[1] != 0)
3250 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003251 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 }
3253 }
3254 else
3255 {
3256 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 newlen += mb_l;
3259 }
3260 }
3261
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 }
3264 else
3265#endif
3266 msg_outtrans_len(ccline.cmdbuff + start, len);
3267}
3268
3269/*
3270 * Put a character on the command line. Shifts the following text to the
3271 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3272 * "c" must be printable (fit in one display cell)!
3273 */
3274 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003275putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276{
3277 if (cmd_silent)
3278 return;
3279 msg_no_more = TRUE;
3280 msg_putchar(c);
3281 if (shift)
3282 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3283 msg_no_more = FALSE;
3284 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003285 extra_char = c;
3286 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287}
3288
3289/*
3290 * Undo a putcmdline(c, FALSE).
3291 */
3292 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003293unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 if (cmd_silent)
3296 return;
3297 msg_no_more = TRUE;
3298 if (ccline.cmdlen == ccline.cmdpos)
3299 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003300 else if (has_mbyte)
3301 draw_cmdline(ccline.cmdpos,
3302 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 else
3304 draw_cmdline(ccline.cmdpos, 1);
3305 msg_no_more = FALSE;
3306 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003307 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308}
3309
3310/*
3311 * Put the given string, of the given length, onto the command line.
3312 * If len is -1, then STRLEN() is used to calculate the length.
3313 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3314 * part will be redrawn, otherwise it will not. If this function is called
3315 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3316 * called afterwards.
3317 */
3318 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003319put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320{
3321 int retval;
3322 int i;
3323 int m;
3324 int c;
3325
3326 if (len < 0)
3327 len = (int)STRLEN(str);
3328
3329 /* Check if ccline.cmdbuff needs to be longer */
3330 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003331 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 else
3333 retval = OK;
3334 if (retval == OK)
3335 {
3336 if (!ccline.overstrike)
3337 {
3338 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3339 ccline.cmdbuff + ccline.cmdpos,
3340 (size_t)(ccline.cmdlen - ccline.cmdpos));
3341 ccline.cmdlen += len;
3342 }
3343 else
3344 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (has_mbyte)
3346 {
3347 /* Count nr of characters in the new string. */
3348 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003349 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 ++m;
3351 /* Count nr of bytes in cmdline that are overwritten by these
3352 * characters. */
3353 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003354 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 --m;
3356 if (i < ccline.cmdlen)
3357 {
3358 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3359 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3360 ccline.cmdlen += ccline.cmdpos + len - i;
3361 }
3362 else
3363 ccline.cmdlen = ccline.cmdpos + len;
3364 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003365 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 ccline.cmdlen = ccline.cmdpos + len;
3367 }
3368 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3369 ccline.cmdbuff[ccline.cmdlen] = NUL;
3370
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 if (enc_utf8)
3372 {
3373 /* When the inserted text starts with a composing character,
3374 * backup to the character before it. There could be two of them.
3375 */
3376 i = 0;
3377 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3378 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3379 {
3380 i = (*mb_head_off)(ccline.cmdbuff,
3381 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3382 ccline.cmdpos -= i;
3383 len += i;
3384 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3385 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003386#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3388 {
3389 /* Check the previous character for Arabic combining pair. */
3390 i = (*mb_head_off)(ccline.cmdbuff,
3391 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3392 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3393 + ccline.cmdpos - i), c))
3394 {
3395 ccline.cmdpos -= i;
3396 len += i;
3397 }
3398 else
3399 i = 0;
3400 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (i != 0)
3403 {
3404 /* Also backup the cursor position. */
3405 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3406 ccline.cmdspos -= i;
3407 msg_col -= i;
3408 if (msg_col < 0)
3409 {
3410 msg_col += Columns;
3411 --msg_row;
3412 }
3413 }
3414 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415
3416 if (redraw && !cmd_silent)
3417 {
3418 msg_no_more = TRUE;
3419 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003420 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3422 /* Avoid clearing the rest of the line too often. */
3423 if (cmdline_row != i || ccline.overstrike)
3424 msg_clr_eos();
3425 msg_no_more = FALSE;
3426 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003427 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003429 m = Columns * Rows;
3430 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003432 }
3433 else
3434 m = MAXCOL;
3435 for (i = 0; i < len; ++i)
3436 {
3437 c = cmdline_charsize(ccline.cmdpos);
3438 /* count ">" for a double-wide char that doesn't fit. */
3439 if (has_mbyte)
3440 correct_cmdspos(ccline.cmdpos, c);
3441 /* Stop cursor at the end of the screen, but do increment the
3442 * insert position, so that entering a very long command
3443 * works, even though you can't see it. */
3444 if (ccline.cmdspos + c < m)
3445 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003446
Bram Moolenaar14184a32019-02-16 15:10:30 +01003447 if (has_mbyte)
3448 {
3449 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3450 if (c > len - i - 1)
3451 c = len - i - 1;
3452 ccline.cmdpos += c;
3453 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003455 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 }
3457 }
3458 if (redraw)
3459 msg_check();
3460 return retval;
3461}
3462
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003463static struct cmdline_info prev_ccline;
3464static int prev_ccline_used = FALSE;
3465
3466/*
3467 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3468 * and overwrite it. But get_cmdline_str() may need it, thus make it
3469 * available globally in prev_ccline.
3470 */
3471 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003472save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003473{
3474 if (!prev_ccline_used)
3475 {
3476 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3477 prev_ccline_used = TRUE;
3478 }
3479 *ccp = prev_ccline;
3480 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003481 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003482}
3483
3484/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003485 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003486 */
3487 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003488restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003489{
3490 ccline = prev_ccline;
3491 prev_ccline = *ccp;
3492}
3493
Bram Moolenaar8299df92004-07-10 09:47:34 +00003494/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003495 * Paste a yank register into the command line.
3496 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497 * insert_reg() can't be used here, because special characters from the
3498 * register contents will be interpreted as commands.
3499 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003500 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003501 */
3502 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003503cmdline_paste(
3504 int regname,
3505 int literally, /* Insert text literally instead of "as typed" */
3506 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003507{
3508 long i;
3509 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003510 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003511 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003512
3513 /* check for valid regname; also accept special characters for CTRL-R in
3514 * the command line */
3515 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003516 && regname != Ctrl_A && regname != Ctrl_L
3517 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003518 return FAIL;
3519
3520 /* A register containing CTRL-R can cause an endless loop. Allow using
3521 * CTRL-C to break the loop. */
3522 line_breakcheck();
3523 if (got_int)
3524 return FAIL;
3525
3526#ifdef FEAT_CLIPBOARD
3527 regname = may_get_selection(regname);
3528#endif
3529
Bram Moolenaar438d1762018-09-30 17:11:48 +02003530 // Need to set "textlock" to avoid nasty things like going to another
3531 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003532 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003533 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003534 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535
3536 if (i)
3537 {
3538 /* Got the value of a special register in "arg". */
3539 if (arg == NULL)
3540 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003541
3542 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3543 * part of the word. */
3544 p = arg;
3545 if (p_is && regname == Ctrl_W)
3546 {
3547 char_u *w;
3548 int len;
3549
3550 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003551 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003552 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003553 if (has_mbyte)
3554 {
3555 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3556 if (!vim_iswordc(mb_ptr2char(w - len)))
3557 break;
3558 w -= len;
3559 }
3560 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003561 {
3562 if (!vim_iswordc(w[-1]))
3563 break;
3564 --w;
3565 }
3566 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003567 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003568 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3569 p += len;
3570 }
3571
3572 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003573 if (allocated)
3574 vim_free(arg);
3575 return OK;
3576 }
3577
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003578 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003579}
3580
3581/*
3582 * Put a string on the command line.
3583 * When "literally" is TRUE, insert literally.
3584 * When "literally" is FALSE, insert as typed, but don't leave the command
3585 * line.
3586 */
3587 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003588cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003589{
3590 int c, cv;
3591
3592 if (literally)
3593 put_on_cmdline(s, -1, TRUE);
3594 else
3595 while (*s != NUL)
3596 {
3597 cv = *s;
3598 if (cv == Ctrl_V && s[1])
3599 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003600 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003601 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003602 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003603 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003604 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3605 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003606#ifdef UNIX
3607 || c == intr_char
3608#endif
3609 || (c == Ctrl_BSL && *s == Ctrl_N))
3610 stuffcharReadbuff(Ctrl_V);
3611 stuffcharReadbuff(c);
3612 }
3613}
3614
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615#ifdef FEAT_WILDMENU
3616/*
3617 * Delete characters on the command line, from "from" to the current
3618 * position.
3619 */
3620 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003621cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622{
3623 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3624 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3625 ccline.cmdlen -= ccline.cmdpos - from;
3626 ccline.cmdpos = from;
3627}
3628#endif
3629
3630/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003631 * This function is called when the screen size changes and with incremental
3632 * search and in other situations where the command line may have been
3633 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 */
3635 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003636redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003638 redrawcmdline_ex(TRUE);
3639}
3640
3641 void
3642redrawcmdline_ex(int do_compute_cmdrow)
3643{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 if (cmd_silent)
3645 return;
3646 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003647 if (do_compute_cmdrow)
3648 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 redrawcmd();
3650 cursorcmd();
3651}
3652
3653 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003654redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
3656 int i;
3657
3658 if (cmd_silent)
3659 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003660 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 msg_putchar(ccline.cmdfirstc);
3662 if (ccline.cmdprompt != NULL)
3663 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003664 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3666 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003667 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 --ccline.cmdindent;
3669 }
3670 else
3671 for (i = ccline.cmdindent; i > 0; --i)
3672 msg_putchar(' ');
3673}
3674
3675/*
3676 * Redraw what is currently on the command line.
3677 */
3678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003679redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 if (cmd_silent)
3682 return;
3683
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003684 /* when 'incsearch' is set there may be no command line while redrawing */
3685 if (ccline.cmdbuff == NULL)
3686 {
3687 windgoto(cmdline_row, 0);
3688 msg_clr_eos();
3689 return;
3690 }
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 msg_start();
3693 redrawcmdprompt();
3694
3695 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3696 msg_no_more = TRUE;
3697 draw_cmdline(0, ccline.cmdlen);
3698 msg_clr_eos();
3699 msg_no_more = FALSE;
3700
3701 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003702 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003703 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 /*
3706 * An emsg() before may have set msg_scroll. This is used in normal mode,
3707 * in cmdline mode we can reset them now.
3708 */
3709 msg_scroll = FALSE; /* next message overwrites cmdline */
3710
3711 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3712 * in cmdline mode */
3713 skip_redraw = FALSE;
3714}
3715
3716 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003717compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003719 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 cmdline_row = Rows - 1;
3721 else
3722 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003723 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724}
3725
3726 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003727cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728{
3729 if (cmd_silent)
3730 return;
3731
3732#ifdef FEAT_RIGHTLEFT
3733 if (cmdmsg_rl)
3734 {
3735 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3736 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3737 if (msg_row <= 0)
3738 msg_row = Rows - 1;
3739 }
3740 else
3741#endif
3742 {
3743 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3744 msg_col = ccline.cmdspos % (int)Columns;
3745 if (msg_row >= Rows)
3746 msg_row = Rows - 1;
3747 }
3748
3749 windgoto(msg_row, msg_col);
3750#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003751 if (p_imst == IM_ON_THE_SPOT)
3752 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#endif
3754#ifdef MCH_CURSOR_SHAPE
3755 mch_update_cursor();
3756#endif
3757}
3758
3759 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003760gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761{
3762 msg_start();
3763#ifdef FEAT_RIGHTLEFT
3764 if (cmdmsg_rl)
3765 msg_col = Columns - 1;
3766 else
3767#endif
3768 msg_col = 0; /* always start in column 0 */
3769 if (clr) /* clear the bottom line(s) */
3770 msg_clr_eos(); /* will reset clear_cmdline */
3771 windgoto(cmdline_row, 0);
3772}
3773
3774/*
3775 * Check the word in front of the cursor for an abbreviation.
3776 * Called when the non-id character "c" has been entered.
3777 * When an abbreviation is recognized it is removed from the text with
3778 * backspaces and the replacement string is inserted, followed by "c".
3779 */
3780 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003781ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003783 int spos = 0;
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3786 return FALSE;
3787
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003788 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3789 * Actually accepts any mark. */
3790 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3791 spos++;
3792 if (ccline.cmdlen - spos > 5
3793 && ccline.cmdbuff[spos] == '\''
3794 && ccline.cmdbuff[spos + 2] == ','
3795 && ccline.cmdbuff[spos + 3] == '\'')
3796 spos += 5;
3797 else
3798 /* check abbreviation from the beginning of the commandline */
3799 spos = 0;
3800
3801 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802}
3803
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003804#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3805 static int
3806#ifdef __BORLANDC__
3807_RTLENTRYF
3808#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003809sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003810{
3811 char_u *p1 = *(char_u **)s1;
3812 char_u *p2 = *(char_u **)s2;
3813
3814 if (*p1 != '<' && *p2 == '<') return -1;
3815 if (*p1 == '<' && *p2 != '<') return 1;
3816 return STRCMP(p1, p2);
3817}
3818#endif
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820/*
3821 * Return FAIL if this is not an appropriate context in which to do
3822 * completion of anything, return OK if it is (even if there are no matches).
3823 * For the caller, this means that the character is just passed through like a
3824 * normal character (instead of being expanded). This allows :s/^I^D etc.
3825 */
3826 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003827nextwild(
3828 expand_T *xp,
3829 int type,
3830 int options, /* extra options for ExpandOne() */
3831 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832{
3833 int i, j;
3834 char_u *p1;
3835 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 int difflen;
3837 int v;
3838
3839 if (xp->xp_numfiles == -1)
3840 {
3841 set_expand_context(xp);
3842 cmd_showtail = expand_showtail(xp);
3843 }
3844
3845 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3846 {
3847 beep_flush();
3848 return OK; /* Something illegal on command line */
3849 }
3850 if (xp->xp_context == EXPAND_NOTHING)
3851 {
3852 /* Caller can use the character as a normal char instead */
3853 return FAIL;
3854 }
3855
Bram Moolenaar32526b32019-01-19 17:43:09 +01003856 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 out_flush();
3858
3859 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003860 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861
3862 if (type == WILD_NEXT || type == WILD_PREV)
3863 {
3864 /*
3865 * Get next/previous match for a previous expanded pattern.
3866 */
3867 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3868 }
3869 else
3870 {
3871 /*
3872 * Translate string into pattern and expand it.
3873 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003874 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3875 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 p2 = NULL;
3877 else
3878 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003879 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003880 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3881 if (escape)
3882 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003883
3884 if (p_wic)
3885 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003886 p2 = ExpandOne(xp, p1,
3887 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003888 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003890 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 if (p2 != NULL && type == WILD_LONGEST)
3892 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003893 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 if (ccline.cmdbuff[i + j] == '*'
3895 || ccline.cmdbuff[i + j] == '?')
3896 break;
3897 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003898 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 }
3900 }
3901 }
3902
3903 if (p2 != NULL && !got_int)
3904 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003905 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003906 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003908 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 xp->xp_pattern = ccline.cmdbuff + i;
3910 }
3911 else
3912 v = OK;
3913 if (v == OK)
3914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003915 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3916 &ccline.cmdbuff[ccline.cmdpos],
3917 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3918 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 ccline.cmdlen += difflen;
3920 ccline.cmdpos += difflen;
3921 }
3922 }
3923 vim_free(p2);
3924
3925 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003926 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927
3928 /* When expanding a ":map" command and no matches are found, assume that
3929 * the key is supposed to be inserted literally */
3930 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3931 return FAIL;
3932
3933 if (xp->xp_numfiles <= 0 && p2 == NULL)
3934 beep_flush();
3935 else if (xp->xp_numfiles == 1)
3936 /* free expanded pattern */
3937 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3938
3939 return OK;
3940}
3941
3942/*
3943 * Do wildcard expansion on the string 'str'.
3944 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003945 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 * Return NULL for failure.
3947 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003948 * "orig" is the originally expanded string, copied to allocated memory. It
3949 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3950 * WILD_PREV "orig" should be NULL.
3951 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003952 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3953 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 *
3955 * mode = WILD_FREE: just free previously expanded matches
3956 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3957 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3958 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3959 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3960 * mode = WILD_ALL: return all matches concatenated
3961 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003962 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 *
3964 * options = WILD_LIST_NOTFOUND: list entries without a match
3965 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3966 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3967 * options = WILD_NO_BEEP: Don't beep for multiple matches
3968 * options = WILD_ADD_SLASH: add a slash after directory names
3969 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3970 * options = WILD_SILENT: don't print warning messages
3971 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003972 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 *
3974 * The variables xp->xp_context and xp->xp_backslash must have been set!
3975 */
3976 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003977ExpandOne(
3978 expand_T *xp,
3979 char_u *str,
3980 char_u *orig, /* allocated copy of original of expanded string */
3981 int options,
3982 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
3984 char_u *ss = NULL;
3985 static int findex;
3986 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003987 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 int i;
3989 long_u len;
3990 int non_suf_match; /* number without matching suffix */
3991
3992 /*
3993 * first handle the case of using an old match
3994 */
3995 if (mode == WILD_NEXT || mode == WILD_PREV)
3996 {
3997 if (xp->xp_numfiles > 0)
3998 {
3999 if (mode == WILD_PREV)
4000 {
4001 if (findex == -1)
4002 findex = xp->xp_numfiles;
4003 --findex;
4004 }
4005 else /* mode == WILD_NEXT */
4006 ++findex;
4007
4008 /*
4009 * When wrapping around, return the original string, set findex to
4010 * -1.
4011 */
4012 if (findex < 0)
4013 {
4014 if (orig_save == NULL)
4015 findex = xp->xp_numfiles - 1;
4016 else
4017 findex = -1;
4018 }
4019 if (findex >= xp->xp_numfiles)
4020 {
4021 if (orig_save == NULL)
4022 findex = 0;
4023 else
4024 findex = -1;
4025 }
4026#ifdef FEAT_WILDMENU
4027 if (p_wmnu)
4028 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4029 findex, cmd_showtail);
4030#endif
4031 if (findex == -1)
4032 return vim_strsave(orig_save);
4033 return vim_strsave(xp->xp_files[findex]);
4034 }
4035 else
4036 return NULL;
4037 }
4038
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004039 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4041 {
4042 FreeWild(xp->xp_numfiles, xp->xp_files);
4043 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004044 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 findex = 0;
4047
4048 if (mode == WILD_FREE) /* only release file name */
4049 return NULL;
4050
4051 if (xp->xp_numfiles == -1)
4052 {
4053 vim_free(orig_save);
4054 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004055 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
4057 /*
4058 * Do the expansion.
4059 */
4060 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4061 options) == FAIL)
4062 {
4063#ifdef FNAME_ILLEGAL
4064 /* Illegal file name has been silently skipped. But when there
4065 * are wildcards, the real problem is that there was no match,
4066 * causing the pattern to be added, which has illegal characters.
4067 */
4068 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004069 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070#endif
4071 }
4072 else if (xp->xp_numfiles == 0)
4073 {
4074 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004075 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 else
4078 {
4079 /* Escape the matches for use on the command line. */
4080 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4081
4082 /*
4083 * Check for matching suffixes in file names.
4084 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004085 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4086 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
4088 if (xp->xp_numfiles)
4089 non_suf_match = xp->xp_numfiles;
4090 else
4091 non_suf_match = 1;
4092 if ((xp->xp_context == EXPAND_FILES
4093 || xp->xp_context == EXPAND_DIRECTORIES)
4094 && xp->xp_numfiles > 1)
4095 {
4096 /*
4097 * More than one match; check suffix.
4098 * The files will have been sorted on matching suffix in
4099 * expand_wildcards, only need to check the first two.
4100 */
4101 non_suf_match = 0;
4102 for (i = 0; i < 2; ++i)
4103 if (match_suffix(xp->xp_files[i]))
4104 ++non_suf_match;
4105 }
4106 if (non_suf_match != 1)
4107 {
4108 /* Can we ever get here unless it's while expanding
4109 * interactively? If not, we can get rid of this all
4110 * together. Don't really want to wait for this message
4111 * (and possibly have to hit return to continue!).
4112 */
4113 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004114 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 else if (!(options & WILD_NO_BEEP))
4116 beep_flush();
4117 }
4118 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4119 ss = vim_strsave(xp->xp_files[0]);
4120 }
4121 }
4122 }
4123
4124 /* Find longest common part */
4125 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4126 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004127 int mb_len = 1;
4128 int c0, ci;
4129
4130 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004132 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004134 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4135 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4136 }
4137 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004138 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004139 for (i = 1; i < xp->xp_numfiles; ++i)
4140 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004141 if (has_mbyte)
4142 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4143 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004144 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004145 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004147 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004148 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004150 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 break;
4152 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004153 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 break;
4155 }
4156 if (i < xp->xp_numfiles)
4157 {
4158 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004159 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 break;
4161 }
4162 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004163
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 ss = alloc((unsigned)len + 1);
4165 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004166 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 findex = -1; /* next p_wc gets first one */
4168 }
4169
4170 /* Concatenate all matching names */
4171 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4172 {
4173 len = 0;
4174 for (i = 0; i < xp->xp_numfiles; ++i)
4175 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4176 ss = lalloc(len, TRUE);
4177 if (ss != NULL)
4178 {
4179 *ss = NUL;
4180 for (i = 0; i < xp->xp_numfiles; ++i)
4181 {
4182 STRCAT(ss, xp->xp_files[i]);
4183 if (i != xp->xp_numfiles - 1)
4184 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4185 }
4186 }
4187 }
4188
4189 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4190 ExpandCleanup(xp);
4191
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004192 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004193 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004194 vim_free(orig);
4195
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 return ss;
4197}
4198
4199/*
4200 * Prepare an expand structure for use.
4201 */
4202 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004203ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004205 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004206 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004208#ifndef BACKSLASH_IN_FILENAME
4209 xp->xp_shell = FALSE;
4210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 xp->xp_numfiles = -1;
4212 xp->xp_files = NULL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004213#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004214 xp->xp_arg = NULL;
4215#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004216 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217}
4218
4219/*
4220 * Cleanup an expand structure after use.
4221 */
4222 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004223ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224{
4225 if (xp->xp_numfiles >= 0)
4226 {
4227 FreeWild(xp->xp_numfiles, xp->xp_files);
4228 xp->xp_numfiles = -1;
4229 }
4230}
4231
4232 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004233ExpandEscape(
4234 expand_T *xp,
4235 char_u *str,
4236 int numfiles,
4237 char_u **files,
4238 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
4240 int i;
4241 char_u *p;
4242
4243 /*
4244 * May change home directory back to "~"
4245 */
4246 if (options & WILD_HOME_REPLACE)
4247 tilde_replace(str, numfiles, files);
4248
4249 if (options & WILD_ESCAPE)
4250 {
4251 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004252 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004253 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 || xp->xp_context == EXPAND_BUFFERS
4255 || xp->xp_context == EXPAND_DIRECTORIES)
4256 {
4257 /*
4258 * Insert a backslash into a file name before a space, \, %, #
4259 * and wildmatch characters, except '~'.
4260 */
4261 for (i = 0; i < numfiles; ++i)
4262 {
4263 /* for ":set path=" we need to escape spaces twice */
4264 if (xp->xp_backslash == XP_BS_THREE)
4265 {
4266 p = vim_strsave_escaped(files[i], (char_u *)" ");
4267 if (p != NULL)
4268 {
4269 vim_free(files[i]);
4270 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004271#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 p = vim_strsave_escaped(files[i], (char_u *)" ");
4273 if (p != NULL)
4274 {
4275 vim_free(files[i]);
4276 files[i] = p;
4277 }
4278#endif
4279 }
4280 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004281#ifdef BACKSLASH_IN_FILENAME
4282 p = vim_strsave_fnameescape(files[i], FALSE);
4283#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004284 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004285#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 if (p != NULL)
4287 {
4288 vim_free(files[i]);
4289 files[i] = p;
4290 }
4291
4292 /* If 'str' starts with "\~", replace "~" at start of
4293 * files[i] with "\~". */
4294 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004295 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004298
4299 /* If the first file starts with a '+' escape it. Otherwise it
4300 * could be seen as "+cmd". */
4301 if (*files[0] == '+')
4302 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 else if (xp->xp_context == EXPAND_TAGS)
4305 {
4306 /*
4307 * Insert a backslash before characters in a tag name that
4308 * would terminate the ":tag" command.
4309 */
4310 for (i = 0; i < numfiles; ++i)
4311 {
4312 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4313 if (p != NULL)
4314 {
4315 vim_free(files[i]);
4316 files[i] = p;
4317 }
4318 }
4319 }
4320 }
4321}
4322
4323/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004324 * Escape special characters in "fname" for when used as a file name argument
4325 * after a Vim command, or, when "shell" is non-zero, a shell command.
4326 * Returns the result in allocated memory.
4327 */
4328 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004329vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004330{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004331 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004332#ifdef BACKSLASH_IN_FILENAME
4333 char_u buf[20];
4334 int j = 0;
4335
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004336 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004337 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004338 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004339 buf[j++] = *p;
4340 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004341 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004342#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004343 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4344 if (shell && csh_like_shell() && p != NULL)
4345 {
4346 char_u *s;
4347
4348 /* For csh and similar shells need to put two backslashes before '!'.
4349 * One is taken by Vim, one by the shell. */
4350 s = vim_strsave_escaped(p, (char_u *)"!");
4351 vim_free(p);
4352 p = s;
4353 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004354#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004355
4356 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4357 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004358 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004359 escape_fname(&p);
4360
4361 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004362}
4363
4364/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004365 * Put a backslash before the file name in "pp", which is in allocated memory.
4366 */
4367 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004368escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004369{
4370 char_u *p;
4371
4372 p = alloc((unsigned)(STRLEN(*pp) + 2));
4373 if (p != NULL)
4374 {
4375 p[0] = '\\';
4376 STRCPY(p + 1, *pp);
4377 vim_free(*pp);
4378 *pp = p;
4379 }
4380}
4381
4382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 * For each file name in files[num_files]:
4384 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4385 */
4386 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004387tilde_replace(
4388 char_u *orig_pat,
4389 int num_files,
4390 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391{
4392 int i;
4393 char_u *p;
4394
4395 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4396 {
4397 for (i = 0; i < num_files; ++i)
4398 {
4399 p = home_replace_save(NULL, files[i]);
4400 if (p != NULL)
4401 {
4402 vim_free(files[i]);
4403 files[i] = p;
4404 }
4405 }
4406 }
4407}
4408
4409/*
4410 * Show all matches for completion on the command line.
4411 * Returns EXPAND_NOTHING when the character that triggered expansion should
4412 * be inserted like a normal character.
4413 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004415showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416{
4417#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4418 int num_files;
4419 char_u **files_found;
4420 int i, j, k;
4421 int maxlen;
4422 int lines;
4423 int columns;
4424 char_u *p;
4425 int lastlen;
4426 int attr;
4427 int showtail;
4428
4429 if (xp->xp_numfiles == -1)
4430 {
4431 set_expand_context(xp);
4432 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4433 &num_files, &files_found);
4434 showtail = expand_showtail(xp);
4435 if (i != EXPAND_OK)
4436 return i;
4437
4438 }
4439 else
4440 {
4441 num_files = xp->xp_numfiles;
4442 files_found = xp->xp_files;
4443 showtail = cmd_showtail;
4444 }
4445
4446#ifdef FEAT_WILDMENU
4447 if (!wildmenu)
4448 {
4449#endif
4450 msg_didany = FALSE; /* lines_left will be set */
4451 msg_start(); /* prepare for paging */
4452 msg_putchar('\n');
4453 out_flush();
4454 cmdline_row = msg_row;
4455 msg_didany = FALSE; /* lines_left will be set again */
4456 msg_start(); /* prepare for paging */
4457#ifdef FEAT_WILDMENU
4458 }
4459#endif
4460
4461 if (got_int)
4462 got_int = FALSE; /* only int. the completion, not the cmd line */
4463#ifdef FEAT_WILDMENU
4464 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004465 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466#endif
4467 else
4468 {
4469 /* find the length of the longest file name */
4470 maxlen = 0;
4471 for (i = 0; i < num_files; ++i)
4472 {
4473 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004474 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 || xp->xp_context == EXPAND_BUFFERS))
4476 {
4477 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4478 j = vim_strsize(NameBuff);
4479 }
4480 else
4481 j = vim_strsize(L_SHOWFILE(i));
4482 if (j > maxlen)
4483 maxlen = j;
4484 }
4485
4486 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4487 lines = num_files;
4488 else
4489 {
4490 /* compute the number of columns and lines for the listing */
4491 maxlen += 2; /* two spaces between file names */
4492 columns = ((int)Columns + 2) / maxlen;
4493 if (columns < 1)
4494 columns = 1;
4495 lines = (num_files + columns - 1) / columns;
4496 }
4497
Bram Moolenaar8820b482017-03-16 17:23:31 +01004498 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499
4500 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4501 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004502 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 msg_clr_eos();
4504 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004505 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507
4508 /* list the files line by line */
4509 for (i = 0; i < lines; ++i)
4510 {
4511 lastlen = 999;
4512 for (k = i; k < num_files; k += lines)
4513 {
4514 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4515 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004516 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 p = files_found[k] + STRLEN(files_found[k]) + 1;
4518 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004519 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004521 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 break;
4523 }
4524 for (j = maxlen - lastlen; --j >= 0; )
4525 msg_putchar(' ');
4526 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004527 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 || xp->xp_context == EXPAND_BUFFERS)
4529 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004530 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004531 if (xp->xp_numfiles != -1)
4532 {
4533 char_u *halved_slash;
4534 char_u *exp_path;
4535
4536 /* Expansion was done before and special characters
4537 * were escaped, need to halve backslashes. Also
4538 * $HOME has been replaced with ~/. */
4539 exp_path = expand_env_save_opt(files_found[k], TRUE);
4540 halved_slash = backslash_halve_save(
4541 exp_path != NULL ? exp_path : files_found[k]);
4542 j = mch_isdir(halved_slash != NULL ? halved_slash
4543 : files_found[k]);
4544 vim_free(exp_path);
4545 vim_free(halved_slash);
4546 }
4547 else
4548 /* Expansion was done here, file names are literal. */
4549 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 if (showtail)
4551 p = L_SHOWFILE(k);
4552 else
4553 {
4554 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4555 TRUE);
4556 p = NameBuff;
4557 }
4558 }
4559 else
4560 {
4561 j = FALSE;
4562 p = L_SHOWFILE(k);
4563 }
4564 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4565 }
4566 if (msg_col > 0) /* when not wrapped around */
4567 {
4568 msg_clr_eos();
4569 msg_putchar('\n');
4570 }
4571 out_flush(); /* show one line at a time */
4572 if (got_int)
4573 {
4574 got_int = FALSE;
4575 break;
4576 }
4577 }
4578
4579 /*
4580 * we redraw the command below the lines that we have just listed
4581 * This is a bit tricky, but it saves a lot of screen updating.
4582 */
4583 cmdline_row = msg_row; /* will put it back later */
4584 }
4585
4586 if (xp->xp_numfiles == -1)
4587 FreeWild(num_files, files_found);
4588
4589 return EXPAND_OK;
4590}
4591
4592/*
4593 * Private gettail for showmatches() (and win_redr_status_matches()):
4594 * Find tail of file name path, but ignore trailing "/".
4595 */
4596 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004597sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598{
4599 char_u *p;
4600 char_u *t = s;
4601 int had_sep = FALSE;
4602
4603 for (p = s; *p != NUL; )
4604 {
4605 if (vim_ispathsep(*p)
4606#ifdef BACKSLASH_IN_FILENAME
4607 && !rem_backslash(p)
4608#endif
4609 )
4610 had_sep = TRUE;
4611 else if (had_sep)
4612 {
4613 t = p;
4614 had_sep = FALSE;
4615 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004616 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 }
4618 return t;
4619}
4620
4621/*
4622 * Return TRUE if we only need to show the tail of completion matches.
4623 * When not completing file names or there is a wildcard in the path FALSE is
4624 * returned.
4625 */
4626 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004627expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 char_u *s;
4630 char_u *end;
4631
4632 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004633 if (xp->xp_context != EXPAND_FILES
4634 && xp->xp_context != EXPAND_SHELLCMD
4635 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 return FALSE;
4637
4638 end = gettail(xp->xp_pattern);
4639 if (end == xp->xp_pattern) /* there is no path separator */
4640 return FALSE;
4641
4642 for (s = xp->xp_pattern; s < end; s++)
4643 {
4644 /* Skip escaped wildcards. Only when the backslash is not a path
4645 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4646 if (rem_backslash(s))
4647 ++s;
4648 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4649 return FALSE;
4650 }
4651 return TRUE;
4652}
4653
4654/*
4655 * Prepare a string for expansion.
4656 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004657 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 * When expanding other names: The string will be used with regcomp(). Copy
4659 * the name into allocated memory and prepend "^".
4660 */
4661 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004662addstar(
4663 char_u *fname,
4664 int len,
4665 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
4667 char_u *retval;
4668 int i, j;
4669 int new_len;
4670 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004671 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004673 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004674 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004675 && context != EXPAND_SHELLCMD
4676 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
4678 /*
4679 * Matching will be done internally (on something other than files).
4680 * So we convert the file-matching-type wildcards into our kind for
4681 * use with vim_regcomp(). First work out how long it will be:
4682 */
4683
4684 /* For help tags the translation is done in find_help_tags().
4685 * For a tag pattern starting with "/" no translation is needed. */
4686 if (context == EXPAND_HELP
4687 || context == EXPAND_COLORS
4688 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004689 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004690 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004691 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004692 || ((context == EXPAND_TAGS_LISTFILES
4693 || context == EXPAND_TAGS)
4694 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 retval = vim_strnsave(fname, len);
4696 else
4697 {
4698 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4699 for (i = 0; i < len; i++)
4700 {
4701 if (fname[i] == '*' || fname[i] == '~')
4702 new_len++; /* '*' needs to be replaced by ".*"
4703 '~' needs to be replaced by "\~" */
4704
4705 /* Buffer names are like file names. "." should be literal */
4706 if (context == EXPAND_BUFFERS && fname[i] == '.')
4707 new_len++; /* "." becomes "\." */
4708
4709 /* Custom expansion takes care of special things, match
4710 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004711 if ((context == EXPAND_USER_DEFINED
4712 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 new_len++; /* '\' becomes "\\" */
4714 }
4715 retval = alloc(new_len);
4716 if (retval != NULL)
4717 {
4718 retval[0] = '^';
4719 j = 1;
4720 for (i = 0; i < len; i++, j++)
4721 {
4722 /* Skip backslash. But why? At least keep it for custom
4723 * expansion. */
4724 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004725 && context != EXPAND_USER_LIST
4726 && fname[i] == '\\'
4727 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 break;
4729
4730 switch (fname[i])
4731 {
4732 case '*': retval[j++] = '.';
4733 break;
4734 case '~': retval[j++] = '\\';
4735 break;
4736 case '?': retval[j] = '.';
4737 continue;
4738 case '.': if (context == EXPAND_BUFFERS)
4739 retval[j++] = '\\';
4740 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004741 case '\\': if (context == EXPAND_USER_DEFINED
4742 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 retval[j++] = '\\';
4744 break;
4745 }
4746 retval[j] = fname[i];
4747 }
4748 retval[j] = NUL;
4749 }
4750 }
4751 }
4752 else
4753 {
4754 retval = alloc(len + 4);
4755 if (retval != NULL)
4756 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004757 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758
4759 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004760 * Don't add a star to *, ~, ~user, $var or `cmd`.
4761 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 * ~ would be at the start of the file name, but not the tail.
4763 * $ could be anywhere in the tail.
4764 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004765 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 */
4767 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004768 ends_in_star = (len > 0 && retval[len - 1] == '*');
4769#ifndef BACKSLASH_IN_FILENAME
4770 for (i = len - 2; i >= 0; --i)
4771 {
4772 if (retval[i] != '\\')
4773 break;
4774 ends_in_star = !ends_in_star;
4775 }
4776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004778 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 && vim_strchr(tail, '$') == NULL
4780 && vim_strchr(retval, '`') == NULL)
4781 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004782 else if (len > 0 && retval[len - 1] == '$')
4783 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 retval[len] = NUL;
4785 }
4786 }
4787 return retval;
4788}
4789
4790/*
4791 * Must parse the command line so far to work out what context we are in.
4792 * Completion can then be done based on that context.
4793 * This routine sets the variables:
4794 * xp->xp_pattern The start of the pattern to be expanded within
4795 * the command line (ends at the cursor).
4796 * xp->xp_context The type of thing to expand. Will be one of:
4797 *
4798 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4799 * the command line, like an unknown command. Caller
4800 * should beep.
4801 * EXPAND_NOTHING Unrecognised context for completion, use char like
4802 * a normal char, rather than for completion. eg
4803 * :s/^I/
4804 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4805 * it.
4806 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4807 * EXPAND_FILES After command with XFILE set, or after setting
4808 * with P_EXPAND set. eg :e ^I, :w>>^I
4809 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4810 * when we know only directories are of interest. eg
4811 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004812 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4814 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4815 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4816 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4817 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4818 * EXPAND_EVENTS Complete event names
4819 * EXPAND_SYNTAX Complete :syntax command arguments
4820 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4821 * EXPAND_AUGROUP Complete autocommand group names
4822 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4823 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4824 * eg :unmap a^I , :cunab x^I
4825 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4826 * eg :call sub^I
4827 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4828 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4829 * names in expressions, eg :while s^I
4830 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004831 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
4833 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004834set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004836 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 if (ccline.cmdfirstc != ':'
4838#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004839 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004840 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841#endif
4842 )
4843 {
4844 xp->xp_context = EXPAND_NOTHING;
4845 return;
4846 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004847 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848}
4849
4850 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004851set_cmd_context(
4852 expand_T *xp,
4853 char_u *str, /* start of command line */
4854 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004855 int col, /* position of cursor */
4856 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857{
4858 int old_char = NUL;
4859 char_u *nextcomm;
4860
4861 /*
4862 * Avoid a UMR warning from Purify, only save the character if it has been
4863 * written before.
4864 */
4865 if (col < len)
4866 old_char = str[col];
4867 str[col] = NUL;
4868 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004869
4870#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004871 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004872 {
4873# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004874 /* pass CMD_SIZE because there is no real command */
4875 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004876# endif
4877 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004878 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004879 {
4880 xp->xp_context = ccline.xp_context;
4881 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004882# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004883 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004884# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004885 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004886 else
4887#endif
4888 while (nextcomm != NULL)
4889 nextcomm = set_one_cmd_context(xp, nextcomm);
4890
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004891 /* Store the string here so that call_user_expand_func() can get to them
4892 * easily. */
4893 xp->xp_line = str;
4894 xp->xp_col = col;
4895
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 str[col] = old_char;
4897}
4898
4899/*
4900 * Expand the command line "str" from context "xp".
4901 * "xp" must have been set by set_cmd_context().
4902 * xp->xp_pattern points into "str", to where the text that is to be expanded
4903 * starts.
4904 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4905 * cursor.
4906 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4907 * key that triggered expansion literally.
4908 * Returns EXPAND_OK otherwise.
4909 */
4910 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004911expand_cmdline(
4912 expand_T *xp,
4913 char_u *str, /* start of command line */
4914 int col, /* position of cursor */
4915 int *matchcount, /* return: nr of matches */
4916 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917{
4918 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004919 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4922 {
4923 beep_flush();
4924 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4925 }
4926 if (xp->xp_context == EXPAND_NOTHING)
4927 {
4928 /* Caller can use the character as a normal char instead */
4929 return EXPAND_NOTHING;
4930 }
4931
4932 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004933 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4934 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 if (file_str == NULL)
4936 return EXPAND_UNSUCCESSFUL;
4937
Bram Moolenaar94950a92010-12-02 16:01:29 +01004938 if (p_wic)
4939 options += WILD_ICASE;
4940
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004942 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
4944 *matchcount = 0;
4945 *matches = NULL;
4946 }
4947 vim_free(file_str);
4948
4949 return EXPAND_OK;
4950}
4951
4952#ifdef FEAT_MULTI_LANG
4953/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004954 * Cleanup matches for help tags:
4955 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4956 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004959cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960{
4961 int i, j;
4962 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004963 char_u buf[4];
4964 char_u *p = buf;
4965
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004966 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004967 {
4968 *p++ = '@';
4969 *p++ = p_hlg[0];
4970 *p++ = p_hlg[1];
4971 }
4972 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
4974 for (i = 0; i < num_file; ++i)
4975 {
4976 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004977 if (len <= 0)
4978 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004979 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 /* Sorting on priority means the same item in another language may
4982 * be anywhere. Search all items for a match up to the "@en". */
4983 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004984 if (j != i && (int)STRLEN(file[j]) == len + 3
4985 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 break;
4987 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004988 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 file[i][len] = NUL;
4990 }
4991 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004992
4993 if (*buf != NUL)
4994 for (i = 0; i < num_file; ++i)
4995 {
4996 len = (int)STRLEN(file[i]) - 3;
4997 if (len <= 0)
4998 continue;
4999 if (STRCMP(file[i] + len, buf) == 0)
5000 {
5001 /* remove the default language */
5002 file[i][len] = NUL;
5003 }
5004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005}
5006#endif
5007
5008/*
5009 * Do the expansion based on xp->xp_context and "pat".
5010 */
5011 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005012ExpandFromContext(
5013 expand_T *xp,
5014 char_u *pat,
5015 int *num_file,
5016 char_u ***file,
5017 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018{
5019#ifdef FEAT_CMDL_COMPL
5020 regmatch_T regmatch;
5021#endif
5022 int ret;
5023 int flags;
5024
5025 flags = EW_DIR; /* include directories */
5026 if (options & WILD_LIST_NOTFOUND)
5027 flags |= EW_NOTFOUND;
5028 if (options & WILD_ADD_SLASH)
5029 flags |= EW_ADDSLASH;
5030 if (options & WILD_KEEP_ALL)
5031 flags |= EW_KEEPALL;
5032 if (options & WILD_SILENT)
5033 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005034 if (options & WILD_ALLLINKS)
5035 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005037 if (xp->xp_context == EXPAND_FILES
5038 || xp->xp_context == EXPAND_DIRECTORIES
5039 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 /*
5042 * Expand file or directory names.
5043 */
5044 int free_pat = FALSE;
5045 int i;
5046
5047 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5048 * space */
5049 if (xp->xp_backslash != XP_BS_NONE)
5050 {
5051 free_pat = TRUE;
5052 pat = vim_strsave(pat);
5053 for (i = 0; pat[i]; ++i)
5054 if (pat[i] == '\\')
5055 {
5056 if (xp->xp_backslash == XP_BS_THREE
5057 && pat[i + 1] == '\\'
5058 && pat[i + 2] == '\\'
5059 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005060 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 if (xp->xp_backslash == XP_BS_ONE
5062 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005063 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 }
5066
5067 if (xp->xp_context == EXPAND_FILES)
5068 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005069 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5070 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 else
5072 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005073 if (options & WILD_ICASE)
5074 flags |= EW_ICASE;
5075
Bram Moolenaard7834d32009-12-02 16:14:36 +00005076 /* Expand wildcards, supporting %:h and the like. */
5077 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (free_pat)
5079 vim_free(pat);
5080 return ret;
5081 }
5082
5083 *file = (char_u **)"";
5084 *num_file = 0;
5085 if (xp->xp_context == EXPAND_HELP)
5086 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005087 /* With an empty argument we would get all the help tags, which is
5088 * very slow. Get matches for "help" instead. */
5089 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5090 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092#ifdef FEAT_MULTI_LANG
5093 cleanup_help_tags(*num_file, *file);
5094#endif
5095 return OK;
5096 }
5097 return FAIL;
5098 }
5099
5100#ifndef FEAT_CMDL_COMPL
5101 return FAIL;
5102#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005103 if (xp->xp_context == EXPAND_SHELLCMD)
5104 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (xp->xp_context == EXPAND_OLD_SETTING)
5106 return ExpandOldSetting(num_file, file);
5107 if (xp->xp_context == EXPAND_BUFFERS)
5108 return ExpandBufnames(pat, num_file, file, options);
5109 if (xp->xp_context == EXPAND_TAGS
5110 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5111 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5112 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005113 {
5114 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005115 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5116 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005117 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005119 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005120 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005121 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005122 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005123 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005124 {
5125 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005126 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005127 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005128 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005129 {
5130 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005131 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005132 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005133# if defined(FEAT_EVAL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005134 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005135 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005136# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005137 if (xp->xp_context == EXPAND_PACKADD)
5138 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5141 if (regmatch.regprog == NULL)
5142 return FAIL;
5143
5144 /* set ignore-case according to p_ic, p_scs and pat */
5145 regmatch.rm_ic = ignorecase(pat);
5146
5147 if (xp->xp_context == EXPAND_SETTINGS
5148 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5149 ret = ExpandSettings(xp, &regmatch, num_file, file);
5150 else if (xp->xp_context == EXPAND_MAPPINGS)
5151 ret = ExpandMappings(&regmatch, num_file, file);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005152# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 else if (xp->xp_context == EXPAND_USER_DEFINED)
5154 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5155# endif
5156 else
5157 {
5158 static struct expgen
5159 {
5160 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005161 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005163 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 } tab[] =
5165 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005166 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5167 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005168 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005169 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005170#ifdef FEAT_CMDHIST
5171 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5172#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005173 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005174 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005175 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5176 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5177 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005179 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5180 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5181 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5182 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
5184#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005185 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5186 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#endif
5188#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005189 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005191#ifdef FEAT_PROFILE
5192 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5193#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005194 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005195 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5196 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005197#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005198 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005199#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005200#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005201 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005202#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005203#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005204 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005205#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005206#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005207 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5208 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005210 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005211 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005212 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 };
5214 int i;
5215
5216 /*
5217 * Find a context in the table and call the ExpandGeneric() with the
5218 * right function to do the expansion.
5219 */
5220 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005221 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (xp->xp_context == tab[i].context)
5223 {
5224 if (tab[i].ic)
5225 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005226 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005227 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 break;
5229 }
5230 }
5231
Bram Moolenaar473de612013-06-08 18:19:48 +02005232 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233
5234 return ret;
5235#endif /* FEAT_CMDL_COMPL */
5236}
5237
5238#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5239/*
5240 * Expand a list of names.
5241 *
5242 * Generic function for command line completion. It calls a function to
5243 * obtain strings, one by one. The strings are matched against a regexp
5244 * program. Matching strings are copied into an array, which is returned.
5245 *
5246 * Returns OK when no problems encountered, FAIL for error (out of memory).
5247 */
5248 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249ExpandGeneric(
5250 expand_T *xp,
5251 regmatch_T *regmatch,
5252 int *num_file,
5253 char_u ***file,
5254 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005256 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257{
5258 int i;
5259 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005260 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 char_u *str;
5262
5263 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005264 * round == 0: count the number of matching names
5265 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005267 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 for (i = 0; ; ++i)
5270 {
5271 str = (*func)(xp, i);
5272 if (str == NULL) /* end of list */
5273 break;
5274 if (*str == NUL) /* skip empty strings */
5275 continue;
5276
5277 if (vim_regexec(regmatch, str, (colnr_T)0))
5278 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005279 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005281 if (escaped)
5282 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5283 else
5284 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 (*file)[count] = str;
5286#ifdef FEAT_MENU
5287 if (func == get_menu_names && str != NULL)
5288 {
5289 /* test for separator added by get_menu_names() */
5290 str += STRLEN(str) - 1;
5291 if (*str == '\001')
5292 *str = '.';
5293 }
5294#endif
5295 }
5296 ++count;
5297 }
5298 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005299 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 {
5301 if (count == 0)
5302 return OK;
5303 *num_file = count;
5304 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5305 if (*file == NULL)
5306 {
5307 *file = (char_u **)"";
5308 return FAIL;
5309 }
5310 count = 0;
5311 }
5312 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005313
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005314 /* Sort the results. Keep menu's in the specified order. */
5315 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005316 {
5317 if (xp->xp_context == EXPAND_EXPRESSION
5318 || xp->xp_context == EXPAND_FUNCTIONS
5319 || xp->xp_context == EXPAND_USER_FUNC)
5320 /* <SNR> functions should be sorted to the end. */
5321 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5322 sort_func_compare);
5323 else
5324 sort_strings(*file, *num_file);
5325 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005326
Bram Moolenaar4f688582007-07-24 12:34:30 +00005327#ifdef FEAT_CMDL_COMPL
5328 /* Reset the variables used for special highlight names expansion, so that
5329 * they don't show up when getting normal highlight names by ID. */
5330 reset_expand_highlight();
5331#endif
5332
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 return OK;
5334}
5335
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005336/*
5337 * Complete a shell command.
5338 * Returns FAIL or OK;
5339 */
5340 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005341expand_shellcmd(
5342 char_u *filepat, /* pattern to match with command names */
5343 int *num_file, /* return: number of matches */
5344 char_u ***file, /* return: array with matches */
5345 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005346{
5347 char_u *pat;
5348 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005349 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005350 int mustfree = FALSE;
5351 garray_T ga;
5352 char_u *buf = alloc(MAXPATHL);
5353 size_t l;
5354 char_u *s, *e;
5355 int flags = flagsarg;
5356 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005357 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005358 hashtab_T found_ht;
5359 hashitem_T *hi;
5360 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005361
5362 if (buf == NULL)
5363 return FAIL;
5364
5365 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5366 * space */
5367 pat = vim_strsave(filepat);
5368 for (i = 0; pat[i]; ++i)
5369 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005370 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005371
Bram Moolenaarb5971142015-03-21 17:32:19 +01005372 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005373
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005374 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5375 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005376 path = (char_u *)".";
5377 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005378 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005379 /* For an absolute name we don't use $PATH. */
5380 if (!mch_isFullName(pat))
5381 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005382 if (path == NULL)
5383 path = (char_u *)"";
5384 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005385
5386 /*
5387 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005388 * collect them in "ga". When "." is not in $PATH also expand for the
5389 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005390 */
5391 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005392 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005393 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005394 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005395#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005396 e = vim_strchr(s, ';');
5397#else
5398 e = vim_strchr(s, ':');
5399#endif
5400 if (e == NULL)
5401 e = s + STRLEN(s);
5402
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005403 if (*s == NUL)
5404 {
5405 if (did_curdir)
5406 break;
5407 // Find directories in the current directory, path is empty.
5408 did_curdir = TRUE;
5409 flags |= EW_DIR;
5410 }
5411 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5412 {
5413 did_curdir = TRUE;
5414 flags |= EW_DIR;
5415 }
5416 else
5417 // Do not match directories inside a $PATH item.
5418 flags &= ~EW_DIR;
5419
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005420 l = e - s;
5421 if (l > MAXPATHL - 5)
5422 break;
5423 vim_strncpy(buf, s, l);
5424 add_pathsep(buf);
5425 l = STRLEN(buf);
5426 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5427
5428 /* Expand matches in one directory of $PATH. */
5429 ret = expand_wildcards(1, &buf, num_file, file, flags);
5430 if (ret == OK)
5431 {
5432 if (ga_grow(&ga, *num_file) == FAIL)
5433 FreeWild(*num_file, *file);
5434 else
5435 {
5436 for (i = 0; i < *num_file; ++i)
5437 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005438 char_u *name = (*file)[i];
5439
5440 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005441 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005442 // Check if this name was already found.
5443 hash = hash_hash(name + l);
5444 hi = hash_lookup(&found_ht, name + l, hash);
5445 if (HASHITEM_EMPTY(hi))
5446 {
5447 // Remove the path that was prepended.
5448 STRMOVE(name, name + l);
5449 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5450 hash_add_item(&found_ht, hi, name, hash);
5451 name = NULL;
5452 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005453 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005454 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005455 }
5456 vim_free(*file);
5457 }
5458 }
5459 if (*e != NUL)
5460 ++e;
5461 }
5462 *file = ga.ga_data;
5463 *num_file = ga.ga_len;
5464
5465 vim_free(buf);
5466 vim_free(pat);
5467 if (mustfree)
5468 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005469 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005470 return OK;
5471}
5472
5473
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005474# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005476 * Call "user_expand_func()" to invoke a user defined Vim script function and
5477 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005479 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005480call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005481 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005482 expand_T *xp,
5483 int *num_file,
5484 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005486 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005487 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005488 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005489 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005490 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491
Bram Moolenaarb7515462013-06-29 12:58:33 +02005492 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005493 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 *num_file = 0;
5495 *file = NULL;
5496
Bram Moolenaarb7515462013-06-29 12:58:33 +02005497 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005498 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005499 keep = ccline.cmdbuff[ccline.cmdlen];
5500 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005501 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005502
Bram Moolenaarffa96842018-06-12 22:05:14 +02005503 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5504
5505 args[0].v_type = VAR_STRING;
5506 args[0].vval.v_string = pat;
5507 args[1].v_type = VAR_STRING;
5508 args[1].vval.v_string = xp->xp_line;
5509 args[2].v_type = VAR_NUMBER;
5510 args[2].vval.v_number = xp->xp_col;
5511 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005513 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005514
Bram Moolenaarded27a12018-08-01 19:06:03 +02005515 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005516
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005517 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005518 if (ccline.cmdbuff != NULL)
5519 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005520
Bram Moolenaarffa96842018-06-12 22:05:14 +02005521 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005522 return ret;
5523}
5524
5525/*
5526 * Expand names with a function defined by the user.
5527 */
5528 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005529ExpandUserDefined(
5530 expand_T *xp,
5531 regmatch_T *regmatch,
5532 int *num_file,
5533 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005534{
5535 char_u *retstr;
5536 char_u *s;
5537 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005538 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005539 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005540 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005541
5542 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5543 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 return FAIL;
5545
5546 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005547 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 e = vim_strchr(s, '\n');
5550 if (e == NULL)
5551 e = s + STRLEN(s);
5552 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005553 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005555 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5556 *e = keep;
5557
5558 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005560 if (ga_grow(&ga, 1) == FAIL)
5561 break;
5562 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5563 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 }
5565
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 if (*e != NUL)
5567 ++e;
5568 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005569 vim_free(retstr);
5570 *file = ga.ga_data;
5571 *num_file = ga.ga_len;
5572 return OK;
5573}
5574
5575/*
5576 * Expand names with a list returned by a function defined by the user.
5577 */
5578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005579ExpandUserList(
5580 expand_T *xp,
5581 int *num_file,
5582 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005583{
5584 list_T *retlist;
5585 listitem_T *li;
5586 garray_T ga;
5587
5588 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5589 if (retlist == NULL)
5590 return FAIL;
5591
5592 ga_init2(&ga, (int)sizeof(char *), 3);
5593 /* Loop over the items in the list. */
5594 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5595 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005596 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5597 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005598
5599 if (ga_grow(&ga, 1) == FAIL)
5600 break;
5601
5602 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005603 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005604 ++ga.ga_len;
5605 }
5606 list_unref(retlist);
5607
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 *file = ga.ga_data;
5609 *num_file = ga.ga_len;
5610 return OK;
5611}
5612#endif
5613
5614/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005615 * Expand color scheme, compiler or filetype names.
5616 * Search from 'runtimepath':
5617 * 'runtimepath'/{dirnames}/{pat}.vim
5618 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5619 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5620 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5621 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005622 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 */
5624 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005625ExpandRTDir(
5626 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005627 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005628 int *num_file,
5629 char_u ***file,
5630 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 char_u *s;
5633 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005634 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005636 int i;
5637 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638
5639 *num_file = 0;
5640 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005641 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005642 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005644 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005646 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5647 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005649 ga_clear_strings(&ga);
5650 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005652 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005653 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005654 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005656
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005657 if (flags & DIP_START) {
5658 for (i = 0; dirnames[i] != NULL; ++i)
5659 {
5660 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5661 if (s == NULL)
5662 {
5663 ga_clear_strings(&ga);
5664 return FAIL;
5665 }
5666 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5667 globpath(p_pp, s, &ga, 0);
5668 vim_free(s);
5669 }
5670 }
5671
5672 if (flags & DIP_OPT) {
5673 for (i = 0; dirnames[i] != NULL; ++i)
5674 {
5675 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5676 if (s == NULL)
5677 {
5678 ga_clear_strings(&ga);
5679 return FAIL;
5680 }
5681 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5682 globpath(p_pp, s, &ga, 0);
5683 vim_free(s);
5684 }
5685 }
5686
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005687 for (i = 0; i < ga.ga_len; ++i)
5688 {
5689 match = ((char_u **)ga.ga_data)[i];
5690 s = match;
5691 e = s + STRLEN(s);
5692 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5693 {
5694 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005695 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005696 if (s < match || vim_ispathsep(*s))
5697 break;
5698 ++s;
5699 *e = NUL;
5700 mch_memmove(match, s, e - s + 1);
5701 }
5702 }
5703
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005704 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005705 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005706
5707 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005708 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005709 remove_duplicates(&ga);
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 *file = ga.ga_data;
5712 *num_file = ga.ga_len;
5713 return OK;
5714}
5715
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005716/*
5717 * Expand loadplugin names:
5718 * 'packpath'/pack/ * /opt/{pat}
5719 */
5720 static int
5721ExpandPackAddDir(
5722 char_u *pat,
5723 int *num_file,
5724 char_u ***file)
5725{
5726 char_u *s;
5727 char_u *e;
5728 char_u *match;
5729 garray_T ga;
5730 int i;
5731 int pat_len;
5732
5733 *num_file = 0;
5734 *file = NULL;
5735 pat_len = (int)STRLEN(pat);
5736 ga_init2(&ga, (int)sizeof(char *), 10);
5737
5738 s = alloc((unsigned)(pat_len + 26));
5739 if (s == NULL)
5740 {
5741 ga_clear_strings(&ga);
5742 return FAIL;
5743 }
5744 sprintf((char *)s, "pack/*/opt/%s*", pat);
5745 globpath(p_pp, s, &ga, 0);
5746 vim_free(s);
5747
5748 for (i = 0; i < ga.ga_len; ++i)
5749 {
5750 match = ((char_u **)ga.ga_data)[i];
5751 s = gettail(match);
5752 e = s + STRLEN(s);
5753 mch_memmove(match, s, e - s + 1);
5754 }
5755
5756 if (ga.ga_len == 0)
5757 return FAIL;
5758
5759 /* Sort and remove duplicates which can happen when specifying multiple
5760 * directories in dirnames. */
5761 remove_duplicates(&ga);
5762
5763 *file = ga.ga_data;
5764 *num_file = ga.ga_len;
5765 return OK;
5766}
5767
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768#endif
5769
5770#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5771/*
5772 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005773 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005775 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005776globpath(
5777 char_u *path,
5778 char_u *file,
5779 garray_T *ga,
5780 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 expand_T xpc;
5783 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 int num_p;
5786 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787
5788 buf = alloc(MAXPATHL);
5789 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005790 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005792 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 /* Loop over all entries in {path}. */
5796 while (*path != NUL)
5797 {
5798 /* Copy one item of the path to buf[] and concatenate the file name. */
5799 copy_option_part(&path, buf, MAXPATHL, ",");
5800 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5801 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005802# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005803 /* Using the platform's path separator (\) makes vim incorrectly
5804 * treat it as an escape character, use '/' instead. */
5805 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5806 STRCAT(buf, "/");
5807# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005811 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5812 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005814 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005816 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 for (i = 0; i < num_p; ++i)
5819 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005820 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005821 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005822 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 FreeWild(num_p, p);
5827 }
5828 }
5829 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830
5831 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
5833
5834#endif
5835
5836#if defined(FEAT_CMDHIST) || defined(PROTO)
5837
5838/*********************************
5839 * Command line history stuff *
5840 *********************************/
5841
5842/*
5843 * Translate a history character to the associated type number.
5844 */
5845 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005846hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847{
5848 if (c == ':')
5849 return HIST_CMD;
5850 if (c == '=')
5851 return HIST_EXPR;
5852 if (c == '@')
5853 return HIST_INPUT;
5854 if (c == '>')
5855 return HIST_DEBUG;
5856 return HIST_SEARCH; /* must be '?' or '/' */
5857}
5858
5859/*
5860 * Table of history names.
5861 * These names are used in :history and various hist...() functions.
5862 * It is sufficient to give the significant prefix of a history name.
5863 */
5864
5865static char *(history_names[]) =
5866{
5867 "cmd",
5868 "search",
5869 "expr",
5870 "input",
5871 "debug",
5872 NULL
5873};
5874
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005875#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5876/*
5877 * Function given to ExpandGeneric() to obtain the possible first
5878 * arguments of the ":history command.
5879 */
5880 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005881get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005882{
5883 static char_u compl[2] = { NUL, NUL };
5884 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005885 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005886 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5887
5888 if (idx < short_names_count)
5889 {
5890 compl[0] = (char_u)short_names[idx];
5891 return compl;
5892 }
5893 if (idx < short_names_count + history_name_count)
5894 return (char_u *)history_names[idx - short_names_count];
5895 if (idx == short_names_count + history_name_count)
5896 return (char_u *)"all";
5897 return NULL;
5898}
5899#endif
5900
Bram Moolenaar071d4272004-06-13 20:20:40 +00005901/*
5902 * init_history() - Initialize the command line history.
5903 * Also used to re-allocate the history when the size changes.
5904 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005905 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005906init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907{
5908 int newlen; /* new length of history table */
5909 histentry_T *temp;
5910 int i;
5911 int j;
5912 int type;
5913
5914 /*
5915 * If size of history table changed, reallocate it
5916 */
5917 newlen = (int)p_hi;
5918 if (newlen != hislen) /* history length changed */
5919 {
5920 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5921 {
5922 if (newlen)
5923 {
5924 temp = (histentry_T *)lalloc(
5925 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5926 if (temp == NULL) /* out of memory! */
5927 {
5928 if (type == 0) /* first one: just keep the old length */
5929 {
5930 newlen = hislen;
5931 break;
5932 }
5933 /* Already changed one table, now we can only have zero
5934 * length for all tables. */
5935 newlen = 0;
5936 type = -1;
5937 continue;
5938 }
5939 }
5940 else
5941 temp = NULL;
5942 if (newlen == 0 || temp != NULL)
5943 {
5944 if (hisidx[type] < 0) /* there are no entries yet */
5945 {
5946 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005947 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 }
5949 else if (newlen > hislen) /* array becomes bigger */
5950 {
5951 for (i = 0; i <= hisidx[type]; ++i)
5952 temp[i] = history[type][i];
5953 j = i;
5954 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005955 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 for ( ; j < hislen; ++i, ++j)
5957 temp[i] = history[type][j];
5958 }
5959 else /* array becomes smaller or 0 */
5960 {
5961 j = hisidx[type];
5962 for (i = newlen - 1; ; --i)
5963 {
5964 if (i >= 0) /* copy newest entries */
5965 temp[i] = history[type][j];
5966 else /* remove older entries */
5967 vim_free(history[type][j].hisstr);
5968 if (--j < 0)
5969 j = hislen - 1;
5970 if (j == hisidx[type])
5971 break;
5972 }
5973 hisidx[type] = newlen - 1;
5974 }
5975 vim_free(history[type]);
5976 history[type] = temp;
5977 }
5978 }
5979 hislen = newlen;
5980 }
5981}
5982
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005983 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005984clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005985{
5986 hisptr->hisnum = 0;
5987 hisptr->viminfo = FALSE;
5988 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005989 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005990}
5991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992/*
5993 * Check if command line 'str' is already in history.
5994 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5995 */
5996 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005997in_history(
5998 int type,
5999 char_u *str,
6000 int move_to_front, /* Move the entry to the front if it exists */
6001 int sep,
6002 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003{
6004 int i;
6005 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006006 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007
6008 if (hisidx[type] < 0)
6009 return FALSE;
6010 i = hisidx[type];
6011 do
6012 {
6013 if (history[type][i].hisstr == NULL)
6014 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006015
6016 /* For search history, check that the separator character matches as
6017 * well. */
6018 p = history[type][i].hisstr;
6019 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006020 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006021 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022 {
6023 if (!move_to_front)
6024 return TRUE;
6025 last_i = i;
6026 break;
6027 }
6028 if (--i < 0)
6029 i = hislen - 1;
6030 } while (i != hisidx[type]);
6031
6032 if (last_i >= 0)
6033 {
6034 str = history[type][i].hisstr;
6035 while (i != hisidx[type])
6036 {
6037 if (++i >= hislen)
6038 i = 0;
6039 history[type][last_i] = history[type][i];
6040 last_i = i;
6041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006043 history[type][i].viminfo = FALSE;
6044 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006045 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 return TRUE;
6047 }
6048 return FALSE;
6049}
6050
6051/*
6052 * Convert history name (from table above) to its HIST_ equivalent.
6053 * When "name" is empty, return "cmd" history.
6054 * Returns -1 for unknown history name.
6055 */
6056 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006057get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058{
6059 int i;
6060 int len = (int)STRLEN(name);
6061
6062 /* No argument: use current history. */
6063 if (len == 0)
6064 return hist_char2type(ccline.cmdfirstc);
6065
6066 for (i = 0; history_names[i] != NULL; ++i)
6067 if (STRNICMP(name, history_names[i], len) == 0)
6068 return i;
6069
6070 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6071 return hist_char2type(name[0]);
6072
6073 return -1;
6074}
6075
6076static int last_maptick = -1; /* last seen maptick */
6077
6078/*
6079 * Add the given string to the given history. If the string is already in the
6080 * history then it is moved to the front. "histype" may be one of he HIST_
6081 * values.
6082 */
6083 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006084add_to_history(
6085 int histype,
6086 char_u *new_entry,
6087 int in_map, /* consider maptick when inside a mapping */
6088 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089{
6090 histentry_T *hisptr;
6091 int len;
6092
6093 if (hislen == 0) /* no history */
6094 return;
6095
Bram Moolenaara939e432013-11-09 05:30:26 +01006096 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6097 return;
6098
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 /*
6100 * Searches inside the same mapping overwrite each other, so that only
6101 * the last line is kept. Be careful not to remove a line that was moved
6102 * down, only lines that were added.
6103 */
6104 if (histype == HIST_SEARCH && in_map)
6105 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006106 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107 {
6108 /* Current line is from the same mapping, remove it */
6109 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6110 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006111 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 --hisnum[histype];
6113 if (--hisidx[HIST_SEARCH] < 0)
6114 hisidx[HIST_SEARCH] = hislen - 1;
6115 }
6116 last_maptick = -1;
6117 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006118 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 {
6120 if (++hisidx[histype] == hislen)
6121 hisidx[histype] = 0;
6122 hisptr = &history[histype][hisidx[histype]];
6123 vim_free(hisptr->hisstr);
6124
6125 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006126 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6128 if (hisptr->hisstr != NULL)
6129 hisptr->hisstr[len + 1] = sep;
6130
6131 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006132 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006133 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (histype == HIST_SEARCH && in_map)
6135 last_maptick = maptick;
6136 }
6137}
6138
6139#if defined(FEAT_EVAL) || defined(PROTO)
6140
6141/*
6142 * Get identifier of newest history entry.
6143 * "histype" may be one of the HIST_ values.
6144 */
6145 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006146get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006147{
6148 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6149 || hisidx[histype] < 0)
6150 return -1;
6151
6152 return history[histype][hisidx[histype]].hisnum;
6153}
6154
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006155/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156 * Calculate history index from a number:
6157 * num > 0: seen as identifying number of a history entry
6158 * num < 0: relative position in history wrt newest entry
6159 * "histype" may be one of the HIST_ values.
6160 */
6161 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006162calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163{
6164 int i;
6165 histentry_T *hist;
6166 int wrapped = FALSE;
6167
6168 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6169 || (i = hisidx[histype]) < 0 || num == 0)
6170 return -1;
6171
6172 hist = history[histype];
6173 if (num > 0)
6174 {
6175 while (hist[i].hisnum > num)
6176 if (--i < 0)
6177 {
6178 if (wrapped)
6179 break;
6180 i += hislen;
6181 wrapped = TRUE;
6182 }
6183 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6184 return i;
6185 }
6186 else if (-num <= hislen)
6187 {
6188 i += num + 1;
6189 if (i < 0)
6190 i += hislen;
6191 if (hist[i].hisstr != NULL)
6192 return i;
6193 }
6194 return -1;
6195}
6196
6197/*
6198 * Get a history entry by its index.
6199 * "histype" may be one of the HIST_ values.
6200 */
6201 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006202get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203{
6204 idx = calc_hist_idx(histype, idx);
6205 if (idx >= 0)
6206 return history[histype][idx].hisstr;
6207 else
6208 return (char_u *)"";
6209}
6210
6211/*
6212 * Clear all entries of a history.
6213 * "histype" may be one of the HIST_ values.
6214 */
6215 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006216clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217{
6218 int i;
6219 histentry_T *hisptr;
6220
6221 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6222 {
6223 hisptr = history[histype];
6224 for (i = hislen; i--;)
6225 {
6226 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006227 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006228 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
6230 hisidx[histype] = -1; /* mark history as cleared */
6231 hisnum[histype] = 0; /* reset identifier counter */
6232 return OK;
6233 }
6234 return FAIL;
6235}
6236
6237/*
6238 * Remove all entries matching {str} from a history.
6239 * "histype" may be one of the HIST_ values.
6240 */
6241 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006242del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 regmatch_T regmatch;
6245 histentry_T *hisptr;
6246 int idx;
6247 int i;
6248 int last;
6249 int found = FALSE;
6250
6251 regmatch.regprog = NULL;
6252 regmatch.rm_ic = FALSE; /* always match case */
6253 if (hislen != 0
6254 && histype >= 0
6255 && histype < HIST_COUNT
6256 && *str != NUL
6257 && (idx = hisidx[histype]) >= 0
6258 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6259 != NULL)
6260 {
6261 i = last = idx;
6262 do
6263 {
6264 hisptr = &history[histype][i];
6265 if (hisptr->hisstr == NULL)
6266 break;
6267 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6268 {
6269 found = TRUE;
6270 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006271 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273 else
6274 {
6275 if (i != last)
6276 {
6277 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006278 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006279 }
6280 if (--last < 0)
6281 last += hislen;
6282 }
6283 if (--i < 0)
6284 i += hislen;
6285 } while (i != idx);
6286 if (history[histype][idx].hisstr == NULL)
6287 hisidx[histype] = -1;
6288 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006289 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290 return found;
6291}
6292
6293/*
6294 * Remove an indexed entry from a history.
6295 * "histype" may be one of the HIST_ values.
6296 */
6297 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006298del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 int i, j;
6301
6302 i = calc_hist_idx(histype, idx);
6303 if (i < 0)
6304 return FALSE;
6305 idx = hisidx[histype];
6306 vim_free(history[histype][i].hisstr);
6307
6308 /* When deleting the last added search string in a mapping, reset
6309 * last_maptick, so that the last added search string isn't deleted again.
6310 */
6311 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6312 last_maptick = -1;
6313
6314 while (i != idx)
6315 {
6316 j = (i + 1) % hislen;
6317 history[histype][i] = history[histype][j];
6318 i = j;
6319 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006320 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 if (--i < 0)
6322 i += hislen;
6323 hisidx[histype] = i;
6324 return TRUE;
6325}
6326
6327#endif /* FEAT_EVAL */
6328
6329#if defined(FEAT_CRYPT) || defined(PROTO)
6330/*
6331 * Very specific function to remove the value in ":set key=val" from the
6332 * history.
6333 */
6334 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006335remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 char_u *p;
6338 int i;
6339
6340 i = hisidx[HIST_CMD];
6341 if (i < 0)
6342 return;
6343 p = history[HIST_CMD][i].hisstr;
6344 if (p != NULL)
6345 for ( ; *p; ++p)
6346 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6347 {
6348 p = vim_strchr(p + 3, '=');
6349 if (p == NULL)
6350 break;
6351 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006352 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 if (p[i] == '\\' && p[i + 1])
6354 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006355 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 --p;
6357 }
6358}
6359#endif
6360
6361#endif /* FEAT_CMDHIST */
6362
Bram Moolenaar064154c2016-03-19 22:50:43 +01006363#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006364/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006365 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006366 * ccline and put the previous value in prev_ccline.
6367 */
6368 static struct cmdline_info *
6369get_ccline_ptr(void)
6370{
6371 if ((State & CMDLINE) == 0)
6372 return NULL;
6373 if (ccline.cmdbuff != NULL)
6374 return &ccline;
6375 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6376 return &prev_ccline;
6377 return NULL;
6378}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006379#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006380
Bram Moolenaar064154c2016-03-19 22:50:43 +01006381#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006382/*
6383 * Get the current command line in allocated memory.
6384 * Only works when the command line is being edited.
6385 * Returns NULL when something is wrong.
6386 */
6387 char_u *
6388get_cmdline_str(void)
6389{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006390 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006391
Bram Moolenaaree91c332018-09-25 22:27:35 +02006392 if (cmdline_star > 0)
6393 return NULL;
6394 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006395 if (p == NULL)
6396 return NULL;
6397 return vim_strnsave(p->cmdbuff, p->cmdlen);
6398}
6399
6400/*
6401 * Get the current command line position, counted in bytes.
6402 * Zero is the first position.
6403 * Only works when the command line is being edited.
6404 * Returns -1 when something is wrong.
6405 */
6406 int
6407get_cmdline_pos(void)
6408{
6409 struct cmdline_info *p = get_ccline_ptr();
6410
6411 if (p == NULL)
6412 return -1;
6413 return p->cmdpos;
6414}
6415
6416/*
6417 * Set the command line byte position to "pos". Zero is the first position.
6418 * Only works when the command line is being edited.
6419 * Returns 1 when failed, 0 when OK.
6420 */
6421 int
6422set_cmdline_pos(
6423 int pos)
6424{
6425 struct cmdline_info *p = get_ccline_ptr();
6426
6427 if (p == NULL)
6428 return 1;
6429
6430 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6431 * changed the command line. */
6432 if (pos < 0)
6433 new_cmdpos = 0;
6434 else
6435 new_cmdpos = pos;
6436 return 0;
6437}
6438#endif
6439
6440#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6441/*
6442 * Get the current command-line type.
6443 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6444 * Only works when the command line is being edited.
6445 * Returns NUL when something is wrong.
6446 */
6447 int
6448get_cmdline_type(void)
6449{
6450 struct cmdline_info *p = get_ccline_ptr();
6451
6452 if (p == NULL)
6453 return NUL;
6454 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006455 return
6456# ifdef FEAT_EVAL
6457 (p->input_fn) ? '@' :
6458# endif
6459 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006460 return p->cmdfirstc;
6461}
6462#endif
6463
Bram Moolenaar071d4272004-06-13 20:20:40 +00006464#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6465/*
6466 * Get indices "num1,num2" that specify a range within a list (not a range of
6467 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6468 * Returns OK if parsed successfully, otherwise FAIL.
6469 */
6470 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006471get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472{
6473 int len;
6474 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006475 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006476
6477 *str = skipwhite(*str);
6478 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6479 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006480 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481 *str += len;
6482 *num1 = (int)num;
6483 first = TRUE;
6484 }
6485 *str = skipwhite(*str);
6486 if (**str == ',') /* parse "to" part of range */
6487 {
6488 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006489 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (len > 0)
6491 {
6492 *num2 = (int)num;
6493 *str = skipwhite(*str + len);
6494 }
6495 else if (!first) /* no number given at all */
6496 return FAIL;
6497 }
6498 else if (first) /* only one number given */
6499 *num2 = *num1;
6500 return OK;
6501}
6502#endif
6503
6504#if defined(FEAT_CMDHIST) || defined(PROTO)
6505/*
6506 * :history command - print a history
6507 */
6508 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006509ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510{
6511 histentry_T *hist;
6512 int histype1 = HIST_CMD;
6513 int histype2 = HIST_CMD;
6514 int hisidx1 = 1;
6515 int hisidx2 = -1;
6516 int idx;
6517 int i, j, k;
6518 char_u *end;
6519 char_u *arg = eap->arg;
6520
6521 if (hislen == 0)
6522 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006523 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 return;
6525 }
6526
6527 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6528 {
6529 end = arg;
6530 while (ASCII_ISALPHA(*end)
6531 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6532 end++;
6533 i = *end;
6534 *end = NUL;
6535 histype1 = get_histtype(arg);
6536 if (histype1 == -1)
6537 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006538 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 {
6540 histype1 = 0;
6541 histype2 = HIST_COUNT-1;
6542 }
6543 else
6544 {
6545 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006546 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547 return;
6548 }
6549 }
6550 else
6551 histype2 = histype1;
6552 *end = i;
6553 }
6554 else
6555 end = arg;
6556 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6557 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006558 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 return;
6560 }
6561
6562 for (; !got_int && histype1 <= histype2; ++histype1)
6563 {
6564 STRCPY(IObuff, "\n # ");
6565 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006566 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 idx = hisidx[histype1];
6568 hist = history[histype1];
6569 j = hisidx1;
6570 k = hisidx2;
6571 if (j < 0)
6572 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6573 if (k < 0)
6574 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6575 if (idx >= 0 && j <= k)
6576 for (i = idx + 1; !got_int; ++i)
6577 {
6578 if (i == hislen)
6579 i = 0;
6580 if (hist[i].hisstr != NULL
6581 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6582 {
6583 msg_putchar('\n');
6584 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6585 hist[i].hisnum);
6586 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6587 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006588 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 else
6590 STRCAT(IObuff, hist[i].hisstr);
6591 msg_outtrans(IObuff);
6592 out_flush();
6593 }
6594 if (i == idx)
6595 break;
6596 }
6597 }
6598}
6599#endif
6600
6601#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006602/*
6603 * Buffers for history read from a viminfo file. Only valid while reading.
6604 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006605static histentry_T *viminfo_history[HIST_COUNT] =
6606 {NULL, NULL, NULL, NULL, NULL};
6607static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6608static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609static int viminfo_add_at_front = FALSE;
6610
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611/*
6612 * Translate a history type number to the associated character.
6613 */
6614 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006615hist_type2char(
6616 int type,
6617 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 if (type == HIST_CMD)
6620 return ':';
6621 if (type == HIST_SEARCH)
6622 {
6623 if (use_question)
6624 return '?';
6625 else
6626 return '/';
6627 }
6628 if (type == HIST_EXPR)
6629 return '=';
6630 return '@';
6631}
6632
6633/*
6634 * Prepare for reading the history from the viminfo file.
6635 * This allocates history arrays to store the read history lines.
6636 */
6637 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006638prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639{
6640 int i;
6641 int num;
6642 int type;
6643 int len;
6644
6645 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006646 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647 if (asklen > hislen)
6648 asklen = hislen;
6649
6650 for (type = 0; type < HIST_COUNT; ++type)
6651 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006652 /* Count the number of empty spaces in the history list. Entries read
6653 * from viminfo previously are also considered empty. If there are
6654 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006656 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 num++;
6658 len = asklen;
6659 if (num > len)
6660 len = num;
6661 if (len <= 0)
6662 viminfo_history[type] = NULL;
6663 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006664 viminfo_history[type] = (histentry_T *)lalloc(
6665 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 if (viminfo_history[type] == NULL)
6667 len = 0;
6668 viminfo_hislen[type] = len;
6669 viminfo_hisidx[type] = 0;
6670 }
6671}
6672
6673/*
6674 * Accept a line from the viminfo, store it in the history array when it's
6675 * new.
6676 */
6677 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006678read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 int type;
6681 long_u len;
6682 char_u *val;
6683 char_u *p;
6684
6685 type = hist_char2type(virp->vir_line[0]);
6686 if (viminfo_hisidx[type] < viminfo_hislen[type])
6687 {
6688 val = viminfo_readstring(virp, 1, TRUE);
6689 if (val != NULL && *val != NUL)
6690 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006691 int sep = (*val == ' ' ? NUL : *val);
6692
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006694 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 {
6696 /* Need to re-allocate to append the separator byte. */
6697 len = STRLEN(val);
6698 p = lalloc(len + 2, TRUE);
6699 if (p != NULL)
6700 {
6701 if (type == HIST_SEARCH)
6702 {
6703 /* Search entry: Move the separator from the first
6704 * column to after the NUL. */
6705 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006706 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 }
6708 else
6709 {
6710 /* Not a search entry: No separator in the viminfo
6711 * file, add a NUL separator. */
6712 mch_memmove(p, val, (size_t)len + 1);
6713 p[len + 1] = NUL;
6714 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006715 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6716 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006717 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6718 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006719 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721 }
6722 }
6723 vim_free(val);
6724 }
6725 return viminfo_readline(virp);
6726}
6727
Bram Moolenaar07219f92013-04-14 23:19:36 +02006728/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006729 * Accept a new style history line from the viminfo, store it in the history
6730 * array when it's new.
6731 */
6732 void
6733handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006734 garray_T *values,
6735 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006736{
6737 int type;
6738 long_u len;
6739 char_u *val;
6740 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006741 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006742
6743 /* Check the format:
6744 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006745 if (values->ga_len < 4
6746 || vp[0].bv_type != BVAL_NR
6747 || vp[1].bv_type != BVAL_NR
6748 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6749 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006750 return;
6751
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006752 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006753 if (type >= HIST_COUNT)
6754 return;
6755 if (viminfo_hisidx[type] < viminfo_hislen[type])
6756 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006757 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006758 if (val != NULL && *val != NUL)
6759 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006760 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6761 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006762 int idx;
6763 int overwrite = FALSE;
6764
6765 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6766 {
6767 /* If lines were written by an older Vim we need to avoid
6768 * getting duplicates. See if the entry already exists. */
6769 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6770 {
6771 p = viminfo_history[type][idx].hisstr;
6772 if (STRCMP(val, p) == 0
6773 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6774 {
6775 overwrite = TRUE;
6776 break;
6777 }
6778 }
6779
6780 if (!overwrite)
6781 {
6782 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006783 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006784 p = lalloc(len + 2, TRUE);
6785 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006786 else
6787 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006788 if (p != NULL)
6789 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006790 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006791 if (!overwrite)
6792 {
6793 mch_memmove(p, val, (size_t)len + 1);
6794 /* Put the separator after the NUL. */
6795 p[len + 1] = sep;
6796 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006797 viminfo_history[type][idx].hisnum = 0;
6798 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006799 viminfo_hisidx[type]++;
6800 }
6801 }
6802 }
6803 }
6804 }
6805}
6806
6807/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006808 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006809 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006810 static void
6811concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812{
6813 int idx;
6814 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006815
6816 idx = hisidx[type] + viminfo_hisidx[type];
6817 if (idx >= hislen)
6818 idx -= hislen;
6819 else if (idx < 0)
6820 idx = hislen - 1;
6821 if (viminfo_add_at_front)
6822 hisidx[type] = idx;
6823 else
6824 {
6825 if (hisidx[type] == -1)
6826 hisidx[type] = hislen - 1;
6827 do
6828 {
6829 if (history[type][idx].hisstr != NULL
6830 || history[type][idx].viminfo)
6831 break;
6832 if (++idx == hislen)
6833 idx = 0;
6834 } while (idx != hisidx[type]);
6835 if (idx != hisidx[type] && --idx < 0)
6836 idx = hislen - 1;
6837 }
6838 for (i = 0; i < viminfo_hisidx[type]; i++)
6839 {
6840 vim_free(history[type][idx].hisstr);
6841 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6842 history[type][idx].viminfo = TRUE;
6843 history[type][idx].time_set = viminfo_history[type][i].time_set;
6844 if (--idx < 0)
6845 idx = hislen - 1;
6846 }
6847 idx += 1;
6848 idx %= hislen;
6849 for (i = 0; i < viminfo_hisidx[type]; i++)
6850 {
6851 history[type][idx++].hisnum = ++hisnum[type];
6852 idx %= hislen;
6853 }
6854}
6855
6856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6857 static int
6858#ifdef __BORLANDC__
6859_RTLENTRYF
6860#endif
6861sort_hist(const void *s1, const void *s2)
6862{
6863 histentry_T *p1 = *(histentry_T **)s1;
6864 histentry_T *p2 = *(histentry_T **)s2;
6865
6866 if (p1->time_set < p2->time_set) return -1;
6867 if (p1->time_set > p2->time_set) return 1;
6868 return 0;
6869}
6870#endif
6871
6872/*
6873 * Merge history lines from viminfo and lines typed in this Vim based on the
6874 * timestamp;
6875 */
6876 static void
6877merge_history(int type)
6878{
6879 int max_len;
6880 histentry_T **tot_hist;
6881 histentry_T *new_hist;
6882 int i;
6883 int len;
6884
6885 /* Make one long list with all entries. */
6886 max_len = hislen + viminfo_hisidx[type];
6887 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6888 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6889 if (tot_hist == NULL || new_hist == NULL)
6890 {
6891 vim_free(tot_hist);
6892 vim_free(new_hist);
6893 return;
6894 }
6895 for (i = 0; i < viminfo_hisidx[type]; i++)
6896 tot_hist[i] = &viminfo_history[type][i];
6897 len = i;
6898 for (i = 0; i < hislen; i++)
6899 if (history[type][i].hisstr != NULL)
6900 tot_hist[len++] = &history[type][i];
6901
6902 /* Sort the list on timestamp. */
6903 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6904
6905 /* Keep the newest ones. */
6906 for (i = 0; i < hislen; i++)
6907 {
6908 if (i < len)
6909 {
6910 new_hist[i] = *tot_hist[i];
6911 tot_hist[i]->hisstr = NULL;
6912 if (new_hist[i].hisnum == 0)
6913 new_hist[i].hisnum = ++hisnum[type];
6914 }
6915 else
6916 clear_hist_entry(&new_hist[i]);
6917 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006918 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006919
6920 /* Free what is not kept. */
6921 for (i = 0; i < viminfo_hisidx[type]; i++)
6922 vim_free(viminfo_history[type][i].hisstr);
6923 for (i = 0; i < hislen; i++)
6924 vim_free(history[type][i].hisstr);
6925 vim_free(history[type]);
6926 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006927 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006928}
6929
6930/*
6931 * Finish reading history lines from viminfo. Not used when writing viminfo.
6932 */
6933 void
6934finish_viminfo_history(vir_T *virp)
6935{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006937 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938
6939 for (type = 0; type < HIST_COUNT; ++type)
6940 {
6941 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006942 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006943
6944 if (merge)
6945 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006947 concat_history(type);
6948
Bram Moolenaard23a8232018-02-10 18:45:26 +01006949 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006950 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 }
6952}
6953
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006954/*
6955 * Write history to viminfo file in "fp".
6956 * When "merge" is TRUE merge history lines with a previously read viminfo
6957 * file, data is in viminfo_history[].
6958 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006961write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962{
6963 int i;
6964 int type;
6965 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006966 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967
6968 init_history();
6969 if (hislen == 0)
6970 return;
6971 for (type = 0; type < HIST_COUNT; ++type)
6972 {
6973 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6974 if (num_saved == 0)
6975 continue;
6976 if (num_saved < 0) /* Use default */
6977 num_saved = hislen;
6978 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6979 type == HIST_CMD ? _("Command Line") :
6980 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006981 type == HIST_EXPR ? _("Expression") :
6982 type == HIST_INPUT ? _("Input Line") :
6983 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006984 if (num_saved > hislen)
6985 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006986
6987 /*
6988 * Merge typed and viminfo history:
6989 * round 1: history of typed commands.
6990 * round 2: history from recently read viminfo.
6991 */
6992 for (round = 1; round <= 2; ++round)
6993 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006994 if (round == 1)
6995 /* start at newest entry, somewhere in the list */
6996 i = hisidx[type];
6997 else if (viminfo_hisidx[type] > 0)
6998 /* start at newest entry, first in the list */
6999 i = 0;
7000 else
7001 /* empty list */
7002 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007003 if (i >= 0)
7004 while (num_saved > 0
7005 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007006 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007007 char_u *p;
7008 time_t timestamp;
7009 int c = NUL;
7010
7011 if (round == 1)
7012 {
7013 p = history[type][i].hisstr;
7014 timestamp = history[type][i].time_set;
7015 }
7016 else
7017 {
7018 p = viminfo_history[type] == NULL ? NULL
7019 : viminfo_history[type][i].hisstr;
7020 timestamp = viminfo_history[type] == NULL ? 0
7021 : viminfo_history[type][i].time_set;
7022 }
7023
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007024 if (p != NULL && (round == 2
7025 || !merge
7026 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007028 --num_saved;
7029 fputc(hist_type2char(type, TRUE), fp);
7030 /* For the search history: put the separator in the
7031 * second column; use a space if there isn't one. */
7032 if (type == HIST_SEARCH)
7033 {
7034 c = p[STRLEN(p) + 1];
7035 putc(c == NUL ? ' ' : c, fp);
7036 }
7037 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007038
7039 {
7040 char cbuf[NUMBUFLEN];
7041
7042 /* New style history with a bar line. Format:
7043 * |{bartype},{histtype},{timestamp},{separator},"text" */
7044 if (c == NUL)
7045 cbuf[0] = NUL;
7046 else
7047 sprintf(cbuf, "%d", c);
7048 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7049 type, (long)timestamp, cbuf);
7050 barline_writestring(fp, p, LSIZE - 20);
7051 putc('\n', fp);
7052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007054 if (round == 1)
7055 {
7056 /* Decrement index, loop around and stop when back at
7057 * the start. */
7058 if (--i < 0)
7059 i = hislen - 1;
7060 if (i == hisidx[type])
7061 break;
7062 }
7063 else
7064 {
7065 /* Increment index. Stop at the end in the while. */
7066 ++i;
7067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007069 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007070 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007071 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007072 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007073 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007074 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 }
7076}
7077#endif /* FEAT_VIMINFO */
7078
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079#if defined(FEAT_CMDWIN) || defined(PROTO)
7080/*
7081 * Open a window on the current command line and history. Allow editing in
7082 * the window. Returns when the window is closed.
7083 * Returns:
7084 * CR if the command is to be executed
7085 * Ctrl_C if it is to be abandoned
7086 * K_IGNORE if editing continues
7087 */
7088 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007089open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007091 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007092 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007093 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 win_T *wp;
7095 int i;
7096 linenr_T lnum;
7097 int histtype;
7098 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 int save_restart_edit = restart_edit;
7100 int save_State = State;
7101 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007102#ifdef FEAT_RIGHTLEFT
7103 int save_cmdmsg_rl = cmdmsg_rl;
7104#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007105#ifdef FEAT_FOLDING
7106 int save_KeyTyped;
7107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108
7109 /* Can't do this recursively. Can't do it when typing a password. */
7110 if (cmdwin_type != 0
7111# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7112 || cmdline_star > 0
7113# endif
7114 )
7115 {
7116 beep_flush();
7117 return K_IGNORE;
7118 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007119 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120
7121 /* Save current window sizes. */
7122 win_size_save(&winsizes);
7123
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007125 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007126
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007127#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007128 // When using completion in Insert mode with <C-R>=<C-F> one can open the
7129 // command line window, but we don't want the popup menu then.
7130 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007131#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007132
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007133 /* don't use a new tab page */
7134 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007135 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007136
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 /* Create a window for the command-line buffer. */
7138 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7139 {
7140 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007141 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 return K_IGNORE;
7143 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007144 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007145
7146 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007147 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007148 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007151#ifdef FEAT_FOLDING
7152 curwin->w_p_fen = FALSE;
7153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007155 curwin->w_p_rl = cmdmsg_rl;
7156 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007158 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159
Bram Moolenaar071d4272004-06-13 20:20:40 +00007160 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007161 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007162 /* But don't allow switching to another buffer. */
7163 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164
Bram Moolenaar46152342005-09-07 21:18:43 +00007165 /* Showing the prompt may have set need_wait_return, reset it. */
7166 need_wait_return = FALSE;
7167
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007168 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7170 {
7171 if (p_wc == TAB)
7172 {
7173 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7174 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7175 }
7176 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7177 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007178 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007180 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7181 * sets 'textwidth' to 78). */
7182 curbuf->b_p_tw = 0;
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184 /* Fill the buffer with the history. */
7185 init_history();
7186 if (hislen > 0)
7187 {
7188 i = hisidx[histtype];
7189 if (i >= 0)
7190 {
7191 lnum = 0;
7192 do
7193 {
7194 if (++i == hislen)
7195 i = 0;
7196 if (history[histtype][i].hisstr != NULL)
7197 ml_append(lnum++, history[histtype][i].hisstr,
7198 (colnr_T)0, FALSE);
7199 }
7200 while (i != hisidx[histtype]);
7201 }
7202 }
7203
7204 /* Replace the empty last line with the current command-line and put the
7205 * cursor there. */
7206 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7207 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7208 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007209 changed_line_abv_curs();
7210 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007211 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 /* No Ex mode here! */
7214 exmode_active = 0;
7215
7216 State = NORMAL;
7217# ifdef FEAT_MOUSE
7218 setmouse();
7219# endif
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007222 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007223 if (restart_edit != 0) /* autocmd with ":startinsert" */
7224 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007225
7226 i = RedrawingDisabled;
7227 RedrawingDisabled = 0;
7228
7229 /*
7230 * Call the main loop until <CR> or CTRL-C is typed.
7231 */
7232 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007233 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 RedrawingDisabled = i;
7236
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007237# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007238 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007239# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007240
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007242 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007243
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007244# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007245 /* Restore KeyTyped in case it is modified by autocommands */
7246 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247# endif
7248
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 exmode_active = save_exmode;
7251
Bram Moolenaarf9821062008-06-20 16:31:07 +00007252 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007254 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255 {
7256 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007257 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 }
7259 else
7260 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007261# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 /* autocmds may abort script processing */
7263 if (aborting() && cmdwin_result != K_IGNORE)
7264 cmdwin_result = Ctrl_C;
7265# endif
7266 /* Set the new command line from the cmdline buffer. */
7267 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007268 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007270 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7271
7272 if (histtype == HIST_CMD)
7273 {
7274 /* Execute the command directly. */
7275 ccline.cmdbuff = vim_strsave((char_u *)p);
7276 cmdwin_result = CAR;
7277 }
7278 else
7279 {
7280 /* First need to cancel what we were doing. */
7281 ccline.cmdbuff = NULL;
7282 stuffcharReadbuff(':');
7283 stuffReadbuff((char_u *)p);
7284 stuffcharReadbuff(CAR);
7285 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286 }
7287 else if (cmdwin_result == K_XF2) /* :qa typed */
7288 {
7289 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7290 cmdwin_result = CAR;
7291 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007292 else if (cmdwin_result == Ctrl_C)
7293 {
7294 /* :q or :close, don't execute any command
7295 * and don't modify the cmd window. */
7296 ccline.cmdbuff = NULL;
7297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 else
7299 ccline.cmdbuff = vim_strsave(ml_get_curline());
7300 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007301 {
7302 ccline.cmdbuff = vim_strsave((char_u *)"");
7303 ccline.cmdlen = 0;
7304 ccline.cmdbufflen = 1;
7305 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 else
7309 {
7310 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7311 ccline.cmdbufflen = ccline.cmdlen + 1;
7312 ccline.cmdpos = curwin->w_cursor.col;
7313 if (ccline.cmdpos > ccline.cmdlen)
7314 ccline.cmdpos = ccline.cmdlen;
7315 if (cmdwin_result == K_IGNORE)
7316 {
7317 set_cmdspos_cursor();
7318 redrawcmd();
7319 }
7320 }
7321
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007323 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007324# ifdef FEAT_CONCEAL
7325 /* Avoid command-line window first character being concealed. */
7326 curwin->w_p_cole = 0;
7327# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007329 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007330 win_goto(old_curwin);
7331 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007332
7333 /* win_close() may have already wiped the buffer when 'bh' is
7334 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007335 if (bufref_valid(&bufref))
7336 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337
7338 /* Restore window sizes. */
7339 win_size_restore(&winsizes);
7340
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007341 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
7343
7344 ga_clear(&winsizes);
7345 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007346# ifdef FEAT_RIGHTLEFT
7347 cmdmsg_rl = save_cmdmsg_rl;
7348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349
7350 State = save_State;
7351# ifdef FEAT_MOUSE
7352 setmouse();
7353# endif
7354
7355 return cmdwin_result;
7356}
7357#endif /* FEAT_CMDWIN */
7358
7359/*
7360 * Used for commands that either take a simple command string argument, or:
7361 * cmd << endmarker
7362 * {script}
7363 * endmarker
7364 * Returns a pointer to allocated memory with {script} or NULL.
7365 */
7366 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007367script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368{
7369 char_u *theline;
7370 char *end_pattern = NULL;
7371 char dot[] = ".";
7372 garray_T ga;
7373
7374 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7375 return NULL;
7376
7377 ga_init2(&ga, 1, 0x400);
7378
7379 if (cmd[2] != NUL)
7380 end_pattern = (char *)skipwhite(cmd + 2);
7381 else
7382 end_pattern = dot;
7383
7384 for (;;)
7385 {
7386 theline = eap->getline(
7387#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007388 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389#endif
7390 NUL, eap->cookie, 0);
7391
7392 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007393 {
7394 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397
7398 ga_concat(&ga, theline);
7399 ga_append(&ga, '\n');
7400 vim_free(theline);
7401 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007402 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403
7404 return (char_u *)ga.ga_data;
7405}