blob: efeae94ed814aeba480227202690f5624b22bae8 [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)
Bram Moolenaareae1b912019-05-09 15:12:55 +0200128static int sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200129#endif
130
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200131
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200132 static void
133trigger_cmd_autocmd(int typechar, int evt)
134{
135 char_u typestr[2];
136
137 typestr[0] = typechar;
138 typestr[1] = NUL;
139 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
140}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200143 * Abandon the command line.
144 */
145 static void
146abandon_cmdline(void)
147{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100148 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200149 if (msg_scrolled == 0)
150 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100151 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200152 redraw_cmdline = TRUE;
153}
154
Bram Moolenaaree219b02017-12-17 14:55:01 +0100155#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200156/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100157 * Guess that the pattern matches everything. Only finds specific cases, such
158 * as a trailing \|, which can happen while typing a pattern.
159 */
160 static int
161empty_pattern(char_u *p)
162{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100163 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100164
165 /* remove trailing \v and the like */
166 while (n >= 2 && p[n - 2] == '\\'
167 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
168 n -= 2;
169 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
170}
171
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200172// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200173typedef struct {
174 colnr_T vs_curswant;
175 colnr_T vs_leftcol;
176 linenr_T vs_topline;
177# ifdef FEAT_DIFF
178 int vs_topfill;
179# endif
180 linenr_T vs_botline;
181 linenr_T vs_empty_rows;
182} viewstate_T;
183
184 static void
185save_viewstate(viewstate_T *vs)
186{
187 vs->vs_curswant = curwin->w_curswant;
188 vs->vs_leftcol = curwin->w_leftcol;
189 vs->vs_topline = curwin->w_topline;
190# ifdef FEAT_DIFF
191 vs->vs_topfill = curwin->w_topfill;
192# endif
193 vs->vs_botline = curwin->w_botline;
194 vs->vs_empty_rows = curwin->w_empty_rows;
195}
196
197 static void
198restore_viewstate(viewstate_T *vs)
199{
200 curwin->w_curswant = vs->vs_curswant;
201 curwin->w_leftcol = vs->vs_leftcol;
202 curwin->w_topline = vs->vs_topline;
203# ifdef FEAT_DIFF
204 curwin->w_topfill = vs->vs_topfill;
205# endif
206 curwin->w_botline = vs->vs_botline;
207 curwin->w_empty_rows = vs->vs_empty_rows;
208}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200209
210// Struct to store the state of 'incsearch' highlighting.
211typedef struct {
212 pos_T search_start; // where 'incsearch' starts searching
213 pos_T save_cursor;
214 viewstate_T init_viewstate;
215 viewstate_T old_viewstate;
216 pos_T match_start;
217 pos_T match_end;
218 int did_incsearch;
219 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200220 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200221} incsearch_state_T;
222
223 static void
224init_incsearch_state(incsearch_state_T *is_state)
225{
226 is_state->match_start = curwin->w_cursor;
227 is_state->did_incsearch = FALSE;
228 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200229 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200230 CLEAR_POS(&is_state->match_end);
231 is_state->save_cursor = curwin->w_cursor; // may be restored later
232 is_state->search_start = curwin->w_cursor;
233 save_viewstate(&is_state->init_viewstate);
234 save_viewstate(&is_state->old_viewstate);
235}
236
237/*
238 * First move cursor to end of match, then to the start. This
239 * moves the whole match onto the screen when 'nowrap' is set.
240 */
241 static void
242set_search_match(pos_T *t)
243{
244 t->lnum += search_match_lines;
245 t->col = search_match_endcol;
246 if (t->lnum > curbuf->b_ml.ml_line_count)
247 {
248 t->lnum = curbuf->b_ml.ml_line_count;
249 coladvance((colnr_T)MAXCOL);
250 }
251}
252
253/*
254 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200255 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200256 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200257 */
258 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200259do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
260 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200261{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200262 char_u *cmd;
263 cmdmod_T save_cmdmod = cmdmod;
264 char_u *p;
265 int delim_optional = FALSE;
266 int delim;
267 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100268 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200269 exarg_T ea;
270 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200271 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 *skiplen = 0;
274 *patlen = ccline.cmdlen;
275
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276 if (!p_is || cmd_silent)
277 return FALSE;
278
279 // by default search all lines
280 search_first_line = 0;
281 search_last_line = MAXLNUM;
282
283 if (firstc == '/' || firstc == '?')
284 return TRUE;
285 if (firstc != ':')
286 return FALSE;
287
288 vim_memset(&ea, 0, sizeof(ea));
289 ea.line1 = 1;
290 ea.line2 = 1;
291 ea.cmd = ccline.cmdbuff;
292 ea.addr_type = ADDR_LINES;
293
294 parse_command_modifiers(&ea, &dummy, TRUE);
295 cmdmod = save_cmdmod;
296
297 cmd = skip_range(ea.cmd, NULL);
298 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
299 return FALSE;
300
301 // Skip over "substitute" to find the pattern separator.
302 for (p = cmd; ASCII_ISALPHA(*p); ++p)
303 ;
304 if (*skipwhite(p) == NUL)
305 return FALSE;
306
307 if (STRNCMP(cmd, "substitute", p - cmd) == 0
308 || STRNCMP(cmd, "smagic", p - cmd) == 0
309 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
310 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200311 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200312 if (*cmd == 's' && cmd[1] == 'm')
313 p_magic = TRUE;
314 else if (*cmd == 's' && cmd[1] == 'n')
315 p_magic = FALSE;
316 }
317 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
318 {
319 // skip over flags
320 while (ASCII_ISALPHA(*(p = skipwhite(p))))
321 ++p;
322 if (*p == NUL)
323 return FALSE;
324 }
325 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
326 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
327 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
328 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
329 || STRNCMP(cmd, "global", p - cmd) == 0)
330 {
331 // skip over "!"
332 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200333 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200334 p++;
335 if (*skipwhite(p) == NUL)
336 return FALSE;
337 }
338 if (*cmd != 'g')
339 delim_optional = TRUE;
340 }
341 else
342 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200343
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200344 p = skipwhite(p);
345 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
346 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200347
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200348 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200349
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200350 if (end == p && !use_last_pat)
351 return FALSE;
352
353 // Don't do 'hlsearch' highlighting if the pattern matches everything.
354 if (!use_last_pat)
355 {
356 char c = *end;
357 int empty;
358
359 *end = NUL;
360 empty = empty_pattern(p);
361 *end = c;
362 if (empty)
363 return FALSE;
364 }
365
366 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200367 *skiplen = (int)(p - ccline.cmdbuff);
368 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200369
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200370 // parse the address range
371 save_cursor = curwin->w_cursor;
372 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200373 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200374 if (ea.addr_count > 0)
375 {
376 // Allow for reverse match.
377 if (ea.line2 < ea.line1)
378 {
379 search_first_line = ea.line2;
380 search_last_line = ea.line1;
381 }
382 else
383 {
384 search_first_line = ea.line1;
385 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200386 }
387 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200388 else if (cmd[0] == 's' && cmd[1] != 'o')
389 {
390 // :s defaults to the current line
391 search_first_line = curwin->w_cursor.lnum;
392 search_last_line = curwin->w_cursor.lnum;
393 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200394
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200395 curwin->w_cursor = save_cursor;
396 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200397}
398
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200399 static void
400finish_incsearch_highlighting(
401 int gotesc,
402 incsearch_state_T *is_state,
403 int call_update_screen)
404{
405 if (is_state->did_incsearch)
406 {
407 is_state->did_incsearch = FALSE;
408 if (gotesc)
409 curwin->w_cursor = is_state->save_cursor;
410 else
411 {
412 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
413 {
414 // put the '" mark at the original position
415 curwin->w_cursor = is_state->save_cursor;
416 setpcmark();
417 }
418 curwin->w_cursor = is_state->search_start;
419 }
420 restore_viewstate(&is_state->old_viewstate);
421 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200422
423 // by default search all lines
424 search_first_line = 0;
425 search_last_line = MAXLNUM;
426
427 p_magic = is_state->magic_save;
428
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200429 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200430 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200431 if (call_update_screen)
432 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200433 }
434}
435
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200436/*
437 * Do 'incsearch' highlighting if desired.
438 */
439 static void
440may_do_incsearch_highlighting(
441 int firstc,
442 long count,
443 incsearch_state_T *is_state)
444{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200445 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200446 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200447 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200448#ifdef FEAT_RELTIME
449 proftime_T tm;
450#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200451 int next_char;
452 int use_last_pat;
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200453 int did_do_incsearch = is_state->did_incsearch;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454
Bram Moolenaar198cb662018-09-06 21:44:17 +0200455 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100456 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200457 save_last_search_pattern();
458
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200459 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200460 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200461 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200462 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200463 if (did_do_incsearch && vpeekc() == NUL)
464 // may have skipped a redraw, do it now
465 redrawcmd();
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();
Bram Moolenaar69a5b862019-07-16 21:38:51 +0200716 curwin->w_cursor = is_state->match_end;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200717 }
718 else
719 vim_beep(BO_ERROR);
720 restore_last_search_pattern();
721 return FAIL;
722}
723
724/*
725 * When CTRL-L typed: add character from the match to the pattern.
726 * May set "*c" to the added character.
727 * Return OK when jumping to cmdline_not_changed.
728 */
729 static int
730may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
731{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200732 int skiplen, patlen;
733
Bram Moolenaar198cb662018-09-06 21:44:17 +0200734 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100735 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200736 save_last_search_pattern();
737
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200738 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200739 {
740 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200741 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200742 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100743 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200744
745 // Add a character from under the cursor for 'incsearch'.
746 if (is_state->did_incsearch)
747 {
748 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200749 *c = gchar_cursor();
750 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200752 // If 'ignorecase' and 'smartcase' are set and the
753 // command line has no uppercase characters, convert
754 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200755 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200756 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200757 if (*c == firstc || vim_strchr((char_u *)(
758 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200759 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200760 // put a backslash before special characters
761 stuffcharReadbuff(*c);
762 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200763 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200764 // add any composing characters
765 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
766 {
767 int save_c = *c;
768
769 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
770 {
771 curwin->w_cursor.col += mb_char2len(*c);
772 *c = gchar_cursor();
773 stuffcharReadbuff(*c);
774 }
775 *c = save_c;
776 }
777 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200778 }
779 }
780 return OK;
781}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200782#endif
783
Bram Moolenaar693f7dc2019-06-21 02:30:38 +0200784#ifdef FEAT_ARABIC
785/*
786 * Return TRUE if the command line has an Arabic character at or after "start"
787 * for "len" bytes.
788 */
789 static int
790cmdline_has_arabic(int start, int len)
791{
792 int j;
793 int mb_l;
794 int u8c;
795 char_u *p;
796 int u8cc[MAX_MCO];
797
798 if (!enc_utf8)
799 return FALSE;
800
801 for (j = start; j < start + len; j += mb_l)
802 {
803 p = ccline.cmdbuff + j;
804 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
805 mb_l = utfc_ptr2len_len(p, start + len - j);
806 if (ARABIC_CHAR(u8c))
807 return TRUE;
808 }
809 return FALSE;
810}
811#endif
812
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200813 void
814cmdline_init(void)
815{
816 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
817}
818
Bram Moolenaar66216052017-12-16 16:33:44 +0100819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 * getcmdline() - accept a command line starting with firstc.
821 *
822 * firstc == ':' get ":" command line.
823 * firstc == '/' or '?' get search pattern
824 * firstc == '=' get expression
825 * firstc == '@' get text for input() function
826 * firstc == '>' get text for debug mode
827 * firstc == NUL get text for :insert command
828 * firstc == -1 like NUL, and break on CTRL-C
829 *
830 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
831 * command line.
832 *
833 * Careful: getcmdline() can be called recursively!
834 *
835 * Return pointer to allocated string if there is a commandline, NULL
836 * otherwise.
837 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100839getcmdline(
840 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200841 long count, // only used for incremental search
Bram Moolenaare96a2492019-06-25 04:12:16 +0200842 int indent, // indent for inside conditionals
843 int do_concat UNUSED)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200844{
845 return getcmdline_int(firstc, count, indent, TRUE);
846}
847
848 static char_u *
849getcmdline_int(
850 int firstc,
851 long count UNUSED, // only used for incremental search
852 int indent, // indent for inside conditionals
853 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854{
855 int c;
856 int i;
857 int j;
858 int gotesc = FALSE; /* TRUE when <ESC> just typed */
859 int do_abbr; /* when TRUE check for abbr. */
860#ifdef FEAT_CMDHIST
861 char_u *lookfor = NULL; /* string to match */
862 int hiscnt; /* current history line in use */
863 int histype; /* history type to be used */
864#endif
865#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200866 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867#endif
868 int did_wild_list = FALSE; /* did wild_list() recently */
869 int wim_index = 0; /* index in wim_flags[] */
870 int res;
871 int save_msg_scroll = msg_scroll;
872 int save_State = State; /* remember State when called */
873 int some_key_typed = FALSE; /* one of the keys was typed */
874#ifdef FEAT_MOUSE
875 /* mouse drag and release events are ignored, unless they are
876 * preceded with a mouse down event */
877 int ignore_drag_release = TRUE;
878#endif
879#ifdef FEAT_EVAL
880 int break_ctrl_c = FALSE;
881#endif
882 expand_T xpc;
883 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000884 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200885 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200886 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887
Bram Moolenaar438d1762018-09-30 17:11:48 +0200888 if (ccline.cmdbuff != NULL)
889 {
890 // Being called recursively. Since ccline is global, we need to save
891 // the current buffer and restore it when returning.
892 save_cmdline(&save_ccline);
893 did_save_ccline = TRUE;
894 }
895 if (init_ccline)
896 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898#ifdef FEAT_EVAL
899 if (firstc == -1)
900 {
901 firstc = NUL;
902 break_ctrl_c = TRUE;
903 }
904#endif
905#ifdef FEAT_RIGHTLEFT
906 /* start without Hebrew mapping for a command line */
907 if (firstc == ':' || firstc == '=' || firstc == '>')
908 cmd_hkmap = 0;
909#endif
910
911 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200912
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200914 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915#endif
916
917 /*
918 * set some variables for redrawcmd()
919 */
920 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000921 ccline.cmdindent = (firstc > 0 ? indent : 0);
922
923 /* alloc initial ccline.cmdbuff */
924 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200926 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 ccline.cmdlen = ccline.cmdpos = 0;
928 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100929 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000931 /* autoindent for :insert and :append */
932 if (firstc <= 0)
933 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200934 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000935 ccline.cmdbuff[indent] = NUL;
936 ccline.cmdpos = indent;
937 ccline.cmdspos = indent;
938 ccline.cmdlen = indent;
939 }
940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000942 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943
944#ifdef FEAT_RIGHTLEFT
945 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
946 && (firstc == '/' || firstc == '?'))
947 cmdmsg_rl = TRUE;
948 else
949 cmdmsg_rl = FALSE;
950#endif
951
952 redir_off = TRUE; /* don't redirect the typed command */
953 if (!cmd_silent)
954 {
955 i = msg_scrolled;
956 msg_scrolled = 0; /* avoid wait_return message */
957 gotocmdline(TRUE);
958 msg_scrolled += i;
959 redrawcmdprompt(); /* draw prompt or indent */
960 set_cmdspos();
961 }
962 xpc.xp_context = EXPAND_NOTHING;
963 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000964#ifndef BACKSLASH_IN_FILENAME
965 xpc.xp_shell = FALSE;
966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000968#if defined(FEAT_EVAL)
969 if (ccline.input_fn)
970 {
971 xpc.xp_context = ccline.xp_context;
972 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200973# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000974 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000975# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000976 }
977#endif
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 /*
980 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
981 * doing ":@0" when register 0 doesn't contain a CR.
982 */
983 msg_scroll = FALSE;
984
985 State = CMDLINE;
986
987 if (firstc == '/' || firstc == '?' || firstc == '@')
988 {
989 /* Use ":lmap" mappings for search pattern and input(). */
990 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
991 b_im_ptr = &curbuf->b_p_iminsert;
992 else
993 b_im_ptr = &curbuf->b_p_imsearch;
994 if (*b_im_ptr == B_IMODE_LMAP)
995 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100996#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 im_set_active(*b_im_ptr == B_IMODE_IM);
998#endif
999 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001000#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001 else if (p_imcmdline)
1002 im_set_active(TRUE);
1003#endif
1004
1005#ifdef FEAT_MOUSE
1006 setmouse();
1007#endif
1008#ifdef CURSOR_SHAPE
1009 ui_cursor_shape(); /* may show different cursor shape */
1010#endif
1011
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001012 /* When inside an autocommand for writing "exiting" may be set and
1013 * terminal mode set to cooked. Need to set raw mode here then. */
1014 settmode(TMODE_RAW);
1015
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001016 /* Trigger CmdlineEnter autocommands. */
1017 cmdline_type = firstc == NUL ? '-' : firstc;
1018 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#ifdef FEAT_CMDHIST
1021 init_history();
1022 hiscnt = hislen; /* set hiscnt to impossible history value */
1023 histype = hist_char2type(firstc);
1024#endif
1025
1026#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +00001027 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028#endif
1029
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001030 /* If something above caused an error, reset the flags, we do want to type
1031 * and execute commands. Display may be messed up a bit. */
1032 if (did_emsg)
1033 redrawcmd();
1034 did_emsg = FALSE;
1035 got_int = FALSE;
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /*
1038 * Collect the command string, handling editing keys.
1039 */
1040 for (;;)
1041 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001042 redir_off = TRUE; /* Don't redirect the typed command.
1043 Repeated, because a ":redir" inside
1044 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045#ifdef USE_ON_FLY_SCROLL
1046 dont_scroll = FALSE; /* allow scrolling here */
1047#endif
1048 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1049
Bram Moolenaar72532d32018-04-07 19:09:09 +02001050 did_emsg = FALSE; /* There can't really be a reason why an error
1051 that occurs while typing a command should
1052 cause the command not to be executed. */
1053
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001055
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001056 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1057 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001058 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001059 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001060 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001061
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 if (KeyTyped)
1063 {
1064 some_key_typed = TRUE;
1065#ifdef FEAT_RIGHTLEFT
1066 if (cmd_hkmap)
1067 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 if (cmdmsg_rl && !KeyStuffed)
1069 {
1070 /* Invert horizontal movements and operations. Only when
1071 * typed by the user directly, not when the result of a
1072 * mapping. */
1073 switch (c)
1074 {
1075 case K_RIGHT: c = K_LEFT; break;
1076 case K_S_RIGHT: c = K_S_LEFT; break;
1077 case K_C_RIGHT: c = K_C_LEFT; break;
1078 case K_LEFT: c = K_RIGHT; break;
1079 case K_S_LEFT: c = K_S_RIGHT; break;
1080 case K_C_LEFT: c = K_C_RIGHT; break;
1081 }
1082 }
1083#endif
1084 }
1085
1086 /*
1087 * Ignore got_int when CTRL-C was typed here.
1088 * Don't ignore it in :global, we really need to break then, e.g., for
1089 * ":g/pat/normal /pat" (without the <CR>).
1090 * Don't ignore it for the input() function.
1091 */
1092 if ((c == Ctrl_C
1093#ifdef UNIX
1094 || c == intr_char
1095#endif
1096 )
1097#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1098 && firstc != '@'
1099#endif
1100#ifdef FEAT_EVAL
1101 && !break_ctrl_c
1102#endif
1103 && !global_busy)
1104 got_int = FALSE;
1105
1106#ifdef FEAT_CMDHIST
1107 /* free old command line when finished moving around in the history
1108 * list */
1109 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001110 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001111 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 && c != K_PAGEDOWN && c != K_PAGEUP
1113 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001114 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001116 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117#endif
1118
1119 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001120 * When there are matching completions to select <S-Tab> works like
1121 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001123 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 c = Ctrl_P;
1125
1126#ifdef FEAT_WILDMENU
1127 /* Special translations for 'wildmenu' */
1128 if (did_wild_list && p_wmnu)
1129 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001130 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001132 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 c = Ctrl_N;
1134 }
1135 /* Hitting CR after "emenu Name.": complete submenu */
1136 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1137 && ccline.cmdpos > 1
1138 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1139 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1140 && (c == '\n' || c == '\r' || c == K_KENTER))
1141 c = K_DOWN;
1142#endif
1143
1144 /* free expanded names when finished walking through matches */
1145 if (xpc.xp_numfiles != -1
1146 && !(c == p_wc && KeyTyped) && c != p_wcm
1147 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1148 && c != Ctrl_L)
1149 {
1150 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1151 did_wild_list = FALSE;
1152#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001153 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154#endif
1155 xpc.xp_context = EXPAND_NOTHING;
1156 wim_index = 0;
1157#ifdef FEAT_WILDMENU
1158 if (p_wmnu && wild_menu_showing != 0)
1159 {
1160 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001161 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001162
1163 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001164 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165
1166 if (wild_menu_showing == WM_SCROLLED)
1167 {
1168 /* Entered command line, move it up */
1169 cmdline_row--;
1170 redrawcmd();
1171 }
1172 else if (save_p_ls != -1)
1173 {
1174 /* restore 'laststatus' and 'winminheight' */
1175 p_ls = save_p_ls;
1176 p_wmh = save_p_wmh;
1177 last_status(FALSE);
1178 update_screen(VALID); /* redraw the screen NOW */
1179 redrawcmd();
1180 save_p_ls = -1;
1181 }
1182 else
1183 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 redraw_statuslines();
1186 }
1187 KeyTyped = skt;
1188 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001189 if (ccline.input_fn)
1190 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 }
1192#endif
1193 }
1194
1195#ifdef FEAT_WILDMENU
1196 /* Special translations for 'wildmenu' */
1197 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1198 {
1199 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001200 if (c == K_DOWN && ccline.cmdpos > 0
1201 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001203 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 {
1205 /* Hitting <Up>: Remove one submenu name in front of the
1206 * cursor */
1207 int found = FALSE;
1208
1209 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1210 i = 0;
1211 while (--j > 0)
1212 {
1213 /* check for start of menu name */
1214 if (ccline.cmdbuff[j] == ' '
1215 && ccline.cmdbuff[j - 1] != '\\')
1216 {
1217 i = j + 1;
1218 break;
1219 }
1220 /* check for start of submenu name */
1221 if (ccline.cmdbuff[j] == '.'
1222 && ccline.cmdbuff[j - 1] != '\\')
1223 {
1224 if (found)
1225 {
1226 i = j + 1;
1227 break;
1228 }
1229 else
1230 found = TRUE;
1231 }
1232 }
1233 if (i > 0)
1234 cmdline_del(i);
1235 c = p_wc;
1236 xpc.xp_context = EXPAND_NOTHING;
1237 }
1238 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001239 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001240 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001241 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 {
1243 char_u upseg[5];
1244
1245 upseg[0] = PATHSEP;
1246 upseg[1] = '.';
1247 upseg[2] = '.';
1248 upseg[3] = PATHSEP;
1249 upseg[4] = NUL;
1250
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001251 if (c == K_DOWN
1252 && ccline.cmdpos > 0
1253 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1254 && (ccline.cmdpos < 3
1255 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1257 {
1258 /* go down a directory */
1259 c = p_wc;
1260 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001261 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 /* If in a direct ancestor, strip off one ../ to go down */
1264 int found = FALSE;
1265
1266 j = ccline.cmdpos;
1267 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1268 while (--j > i)
1269 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001270 if (has_mbyte)
1271 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (vim_ispathsep(ccline.cmdbuff[j]))
1273 {
1274 found = TRUE;
1275 break;
1276 }
1277 }
1278 if (found
1279 && ccline.cmdbuff[j - 1] == '.'
1280 && ccline.cmdbuff[j - 2] == '.'
1281 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1282 {
1283 cmdline_del(j - 2);
1284 c = p_wc;
1285 }
1286 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001287 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 {
1289 /* go up a directory */
1290 int found = FALSE;
1291
1292 j = ccline.cmdpos - 1;
1293 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1294 while (--j > i)
1295 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 if (has_mbyte)
1297 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (vim_ispathsep(ccline.cmdbuff[j])
1299#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001300 && vim_strchr((char_u *)" *?[{`$%#",
1301 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302#endif
1303 )
1304 {
1305 if (found)
1306 {
1307 i = j + 1;
1308 break;
1309 }
1310 else
1311 found = TRUE;
1312 }
1313 }
1314
1315 if (!found)
1316 j = i;
1317 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1318 j += 4;
1319 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1320 && j == i)
1321 j += 3;
1322 else
1323 j = 0;
1324 if (j > 0)
1325 {
1326 /* TODO this is only for DOS/UNIX systems - need to put in
1327 * machine-specific stuff here and in upseg init */
1328 cmdline_del(j);
1329 put_on_cmdline(upseg + 1, 3, FALSE);
1330 }
1331 else if (ccline.cmdpos > i)
1332 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001333
1334 /* Now complete in the new directory. Set KeyTyped in case the
1335 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001337 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 }
1339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340
1341#endif /* FEAT_WILDMENU */
1342
1343 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1344 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1345 if (c == Ctrl_BSL)
1346 {
1347 ++no_mapping;
1348 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001349 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 --no_mapping;
1351 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001352 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1353 * is in a mapping. */
1354 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001355 || (ccline.cmdfirstc == '=' && KeyTyped)
1356#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001357 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001358#endif
1359 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
1361 vungetc(c);
1362 c = Ctrl_BSL;
1363 }
1364#ifdef FEAT_EVAL
1365 else if (c == 'e')
1366 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001367 char_u *p = NULL;
1368 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369
1370 /*
1371 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001372 * Need to save and restore the current command line, to be
1373 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 */
1375 if (ccline.cmdpos == ccline.cmdlen)
1376 new_cmdpos = 99999; /* keep it at the end */
1377 else
1378 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001379
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381 if (c == '=')
1382 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001384 * to avoid nasty things like going to another buffer when
1385 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001386 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001389
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001390 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001392 len = (int)STRLEN(p);
1393 if (realloc_cmdbuff(len + 1) == OK)
1394 {
1395 ccline.cmdlen = len;
1396 STRCPY(ccline.cmdbuff, p);
1397 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001399 /* Restore the cursor or use the position set with
1400 * set_cmdline_pos(). */
1401 if (new_cmdpos > ccline.cmdlen)
1402 ccline.cmdpos = ccline.cmdlen;
1403 else
1404 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001406 KeyTyped = FALSE; /* Don't do p_wc completion. */
1407 redrawcmd();
1408 goto cmdline_changed;
1409 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001410 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 }
1412 }
1413 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001414 got_int = FALSE; /* don't abandon the command line */
1415 did_emsg = FALSE;
1416 emsg_on_display = FALSE;
1417 redrawcmd();
1418 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420#endif
1421 else
1422 {
1423 if (c == Ctrl_G && p_im && restart_edit == 0)
1424 restart_edit = 'a';
1425 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1426 in history */
1427 goto returncmd; /* back to Normal mode */
1428 }
1429 }
1430
1431#ifdef FEAT_CMDWIN
1432 if (c == cedit_key || c == K_CMDWIN)
1433 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001434 if (ex_normal_busy == 0 && got_int == FALSE)
1435 {
1436 /*
1437 * Open a window to edit the command line (and history).
1438 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001439 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001440 some_key_typed = TRUE;
1441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443# ifdef FEAT_DIGRAPHS
1444 else
1445# endif
1446#endif
1447#ifdef FEAT_DIGRAPHS
1448 c = do_digraph(c);
1449#endif
1450
1451 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1452 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1453 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001454 /* In Ex mode a backslash escapes a newline. */
1455 if (exmode_active
1456 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001457 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001458 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001459 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001461 if (c == K_KENTER)
1462 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001464 else
1465 {
1466 gotesc = FALSE; /* Might have typed ESC previously, don't
1467 truncate the cmdline now. */
1468 if (ccheck_abbr(c + ABBR_OFF))
1469 goto cmdline_changed;
1470 if (!cmd_silent)
1471 {
1472 windgoto(msg_row, 0);
1473 out_flush();
1474 }
1475 break;
1476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477 }
1478
1479 /*
1480 * Completion for 'wildchar' or 'wildcharm' key.
1481 * - hitting <ESC> twice means: abandon command line.
1482 * - wildcard expansion is only done when the 'wildchar' key is really
1483 * typed, not when it comes from a macro
1484 */
1485 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1486 {
1487 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1488 {
1489 /* if 'wildmode' contains "list" may still need to list */
1490 if (xpc.xp_numfiles > 1
1491 && !did_wild_list
1492 && (wim_flags[wim_index] & WIM_LIST))
1493 {
1494 (void)showmatches(&xpc, FALSE);
1495 redrawcmd();
1496 did_wild_list = TRUE;
1497 }
1498 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001499 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1500 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001502 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1503 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 else
1505 res = OK; /* don't insert 'wildchar' now */
1506 }
1507 else /* typed p_wc first time */
1508 {
1509 wim_index = 0;
1510 j = ccline.cmdpos;
1511 /* if 'wildmode' first contains "longest", get longest
1512 * common part */
1513 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001514 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1515 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001516 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001517 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1518 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519
1520 /* if interrupted while completing, behave like it failed */
1521 if (got_int)
1522 {
1523 (void)vpeekc(); /* remove <C-C> from input stream */
1524 got_int = FALSE; /* don't abandon the command line */
1525 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1526#ifdef FEAT_WILDMENU
1527 xpc.xp_context = EXPAND_NOTHING;
1528#endif
1529 goto cmdline_changed;
1530 }
1531
1532 /* when more than one match, and 'wildmode' first contains
1533 * "list", or no change and 'wildmode' contains "longest,list",
1534 * list all matches */
1535 if (res == OK && xpc.xp_numfiles > 1)
1536 {
1537 /* a "longest" that didn't do anything is skipped (but not
1538 * "list:longest") */
1539 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1540 wim_index = 1;
1541 if ((wim_flags[wim_index] & WIM_LIST)
1542#ifdef FEAT_WILDMENU
1543 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1544#endif
1545 )
1546 {
1547 if (!(wim_flags[0] & WIM_LONGEST))
1548 {
1549#ifdef FEAT_WILDMENU
1550 int p_wmnu_save = p_wmnu;
1551 p_wmnu = 0;
1552#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001553 /* remove match */
1554 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555#ifdef FEAT_WILDMENU
1556 p_wmnu = p_wmnu_save;
1557#endif
1558 }
1559#ifdef FEAT_WILDMENU
1560 (void)showmatches(&xpc, p_wmnu
1561 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1562#else
1563 (void)showmatches(&xpc, FALSE);
1564#endif
1565 redrawcmd();
1566 did_wild_list = TRUE;
1567 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001568 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1569 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001571 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1572 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001575 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 }
1577#ifdef FEAT_WILDMENU
1578 else if (xpc.xp_numfiles == -1)
1579 xpc.xp_context = EXPAND_NOTHING;
1580#endif
1581 }
1582 if (wim_index < 3)
1583 ++wim_index;
1584 if (c == ESC)
1585 gotesc = TRUE;
1586 if (res == OK)
1587 goto cmdline_changed;
1588 }
1589
1590 gotesc = FALSE;
1591
1592 /* <S-Tab> goes to last match, in a clumsy way */
1593 if (c == K_S_TAB && KeyTyped)
1594 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001595 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1596 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1597 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 goto cmdline_changed;
1599 }
1600
1601 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1602 c = NL;
1603
1604 do_abbr = TRUE; /* default: check for abbreviation */
1605
1606 /*
1607 * Big switch for a typed command line character.
1608 */
1609 switch (c)
1610 {
1611 case K_BS:
1612 case Ctrl_H:
1613 case K_DEL:
1614 case K_KDEL:
1615 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 if (c == K_KDEL)
1617 c = K_DEL;
1618
1619 /*
1620 * delete current character is the same as backspace on next
1621 * character, except at end of line
1622 */
1623 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1624 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 if (has_mbyte && c == K_DEL)
1626 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1627 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 if (ccline.cmdpos > 0)
1629 {
1630 char_u *p;
1631
1632 j = ccline.cmdpos;
1633 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 if (has_mbyte)
1635 {
1636 p = mb_prevptr(ccline.cmdbuff, p);
1637 if (c == Ctrl_W)
1638 {
1639 while (p > ccline.cmdbuff && vim_isspace(*p))
1640 p = mb_prevptr(ccline.cmdbuff, p);
1641 i = mb_get_class(p);
1642 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1643 p = mb_prevptr(ccline.cmdbuff, p);
1644 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001645 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001648 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1651 --p;
1652 i = vim_iswordc(p[-1]);
1653 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1654 && vim_iswordc(p[-1]) == i)
1655 --p;
1656 }
1657 else
1658 --p;
1659 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1660 ccline.cmdlen -= j - ccline.cmdpos;
1661 i = ccline.cmdpos;
1662 while (i < ccline.cmdlen)
1663 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1664
1665 /* Truncate at the end, required for multi-byte chars. */
1666 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001667#ifdef FEAT_SEARCH_EXTRA
1668 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001669 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001670 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001671 /* save view settings, so that the screen
1672 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001673 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001674 }
1675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 redrawcmd();
1677 }
1678 else if (ccline.cmdlen == 0 && c != Ctrl_W
1679 && ccline.cmdprompt == NULL && indent == 0)
1680 {
1681 /* In ex and debug mode it doesn't make sense to return. */
1682 if (exmode_active
1683#ifdef FEAT_EVAL
1684 || ccline.cmdfirstc == '>'
1685#endif
1686 )
1687 goto cmdline_not_changed;
1688
Bram Moolenaard23a8232018-02-10 18:45:26 +01001689 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 if (!cmd_silent)
1691 {
1692#ifdef FEAT_RIGHTLEFT
1693 if (cmdmsg_rl)
1694 msg_col = Columns;
1695 else
1696#endif
1697 msg_col = 0;
1698 msg_putchar(' '); /* delete ':' */
1699 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001700#ifdef FEAT_SEARCH_EXTRA
1701 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001702 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 redraw_cmdline = TRUE;
1705 goto returncmd; /* back to cmd mode */
1706 }
1707 goto cmdline_changed;
1708
1709 case K_INS:
1710 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 ccline.overstrike = !ccline.overstrike;
1712#ifdef CURSOR_SHAPE
1713 ui_cursor_shape(); /* may show different cursor shape */
1714#endif
1715 goto cmdline_not_changed;
1716
1717 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001718 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
1720 /* ":lmap" mappings exists, toggle use of mappings. */
1721 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001722#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 im_set_active(FALSE); /* Disable input method */
1724#endif
1725 if (b_im_ptr != NULL)
1726 {
1727 if (State & LANGMAP)
1728 *b_im_ptr = B_IMODE_LMAP;
1729 else
1730 *b_im_ptr = B_IMODE_NONE;
1731 }
1732 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001733#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 else
1735 {
1736 /* There are no ":lmap" mappings, toggle IM. When
1737 * 'imdisable' is set don't try getting the status, it's
1738 * always off. */
1739 if ((p_imdisable && b_im_ptr != NULL)
1740 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1741 {
1742 im_set_active(FALSE); /* Disable input method */
1743 if (b_im_ptr != NULL)
1744 *b_im_ptr = B_IMODE_NONE;
1745 }
1746 else
1747 {
1748 im_set_active(TRUE); /* Enable input method */
1749 if (b_im_ptr != NULL)
1750 *b_im_ptr = B_IMODE_IM;
1751 }
1752 }
1753#endif
1754 if (b_im_ptr != NULL)
1755 {
1756 if (b_im_ptr == &curbuf->b_p_iminsert)
1757 set_iminsert_global();
1758 else
1759 set_imsearch_global();
1760 }
1761#ifdef CURSOR_SHAPE
1762 ui_cursor_shape(); /* may show different cursor shape */
1763#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001764#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 /* Show/unshow value of 'keymap' in status lines later. */
1766 status_redraw_curbuf();
1767#endif
1768 goto cmdline_not_changed;
1769
1770/* case '@': only in very old vi */
1771 case Ctrl_U:
1772 /* delete all characters left of the cursor */
1773 j = ccline.cmdpos;
1774 ccline.cmdlen -= j;
1775 i = ccline.cmdpos = 0;
1776 while (i < ccline.cmdlen)
1777 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1778 /* Truncate at the end, required for multi-byte chars. */
1779 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001780#ifdef FEAT_SEARCH_EXTRA
1781 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001782 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 redrawcmd();
1785 goto cmdline_changed;
1786
1787#ifdef FEAT_CLIPBOARD
1788 case Ctrl_Y:
1789 /* Copy the modeless selection, if there is one. */
1790 if (clip_star.state != SELECT_CLEARED)
1791 {
1792 if (clip_star.state == SELECT_DONE)
1793 clip_copy_modeless_selection(TRUE);
1794 goto cmdline_not_changed;
1795 }
1796 break;
1797#endif
1798
1799 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1800 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001801 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001802 * ":normal" runs out of characters. */
1803 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001804 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 goto cmdline_not_changed;
1806
1807 gotesc = TRUE; /* will free ccline.cmdbuff after
1808 putting it in history */
1809 goto returncmd; /* back to cmd mode */
1810
1811 case Ctrl_R: /* insert register */
1812#ifdef USE_ON_FLY_SCROLL
1813 dont_scroll = TRUE; /* disallow scrolling here */
1814#endif
1815 putcmdline('"', TRUE);
1816 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001817 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 if (i == Ctrl_O)
1819 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1820 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001821 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001822 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 --no_mapping;
1824#ifdef FEAT_EVAL
1825 /*
1826 * Insert the result of an expression.
1827 * Need to save the current command line, to be able to enter
1828 * a new one...
1829 */
1830 new_cmdpos = -1;
1831 if (c == '=')
1832 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001833 if (ccline.cmdfirstc == '=' // can't do this recursively
1834 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 {
1836 beep_flush();
1837 c = ESC;
1838 }
1839 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 }
1842#endif
1843 if (c != ESC) /* use ESC to cancel inserting register */
1844 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001845 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001846
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001847#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001848 /* When there was a serious error abort getting the
1849 * command line. */
1850 if (aborting())
1851 {
1852 gotesc = TRUE; /* will free ccline.cmdbuff after
1853 putting it in history */
1854 goto returncmd; /* back to cmd mode */
1855 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 KeyTyped = FALSE; /* Don't do p_wc completion. */
1858#ifdef FEAT_EVAL
1859 if (new_cmdpos >= 0)
1860 {
1861 /* set_cmdline_pos() was used */
1862 if (new_cmdpos > ccline.cmdlen)
1863 ccline.cmdpos = ccline.cmdlen;
1864 else
1865 ccline.cmdpos = new_cmdpos;
1866 }
1867#endif
1868 }
1869 redrawcmd();
1870 goto cmdline_changed;
1871
1872 case Ctrl_D:
1873 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1874 break; /* Use ^D as normal char instead */
1875
1876 redrawcmd();
1877 continue; /* don't do incremental search now */
1878
1879 case K_RIGHT:
1880 case K_S_RIGHT:
1881 case K_C_RIGHT:
1882 do
1883 {
1884 if (ccline.cmdpos >= ccline.cmdlen)
1885 break;
1886 i = cmdline_charsize(ccline.cmdpos);
1887 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1888 break;
1889 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001891 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 + ccline.cmdpos);
1893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 ++ccline.cmdpos;
1895 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001896 while ((c == K_S_RIGHT || c == K_C_RIGHT
1897 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 if (has_mbyte)
1900 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 goto cmdline_not_changed;
1902
1903 case K_LEFT:
1904 case K_S_LEFT:
1905 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001906 if (ccline.cmdpos == 0)
1907 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 do
1909 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911 if (has_mbyte) /* move to first byte of char */
1912 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1913 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1915 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001916 while (ccline.cmdpos > 0
1917 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001918 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 if (has_mbyte)
1921 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 goto cmdline_not_changed;
1923
1924 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001925 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001926 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927
Bram Moolenaar4f974752019-02-17 17:44:42 +01001928#ifdef FEAT_GUI_MSWIN
1929 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1930 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001931 case K_F4:
1932 if (mod_mask == MOD_MASK_ALT)
1933 {
1934 redrawcmd(); /* somehow the cmdline is cleared */
1935 goto cmdline_not_changed;
1936 }
1937 break;
1938#endif
1939
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940#ifdef FEAT_MOUSE
1941 case K_MIDDLEDRAG:
1942 case K_MIDDLERELEASE:
1943 goto cmdline_not_changed; /* Ignore mouse */
1944
1945 case K_MIDDLEMOUSE:
1946# ifdef FEAT_GUI
1947 /* When GUI is active, also paste when 'mouse' is empty */
1948 if (!gui.in_use)
1949# endif
1950 if (!mouse_has(MOUSE_COMMAND))
1951 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001952# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001954 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001956# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001957 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 redrawcmd();
1959 goto cmdline_changed;
1960
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001961# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001963 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 redrawcmd();
1965 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001966# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 case K_LEFTDRAG:
1969 case K_LEFTRELEASE:
1970 case K_RIGHTDRAG:
1971 case K_RIGHTRELEASE:
1972 /* Ignore drag and release events when the button-down wasn't
1973 * seen before. */
1974 if (ignore_drag_release)
1975 goto cmdline_not_changed;
1976 /* FALLTHROUGH */
1977 case K_LEFTMOUSE:
1978 case K_RIGHTMOUSE:
1979 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1980 ignore_drag_release = TRUE;
1981 else
1982 ignore_drag_release = FALSE;
1983# ifdef FEAT_GUI
1984 /* When GUI is active, also move when 'mouse' is empty */
1985 if (!gui.in_use)
1986# endif
1987 if (!mouse_has(MOUSE_COMMAND))
1988 goto cmdline_not_changed; /* Ignore mouse */
1989# ifdef FEAT_CLIPBOARD
1990 if (mouse_row < cmdline_row && clip_star.available)
1991 {
1992 int button, is_click, is_drag;
1993
1994 /*
1995 * Handle modeless selection.
1996 */
1997 button = get_mouse_button(KEY2TERMCAP1(c),
1998 &is_click, &is_drag);
1999 if (mouse_model_popup() && button == MOUSE_LEFT
2000 && (mod_mask & MOD_MASK_SHIFT))
2001 {
2002 /* Translate shift-left to right button. */
2003 button = MOUSE_RIGHT;
2004 mod_mask &= ~MOD_MASK_SHIFT;
2005 }
2006 clip_modeless(button, is_click, is_drag);
2007 goto cmdline_not_changed;
2008 }
2009# endif
2010
2011 set_cmdspos();
2012 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2013 ++ccline.cmdpos)
2014 {
2015 i = cmdline_charsize(ccline.cmdpos);
2016 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2017 && mouse_col < ccline.cmdspos % Columns + i)
2018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (has_mbyte)
2020 {
2021 /* Count ">" for double-wide char that doesn't fit. */
2022 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002023 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 + ccline.cmdpos) - 1;
2025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 ccline.cmdspos += i;
2027 }
2028 goto cmdline_not_changed;
2029
2030 /* Mouse scroll wheel: ignored here */
2031 case K_MOUSEDOWN:
2032 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002033 case K_MOUSELEFT:
2034 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 /* Alternate buttons ignored here */
2036 case K_X1MOUSE:
2037 case K_X1DRAG:
2038 case K_X1RELEASE:
2039 case K_X2MOUSE:
2040 case K_X2DRAG:
2041 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002042 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 goto cmdline_not_changed;
2044
2045#endif /* FEAT_MOUSE */
2046
2047#ifdef FEAT_GUI
2048 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2049 case K_LEFTRELEASE_NM:
2050 goto cmdline_not_changed;
2051
2052 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002053 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 {
2055 gui_do_scroll();
2056 redrawcmd();
2057 }
2058 goto cmdline_not_changed;
2059
2060 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002061 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002063 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 redrawcmd();
2065 }
2066 goto cmdline_not_changed;
2067#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002068#ifdef FEAT_GUI_TABLINE
2069 case K_TABLINE:
2070 case K_TABMENU:
2071 /* Don't want to change any tabs here. Make sure the same tab
2072 * is still selected. */
2073 if (gui_use_tabline())
2074 gui_mch_set_curtab(tabpage_index(curtab));
2075 goto cmdline_not_changed;
2076#endif
2077
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 case K_SELECT: /* end of Select mode mapping - ignore */
2079 goto cmdline_not_changed;
2080
2081 case Ctrl_B: /* begin of command line */
2082 case K_HOME:
2083 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 case K_S_HOME:
2085 case K_C_HOME:
2086 ccline.cmdpos = 0;
2087 set_cmdspos();
2088 goto cmdline_not_changed;
2089
2090 case Ctrl_E: /* end of command line */
2091 case K_END:
2092 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 case K_S_END:
2094 case K_C_END:
2095 ccline.cmdpos = ccline.cmdlen;
2096 set_cmdspos_cursor();
2097 goto cmdline_not_changed;
2098
2099 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002100 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 break;
2102 goto cmdline_changed;
2103
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002104 case Ctrl_L:
2105#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002106 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002107 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002108#endif
2109
2110 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002111 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002112 break;
2113 goto cmdline_changed;
2114
2115 case Ctrl_N: /* next match */
2116 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002117 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002119 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2120 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002122 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002125 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 case K_UP:
2127 case K_DOWN:
2128 case K_S_UP:
2129 case K_S_DOWN:
2130 case K_PAGEUP:
2131 case K_KPAGEUP:
2132 case K_PAGEDOWN:
2133 case K_KPAGEDOWN:
2134 if (hislen == 0 || firstc == NUL) /* no history */
2135 goto cmdline_not_changed;
2136
2137 i = hiscnt;
2138
2139 /* save current command string so it can be restored later */
2140 if (lookfor == NULL)
2141 {
2142 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2143 goto cmdline_not_changed;
2144 lookfor[ccline.cmdpos] = NUL;
2145 }
2146
2147 j = (int)STRLEN(lookfor);
2148 for (;;)
2149 {
2150 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002151 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002152 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 {
2154 if (hiscnt == hislen) /* first time */
2155 hiscnt = hisidx[histype];
2156 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2157 hiscnt = hislen - 1;
2158 else if (hiscnt != hisidx[histype] + 1)
2159 --hiscnt;
2160 else /* at top of list */
2161 {
2162 hiscnt = i;
2163 break;
2164 }
2165 }
2166 else /* one step forwards */
2167 {
2168 /* on last entry, clear the line */
2169 if (hiscnt == hisidx[histype])
2170 {
2171 hiscnt = hislen;
2172 break;
2173 }
2174
2175 /* not on a history line, nothing to do */
2176 if (hiscnt == hislen)
2177 break;
2178 if (hiscnt == hislen - 1) /* wrap around */
2179 hiscnt = 0;
2180 else
2181 ++hiscnt;
2182 }
2183 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2184 {
2185 hiscnt = i;
2186 break;
2187 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002188 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002189 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 || STRNCMP(history[histype][hiscnt].hisstr,
2191 lookfor, (size_t)j) == 0)
2192 break;
2193 }
2194
2195 if (hiscnt != i) /* jumped to other entry */
2196 {
2197 char_u *p;
2198 int len;
2199 int old_firstc;
2200
Bram Moolenaar438d1762018-09-30 17:11:48 +02002201 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002202 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 if (hiscnt == hislen)
2204 p = lookfor; /* back to the old one */
2205 else
2206 p = history[histype][hiscnt].hisstr;
2207
2208 if (histype == HIST_SEARCH
2209 && p != lookfor
2210 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2211 {
2212 /* Correct for the separator character used when
2213 * adding the history entry vs the one used now.
2214 * First loop: count length.
2215 * Second loop: copy the characters. */
2216 for (i = 0; i <= 1; ++i)
2217 {
2218 len = 0;
2219 for (j = 0; p[j] != NUL; ++j)
2220 {
2221 /* Replace old sep with new sep, unless it is
2222 * escaped. */
2223 if (p[j] == old_firstc
2224 && (j == 0 || p[j - 1] != '\\'))
2225 {
2226 if (i > 0)
2227 ccline.cmdbuff[len] = firstc;
2228 }
2229 else
2230 {
2231 /* Escape new sep, unless it is already
2232 * escaped. */
2233 if (p[j] == firstc
2234 && (j == 0 || p[j - 1] != '\\'))
2235 {
2236 if (i > 0)
2237 ccline.cmdbuff[len] = '\\';
2238 ++len;
2239 }
2240 if (i > 0)
2241 ccline.cmdbuff[len] = p[j];
2242 }
2243 ++len;
2244 }
2245 if (i == 0)
2246 {
2247 alloc_cmdbuff(len);
2248 if (ccline.cmdbuff == NULL)
2249 goto returncmd;
2250 }
2251 }
2252 ccline.cmdbuff[len] = NUL;
2253 }
2254 else
2255 {
2256 alloc_cmdbuff((int)STRLEN(p));
2257 if (ccline.cmdbuff == NULL)
2258 goto returncmd;
2259 STRCPY(ccline.cmdbuff, p);
2260 }
2261
2262 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2263 redrawcmd();
2264 goto cmdline_changed;
2265 }
2266 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002267#endif
2268 goto cmdline_not_changed;
2269
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002270#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002271 case Ctrl_G: /* next match */
2272 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002273 if (may_adjust_incsearch_highlighting(
2274 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002275 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002276 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277#endif
2278
2279 case Ctrl_V:
2280 case Ctrl_Q:
2281#ifdef FEAT_MOUSE
2282 ignore_drag_release = TRUE;
2283#endif
2284 putcmdline('^', TRUE);
2285 c = get_literal(); /* get next (two) character(s) */
2286 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002287 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 /* may need to remove ^ when composing char was typed */
2289 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2290 {
2291 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2292 msg_putchar(' ');
2293 cursorcmd();
2294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 break;
2296
2297#ifdef FEAT_DIGRAPHS
2298 case Ctrl_K:
2299#ifdef FEAT_MOUSE
2300 ignore_drag_release = TRUE;
2301#endif
2302 putcmdline('?', TRUE);
2303#ifdef USE_ON_FLY_SCROLL
2304 dont_scroll = TRUE; /* disallow scrolling here */
2305#endif
2306 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002307 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (c != NUL)
2309 break;
2310
2311 redrawcmd();
2312 goto cmdline_not_changed;
2313#endif /* FEAT_DIGRAPHS */
2314
2315#ifdef FEAT_RIGHTLEFT
2316 case Ctrl__: /* CTRL-_: switch language mode */
2317 if (!p_ari)
2318 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002319 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 goto cmdline_not_changed;
2321#endif
2322
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002323 case K_PS:
2324 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2325 goto cmdline_changed;
2326
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 default:
2328#ifdef UNIX
2329 if (c == intr_char)
2330 {
2331 gotesc = TRUE; /* will free ccline.cmdbuff after
2332 putting it in history */
2333 goto returncmd; /* back to Normal mode */
2334 }
2335#endif
2336 /*
2337 * Normal character with no special meaning. Just set mod_mask
2338 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2339 * the string <S-Space>. This should only happen after ^V.
2340 */
2341 if (!IS_SPECIAL(c))
2342 mod_mask = 0x0;
2343 break;
2344 }
2345 /*
2346 * End of switch on command line character.
2347 * We come here if we have a normal character.
2348 */
2349
Bram Moolenaar13505972019-01-24 15:04:48 +01002350 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2351 && (ccheck_abbr(
2352 // Add ABBR_OFF for characters above 0x100, this is
2353 // what check_abbr() expects.
2354 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2355 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 goto cmdline_changed;
2357
2358 /*
2359 * put the character in the command line
2360 */
2361 if (IS_SPECIAL(c) || mod_mask != 0)
2362 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2363 else
2364 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 if (has_mbyte)
2366 {
2367 j = (*mb_char2bytes)(c, IObuff);
2368 IObuff[j] = NUL; /* exclude composing chars */
2369 put_on_cmdline(IObuff, j, TRUE);
2370 }
2371 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 IObuff[0] = c;
2374 put_on_cmdline(IObuff, 1, TRUE);
2375 }
2376 }
2377 goto cmdline_changed;
2378
2379/*
2380 * This part implements incremental searches for "/" and "?"
2381 * Jump to cmdline_not_changed when a character has been read but the command
2382 * line did not change. Then we only search and redraw if something changed in
2383 * the past.
2384 * Jump to cmdline_changed when the command line did change.
2385 * (Sorry for the goto's, I know it is ugly).
2386 */
2387cmdline_not_changed:
2388#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002389 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002390 continue;
2391#endif
2392
2393cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002394 /* Trigger CmdlineChanged autocommands. */
2395 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002396
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002398 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399#endif
2400
2401#ifdef FEAT_RIGHTLEFT
2402 if (cmdmsg_rl
2403# ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02002404 || (p_arshape && !p_tbidi
2405 && cmdline_has_arabic(0, ccline.cmdlen))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406# endif
2407 )
2408 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002409 * right-left typing. Not efficient, but it works.
2410 * Do it only when there are no characters left to read
2411 * to avoid useless intermediate redraws. */
2412 if (vpeekc() == NUL)
2413 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#endif
2415 }
2416
2417returncmd:
2418
2419#ifdef FEAT_RIGHTLEFT
2420 cmdmsg_rl = FALSE;
2421#endif
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002424 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425
2426#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002427 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428#endif
2429
2430 if (ccline.cmdbuff != NULL)
2431 {
2432 /*
2433 * Put line in history buffer (":" and "=" only when it was typed).
2434 */
2435#ifdef FEAT_CMDHIST
2436 if (ccline.cmdlen && firstc != NUL
2437 && (some_key_typed || histype == HIST_SEARCH))
2438 {
2439 add_to_history(histype, ccline.cmdbuff, TRUE,
2440 histype == HIST_SEARCH ? firstc : NUL);
2441 if (firstc == ':')
2442 {
2443 vim_free(new_last_cmdline);
2444 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2445 }
2446 }
2447#endif
2448
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002449 if (gotesc)
2450 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 }
2452
2453 /*
2454 * If the screen was shifted up, redraw the whole screen (later).
2455 * If the line is too long, clear it, so ruler and shown command do
2456 * not get printed in the middle of it.
2457 */
2458 msg_check();
2459 msg_scroll = save_msg_scroll;
2460 redir_off = FALSE;
2461
2462 /* When the command line was typed, no need for a wait-return prompt. */
2463 if (some_key_typed)
2464 need_wait_return = FALSE;
2465
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002466 /* Trigger CmdlineLeave autocommands. */
2467 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002470#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2472 im_save_status(b_im_ptr);
2473 im_set_active(FALSE);
2474#endif
2475#ifdef FEAT_MOUSE
2476 setmouse();
2477#endif
2478#ifdef CURSOR_SHAPE
2479 ui_cursor_shape(); /* may show different cursor shape */
2480#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002481 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar438d1762018-09-30 17:11:48 +02002483theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002484 {
2485 char_u *p = ccline.cmdbuff;
2486
Bram Moolenaar438d1762018-09-30 17:11:48 +02002487 if (did_save_ccline)
2488 restore_cmdline(&save_ccline);
2489 else
2490 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002491 return p;
2492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493}
2494
2495#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2496/*
2497 * Get a command line with a prompt.
2498 * This is prepared to be called recursively from getcmdline() (e.g. by
2499 * f_input() when evaluating an expression from CTRL-R =).
2500 * Returns the command line in allocated memory, or NULL.
2501 */
2502 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002503getcmdline_prompt(
2504 int firstc,
2505 char_u *prompt, /* command line prompt */
2506 int attr, /* attributes for prompt */
2507 int xp_context, /* type of expansion */
2508 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 char_u *s;
2511 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002512 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002514 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515
Bram Moolenaar438d1762018-09-30 17:11:48 +02002516 if (ccline.cmdbuff != NULL)
2517 {
2518 // Save the values of the current cmdline and restore them below.
2519 save_cmdline(&save_ccline);
2520 did_save_ccline = TRUE;
2521 }
2522
2523 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 ccline.cmdprompt = prompt;
2525 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002526# ifdef FEAT_EVAL
2527 ccline.xp_context = xp_context;
2528 ccline.xp_arg = xp_arg;
2529 ccline.input_fn = (firstc == '@');
2530# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002531 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002532 s = getcmdline_int(firstc, 1L, 0, FALSE);
2533
2534 if (did_save_ccline)
2535 restore_cmdline(&save_ccline);
2536
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002537 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002538 /* Restore msg_col, the prompt from input() may have changed it.
2539 * But only if called recursively and the commandline is therefore being
2540 * restored to an old one; if not, the input() prompt stays on the screen,
2541 * so we need its modified msg_col left intact. */
2542 if (ccline.cmdbuff != NULL)
2543 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 return s;
2546}
2547#endif
2548
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002549/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002550 * Return TRUE when the text must not be changed and we can't switch to
2551 * another window or buffer. Used when editing the command line, evaluating
2552 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002553 */
2554 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002555text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002556{
2557#ifdef FEAT_CMDWIN
2558 if (cmdwin_type != 0)
2559 return TRUE;
2560#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002561 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002562}
2563
2564/*
2565 * Give an error message for a command that isn't allowed while the cmdline
2566 * window is open or editing the cmdline in another way.
2567 */
2568 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002569text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002570{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002571 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002572}
2573
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002574 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002575get_text_locked_msg(void)
2576{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002577#ifdef FEAT_CMDWIN
2578 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002579 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002580#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002581 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002582}
2583
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002584/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002585 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2586 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002587 */
2588 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002589curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002590{
2591 if (curbuf_lock > 0)
2592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002593 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002594 return TRUE;
2595 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002596 return allbuf_locked();
2597}
2598
2599/*
2600 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2601 * message.
2602 */
2603 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002604allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002605{
2606 if (allbuf_lock > 0)
2607 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002608 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002609 return TRUE;
2610 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002611 return FALSE;
2612}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002613
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002615cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616{
2617#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2618 if (cmdline_star > 0) /* showing '*', always 1 position */
2619 return 1;
2620#endif
2621 return ptr2cells(ccline.cmdbuff + idx);
2622}
2623
2624/*
2625 * Compute the offset of the cursor on the command line for the prompt and
2626 * indent.
2627 */
2628 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002629set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002631 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 ccline.cmdspos = 1 + ccline.cmdindent;
2633 else
2634 ccline.cmdspos = 0 + ccline.cmdindent;
2635}
2636
2637/*
2638 * Compute the screen position for the cursor on the command line.
2639 */
2640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002641set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
2643 int i, m, c;
2644
2645 set_cmdspos();
2646 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002649 if (m < 0) /* overflow, Columns or Rows at weird value */
2650 m = MAXCOL;
2651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 else
2653 m = MAXCOL;
2654 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2655 {
2656 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2658 if (has_mbyte)
2659 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002660 /* If the cmdline doesn't fit, show cursor on last visible char.
2661 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 if ((ccline.cmdspos += c) >= m)
2663 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 ccline.cmdspos -= c;
2665 break;
2666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002668 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
2670}
2671
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672/*
2673 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2674 * character that doesn't fit, so that a ">" must be displayed.
2675 */
2676 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002677correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002679 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2681 && ccline.cmdspos % Columns + cells > Columns)
2682 ccline.cmdspos++;
2683}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684
2685/*
2686 * Get an Ex command line for the ":" command.
2687 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002689getexline(
2690 int c, /* normally ':', NUL for ":append" */
2691 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002692 int indent, /* indent for inside conditionals */
2693 int do_concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 /* When executing a register, remove ':' that's in front of each line. */
2696 if (exec_from_reg && vpeekc() == ':')
2697 (void)vgetc();
Bram Moolenaare96a2492019-06-25 04:12:16 +02002698 return getcmdline(c, 1L, indent, do_concat);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699}
2700
2701/*
2702 * Get an Ex command line for Ex mode.
2703 * In Ex mode we only use the OS supplied line editing features and no
2704 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002705 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002708getexmodeline(
2709 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002710 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002711 void *cookie UNUSED,
Bram Moolenaare96a2492019-06-25 04:12:16 +02002712 int indent, /* indent for inside conditionals */
2713 int do_concat UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002715 garray_T line_ga;
2716 char_u *pend;
2717 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002718 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002719 int escaped = FALSE; /* CTRL-V typed */
2720 int vcol = 0;
2721 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002722 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002723 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 /* Switch cursor on now. This avoids that it happens after the "\n", which
2726 * confuses the system function that computes tabstops. */
2727 cursor_on();
2728
2729 /* always start in column 0; write a newline if necessary */
2730 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002731 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002735 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002736 if (p_prompt)
2737 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 while (indent-- > 0)
2739 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 }
2742
2743 ga_init2(&line_ga, 1, 30);
2744
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002745 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002746 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002747 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002748 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002749 while (indent >= 8)
2750 {
2751 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002752 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002753 indent -= 8;
2754 }
2755 while (indent-- > 0)
2756 {
2757 ga_append(&line_ga, ' ');
2758 msg_putchar(' ');
2759 }
2760 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002761 ++no_mapping;
2762 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002763
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 /*
2765 * Get the line, one character at a time.
2766 */
2767 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002770 long sw;
2771 char_u *s;
2772
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 if (ga_grow(&line_ga, 40) == FAIL)
2774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775
Bram Moolenaarba748c82017-02-26 14:00:07 +01002776 /*
2777 * Get one character at a time.
2778 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002779 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002780
2781 /* Check for a ":normal" command and no more characters left. */
2782 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2783 c1 = '\n';
2784 else
2785 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786
2787 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002788 * Handle line editing.
2789 * Previously this was left to the system, putting the terminal in
2790 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002794 msg_putchar('\n');
2795 break;
2796 }
2797
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002798 if (c1 == K_PS)
2799 {
2800 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2801 goto redraw;
2802 }
2803
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 if (!escaped)
2805 {
2806 /* CR typed means "enter", which is NL */
2807 if (c1 == '\r')
2808 c1 = '\n';
2809
2810 if (c1 == BS || c1 == K_BS
2811 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813 if (line_ga.ga_len > 0)
2814 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002815 if (has_mbyte)
2816 {
2817 p = (char_u *)line_ga.ga_data;
2818 p[line_ga.ga_len] = NUL;
2819 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2820 line_ga.ga_len -= len;
2821 }
2822 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002823 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 goto redraw;
2825 }
2826 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
2828
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002829 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 msg_col = startcol;
2832 msg_clr_eos();
2833 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002834 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 }
2836
2837 if (c1 == Ctrl_T)
2838 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002839 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002840 p = (char_u *)line_ga.ga_data;
2841 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002842 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002843 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002845 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002847 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002848 p = (char_u *)line_ga.ga_data;
2849 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2851 *s = ' ';
2852 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854redraw:
2855 /* redraw the line */
2856 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002858 p = (char_u *)line_ga.ga_data;
2859 p[line_ga.ga_len] = NUL;
2860 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002862 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002865 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002866 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002867 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002869 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002871 len = MB_PTR2LEN(p);
2872 msg_outtrans_len(p, len);
2873 vcol += ptr2cells(p);
2874 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 }
2876 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002877 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002878 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002879 continue;
2880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002882 if (c1 == Ctrl_D)
2883 {
2884 /* Delete one shiftwidth. */
2885 p = (char_u *)line_ga.ga_data;
2886 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002888 if (prev_char == '^')
2889 ex_keep_indent = TRUE;
2890 indent = 0;
2891 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 }
2893 else
2894 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002896 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002897 if (indent > 0)
2898 {
2899 --indent;
2900 indent -= indent % get_sw_value(curbuf);
2901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002903 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002904 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002905 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002906 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2907 --line_ga.ga_len;
2908 }
2909 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911
2912 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2913 {
2914 escaped = TRUE;
2915 continue;
2916 }
2917
2918 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2919 if (IS_SPECIAL(c1))
2920 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922
2923 if (IS_SPECIAL(c1))
2924 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002925 if (has_mbyte)
2926 len = (*mb_char2bytes)(c1,
2927 (char_u *)line_ga.ga_data + line_ga.ga_len);
2928 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002929 {
2930 len = 1;
2931 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2932 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933 if (c1 == '\n')
2934 msg_putchar('\n');
2935 else if (c1 == TAB)
2936 {
2937 /* Don't use chartabsize(), 'ts' can be different */
2938 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002939 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002940 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002945 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002946 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002948 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002949 escaped = FALSE;
2950
2951 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002952 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002953
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002954 /* We are done when a NL is entered, but not when it comes after an
2955 * odd number of backslashes, that results in a NUL. */
2956 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002958 int bcount = 0;
2959
2960 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2961 ++bcount;
2962
2963 if (bcount > 0)
2964 {
2965 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2966 * "\NL", etc. */
2967 line_ga.ga_len -= (bcount + 1) / 2;
2968 pend -= (bcount + 1) / 2;
2969 pend[-1] = '\n';
2970 }
2971
2972 if ((bcount & 1) == 0)
2973 {
2974 --line_ga.ga_len;
2975 --pend;
2976 *pend = NUL;
2977 break;
2978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 }
2980 }
2981
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002982 --no_mapping;
2983 --allow_keys;
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 /* make following messages go to the next line */
2986 msg_didout = FALSE;
2987 msg_col = 0;
2988 if (msg_row < Rows - 1)
2989 ++msg_row;
2990 emsg_on_display = FALSE; /* don't want ui_delay() */
2991
2992 if (got_int)
2993 ga_clear(&line_ga);
2994
2995 return (char_u *)line_ga.ga_data;
2996}
2997
Bram Moolenaare344bea2005-09-01 20:46:49 +00002998# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2999 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000/*
3001 * Return TRUE if ccline.overstrike is on.
3002 */
3003 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003004cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 return ccline.overstrike;
3007}
3008
3009/*
3010 * Return TRUE if the cursor is at the end of the cmdline.
3011 */
3012 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 return (ccline.cmdpos >= ccline.cmdlen);
3016}
3017#endif
3018
Bram Moolenaar9372a112005-12-06 19:59:18 +00003019#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020/*
3021 * Return the virtual column number at the current cursor position.
3022 * This is used by the IM code to obtain the start of the preedit string.
3023 */
3024 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003025cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026{
3027 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3028 return MAXCOL;
3029
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 if (has_mbyte)
3031 {
3032 colnr_T col;
3033 int i = 0;
3034
3035 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003036 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038 return col;
3039 }
3040 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 return ccline.cmdpos;
3042}
3043#endif
3044
3045#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3046/*
3047 * If part of the command line is an IM preedit string, redraw it with
3048 * IM feedback attributes. The cursor position is restored after drawing.
3049 */
3050 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003051redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
3053 if ((State & CMDLINE)
3054 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003055 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 && !p_imdisable
3057 && im_is_preediting())
3058 {
3059 int cmdpos = 0;
3060 int cmdspos;
3061 int old_row;
3062 int old_col;
3063 colnr_T col;
3064
3065 old_row = msg_row;
3066 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003067 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 if (has_mbyte)
3070 {
3071 for (col = 0; col < preedit_start_col
3072 && cmdpos < ccline.cmdlen; ++col)
3073 {
3074 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003075 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 }
3077 }
3078 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 cmdspos += preedit_start_col;
3081 cmdpos += preedit_start_col;
3082 }
3083
3084 msg_row = cmdline_row + (cmdspos / (int)Columns);
3085 msg_col = cmdspos % (int)Columns;
3086 if (msg_row >= Rows)
3087 msg_row = Rows - 1;
3088
3089 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3090 {
3091 int char_len;
3092 int char_attr;
3093
3094 char_attr = im_get_feedback_attr(col);
3095 if (char_attr < 0)
3096 break; /* end of preedit string */
3097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003099 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 char_len = 1;
3102
3103 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3104 cmdpos += char_len;
3105 }
3106
3107 msg_row = old_row;
3108 msg_col = old_col;
3109 }
3110}
3111#endif /* FEAT_XIM && FEAT_GUI_GTK */
3112
3113/*
3114 * Allocate a new command line buffer.
3115 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 */
3117 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003118alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119{
3120 /*
3121 * give some extra space to avoid having to allocate all the time
3122 */
3123 if (len < 80)
3124 len = 100;
3125 else
3126 len += 20;
3127
3128 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3129 ccline.cmdbufflen = len;
3130}
3131
3132/*
3133 * Re-allocate the command line to length len + something extra.
3134 * return FAIL for failure, OK otherwise
3135 */
3136 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003137realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138{
3139 char_u *p;
3140
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003141 if (len < ccline.cmdbufflen)
3142 return OK; /* no need to resize */
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 p = ccline.cmdbuff;
3145 alloc_cmdbuff(len); /* will get some more */
3146 if (ccline.cmdbuff == NULL) /* out of memory */
3147 {
3148 ccline.cmdbuff = p; /* keep the old one */
3149 return FAIL;
3150 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003151 /* There isn't always a NUL after the command, but it may need to be
3152 * there, thus copy up to the NUL and add a NUL. */
3153 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3154 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003156
3157 if (ccline.xpc != NULL
3158 && ccline.xpc->xp_pattern != NULL
3159 && ccline.xpc->xp_context != EXPAND_NOTHING
3160 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3161 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003162 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003163
3164 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3165 * to point into the newly allocated memory. */
3166 if (i >= 0 && i <= ccline.cmdlen)
3167 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3168 }
3169
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 return OK;
3171}
3172
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003173#if defined(FEAT_ARABIC) || defined(PROTO)
3174static char_u *arshape_buf = NULL;
3175
3176# if defined(EXITFREE) || defined(PROTO)
3177 void
Bram Moolenaar48ac6712019-07-04 20:26:21 +02003178free_arshape_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003179{
3180 vim_free(arshape_buf);
3181}
3182# endif
3183#endif
3184
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185/*
3186 * Draw part of the cmdline at the current cursor position. But draw stars
3187 * when cmdline_star is TRUE.
3188 */
3189 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003190draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191{
3192#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3193 int i;
3194
3195 if (cmdline_star > 0)
3196 for (i = 0; i < len; ++i)
3197 {
3198 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003200 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 }
3202 else
3203#endif
3204#ifdef FEAT_ARABIC
Bram Moolenaar693f7dc2019-06-21 02:30:38 +02003205 if (p_arshape && !p_tbidi && cmdline_has_arabic(start, len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 static int buflen = 0;
3208 char_u *p;
3209 int j;
3210 int newlen = 0;
3211 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003212 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 int prev_c = 0;
3214 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003215 int u8c;
3216 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218
3219 /*
3220 * Do arabic shaping into a temporary buffer. This is very
3221 * inefficient!
3222 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003223 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 {
3225 /* Re-allocate the buffer. We keep it around to avoid a lot of
3226 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003227 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003228 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003229 arshape_buf = alloc(buflen);
3230 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 return; /* out of memory */
3232 }
3233
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003234 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3235 {
3236 /* Prepend a space to draw the leading composing char on. */
3237 arshape_buf[0] = ' ';
3238 newlen = 1;
3239 }
3240
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 for (j = start; j < start + len; j += mb_l)
3242 {
3243 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003244 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003245 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 if (ARABIC_CHAR(u8c))
3247 {
3248 /* Do Arabic shaping. */
3249 if (cmdmsg_rl)
3250 {
3251 /* displaying from right to left */
3252 pc = prev_c;
3253 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003254 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 if (j + mb_l >= start + len)
3256 nc = NUL;
3257 else
3258 nc = utf_ptr2char(p + mb_l);
3259 }
3260 else
3261 {
3262 /* displaying from left to right */
3263 if (j + mb_l >= start + len)
3264 pc = NUL;
3265 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003266 {
3267 int pcc[MAX_MCO];
3268
3269 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003271 pc1 = pcc[0];
3272 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 nc = prev_c;
3274 }
3275 prev_c = u8c;
3276
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003277 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003280 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003282 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3283 if (u8cc[1] != 0)
3284 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 }
3287 }
3288 else
3289 {
3290 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003291 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 newlen += mb_l;
3293 }
3294 }
3295
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003296 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 }
3298 else
3299#endif
3300 msg_outtrans_len(ccline.cmdbuff + start, len);
3301}
3302
3303/*
3304 * Put a character on the command line. Shifts the following text to the
3305 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3306 * "c" must be printable (fit in one display cell)!
3307 */
3308 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003309putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310{
3311 if (cmd_silent)
3312 return;
3313 msg_no_more = TRUE;
3314 msg_putchar(c);
3315 if (shift)
3316 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3317 msg_no_more = FALSE;
3318 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003319 extra_char = c;
3320 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321}
3322
3323/*
3324 * Undo a putcmdline(c, FALSE).
3325 */
3326 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003327unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
3329 if (cmd_silent)
3330 return;
3331 msg_no_more = TRUE;
3332 if (ccline.cmdlen == ccline.cmdpos)
3333 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003334 else if (has_mbyte)
3335 draw_cmdline(ccline.cmdpos,
3336 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 else
3338 draw_cmdline(ccline.cmdpos, 1);
3339 msg_no_more = FALSE;
3340 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003341 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342}
3343
3344/*
3345 * Put the given string, of the given length, onto the command line.
3346 * If len is -1, then STRLEN() is used to calculate the length.
3347 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3348 * part will be redrawn, otherwise it will not. If this function is called
3349 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3350 * called afterwards.
3351 */
3352 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003353put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 int retval;
3356 int i;
3357 int m;
3358 int c;
3359
3360 if (len < 0)
3361 len = (int)STRLEN(str);
3362
3363 /* Check if ccline.cmdbuff needs to be longer */
3364 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003365 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 else
3367 retval = OK;
3368 if (retval == OK)
3369 {
3370 if (!ccline.overstrike)
3371 {
3372 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3373 ccline.cmdbuff + ccline.cmdpos,
3374 (size_t)(ccline.cmdlen - ccline.cmdpos));
3375 ccline.cmdlen += len;
3376 }
3377 else
3378 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 if (has_mbyte)
3380 {
3381 /* Count nr of characters in the new string. */
3382 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003383 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 ++m;
3385 /* Count nr of bytes in cmdline that are overwritten by these
3386 * characters. */
3387 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003388 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 --m;
3390 if (i < ccline.cmdlen)
3391 {
3392 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3393 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3394 ccline.cmdlen += ccline.cmdpos + len - i;
3395 }
3396 else
3397 ccline.cmdlen = ccline.cmdpos + len;
3398 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003399 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 ccline.cmdlen = ccline.cmdpos + len;
3401 }
3402 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3403 ccline.cmdbuff[ccline.cmdlen] = NUL;
3404
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 if (enc_utf8)
3406 {
3407 /* When the inserted text starts with a composing character,
3408 * backup to the character before it. There could be two of them.
3409 */
3410 i = 0;
3411 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3412 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3413 {
3414 i = (*mb_head_off)(ccline.cmdbuff,
3415 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3416 ccline.cmdpos -= i;
3417 len += i;
3418 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3419 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003420#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3422 {
3423 /* Check the previous character for Arabic combining pair. */
3424 i = (*mb_head_off)(ccline.cmdbuff,
3425 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3426 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3427 + ccline.cmdpos - i), c))
3428 {
3429 ccline.cmdpos -= i;
3430 len += i;
3431 }
3432 else
3433 i = 0;
3434 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 if (i != 0)
3437 {
3438 /* Also backup the cursor position. */
3439 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3440 ccline.cmdspos -= i;
3441 msg_col -= i;
3442 if (msg_col < 0)
3443 {
3444 msg_col += Columns;
3445 --msg_row;
3446 }
3447 }
3448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449
3450 if (redraw && !cmd_silent)
3451 {
3452 msg_no_more = TRUE;
3453 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003454 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3456 /* Avoid clearing the rest of the line too often. */
3457 if (cmdline_row != i || ccline.overstrike)
3458 msg_clr_eos();
3459 msg_no_more = FALSE;
3460 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003461 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003463 m = Columns * Rows;
3464 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003466 }
3467 else
3468 m = MAXCOL;
3469 for (i = 0; i < len; ++i)
3470 {
3471 c = cmdline_charsize(ccline.cmdpos);
3472 /* count ">" for a double-wide char that doesn't fit. */
3473 if (has_mbyte)
3474 correct_cmdspos(ccline.cmdpos, c);
3475 /* Stop cursor at the end of the screen, but do increment the
3476 * insert position, so that entering a very long command
3477 * works, even though you can't see it. */
3478 if (ccline.cmdspos + c < m)
3479 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003480
Bram Moolenaar14184a32019-02-16 15:10:30 +01003481 if (has_mbyte)
3482 {
3483 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3484 if (c > len - i - 1)
3485 c = len - i - 1;
3486 ccline.cmdpos += c;
3487 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003489 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491 }
3492 if (redraw)
3493 msg_check();
3494 return retval;
3495}
3496
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003497static struct cmdline_info prev_ccline;
3498static int prev_ccline_used = FALSE;
3499
3500/*
3501 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3502 * and overwrite it. But get_cmdline_str() may need it, thus make it
3503 * available globally in prev_ccline.
3504 */
3505 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003506save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003507{
3508 if (!prev_ccline_used)
3509 {
3510 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3511 prev_ccline_used = TRUE;
3512 }
3513 *ccp = prev_ccline;
3514 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003515 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003516}
3517
3518/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003519 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003520 */
3521 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003522restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003523{
3524 ccline = prev_ccline;
3525 prev_ccline = *ccp;
3526}
3527
Bram Moolenaar8299df92004-07-10 09:47:34 +00003528/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003529 * Paste a yank register into the command line.
3530 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531 * insert_reg() can't be used here, because special characters from the
3532 * register contents will be interpreted as commands.
3533 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003534 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003535 */
3536 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003537cmdline_paste(
3538 int regname,
3539 int literally, /* Insert text literally instead of "as typed" */
3540 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003541{
3542 long i;
3543 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003544 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003545 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003546
3547 /* check for valid regname; also accept special characters for CTRL-R in
3548 * the command line */
3549 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003550 && regname != Ctrl_A && regname != Ctrl_L
3551 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003552 return FAIL;
3553
3554 /* A register containing CTRL-R can cause an endless loop. Allow using
3555 * CTRL-C to break the loop. */
3556 line_breakcheck();
3557 if (got_int)
3558 return FAIL;
3559
3560#ifdef FEAT_CLIPBOARD
3561 regname = may_get_selection(regname);
3562#endif
3563
Bram Moolenaar438d1762018-09-30 17:11:48 +02003564 // Need to set "textlock" to avoid nasty things like going to another
3565 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003566 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003567 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003568 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003569
3570 if (i)
3571 {
3572 /* Got the value of a special register in "arg". */
3573 if (arg == NULL)
3574 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003575
3576 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3577 * part of the word. */
3578 p = arg;
3579 if (p_is && regname == Ctrl_W)
3580 {
3581 char_u *w;
3582 int len;
3583
3584 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003585 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003586 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003587 if (has_mbyte)
3588 {
3589 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3590 if (!vim_iswordc(mb_ptr2char(w - len)))
3591 break;
3592 w -= len;
3593 }
3594 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003595 {
3596 if (!vim_iswordc(w[-1]))
3597 break;
3598 --w;
3599 }
3600 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003601 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003602 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3603 p += len;
3604 }
3605
3606 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003607 if (allocated)
3608 vim_free(arg);
3609 return OK;
3610 }
3611
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003612 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613}
3614
3615/*
3616 * Put a string on the command line.
3617 * When "literally" is TRUE, insert literally.
3618 * When "literally" is FALSE, insert as typed, but don't leave the command
3619 * line.
3620 */
3621 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003622cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003623{
3624 int c, cv;
3625
3626 if (literally)
3627 put_on_cmdline(s, -1, TRUE);
3628 else
3629 while (*s != NUL)
3630 {
3631 cv = *s;
3632 if (cv == Ctrl_V && s[1])
3633 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003634 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003635 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003636 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003637 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003638 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3639 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003640#ifdef UNIX
3641 || c == intr_char
3642#endif
3643 || (c == Ctrl_BSL && *s == Ctrl_N))
3644 stuffcharReadbuff(Ctrl_V);
3645 stuffcharReadbuff(c);
3646 }
3647}
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649#ifdef FEAT_WILDMENU
3650/*
3651 * Delete characters on the command line, from "from" to the current
3652 * position.
3653 */
3654 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003655cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656{
3657 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3658 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3659 ccline.cmdlen -= ccline.cmdpos - from;
3660 ccline.cmdpos = from;
3661}
3662#endif
3663
3664/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003665 * This function is called when the screen size changes and with incremental
3666 * search and in other situations where the command line may have been
3667 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
3669 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003670redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003672 redrawcmdline_ex(TRUE);
3673}
3674
3675 void
3676redrawcmdline_ex(int do_compute_cmdrow)
3677{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 if (cmd_silent)
3679 return;
3680 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003681 if (do_compute_cmdrow)
3682 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 redrawcmd();
3684 cursorcmd();
3685}
3686
3687 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003688redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689{
3690 int i;
3691
3692 if (cmd_silent)
3693 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003694 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 msg_putchar(ccline.cmdfirstc);
3696 if (ccline.cmdprompt != NULL)
3697 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003698 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3700 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003701 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 --ccline.cmdindent;
3703 }
3704 else
3705 for (i = ccline.cmdindent; i > 0; --i)
3706 msg_putchar(' ');
3707}
3708
3709/*
3710 * Redraw what is currently on the command line.
3711 */
3712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003713redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
3715 if (cmd_silent)
3716 return;
3717
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003718 /* when 'incsearch' is set there may be no command line while redrawing */
3719 if (ccline.cmdbuff == NULL)
3720 {
3721 windgoto(cmdline_row, 0);
3722 msg_clr_eos();
3723 return;
3724 }
3725
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 msg_start();
3727 redrawcmdprompt();
3728
3729 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3730 msg_no_more = TRUE;
3731 draw_cmdline(0, ccline.cmdlen);
3732 msg_clr_eos();
3733 msg_no_more = FALSE;
3734
3735 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003736 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003737 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738
3739 /*
3740 * An emsg() before may have set msg_scroll. This is used in normal mode,
3741 * in cmdline mode we can reset them now.
3742 */
3743 msg_scroll = FALSE; /* next message overwrites cmdline */
3744
3745 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3746 * in cmdline mode */
3747 skip_redraw = FALSE;
3748}
3749
3750 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003751compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003753 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 cmdline_row = Rows - 1;
3755 else
3756 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003757 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758}
3759
3760 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003761cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
3763 if (cmd_silent)
3764 return;
3765
3766#ifdef FEAT_RIGHTLEFT
3767 if (cmdmsg_rl)
3768 {
3769 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3770 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3771 if (msg_row <= 0)
3772 msg_row = Rows - 1;
3773 }
3774 else
3775#endif
3776 {
3777 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3778 msg_col = ccline.cmdspos % (int)Columns;
3779 if (msg_row >= Rows)
3780 msg_row = Rows - 1;
3781 }
3782
3783 windgoto(msg_row, msg_col);
3784#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003785 if (p_imst == IM_ON_THE_SPOT)
3786 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787#endif
3788#ifdef MCH_CURSOR_SHAPE
3789 mch_update_cursor();
3790#endif
3791}
3792
3793 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003794gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 msg_start();
3797#ifdef FEAT_RIGHTLEFT
3798 if (cmdmsg_rl)
3799 msg_col = Columns - 1;
3800 else
3801#endif
3802 msg_col = 0; /* always start in column 0 */
3803 if (clr) /* clear the bottom line(s) */
3804 msg_clr_eos(); /* will reset clear_cmdline */
3805 windgoto(cmdline_row, 0);
3806}
3807
3808/*
3809 * Check the word in front of the cursor for an abbreviation.
3810 * Called when the non-id character "c" has been entered.
3811 * When an abbreviation is recognized it is removed from the text with
3812 * backspaces and the replacement string is inserted, followed by "c".
3813 */
3814 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003815ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003817 int spos = 0;
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3820 return FALSE;
3821
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003822 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3823 * Actually accepts any mark. */
3824 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3825 spos++;
3826 if (ccline.cmdlen - spos > 5
3827 && ccline.cmdbuff[spos] == '\''
3828 && ccline.cmdbuff[spos + 2] == ','
3829 && ccline.cmdbuff[spos + 3] == '\'')
3830 spos += 5;
3831 else
3832 /* check abbreviation from the beginning of the commandline */
3833 spos = 0;
3834
3835 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836}
3837
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003838#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3839 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003840sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003841{
3842 char_u *p1 = *(char_u **)s1;
3843 char_u *p2 = *(char_u **)s2;
3844
3845 if (*p1 != '<' && *p2 == '<') return -1;
3846 if (*p1 == '<' && *p2 != '<') return 1;
3847 return STRCMP(p1, p2);
3848}
3849#endif
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851/*
3852 * Return FAIL if this is not an appropriate context in which to do
3853 * completion of anything, return OK if it is (even if there are no matches).
3854 * For the caller, this means that the character is just passed through like a
3855 * normal character (instead of being expanded). This allows :s/^I^D etc.
3856 */
3857 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003858nextwild(
3859 expand_T *xp,
3860 int type,
3861 int options, /* extra options for ExpandOne() */
3862 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863{
3864 int i, j;
3865 char_u *p1;
3866 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 int difflen;
3868 int v;
3869
3870 if (xp->xp_numfiles == -1)
3871 {
3872 set_expand_context(xp);
3873 cmd_showtail = expand_showtail(xp);
3874 }
3875
3876 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3877 {
3878 beep_flush();
3879 return OK; /* Something illegal on command line */
3880 }
3881 if (xp->xp_context == EXPAND_NOTHING)
3882 {
3883 /* Caller can use the character as a normal char instead */
3884 return FAIL;
3885 }
3886
Bram Moolenaar32526b32019-01-19 17:43:09 +01003887 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 out_flush();
3889
3890 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003891 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892
3893 if (type == WILD_NEXT || type == WILD_PREV)
3894 {
3895 /*
3896 * Get next/previous match for a previous expanded pattern.
3897 */
3898 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3899 }
3900 else
3901 {
3902 /*
3903 * Translate string into pattern and expand it.
3904 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003905 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3906 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 p2 = NULL;
3908 else
3909 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003910 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003911 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3912 if (escape)
3913 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003914
3915 if (p_wic)
3916 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003917 p2 = ExpandOne(xp, p1,
3918 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003919 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003921 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 if (p2 != NULL && type == WILD_LONGEST)
3923 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003924 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 if (ccline.cmdbuff[i + j] == '*'
3926 || ccline.cmdbuff[i + j] == '?')
3927 break;
3928 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003929 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 }
3931 }
3932 }
3933
3934 if (p2 != NULL && !got_int)
3935 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003936 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003937 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003939 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 xp->xp_pattern = ccline.cmdbuff + i;
3941 }
3942 else
3943 v = OK;
3944 if (v == OK)
3945 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003946 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3947 &ccline.cmdbuff[ccline.cmdpos],
3948 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3949 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 ccline.cmdlen += difflen;
3951 ccline.cmdpos += difflen;
3952 }
3953 }
3954 vim_free(p2);
3955
3956 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003957 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
3959 /* When expanding a ":map" command and no matches are found, assume that
3960 * the key is supposed to be inserted literally */
3961 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3962 return FAIL;
3963
3964 if (xp->xp_numfiles <= 0 && p2 == NULL)
3965 beep_flush();
3966 else if (xp->xp_numfiles == 1)
3967 /* free expanded pattern */
3968 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3969
3970 return OK;
3971}
3972
3973/*
3974 * Do wildcard expansion on the string 'str'.
3975 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003976 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * Return NULL for failure.
3978 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003979 * "orig" is the originally expanded string, copied to allocated memory. It
3980 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3981 * WILD_PREV "orig" should be NULL.
3982 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003983 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3984 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 *
3986 * mode = WILD_FREE: just free previously expanded matches
3987 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3988 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3989 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3990 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3991 * mode = WILD_ALL: return all matches concatenated
3992 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003993 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 *
3995 * options = WILD_LIST_NOTFOUND: list entries without a match
3996 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3997 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3998 * options = WILD_NO_BEEP: Don't beep for multiple matches
3999 * options = WILD_ADD_SLASH: add a slash after directory names
4000 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4001 * options = WILD_SILENT: don't print warning messages
4002 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004003 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 *
4005 * The variables xp->xp_context and xp->xp_backslash must have been set!
4006 */
4007 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004008ExpandOne(
4009 expand_T *xp,
4010 char_u *str,
4011 char_u *orig, /* allocated copy of original of expanded string */
4012 int options,
4013 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014{
4015 char_u *ss = NULL;
4016 static int findex;
4017 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004018 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 int i;
4020 long_u len;
4021 int non_suf_match; /* number without matching suffix */
4022
4023 /*
4024 * first handle the case of using an old match
4025 */
4026 if (mode == WILD_NEXT || mode == WILD_PREV)
4027 {
4028 if (xp->xp_numfiles > 0)
4029 {
4030 if (mode == WILD_PREV)
4031 {
4032 if (findex == -1)
4033 findex = xp->xp_numfiles;
4034 --findex;
4035 }
4036 else /* mode == WILD_NEXT */
4037 ++findex;
4038
4039 /*
4040 * When wrapping around, return the original string, set findex to
4041 * -1.
4042 */
4043 if (findex < 0)
4044 {
4045 if (orig_save == NULL)
4046 findex = xp->xp_numfiles - 1;
4047 else
4048 findex = -1;
4049 }
4050 if (findex >= xp->xp_numfiles)
4051 {
4052 if (orig_save == NULL)
4053 findex = 0;
4054 else
4055 findex = -1;
4056 }
4057#ifdef FEAT_WILDMENU
4058 if (p_wmnu)
4059 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4060 findex, cmd_showtail);
4061#endif
4062 if (findex == -1)
4063 return vim_strsave(orig_save);
4064 return vim_strsave(xp->xp_files[findex]);
4065 }
4066 else
4067 return NULL;
4068 }
4069
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004070 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4072 {
4073 FreeWild(xp->xp_numfiles, xp->xp_files);
4074 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004075 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 }
4077 findex = 0;
4078
4079 if (mode == WILD_FREE) /* only release file name */
4080 return NULL;
4081
4082 if (xp->xp_numfiles == -1)
4083 {
4084 vim_free(orig_save);
4085 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004086 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 /*
4089 * Do the expansion.
4090 */
4091 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4092 options) == FAIL)
4093 {
4094#ifdef FNAME_ILLEGAL
4095 /* Illegal file name has been silently skipped. But when there
4096 * are wildcards, the real problem is that there was no match,
4097 * causing the pattern to be added, which has illegal characters.
4098 */
4099 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004100 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101#endif
4102 }
4103 else if (xp->xp_numfiles == 0)
4104 {
4105 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004106 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 else
4109 {
4110 /* Escape the matches for use on the command line. */
4111 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4112
4113 /*
4114 * Check for matching suffixes in file names.
4115 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004116 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4117 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 {
4119 if (xp->xp_numfiles)
4120 non_suf_match = xp->xp_numfiles;
4121 else
4122 non_suf_match = 1;
4123 if ((xp->xp_context == EXPAND_FILES
4124 || xp->xp_context == EXPAND_DIRECTORIES)
4125 && xp->xp_numfiles > 1)
4126 {
4127 /*
4128 * More than one match; check suffix.
4129 * The files will have been sorted on matching suffix in
4130 * expand_wildcards, only need to check the first two.
4131 */
4132 non_suf_match = 0;
4133 for (i = 0; i < 2; ++i)
4134 if (match_suffix(xp->xp_files[i]))
4135 ++non_suf_match;
4136 }
4137 if (non_suf_match != 1)
4138 {
4139 /* Can we ever get here unless it's while expanding
4140 * interactively? If not, we can get rid of this all
4141 * together. Don't really want to wait for this message
4142 * (and possibly have to hit return to continue!).
4143 */
4144 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004145 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 else if (!(options & WILD_NO_BEEP))
4147 beep_flush();
4148 }
4149 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4150 ss = vim_strsave(xp->xp_files[0]);
4151 }
4152 }
4153 }
4154
4155 /* Find longest common part */
4156 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4157 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004158 int mb_len = 1;
4159 int c0, ci;
4160
4161 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004163 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004165 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4166 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4167 }
4168 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004169 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004170 for (i = 1; i < xp->xp_numfiles; ++i)
4171 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004172 if (has_mbyte)
4173 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4174 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004175 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004176 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004178 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004179 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004181 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 break;
4183 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004184 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 break;
4186 }
4187 if (i < xp->xp_numfiles)
4188 {
4189 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004190 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 break;
4192 }
4193 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004194
Bram Moolenaar964b3742019-05-24 18:54:09 +02004195 ss = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004197 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 findex = -1; /* next p_wc gets first one */
4199 }
4200
4201 /* Concatenate all matching names */
4202 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4203 {
4204 len = 0;
4205 for (i = 0; i < xp->xp_numfiles; ++i)
4206 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02004207 ss = alloc(len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 if (ss != NULL)
4209 {
4210 *ss = NUL;
4211 for (i = 0; i < xp->xp_numfiles; ++i)
4212 {
4213 STRCAT(ss, xp->xp_files[i]);
4214 if (i != xp->xp_numfiles - 1)
4215 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4216 }
4217 }
4218 }
4219
4220 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4221 ExpandCleanup(xp);
4222
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004223 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004224 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004225 vim_free(orig);
4226
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return ss;
4228}
4229
4230/*
4231 * Prepare an expand structure for use.
4232 */
4233 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004234ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004236 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004237 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004239#ifndef BACKSLASH_IN_FILENAME
4240 xp->xp_shell = FALSE;
4241#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 xp->xp_numfiles = -1;
4243 xp->xp_files = NULL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004244#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004245 xp->xp_arg = NULL;
4246#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004247 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248}
4249
4250/*
4251 * Cleanup an expand structure after use.
4252 */
4253 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004254ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255{
4256 if (xp->xp_numfiles >= 0)
4257 {
4258 FreeWild(xp->xp_numfiles, xp->xp_files);
4259 xp->xp_numfiles = -1;
4260 }
4261}
4262
4263 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004264ExpandEscape(
4265 expand_T *xp,
4266 char_u *str,
4267 int numfiles,
4268 char_u **files,
4269 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270{
4271 int i;
4272 char_u *p;
4273
4274 /*
4275 * May change home directory back to "~"
4276 */
4277 if (options & WILD_HOME_REPLACE)
4278 tilde_replace(str, numfiles, files);
4279
4280 if (options & WILD_ESCAPE)
4281 {
4282 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004283 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004284 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 || xp->xp_context == EXPAND_BUFFERS
4286 || xp->xp_context == EXPAND_DIRECTORIES)
4287 {
4288 /*
4289 * Insert a backslash into a file name before a space, \, %, #
4290 * and wildmatch characters, except '~'.
4291 */
4292 for (i = 0; i < numfiles; ++i)
4293 {
4294 /* for ":set path=" we need to escape spaces twice */
4295 if (xp->xp_backslash == XP_BS_THREE)
4296 {
4297 p = vim_strsave_escaped(files[i], (char_u *)" ");
4298 if (p != NULL)
4299 {
4300 vim_free(files[i]);
4301 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004302#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 p = vim_strsave_escaped(files[i], (char_u *)" ");
4304 if (p != NULL)
4305 {
4306 vim_free(files[i]);
4307 files[i] = p;
4308 }
4309#endif
4310 }
4311 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004312#ifdef BACKSLASH_IN_FILENAME
4313 p = vim_strsave_fnameescape(files[i], FALSE);
4314#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004315 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004316#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 if (p != NULL)
4318 {
4319 vim_free(files[i]);
4320 files[i] = p;
4321 }
4322
4323 /* If 'str' starts with "\~", replace "~" at start of
4324 * files[i] with "\~". */
4325 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004326 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004329
4330 /* If the first file starts with a '+' escape it. Otherwise it
4331 * could be seen as "+cmd". */
4332 if (*files[0] == '+')
4333 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 else if (xp->xp_context == EXPAND_TAGS)
4336 {
4337 /*
4338 * Insert a backslash before characters in a tag name that
4339 * would terminate the ":tag" command.
4340 */
4341 for (i = 0; i < numfiles; ++i)
4342 {
4343 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4344 if (p != NULL)
4345 {
4346 vim_free(files[i]);
4347 files[i] = p;
4348 }
4349 }
4350 }
4351 }
4352}
4353
4354/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004355 * Escape special characters in "fname" for when used as a file name argument
4356 * after a Vim command, or, when "shell" is non-zero, a shell command.
4357 * Returns the result in allocated memory.
4358 */
4359 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004360vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004361{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004362 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004363#ifdef BACKSLASH_IN_FILENAME
4364 char_u buf[20];
4365 int j = 0;
4366
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004367 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004368 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004369 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004370 buf[j++] = *p;
4371 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004372 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004373#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004374 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4375 if (shell && csh_like_shell() && p != NULL)
4376 {
4377 char_u *s;
4378
4379 /* For csh and similar shells need to put two backslashes before '!'.
4380 * One is taken by Vim, one by the shell. */
4381 s = vim_strsave_escaped(p, (char_u *)"!");
4382 vim_free(p);
4383 p = s;
4384 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004385#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004386
4387 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4388 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004389 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004390 escape_fname(&p);
4391
4392 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004393}
4394
4395/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004396 * Put a backslash before the file name in "pp", which is in allocated memory.
4397 */
4398 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004399escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004400{
4401 char_u *p;
4402
Bram Moolenaar964b3742019-05-24 18:54:09 +02004403 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00004404 if (p != NULL)
4405 {
4406 p[0] = '\\';
4407 STRCPY(p + 1, *pp);
4408 vim_free(*pp);
4409 *pp = p;
4410 }
4411}
4412
4413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 * For each file name in files[num_files]:
4415 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4416 */
4417 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004418tilde_replace(
4419 char_u *orig_pat,
4420 int num_files,
4421 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 int i;
4424 char_u *p;
4425
4426 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4427 {
4428 for (i = 0; i < num_files; ++i)
4429 {
4430 p = home_replace_save(NULL, files[i]);
4431 if (p != NULL)
4432 {
4433 vim_free(files[i]);
4434 files[i] = p;
4435 }
4436 }
4437 }
4438}
4439
4440/*
4441 * Show all matches for completion on the command line.
4442 * Returns EXPAND_NOTHING when the character that triggered expansion should
4443 * be inserted like a normal character.
4444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004446showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447{
4448#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4449 int num_files;
4450 char_u **files_found;
4451 int i, j, k;
4452 int maxlen;
4453 int lines;
4454 int columns;
4455 char_u *p;
4456 int lastlen;
4457 int attr;
4458 int showtail;
4459
4460 if (xp->xp_numfiles == -1)
4461 {
4462 set_expand_context(xp);
4463 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4464 &num_files, &files_found);
4465 showtail = expand_showtail(xp);
4466 if (i != EXPAND_OK)
4467 return i;
4468
4469 }
4470 else
4471 {
4472 num_files = xp->xp_numfiles;
4473 files_found = xp->xp_files;
4474 showtail = cmd_showtail;
4475 }
4476
4477#ifdef FEAT_WILDMENU
4478 if (!wildmenu)
4479 {
4480#endif
4481 msg_didany = FALSE; /* lines_left will be set */
4482 msg_start(); /* prepare for paging */
4483 msg_putchar('\n');
4484 out_flush();
4485 cmdline_row = msg_row;
4486 msg_didany = FALSE; /* lines_left will be set again */
4487 msg_start(); /* prepare for paging */
4488#ifdef FEAT_WILDMENU
4489 }
4490#endif
4491
4492 if (got_int)
4493 got_int = FALSE; /* only int. the completion, not the cmd line */
4494#ifdef FEAT_WILDMENU
4495 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004496 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497#endif
4498 else
4499 {
4500 /* find the length of the longest file name */
4501 maxlen = 0;
4502 for (i = 0; i < num_files; ++i)
4503 {
4504 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004505 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 || xp->xp_context == EXPAND_BUFFERS))
4507 {
4508 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4509 j = vim_strsize(NameBuff);
4510 }
4511 else
4512 j = vim_strsize(L_SHOWFILE(i));
4513 if (j > maxlen)
4514 maxlen = j;
4515 }
4516
4517 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4518 lines = num_files;
4519 else
4520 {
4521 /* compute the number of columns and lines for the listing */
4522 maxlen += 2; /* two spaces between file names */
4523 columns = ((int)Columns + 2) / maxlen;
4524 if (columns < 1)
4525 columns = 1;
4526 lines = (num_files + columns - 1) / columns;
4527 }
4528
Bram Moolenaar8820b482017-03-16 17:23:31 +01004529 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530
4531 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4532 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004533 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 msg_clr_eos();
4535 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004536 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538
4539 /* list the files line by line */
4540 for (i = 0; i < lines; ++i)
4541 {
4542 lastlen = 999;
4543 for (k = i; k < num_files; k += lines)
4544 {
4545 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4546 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004547 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 p = files_found[k] + STRLEN(files_found[k]) + 1;
4549 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004550 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004552 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 break;
4554 }
4555 for (j = maxlen - lastlen; --j >= 0; )
4556 msg_putchar(' ');
4557 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004558 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 || xp->xp_context == EXPAND_BUFFERS)
4560 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004561 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004562 if (xp->xp_numfiles != -1)
4563 {
4564 char_u *halved_slash;
4565 char_u *exp_path;
4566
4567 /* Expansion was done before and special characters
4568 * were escaped, need to halve backslashes. Also
4569 * $HOME has been replaced with ~/. */
4570 exp_path = expand_env_save_opt(files_found[k], TRUE);
4571 halved_slash = backslash_halve_save(
4572 exp_path != NULL ? exp_path : files_found[k]);
4573 j = mch_isdir(halved_slash != NULL ? halved_slash
4574 : files_found[k]);
4575 vim_free(exp_path);
4576 vim_free(halved_slash);
4577 }
4578 else
4579 /* Expansion was done here, file names are literal. */
4580 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 if (showtail)
4582 p = L_SHOWFILE(k);
4583 else
4584 {
4585 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4586 TRUE);
4587 p = NameBuff;
4588 }
4589 }
4590 else
4591 {
4592 j = FALSE;
4593 p = L_SHOWFILE(k);
4594 }
4595 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4596 }
4597 if (msg_col > 0) /* when not wrapped around */
4598 {
4599 msg_clr_eos();
4600 msg_putchar('\n');
4601 }
4602 out_flush(); /* show one line at a time */
4603 if (got_int)
4604 {
4605 got_int = FALSE;
4606 break;
4607 }
4608 }
4609
4610 /*
4611 * we redraw the command below the lines that we have just listed
4612 * This is a bit tricky, but it saves a lot of screen updating.
4613 */
4614 cmdline_row = msg_row; /* will put it back later */
4615 }
4616
4617 if (xp->xp_numfiles == -1)
4618 FreeWild(num_files, files_found);
4619
4620 return EXPAND_OK;
4621}
4622
4623/*
4624 * Private gettail for showmatches() (and win_redr_status_matches()):
4625 * Find tail of file name path, but ignore trailing "/".
4626 */
4627 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004628sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 char_u *p;
4631 char_u *t = s;
4632 int had_sep = FALSE;
4633
4634 for (p = s; *p != NUL; )
4635 {
4636 if (vim_ispathsep(*p)
4637#ifdef BACKSLASH_IN_FILENAME
4638 && !rem_backslash(p)
4639#endif
4640 )
4641 had_sep = TRUE;
4642 else if (had_sep)
4643 {
4644 t = p;
4645 had_sep = FALSE;
4646 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004647 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 }
4649 return t;
4650}
4651
4652/*
4653 * Return TRUE if we only need to show the tail of completion matches.
4654 * When not completing file names or there is a wildcard in the path FALSE is
4655 * returned.
4656 */
4657 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004658expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
4660 char_u *s;
4661 char_u *end;
4662
4663 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004664 if (xp->xp_context != EXPAND_FILES
4665 && xp->xp_context != EXPAND_SHELLCMD
4666 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FALSE;
4668
4669 end = gettail(xp->xp_pattern);
4670 if (end == xp->xp_pattern) /* there is no path separator */
4671 return FALSE;
4672
4673 for (s = xp->xp_pattern; s < end; s++)
4674 {
4675 /* Skip escaped wildcards. Only when the backslash is not a path
4676 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4677 if (rem_backslash(s))
4678 ++s;
4679 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4680 return FALSE;
4681 }
4682 return TRUE;
4683}
4684
4685/*
4686 * Prepare a string for expansion.
4687 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004688 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 * When expanding other names: The string will be used with regcomp(). Copy
4690 * the name into allocated memory and prepend "^".
4691 */
4692 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004693addstar(
4694 char_u *fname,
4695 int len,
4696 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
4698 char_u *retval;
4699 int i, j;
4700 int new_len;
4701 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004702 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004704 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004705 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004706 && context != EXPAND_SHELLCMD
4707 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 {
4709 /*
4710 * Matching will be done internally (on something other than files).
4711 * So we convert the file-matching-type wildcards into our kind for
4712 * use with vim_regcomp(). First work out how long it will be:
4713 */
4714
4715 /* For help tags the translation is done in find_help_tags().
4716 * For a tag pattern starting with "/" no translation is needed. */
4717 if (context == EXPAND_HELP
4718 || context == EXPAND_COLORS
4719 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004720 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004721 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004722 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004723 || ((context == EXPAND_TAGS_LISTFILES
4724 || context == EXPAND_TAGS)
4725 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 retval = vim_strnsave(fname, len);
4727 else
4728 {
4729 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4730 for (i = 0; i < len; i++)
4731 {
4732 if (fname[i] == '*' || fname[i] == '~')
4733 new_len++; /* '*' needs to be replaced by ".*"
4734 '~' needs to be replaced by "\~" */
4735
4736 /* Buffer names are like file names. "." should be literal */
4737 if (context == EXPAND_BUFFERS && fname[i] == '.')
4738 new_len++; /* "." becomes "\." */
4739
4740 /* Custom expansion takes care of special things, match
4741 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004742 if ((context == EXPAND_USER_DEFINED
4743 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 new_len++; /* '\' becomes "\\" */
4745 }
4746 retval = alloc(new_len);
4747 if (retval != NULL)
4748 {
4749 retval[0] = '^';
4750 j = 1;
4751 for (i = 0; i < len; i++, j++)
4752 {
4753 /* Skip backslash. But why? At least keep it for custom
4754 * expansion. */
4755 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004756 && context != EXPAND_USER_LIST
4757 && fname[i] == '\\'
4758 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 break;
4760
4761 switch (fname[i])
4762 {
4763 case '*': retval[j++] = '.';
4764 break;
4765 case '~': retval[j++] = '\\';
4766 break;
4767 case '?': retval[j] = '.';
4768 continue;
4769 case '.': if (context == EXPAND_BUFFERS)
4770 retval[j++] = '\\';
4771 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004772 case '\\': if (context == EXPAND_USER_DEFINED
4773 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 retval[j++] = '\\';
4775 break;
4776 }
4777 retval[j] = fname[i];
4778 }
4779 retval[j] = NUL;
4780 }
4781 }
4782 }
4783 else
4784 {
4785 retval = alloc(len + 4);
4786 if (retval != NULL)
4787 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004788 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789
4790 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004791 * Don't add a star to *, ~, ~user, $var or `cmd`.
4792 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 * ~ would be at the start of the file name, but not the tail.
4794 * $ could be anywhere in the tail.
4795 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004796 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 */
4798 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004799 ends_in_star = (len > 0 && retval[len - 1] == '*');
4800#ifndef BACKSLASH_IN_FILENAME
4801 for (i = len - 2; i >= 0; --i)
4802 {
4803 if (retval[i] != '\\')
4804 break;
4805 ends_in_star = !ends_in_star;
4806 }
4807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004809 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 && vim_strchr(tail, '$') == NULL
4811 && vim_strchr(retval, '`') == NULL)
4812 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004813 else if (len > 0 && retval[len - 1] == '$')
4814 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 retval[len] = NUL;
4816 }
4817 }
4818 return retval;
4819}
4820
4821/*
4822 * Must parse the command line so far to work out what context we are in.
4823 * Completion can then be done based on that context.
4824 * This routine sets the variables:
4825 * xp->xp_pattern The start of the pattern to be expanded within
4826 * the command line (ends at the cursor).
4827 * xp->xp_context The type of thing to expand. Will be one of:
4828 *
4829 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4830 * the command line, like an unknown command. Caller
4831 * should beep.
4832 * EXPAND_NOTHING Unrecognised context for completion, use char like
4833 * a normal char, rather than for completion. eg
4834 * :s/^I/
4835 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4836 * it.
4837 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
Bram Moolenaar8071cb22019-07-12 17:58:01 +02004838 * EXPAND_FILES After command with EX_XFILE set, or after setting
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 * with P_EXPAND set. eg :e ^I, :w>>^I
4840 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4841 * when we know only directories are of interest. eg
4842 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004843 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4845 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4846 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4847 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4848 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4849 * EXPAND_EVENTS Complete event names
4850 * EXPAND_SYNTAX Complete :syntax command arguments
4851 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4852 * EXPAND_AUGROUP Complete autocommand group names
4853 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4854 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4855 * eg :unmap a^I , :cunab x^I
4856 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4857 * eg :call sub^I
4858 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4859 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4860 * names in expressions, eg :while s^I
4861 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004862 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 */
4864 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004865set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004867 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 if (ccline.cmdfirstc != ':'
4869#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004870 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004871 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872#endif
4873 )
4874 {
4875 xp->xp_context = EXPAND_NOTHING;
4876 return;
4877 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004878 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879}
4880
4881 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004882set_cmd_context(
4883 expand_T *xp,
4884 char_u *str, /* start of command line */
4885 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004886 int col, /* position of cursor */
4887 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888{
4889 int old_char = NUL;
4890 char_u *nextcomm;
4891
4892 /*
4893 * Avoid a UMR warning from Purify, only save the character if it has been
4894 * written before.
4895 */
4896 if (col < len)
4897 old_char = str[col];
4898 str[col] = NUL;
4899 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004900
4901#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004902 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004903 {
4904# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004905 /* pass CMD_SIZE because there is no real command */
4906 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004907# endif
4908 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004909 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004910 {
4911 xp->xp_context = ccline.xp_context;
4912 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004913# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004914 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004915# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004916 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004917 else
4918#endif
4919 while (nextcomm != NULL)
4920 nextcomm = set_one_cmd_context(xp, nextcomm);
4921
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004922 /* Store the string here so that call_user_expand_func() can get to them
4923 * easily. */
4924 xp->xp_line = str;
4925 xp->xp_col = col;
4926
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 str[col] = old_char;
4928}
4929
4930/*
4931 * Expand the command line "str" from context "xp".
4932 * "xp" must have been set by set_cmd_context().
4933 * xp->xp_pattern points into "str", to where the text that is to be expanded
4934 * starts.
4935 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4936 * cursor.
4937 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4938 * key that triggered expansion literally.
4939 * Returns EXPAND_OK otherwise.
4940 */
4941 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004942expand_cmdline(
4943 expand_T *xp,
4944 char_u *str, /* start of command line */
4945 int col, /* position of cursor */
4946 int *matchcount, /* return: nr of matches */
4947 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948{
4949 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004950 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4953 {
4954 beep_flush();
4955 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4956 }
4957 if (xp->xp_context == EXPAND_NOTHING)
4958 {
4959 /* Caller can use the character as a normal char instead */
4960 return EXPAND_NOTHING;
4961 }
4962
4963 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004964 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4965 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 if (file_str == NULL)
4967 return EXPAND_UNSUCCESSFUL;
4968
Bram Moolenaar94950a92010-12-02 16:01:29 +01004969 if (p_wic)
4970 options += WILD_ICASE;
4971
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004973 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 {
4975 *matchcount = 0;
4976 *matches = NULL;
4977 }
4978 vim_free(file_str);
4979
4980 return EXPAND_OK;
4981}
4982
4983#ifdef FEAT_MULTI_LANG
4984/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004985 * Cleanup matches for help tags:
4986 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4987 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004990cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
4992 int i, j;
4993 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004994 char_u buf[4];
4995 char_u *p = buf;
4996
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004997 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004998 {
4999 *p++ = '@';
5000 *p++ = p_hlg[0];
5001 *p++ = p_hlg[1];
5002 }
5003 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004
5005 for (i = 0; i < num_file; ++i)
5006 {
5007 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005008 if (len <= 0)
5009 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005010 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 {
5012 /* Sorting on priority means the same item in another language may
5013 * be anywhere. Search all items for a match up to the "@en". */
5014 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005015 if (j != i && (int)STRLEN(file[j]) == len + 3
5016 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 break;
5018 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005019 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 file[i][len] = NUL;
5021 }
5022 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005023
5024 if (*buf != NUL)
5025 for (i = 0; i < num_file; ++i)
5026 {
5027 len = (int)STRLEN(file[i]) - 3;
5028 if (len <= 0)
5029 continue;
5030 if (STRCMP(file[i] + len, buf) == 0)
5031 {
5032 /* remove the default language */
5033 file[i][len] = NUL;
5034 }
5035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036}
5037#endif
5038
5039/*
5040 * Do the expansion based on xp->xp_context and "pat".
5041 */
5042 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005043ExpandFromContext(
5044 expand_T *xp,
5045 char_u *pat,
5046 int *num_file,
5047 char_u ***file,
5048 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049{
5050#ifdef FEAT_CMDL_COMPL
5051 regmatch_T regmatch;
5052#endif
5053 int ret;
5054 int flags;
5055
5056 flags = EW_DIR; /* include directories */
5057 if (options & WILD_LIST_NOTFOUND)
5058 flags |= EW_NOTFOUND;
5059 if (options & WILD_ADD_SLASH)
5060 flags |= EW_ADDSLASH;
5061 if (options & WILD_KEEP_ALL)
5062 flags |= EW_KEEPALL;
5063 if (options & WILD_SILENT)
5064 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005065 if (options & WILD_ALLLINKS)
5066 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005068 if (xp->xp_context == EXPAND_FILES
5069 || xp->xp_context == EXPAND_DIRECTORIES
5070 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 {
5072 /*
5073 * Expand file or directory names.
5074 */
5075 int free_pat = FALSE;
5076 int i;
5077
5078 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5079 * space */
5080 if (xp->xp_backslash != XP_BS_NONE)
5081 {
5082 free_pat = TRUE;
5083 pat = vim_strsave(pat);
5084 for (i = 0; pat[i]; ++i)
5085 if (pat[i] == '\\')
5086 {
5087 if (xp->xp_backslash == XP_BS_THREE
5088 && pat[i + 1] == '\\'
5089 && pat[i + 2] == '\\'
5090 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005091 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 if (xp->xp_backslash == XP_BS_ONE
5093 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005094 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 }
5097
5098 if (xp->xp_context == EXPAND_FILES)
5099 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005100 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5101 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 else
5103 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005104 if (options & WILD_ICASE)
5105 flags |= EW_ICASE;
5106
Bram Moolenaard7834d32009-12-02 16:14:36 +00005107 /* Expand wildcards, supporting %:h and the like. */
5108 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 if (free_pat)
5110 vim_free(pat);
5111 return ret;
5112 }
5113
5114 *file = (char_u **)"";
5115 *num_file = 0;
5116 if (xp->xp_context == EXPAND_HELP)
5117 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005118 /* With an empty argument we would get all the help tags, which is
5119 * very slow. Get matches for "help" instead. */
5120 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5121 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 {
5123#ifdef FEAT_MULTI_LANG
5124 cleanup_help_tags(*num_file, *file);
5125#endif
5126 return OK;
5127 }
5128 return FAIL;
5129 }
5130
5131#ifndef FEAT_CMDL_COMPL
5132 return FAIL;
5133#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005134 if (xp->xp_context == EXPAND_SHELLCMD)
5135 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 if (xp->xp_context == EXPAND_OLD_SETTING)
5137 return ExpandOldSetting(num_file, file);
5138 if (xp->xp_context == EXPAND_BUFFERS)
5139 return ExpandBufnames(pat, num_file, file, options);
5140 if (xp->xp_context == EXPAND_TAGS
5141 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5142 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5143 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005144 {
5145 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005146 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5147 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005150 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005151 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005152 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005153 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005154 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005155 {
5156 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005157 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005158 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005159 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005160 {
5161 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005162 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005163 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005164# if defined(FEAT_EVAL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005165 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005166 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005167# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005168 if (xp->xp_context == EXPAND_PACKADD)
5169 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
5171 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5172 if (regmatch.regprog == NULL)
5173 return FAIL;
5174
5175 /* set ignore-case according to p_ic, p_scs and pat */
5176 regmatch.rm_ic = ignorecase(pat);
5177
5178 if (xp->xp_context == EXPAND_SETTINGS
5179 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5180 ret = ExpandSettings(xp, &regmatch, num_file, file);
5181 else if (xp->xp_context == EXPAND_MAPPINGS)
5182 ret = ExpandMappings(&regmatch, num_file, file);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005183# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 else if (xp->xp_context == EXPAND_USER_DEFINED)
5185 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5186# endif
5187 else
5188 {
5189 static struct expgen
5190 {
5191 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005192 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005194 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 } tab[] =
5196 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005197 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5198 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005199 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005200 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005201#ifdef FEAT_CMDHIST
5202 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5203#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005204 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005205 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005206 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5207 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5208 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005210 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5211 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5212 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5213 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214#endif
5215#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5217 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#endif
5219#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005220 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005222#ifdef FEAT_PROFILE
5223 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5224#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005225 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005226 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5227 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005228#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005229 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005230#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005231#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005232 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005233#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005234#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005236#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005237#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005238 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5239 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005241 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005242 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005243 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 };
5245 int i;
5246
5247 /*
5248 * Find a context in the table and call the ExpandGeneric() with the
5249 * right function to do the expansion.
5250 */
5251 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005252 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (xp->xp_context == tab[i].context)
5254 {
5255 if (tab[i].ic)
5256 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005257 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005258 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 break;
5260 }
5261 }
5262
Bram Moolenaar473de612013-06-08 18:19:48 +02005263 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265 return ret;
5266#endif /* FEAT_CMDL_COMPL */
5267}
5268
5269#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5270/*
5271 * Expand a list of names.
5272 *
5273 * Generic function for command line completion. It calls a function to
5274 * obtain strings, one by one. The strings are matched against a regexp
5275 * program. Matching strings are copied into an array, which is returned.
5276 *
5277 * Returns OK when no problems encountered, FAIL for error (out of memory).
5278 */
5279 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005280ExpandGeneric(
5281 expand_T *xp,
5282 regmatch_T *regmatch,
5283 int *num_file,
5284 char_u ***file,
5285 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005287 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288{
5289 int i;
5290 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005291 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 char_u *str;
5293
5294 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005295 * round == 0: count the number of matching names
5296 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005298 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
5300 for (i = 0; ; ++i)
5301 {
5302 str = (*func)(xp, i);
5303 if (str == NULL) /* end of list */
5304 break;
5305 if (*str == NUL) /* skip empty strings */
5306 continue;
5307
5308 if (vim_regexec(regmatch, str, (colnr_T)0))
5309 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005310 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005311 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005312 if (escaped)
5313 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5314 else
5315 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 (*file)[count] = str;
5317#ifdef FEAT_MENU
5318 if (func == get_menu_names && str != NULL)
5319 {
5320 /* test for separator added by get_menu_names() */
5321 str += STRLEN(str) - 1;
5322 if (*str == '\001')
5323 *str = '.';
5324 }
5325#endif
5326 }
5327 ++count;
5328 }
5329 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005330 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 {
5332 if (count == 0)
5333 return OK;
5334 *num_file = count;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005335 *file = ALLOC_MULT(char_u *, count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (*file == NULL)
5337 {
5338 *file = (char_u **)"";
5339 return FAIL;
5340 }
5341 count = 0;
5342 }
5343 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005344
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005345 /* Sort the results. Keep menu's in the specified order. */
5346 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005347 {
5348 if (xp->xp_context == EXPAND_EXPRESSION
5349 || xp->xp_context == EXPAND_FUNCTIONS
5350 || xp->xp_context == EXPAND_USER_FUNC)
5351 /* <SNR> functions should be sorted to the end. */
5352 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5353 sort_func_compare);
5354 else
5355 sort_strings(*file, *num_file);
5356 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005357
Bram Moolenaar4f688582007-07-24 12:34:30 +00005358#ifdef FEAT_CMDL_COMPL
5359 /* Reset the variables used for special highlight names expansion, so that
5360 * they don't show up when getting normal highlight names by ID. */
5361 reset_expand_highlight();
5362#endif
5363
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 return OK;
5365}
5366
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005367/*
5368 * Complete a shell command.
5369 * Returns FAIL or OK;
5370 */
5371 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005372expand_shellcmd(
5373 char_u *filepat, /* pattern to match with command names */
5374 int *num_file, /* return: number of matches */
5375 char_u ***file, /* return: array with matches */
5376 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005377{
5378 char_u *pat;
5379 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005380 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005381 int mustfree = FALSE;
5382 garray_T ga;
5383 char_u *buf = alloc(MAXPATHL);
5384 size_t l;
5385 char_u *s, *e;
5386 int flags = flagsarg;
5387 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005388 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005389 hashtab_T found_ht;
5390 hashitem_T *hi;
5391 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005392
5393 if (buf == NULL)
5394 return FAIL;
5395
5396 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5397 * space */
5398 pat = vim_strsave(filepat);
5399 for (i = 0; pat[i]; ++i)
5400 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005401 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005402
Bram Moolenaarb5971142015-03-21 17:32:19 +01005403 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005404
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005405 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5406 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005407 path = (char_u *)".";
5408 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005409 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005410 /* For an absolute name we don't use $PATH. */
5411 if (!mch_isFullName(pat))
5412 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005413 if (path == NULL)
5414 path = (char_u *)"";
5415 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005416
5417 /*
5418 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005419 * collect them in "ga". When "." is not in $PATH also expand for the
5420 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005421 */
5422 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005423 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005424 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005425 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005426#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005427 e = vim_strchr(s, ';');
5428#else
5429 e = vim_strchr(s, ':');
5430#endif
5431 if (e == NULL)
5432 e = s + STRLEN(s);
5433
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005434 if (*s == NUL)
5435 {
5436 if (did_curdir)
5437 break;
5438 // Find directories in the current directory, path is empty.
5439 did_curdir = TRUE;
5440 flags |= EW_DIR;
5441 }
5442 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5443 {
5444 did_curdir = TRUE;
5445 flags |= EW_DIR;
5446 }
5447 else
5448 // Do not match directories inside a $PATH item.
5449 flags &= ~EW_DIR;
5450
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005451 l = e - s;
5452 if (l > MAXPATHL - 5)
5453 break;
5454 vim_strncpy(buf, s, l);
5455 add_pathsep(buf);
5456 l = STRLEN(buf);
5457 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5458
5459 /* Expand matches in one directory of $PATH. */
5460 ret = expand_wildcards(1, &buf, num_file, file, flags);
5461 if (ret == OK)
5462 {
5463 if (ga_grow(&ga, *num_file) == FAIL)
5464 FreeWild(*num_file, *file);
5465 else
5466 {
5467 for (i = 0; i < *num_file; ++i)
5468 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005469 char_u *name = (*file)[i];
5470
5471 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005472 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005473 // Check if this name was already found.
5474 hash = hash_hash(name + l);
5475 hi = hash_lookup(&found_ht, name + l, hash);
5476 if (HASHITEM_EMPTY(hi))
5477 {
5478 // Remove the path that was prepended.
5479 STRMOVE(name, name + l);
5480 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5481 hash_add_item(&found_ht, hi, name, hash);
5482 name = NULL;
5483 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005484 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005485 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005486 }
5487 vim_free(*file);
5488 }
5489 }
5490 if (*e != NUL)
5491 ++e;
5492 }
5493 *file = ga.ga_data;
5494 *num_file = ga.ga_len;
5495
5496 vim_free(buf);
5497 vim_free(pat);
5498 if (mustfree)
5499 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005500 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005501 return OK;
5502}
5503
5504
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005505# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005507 * Call "user_expand_func()" to invoke a user defined Vim script function and
5508 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005510 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005511call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005512 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005513 expand_T *xp,
5514 int *num_file,
5515 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005517 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005518 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005519 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005520 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005521 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
Bram Moolenaarb7515462013-06-29 12:58:33 +02005523 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005524 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 *num_file = 0;
5526 *file = NULL;
5527
Bram Moolenaarb7515462013-06-29 12:58:33 +02005528 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005529 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005530 keep = ccline.cmdbuff[ccline.cmdlen];
5531 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005532 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005533
Bram Moolenaarffa96842018-06-12 22:05:14 +02005534 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5535
5536 args[0].v_type = VAR_STRING;
5537 args[0].vval.v_string = pat;
5538 args[1].v_type = VAR_STRING;
5539 args[1].vval.v_string = xp->xp_line;
5540 args[2].v_type = VAR_NUMBER;
5541 args[2].vval.v_number = xp->xp_col;
5542 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005544 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005545
Bram Moolenaarded27a12018-08-01 19:06:03 +02005546 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005547
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005548 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005549 if (ccline.cmdbuff != NULL)
5550 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005551
Bram Moolenaarffa96842018-06-12 22:05:14 +02005552 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005553 return ret;
5554}
5555
5556/*
5557 * Expand names with a function defined by the user.
5558 */
5559 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005560ExpandUserDefined(
5561 expand_T *xp,
5562 regmatch_T *regmatch,
5563 int *num_file,
5564 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005565{
5566 char_u *retstr;
5567 char_u *s;
5568 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005569 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005570 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005571 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005572
5573 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5574 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 return FAIL;
5576
5577 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005578 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 {
5580 e = vim_strchr(s, '\n');
5581 if (e == NULL)
5582 e = s + STRLEN(s);
5583 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005584 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005586 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5587 *e = keep;
5588
5589 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005591 if (ga_grow(&ga, 1) == FAIL)
5592 break;
5593 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5594 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
5596
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 if (*e != NUL)
5598 ++e;
5599 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005600 vim_free(retstr);
5601 *file = ga.ga_data;
5602 *num_file = ga.ga_len;
5603 return OK;
5604}
5605
5606/*
5607 * Expand names with a list returned by a function defined by the user.
5608 */
5609 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005610ExpandUserList(
5611 expand_T *xp,
5612 int *num_file,
5613 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005614{
5615 list_T *retlist;
5616 listitem_T *li;
5617 garray_T ga;
5618
5619 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5620 if (retlist == NULL)
5621 return FAIL;
5622
5623 ga_init2(&ga, (int)sizeof(char *), 3);
5624 /* Loop over the items in the list. */
5625 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5626 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005627 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5628 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005629
5630 if (ga_grow(&ga, 1) == FAIL)
5631 break;
5632
5633 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005634 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005635 ++ga.ga_len;
5636 }
5637 list_unref(retlist);
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 *file = ga.ga_data;
5640 *num_file = ga.ga_len;
5641 return OK;
5642}
5643#endif
5644
5645/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005646 * Expand color scheme, compiler or filetype names.
5647 * Search from 'runtimepath':
5648 * 'runtimepath'/{dirnames}/{pat}.vim
5649 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5650 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5651 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5652 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005653 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 */
5655 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005656ExpandRTDir(
5657 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005658 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005659 int *num_file,
5660 char_u ***file,
5661 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 char_u *s;
5664 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005665 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005667 int i;
5668 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669
5670 *num_file = 0;
5671 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005672 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005673 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005675 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005677 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005678 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005680 ga_clear_strings(&ga);
5681 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005683 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005684 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005685 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005687
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005688 if (flags & DIP_START) {
5689 for (i = 0; dirnames[i] != NULL; ++i)
5690 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005691 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005692 if (s == NULL)
5693 {
5694 ga_clear_strings(&ga);
5695 return FAIL;
5696 }
5697 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5698 globpath(p_pp, s, &ga, 0);
5699 vim_free(s);
5700 }
5701 }
5702
5703 if (flags & DIP_OPT) {
5704 for (i = 0; dirnames[i] != NULL; ++i)
5705 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005706 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005707 if (s == NULL)
5708 {
5709 ga_clear_strings(&ga);
5710 return FAIL;
5711 }
5712 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5713 globpath(p_pp, s, &ga, 0);
5714 vim_free(s);
5715 }
5716 }
5717
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005718 for (i = 0; i < ga.ga_len; ++i)
5719 {
5720 match = ((char_u **)ga.ga_data)[i];
5721 s = match;
5722 e = s + STRLEN(s);
5723 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5724 {
5725 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005726 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005727 if (s < match || vim_ispathsep(*s))
5728 break;
5729 ++s;
5730 *e = NUL;
5731 mch_memmove(match, s, e - s + 1);
5732 }
5733 }
5734
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005735 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005736 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005737
5738 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005739 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005740 remove_duplicates(&ga);
5741
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 *file = ga.ga_data;
5743 *num_file = ga.ga_len;
5744 return OK;
5745}
5746
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005747/*
5748 * Expand loadplugin names:
5749 * 'packpath'/pack/ * /opt/{pat}
5750 */
5751 static int
5752ExpandPackAddDir(
5753 char_u *pat,
5754 int *num_file,
5755 char_u ***file)
5756{
5757 char_u *s;
5758 char_u *e;
5759 char_u *match;
5760 garray_T ga;
5761 int i;
5762 int pat_len;
5763
5764 *num_file = 0;
5765 *file = NULL;
5766 pat_len = (int)STRLEN(pat);
5767 ga_init2(&ga, (int)sizeof(char *), 10);
5768
Bram Moolenaar964b3742019-05-24 18:54:09 +02005769 s = alloc(pat_len + 26);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005770 if (s == NULL)
5771 {
5772 ga_clear_strings(&ga);
5773 return FAIL;
5774 }
5775 sprintf((char *)s, "pack/*/opt/%s*", pat);
5776 globpath(p_pp, s, &ga, 0);
5777 vim_free(s);
5778
5779 for (i = 0; i < ga.ga_len; ++i)
5780 {
5781 match = ((char_u **)ga.ga_data)[i];
5782 s = gettail(match);
5783 e = s + STRLEN(s);
5784 mch_memmove(match, s, e - s + 1);
5785 }
5786
5787 if (ga.ga_len == 0)
5788 return FAIL;
5789
5790 /* Sort and remove duplicates which can happen when specifying multiple
5791 * directories in dirnames. */
5792 remove_duplicates(&ga);
5793
5794 *file = ga.ga_data;
5795 *num_file = ga.ga_len;
5796 return OK;
5797}
5798
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799#endif
5800
5801#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5802/*
5803 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005804 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005806 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005807globpath(
5808 char_u *path,
5809 char_u *file,
5810 garray_T *ga,
5811 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812{
5813 expand_T xpc;
5814 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 int num_p;
5817 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818
5819 buf = alloc(MAXPATHL);
5820 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005821 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005823 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005825
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 /* Loop over all entries in {path}. */
5827 while (*path != NUL)
5828 {
5829 /* Copy one item of the path to buf[] and concatenate the file name. */
5830 copy_option_part(&path, buf, MAXPATHL, ",");
5831 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5832 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005833# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005834 /* Using the platform's path separator (\) makes vim incorrectly
5835 * treat it as an escape character, use '/' instead. */
5836 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5837 STRCAT(buf, "/");
5838# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005842 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5843 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005845 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005847 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 for (i = 0; i < num_p; ++i)
5850 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005851 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005852 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005853 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005856
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 FreeWild(num_p, p);
5858 }
5859 }
5860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861
5862 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863}
5864
5865#endif
5866
5867#if defined(FEAT_CMDHIST) || defined(PROTO)
5868
5869/*********************************
5870 * Command line history stuff *
5871 *********************************/
5872
5873/*
5874 * Translate a history character to the associated type number.
5875 */
5876 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005877hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878{
5879 if (c == ':')
5880 return HIST_CMD;
5881 if (c == '=')
5882 return HIST_EXPR;
5883 if (c == '@')
5884 return HIST_INPUT;
5885 if (c == '>')
5886 return HIST_DEBUG;
5887 return HIST_SEARCH; /* must be '?' or '/' */
5888}
5889
5890/*
5891 * Table of history names.
5892 * These names are used in :history and various hist...() functions.
5893 * It is sufficient to give the significant prefix of a history name.
5894 */
5895
5896static char *(history_names[]) =
5897{
5898 "cmd",
5899 "search",
5900 "expr",
5901 "input",
5902 "debug",
5903 NULL
5904};
5905
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005906#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5907/*
5908 * Function given to ExpandGeneric() to obtain the possible first
5909 * arguments of the ":history command.
5910 */
5911 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005912get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005913{
5914 static char_u compl[2] = { NUL, NUL };
5915 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005916 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005917 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5918
5919 if (idx < short_names_count)
5920 {
5921 compl[0] = (char_u)short_names[idx];
5922 return compl;
5923 }
5924 if (idx < short_names_count + history_name_count)
5925 return (char_u *)history_names[idx - short_names_count];
5926 if (idx == short_names_count + history_name_count)
5927 return (char_u *)"all";
5928 return NULL;
5929}
5930#endif
5931
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932/*
5933 * init_history() - Initialize the command line history.
5934 * Also used to re-allocate the history when the size changes.
5935 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005936 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005937init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938{
5939 int newlen; /* new length of history table */
5940 histentry_T *temp;
5941 int i;
5942 int j;
5943 int type;
5944
5945 /*
5946 * If size of history table changed, reallocate it
5947 */
5948 newlen = (int)p_hi;
5949 if (newlen != hislen) /* history length changed */
5950 {
5951 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5952 {
5953 if (newlen)
5954 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005955 temp = ALLOC_MULT(histentry_T, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956 if (temp == NULL) /* out of memory! */
5957 {
5958 if (type == 0) /* first one: just keep the old length */
5959 {
5960 newlen = hislen;
5961 break;
5962 }
5963 /* Already changed one table, now we can only have zero
5964 * length for all tables. */
5965 newlen = 0;
5966 type = -1;
5967 continue;
5968 }
5969 }
5970 else
5971 temp = NULL;
5972 if (newlen == 0 || temp != NULL)
5973 {
5974 if (hisidx[type] < 0) /* there are no entries yet */
5975 {
5976 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005977 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
5979 else if (newlen > hislen) /* array becomes bigger */
5980 {
5981 for (i = 0; i <= hisidx[type]; ++i)
5982 temp[i] = history[type][i];
5983 j = i;
5984 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005985 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 for ( ; j < hislen; ++i, ++j)
5987 temp[i] = history[type][j];
5988 }
5989 else /* array becomes smaller or 0 */
5990 {
5991 j = hisidx[type];
5992 for (i = newlen - 1; ; --i)
5993 {
5994 if (i >= 0) /* copy newest entries */
5995 temp[i] = history[type][j];
5996 else /* remove older entries */
5997 vim_free(history[type][j].hisstr);
5998 if (--j < 0)
5999 j = hislen - 1;
6000 if (j == hisidx[type])
6001 break;
6002 }
6003 hisidx[type] = newlen - 1;
6004 }
6005 vim_free(history[type]);
6006 history[type] = temp;
6007 }
6008 }
6009 hislen = newlen;
6010 }
6011}
6012
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006013 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006014clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006015{
6016 hisptr->hisnum = 0;
6017 hisptr->viminfo = FALSE;
6018 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006019 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006020}
6021
Bram Moolenaar071d4272004-06-13 20:20:40 +00006022/*
6023 * Check if command line 'str' is already in history.
6024 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6025 */
6026 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006027in_history(
6028 int type,
6029 char_u *str,
6030 int move_to_front, /* Move the entry to the front if it exists */
6031 int sep,
6032 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033{
6034 int i;
6035 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006036 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
6038 if (hisidx[type] < 0)
6039 return FALSE;
6040 i = hisidx[type];
6041 do
6042 {
6043 if (history[type][i].hisstr == NULL)
6044 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006045
6046 /* For search history, check that the separator character matches as
6047 * well. */
6048 p = history[type][i].hisstr;
6049 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006050 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006051 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006052 {
6053 if (!move_to_front)
6054 return TRUE;
6055 last_i = i;
6056 break;
6057 }
6058 if (--i < 0)
6059 i = hislen - 1;
6060 } while (i != hisidx[type]);
6061
6062 if (last_i >= 0)
6063 {
6064 str = history[type][i].hisstr;
6065 while (i != hisidx[type])
6066 {
6067 if (++i >= hislen)
6068 i = 0;
6069 history[type][last_i] = history[type][i];
6070 last_i = i;
6071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006073 history[type][i].viminfo = FALSE;
6074 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006075 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 return TRUE;
6077 }
6078 return FALSE;
6079}
6080
6081/*
6082 * Convert history name (from table above) to its HIST_ equivalent.
6083 * When "name" is empty, return "cmd" history.
6084 * Returns -1 for unknown history name.
6085 */
6086 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006087get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 int i;
6090 int len = (int)STRLEN(name);
6091
6092 /* No argument: use current history. */
6093 if (len == 0)
6094 return hist_char2type(ccline.cmdfirstc);
6095
6096 for (i = 0; history_names[i] != NULL; ++i)
6097 if (STRNICMP(name, history_names[i], len) == 0)
6098 return i;
6099
6100 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6101 return hist_char2type(name[0]);
6102
6103 return -1;
6104}
6105
6106static int last_maptick = -1; /* last seen maptick */
6107
6108/*
6109 * Add the given string to the given history. If the string is already in the
6110 * history then it is moved to the front. "histype" may be one of he HIST_
6111 * values.
6112 */
6113 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006114add_to_history(
6115 int histype,
6116 char_u *new_entry,
6117 int in_map, /* consider maptick when inside a mapping */
6118 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119{
6120 histentry_T *hisptr;
6121 int len;
6122
6123 if (hislen == 0) /* no history */
6124 return;
6125
Bram Moolenaara939e432013-11-09 05:30:26 +01006126 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6127 return;
6128
Bram Moolenaar071d4272004-06-13 20:20:40 +00006129 /*
6130 * Searches inside the same mapping overwrite each other, so that only
6131 * the last line is kept. Be careful not to remove a line that was moved
6132 * down, only lines that were added.
6133 */
6134 if (histype == HIST_SEARCH && in_map)
6135 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006136 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 {
6138 /* Current line is from the same mapping, remove it */
6139 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6140 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006141 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 --hisnum[histype];
6143 if (--hisidx[HIST_SEARCH] < 0)
6144 hisidx[HIST_SEARCH] = hislen - 1;
6145 }
6146 last_maptick = -1;
6147 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006148 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 {
6150 if (++hisidx[histype] == hislen)
6151 hisidx[histype] = 0;
6152 hisptr = &history[histype][hisidx[histype]];
6153 vim_free(hisptr->hisstr);
6154
6155 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006156 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6158 if (hisptr->hisstr != NULL)
6159 hisptr->hisstr[len + 1] = sep;
6160
6161 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006162 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006163 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (histype == HIST_SEARCH && in_map)
6165 last_maptick = maptick;
6166 }
6167}
6168
6169#if defined(FEAT_EVAL) || defined(PROTO)
6170
6171/*
6172 * Get identifier of newest history entry.
6173 * "histype" may be one of the HIST_ values.
6174 */
6175 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006176get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6179 || hisidx[histype] < 0)
6180 return -1;
6181
6182 return history[histype][hisidx[histype]].hisnum;
6183}
6184
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006185/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 * Calculate history index from a number:
6187 * num > 0: seen as identifying number of a history entry
6188 * num < 0: relative position in history wrt newest entry
6189 * "histype" may be one of the HIST_ values.
6190 */
6191 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006192calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
6194 int i;
6195 histentry_T *hist;
6196 int wrapped = FALSE;
6197
6198 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6199 || (i = hisidx[histype]) < 0 || num == 0)
6200 return -1;
6201
6202 hist = history[histype];
6203 if (num > 0)
6204 {
6205 while (hist[i].hisnum > num)
6206 if (--i < 0)
6207 {
6208 if (wrapped)
6209 break;
6210 i += hislen;
6211 wrapped = TRUE;
6212 }
6213 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6214 return i;
6215 }
6216 else if (-num <= hislen)
6217 {
6218 i += num + 1;
6219 if (i < 0)
6220 i += hislen;
6221 if (hist[i].hisstr != NULL)
6222 return i;
6223 }
6224 return -1;
6225}
6226
6227/*
6228 * Get a history entry by its index.
6229 * "histype" may be one of the HIST_ values.
6230 */
6231 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006232get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233{
6234 idx = calc_hist_idx(histype, idx);
6235 if (idx >= 0)
6236 return history[histype][idx].hisstr;
6237 else
6238 return (char_u *)"";
6239}
6240
6241/*
6242 * Clear all entries of a history.
6243 * "histype" may be one of the HIST_ values.
6244 */
6245 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 int i;
6249 histentry_T *hisptr;
6250
6251 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6252 {
6253 hisptr = history[histype];
6254 for (i = hislen; i--;)
6255 {
6256 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006257 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006258 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 }
6260 hisidx[histype] = -1; /* mark history as cleared */
6261 hisnum[histype] = 0; /* reset identifier counter */
6262 return OK;
6263 }
6264 return FAIL;
6265}
6266
6267/*
6268 * Remove all entries matching {str} from a history.
6269 * "histype" may be one of the HIST_ values.
6270 */
6271 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006272del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273{
6274 regmatch_T regmatch;
6275 histentry_T *hisptr;
6276 int idx;
6277 int i;
6278 int last;
6279 int found = FALSE;
6280
6281 regmatch.regprog = NULL;
6282 regmatch.rm_ic = FALSE; /* always match case */
6283 if (hislen != 0
6284 && histype >= 0
6285 && histype < HIST_COUNT
6286 && *str != NUL
6287 && (idx = hisidx[histype]) >= 0
6288 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6289 != NULL)
6290 {
6291 i = last = idx;
6292 do
6293 {
6294 hisptr = &history[histype][i];
6295 if (hisptr->hisstr == NULL)
6296 break;
6297 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6298 {
6299 found = TRUE;
6300 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006301 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303 else
6304 {
6305 if (i != last)
6306 {
6307 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006308 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 }
6310 if (--last < 0)
6311 last += hislen;
6312 }
6313 if (--i < 0)
6314 i += hislen;
6315 } while (i != idx);
6316 if (history[histype][idx].hisstr == NULL)
6317 hisidx[histype] = -1;
6318 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006319 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006320 return found;
6321}
6322
6323/*
6324 * Remove an indexed entry from a history.
6325 * "histype" may be one of the HIST_ values.
6326 */
6327 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006328del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 int i, j;
6331
6332 i = calc_hist_idx(histype, idx);
6333 if (i < 0)
6334 return FALSE;
6335 idx = hisidx[histype];
6336 vim_free(history[histype][i].hisstr);
6337
6338 /* When deleting the last added search string in a mapping, reset
6339 * last_maptick, so that the last added search string isn't deleted again.
6340 */
6341 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6342 last_maptick = -1;
6343
6344 while (i != idx)
6345 {
6346 j = (i + 1) % hislen;
6347 history[histype][i] = history[histype][j];
6348 i = j;
6349 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006350 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 if (--i < 0)
6352 i += hislen;
6353 hisidx[histype] = i;
6354 return TRUE;
6355}
6356
6357#endif /* FEAT_EVAL */
6358
6359#if defined(FEAT_CRYPT) || defined(PROTO)
6360/*
6361 * Very specific function to remove the value in ":set key=val" from the
6362 * history.
6363 */
6364 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006365remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366{
6367 char_u *p;
6368 int i;
6369
6370 i = hisidx[HIST_CMD];
6371 if (i < 0)
6372 return;
6373 p = history[HIST_CMD][i].hisstr;
6374 if (p != NULL)
6375 for ( ; *p; ++p)
6376 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6377 {
6378 p = vim_strchr(p + 3, '=');
6379 if (p == NULL)
6380 break;
6381 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006382 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 if (p[i] == '\\' && p[i + 1])
6384 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006385 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386 --p;
6387 }
6388}
6389#endif
6390
6391#endif /* FEAT_CMDHIST */
6392
Bram Moolenaar064154c2016-03-19 22:50:43 +01006393#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006394/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006395 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006396 * ccline and put the previous value in prev_ccline.
6397 */
6398 static struct cmdline_info *
6399get_ccline_ptr(void)
6400{
6401 if ((State & CMDLINE) == 0)
6402 return NULL;
6403 if (ccline.cmdbuff != NULL)
6404 return &ccline;
6405 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6406 return &prev_ccline;
6407 return NULL;
6408}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006409#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006410
Bram Moolenaar064154c2016-03-19 22:50:43 +01006411#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006412/*
6413 * Get the current command line in allocated memory.
6414 * Only works when the command line is being edited.
6415 * Returns NULL when something is wrong.
6416 */
6417 char_u *
6418get_cmdline_str(void)
6419{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006420 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006421
Bram Moolenaaree91c332018-09-25 22:27:35 +02006422 if (cmdline_star > 0)
6423 return NULL;
6424 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006425 if (p == NULL)
6426 return NULL;
6427 return vim_strnsave(p->cmdbuff, p->cmdlen);
6428}
6429
6430/*
6431 * Get the current command line position, counted in bytes.
6432 * Zero is the first position.
6433 * Only works when the command line is being edited.
6434 * Returns -1 when something is wrong.
6435 */
6436 int
6437get_cmdline_pos(void)
6438{
6439 struct cmdline_info *p = get_ccline_ptr();
6440
6441 if (p == NULL)
6442 return -1;
6443 return p->cmdpos;
6444}
6445
6446/*
6447 * Set the command line byte position to "pos". Zero is the first position.
6448 * Only works when the command line is being edited.
6449 * Returns 1 when failed, 0 when OK.
6450 */
6451 int
6452set_cmdline_pos(
6453 int pos)
6454{
6455 struct cmdline_info *p = get_ccline_ptr();
6456
6457 if (p == NULL)
6458 return 1;
6459
6460 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6461 * changed the command line. */
6462 if (pos < 0)
6463 new_cmdpos = 0;
6464 else
6465 new_cmdpos = pos;
6466 return 0;
6467}
6468#endif
6469
6470#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6471/*
6472 * Get the current command-line type.
6473 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6474 * Only works when the command line is being edited.
6475 * Returns NUL when something is wrong.
6476 */
6477 int
6478get_cmdline_type(void)
6479{
6480 struct cmdline_info *p = get_ccline_ptr();
6481
6482 if (p == NULL)
6483 return NUL;
6484 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006485 return
6486# ifdef FEAT_EVAL
6487 (p->input_fn) ? '@' :
6488# endif
6489 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006490 return p->cmdfirstc;
6491}
6492#endif
6493
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6495/*
6496 * Get indices "num1,num2" that specify a range within a list (not a range of
6497 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6498 * Returns OK if parsed successfully, otherwise FAIL.
6499 */
6500 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006501get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
6503 int len;
6504 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006505 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006506
6507 *str = skipwhite(*str);
6508 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6509 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006510 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 *str += len;
6512 *num1 = (int)num;
6513 first = TRUE;
6514 }
6515 *str = skipwhite(*str);
6516 if (**str == ',') /* parse "to" part of range */
6517 {
6518 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006519 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 if (len > 0)
6521 {
6522 *num2 = (int)num;
6523 *str = skipwhite(*str + len);
6524 }
6525 else if (!first) /* no number given at all */
6526 return FAIL;
6527 }
6528 else if (first) /* only one number given */
6529 *num2 = *num1;
6530 return OK;
6531}
6532#endif
6533
6534#if defined(FEAT_CMDHIST) || defined(PROTO)
6535/*
6536 * :history command - print a history
6537 */
6538 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006539ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540{
6541 histentry_T *hist;
6542 int histype1 = HIST_CMD;
6543 int histype2 = HIST_CMD;
6544 int hisidx1 = 1;
6545 int hisidx2 = -1;
6546 int idx;
6547 int i, j, k;
6548 char_u *end;
6549 char_u *arg = eap->arg;
6550
6551 if (hislen == 0)
6552 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006553 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 return;
6555 }
6556
6557 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6558 {
6559 end = arg;
6560 while (ASCII_ISALPHA(*end)
6561 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6562 end++;
6563 i = *end;
6564 *end = NUL;
6565 histype1 = get_histtype(arg);
6566 if (histype1 == -1)
6567 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006568 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
6570 histype1 = 0;
6571 histype2 = HIST_COUNT-1;
6572 }
6573 else
6574 {
6575 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006576 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 return;
6578 }
6579 }
6580 else
6581 histype2 = histype1;
6582 *end = i;
6583 }
6584 else
6585 end = arg;
6586 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6587 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006588 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589 return;
6590 }
6591
6592 for (; !got_int && histype1 <= histype2; ++histype1)
6593 {
6594 STRCPY(IObuff, "\n # ");
6595 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006596 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 idx = hisidx[histype1];
6598 hist = history[histype1];
6599 j = hisidx1;
6600 k = hisidx2;
6601 if (j < 0)
6602 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6603 if (k < 0)
6604 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6605 if (idx >= 0 && j <= k)
6606 for (i = idx + 1; !got_int; ++i)
6607 {
6608 if (i == hislen)
6609 i = 0;
6610 if (hist[i].hisstr != NULL
6611 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6612 {
6613 msg_putchar('\n');
6614 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6615 hist[i].hisnum);
6616 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6617 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006618 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 else
6620 STRCAT(IObuff, hist[i].hisstr);
6621 msg_outtrans(IObuff);
6622 out_flush();
6623 }
6624 if (i == idx)
6625 break;
6626 }
6627 }
6628}
6629#endif
6630
6631#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006632/*
6633 * Buffers for history read from a viminfo file. Only valid while reading.
6634 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006635static histentry_T *viminfo_history[HIST_COUNT] =
6636 {NULL, NULL, NULL, NULL, NULL};
6637static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6638static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639static int viminfo_add_at_front = FALSE;
6640
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641/*
6642 * Translate a history type number to the associated character.
6643 */
6644 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006645hist_type2char(
6646 int type,
6647 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 if (type == HIST_CMD)
6650 return ':';
6651 if (type == HIST_SEARCH)
6652 {
6653 if (use_question)
6654 return '?';
6655 else
6656 return '/';
6657 }
6658 if (type == HIST_EXPR)
6659 return '=';
6660 return '@';
6661}
6662
6663/*
6664 * Prepare for reading the history from the viminfo file.
6665 * This allocates history arrays to store the read history lines.
6666 */
6667 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006668prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669{
6670 int i;
6671 int num;
6672 int type;
6673 int len;
6674
6675 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006676 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (asklen > hislen)
6678 asklen = hislen;
6679
6680 for (type = 0; type < HIST_COUNT; ++type)
6681 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006682 /* Count the number of empty spaces in the history list. Entries read
6683 * from viminfo previously are also considered empty. If there are
6684 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006686 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 num++;
6688 len = asklen;
6689 if (num > len)
6690 len = num;
6691 if (len <= 0)
6692 viminfo_history[type] = NULL;
6693 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006694 viminfo_history[type] = LALLOC_MULT(histentry_T, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 if (viminfo_history[type] == NULL)
6696 len = 0;
6697 viminfo_hislen[type] = len;
6698 viminfo_hisidx[type] = 0;
6699 }
6700}
6701
6702/*
6703 * Accept a line from the viminfo, store it in the history array when it's
6704 * new.
6705 */
6706 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006707read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006708{
6709 int type;
6710 long_u len;
6711 char_u *val;
6712 char_u *p;
6713
6714 type = hist_char2type(virp->vir_line[0]);
6715 if (viminfo_hisidx[type] < viminfo_hislen[type])
6716 {
6717 val = viminfo_readstring(virp, 1, TRUE);
6718 if (val != NULL && *val != NUL)
6719 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006720 int sep = (*val == ' ' ? NUL : *val);
6721
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006723 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 {
6725 /* Need to re-allocate to append the separator byte. */
6726 len = STRLEN(val);
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006727 p = alloc(len + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 if (p != NULL)
6729 {
6730 if (type == HIST_SEARCH)
6731 {
6732 /* Search entry: Move the separator from the first
6733 * column to after the NUL. */
6734 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006735 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737 else
6738 {
6739 /* Not a search entry: No separator in the viminfo
6740 * file, add a NUL separator. */
6741 mch_memmove(p, val, (size_t)len + 1);
6742 p[len + 1] = NUL;
6743 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006744 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6745 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006746 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6747 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006748 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750 }
6751 }
6752 vim_free(val);
6753 }
6754 return viminfo_readline(virp);
6755}
6756
Bram Moolenaar07219f92013-04-14 23:19:36 +02006757/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006758 * Accept a new style history line from the viminfo, store it in the history
6759 * array when it's new.
6760 */
6761 void
6762handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006763 garray_T *values,
6764 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006765{
6766 int type;
6767 long_u len;
6768 char_u *val;
6769 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006770 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006771
6772 /* Check the format:
6773 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006774 if (values->ga_len < 4
6775 || vp[0].bv_type != BVAL_NR
6776 || vp[1].bv_type != BVAL_NR
6777 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6778 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006779 return;
6780
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006781 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006782 if (type >= HIST_COUNT)
6783 return;
6784 if (viminfo_hisidx[type] < viminfo_hislen[type])
6785 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006786 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006787 if (val != NULL && *val != NUL)
6788 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006789 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6790 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006791 int idx;
6792 int overwrite = FALSE;
6793
6794 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6795 {
6796 /* If lines were written by an older Vim we need to avoid
6797 * getting duplicates. See if the entry already exists. */
6798 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6799 {
6800 p = viminfo_history[type][idx].hisstr;
6801 if (STRCMP(val, p) == 0
6802 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6803 {
6804 overwrite = TRUE;
6805 break;
6806 }
6807 }
6808
6809 if (!overwrite)
6810 {
6811 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006812 len = vp[3].bv_len;
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02006813 p = alloc(len + 2);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006814 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006815 else
6816 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006817 if (p != NULL)
6818 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006819 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006820 if (!overwrite)
6821 {
6822 mch_memmove(p, val, (size_t)len + 1);
6823 /* Put the separator after the NUL. */
6824 p[len + 1] = sep;
6825 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006826 viminfo_history[type][idx].hisnum = 0;
6827 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006828 viminfo_hisidx[type]++;
6829 }
6830 }
6831 }
6832 }
6833 }
6834}
6835
6836/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006837 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006838 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006839 static void
6840concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 int idx;
6843 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006844
6845 idx = hisidx[type] + viminfo_hisidx[type];
6846 if (idx >= hislen)
6847 idx -= hislen;
6848 else if (idx < 0)
6849 idx = hislen - 1;
6850 if (viminfo_add_at_front)
6851 hisidx[type] = idx;
6852 else
6853 {
6854 if (hisidx[type] == -1)
6855 hisidx[type] = hislen - 1;
6856 do
6857 {
6858 if (history[type][idx].hisstr != NULL
6859 || history[type][idx].viminfo)
6860 break;
6861 if (++idx == hislen)
6862 idx = 0;
6863 } while (idx != hisidx[type]);
6864 if (idx != hisidx[type] && --idx < 0)
6865 idx = hislen - 1;
6866 }
6867 for (i = 0; i < viminfo_hisidx[type]; i++)
6868 {
6869 vim_free(history[type][idx].hisstr);
6870 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6871 history[type][idx].viminfo = TRUE;
6872 history[type][idx].time_set = viminfo_history[type][i].time_set;
6873 if (--idx < 0)
6874 idx = hislen - 1;
6875 }
6876 idx += 1;
6877 idx %= hislen;
6878 for (i = 0; i < viminfo_hisidx[type]; i++)
6879 {
6880 history[type][idx++].hisnum = ++hisnum[type];
6881 idx %= hislen;
6882 }
6883}
6884
6885#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6886 static int
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006887sort_hist(const void *s1, const void *s2)
6888{
6889 histentry_T *p1 = *(histentry_T **)s1;
6890 histentry_T *p2 = *(histentry_T **)s2;
6891
6892 if (p1->time_set < p2->time_set) return -1;
6893 if (p1->time_set > p2->time_set) return 1;
6894 return 0;
6895}
6896#endif
6897
6898/*
6899 * Merge history lines from viminfo and lines typed in this Vim based on the
6900 * timestamp;
6901 */
6902 static void
6903merge_history(int type)
6904{
6905 int max_len;
6906 histentry_T **tot_hist;
6907 histentry_T *new_hist;
6908 int i;
6909 int len;
6910
6911 /* Make one long list with all entries. */
6912 max_len = hislen + viminfo_hisidx[type];
Bram Moolenaarc799fe22019-05-28 23:08:19 +02006913 tot_hist = ALLOC_MULT(histentry_T *, max_len);
6914 new_hist = ALLOC_MULT(histentry_T, hislen );
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006915 if (tot_hist == NULL || new_hist == NULL)
6916 {
6917 vim_free(tot_hist);
6918 vim_free(new_hist);
6919 return;
6920 }
6921 for (i = 0; i < viminfo_hisidx[type]; i++)
6922 tot_hist[i] = &viminfo_history[type][i];
6923 len = i;
6924 for (i = 0; i < hislen; i++)
6925 if (history[type][i].hisstr != NULL)
6926 tot_hist[len++] = &history[type][i];
6927
6928 /* Sort the list on timestamp. */
6929 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6930
6931 /* Keep the newest ones. */
6932 for (i = 0; i < hislen; i++)
6933 {
6934 if (i < len)
6935 {
6936 new_hist[i] = *tot_hist[i];
6937 tot_hist[i]->hisstr = NULL;
6938 if (new_hist[i].hisnum == 0)
6939 new_hist[i].hisnum = ++hisnum[type];
6940 }
6941 else
6942 clear_hist_entry(&new_hist[i]);
6943 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006944 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006945
6946 /* Free what is not kept. */
6947 for (i = 0; i < viminfo_hisidx[type]; i++)
6948 vim_free(viminfo_history[type][i].hisstr);
6949 for (i = 0; i < hislen; i++)
6950 vim_free(history[type][i].hisstr);
6951 vim_free(history[type]);
6952 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006953 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006954}
6955
6956/*
6957 * Finish reading history lines from viminfo. Not used when writing viminfo.
6958 */
6959 void
6960finish_viminfo_history(vir_T *virp)
6961{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006963 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964
6965 for (type = 0; type < HIST_COUNT; ++type)
6966 {
6967 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006968 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006969
6970 if (merge)
6971 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006973 concat_history(type);
6974
Bram Moolenaard23a8232018-02-10 18:45:26 +01006975 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006976 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978}
6979
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006980/*
6981 * Write history to viminfo file in "fp".
6982 * When "merge" is TRUE merge history lines with a previously read viminfo
6983 * file, data is in viminfo_history[].
6984 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006987write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988{
6989 int i;
6990 int type;
6991 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006992 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993
6994 init_history();
6995 if (hislen == 0)
6996 return;
6997 for (type = 0; type < HIST_COUNT; ++type)
6998 {
6999 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7000 if (num_saved == 0)
7001 continue;
7002 if (num_saved < 0) /* Use default */
7003 num_saved = hislen;
7004 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7005 type == HIST_CMD ? _("Command Line") :
7006 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007007 type == HIST_EXPR ? _("Expression") :
7008 type == HIST_INPUT ? _("Input Line") :
7009 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 if (num_saved > hislen)
7011 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007012
7013 /*
7014 * Merge typed and viminfo history:
7015 * round 1: history of typed commands.
7016 * round 2: history from recently read viminfo.
7017 */
7018 for (round = 1; round <= 2; ++round)
7019 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007020 if (round == 1)
7021 /* start at newest entry, somewhere in the list */
7022 i = hisidx[type];
7023 else if (viminfo_hisidx[type] > 0)
7024 /* start at newest entry, first in the list */
7025 i = 0;
7026 else
7027 /* empty list */
7028 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007029 if (i >= 0)
7030 while (num_saved > 0
7031 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007033 char_u *p;
7034 time_t timestamp;
7035 int c = NUL;
7036
7037 if (round == 1)
7038 {
7039 p = history[type][i].hisstr;
7040 timestamp = history[type][i].time_set;
7041 }
7042 else
7043 {
7044 p = viminfo_history[type] == NULL ? NULL
7045 : viminfo_history[type][i].hisstr;
7046 timestamp = viminfo_history[type] == NULL ? 0
7047 : viminfo_history[type][i].time_set;
7048 }
7049
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007050 if (p != NULL && (round == 2
7051 || !merge
7052 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007054 --num_saved;
7055 fputc(hist_type2char(type, TRUE), fp);
7056 /* For the search history: put the separator in the
7057 * second column; use a space if there isn't one. */
7058 if (type == HIST_SEARCH)
7059 {
7060 c = p[STRLEN(p) + 1];
7061 putc(c == NUL ? ' ' : c, fp);
7062 }
7063 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007064
7065 {
7066 char cbuf[NUMBUFLEN];
7067
7068 /* New style history with a bar line. Format:
7069 * |{bartype},{histtype},{timestamp},{separator},"text" */
7070 if (c == NUL)
7071 cbuf[0] = NUL;
7072 else
7073 sprintf(cbuf, "%d", c);
7074 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7075 type, (long)timestamp, cbuf);
7076 barline_writestring(fp, p, LSIZE - 20);
7077 putc('\n', fp);
7078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007080 if (round == 1)
7081 {
7082 /* Decrement index, loop around and stop when back at
7083 * the start. */
7084 if (--i < 0)
7085 i = hislen - 1;
7086 if (i == hisidx[type])
7087 break;
7088 }
7089 else
7090 {
7091 /* Increment index. Stop at the end in the while. */
7092 ++i;
7093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007095 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007096 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007097 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007098 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007099 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007100 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 }
7102}
7103#endif /* FEAT_VIMINFO */
7104
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105#if defined(FEAT_CMDWIN) || defined(PROTO)
7106/*
7107 * Open a window on the current command line and history. Allow editing in
7108 * the window. Returns when the window is closed.
7109 * Returns:
7110 * CR if the command is to be executed
7111 * Ctrl_C if it is to be abandoned
7112 * K_IGNORE if editing continues
7113 */
7114 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007115open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007116{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007117 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007119 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 win_T *wp;
7121 int i;
7122 linenr_T lnum;
7123 int histtype;
7124 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 int save_restart_edit = restart_edit;
7126 int save_State = State;
7127 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007128#ifdef FEAT_RIGHTLEFT
7129 int save_cmdmsg_rl = cmdmsg_rl;
7130#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007131#ifdef FEAT_FOLDING
7132 int save_KeyTyped;
7133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
7135 /* Can't do this recursively. Can't do it when typing a password. */
7136 if (cmdwin_type != 0
7137# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7138 || cmdline_star > 0
7139# endif
7140 )
7141 {
7142 beep_flush();
7143 return K_IGNORE;
7144 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007145 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146
7147 /* Save current window sizes. */
7148 win_size_save(&winsizes);
7149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007151 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007152
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007153#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007154 // When using completion in Insert mode with <C-R>=<C-F> one can open the
7155 // command line window, but we don't want the popup menu then.
7156 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007157#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007158
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007159 /* don't use a new tab page */
7160 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007161 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007162
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 /* Create a window for the command-line buffer. */
7164 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7165 {
7166 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007167 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 return K_IGNORE;
7169 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007170 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
7172 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007173 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007174 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007177#ifdef FEAT_FOLDING
7178 curwin->w_p_fen = FALSE;
7179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007181 curwin->w_p_rl = cmdmsg_rl;
7182 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007184 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007187 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007188 /* But don't allow switching to another buffer. */
7189 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190
Bram Moolenaar46152342005-09-07 21:18:43 +00007191 /* Showing the prompt may have set need_wait_return, reset it. */
7192 need_wait_return = FALSE;
7193
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007194 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7196 {
7197 if (p_wc == TAB)
7198 {
7199 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7200 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7201 }
7202 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7203 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007204 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007206 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7207 * sets 'textwidth' to 78). */
7208 curbuf->b_p_tw = 0;
7209
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210 /* Fill the buffer with the history. */
7211 init_history();
7212 if (hislen > 0)
7213 {
7214 i = hisidx[histtype];
7215 if (i >= 0)
7216 {
7217 lnum = 0;
7218 do
7219 {
7220 if (++i == hislen)
7221 i = 0;
7222 if (history[histtype][i].hisstr != NULL)
7223 ml_append(lnum++, history[histtype][i].hisstr,
7224 (colnr_T)0, FALSE);
7225 }
7226 while (i != hisidx[histtype]);
7227 }
7228 }
7229
7230 /* Replace the empty last line with the current command-line and put the
7231 * cursor there. */
7232 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7233 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7234 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007235 changed_line_abv_curs();
7236 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007237 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 /* No Ex mode here! */
7240 exmode_active = 0;
7241
7242 State = NORMAL;
7243# ifdef FEAT_MOUSE
7244 setmouse();
7245# endif
7246
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007248 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007249 if (restart_edit != 0) /* autocmd with ":startinsert" */
7250 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
7252 i = RedrawingDisabled;
7253 RedrawingDisabled = 0;
7254
7255 /*
7256 * Call the main loop until <CR> or CTRL-C is typed.
7257 */
7258 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007259 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
7261 RedrawingDisabled = i;
7262
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007263# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007264 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007265# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007266
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007268 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007269
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007270# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007271 /* Restore KeyTyped in case it is modified by autocommands */
7272 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273# endif
7274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 exmode_active = save_exmode;
7277
Bram Moolenaarf9821062008-06-20 16:31:07 +00007278 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007280 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281 {
7282 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007283 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 }
7285 else
7286 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007287# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 /* autocmds may abort script processing */
7289 if (aborting() && cmdwin_result != K_IGNORE)
7290 cmdwin_result = Ctrl_C;
7291# endif
7292 /* Set the new command line from the cmdline buffer. */
7293 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007294 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007296 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7297
7298 if (histtype == HIST_CMD)
7299 {
7300 /* Execute the command directly. */
7301 ccline.cmdbuff = vim_strsave((char_u *)p);
7302 cmdwin_result = CAR;
7303 }
7304 else
7305 {
7306 /* First need to cancel what we were doing. */
7307 ccline.cmdbuff = NULL;
7308 stuffcharReadbuff(':');
7309 stuffReadbuff((char_u *)p);
7310 stuffcharReadbuff(CAR);
7311 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 }
7313 else if (cmdwin_result == K_XF2) /* :qa typed */
7314 {
7315 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7316 cmdwin_result = CAR;
7317 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007318 else if (cmdwin_result == Ctrl_C)
7319 {
7320 /* :q or :close, don't execute any command
7321 * and don't modify the cmd window. */
7322 ccline.cmdbuff = NULL;
7323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324 else
7325 ccline.cmdbuff = vim_strsave(ml_get_curline());
7326 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007327 {
7328 ccline.cmdbuff = vim_strsave((char_u *)"");
7329 ccline.cmdlen = 0;
7330 ccline.cmdbufflen = 1;
7331 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334 else
7335 {
7336 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7337 ccline.cmdbufflen = ccline.cmdlen + 1;
7338 ccline.cmdpos = curwin->w_cursor.col;
7339 if (ccline.cmdpos > ccline.cmdlen)
7340 ccline.cmdpos = ccline.cmdlen;
7341 if (cmdwin_result == K_IGNORE)
7342 {
7343 set_cmdspos_cursor();
7344 redrawcmd();
7345 }
7346 }
7347
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007349 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007350# ifdef FEAT_CONCEAL
7351 /* Avoid command-line window first character being concealed. */
7352 curwin->w_p_cole = 0;
7353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007354 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007355 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356 win_goto(old_curwin);
7357 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007358
7359 /* win_close() may have already wiped the buffer when 'bh' is
7360 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007361 if (bufref_valid(&bufref))
7362 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363
7364 /* Restore window sizes. */
7365 win_size_restore(&winsizes);
7366
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007367 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007368 }
7369
7370 ga_clear(&winsizes);
7371 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007372# ifdef FEAT_RIGHTLEFT
7373 cmdmsg_rl = save_cmdmsg_rl;
7374# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007375
7376 State = save_State;
7377# ifdef FEAT_MOUSE
7378 setmouse();
7379# endif
7380
7381 return cmdwin_result;
7382}
7383#endif /* FEAT_CMDWIN */
7384
7385/*
7386 * Used for commands that either take a simple command string argument, or:
7387 * cmd << endmarker
7388 * {script}
7389 * endmarker
7390 * Returns a pointer to allocated memory with {script} or NULL.
7391 */
7392 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007393script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394{
7395 char_u *theline;
7396 char *end_pattern = NULL;
7397 char dot[] = ".";
7398 garray_T ga;
7399
7400 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7401 return NULL;
7402
7403 ga_init2(&ga, 1, 0x400);
7404
7405 if (cmd[2] != NUL)
7406 end_pattern = (char *)skipwhite(cmd + 2);
7407 else
7408 end_pattern = dot;
7409
7410 for (;;)
7411 {
7412 theline = eap->getline(
7413#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007414 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#endif
Bram Moolenaare96a2492019-06-25 04:12:16 +02007416 NUL, eap->cookie, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417
7418 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007419 {
7420 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
7424 ga_concat(&ga, theline);
7425 ga_append(&ga, '\n');
7426 vim_free(theline);
7427 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007428 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429
7430 return (char_u *)ga.ga_data;
7431}