blob: 00193fee881a7bd1d4b6c11fa204520113ea757d [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 Moolenaar0ee81cb2018-08-10 22:07:32 +0200453
Bram Moolenaar198cb662018-09-06 21:44:17 +0200454 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100455 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200456 save_last_search_pattern();
457
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200458 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200459 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200460 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200461 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200462 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200463 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200464
465 // If there is a character waiting, search and redraw later.
466 if (char_avail())
467 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200468 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200469 is_state->incsearch_postponed = TRUE;
470 return;
471 }
472 is_state->incsearch_postponed = FALSE;
473
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200474 if (search_first_line == 0)
475 // start at the original cursor position
476 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100477 else if (search_first_line > curbuf->b_ml.ml_line_count)
478 {
479 // start after the last line
480 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
481 curwin->w_cursor.col = MAXCOL;
482 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200483 else
484 {
485 // start at the first line in the range
486 curwin->w_cursor.lnum = search_first_line;
487 curwin->w_cursor.col = 0;
488 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200489
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200490 // Use the previous pattern for ":s//".
491 next_char = ccline.cmdbuff[skiplen + patlen];
492 use_last_pat = patlen == 0 && skiplen > 0
493 && ccline.cmdbuff[skiplen - 1] == next_char;
494
495 // If there is no pattern, don't do anything.
496 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200497 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200498 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200499 set_no_hlsearch(TRUE); // turn off previous highlight
500 redraw_all_later(SOME_VALID);
501 }
502 else
503 {
504 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
505
506 cursor_off(); // so the user knows we're busy
507 out_flush();
508 ++emsg_off; // so it doesn't beep if bad expr
509#ifdef FEAT_RELTIME
510 // Set the time limit to half a second.
511 profile_setlimit(500L, &tm);
512#endif
513 if (!p_hls)
514 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200515 if (search_first_line != 0)
516 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200517 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200518 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200519 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200520#ifdef FEAT_RELTIME
521 &tm, NULL
522#else
523 NULL, NULL
524#endif
525 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200526 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200527 --emsg_off;
528
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200529 if (curwin->w_cursor.lnum < search_first_line
530 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200531 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200532 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200533 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200534 curwin->w_cursor = is_state->search_start;
535 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200536
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200537 // if interrupted while searching, behave like it failed
538 if (got_int)
539 {
540 (void)vpeekc(); // remove <C-C> from input stream
541 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200542 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200543 }
544 else if (char_avail())
545 // cancelled searching because a char was typed
546 is_state->incsearch_postponed = TRUE;
547 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200548 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200549 highlight_match = TRUE; // highlight position
550 else
551 highlight_match = FALSE; // remove highlight
552
553 // First restore the old curwin values, so the screen is positioned in the
554 // same way as the actual search command.
555 restore_viewstate(&is_state->old_viewstate);
556 changed_cline_bef_curs();
557 update_topline();
558
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200559 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200560 {
561 pos_T save_pos = curwin->w_cursor;
562
563 is_state->match_start = curwin->w_cursor;
564 set_search_match(&curwin->w_cursor);
565 validate_cursor();
566 end_pos = curwin->w_cursor;
567 is_state->match_end = end_pos;
568 curwin->w_cursor = save_pos;
569 }
570 else
571 end_pos = curwin->w_cursor; // shutup gcc 4
572
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200573 // Disable 'hlsearch' highlighting if the pattern matches everything.
574 // Avoids a flash when typing "foo\|".
575 if (!use_last_pat)
576 {
577 next_char = ccline.cmdbuff[skiplen + patlen];
578 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200579 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
580 {
581 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200582 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200583 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200584 ccline.cmdbuff[skiplen + patlen] = next_char;
585 }
586
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200587 validate_cursor();
588 // May redraw the status line to show the cursor position.
589 if (p_ru && curwin->w_status_height > 0)
590 curwin->w_redr_status = TRUE;
591
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200592 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 restore_last_search_pattern();
594
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200595 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
596 // end of the pattern, e.g. for ":s/pat/".
597 if (ccline.cmdbuff[skiplen + patlen] != NUL)
598 curwin->w_cursor = is_state->search_start;
599 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200600 curwin->w_cursor = end_pos;
601
602 msg_starthere();
603 redrawcmdline();
604 is_state->did_incsearch = TRUE;
605}
606
607/*
608 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
609 * or previous match.
610 * Returns FAIL when jumping to cmdline_not_changed;
611 */
612 static int
613may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200614 int firstc,
615 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200617 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200618{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200619 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 pos_T t;
621 char_u *pat;
622 int search_flags = SEARCH_NOOF;
623 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200624 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200625
Bram Moolenaar198cb662018-09-06 21:44:17 +0200626 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100627 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200628 save_last_search_pattern();
629
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200630 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200631 {
632 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200633 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200634 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200635 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200636 {
637 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200638 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200639 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200640
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200641 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200642 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200643 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200644 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200645 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200646 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200647 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200648 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200649
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200650 cursor_off();
651 out_flush();
652 if (c == Ctrl_G)
653 {
654 t = is_state->match_end;
655 if (LT_POS(is_state->match_start, is_state->match_end))
656 // Start searching at the end of the match not at the beginning of
657 // the next column.
658 (void)decl(&t);
659 search_flags += SEARCH_COL;
660 }
661 else
662 t = is_state->match_start;
663 if (!p_hls)
664 search_flags += SEARCH_KEEP;
665 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200666 save = pat[patlen];
667 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100668 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200669 c == Ctrl_G ? FORWARD : BACKWARD,
670 pat, count, search_flags,
671 RE_SEARCH, 0, NULL, NULL);
672 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200673 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200674 if (i)
675 {
676 is_state->search_start = is_state->match_start;
677 is_state->match_end = t;
678 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200679 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200680 {
681 // Move just before the current match, so that when nv_search
682 // finishes the cursor will be put back on the match.
683 is_state->search_start = t;
684 (void)decl(&is_state->search_start);
685 }
686 else if (c == Ctrl_G && firstc == '?')
687 {
688 // Move just after the current match, so that when nv_search
689 // finishes the cursor will be put back on the match.
690 is_state->search_start = t;
691 (void)incl(&is_state->search_start);
692 }
693 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
694 {
695 // wrap around
696 is_state->search_start = t;
697 if (firstc == '?')
698 (void)incl(&is_state->search_start);
699 else
700 (void)decl(&is_state->search_start);
701 }
702
703 set_search_match(&is_state->match_end);
704 curwin->w_cursor = is_state->match_start;
705 changed_cline_bef_curs();
706 update_topline();
707 validate_cursor();
708 highlight_match = TRUE;
709 save_viewstate(&is_state->old_viewstate);
710 update_screen(NOT_VALID);
711 redrawcmdline();
712 }
713 else
714 vim_beep(BO_ERROR);
715 restore_last_search_pattern();
716 return FAIL;
717}
718
719/*
720 * When CTRL-L typed: add character from the match to the pattern.
721 * May set "*c" to the added character.
722 * Return OK when jumping to cmdline_not_changed.
723 */
724 static int
725may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
726{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200727 int skiplen, patlen;
728
Bram Moolenaar198cb662018-09-06 21:44:17 +0200729 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100730 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200731 save_last_search_pattern();
732
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200733 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200734 {
735 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200736 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200737 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100738 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200739
740 // Add a character from under the cursor for 'incsearch'.
741 if (is_state->did_incsearch)
742 {
743 curwin->w_cursor = is_state->match_end;
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200744 *c = gchar_cursor();
745 if (*c != NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200746 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200747 // If 'ignorecase' and 'smartcase' are set and the
748 // command line has no uppercase characters, convert
749 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200750 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200751 *c = MB_TOLOWER(*c);
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200752 if (*c == firstc || vim_strchr((char_u *)(
753 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200754 {
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200755 // put a backslash before special characters
756 stuffcharReadbuff(*c);
757 *c = '\\';
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200758 }
Bram Moolenaar730f48f2019-04-11 13:45:57 +0200759 // add any composing characters
760 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
761 {
762 int save_c = *c;
763
764 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
765 {
766 curwin->w_cursor.col += mb_char2len(*c);
767 *c = gchar_cursor();
768 stuffcharReadbuff(*c);
769 }
770 *c = save_c;
771 }
772 return FAIL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200773 }
774 }
775 return OK;
776}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200777#endif
778
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200779 void
780cmdline_init(void)
781{
782 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
783}
784
Bram Moolenaar66216052017-12-16 16:33:44 +0100785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 * getcmdline() - accept a command line starting with firstc.
787 *
788 * firstc == ':' get ":" command line.
789 * firstc == '/' or '?' get search pattern
790 * firstc == '=' get expression
791 * firstc == '@' get text for input() function
792 * firstc == '>' get text for debug mode
793 * firstc == NUL get text for :insert command
794 * firstc == -1 like NUL, and break on CTRL-C
795 *
796 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
797 * command line.
798 *
799 * Careful: getcmdline() can be called recursively!
800 *
801 * Return pointer to allocated string if there is a commandline, NULL
802 * otherwise.
803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100805getcmdline(
806 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200807 long count, // only used for incremental search
808 int indent) // indent for inside conditionals
809{
810 return getcmdline_int(firstc, count, indent, TRUE);
811}
812
813 static char_u *
814getcmdline_int(
815 int firstc,
816 long count UNUSED, // only used for incremental search
817 int indent, // indent for inside conditionals
818 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819{
820 int c;
821 int i;
822 int j;
823 int gotesc = FALSE; /* TRUE when <ESC> just typed */
824 int do_abbr; /* when TRUE check for abbr. */
825#ifdef FEAT_CMDHIST
826 char_u *lookfor = NULL; /* string to match */
827 int hiscnt; /* current history line in use */
828 int histype; /* history type to be used */
829#endif
830#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200831 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832#endif
833 int did_wild_list = FALSE; /* did wild_list() recently */
834 int wim_index = 0; /* index in wim_flags[] */
835 int res;
836 int save_msg_scroll = msg_scroll;
837 int save_State = State; /* remember State when called */
838 int some_key_typed = FALSE; /* one of the keys was typed */
839#ifdef FEAT_MOUSE
840 /* mouse drag and release events are ignored, unless they are
841 * preceded with a mouse down event */
842 int ignore_drag_release = TRUE;
843#endif
844#ifdef FEAT_EVAL
845 int break_ctrl_c = FALSE;
846#endif
847 expand_T xpc;
848 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000849 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200850 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200851 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852
Bram Moolenaar438d1762018-09-30 17:11:48 +0200853 if (ccline.cmdbuff != NULL)
854 {
855 // Being called recursively. Since ccline is global, we need to save
856 // the current buffer and restore it when returning.
857 save_cmdline(&save_ccline);
858 did_save_ccline = TRUE;
859 }
860 if (init_ccline)
861 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
862
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863#ifdef FEAT_EVAL
864 if (firstc == -1)
865 {
866 firstc = NUL;
867 break_ctrl_c = TRUE;
868 }
869#endif
870#ifdef FEAT_RIGHTLEFT
871 /* start without Hebrew mapping for a command line */
872 if (firstc == ':' || firstc == '=' || firstc == '>')
873 cmd_hkmap = 0;
874#endif
875
876 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200879 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880#endif
881
882 /*
883 * set some variables for redrawcmd()
884 */
885 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000886 ccline.cmdindent = (firstc > 0 ? indent : 0);
887
888 /* alloc initial ccline.cmdbuff */
889 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200891 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 ccline.cmdlen = ccline.cmdpos = 0;
893 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100894 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000896 /* autoindent for :insert and :append */
897 if (firstc <= 0)
898 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200899 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000900 ccline.cmdbuff[indent] = NUL;
901 ccline.cmdpos = indent;
902 ccline.cmdspos = indent;
903 ccline.cmdlen = indent;
904 }
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000907 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
909#ifdef FEAT_RIGHTLEFT
910 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
911 && (firstc == '/' || firstc == '?'))
912 cmdmsg_rl = TRUE;
913 else
914 cmdmsg_rl = FALSE;
915#endif
916
917 redir_off = TRUE; /* don't redirect the typed command */
918 if (!cmd_silent)
919 {
920 i = msg_scrolled;
921 msg_scrolled = 0; /* avoid wait_return message */
922 gotocmdline(TRUE);
923 msg_scrolled += i;
924 redrawcmdprompt(); /* draw prompt or indent */
925 set_cmdspos();
926 }
927 xpc.xp_context = EXPAND_NOTHING;
928 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000929#ifndef BACKSLASH_IN_FILENAME
930 xpc.xp_shell = FALSE;
931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000933#if defined(FEAT_EVAL)
934 if (ccline.input_fn)
935 {
936 xpc.xp_context = ccline.xp_context;
937 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +0200938# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000939 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000940# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000941 }
942#endif
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 /*
945 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
946 * doing ":@0" when register 0 doesn't contain a CR.
947 */
948 msg_scroll = FALSE;
949
950 State = CMDLINE;
951
952 if (firstc == '/' || firstc == '?' || firstc == '@')
953 {
954 /* Use ":lmap" mappings for search pattern and input(). */
955 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
956 b_im_ptr = &curbuf->b_p_iminsert;
957 else
958 b_im_ptr = &curbuf->b_p_imsearch;
959 if (*b_im_ptr == B_IMODE_LMAP)
960 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100961#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 im_set_active(*b_im_ptr == B_IMODE_IM);
963#endif
964 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100965#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 else if (p_imcmdline)
967 im_set_active(TRUE);
968#endif
969
970#ifdef FEAT_MOUSE
971 setmouse();
972#endif
973#ifdef CURSOR_SHAPE
974 ui_cursor_shape(); /* may show different cursor shape */
975#endif
976
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000977 /* When inside an autocommand for writing "exiting" may be set and
978 * terminal mode set to cooked. Need to set raw mode here then. */
979 settmode(TMODE_RAW);
980
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200981 /* Trigger CmdlineEnter autocommands. */
982 cmdline_type = firstc == NUL ? '-' : firstc;
983 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef FEAT_CMDHIST
986 init_history();
987 hiscnt = hislen; /* set hiscnt to impossible history value */
988 histype = hist_char2type(firstc);
989#endif
990
991#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000992 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#endif
994
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200995 /* If something above caused an error, reset the flags, we do want to type
996 * and execute commands. Display may be messed up a bit. */
997 if (did_emsg)
998 redrawcmd();
999 did_emsg = FALSE;
1000 got_int = FALSE;
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /*
1003 * Collect the command string, handling editing keys.
1004 */
1005 for (;;)
1006 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001007 redir_off = TRUE; /* Don't redirect the typed command.
1008 Repeated, because a ":redir" inside
1009 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010#ifdef USE_ON_FLY_SCROLL
1011 dont_scroll = FALSE; /* allow scrolling here */
1012#endif
1013 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1014
Bram Moolenaar72532d32018-04-07 19:09:09 +02001015 did_emsg = FALSE; /* There can't really be a reason why an error
1016 that occurs while typing a command should
1017 cause the command not to be executed. */
1018
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001020
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001021 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1022 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001023 do
Bram Moolenaar30405d32008-01-02 20:55:27 +00001024 c = safe_vgetc();
Bram Moolenaarabab0b02019-03-30 18:47:01 +01001025 while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 if (KeyTyped)
1028 {
1029 some_key_typed = TRUE;
1030#ifdef FEAT_RIGHTLEFT
1031 if (cmd_hkmap)
1032 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 if (cmdmsg_rl && !KeyStuffed)
1034 {
1035 /* Invert horizontal movements and operations. Only when
1036 * typed by the user directly, not when the result of a
1037 * mapping. */
1038 switch (c)
1039 {
1040 case K_RIGHT: c = K_LEFT; break;
1041 case K_S_RIGHT: c = K_S_LEFT; break;
1042 case K_C_RIGHT: c = K_C_LEFT; break;
1043 case K_LEFT: c = K_RIGHT; break;
1044 case K_S_LEFT: c = K_S_RIGHT; break;
1045 case K_C_LEFT: c = K_C_RIGHT; break;
1046 }
1047 }
1048#endif
1049 }
1050
1051 /*
1052 * Ignore got_int when CTRL-C was typed here.
1053 * Don't ignore it in :global, we really need to break then, e.g., for
1054 * ":g/pat/normal /pat" (without the <CR>).
1055 * Don't ignore it for the input() function.
1056 */
1057 if ((c == Ctrl_C
1058#ifdef UNIX
1059 || c == intr_char
1060#endif
1061 )
1062#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1063 && firstc != '@'
1064#endif
1065#ifdef FEAT_EVAL
1066 && !break_ctrl_c
1067#endif
1068 && !global_busy)
1069 got_int = FALSE;
1070
1071#ifdef FEAT_CMDHIST
1072 /* free old command line when finished moving around in the history
1073 * list */
1074 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001075 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001076 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 && c != K_PAGEDOWN && c != K_PAGEUP
1078 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001079 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001081 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082#endif
1083
1084 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001085 * When there are matching completions to select <S-Tab> works like
1086 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001088 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 c = Ctrl_P;
1090
1091#ifdef FEAT_WILDMENU
1092 /* Special translations for 'wildmenu' */
1093 if (did_wild_list && p_wmnu)
1094 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001095 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001097 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 c = Ctrl_N;
1099 }
1100 /* Hitting CR after "emenu Name.": complete submenu */
1101 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1102 && ccline.cmdpos > 1
1103 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1104 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1105 && (c == '\n' || c == '\r' || c == K_KENTER))
1106 c = K_DOWN;
1107#endif
1108
1109 /* free expanded names when finished walking through matches */
1110 if (xpc.xp_numfiles != -1
1111 && !(c == p_wc && KeyTyped) && c != p_wcm
1112 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1113 && c != Ctrl_L)
1114 {
1115 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1116 did_wild_list = FALSE;
1117#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001118 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#endif
1120 xpc.xp_context = EXPAND_NOTHING;
1121 wim_index = 0;
1122#ifdef FEAT_WILDMENU
1123 if (p_wmnu && wild_menu_showing != 0)
1124 {
1125 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001126 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001127
1128 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001129 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130
1131 if (wild_menu_showing == WM_SCROLLED)
1132 {
1133 /* Entered command line, move it up */
1134 cmdline_row--;
1135 redrawcmd();
1136 }
1137 else if (save_p_ls != -1)
1138 {
1139 /* restore 'laststatus' and 'winminheight' */
1140 p_ls = save_p_ls;
1141 p_wmh = save_p_wmh;
1142 last_status(FALSE);
1143 update_screen(VALID); /* redraw the screen NOW */
1144 redrawcmd();
1145 save_p_ls = -1;
1146 }
1147 else
1148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 redraw_statuslines();
1151 }
1152 KeyTyped = skt;
1153 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001154 if (ccline.input_fn)
1155 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157#endif
1158 }
1159
1160#ifdef FEAT_WILDMENU
1161 /* Special translations for 'wildmenu' */
1162 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1163 {
1164 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001165 if (c == K_DOWN && ccline.cmdpos > 0
1166 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001168 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 {
1170 /* Hitting <Up>: Remove one submenu name in front of the
1171 * cursor */
1172 int found = FALSE;
1173
1174 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1175 i = 0;
1176 while (--j > 0)
1177 {
1178 /* check for start of menu name */
1179 if (ccline.cmdbuff[j] == ' '
1180 && ccline.cmdbuff[j - 1] != '\\')
1181 {
1182 i = j + 1;
1183 break;
1184 }
1185 /* check for start of submenu name */
1186 if (ccline.cmdbuff[j] == '.'
1187 && ccline.cmdbuff[j - 1] != '\\')
1188 {
1189 if (found)
1190 {
1191 i = j + 1;
1192 break;
1193 }
1194 else
1195 found = TRUE;
1196 }
1197 }
1198 if (i > 0)
1199 cmdline_del(i);
1200 c = p_wc;
1201 xpc.xp_context = EXPAND_NOTHING;
1202 }
1203 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001204 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001205 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001206 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 {
1208 char_u upseg[5];
1209
1210 upseg[0] = PATHSEP;
1211 upseg[1] = '.';
1212 upseg[2] = '.';
1213 upseg[3] = PATHSEP;
1214 upseg[4] = NUL;
1215
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001216 if (c == K_DOWN
1217 && ccline.cmdpos > 0
1218 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1219 && (ccline.cmdpos < 3
1220 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1222 {
1223 /* go down a directory */
1224 c = p_wc;
1225 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001226 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 {
1228 /* If in a direct ancestor, strip off one ../ to go down */
1229 int found = FALSE;
1230
1231 j = ccline.cmdpos;
1232 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1233 while (--j > i)
1234 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001235 if (has_mbyte)
1236 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 if (vim_ispathsep(ccline.cmdbuff[j]))
1238 {
1239 found = TRUE;
1240 break;
1241 }
1242 }
1243 if (found
1244 && ccline.cmdbuff[j - 1] == '.'
1245 && ccline.cmdbuff[j - 2] == '.'
1246 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1247 {
1248 cmdline_del(j - 2);
1249 c = p_wc;
1250 }
1251 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001252 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 {
1254 /* go up a directory */
1255 int found = FALSE;
1256
1257 j = ccline.cmdpos - 1;
1258 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1259 while (--j > i)
1260 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 if (has_mbyte)
1262 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 if (vim_ispathsep(ccline.cmdbuff[j])
1264#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001265 && vim_strchr((char_u *)" *?[{`$%#",
1266 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267#endif
1268 )
1269 {
1270 if (found)
1271 {
1272 i = j + 1;
1273 break;
1274 }
1275 else
1276 found = TRUE;
1277 }
1278 }
1279
1280 if (!found)
1281 j = i;
1282 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1283 j += 4;
1284 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1285 && j == i)
1286 j += 3;
1287 else
1288 j = 0;
1289 if (j > 0)
1290 {
1291 /* TODO this is only for DOS/UNIX systems - need to put in
1292 * machine-specific stuff here and in upseg init */
1293 cmdline_del(j);
1294 put_on_cmdline(upseg + 1, 3, FALSE);
1295 }
1296 else if (ccline.cmdpos > i)
1297 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001298
1299 /* Now complete in the new directory. Set KeyTyped in case the
1300 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001302 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 }
1304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305
1306#endif /* FEAT_WILDMENU */
1307
1308 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1309 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1310 if (c == Ctrl_BSL)
1311 {
1312 ++no_mapping;
1313 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001314 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 --no_mapping;
1316 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001317 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1318 * is in a mapping. */
1319 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001320 || (ccline.cmdfirstc == '=' && KeyTyped)
1321#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001322 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001323#endif
1324 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 {
1326 vungetc(c);
1327 c = Ctrl_BSL;
1328 }
1329#ifdef FEAT_EVAL
1330 else if (c == 'e')
1331 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001332 char_u *p = NULL;
1333 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334
1335 /*
1336 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001337 * Need to save and restore the current command line, to be
1338 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 */
1340 if (ccline.cmdpos == ccline.cmdlen)
1341 new_cmdpos = 99999; /* keep it at the end */
1342 else
1343 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001344
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (c == '=')
1347 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001348 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001349 * to avoid nasty things like going to another buffer when
1350 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001351 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001353 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001354
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001355 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001357 len = (int)STRLEN(p);
1358 if (realloc_cmdbuff(len + 1) == OK)
1359 {
1360 ccline.cmdlen = len;
1361 STRCPY(ccline.cmdbuff, p);
1362 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001364 /* Restore the cursor or use the position set with
1365 * set_cmdline_pos(). */
1366 if (new_cmdpos > ccline.cmdlen)
1367 ccline.cmdpos = ccline.cmdlen;
1368 else
1369 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001371 KeyTyped = FALSE; /* Don't do p_wc completion. */
1372 redrawcmd();
1373 goto cmdline_changed;
1374 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001375 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377 }
1378 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001379 got_int = FALSE; /* don't abandon the command line */
1380 did_emsg = FALSE;
1381 emsg_on_display = FALSE;
1382 redrawcmd();
1383 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 }
1385#endif
1386 else
1387 {
1388 if (c == Ctrl_G && p_im && restart_edit == 0)
1389 restart_edit = 'a';
1390 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1391 in history */
1392 goto returncmd; /* back to Normal mode */
1393 }
1394 }
1395
1396#ifdef FEAT_CMDWIN
1397 if (c == cedit_key || c == K_CMDWIN)
1398 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001399 if (ex_normal_busy == 0 && got_int == FALSE)
1400 {
1401 /*
1402 * Open a window to edit the command line (and history).
1403 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001404 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001405 some_key_typed = TRUE;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 }
1408# ifdef FEAT_DIGRAPHS
1409 else
1410# endif
1411#endif
1412#ifdef FEAT_DIGRAPHS
1413 c = do_digraph(c);
1414#endif
1415
1416 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1417 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1418 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001419 /* In Ex mode a backslash escapes a newline. */
1420 if (exmode_active
1421 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001422 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001423 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001424 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001426 if (c == K_KENTER)
1427 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001429 else
1430 {
1431 gotesc = FALSE; /* Might have typed ESC previously, don't
1432 truncate the cmdline now. */
1433 if (ccheck_abbr(c + ABBR_OFF))
1434 goto cmdline_changed;
1435 if (!cmd_silent)
1436 {
1437 windgoto(msg_row, 0);
1438 out_flush();
1439 }
1440 break;
1441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 }
1443
1444 /*
1445 * Completion for 'wildchar' or 'wildcharm' key.
1446 * - hitting <ESC> twice means: abandon command line.
1447 * - wildcard expansion is only done when the 'wildchar' key is really
1448 * typed, not when it comes from a macro
1449 */
1450 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1451 {
1452 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1453 {
1454 /* if 'wildmode' contains "list" may still need to list */
1455 if (xpc.xp_numfiles > 1
1456 && !did_wild_list
1457 && (wim_flags[wim_index] & WIM_LIST))
1458 {
1459 (void)showmatches(&xpc, FALSE);
1460 redrawcmd();
1461 did_wild_list = TRUE;
1462 }
1463 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001464 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1465 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001467 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1468 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 else
1470 res = OK; /* don't insert 'wildchar' now */
1471 }
1472 else /* typed p_wc first time */
1473 {
1474 wim_index = 0;
1475 j = ccline.cmdpos;
1476 /* if 'wildmode' first contains "longest", get longest
1477 * common part */
1478 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001479 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1480 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001482 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1483 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484
1485 /* if interrupted while completing, behave like it failed */
1486 if (got_int)
1487 {
1488 (void)vpeekc(); /* remove <C-C> from input stream */
1489 got_int = FALSE; /* don't abandon the command line */
1490 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1491#ifdef FEAT_WILDMENU
1492 xpc.xp_context = EXPAND_NOTHING;
1493#endif
1494 goto cmdline_changed;
1495 }
1496
1497 /* when more than one match, and 'wildmode' first contains
1498 * "list", or no change and 'wildmode' contains "longest,list",
1499 * list all matches */
1500 if (res == OK && xpc.xp_numfiles > 1)
1501 {
1502 /* a "longest" that didn't do anything is skipped (but not
1503 * "list:longest") */
1504 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1505 wim_index = 1;
1506 if ((wim_flags[wim_index] & WIM_LIST)
1507#ifdef FEAT_WILDMENU
1508 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1509#endif
1510 )
1511 {
1512 if (!(wim_flags[0] & WIM_LONGEST))
1513 {
1514#ifdef FEAT_WILDMENU
1515 int p_wmnu_save = p_wmnu;
1516 p_wmnu = 0;
1517#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001518 /* remove match */
1519 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520#ifdef FEAT_WILDMENU
1521 p_wmnu = p_wmnu_save;
1522#endif
1523 }
1524#ifdef FEAT_WILDMENU
1525 (void)showmatches(&xpc, p_wmnu
1526 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1527#else
1528 (void)showmatches(&xpc, FALSE);
1529#endif
1530 redrawcmd();
1531 did_wild_list = TRUE;
1532 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001533 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1534 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001536 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1537 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 }
1539 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001540 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 }
1542#ifdef FEAT_WILDMENU
1543 else if (xpc.xp_numfiles == -1)
1544 xpc.xp_context = EXPAND_NOTHING;
1545#endif
1546 }
1547 if (wim_index < 3)
1548 ++wim_index;
1549 if (c == ESC)
1550 gotesc = TRUE;
1551 if (res == OK)
1552 goto cmdline_changed;
1553 }
1554
1555 gotesc = FALSE;
1556
1557 /* <S-Tab> goes to last match, in a clumsy way */
1558 if (c == K_S_TAB && KeyTyped)
1559 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001560 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1561 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1562 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 goto cmdline_changed;
1564 }
1565
1566 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1567 c = NL;
1568
1569 do_abbr = TRUE; /* default: check for abbreviation */
1570
1571 /*
1572 * Big switch for a typed command line character.
1573 */
1574 switch (c)
1575 {
1576 case K_BS:
1577 case Ctrl_H:
1578 case K_DEL:
1579 case K_KDEL:
1580 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (c == K_KDEL)
1582 c = K_DEL;
1583
1584 /*
1585 * delete current character is the same as backspace on next
1586 * character, except at end of line
1587 */
1588 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1589 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (has_mbyte && c == K_DEL)
1591 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1592 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 if (ccline.cmdpos > 0)
1594 {
1595 char_u *p;
1596
1597 j = ccline.cmdpos;
1598 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (has_mbyte)
1600 {
1601 p = mb_prevptr(ccline.cmdbuff, p);
1602 if (c == Ctrl_W)
1603 {
1604 while (p > ccline.cmdbuff && vim_isspace(*p))
1605 p = mb_prevptr(ccline.cmdbuff, p);
1606 i = mb_get_class(p);
1607 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1608 p = mb_prevptr(ccline.cmdbuff, p);
1609 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001610 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001613 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 {
1615 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1616 --p;
1617 i = vim_iswordc(p[-1]);
1618 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1619 && vim_iswordc(p[-1]) == i)
1620 --p;
1621 }
1622 else
1623 --p;
1624 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1625 ccline.cmdlen -= j - ccline.cmdpos;
1626 i = ccline.cmdpos;
1627 while (i < ccline.cmdlen)
1628 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1629
1630 /* Truncate at the end, required for multi-byte chars. */
1631 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001632#ifdef FEAT_SEARCH_EXTRA
1633 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001634 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001635 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001636 /* save view settings, so that the screen
1637 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001638 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001639 }
1640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 redrawcmd();
1642 }
1643 else if (ccline.cmdlen == 0 && c != Ctrl_W
1644 && ccline.cmdprompt == NULL && indent == 0)
1645 {
1646 /* In ex and debug mode it doesn't make sense to return. */
1647 if (exmode_active
1648#ifdef FEAT_EVAL
1649 || ccline.cmdfirstc == '>'
1650#endif
1651 )
1652 goto cmdline_not_changed;
1653
Bram Moolenaard23a8232018-02-10 18:45:26 +01001654 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655 if (!cmd_silent)
1656 {
1657#ifdef FEAT_RIGHTLEFT
1658 if (cmdmsg_rl)
1659 msg_col = Columns;
1660 else
1661#endif
1662 msg_col = 0;
1663 msg_putchar(' '); /* delete ':' */
1664 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001665#ifdef FEAT_SEARCH_EXTRA
1666 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001667 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 redraw_cmdline = TRUE;
1670 goto returncmd; /* back to cmd mode */
1671 }
1672 goto cmdline_changed;
1673
1674 case K_INS:
1675 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 ccline.overstrike = !ccline.overstrike;
1677#ifdef CURSOR_SHAPE
1678 ui_cursor_shape(); /* may show different cursor shape */
1679#endif
1680 goto cmdline_not_changed;
1681
1682 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001683 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 {
1685 /* ":lmap" mappings exists, toggle use of mappings. */
1686 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001687#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 im_set_active(FALSE); /* Disable input method */
1689#endif
1690 if (b_im_ptr != NULL)
1691 {
1692 if (State & LANGMAP)
1693 *b_im_ptr = B_IMODE_LMAP;
1694 else
1695 *b_im_ptr = B_IMODE_NONE;
1696 }
1697 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001698#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 else
1700 {
1701 /* There are no ":lmap" mappings, toggle IM. When
1702 * 'imdisable' is set don't try getting the status, it's
1703 * always off. */
1704 if ((p_imdisable && b_im_ptr != NULL)
1705 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1706 {
1707 im_set_active(FALSE); /* Disable input method */
1708 if (b_im_ptr != NULL)
1709 *b_im_ptr = B_IMODE_NONE;
1710 }
1711 else
1712 {
1713 im_set_active(TRUE); /* Enable input method */
1714 if (b_im_ptr != NULL)
1715 *b_im_ptr = B_IMODE_IM;
1716 }
1717 }
1718#endif
1719 if (b_im_ptr != NULL)
1720 {
1721 if (b_im_ptr == &curbuf->b_p_iminsert)
1722 set_iminsert_global();
1723 else
1724 set_imsearch_global();
1725 }
1726#ifdef CURSOR_SHAPE
1727 ui_cursor_shape(); /* may show different cursor shape */
1728#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001729#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 /* Show/unshow value of 'keymap' in status lines later. */
1731 status_redraw_curbuf();
1732#endif
1733 goto cmdline_not_changed;
1734
1735/* case '@': only in very old vi */
1736 case Ctrl_U:
1737 /* delete all characters left of the cursor */
1738 j = ccline.cmdpos;
1739 ccline.cmdlen -= j;
1740 i = ccline.cmdpos = 0;
1741 while (i < ccline.cmdlen)
1742 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1743 /* Truncate at the end, required for multi-byte chars. */
1744 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001745#ifdef FEAT_SEARCH_EXTRA
1746 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001747 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 redrawcmd();
1750 goto cmdline_changed;
1751
1752#ifdef FEAT_CLIPBOARD
1753 case Ctrl_Y:
1754 /* Copy the modeless selection, if there is one. */
1755 if (clip_star.state != SELECT_CLEARED)
1756 {
1757 if (clip_star.state == SELECT_DONE)
1758 clip_copy_modeless_selection(TRUE);
1759 goto cmdline_not_changed;
1760 }
1761 break;
1762#endif
1763
1764 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1765 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001766 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001767 * ":normal" runs out of characters. */
1768 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001769 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 goto cmdline_not_changed;
1771
1772 gotesc = TRUE; /* will free ccline.cmdbuff after
1773 putting it in history */
1774 goto returncmd; /* back to cmd mode */
1775
1776 case Ctrl_R: /* insert register */
1777#ifdef USE_ON_FLY_SCROLL
1778 dont_scroll = TRUE; /* disallow scrolling here */
1779#endif
1780 putcmdline('"', TRUE);
1781 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001782 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 if (i == Ctrl_O)
1784 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1785 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001786 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001787 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 --no_mapping;
1789#ifdef FEAT_EVAL
1790 /*
1791 * Insert the result of an expression.
1792 * Need to save the current command line, to be able to enter
1793 * a new one...
1794 */
1795 new_cmdpos = -1;
1796 if (c == '=')
1797 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001798 if (ccline.cmdfirstc == '=' // can't do this recursively
1799 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 {
1801 beep_flush();
1802 c = ESC;
1803 }
1804 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806 }
1807#endif
1808 if (c != ESC) /* use ESC to cancel inserting register */
1809 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001810 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001811
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001812#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001813 /* When there was a serious error abort getting the
1814 * command line. */
1815 if (aborting())
1816 {
1817 gotesc = TRUE; /* will free ccline.cmdbuff after
1818 putting it in history */
1819 goto returncmd; /* back to cmd mode */
1820 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 KeyTyped = FALSE; /* Don't do p_wc completion. */
1823#ifdef FEAT_EVAL
1824 if (new_cmdpos >= 0)
1825 {
1826 /* set_cmdline_pos() was used */
1827 if (new_cmdpos > ccline.cmdlen)
1828 ccline.cmdpos = ccline.cmdlen;
1829 else
1830 ccline.cmdpos = new_cmdpos;
1831 }
1832#endif
1833 }
1834 redrawcmd();
1835 goto cmdline_changed;
1836
1837 case Ctrl_D:
1838 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1839 break; /* Use ^D as normal char instead */
1840
1841 redrawcmd();
1842 continue; /* don't do incremental search now */
1843
1844 case K_RIGHT:
1845 case K_S_RIGHT:
1846 case K_C_RIGHT:
1847 do
1848 {
1849 if (ccline.cmdpos >= ccline.cmdlen)
1850 break;
1851 i = cmdline_charsize(ccline.cmdpos);
1852 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1853 break;
1854 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001856 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 + ccline.cmdpos);
1858 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 ++ccline.cmdpos;
1860 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001861 while ((c == K_S_RIGHT || c == K_C_RIGHT
1862 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (has_mbyte)
1865 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 goto cmdline_not_changed;
1867
1868 case K_LEFT:
1869 case K_S_LEFT:
1870 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001871 if (ccline.cmdpos == 0)
1872 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 do
1874 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (has_mbyte) /* move to first byte of char */
1877 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1878 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1880 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001881 while (ccline.cmdpos > 0
1882 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001883 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (has_mbyte)
1886 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 goto cmdline_not_changed;
1888
1889 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001890 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001891 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
Bram Moolenaar4f974752019-02-17 17:44:42 +01001893#ifdef FEAT_GUI_MSWIN
1894 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1895 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001896 case K_F4:
1897 if (mod_mask == MOD_MASK_ALT)
1898 {
1899 redrawcmd(); /* somehow the cmdline is cleared */
1900 goto cmdline_not_changed;
1901 }
1902 break;
1903#endif
1904
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905#ifdef FEAT_MOUSE
1906 case K_MIDDLEDRAG:
1907 case K_MIDDLERELEASE:
1908 goto cmdline_not_changed; /* Ignore mouse */
1909
1910 case K_MIDDLEMOUSE:
1911# ifdef FEAT_GUI
1912 /* When GUI is active, also paste when 'mouse' is empty */
1913 if (!gui.in_use)
1914# endif
1915 if (!mouse_has(MOUSE_COMMAND))
1916 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001917# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001919 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001921# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001922 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 redrawcmd();
1924 goto cmdline_changed;
1925
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001926# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001928 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 redrawcmd();
1930 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001931# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932
1933 case K_LEFTDRAG:
1934 case K_LEFTRELEASE:
1935 case K_RIGHTDRAG:
1936 case K_RIGHTRELEASE:
1937 /* Ignore drag and release events when the button-down wasn't
1938 * seen before. */
1939 if (ignore_drag_release)
1940 goto cmdline_not_changed;
1941 /* FALLTHROUGH */
1942 case K_LEFTMOUSE:
1943 case K_RIGHTMOUSE:
1944 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1945 ignore_drag_release = TRUE;
1946 else
1947 ignore_drag_release = FALSE;
1948# ifdef FEAT_GUI
1949 /* When GUI is active, also move when 'mouse' is empty */
1950 if (!gui.in_use)
1951# endif
1952 if (!mouse_has(MOUSE_COMMAND))
1953 goto cmdline_not_changed; /* Ignore mouse */
1954# ifdef FEAT_CLIPBOARD
1955 if (mouse_row < cmdline_row && clip_star.available)
1956 {
1957 int button, is_click, is_drag;
1958
1959 /*
1960 * Handle modeless selection.
1961 */
1962 button = get_mouse_button(KEY2TERMCAP1(c),
1963 &is_click, &is_drag);
1964 if (mouse_model_popup() && button == MOUSE_LEFT
1965 && (mod_mask & MOD_MASK_SHIFT))
1966 {
1967 /* Translate shift-left to right button. */
1968 button = MOUSE_RIGHT;
1969 mod_mask &= ~MOD_MASK_SHIFT;
1970 }
1971 clip_modeless(button, is_click, is_drag);
1972 goto cmdline_not_changed;
1973 }
1974# endif
1975
1976 set_cmdspos();
1977 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1978 ++ccline.cmdpos)
1979 {
1980 i = cmdline_charsize(ccline.cmdpos);
1981 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1982 && mouse_col < ccline.cmdspos % Columns + i)
1983 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 if (has_mbyte)
1985 {
1986 /* Count ">" for double-wide char that doesn't fit. */
1987 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001988 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 + ccline.cmdpos) - 1;
1990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 ccline.cmdspos += i;
1992 }
1993 goto cmdline_not_changed;
1994
1995 /* Mouse scroll wheel: ignored here */
1996 case K_MOUSEDOWN:
1997 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001998 case K_MOUSELEFT:
1999 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 /* Alternate buttons ignored here */
2001 case K_X1MOUSE:
2002 case K_X1DRAG:
2003 case K_X1RELEASE:
2004 case K_X2MOUSE:
2005 case K_X2DRAG:
2006 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002007 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 goto cmdline_not_changed;
2009
2010#endif /* FEAT_MOUSE */
2011
2012#ifdef FEAT_GUI
2013 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2014 case K_LEFTRELEASE_NM:
2015 goto cmdline_not_changed;
2016
2017 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002018 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 {
2020 gui_do_scroll();
2021 redrawcmd();
2022 }
2023 goto cmdline_not_changed;
2024
2025 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002026 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002028 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029 redrawcmd();
2030 }
2031 goto cmdline_not_changed;
2032#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002033#ifdef FEAT_GUI_TABLINE
2034 case K_TABLINE:
2035 case K_TABMENU:
2036 /* Don't want to change any tabs here. Make sure the same tab
2037 * is still selected. */
2038 if (gui_use_tabline())
2039 gui_mch_set_curtab(tabpage_index(curtab));
2040 goto cmdline_not_changed;
2041#endif
2042
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 case K_SELECT: /* end of Select mode mapping - ignore */
2044 goto cmdline_not_changed;
2045
2046 case Ctrl_B: /* begin of command line */
2047 case K_HOME:
2048 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 case K_S_HOME:
2050 case K_C_HOME:
2051 ccline.cmdpos = 0;
2052 set_cmdspos();
2053 goto cmdline_not_changed;
2054
2055 case Ctrl_E: /* end of command line */
2056 case K_END:
2057 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 case K_S_END:
2059 case K_C_END:
2060 ccline.cmdpos = ccline.cmdlen;
2061 set_cmdspos_cursor();
2062 goto cmdline_not_changed;
2063
2064 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002065 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 break;
2067 goto cmdline_changed;
2068
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002069 case Ctrl_L:
2070#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002071 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002072 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002073#endif
2074
2075 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002076 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 break;
2078 goto cmdline_changed;
2079
2080 case Ctrl_N: /* next match */
2081 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002082 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002084 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2085 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002087 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002090 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 case K_UP:
2092 case K_DOWN:
2093 case K_S_UP:
2094 case K_S_DOWN:
2095 case K_PAGEUP:
2096 case K_KPAGEUP:
2097 case K_PAGEDOWN:
2098 case K_KPAGEDOWN:
2099 if (hislen == 0 || firstc == NUL) /* no history */
2100 goto cmdline_not_changed;
2101
2102 i = hiscnt;
2103
2104 /* save current command string so it can be restored later */
2105 if (lookfor == NULL)
2106 {
2107 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2108 goto cmdline_not_changed;
2109 lookfor[ccline.cmdpos] = NUL;
2110 }
2111
2112 j = (int)STRLEN(lookfor);
2113 for (;;)
2114 {
2115 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002116 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002117 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 {
2119 if (hiscnt == hislen) /* first time */
2120 hiscnt = hisidx[histype];
2121 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2122 hiscnt = hislen - 1;
2123 else if (hiscnt != hisidx[histype] + 1)
2124 --hiscnt;
2125 else /* at top of list */
2126 {
2127 hiscnt = i;
2128 break;
2129 }
2130 }
2131 else /* one step forwards */
2132 {
2133 /* on last entry, clear the line */
2134 if (hiscnt == hisidx[histype])
2135 {
2136 hiscnt = hislen;
2137 break;
2138 }
2139
2140 /* not on a history line, nothing to do */
2141 if (hiscnt == hislen)
2142 break;
2143 if (hiscnt == hislen - 1) /* wrap around */
2144 hiscnt = 0;
2145 else
2146 ++hiscnt;
2147 }
2148 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2149 {
2150 hiscnt = i;
2151 break;
2152 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002153 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002154 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 || STRNCMP(history[histype][hiscnt].hisstr,
2156 lookfor, (size_t)j) == 0)
2157 break;
2158 }
2159
2160 if (hiscnt != i) /* jumped to other entry */
2161 {
2162 char_u *p;
2163 int len;
2164 int old_firstc;
2165
Bram Moolenaar438d1762018-09-30 17:11:48 +02002166 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002167 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 if (hiscnt == hislen)
2169 p = lookfor; /* back to the old one */
2170 else
2171 p = history[histype][hiscnt].hisstr;
2172
2173 if (histype == HIST_SEARCH
2174 && p != lookfor
2175 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2176 {
2177 /* Correct for the separator character used when
2178 * adding the history entry vs the one used now.
2179 * First loop: count length.
2180 * Second loop: copy the characters. */
2181 for (i = 0; i <= 1; ++i)
2182 {
2183 len = 0;
2184 for (j = 0; p[j] != NUL; ++j)
2185 {
2186 /* Replace old sep with new sep, unless it is
2187 * escaped. */
2188 if (p[j] == old_firstc
2189 && (j == 0 || p[j - 1] != '\\'))
2190 {
2191 if (i > 0)
2192 ccline.cmdbuff[len] = firstc;
2193 }
2194 else
2195 {
2196 /* Escape new sep, unless it is already
2197 * escaped. */
2198 if (p[j] == firstc
2199 && (j == 0 || p[j - 1] != '\\'))
2200 {
2201 if (i > 0)
2202 ccline.cmdbuff[len] = '\\';
2203 ++len;
2204 }
2205 if (i > 0)
2206 ccline.cmdbuff[len] = p[j];
2207 }
2208 ++len;
2209 }
2210 if (i == 0)
2211 {
2212 alloc_cmdbuff(len);
2213 if (ccline.cmdbuff == NULL)
2214 goto returncmd;
2215 }
2216 }
2217 ccline.cmdbuff[len] = NUL;
2218 }
2219 else
2220 {
2221 alloc_cmdbuff((int)STRLEN(p));
2222 if (ccline.cmdbuff == NULL)
2223 goto returncmd;
2224 STRCPY(ccline.cmdbuff, p);
2225 }
2226
2227 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2228 redrawcmd();
2229 goto cmdline_changed;
2230 }
2231 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002232#endif
2233 goto cmdline_not_changed;
2234
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002235#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002236 case Ctrl_G: /* next match */
2237 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002238 if (may_adjust_incsearch_highlighting(
2239 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002240 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002241 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242#endif
2243
2244 case Ctrl_V:
2245 case Ctrl_Q:
2246#ifdef FEAT_MOUSE
2247 ignore_drag_release = TRUE;
2248#endif
2249 putcmdline('^', TRUE);
2250 c = get_literal(); /* get next (two) character(s) */
2251 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002252 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 /* may need to remove ^ when composing char was typed */
2254 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2255 {
2256 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2257 msg_putchar(' ');
2258 cursorcmd();
2259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 break;
2261
2262#ifdef FEAT_DIGRAPHS
2263 case Ctrl_K:
2264#ifdef FEAT_MOUSE
2265 ignore_drag_release = TRUE;
2266#endif
2267 putcmdline('?', TRUE);
2268#ifdef USE_ON_FLY_SCROLL
2269 dont_scroll = TRUE; /* disallow scrolling here */
2270#endif
2271 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002272 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (c != NUL)
2274 break;
2275
2276 redrawcmd();
2277 goto cmdline_not_changed;
2278#endif /* FEAT_DIGRAPHS */
2279
2280#ifdef FEAT_RIGHTLEFT
2281 case Ctrl__: /* CTRL-_: switch language mode */
2282 if (!p_ari)
2283 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002284 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 goto cmdline_not_changed;
2286#endif
2287
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002288 case K_PS:
2289 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2290 goto cmdline_changed;
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 default:
2293#ifdef UNIX
2294 if (c == intr_char)
2295 {
2296 gotesc = TRUE; /* will free ccline.cmdbuff after
2297 putting it in history */
2298 goto returncmd; /* back to Normal mode */
2299 }
2300#endif
2301 /*
2302 * Normal character with no special meaning. Just set mod_mask
2303 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2304 * the string <S-Space>. This should only happen after ^V.
2305 */
2306 if (!IS_SPECIAL(c))
2307 mod_mask = 0x0;
2308 break;
2309 }
2310 /*
2311 * End of switch on command line character.
2312 * We come here if we have a normal character.
2313 */
2314
Bram Moolenaar13505972019-01-24 15:04:48 +01002315 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2316 && (ccheck_abbr(
2317 // Add ABBR_OFF for characters above 0x100, this is
2318 // what check_abbr() expects.
2319 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2320 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 goto cmdline_changed;
2322
2323 /*
2324 * put the character in the command line
2325 */
2326 if (IS_SPECIAL(c) || mod_mask != 0)
2327 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2328 else
2329 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (has_mbyte)
2331 {
2332 j = (*mb_char2bytes)(c, IObuff);
2333 IObuff[j] = NUL; /* exclude composing chars */
2334 put_on_cmdline(IObuff, j, TRUE);
2335 }
2336 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
2338 IObuff[0] = c;
2339 put_on_cmdline(IObuff, 1, TRUE);
2340 }
2341 }
2342 goto cmdline_changed;
2343
2344/*
2345 * This part implements incremental searches for "/" and "?"
2346 * Jump to cmdline_not_changed when a character has been read but the command
2347 * line did not change. Then we only search and redraw if something changed in
2348 * the past.
2349 * Jump to cmdline_changed when the command line did change.
2350 * (Sorry for the goto's, I know it is ugly).
2351 */
2352cmdline_not_changed:
2353#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002354 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 continue;
2356#endif
2357
2358cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002359 /* Trigger CmdlineChanged autocommands. */
2360 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002363 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364#endif
2365
2366#ifdef FEAT_RIGHTLEFT
2367 if (cmdmsg_rl
2368# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002369 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370# endif
2371 )
2372 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002373 * right-left typing. Not efficient, but it works.
2374 * Do it only when there are no characters left to read
2375 * to avoid useless intermediate redraws. */
2376 if (vpeekc() == NUL)
2377 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378#endif
2379 }
2380
2381returncmd:
2382
2383#ifdef FEAT_RIGHTLEFT
2384 cmdmsg_rl = FALSE;
2385#endif
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002388 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002391 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#endif
2393
2394 if (ccline.cmdbuff != NULL)
2395 {
2396 /*
2397 * Put line in history buffer (":" and "=" only when it was typed).
2398 */
2399#ifdef FEAT_CMDHIST
2400 if (ccline.cmdlen && firstc != NUL
2401 && (some_key_typed || histype == HIST_SEARCH))
2402 {
2403 add_to_history(histype, ccline.cmdbuff, TRUE,
2404 histype == HIST_SEARCH ? firstc : NUL);
2405 if (firstc == ':')
2406 {
2407 vim_free(new_last_cmdline);
2408 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2409 }
2410 }
2411#endif
2412
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002413 if (gotesc)
2414 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416
2417 /*
2418 * If the screen was shifted up, redraw the whole screen (later).
2419 * If the line is too long, clear it, so ruler and shown command do
2420 * not get printed in the middle of it.
2421 */
2422 msg_check();
2423 msg_scroll = save_msg_scroll;
2424 redir_off = FALSE;
2425
2426 /* When the command line was typed, no need for a wait-return prompt. */
2427 if (some_key_typed)
2428 need_wait_return = FALSE;
2429
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002430 /* Trigger CmdlineLeave autocommands. */
2431 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002432
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002434#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2436 im_save_status(b_im_ptr);
2437 im_set_active(FALSE);
2438#endif
2439#ifdef FEAT_MOUSE
2440 setmouse();
2441#endif
2442#ifdef CURSOR_SHAPE
2443 ui_cursor_shape(); /* may show different cursor shape */
2444#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002445 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446
Bram Moolenaar438d1762018-09-30 17:11:48 +02002447theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002448 {
2449 char_u *p = ccline.cmdbuff;
2450
Bram Moolenaar438d1762018-09-30 17:11:48 +02002451 if (did_save_ccline)
2452 restore_cmdline(&save_ccline);
2453 else
2454 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002455 return p;
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457}
2458
2459#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2460/*
2461 * Get a command line with a prompt.
2462 * This is prepared to be called recursively from getcmdline() (e.g. by
2463 * f_input() when evaluating an expression from CTRL-R =).
2464 * Returns the command line in allocated memory, or NULL.
2465 */
2466 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002467getcmdline_prompt(
2468 int firstc,
2469 char_u *prompt, /* command line prompt */
2470 int attr, /* attributes for prompt */
2471 int xp_context, /* type of expansion */
2472 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 char_u *s;
2475 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002476 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002478 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
Bram Moolenaar438d1762018-09-30 17:11:48 +02002480 if (ccline.cmdbuff != NULL)
2481 {
2482 // Save the values of the current cmdline and restore them below.
2483 save_cmdline(&save_ccline);
2484 did_save_ccline = TRUE;
2485 }
2486
2487 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 ccline.cmdprompt = prompt;
2489 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002490# ifdef FEAT_EVAL
2491 ccline.xp_context = xp_context;
2492 ccline.xp_arg = xp_arg;
2493 ccline.input_fn = (firstc == '@');
2494# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002495 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002496 s = getcmdline_int(firstc, 1L, 0, FALSE);
2497
2498 if (did_save_ccline)
2499 restore_cmdline(&save_ccline);
2500
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002501 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002502 /* Restore msg_col, the prompt from input() may have changed it.
2503 * But only if called recursively and the commandline is therefore being
2504 * restored to an old one; if not, the input() prompt stays on the screen,
2505 * so we need its modified msg_col left intact. */
2506 if (ccline.cmdbuff != NULL)
2507 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 return s;
2510}
2511#endif
2512
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002513/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002514 * Return TRUE when the text must not be changed and we can't switch to
2515 * another window or buffer. Used when editing the command line, evaluating
2516 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002517 */
2518 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002519text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002520{
2521#ifdef FEAT_CMDWIN
2522 if (cmdwin_type != 0)
2523 return TRUE;
2524#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002525 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002526}
2527
2528/*
2529 * Give an error message for a command that isn't allowed while the cmdline
2530 * window is open or editing the cmdline in another way.
2531 */
2532 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002533text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002534{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002535 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002536}
2537
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002538 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002539get_text_locked_msg(void)
2540{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002541#ifdef FEAT_CMDWIN
2542 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002543 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002544#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002545 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002546}
2547
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002548/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002549 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2550 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002551 */
2552 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002553curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002554{
2555 if (curbuf_lock > 0)
2556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002557 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002558 return TRUE;
2559 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002560 return allbuf_locked();
2561}
2562
2563/*
2564 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2565 * message.
2566 */
2567 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002568allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002569{
2570 if (allbuf_lock > 0)
2571 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002572 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002573 return TRUE;
2574 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002575 return FALSE;
2576}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002579cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2582 if (cmdline_star > 0) /* showing '*', always 1 position */
2583 return 1;
2584#endif
2585 return ptr2cells(ccline.cmdbuff + idx);
2586}
2587
2588/*
2589 * Compute the offset of the cursor on the command line for the prompt and
2590 * indent.
2591 */
2592 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002593set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002595 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 ccline.cmdspos = 1 + ccline.cmdindent;
2597 else
2598 ccline.cmdspos = 0 + ccline.cmdindent;
2599}
2600
2601/*
2602 * Compute the screen position for the cursor on the command line.
2603 */
2604 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002605set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 int i, m, c;
2608
2609 set_cmdspos();
2610 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002613 if (m < 0) /* overflow, Columns or Rows at weird value */
2614 m = MAXCOL;
2615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 else
2617 m = MAXCOL;
2618 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2619 {
2620 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2622 if (has_mbyte)
2623 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002624 /* If the cmdline doesn't fit, show cursor on last visible char.
2625 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if ((ccline.cmdspos += c) >= m)
2627 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 ccline.cmdspos -= c;
2629 break;
2630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002632 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 }
2634}
2635
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636/*
2637 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2638 * character that doesn't fit, so that a ">" must be displayed.
2639 */
2640 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002641correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002643 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2645 && ccline.cmdspos % Columns + cells > Columns)
2646 ccline.cmdspos++;
2647}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648
2649/*
2650 * Get an Ex command line for the ":" command.
2651 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002653getexline(
2654 int c, /* normally ':', NUL for ":append" */
2655 void *cookie UNUSED,
2656 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
2658 /* When executing a register, remove ':' that's in front of each line. */
2659 if (exec_from_reg && vpeekc() == ':')
2660 (void)vgetc();
2661 return getcmdline(c, 1L, indent);
2662}
2663
2664/*
2665 * Get an Ex command line for Ex mode.
2666 * In Ex mode we only use the OS supplied line editing features and no
2667 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002668 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002671getexmodeline(
2672 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002673 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002674 void *cookie UNUSED,
2675 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 garray_T line_ga;
2678 char_u *pend;
2679 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002680 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 int escaped = FALSE; /* CTRL-V typed */
2682 int vcol = 0;
2683 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002684 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002685 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686
2687 /* Switch cursor on now. This avoids that it happens after the "\n", which
2688 * confuses the system function that computes tabstops. */
2689 cursor_on();
2690
2691 /* always start in column 0; write a newline if necessary */
2692 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002693 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002695 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002697 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002698 if (p_prompt)
2699 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 while (indent-- > 0)
2701 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704
2705 ga_init2(&line_ga, 1, 30);
2706
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002707 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002708 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002709 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002710 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002711 while (indent >= 8)
2712 {
2713 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002714 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002715 indent -= 8;
2716 }
2717 while (indent-- > 0)
2718 {
2719 ga_append(&line_ga, ' ');
2720 msg_putchar(' ');
2721 }
2722 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002723 ++no_mapping;
2724 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002725
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 /*
2727 * Get the line, one character at a time.
2728 */
2729 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002732 long sw;
2733 char_u *s;
2734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 if (ga_grow(&line_ga, 40) == FAIL)
2736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737
Bram Moolenaarba748c82017-02-26 14:00:07 +01002738 /*
2739 * Get one character at a time.
2740 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002741 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002742
2743 /* Check for a ":normal" command and no more characters left. */
2744 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2745 c1 = '\n';
2746 else
2747 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748
2749 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002750 * Handle line editing.
2751 * Previously this was left to the system, putting the terminal in
2752 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002756 msg_putchar('\n');
2757 break;
2758 }
2759
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002760 if (c1 == K_PS)
2761 {
2762 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2763 goto redraw;
2764 }
2765
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002766 if (!escaped)
2767 {
2768 /* CR typed means "enter", which is NL */
2769 if (c1 == '\r')
2770 c1 = '\n';
2771
2772 if (c1 == BS || c1 == K_BS
2773 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 if (line_ga.ga_len > 0)
2776 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002777 if (has_mbyte)
2778 {
2779 p = (char_u *)line_ga.ga_data;
2780 p[line_ga.ga_len] = NUL;
2781 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2782 line_ga.ga_len -= len;
2783 }
2784 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002785 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002786 goto redraw;
2787 }
2788 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 }
2790
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002793 msg_col = startcol;
2794 msg_clr_eos();
2795 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002796 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002797 }
2798
2799 if (c1 == Ctrl_T)
2800 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002801 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 p = (char_u *)line_ga.ga_data;
2803 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002804 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002805 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002807 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002809 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002810 p = (char_u *)line_ga.ga_data;
2811 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002812 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2813 *s = ' ';
2814 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816redraw:
2817 /* redraw the line */
2818 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002819 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002820 p = (char_u *)line_ga.ga_data;
2821 p[line_ga.ga_len] = NUL;
2822 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002824 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002828 while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002829 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002831 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002833 len = MB_PTR2LEN(p);
2834 msg_outtrans_len(p, len);
2835 vcol += ptr2cells(p);
2836 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 }
2838 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002839 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002840 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 continue;
2842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002844 if (c1 == Ctrl_D)
2845 {
2846 /* Delete one shiftwidth. */
2847 p = (char_u *)line_ga.ga_data;
2848 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850 if (prev_char == '^')
2851 ex_keep_indent = TRUE;
2852 indent = 0;
2853 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 }
2855 else
2856 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002858 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002859 if (indent > 0)
2860 {
2861 --indent;
2862 indent -= indent % get_sw_value(curbuf);
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002865 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002866 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002867 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2869 --line_ga.ga_len;
2870 }
2871 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002873
2874 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2875 {
2876 escaped = TRUE;
2877 continue;
2878 }
2879
2880 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2881 if (IS_SPECIAL(c1))
2882 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002884
2885 if (IS_SPECIAL(c1))
2886 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002887 if (has_mbyte)
2888 len = (*mb_char2bytes)(c1,
2889 (char_u *)line_ga.ga_data + line_ga.ga_len);
2890 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002891 {
2892 len = 1;
2893 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2894 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 if (c1 == '\n')
2896 msg_putchar('\n');
2897 else if (c1 == TAB)
2898 {
2899 /* Don't use chartabsize(), 'ts' can be different */
2900 do
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002901 msg_putchar(' ');
Bram Moolenaarabab0b02019-03-30 18:47:01 +01002902 while (++vcol % 8);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002906 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002907 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002908 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002910 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002911 escaped = FALSE;
2912
2913 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002914 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002915
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002916 /* We are done when a NL is entered, but not when it comes after an
2917 * odd number of backslashes, that results in a NUL. */
2918 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002920 int bcount = 0;
2921
2922 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2923 ++bcount;
2924
2925 if (bcount > 0)
2926 {
2927 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2928 * "\NL", etc. */
2929 line_ga.ga_len -= (bcount + 1) / 2;
2930 pend -= (bcount + 1) / 2;
2931 pend[-1] = '\n';
2932 }
2933
2934 if ((bcount & 1) == 0)
2935 {
2936 --line_ga.ga_len;
2937 --pend;
2938 *pend = NUL;
2939 break;
2940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 }
2942 }
2943
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002944 --no_mapping;
2945 --allow_keys;
2946
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 /* make following messages go to the next line */
2948 msg_didout = FALSE;
2949 msg_col = 0;
2950 if (msg_row < Rows - 1)
2951 ++msg_row;
2952 emsg_on_display = FALSE; /* don't want ui_delay() */
2953
2954 if (got_int)
2955 ga_clear(&line_ga);
2956
2957 return (char_u *)line_ga.ga_data;
2958}
2959
Bram Moolenaare344bea2005-09-01 20:46:49 +00002960# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2961 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962/*
2963 * Return TRUE if ccline.overstrike is on.
2964 */
2965 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002966cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967{
2968 return ccline.overstrike;
2969}
2970
2971/*
2972 * Return TRUE if the cursor is at the end of the cmdline.
2973 */
2974 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002975cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976{
2977 return (ccline.cmdpos >= ccline.cmdlen);
2978}
2979#endif
2980
Bram Moolenaar9372a112005-12-06 19:59:18 +00002981#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982/*
2983 * Return the virtual column number at the current cursor position.
2984 * This is used by the IM code to obtain the start of the preedit string.
2985 */
2986 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002987cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988{
2989 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2990 return MAXCOL;
2991
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 if (has_mbyte)
2993 {
2994 colnr_T col;
2995 int i = 0;
2996
2997 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002998 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
3000 return col;
3001 }
3002 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 return ccline.cmdpos;
3004}
3005#endif
3006
3007#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3008/*
3009 * If part of the command line is an IM preedit string, redraw it with
3010 * IM feedback attributes. The cursor position is restored after drawing.
3011 */
3012 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003013redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014{
3015 if ((State & CMDLINE)
3016 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003017 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 && !p_imdisable
3019 && im_is_preediting())
3020 {
3021 int cmdpos = 0;
3022 int cmdspos;
3023 int old_row;
3024 int old_col;
3025 colnr_T col;
3026
3027 old_row = msg_row;
3028 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003029 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (has_mbyte)
3032 {
3033 for (col = 0; col < preedit_start_col
3034 && cmdpos < ccline.cmdlen; ++col)
3035 {
3036 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003037 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 }
3039 }
3040 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 {
3042 cmdspos += preedit_start_col;
3043 cmdpos += preedit_start_col;
3044 }
3045
3046 msg_row = cmdline_row + (cmdspos / (int)Columns);
3047 msg_col = cmdspos % (int)Columns;
3048 if (msg_row >= Rows)
3049 msg_row = Rows - 1;
3050
3051 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3052 {
3053 int char_len;
3054 int char_attr;
3055
3056 char_attr = im_get_feedback_attr(col);
3057 if (char_attr < 0)
3058 break; /* end of preedit string */
3059
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003061 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 char_len = 1;
3064
3065 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3066 cmdpos += char_len;
3067 }
3068
3069 msg_row = old_row;
3070 msg_col = old_col;
3071 }
3072}
3073#endif /* FEAT_XIM && FEAT_GUI_GTK */
3074
3075/*
3076 * Allocate a new command line buffer.
3077 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 */
3079 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003080alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081{
3082 /*
3083 * give some extra space to avoid having to allocate all the time
3084 */
3085 if (len < 80)
3086 len = 100;
3087 else
3088 len += 20;
3089
3090 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3091 ccline.cmdbufflen = len;
3092}
3093
3094/*
3095 * Re-allocate the command line to length len + something extra.
3096 * return FAIL for failure, OK otherwise
3097 */
3098 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003099realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
3101 char_u *p;
3102
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003103 if (len < ccline.cmdbufflen)
3104 return OK; /* no need to resize */
3105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 p = ccline.cmdbuff;
3107 alloc_cmdbuff(len); /* will get some more */
3108 if (ccline.cmdbuff == NULL) /* out of memory */
3109 {
3110 ccline.cmdbuff = p; /* keep the old one */
3111 return FAIL;
3112 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003113 /* There isn't always a NUL after the command, but it may need to be
3114 * there, thus copy up to the NUL and add a NUL. */
3115 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3116 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003118
3119 if (ccline.xpc != NULL
3120 && ccline.xpc->xp_pattern != NULL
3121 && ccline.xpc->xp_context != EXPAND_NOTHING
3122 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3123 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003124 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003125
3126 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3127 * to point into the newly allocated memory. */
3128 if (i >= 0 && i <= ccline.cmdlen)
3129 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3130 }
3131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 return OK;
3133}
3134
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003135#if defined(FEAT_ARABIC) || defined(PROTO)
3136static char_u *arshape_buf = NULL;
3137
3138# if defined(EXITFREE) || defined(PROTO)
3139 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003140free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003141{
3142 vim_free(arshape_buf);
3143}
3144# endif
3145#endif
3146
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147/*
3148 * Draw part of the cmdline at the current cursor position. But draw stars
3149 * when cmdline_star is TRUE.
3150 */
3151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3155 int i;
3156
3157 if (cmdline_star > 0)
3158 for (i = 0; i < len; ++i)
3159 {
3160 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003162 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 }
3164 else
3165#endif
3166#ifdef FEAT_ARABIC
3167 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3168 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 static int buflen = 0;
3170 char_u *p;
3171 int j;
3172 int newlen = 0;
3173 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003174 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 int prev_c = 0;
3176 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003177 int u8c;
3178 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180
3181 /*
3182 * Do arabic shaping into a temporary buffer. This is very
3183 * inefficient!
3184 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003185 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
3187 /* Re-allocate the buffer. We keep it around to avoid a lot of
3188 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003190 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003191 arshape_buf = alloc(buflen);
3192 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 return; /* out of memory */
3194 }
3195
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003196 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3197 {
3198 /* Prepend a space to draw the leading composing char on. */
3199 arshape_buf[0] = ' ';
3200 newlen = 1;
3201 }
3202
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 for (j = start; j < start + len; j += mb_l)
3204 {
3205 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003206 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003207 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 if (ARABIC_CHAR(u8c))
3209 {
3210 /* Do Arabic shaping. */
3211 if (cmdmsg_rl)
3212 {
3213 /* displaying from right to left */
3214 pc = prev_c;
3215 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003216 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 if (j + mb_l >= start + len)
3218 nc = NUL;
3219 else
3220 nc = utf_ptr2char(p + mb_l);
3221 }
3222 else
3223 {
3224 /* displaying from left to right */
3225 if (j + mb_l >= start + len)
3226 pc = NUL;
3227 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003228 {
3229 int pcc[MAX_MCO];
3230
3231 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003233 pc1 = pcc[0];
3234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 nc = prev_c;
3236 }
3237 prev_c = u8c;
3238
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003239 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003241 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003244 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3245 if (u8cc[1] != 0)
3246 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 }
3250 else
3251 {
3252 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003253 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 newlen += mb_l;
3255 }
3256 }
3257
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260 else
3261#endif
3262 msg_outtrans_len(ccline.cmdbuff + start, len);
3263}
3264
3265/*
3266 * Put a character on the command line. Shifts the following text to the
3267 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3268 * "c" must be printable (fit in one display cell)!
3269 */
3270 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003271putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 if (cmd_silent)
3274 return;
3275 msg_no_more = TRUE;
3276 msg_putchar(c);
3277 if (shift)
3278 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3279 msg_no_more = FALSE;
3280 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003281 extra_char = c;
3282 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283}
3284
3285/*
3286 * Undo a putcmdline(c, FALSE).
3287 */
3288 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003289unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290{
3291 if (cmd_silent)
3292 return;
3293 msg_no_more = TRUE;
3294 if (ccline.cmdlen == ccline.cmdpos)
3295 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003296 else if (has_mbyte)
3297 draw_cmdline(ccline.cmdpos,
3298 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 else
3300 draw_cmdline(ccline.cmdpos, 1);
3301 msg_no_more = FALSE;
3302 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003303 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304}
3305
3306/*
3307 * Put the given string, of the given length, onto the command line.
3308 * If len is -1, then STRLEN() is used to calculate the length.
3309 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3310 * part will be redrawn, otherwise it will not. If this function is called
3311 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3312 * called afterwards.
3313 */
3314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003315put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 int retval;
3318 int i;
3319 int m;
3320 int c;
3321
3322 if (len < 0)
3323 len = (int)STRLEN(str);
3324
3325 /* Check if ccline.cmdbuff needs to be longer */
3326 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003327 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else
3329 retval = OK;
3330 if (retval == OK)
3331 {
3332 if (!ccline.overstrike)
3333 {
3334 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3335 ccline.cmdbuff + ccline.cmdpos,
3336 (size_t)(ccline.cmdlen - ccline.cmdpos));
3337 ccline.cmdlen += len;
3338 }
3339 else
3340 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 if (has_mbyte)
3342 {
3343 /* Count nr of characters in the new string. */
3344 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003345 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 ++m;
3347 /* Count nr of bytes in cmdline that are overwritten by these
3348 * characters. */
3349 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003350 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 --m;
3352 if (i < ccline.cmdlen)
3353 {
3354 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3355 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3356 ccline.cmdlen += ccline.cmdpos + len - i;
3357 }
3358 else
3359 ccline.cmdlen = ccline.cmdpos + len;
3360 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003361 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 ccline.cmdlen = ccline.cmdpos + len;
3363 }
3364 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3365 ccline.cmdbuff[ccline.cmdlen] = NUL;
3366
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 if (enc_utf8)
3368 {
3369 /* When the inserted text starts with a composing character,
3370 * backup to the character before it. There could be two of them.
3371 */
3372 i = 0;
3373 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3374 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3375 {
3376 i = (*mb_head_off)(ccline.cmdbuff,
3377 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3378 ccline.cmdpos -= i;
3379 len += i;
3380 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3381 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003382#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3384 {
3385 /* Check the previous character for Arabic combining pair. */
3386 i = (*mb_head_off)(ccline.cmdbuff,
3387 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3388 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3389 + ccline.cmdpos - i), c))
3390 {
3391 ccline.cmdpos -= i;
3392 len += i;
3393 }
3394 else
3395 i = 0;
3396 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398 if (i != 0)
3399 {
3400 /* Also backup the cursor position. */
3401 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3402 ccline.cmdspos -= i;
3403 msg_col -= i;
3404 if (msg_col < 0)
3405 {
3406 msg_col += Columns;
3407 --msg_row;
3408 }
3409 }
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411
3412 if (redraw && !cmd_silent)
3413 {
3414 msg_no_more = TRUE;
3415 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003416 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3418 /* Avoid clearing the rest of the line too often. */
3419 if (cmdline_row != i || ccline.overstrike)
3420 msg_clr_eos();
3421 msg_no_more = FALSE;
3422 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003423 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003425 m = Columns * Rows;
3426 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003428 }
3429 else
3430 m = MAXCOL;
3431 for (i = 0; i < len; ++i)
3432 {
3433 c = cmdline_charsize(ccline.cmdpos);
3434 /* count ">" for a double-wide char that doesn't fit. */
3435 if (has_mbyte)
3436 correct_cmdspos(ccline.cmdpos, c);
3437 /* Stop cursor at the end of the screen, but do increment the
3438 * insert position, so that entering a very long command
3439 * works, even though you can't see it. */
3440 if (ccline.cmdspos + c < m)
3441 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003442
Bram Moolenaar14184a32019-02-16 15:10:30 +01003443 if (has_mbyte)
3444 {
3445 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3446 if (c > len - i - 1)
3447 c = len - i - 1;
3448 ccline.cmdpos += c;
3449 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003451 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 }
3453 }
3454 if (redraw)
3455 msg_check();
3456 return retval;
3457}
3458
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003459static struct cmdline_info prev_ccline;
3460static int prev_ccline_used = FALSE;
3461
3462/*
3463 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3464 * and overwrite it. But get_cmdline_str() may need it, thus make it
3465 * available globally in prev_ccline.
3466 */
3467 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003468save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003469{
3470 if (!prev_ccline_used)
3471 {
3472 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3473 prev_ccline_used = TRUE;
3474 }
3475 *ccp = prev_ccline;
3476 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003477 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003478}
3479
3480/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003481 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003482 */
3483 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003484restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003485{
3486 ccline = prev_ccline;
3487 prev_ccline = *ccp;
3488}
3489
Bram Moolenaar8299df92004-07-10 09:47:34 +00003490/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003491 * Paste a yank register into the command line.
3492 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003493 * insert_reg() can't be used here, because special characters from the
3494 * register contents will be interpreted as commands.
3495 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003496 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003497 */
3498 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003499cmdline_paste(
3500 int regname,
3501 int literally, /* Insert text literally instead of "as typed" */
3502 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003503{
3504 long i;
3505 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003506 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003507 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003508
3509 /* check for valid regname; also accept special characters for CTRL-R in
3510 * the command line */
3511 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003512 && regname != Ctrl_A && regname != Ctrl_L
3513 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003514 return FAIL;
3515
3516 /* A register containing CTRL-R can cause an endless loop. Allow using
3517 * CTRL-C to break the loop. */
3518 line_breakcheck();
3519 if (got_int)
3520 return FAIL;
3521
3522#ifdef FEAT_CLIPBOARD
3523 regname = may_get_selection(regname);
3524#endif
3525
Bram Moolenaar438d1762018-09-30 17:11:48 +02003526 // Need to set "textlock" to avoid nasty things like going to another
3527 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003528 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003529 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003530 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531
3532 if (i)
3533 {
3534 /* Got the value of a special register in "arg". */
3535 if (arg == NULL)
3536 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003537
3538 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3539 * part of the word. */
3540 p = arg;
3541 if (p_is && regname == Ctrl_W)
3542 {
3543 char_u *w;
3544 int len;
3545
3546 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003547 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003548 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003549 if (has_mbyte)
3550 {
3551 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3552 if (!vim_iswordc(mb_ptr2char(w - len)))
3553 break;
3554 w -= len;
3555 }
3556 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003557 {
3558 if (!vim_iswordc(w[-1]))
3559 break;
3560 --w;
3561 }
3562 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003563 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003564 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3565 p += len;
3566 }
3567
3568 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003569 if (allocated)
3570 vim_free(arg);
3571 return OK;
3572 }
3573
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003574 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003575}
3576
3577/*
3578 * Put a string on the command line.
3579 * When "literally" is TRUE, insert literally.
3580 * When "literally" is FALSE, insert as typed, but don't leave the command
3581 * line.
3582 */
3583 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003584cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003585{
3586 int c, cv;
3587
3588 if (literally)
3589 put_on_cmdline(s, -1, TRUE);
3590 else
3591 while (*s != NUL)
3592 {
3593 cv = *s;
3594 if (cv == Ctrl_V && s[1])
3595 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003597 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003598 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003599 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003600 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3601 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003602#ifdef UNIX
3603 || c == intr_char
3604#endif
3605 || (c == Ctrl_BSL && *s == Ctrl_N))
3606 stuffcharReadbuff(Ctrl_V);
3607 stuffcharReadbuff(c);
3608 }
3609}
3610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611#ifdef FEAT_WILDMENU
3612/*
3613 * Delete characters on the command line, from "from" to the current
3614 * position.
3615 */
3616 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003617cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618{
3619 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3620 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3621 ccline.cmdlen -= ccline.cmdpos - from;
3622 ccline.cmdpos = from;
3623}
3624#endif
3625
3626/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003627 * This function is called when the screen size changes and with incremental
3628 * search and in other situations where the command line may have been
3629 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 */
3631 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003632redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003634 redrawcmdline_ex(TRUE);
3635}
3636
3637 void
3638redrawcmdline_ex(int do_compute_cmdrow)
3639{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (cmd_silent)
3641 return;
3642 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003643 if (do_compute_cmdrow)
3644 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 redrawcmd();
3646 cursorcmd();
3647}
3648
3649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003650redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651{
3652 int i;
3653
3654 if (cmd_silent)
3655 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003656 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 msg_putchar(ccline.cmdfirstc);
3658 if (ccline.cmdprompt != NULL)
3659 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003660 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3662 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003663 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 --ccline.cmdindent;
3665 }
3666 else
3667 for (i = ccline.cmdindent; i > 0; --i)
3668 msg_putchar(' ');
3669}
3670
3671/*
3672 * Redraw what is currently on the command line.
3673 */
3674 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003675redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 if (cmd_silent)
3678 return;
3679
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003680 /* when 'incsearch' is set there may be no command line while redrawing */
3681 if (ccline.cmdbuff == NULL)
3682 {
3683 windgoto(cmdline_row, 0);
3684 msg_clr_eos();
3685 return;
3686 }
3687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 msg_start();
3689 redrawcmdprompt();
3690
3691 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3692 msg_no_more = TRUE;
3693 draw_cmdline(0, ccline.cmdlen);
3694 msg_clr_eos();
3695 msg_no_more = FALSE;
3696
3697 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003698 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003699 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700
3701 /*
3702 * An emsg() before may have set msg_scroll. This is used in normal mode,
3703 * in cmdline mode we can reset them now.
3704 */
3705 msg_scroll = FALSE; /* next message overwrites cmdline */
3706
3707 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3708 * in cmdline mode */
3709 skip_redraw = FALSE;
3710}
3711
3712 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003713compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003715 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 cmdline_row = Rows - 1;
3717 else
3718 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003719 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720}
3721
3722 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003723cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 if (cmd_silent)
3726 return;
3727
3728#ifdef FEAT_RIGHTLEFT
3729 if (cmdmsg_rl)
3730 {
3731 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3732 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3733 if (msg_row <= 0)
3734 msg_row = Rows - 1;
3735 }
3736 else
3737#endif
3738 {
3739 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3740 msg_col = ccline.cmdspos % (int)Columns;
3741 if (msg_row >= Rows)
3742 msg_row = Rows - 1;
3743 }
3744
3745 windgoto(msg_row, msg_col);
3746#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003747 if (p_imst == IM_ON_THE_SPOT)
3748 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#endif
3750#ifdef MCH_CURSOR_SHAPE
3751 mch_update_cursor();
3752#endif
3753}
3754
3755 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003756gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757{
3758 msg_start();
3759#ifdef FEAT_RIGHTLEFT
3760 if (cmdmsg_rl)
3761 msg_col = Columns - 1;
3762 else
3763#endif
3764 msg_col = 0; /* always start in column 0 */
3765 if (clr) /* clear the bottom line(s) */
3766 msg_clr_eos(); /* will reset clear_cmdline */
3767 windgoto(cmdline_row, 0);
3768}
3769
3770/*
3771 * Check the word in front of the cursor for an abbreviation.
3772 * Called when the non-id character "c" has been entered.
3773 * When an abbreviation is recognized it is removed from the text with
3774 * backspaces and the replacement string is inserted, followed by "c".
3775 */
3776 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003777ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003779 int spos = 0;
3780
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3782 return FALSE;
3783
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003784 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3785 * Actually accepts any mark. */
3786 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3787 spos++;
3788 if (ccline.cmdlen - spos > 5
3789 && ccline.cmdbuff[spos] == '\''
3790 && ccline.cmdbuff[spos + 2] == ','
3791 && ccline.cmdbuff[spos + 3] == '\'')
3792 spos += 5;
3793 else
3794 /* check abbreviation from the beginning of the commandline */
3795 spos = 0;
3796
3797 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798}
3799
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003800#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3801 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003802sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003803{
3804 char_u *p1 = *(char_u **)s1;
3805 char_u *p2 = *(char_u **)s2;
3806
3807 if (*p1 != '<' && *p2 == '<') return -1;
3808 if (*p1 == '<' && *p2 != '<') return 1;
3809 return STRCMP(p1, p2);
3810}
3811#endif
3812
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813/*
3814 * Return FAIL if this is not an appropriate context in which to do
3815 * completion of anything, return OK if it is (even if there are no matches).
3816 * For the caller, this means that the character is just passed through like a
3817 * normal character (instead of being expanded). This allows :s/^I^D etc.
3818 */
3819 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003820nextwild(
3821 expand_T *xp,
3822 int type,
3823 int options, /* extra options for ExpandOne() */
3824 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825{
3826 int i, j;
3827 char_u *p1;
3828 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 int difflen;
3830 int v;
3831
3832 if (xp->xp_numfiles == -1)
3833 {
3834 set_expand_context(xp);
3835 cmd_showtail = expand_showtail(xp);
3836 }
3837
3838 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3839 {
3840 beep_flush();
3841 return OK; /* Something illegal on command line */
3842 }
3843 if (xp->xp_context == EXPAND_NOTHING)
3844 {
3845 /* Caller can use the character as a normal char instead */
3846 return FAIL;
3847 }
3848
Bram Moolenaar32526b32019-01-19 17:43:09 +01003849 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 out_flush();
3851
3852 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003853 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
3855 if (type == WILD_NEXT || type == WILD_PREV)
3856 {
3857 /*
3858 * Get next/previous match for a previous expanded pattern.
3859 */
3860 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3861 }
3862 else
3863 {
3864 /*
3865 * Translate string into pattern and expand it.
3866 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003867 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3868 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 p2 = NULL;
3870 else
3871 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003872 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003873 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3874 if (escape)
3875 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003876
3877 if (p_wic)
3878 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003879 p2 = ExpandOne(xp, p1,
3880 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003881 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003883 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 if (p2 != NULL && type == WILD_LONGEST)
3885 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003886 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (ccline.cmdbuff[i + j] == '*'
3888 || ccline.cmdbuff[i + j] == '?')
3889 break;
3890 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003891 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 }
3893 }
3894 }
3895
3896 if (p2 != NULL && !got_int)
3897 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003898 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003899 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003901 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 xp->xp_pattern = ccline.cmdbuff + i;
3903 }
3904 else
3905 v = OK;
3906 if (v == OK)
3907 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003908 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3909 &ccline.cmdbuff[ccline.cmdpos],
3910 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3911 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 ccline.cmdlen += difflen;
3913 ccline.cmdpos += difflen;
3914 }
3915 }
3916 vim_free(p2);
3917
3918 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003919 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 /* When expanding a ":map" command and no matches are found, assume that
3922 * the key is supposed to be inserted literally */
3923 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3924 return FAIL;
3925
3926 if (xp->xp_numfiles <= 0 && p2 == NULL)
3927 beep_flush();
3928 else if (xp->xp_numfiles == 1)
3929 /* free expanded pattern */
3930 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3931
3932 return OK;
3933}
3934
3935/*
3936 * Do wildcard expansion on the string 'str'.
3937 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003938 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 * Return NULL for failure.
3940 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003941 * "orig" is the originally expanded string, copied to allocated memory. It
3942 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3943 * WILD_PREV "orig" should be NULL.
3944 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003945 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3946 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 *
3948 * mode = WILD_FREE: just free previously expanded matches
3949 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3950 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3951 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3952 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3953 * mode = WILD_ALL: return all matches concatenated
3954 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003955 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 *
3957 * options = WILD_LIST_NOTFOUND: list entries without a match
3958 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3959 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3960 * options = WILD_NO_BEEP: Don't beep for multiple matches
3961 * options = WILD_ADD_SLASH: add a slash after directory names
3962 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3963 * options = WILD_SILENT: don't print warning messages
3964 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003965 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 *
3967 * The variables xp->xp_context and xp->xp_backslash must have been set!
3968 */
3969 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003970ExpandOne(
3971 expand_T *xp,
3972 char_u *str,
3973 char_u *orig, /* allocated copy of original of expanded string */
3974 int options,
3975 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976{
3977 char_u *ss = NULL;
3978 static int findex;
3979 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003980 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 int i;
3982 long_u len;
3983 int non_suf_match; /* number without matching suffix */
3984
3985 /*
3986 * first handle the case of using an old match
3987 */
3988 if (mode == WILD_NEXT || mode == WILD_PREV)
3989 {
3990 if (xp->xp_numfiles > 0)
3991 {
3992 if (mode == WILD_PREV)
3993 {
3994 if (findex == -1)
3995 findex = xp->xp_numfiles;
3996 --findex;
3997 }
3998 else /* mode == WILD_NEXT */
3999 ++findex;
4000
4001 /*
4002 * When wrapping around, return the original string, set findex to
4003 * -1.
4004 */
4005 if (findex < 0)
4006 {
4007 if (orig_save == NULL)
4008 findex = xp->xp_numfiles - 1;
4009 else
4010 findex = -1;
4011 }
4012 if (findex >= xp->xp_numfiles)
4013 {
4014 if (orig_save == NULL)
4015 findex = 0;
4016 else
4017 findex = -1;
4018 }
4019#ifdef FEAT_WILDMENU
4020 if (p_wmnu)
4021 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4022 findex, cmd_showtail);
4023#endif
4024 if (findex == -1)
4025 return vim_strsave(orig_save);
4026 return vim_strsave(xp->xp_files[findex]);
4027 }
4028 else
4029 return NULL;
4030 }
4031
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004032 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4034 {
4035 FreeWild(xp->xp_numfiles, xp->xp_files);
4036 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004037 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 }
4039 findex = 0;
4040
4041 if (mode == WILD_FREE) /* only release file name */
4042 return NULL;
4043
4044 if (xp->xp_numfiles == -1)
4045 {
4046 vim_free(orig_save);
4047 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004048 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049
4050 /*
4051 * Do the expansion.
4052 */
4053 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4054 options) == FAIL)
4055 {
4056#ifdef FNAME_ILLEGAL
4057 /* Illegal file name has been silently skipped. But when there
4058 * are wildcards, the real problem is that there was no match,
4059 * causing the pattern to be added, which has illegal characters.
4060 */
4061 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004062 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063#endif
4064 }
4065 else if (xp->xp_numfiles == 0)
4066 {
4067 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004068 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 else
4071 {
4072 /* Escape the matches for use on the command line. */
4073 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4074
4075 /*
4076 * Check for matching suffixes in file names.
4077 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004078 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4079 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 {
4081 if (xp->xp_numfiles)
4082 non_suf_match = xp->xp_numfiles;
4083 else
4084 non_suf_match = 1;
4085 if ((xp->xp_context == EXPAND_FILES
4086 || xp->xp_context == EXPAND_DIRECTORIES)
4087 && xp->xp_numfiles > 1)
4088 {
4089 /*
4090 * More than one match; check suffix.
4091 * The files will have been sorted on matching suffix in
4092 * expand_wildcards, only need to check the first two.
4093 */
4094 non_suf_match = 0;
4095 for (i = 0; i < 2; ++i)
4096 if (match_suffix(xp->xp_files[i]))
4097 ++non_suf_match;
4098 }
4099 if (non_suf_match != 1)
4100 {
4101 /* Can we ever get here unless it's while expanding
4102 * interactively? If not, we can get rid of this all
4103 * together. Don't really want to wait for this message
4104 * (and possibly have to hit return to continue!).
4105 */
4106 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004107 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 else if (!(options & WILD_NO_BEEP))
4109 beep_flush();
4110 }
4111 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4112 ss = vim_strsave(xp->xp_files[0]);
4113 }
4114 }
4115 }
4116
4117 /* Find longest common part */
4118 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4119 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004120 int mb_len = 1;
4121 int c0, ci;
4122
4123 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004125 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004127 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4128 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4129 }
4130 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004131 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004132 for (i = 1; i < xp->xp_numfiles; ++i)
4133 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004134 if (has_mbyte)
4135 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4136 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004137 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004138 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004140 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004141 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004143 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 break;
4145 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004146 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 break;
4148 }
4149 if (i < xp->xp_numfiles)
4150 {
4151 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004152 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 break;
4154 }
4155 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004156
Bram Moolenaar964b3742019-05-24 18:54:09 +02004157 ss = alloc(len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004159 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 findex = -1; /* next p_wc gets first one */
4161 }
4162
4163 /* Concatenate all matching names */
4164 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4165 {
4166 len = 0;
4167 for (i = 0; i < xp->xp_numfiles; ++i)
4168 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4169 ss = lalloc(len, TRUE);
4170 if (ss != NULL)
4171 {
4172 *ss = NUL;
4173 for (i = 0; i < xp->xp_numfiles; ++i)
4174 {
4175 STRCAT(ss, xp->xp_files[i]);
4176 if (i != xp->xp_numfiles - 1)
4177 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4178 }
4179 }
4180 }
4181
4182 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4183 ExpandCleanup(xp);
4184
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004185 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004186 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004187 vim_free(orig);
4188
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return ss;
4190}
4191
4192/*
4193 * Prepare an expand structure for use.
4194 */
4195 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004196ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004198 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004199 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004201#ifndef BACKSLASH_IN_FILENAME
4202 xp->xp_shell = FALSE;
4203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 xp->xp_numfiles = -1;
4205 xp->xp_files = NULL;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004206#if defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004207 xp->xp_arg = NULL;
4208#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004209 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210}
4211
4212/*
4213 * Cleanup an expand structure after use.
4214 */
4215 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004216ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217{
4218 if (xp->xp_numfiles >= 0)
4219 {
4220 FreeWild(xp->xp_numfiles, xp->xp_files);
4221 xp->xp_numfiles = -1;
4222 }
4223}
4224
4225 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004226ExpandEscape(
4227 expand_T *xp,
4228 char_u *str,
4229 int numfiles,
4230 char_u **files,
4231 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
4233 int i;
4234 char_u *p;
4235
4236 /*
4237 * May change home directory back to "~"
4238 */
4239 if (options & WILD_HOME_REPLACE)
4240 tilde_replace(str, numfiles, files);
4241
4242 if (options & WILD_ESCAPE)
4243 {
4244 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004245 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004246 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 || xp->xp_context == EXPAND_BUFFERS
4248 || xp->xp_context == EXPAND_DIRECTORIES)
4249 {
4250 /*
4251 * Insert a backslash into a file name before a space, \, %, #
4252 * and wildmatch characters, except '~'.
4253 */
4254 for (i = 0; i < numfiles; ++i)
4255 {
4256 /* for ":set path=" we need to escape spaces twice */
4257 if (xp->xp_backslash == XP_BS_THREE)
4258 {
4259 p = vim_strsave_escaped(files[i], (char_u *)" ");
4260 if (p != NULL)
4261 {
4262 vim_free(files[i]);
4263 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004264#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 p = vim_strsave_escaped(files[i], (char_u *)" ");
4266 if (p != NULL)
4267 {
4268 vim_free(files[i]);
4269 files[i] = p;
4270 }
4271#endif
4272 }
4273 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004274#ifdef BACKSLASH_IN_FILENAME
4275 p = vim_strsave_fnameescape(files[i], FALSE);
4276#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004277 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 if (p != NULL)
4280 {
4281 vim_free(files[i]);
4282 files[i] = p;
4283 }
4284
4285 /* If 'str' starts with "\~", replace "~" at start of
4286 * files[i] with "\~". */
4287 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004288 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004291
4292 /* If the first file starts with a '+' escape it. Otherwise it
4293 * could be seen as "+cmd". */
4294 if (*files[0] == '+')
4295 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 else if (xp->xp_context == EXPAND_TAGS)
4298 {
4299 /*
4300 * Insert a backslash before characters in a tag name that
4301 * would terminate the ":tag" command.
4302 */
4303 for (i = 0; i < numfiles; ++i)
4304 {
4305 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4306 if (p != NULL)
4307 {
4308 vim_free(files[i]);
4309 files[i] = p;
4310 }
4311 }
4312 }
4313 }
4314}
4315
4316/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004317 * Escape special characters in "fname" for when used as a file name argument
4318 * after a Vim command, or, when "shell" is non-zero, a shell command.
4319 * Returns the result in allocated memory.
4320 */
4321 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004322vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004323{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004324 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004325#ifdef BACKSLASH_IN_FILENAME
4326 char_u buf[20];
4327 int j = 0;
4328
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004329 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004330 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004331 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004332 buf[j++] = *p;
4333 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004334 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004335#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004336 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4337 if (shell && csh_like_shell() && p != NULL)
4338 {
4339 char_u *s;
4340
4341 /* For csh and similar shells need to put two backslashes before '!'.
4342 * One is taken by Vim, one by the shell. */
4343 s = vim_strsave_escaped(p, (char_u *)"!");
4344 vim_free(p);
4345 p = s;
4346 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004347#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004348
4349 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4350 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004351 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004352 escape_fname(&p);
4353
4354 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004355}
4356
4357/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004358 * Put a backslash before the file name in "pp", which is in allocated memory.
4359 */
4360 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004361escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004362{
4363 char_u *p;
4364
Bram Moolenaar964b3742019-05-24 18:54:09 +02004365 p = alloc(STRLEN(*pp) + 2);
Bram Moolenaar45360022005-07-21 21:08:21 +00004366 if (p != NULL)
4367 {
4368 p[0] = '\\';
4369 STRCPY(p + 1, *pp);
4370 vim_free(*pp);
4371 *pp = p;
4372 }
4373}
4374
4375/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * For each file name in files[num_files]:
4377 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4378 */
4379 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004380tilde_replace(
4381 char_u *orig_pat,
4382 int num_files,
4383 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 int i;
4386 char_u *p;
4387
4388 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4389 {
4390 for (i = 0; i < num_files; ++i)
4391 {
4392 p = home_replace_save(NULL, files[i]);
4393 if (p != NULL)
4394 {
4395 vim_free(files[i]);
4396 files[i] = p;
4397 }
4398 }
4399 }
4400}
4401
4402/*
4403 * Show all matches for completion on the command line.
4404 * Returns EXPAND_NOTHING when the character that triggered expansion should
4405 * be inserted like a normal character.
4406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004408showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409{
4410#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4411 int num_files;
4412 char_u **files_found;
4413 int i, j, k;
4414 int maxlen;
4415 int lines;
4416 int columns;
4417 char_u *p;
4418 int lastlen;
4419 int attr;
4420 int showtail;
4421
4422 if (xp->xp_numfiles == -1)
4423 {
4424 set_expand_context(xp);
4425 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4426 &num_files, &files_found);
4427 showtail = expand_showtail(xp);
4428 if (i != EXPAND_OK)
4429 return i;
4430
4431 }
4432 else
4433 {
4434 num_files = xp->xp_numfiles;
4435 files_found = xp->xp_files;
4436 showtail = cmd_showtail;
4437 }
4438
4439#ifdef FEAT_WILDMENU
4440 if (!wildmenu)
4441 {
4442#endif
4443 msg_didany = FALSE; /* lines_left will be set */
4444 msg_start(); /* prepare for paging */
4445 msg_putchar('\n');
4446 out_flush();
4447 cmdline_row = msg_row;
4448 msg_didany = FALSE; /* lines_left will be set again */
4449 msg_start(); /* prepare for paging */
4450#ifdef FEAT_WILDMENU
4451 }
4452#endif
4453
4454 if (got_int)
4455 got_int = FALSE; /* only int. the completion, not the cmd line */
4456#ifdef FEAT_WILDMENU
4457 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004458 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459#endif
4460 else
4461 {
4462 /* find the length of the longest file name */
4463 maxlen = 0;
4464 for (i = 0; i < num_files; ++i)
4465 {
4466 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004467 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 || xp->xp_context == EXPAND_BUFFERS))
4469 {
4470 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4471 j = vim_strsize(NameBuff);
4472 }
4473 else
4474 j = vim_strsize(L_SHOWFILE(i));
4475 if (j > maxlen)
4476 maxlen = j;
4477 }
4478
4479 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4480 lines = num_files;
4481 else
4482 {
4483 /* compute the number of columns and lines for the listing */
4484 maxlen += 2; /* two spaces between file names */
4485 columns = ((int)Columns + 2) / maxlen;
4486 if (columns < 1)
4487 columns = 1;
4488 lines = (num_files + columns - 1) / columns;
4489 }
4490
Bram Moolenaar8820b482017-03-16 17:23:31 +01004491 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492
4493 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4494 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004495 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 msg_clr_eos();
4497 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004498 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 }
4500
4501 /* list the files line by line */
4502 for (i = 0; i < lines; ++i)
4503 {
4504 lastlen = 999;
4505 for (k = i; k < num_files; k += lines)
4506 {
4507 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4508 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004509 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 p = files_found[k] + STRLEN(files_found[k]) + 1;
4511 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004512 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004514 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 break;
4516 }
4517 for (j = maxlen - lastlen; --j >= 0; )
4518 msg_putchar(' ');
4519 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004520 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 || xp->xp_context == EXPAND_BUFFERS)
4522 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004523 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004524 if (xp->xp_numfiles != -1)
4525 {
4526 char_u *halved_slash;
4527 char_u *exp_path;
4528
4529 /* Expansion was done before and special characters
4530 * were escaped, need to halve backslashes. Also
4531 * $HOME has been replaced with ~/. */
4532 exp_path = expand_env_save_opt(files_found[k], TRUE);
4533 halved_slash = backslash_halve_save(
4534 exp_path != NULL ? exp_path : files_found[k]);
4535 j = mch_isdir(halved_slash != NULL ? halved_slash
4536 : files_found[k]);
4537 vim_free(exp_path);
4538 vim_free(halved_slash);
4539 }
4540 else
4541 /* Expansion was done here, file names are literal. */
4542 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 if (showtail)
4544 p = L_SHOWFILE(k);
4545 else
4546 {
4547 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4548 TRUE);
4549 p = NameBuff;
4550 }
4551 }
4552 else
4553 {
4554 j = FALSE;
4555 p = L_SHOWFILE(k);
4556 }
4557 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4558 }
4559 if (msg_col > 0) /* when not wrapped around */
4560 {
4561 msg_clr_eos();
4562 msg_putchar('\n');
4563 }
4564 out_flush(); /* show one line at a time */
4565 if (got_int)
4566 {
4567 got_int = FALSE;
4568 break;
4569 }
4570 }
4571
4572 /*
4573 * we redraw the command below the lines that we have just listed
4574 * This is a bit tricky, but it saves a lot of screen updating.
4575 */
4576 cmdline_row = msg_row; /* will put it back later */
4577 }
4578
4579 if (xp->xp_numfiles == -1)
4580 FreeWild(num_files, files_found);
4581
4582 return EXPAND_OK;
4583}
4584
4585/*
4586 * Private gettail for showmatches() (and win_redr_status_matches()):
4587 * Find tail of file name path, but ignore trailing "/".
4588 */
4589 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004590sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591{
4592 char_u *p;
4593 char_u *t = s;
4594 int had_sep = FALSE;
4595
4596 for (p = s; *p != NUL; )
4597 {
4598 if (vim_ispathsep(*p)
4599#ifdef BACKSLASH_IN_FILENAME
4600 && !rem_backslash(p)
4601#endif
4602 )
4603 had_sep = TRUE;
4604 else if (had_sep)
4605 {
4606 t = p;
4607 had_sep = FALSE;
4608 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004609 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 }
4611 return t;
4612}
4613
4614/*
4615 * Return TRUE if we only need to show the tail of completion matches.
4616 * When not completing file names or there is a wildcard in the path FALSE is
4617 * returned.
4618 */
4619 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004620expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621{
4622 char_u *s;
4623 char_u *end;
4624
4625 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004626 if (xp->xp_context != EXPAND_FILES
4627 && xp->xp_context != EXPAND_SHELLCMD
4628 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 return FALSE;
4630
4631 end = gettail(xp->xp_pattern);
4632 if (end == xp->xp_pattern) /* there is no path separator */
4633 return FALSE;
4634
4635 for (s = xp->xp_pattern; s < end; s++)
4636 {
4637 /* Skip escaped wildcards. Only when the backslash is not a path
4638 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4639 if (rem_backslash(s))
4640 ++s;
4641 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4642 return FALSE;
4643 }
4644 return TRUE;
4645}
4646
4647/*
4648 * Prepare a string for expansion.
4649 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004650 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 * When expanding other names: The string will be used with regcomp(). Copy
4652 * the name into allocated memory and prepend "^".
4653 */
4654 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004655addstar(
4656 char_u *fname,
4657 int len,
4658 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659{
4660 char_u *retval;
4661 int i, j;
4662 int new_len;
4663 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004664 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004666 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004667 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004668 && context != EXPAND_SHELLCMD
4669 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 /*
4672 * Matching will be done internally (on something other than files).
4673 * So we convert the file-matching-type wildcards into our kind for
4674 * use with vim_regcomp(). First work out how long it will be:
4675 */
4676
4677 /* For help tags the translation is done in find_help_tags().
4678 * For a tag pattern starting with "/" no translation is needed. */
4679 if (context == EXPAND_HELP
4680 || context == EXPAND_COLORS
4681 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004682 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004683 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004684 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004685 || ((context == EXPAND_TAGS_LISTFILES
4686 || context == EXPAND_TAGS)
4687 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 retval = vim_strnsave(fname, len);
4689 else
4690 {
4691 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4692 for (i = 0; i < len; i++)
4693 {
4694 if (fname[i] == '*' || fname[i] == '~')
4695 new_len++; /* '*' needs to be replaced by ".*"
4696 '~' needs to be replaced by "\~" */
4697
4698 /* Buffer names are like file names. "." should be literal */
4699 if (context == EXPAND_BUFFERS && fname[i] == '.')
4700 new_len++; /* "." becomes "\." */
4701
4702 /* Custom expansion takes care of special things, match
4703 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004704 if ((context == EXPAND_USER_DEFINED
4705 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 new_len++; /* '\' becomes "\\" */
4707 }
4708 retval = alloc(new_len);
4709 if (retval != NULL)
4710 {
4711 retval[0] = '^';
4712 j = 1;
4713 for (i = 0; i < len; i++, j++)
4714 {
4715 /* Skip backslash. But why? At least keep it for custom
4716 * expansion. */
4717 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004718 && context != EXPAND_USER_LIST
4719 && fname[i] == '\\'
4720 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 break;
4722
4723 switch (fname[i])
4724 {
4725 case '*': retval[j++] = '.';
4726 break;
4727 case '~': retval[j++] = '\\';
4728 break;
4729 case '?': retval[j] = '.';
4730 continue;
4731 case '.': if (context == EXPAND_BUFFERS)
4732 retval[j++] = '\\';
4733 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004734 case '\\': if (context == EXPAND_USER_DEFINED
4735 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 retval[j++] = '\\';
4737 break;
4738 }
4739 retval[j] = fname[i];
4740 }
4741 retval[j] = NUL;
4742 }
4743 }
4744 }
4745 else
4746 {
4747 retval = alloc(len + 4);
4748 if (retval != NULL)
4749 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004750 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004753 * Don't add a star to *, ~, ~user, $var or `cmd`.
4754 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 * ~ would be at the start of the file name, but not the tail.
4756 * $ could be anywhere in the tail.
4757 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004758 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 */
4760 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004761 ends_in_star = (len > 0 && retval[len - 1] == '*');
4762#ifndef BACKSLASH_IN_FILENAME
4763 for (i = len - 2; i >= 0; --i)
4764 {
4765 if (retval[i] != '\\')
4766 break;
4767 ends_in_star = !ends_in_star;
4768 }
4769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004771 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 && vim_strchr(tail, '$') == NULL
4773 && vim_strchr(retval, '`') == NULL)
4774 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004775 else if (len > 0 && retval[len - 1] == '$')
4776 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 retval[len] = NUL;
4778 }
4779 }
4780 return retval;
4781}
4782
4783/*
4784 * Must parse the command line so far to work out what context we are in.
4785 * Completion can then be done based on that context.
4786 * This routine sets the variables:
4787 * xp->xp_pattern The start of the pattern to be expanded within
4788 * the command line (ends at the cursor).
4789 * xp->xp_context The type of thing to expand. Will be one of:
4790 *
4791 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4792 * the command line, like an unknown command. Caller
4793 * should beep.
4794 * EXPAND_NOTHING Unrecognised context for completion, use char like
4795 * a normal char, rather than for completion. eg
4796 * :s/^I/
4797 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4798 * it.
4799 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4800 * EXPAND_FILES After command with XFILE set, or after setting
4801 * with P_EXPAND set. eg :e ^I, :w>>^I
4802 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4803 * when we know only directories are of interest. eg
4804 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004805 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4807 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4808 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4809 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4810 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4811 * EXPAND_EVENTS Complete event names
4812 * EXPAND_SYNTAX Complete :syntax command arguments
4813 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4814 * EXPAND_AUGROUP Complete autocommand group names
4815 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4816 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4817 * eg :unmap a^I , :cunab x^I
4818 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4819 * eg :call sub^I
4820 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4821 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4822 * names in expressions, eg :while s^I
4823 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004824 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 */
4826 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004827set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004829 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 if (ccline.cmdfirstc != ':'
4831#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004832 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004833 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#endif
4835 )
4836 {
4837 xp->xp_context = EXPAND_NOTHING;
4838 return;
4839 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004840 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841}
4842
4843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004844set_cmd_context(
4845 expand_T *xp,
4846 char_u *str, /* start of command line */
4847 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004848 int col, /* position of cursor */
4849 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850{
4851 int old_char = NUL;
4852 char_u *nextcomm;
4853
4854 /*
4855 * Avoid a UMR warning from Purify, only save the character if it has been
4856 * written before.
4857 */
4858 if (col < len)
4859 old_char = str[col];
4860 str[col] = NUL;
4861 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004862
4863#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004864 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004865 {
4866# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004867 /* pass CMD_SIZE because there is no real command */
4868 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004869# endif
4870 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004871 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004872 {
4873 xp->xp_context = ccline.xp_context;
4874 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaarac9fb182019-04-27 13:04:13 +02004875# if defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004876 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004877# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004878 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004879 else
4880#endif
4881 while (nextcomm != NULL)
4882 nextcomm = set_one_cmd_context(xp, nextcomm);
4883
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004884 /* Store the string here so that call_user_expand_func() can get to them
4885 * easily. */
4886 xp->xp_line = str;
4887 xp->xp_col = col;
4888
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 str[col] = old_char;
4890}
4891
4892/*
4893 * Expand the command line "str" from context "xp".
4894 * "xp" must have been set by set_cmd_context().
4895 * xp->xp_pattern points into "str", to where the text that is to be expanded
4896 * starts.
4897 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4898 * cursor.
4899 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4900 * key that triggered expansion literally.
4901 * Returns EXPAND_OK otherwise.
4902 */
4903 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004904expand_cmdline(
4905 expand_T *xp,
4906 char_u *str, /* start of command line */
4907 int col, /* position of cursor */
4908 int *matchcount, /* return: nr of matches */
4909 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
4911 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004912 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913
4914 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4915 {
4916 beep_flush();
4917 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4918 }
4919 if (xp->xp_context == EXPAND_NOTHING)
4920 {
4921 /* Caller can use the character as a normal char instead */
4922 return EXPAND_NOTHING;
4923 }
4924
4925 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004926 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4927 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 if (file_str == NULL)
4929 return EXPAND_UNSUCCESSFUL;
4930
Bram Moolenaar94950a92010-12-02 16:01:29 +01004931 if (p_wic)
4932 options += WILD_ICASE;
4933
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004935 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 {
4937 *matchcount = 0;
4938 *matches = NULL;
4939 }
4940 vim_free(file_str);
4941
4942 return EXPAND_OK;
4943}
4944
4945#ifdef FEAT_MULTI_LANG
4946/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004947 * Cleanup matches for help tags:
4948 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4949 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004952cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953{
4954 int i, j;
4955 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004956 char_u buf[4];
4957 char_u *p = buf;
4958
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004959 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004960 {
4961 *p++ = '@';
4962 *p++ = p_hlg[0];
4963 *p++ = p_hlg[1];
4964 }
4965 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966
4967 for (i = 0; i < num_file; ++i)
4968 {
4969 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004970 if (len <= 0)
4971 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004972 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 {
4974 /* Sorting on priority means the same item in another language may
4975 * be anywhere. Search all items for a match up to the "@en". */
4976 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004977 if (j != i && (int)STRLEN(file[j]) == len + 3
4978 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 break;
4980 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004981 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 file[i][len] = NUL;
4983 }
4984 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004985
4986 if (*buf != NUL)
4987 for (i = 0; i < num_file; ++i)
4988 {
4989 len = (int)STRLEN(file[i]) - 3;
4990 if (len <= 0)
4991 continue;
4992 if (STRCMP(file[i] + len, buf) == 0)
4993 {
4994 /* remove the default language */
4995 file[i][len] = NUL;
4996 }
4997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998}
4999#endif
5000
5001/*
5002 * Do the expansion based on xp->xp_context and "pat".
5003 */
5004 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005005ExpandFromContext(
5006 expand_T *xp,
5007 char_u *pat,
5008 int *num_file,
5009 char_u ***file,
5010 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011{
5012#ifdef FEAT_CMDL_COMPL
5013 regmatch_T regmatch;
5014#endif
5015 int ret;
5016 int flags;
5017
5018 flags = EW_DIR; /* include directories */
5019 if (options & WILD_LIST_NOTFOUND)
5020 flags |= EW_NOTFOUND;
5021 if (options & WILD_ADD_SLASH)
5022 flags |= EW_ADDSLASH;
5023 if (options & WILD_KEEP_ALL)
5024 flags |= EW_KEEPALL;
5025 if (options & WILD_SILENT)
5026 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005027 if (options & WILD_ALLLINKS)
5028 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005030 if (xp->xp_context == EXPAND_FILES
5031 || xp->xp_context == EXPAND_DIRECTORIES
5032 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 /*
5035 * Expand file or directory names.
5036 */
5037 int free_pat = FALSE;
5038 int i;
5039
5040 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5041 * space */
5042 if (xp->xp_backslash != XP_BS_NONE)
5043 {
5044 free_pat = TRUE;
5045 pat = vim_strsave(pat);
5046 for (i = 0; pat[i]; ++i)
5047 if (pat[i] == '\\')
5048 {
5049 if (xp->xp_backslash == XP_BS_THREE
5050 && pat[i + 1] == '\\'
5051 && pat[i + 2] == '\\'
5052 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005053 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (xp->xp_backslash == XP_BS_ONE
5055 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005056 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 }
5059
5060 if (xp->xp_context == EXPAND_FILES)
5061 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005062 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5063 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 else
5065 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005066 if (options & WILD_ICASE)
5067 flags |= EW_ICASE;
5068
Bram Moolenaard7834d32009-12-02 16:14:36 +00005069 /* Expand wildcards, supporting %:h and the like. */
5070 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 if (free_pat)
5072 vim_free(pat);
5073 return ret;
5074 }
5075
5076 *file = (char_u **)"";
5077 *num_file = 0;
5078 if (xp->xp_context == EXPAND_HELP)
5079 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005080 /* With an empty argument we would get all the help tags, which is
5081 * very slow. Get matches for "help" instead. */
5082 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5083 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
5085#ifdef FEAT_MULTI_LANG
5086 cleanup_help_tags(*num_file, *file);
5087#endif
5088 return OK;
5089 }
5090 return FAIL;
5091 }
5092
5093#ifndef FEAT_CMDL_COMPL
5094 return FAIL;
5095#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005096 if (xp->xp_context == EXPAND_SHELLCMD)
5097 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 if (xp->xp_context == EXPAND_OLD_SETTING)
5099 return ExpandOldSetting(num_file, file);
5100 if (xp->xp_context == EXPAND_BUFFERS)
5101 return ExpandBufnames(pat, num_file, file, options);
5102 if (xp->xp_context == EXPAND_TAGS
5103 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5104 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5105 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005106 {
5107 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005108 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5109 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005112 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005113 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005114 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005115 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005116 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005117 {
5118 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005119 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005120 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005121 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005122 {
5123 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005124 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005125 }
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005126# if defined(FEAT_EVAL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005127 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005128 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005129# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005130 if (xp->xp_context == EXPAND_PACKADD)
5131 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
5133 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5134 if (regmatch.regprog == NULL)
5135 return FAIL;
5136
5137 /* set ignore-case according to p_ic, p_scs and pat */
5138 regmatch.rm_ic = ignorecase(pat);
5139
5140 if (xp->xp_context == EXPAND_SETTINGS
5141 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5142 ret = ExpandSettings(xp, &regmatch, num_file, file);
5143 else if (xp->xp_context == EXPAND_MAPPINGS)
5144 ret = ExpandMappings(&regmatch, num_file, file);
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005145# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else if (xp->xp_context == EXPAND_USER_DEFINED)
5147 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5148# endif
5149 else
5150 {
5151 static struct expgen
5152 {
5153 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005154 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005156 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 } tab[] =
5158 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005159 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5160 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005161 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005162 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005163#ifdef FEAT_CMDHIST
5164 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5165#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005166 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005167 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005168 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5169 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5170 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005172 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5173 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5174 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5175 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#endif
5177#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005178 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5179 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#endif
5181#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005182 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005184#ifdef FEAT_PROFILE
5185 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5186#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005187 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005188 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5189 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005190#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005191 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005192#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005193#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005194 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005195#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005196#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005197 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005198#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005199#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005200 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5201 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005203 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005204 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005205 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 };
5207 int i;
5208
5209 /*
5210 * Find a context in the table and call the ExpandGeneric() with the
5211 * right function to do the expansion.
5212 */
5213 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005214 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (xp->xp_context == tab[i].context)
5216 {
5217 if (tab[i].ic)
5218 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005219 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005220 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 break;
5222 }
5223 }
5224
Bram Moolenaar473de612013-06-08 18:19:48 +02005225 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
5227 return ret;
5228#endif /* FEAT_CMDL_COMPL */
5229}
5230
5231#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5232/*
5233 * Expand a list of names.
5234 *
5235 * Generic function for command line completion. It calls a function to
5236 * obtain strings, one by one. The strings are matched against a regexp
5237 * program. Matching strings are copied into an array, which is returned.
5238 *
5239 * Returns OK when no problems encountered, FAIL for error (out of memory).
5240 */
5241 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005242ExpandGeneric(
5243 expand_T *xp,
5244 regmatch_T *regmatch,
5245 int *num_file,
5246 char_u ***file,
5247 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005249 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250{
5251 int i;
5252 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005253 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 char_u *str;
5255
5256 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005257 * round == 0: count the number of matching names
5258 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005260 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 {
5262 for (i = 0; ; ++i)
5263 {
5264 str = (*func)(xp, i);
5265 if (str == NULL) /* end of list */
5266 break;
5267 if (*str == NUL) /* skip empty strings */
5268 continue;
5269
5270 if (vim_regexec(regmatch, str, (colnr_T)0))
5271 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005272 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005274 if (escaped)
5275 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5276 else
5277 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 (*file)[count] = str;
5279#ifdef FEAT_MENU
5280 if (func == get_menu_names && str != NULL)
5281 {
5282 /* test for separator added by get_menu_names() */
5283 str += STRLEN(str) - 1;
5284 if (*str == '\001')
5285 *str = '.';
5286 }
5287#endif
5288 }
5289 ++count;
5290 }
5291 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005292 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
5294 if (count == 0)
5295 return OK;
5296 *num_file = count;
Bram Moolenaar964b3742019-05-24 18:54:09 +02005297 *file = (char_u **)alloc(count * sizeof(char_u *));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (*file == NULL)
5299 {
5300 *file = (char_u **)"";
5301 return FAIL;
5302 }
5303 count = 0;
5304 }
5305 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005306
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005307 /* Sort the results. Keep menu's in the specified order. */
5308 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005309 {
5310 if (xp->xp_context == EXPAND_EXPRESSION
5311 || xp->xp_context == EXPAND_FUNCTIONS
5312 || xp->xp_context == EXPAND_USER_FUNC)
5313 /* <SNR> functions should be sorted to the end. */
5314 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5315 sort_func_compare);
5316 else
5317 sort_strings(*file, *num_file);
5318 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005319
Bram Moolenaar4f688582007-07-24 12:34:30 +00005320#ifdef FEAT_CMDL_COMPL
5321 /* Reset the variables used for special highlight names expansion, so that
5322 * they don't show up when getting normal highlight names by ID. */
5323 reset_expand_highlight();
5324#endif
5325
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 return OK;
5327}
5328
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005329/*
5330 * Complete a shell command.
5331 * Returns FAIL or OK;
5332 */
5333 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005334expand_shellcmd(
5335 char_u *filepat, /* pattern to match with command names */
5336 int *num_file, /* return: number of matches */
5337 char_u ***file, /* return: array with matches */
5338 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005339{
5340 char_u *pat;
5341 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005342 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005343 int mustfree = FALSE;
5344 garray_T ga;
5345 char_u *buf = alloc(MAXPATHL);
5346 size_t l;
5347 char_u *s, *e;
5348 int flags = flagsarg;
5349 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005350 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005351 hashtab_T found_ht;
5352 hashitem_T *hi;
5353 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005354
5355 if (buf == NULL)
5356 return FAIL;
5357
5358 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5359 * space */
5360 pat = vim_strsave(filepat);
5361 for (i = 0; pat[i]; ++i)
5362 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005363 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005364
Bram Moolenaarb5971142015-03-21 17:32:19 +01005365 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005366
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005367 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5368 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005369 path = (char_u *)".";
5370 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005371 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005372 /* For an absolute name we don't use $PATH. */
5373 if (!mch_isFullName(pat))
5374 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005375 if (path == NULL)
5376 path = (char_u *)"";
5377 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005378
5379 /*
5380 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005381 * collect them in "ga". When "." is not in $PATH also expand for the
5382 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005383 */
5384 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005385 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005386 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005387 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005388#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005389 e = vim_strchr(s, ';');
5390#else
5391 e = vim_strchr(s, ':');
5392#endif
5393 if (e == NULL)
5394 e = s + STRLEN(s);
5395
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005396 if (*s == NUL)
5397 {
5398 if (did_curdir)
5399 break;
5400 // Find directories in the current directory, path is empty.
5401 did_curdir = TRUE;
5402 flags |= EW_DIR;
5403 }
5404 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5405 {
5406 did_curdir = TRUE;
5407 flags |= EW_DIR;
5408 }
5409 else
5410 // Do not match directories inside a $PATH item.
5411 flags &= ~EW_DIR;
5412
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005413 l = e - s;
5414 if (l > MAXPATHL - 5)
5415 break;
5416 vim_strncpy(buf, s, l);
5417 add_pathsep(buf);
5418 l = STRLEN(buf);
5419 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5420
5421 /* Expand matches in one directory of $PATH. */
5422 ret = expand_wildcards(1, &buf, num_file, file, flags);
5423 if (ret == OK)
5424 {
5425 if (ga_grow(&ga, *num_file) == FAIL)
5426 FreeWild(*num_file, *file);
5427 else
5428 {
5429 for (i = 0; i < *num_file; ++i)
5430 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005431 char_u *name = (*file)[i];
5432
5433 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005434 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005435 // Check if this name was already found.
5436 hash = hash_hash(name + l);
5437 hi = hash_lookup(&found_ht, name + l, hash);
5438 if (HASHITEM_EMPTY(hi))
5439 {
5440 // Remove the path that was prepended.
5441 STRMOVE(name, name + l);
5442 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5443 hash_add_item(&found_ht, hi, name, hash);
5444 name = NULL;
5445 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005446 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005447 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005448 }
5449 vim_free(*file);
5450 }
5451 }
5452 if (*e != NUL)
5453 ++e;
5454 }
5455 *file = ga.ga_data;
5456 *num_file = ga.ga_len;
5457
5458 vim_free(buf);
5459 vim_free(pat);
5460 if (mustfree)
5461 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005462 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005463 return OK;
5464}
5465
5466
Bram Moolenaarac9fb182019-04-27 13:04:13 +02005467# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005469 * Call "user_expand_func()" to invoke a user defined Vim script function and
5470 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005472 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005473call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005474 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005475 expand_T *xp,
5476 int *num_file,
5477 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005479 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005480 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005481 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005482 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005483 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484
Bram Moolenaarb7515462013-06-29 12:58:33 +02005485 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005486 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 *num_file = 0;
5488 *file = NULL;
5489
Bram Moolenaarb7515462013-06-29 12:58:33 +02005490 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005491 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005492 keep = ccline.cmdbuff[ccline.cmdlen];
5493 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005494 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005495
Bram Moolenaarffa96842018-06-12 22:05:14 +02005496 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5497
5498 args[0].v_type = VAR_STRING;
5499 args[0].vval.v_string = pat;
5500 args[1].v_type = VAR_STRING;
5501 args[1].vval.v_string = xp->xp_line;
5502 args[2].v_type = VAR_NUMBER;
5503 args[2].vval.v_number = xp->xp_col;
5504 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005506 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005507
Bram Moolenaarded27a12018-08-01 19:06:03 +02005508 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005509
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005510 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005511 if (ccline.cmdbuff != NULL)
5512 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005513
Bram Moolenaarffa96842018-06-12 22:05:14 +02005514 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005515 return ret;
5516}
5517
5518/*
5519 * Expand names with a function defined by the user.
5520 */
5521 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005522ExpandUserDefined(
5523 expand_T *xp,
5524 regmatch_T *regmatch,
5525 int *num_file,
5526 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005527{
5528 char_u *retstr;
5529 char_u *s;
5530 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005531 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005532 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005533 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005534
5535 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5536 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 return FAIL;
5538
5539 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005540 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 e = vim_strchr(s, '\n');
5543 if (e == NULL)
5544 e = s + STRLEN(s);
5545 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005546 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005548 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5549 *e = keep;
5550
5551 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005553 if (ga_grow(&ga, 1) == FAIL)
5554 break;
5555 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5556 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (*e != NUL)
5560 ++e;
5561 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005562 vim_free(retstr);
5563 *file = ga.ga_data;
5564 *num_file = ga.ga_len;
5565 return OK;
5566}
5567
5568/*
5569 * Expand names with a list returned by a function defined by the user.
5570 */
5571 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005572ExpandUserList(
5573 expand_T *xp,
5574 int *num_file,
5575 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005576{
5577 list_T *retlist;
5578 listitem_T *li;
5579 garray_T ga;
5580
5581 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5582 if (retlist == NULL)
5583 return FAIL;
5584
5585 ga_init2(&ga, (int)sizeof(char *), 3);
5586 /* Loop over the items in the list. */
5587 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5588 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005589 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5590 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005591
5592 if (ga_grow(&ga, 1) == FAIL)
5593 break;
5594
5595 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005596 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005597 ++ga.ga_len;
5598 }
5599 list_unref(retlist);
5600
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 *file = ga.ga_data;
5602 *num_file = ga.ga_len;
5603 return OK;
5604}
5605#endif
5606
5607/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005608 * Expand color scheme, compiler or filetype names.
5609 * Search from 'runtimepath':
5610 * 'runtimepath'/{dirnames}/{pat}.vim
5611 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5612 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5613 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5614 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005615 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 */
5617 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005618ExpandRTDir(
5619 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005620 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005621 int *num_file,
5622 char_u ***file,
5623 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 char_u *s;
5626 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005627 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005629 int i;
5630 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632 *num_file = 0;
5633 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005634 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005635 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005637 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005639 s = alloc(STRLEN(dirnames[i]) + pat_len + 7);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005640 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005642 ga_clear_strings(&ga);
5643 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005645 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005646 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005647 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005649
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005650 if (flags & DIP_START) {
5651 for (i = 0; dirnames[i] != NULL; ++i)
5652 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005653 s = alloc(STRLEN(dirnames[i]) + pat_len + 22);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005654 if (s == NULL)
5655 {
5656 ga_clear_strings(&ga);
5657 return FAIL;
5658 }
5659 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5660 globpath(p_pp, s, &ga, 0);
5661 vim_free(s);
5662 }
5663 }
5664
5665 if (flags & DIP_OPT) {
5666 for (i = 0; dirnames[i] != NULL; ++i)
5667 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005668 s = alloc(STRLEN(dirnames[i]) + pat_len + 20);
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005669 if (s == NULL)
5670 {
5671 ga_clear_strings(&ga);
5672 return FAIL;
5673 }
5674 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5675 globpath(p_pp, s, &ga, 0);
5676 vim_free(s);
5677 }
5678 }
5679
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005680 for (i = 0; i < ga.ga_len; ++i)
5681 {
5682 match = ((char_u **)ga.ga_data)[i];
5683 s = match;
5684 e = s + STRLEN(s);
5685 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5686 {
5687 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005688 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005689 if (s < match || vim_ispathsep(*s))
5690 break;
5691 ++s;
5692 *e = NUL;
5693 mch_memmove(match, s, e - s + 1);
5694 }
5695 }
5696
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005697 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005698 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005699
5700 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005701 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005702 remove_duplicates(&ga);
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 *file = ga.ga_data;
5705 *num_file = ga.ga_len;
5706 return OK;
5707}
5708
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005709/*
5710 * Expand loadplugin names:
5711 * 'packpath'/pack/ * /opt/{pat}
5712 */
5713 static int
5714ExpandPackAddDir(
5715 char_u *pat,
5716 int *num_file,
5717 char_u ***file)
5718{
5719 char_u *s;
5720 char_u *e;
5721 char_u *match;
5722 garray_T ga;
5723 int i;
5724 int pat_len;
5725
5726 *num_file = 0;
5727 *file = NULL;
5728 pat_len = (int)STRLEN(pat);
5729 ga_init2(&ga, (int)sizeof(char *), 10);
5730
Bram Moolenaar964b3742019-05-24 18:54:09 +02005731 s = alloc(pat_len + 26);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005732 if (s == NULL)
5733 {
5734 ga_clear_strings(&ga);
5735 return FAIL;
5736 }
5737 sprintf((char *)s, "pack/*/opt/%s*", pat);
5738 globpath(p_pp, s, &ga, 0);
5739 vim_free(s);
5740
5741 for (i = 0; i < ga.ga_len; ++i)
5742 {
5743 match = ((char_u **)ga.ga_data)[i];
5744 s = gettail(match);
5745 e = s + STRLEN(s);
5746 mch_memmove(match, s, e - s + 1);
5747 }
5748
5749 if (ga.ga_len == 0)
5750 return FAIL;
5751
5752 /* Sort and remove duplicates which can happen when specifying multiple
5753 * directories in dirnames. */
5754 remove_duplicates(&ga);
5755
5756 *file = ga.ga_data;
5757 *num_file = ga.ga_len;
5758 return OK;
5759}
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761#endif
5762
5763#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5764/*
5765 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005766 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005768 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005769globpath(
5770 char_u *path,
5771 char_u *file,
5772 garray_T *ga,
5773 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 expand_T xpc;
5776 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 int num_p;
5779 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 buf = alloc(MAXPATHL);
5782 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005783 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005785 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005787
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 /* Loop over all entries in {path}. */
5789 while (*path != NUL)
5790 {
5791 /* Copy one item of the path to buf[] and concatenate the file name. */
5792 copy_option_part(&path, buf, MAXPATHL, ",");
5793 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5794 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005795# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005796 /* Using the platform's path separator (\) makes vim incorrectly
5797 * treat it as an escape character, use '/' instead. */
5798 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5799 STRCAT(buf, "/");
5800# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005804 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5805 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005807 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005809 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 for (i = 0; i < num_p; ++i)
5812 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005813 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005814 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005815 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005818
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 FreeWild(num_p, p);
5820 }
5821 }
5822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823
5824 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825}
5826
5827#endif
5828
5829#if defined(FEAT_CMDHIST) || defined(PROTO)
5830
5831/*********************************
5832 * Command line history stuff *
5833 *********************************/
5834
5835/*
5836 * Translate a history character to the associated type number.
5837 */
5838 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005839hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 if (c == ':')
5842 return HIST_CMD;
5843 if (c == '=')
5844 return HIST_EXPR;
5845 if (c == '@')
5846 return HIST_INPUT;
5847 if (c == '>')
5848 return HIST_DEBUG;
5849 return HIST_SEARCH; /* must be '?' or '/' */
5850}
5851
5852/*
5853 * Table of history names.
5854 * These names are used in :history and various hist...() functions.
5855 * It is sufficient to give the significant prefix of a history name.
5856 */
5857
5858static char *(history_names[]) =
5859{
5860 "cmd",
5861 "search",
5862 "expr",
5863 "input",
5864 "debug",
5865 NULL
5866};
5867
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005868#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5869/*
5870 * Function given to ExpandGeneric() to obtain the possible first
5871 * arguments of the ":history command.
5872 */
5873 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005874get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005875{
5876 static char_u compl[2] = { NUL, NUL };
5877 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005878 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005879 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5880
5881 if (idx < short_names_count)
5882 {
5883 compl[0] = (char_u)short_names[idx];
5884 return compl;
5885 }
5886 if (idx < short_names_count + history_name_count)
5887 return (char_u *)history_names[idx - short_names_count];
5888 if (idx == short_names_count + history_name_count)
5889 return (char_u *)"all";
5890 return NULL;
5891}
5892#endif
5893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894/*
5895 * init_history() - Initialize the command line history.
5896 * Also used to re-allocate the history when the size changes.
5897 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005899init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 int newlen; /* new length of history table */
5902 histentry_T *temp;
5903 int i;
5904 int j;
5905 int type;
5906
5907 /*
5908 * If size of history table changed, reallocate it
5909 */
5910 newlen = (int)p_hi;
5911 if (newlen != hislen) /* history length changed */
5912 {
5913 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5914 {
5915 if (newlen)
5916 {
5917 temp = (histentry_T *)lalloc(
5918 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5919 if (temp == NULL) /* out of memory! */
5920 {
5921 if (type == 0) /* first one: just keep the old length */
5922 {
5923 newlen = hislen;
5924 break;
5925 }
5926 /* Already changed one table, now we can only have zero
5927 * length for all tables. */
5928 newlen = 0;
5929 type = -1;
5930 continue;
5931 }
5932 }
5933 else
5934 temp = NULL;
5935 if (newlen == 0 || temp != NULL)
5936 {
5937 if (hisidx[type] < 0) /* there are no entries yet */
5938 {
5939 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005940 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 }
5942 else if (newlen > hislen) /* array becomes bigger */
5943 {
5944 for (i = 0; i <= hisidx[type]; ++i)
5945 temp[i] = history[type][i];
5946 j = i;
5947 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005948 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 for ( ; j < hislen; ++i, ++j)
5950 temp[i] = history[type][j];
5951 }
5952 else /* array becomes smaller or 0 */
5953 {
5954 j = hisidx[type];
5955 for (i = newlen - 1; ; --i)
5956 {
5957 if (i >= 0) /* copy newest entries */
5958 temp[i] = history[type][j];
5959 else /* remove older entries */
5960 vim_free(history[type][j].hisstr);
5961 if (--j < 0)
5962 j = hislen - 1;
5963 if (j == hisidx[type])
5964 break;
5965 }
5966 hisidx[type] = newlen - 1;
5967 }
5968 vim_free(history[type]);
5969 history[type] = temp;
5970 }
5971 }
5972 hislen = newlen;
5973 }
5974}
5975
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005976 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005977clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005978{
5979 hisptr->hisnum = 0;
5980 hisptr->viminfo = FALSE;
5981 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005982 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005983}
5984
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985/*
5986 * Check if command line 'str' is already in history.
5987 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
5988 */
5989 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005990in_history(
5991 int type,
5992 char_u *str,
5993 int move_to_front, /* Move the entry to the front if it exists */
5994 int sep,
5995 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996{
5997 int i;
5998 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02005999 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000
6001 if (hisidx[type] < 0)
6002 return FALSE;
6003 i = hisidx[type];
6004 do
6005 {
6006 if (history[type][i].hisstr == NULL)
6007 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006008
6009 /* For search history, check that the separator character matches as
6010 * well. */
6011 p = history[type][i].hisstr;
6012 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006013 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006014 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 {
6016 if (!move_to_front)
6017 return TRUE;
6018 last_i = i;
6019 break;
6020 }
6021 if (--i < 0)
6022 i = hislen - 1;
6023 } while (i != hisidx[type]);
6024
6025 if (last_i >= 0)
6026 {
6027 str = history[type][i].hisstr;
6028 while (i != hisidx[type])
6029 {
6030 if (++i >= hislen)
6031 i = 0;
6032 history[type][last_i] = history[type][i];
6033 last_i = i;
6034 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006036 history[type][i].viminfo = FALSE;
6037 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006038 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 return TRUE;
6040 }
6041 return FALSE;
6042}
6043
6044/*
6045 * Convert history name (from table above) to its HIST_ equivalent.
6046 * When "name" is empty, return "cmd" history.
6047 * Returns -1 for unknown history name.
6048 */
6049 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006050get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051{
6052 int i;
6053 int len = (int)STRLEN(name);
6054
6055 /* No argument: use current history. */
6056 if (len == 0)
6057 return hist_char2type(ccline.cmdfirstc);
6058
6059 for (i = 0; history_names[i] != NULL; ++i)
6060 if (STRNICMP(name, history_names[i], len) == 0)
6061 return i;
6062
6063 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6064 return hist_char2type(name[0]);
6065
6066 return -1;
6067}
6068
6069static int last_maptick = -1; /* last seen maptick */
6070
6071/*
6072 * Add the given string to the given history. If the string is already in the
6073 * history then it is moved to the front. "histype" may be one of he HIST_
6074 * values.
6075 */
6076 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006077add_to_history(
6078 int histype,
6079 char_u *new_entry,
6080 int in_map, /* consider maptick when inside a mapping */
6081 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082{
6083 histentry_T *hisptr;
6084 int len;
6085
6086 if (hislen == 0) /* no history */
6087 return;
6088
Bram Moolenaara939e432013-11-09 05:30:26 +01006089 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6090 return;
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 /*
6093 * Searches inside the same mapping overwrite each other, so that only
6094 * the last line is kept. Be careful not to remove a line that was moved
6095 * down, only lines that were added.
6096 */
6097 if (histype == HIST_SEARCH && in_map)
6098 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006099 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 {
6101 /* Current line is from the same mapping, remove it */
6102 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6103 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006104 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 --hisnum[histype];
6106 if (--hisidx[HIST_SEARCH] < 0)
6107 hisidx[HIST_SEARCH] = hislen - 1;
6108 }
6109 last_maptick = -1;
6110 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006111 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 if (++hisidx[histype] == hislen)
6114 hisidx[histype] = 0;
6115 hisptr = &history[histype][hisidx[histype]];
6116 vim_free(hisptr->hisstr);
6117
6118 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006119 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6121 if (hisptr->hisstr != NULL)
6122 hisptr->hisstr[len + 1] = sep;
6123
6124 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006125 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006126 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006127 if (histype == HIST_SEARCH && in_map)
6128 last_maptick = maptick;
6129 }
6130}
6131
6132#if defined(FEAT_EVAL) || defined(PROTO)
6133
6134/*
6135 * Get identifier of newest history entry.
6136 * "histype" may be one of the HIST_ values.
6137 */
6138 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006139get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140{
6141 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6142 || hisidx[histype] < 0)
6143 return -1;
6144
6145 return history[histype][hisidx[histype]].hisnum;
6146}
6147
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 * Calculate history index from a number:
6150 * num > 0: seen as identifying number of a history entry
6151 * num < 0: relative position in history wrt newest entry
6152 * "histype" may be one of the HIST_ values.
6153 */
6154 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 int i;
6158 histentry_T *hist;
6159 int wrapped = FALSE;
6160
6161 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6162 || (i = hisidx[histype]) < 0 || num == 0)
6163 return -1;
6164
6165 hist = history[histype];
6166 if (num > 0)
6167 {
6168 while (hist[i].hisnum > num)
6169 if (--i < 0)
6170 {
6171 if (wrapped)
6172 break;
6173 i += hislen;
6174 wrapped = TRUE;
6175 }
6176 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6177 return i;
6178 }
6179 else if (-num <= hislen)
6180 {
6181 i += num + 1;
6182 if (i < 0)
6183 i += hislen;
6184 if (hist[i].hisstr != NULL)
6185 return i;
6186 }
6187 return -1;
6188}
6189
6190/*
6191 * Get a history entry by its index.
6192 * "histype" may be one of the HIST_ values.
6193 */
6194 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006195get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196{
6197 idx = calc_hist_idx(histype, idx);
6198 if (idx >= 0)
6199 return history[histype][idx].hisstr;
6200 else
6201 return (char_u *)"";
6202}
6203
6204/*
6205 * Clear all entries of a history.
6206 * "histype" may be one of the HIST_ values.
6207 */
6208 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006209clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211 int i;
6212 histentry_T *hisptr;
6213
6214 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6215 {
6216 hisptr = history[histype];
6217 for (i = hislen; i--;)
6218 {
6219 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006220 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006221 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 }
6223 hisidx[histype] = -1; /* mark history as cleared */
6224 hisnum[histype] = 0; /* reset identifier counter */
6225 return OK;
6226 }
6227 return FAIL;
6228}
6229
6230/*
6231 * Remove all entries matching {str} from a history.
6232 * "histype" may be one of the HIST_ values.
6233 */
6234 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006235del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 regmatch_T regmatch;
6238 histentry_T *hisptr;
6239 int idx;
6240 int i;
6241 int last;
6242 int found = FALSE;
6243
6244 regmatch.regprog = NULL;
6245 regmatch.rm_ic = FALSE; /* always match case */
6246 if (hislen != 0
6247 && histype >= 0
6248 && histype < HIST_COUNT
6249 && *str != NUL
6250 && (idx = hisidx[histype]) >= 0
6251 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6252 != NULL)
6253 {
6254 i = last = idx;
6255 do
6256 {
6257 hisptr = &history[histype][i];
6258 if (hisptr->hisstr == NULL)
6259 break;
6260 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6261 {
6262 found = TRUE;
6263 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006264 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006265 }
6266 else
6267 {
6268 if (i != last)
6269 {
6270 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006271 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 }
6273 if (--last < 0)
6274 last += hislen;
6275 }
6276 if (--i < 0)
6277 i += hislen;
6278 } while (i != idx);
6279 if (history[histype][idx].hisstr == NULL)
6280 hisidx[histype] = -1;
6281 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006282 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283 return found;
6284}
6285
6286/*
6287 * Remove an indexed entry from a history.
6288 * "histype" may be one of the HIST_ values.
6289 */
6290 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006291del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006292{
6293 int i, j;
6294
6295 i = calc_hist_idx(histype, idx);
6296 if (i < 0)
6297 return FALSE;
6298 idx = hisidx[histype];
6299 vim_free(history[histype][i].hisstr);
6300
6301 /* When deleting the last added search string in a mapping, reset
6302 * last_maptick, so that the last added search string isn't deleted again.
6303 */
6304 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6305 last_maptick = -1;
6306
6307 while (i != idx)
6308 {
6309 j = (i + 1) % hislen;
6310 history[histype][i] = history[histype][j];
6311 i = j;
6312 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006313 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 if (--i < 0)
6315 i += hislen;
6316 hisidx[histype] = i;
6317 return TRUE;
6318}
6319
6320#endif /* FEAT_EVAL */
6321
6322#if defined(FEAT_CRYPT) || defined(PROTO)
6323/*
6324 * Very specific function to remove the value in ":set key=val" from the
6325 * history.
6326 */
6327 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006328remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329{
6330 char_u *p;
6331 int i;
6332
6333 i = hisidx[HIST_CMD];
6334 if (i < 0)
6335 return;
6336 p = history[HIST_CMD][i].hisstr;
6337 if (p != NULL)
6338 for ( ; *p; ++p)
6339 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6340 {
6341 p = vim_strchr(p + 3, '=');
6342 if (p == NULL)
6343 break;
6344 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006345 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346 if (p[i] == '\\' && p[i + 1])
6347 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006348 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 --p;
6350 }
6351}
6352#endif
6353
6354#endif /* FEAT_CMDHIST */
6355
Bram Moolenaar064154c2016-03-19 22:50:43 +01006356#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006357/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006358 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006359 * ccline and put the previous value in prev_ccline.
6360 */
6361 static struct cmdline_info *
6362get_ccline_ptr(void)
6363{
6364 if ((State & CMDLINE) == 0)
6365 return NULL;
6366 if (ccline.cmdbuff != NULL)
6367 return &ccline;
6368 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6369 return &prev_ccline;
6370 return NULL;
6371}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006372#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006373
Bram Moolenaar064154c2016-03-19 22:50:43 +01006374#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006375/*
6376 * Get the current command line in allocated memory.
6377 * Only works when the command line is being edited.
6378 * Returns NULL when something is wrong.
6379 */
6380 char_u *
6381get_cmdline_str(void)
6382{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006383 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006384
Bram Moolenaaree91c332018-09-25 22:27:35 +02006385 if (cmdline_star > 0)
6386 return NULL;
6387 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006388 if (p == NULL)
6389 return NULL;
6390 return vim_strnsave(p->cmdbuff, p->cmdlen);
6391}
6392
6393/*
6394 * Get the current command line position, counted in bytes.
6395 * Zero is the first position.
6396 * Only works when the command line is being edited.
6397 * Returns -1 when something is wrong.
6398 */
6399 int
6400get_cmdline_pos(void)
6401{
6402 struct cmdline_info *p = get_ccline_ptr();
6403
6404 if (p == NULL)
6405 return -1;
6406 return p->cmdpos;
6407}
6408
6409/*
6410 * Set the command line byte position to "pos". Zero is the first position.
6411 * Only works when the command line is being edited.
6412 * Returns 1 when failed, 0 when OK.
6413 */
6414 int
6415set_cmdline_pos(
6416 int pos)
6417{
6418 struct cmdline_info *p = get_ccline_ptr();
6419
6420 if (p == NULL)
6421 return 1;
6422
6423 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6424 * changed the command line. */
6425 if (pos < 0)
6426 new_cmdpos = 0;
6427 else
6428 new_cmdpos = pos;
6429 return 0;
6430}
6431#endif
6432
6433#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6434/*
6435 * Get the current command-line type.
6436 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6437 * Only works when the command line is being edited.
6438 * Returns NUL when something is wrong.
6439 */
6440 int
6441get_cmdline_type(void)
6442{
6443 struct cmdline_info *p = get_ccline_ptr();
6444
6445 if (p == NULL)
6446 return NUL;
6447 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006448 return
6449# ifdef FEAT_EVAL
6450 (p->input_fn) ? '@' :
6451# endif
6452 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006453 return p->cmdfirstc;
6454}
6455#endif
6456
Bram Moolenaar071d4272004-06-13 20:20:40 +00006457#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6458/*
6459 * Get indices "num1,num2" that specify a range within a list (not a range of
6460 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6461 * Returns OK if parsed successfully, otherwise FAIL.
6462 */
6463 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006464get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465{
6466 int len;
6467 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006468 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469
6470 *str = skipwhite(*str);
6471 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6472 {
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006473 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 *str += len;
6475 *num1 = (int)num;
6476 first = TRUE;
6477 }
6478 *str = skipwhite(*str);
6479 if (**str == ',') /* parse "to" part of range */
6480 {
6481 *str = skipwhite(*str + 1);
Bram Moolenaar16e9b852019-05-19 19:59:35 +02006482 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 if (len > 0)
6484 {
6485 *num2 = (int)num;
6486 *str = skipwhite(*str + len);
6487 }
6488 else if (!first) /* no number given at all */
6489 return FAIL;
6490 }
6491 else if (first) /* only one number given */
6492 *num2 = *num1;
6493 return OK;
6494}
6495#endif
6496
6497#if defined(FEAT_CMDHIST) || defined(PROTO)
6498/*
6499 * :history command - print a history
6500 */
6501 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006502ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503{
6504 histentry_T *hist;
6505 int histype1 = HIST_CMD;
6506 int histype2 = HIST_CMD;
6507 int hisidx1 = 1;
6508 int hisidx2 = -1;
6509 int idx;
6510 int i, j, k;
6511 char_u *end;
6512 char_u *arg = eap->arg;
6513
6514 if (hislen == 0)
6515 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006516 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 return;
6518 }
6519
6520 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6521 {
6522 end = arg;
6523 while (ASCII_ISALPHA(*end)
6524 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6525 end++;
6526 i = *end;
6527 *end = NUL;
6528 histype1 = get_histtype(arg);
6529 if (histype1 == -1)
6530 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006531 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
6533 histype1 = 0;
6534 histype2 = HIST_COUNT-1;
6535 }
6536 else
6537 {
6538 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006539 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 return;
6541 }
6542 }
6543 else
6544 histype2 = histype1;
6545 *end = i;
6546 }
6547 else
6548 end = arg;
6549 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6550 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006551 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 return;
6553 }
6554
6555 for (; !got_int && histype1 <= histype2; ++histype1)
6556 {
6557 STRCPY(IObuff, "\n # ");
6558 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006559 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 idx = hisidx[histype1];
6561 hist = history[histype1];
6562 j = hisidx1;
6563 k = hisidx2;
6564 if (j < 0)
6565 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6566 if (k < 0)
6567 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6568 if (idx >= 0 && j <= k)
6569 for (i = idx + 1; !got_int; ++i)
6570 {
6571 if (i == hislen)
6572 i = 0;
6573 if (hist[i].hisstr != NULL
6574 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6575 {
6576 msg_putchar('\n');
6577 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6578 hist[i].hisnum);
6579 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6580 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006581 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 else
6583 STRCAT(IObuff, hist[i].hisstr);
6584 msg_outtrans(IObuff);
6585 out_flush();
6586 }
6587 if (i == idx)
6588 break;
6589 }
6590 }
6591}
6592#endif
6593
6594#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006595/*
6596 * Buffers for history read from a viminfo file. Only valid while reading.
6597 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006598static histentry_T *viminfo_history[HIST_COUNT] =
6599 {NULL, NULL, NULL, NULL, NULL};
6600static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6601static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602static int viminfo_add_at_front = FALSE;
6603
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604/*
6605 * Translate a history type number to the associated character.
6606 */
6607 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006608hist_type2char(
6609 int type,
6610 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611{
6612 if (type == HIST_CMD)
6613 return ':';
6614 if (type == HIST_SEARCH)
6615 {
6616 if (use_question)
6617 return '?';
6618 else
6619 return '/';
6620 }
6621 if (type == HIST_EXPR)
6622 return '=';
6623 return '@';
6624}
6625
6626/*
6627 * Prepare for reading the history from the viminfo file.
6628 * This allocates history arrays to store the read history lines.
6629 */
6630 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006631prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632{
6633 int i;
6634 int num;
6635 int type;
6636 int len;
6637
6638 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006639 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 if (asklen > hislen)
6641 asklen = hislen;
6642
6643 for (type = 0; type < HIST_COUNT; ++type)
6644 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006645 /* Count the number of empty spaces in the history list. Entries read
6646 * from viminfo previously are also considered empty. If there are
6647 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006649 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 num++;
6651 len = asklen;
6652 if (num > len)
6653 len = num;
6654 if (len <= 0)
6655 viminfo_history[type] = NULL;
6656 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006657 viminfo_history[type] = (histentry_T *)lalloc(
6658 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 if (viminfo_history[type] == NULL)
6660 len = 0;
6661 viminfo_hislen[type] = len;
6662 viminfo_hisidx[type] = 0;
6663 }
6664}
6665
6666/*
6667 * Accept a line from the viminfo, store it in the history array when it's
6668 * new.
6669 */
6670 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006671read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672{
6673 int type;
6674 long_u len;
6675 char_u *val;
6676 char_u *p;
6677
6678 type = hist_char2type(virp->vir_line[0]);
6679 if (viminfo_hisidx[type] < viminfo_hislen[type])
6680 {
6681 val = viminfo_readstring(virp, 1, TRUE);
6682 if (val != NULL && *val != NUL)
6683 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006684 int sep = (*val == ' ' ? NUL : *val);
6685
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006687 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 {
6689 /* Need to re-allocate to append the separator byte. */
6690 len = STRLEN(val);
6691 p = lalloc(len + 2, TRUE);
6692 if (p != NULL)
6693 {
6694 if (type == HIST_SEARCH)
6695 {
6696 /* Search entry: Move the separator from the first
6697 * column to after the NUL. */
6698 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006699 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 }
6701 else
6702 {
6703 /* Not a search entry: No separator in the viminfo
6704 * file, add a NUL separator. */
6705 mch_memmove(p, val, (size_t)len + 1);
6706 p[len + 1] = NUL;
6707 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006708 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6709 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006710 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6711 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006712 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 }
6714 }
6715 }
6716 vim_free(val);
6717 }
6718 return viminfo_readline(virp);
6719}
6720
Bram Moolenaar07219f92013-04-14 23:19:36 +02006721/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006722 * Accept a new style history line from the viminfo, store it in the history
6723 * array when it's new.
6724 */
6725 void
6726handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006727 garray_T *values,
6728 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006729{
6730 int type;
6731 long_u len;
6732 char_u *val;
6733 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006734 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006735
6736 /* Check the format:
6737 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006738 if (values->ga_len < 4
6739 || vp[0].bv_type != BVAL_NR
6740 || vp[1].bv_type != BVAL_NR
6741 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6742 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006743 return;
6744
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006745 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006746 if (type >= HIST_COUNT)
6747 return;
6748 if (viminfo_hisidx[type] < viminfo_hislen[type])
6749 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006750 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006751 if (val != NULL && *val != NUL)
6752 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006753 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6754 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006755 int idx;
6756 int overwrite = FALSE;
6757
6758 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6759 {
6760 /* If lines were written by an older Vim we need to avoid
6761 * getting duplicates. See if the entry already exists. */
6762 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6763 {
6764 p = viminfo_history[type][idx].hisstr;
6765 if (STRCMP(val, p) == 0
6766 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6767 {
6768 overwrite = TRUE;
6769 break;
6770 }
6771 }
6772
6773 if (!overwrite)
6774 {
6775 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006776 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 p = lalloc(len + 2, TRUE);
6778 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006779 else
6780 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006781 if (p != NULL)
6782 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006783 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006784 if (!overwrite)
6785 {
6786 mch_memmove(p, val, (size_t)len + 1);
6787 /* Put the separator after the NUL. */
6788 p[len + 1] = sep;
6789 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006790 viminfo_history[type][idx].hisnum = 0;
6791 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006792 viminfo_hisidx[type]++;
6793 }
6794 }
6795 }
6796 }
6797 }
6798}
6799
6800/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006801 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006802 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006803 static void
6804concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805{
6806 int idx;
6807 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006808
6809 idx = hisidx[type] + viminfo_hisidx[type];
6810 if (idx >= hislen)
6811 idx -= hislen;
6812 else if (idx < 0)
6813 idx = hislen - 1;
6814 if (viminfo_add_at_front)
6815 hisidx[type] = idx;
6816 else
6817 {
6818 if (hisidx[type] == -1)
6819 hisidx[type] = hislen - 1;
6820 do
6821 {
6822 if (history[type][idx].hisstr != NULL
6823 || history[type][idx].viminfo)
6824 break;
6825 if (++idx == hislen)
6826 idx = 0;
6827 } while (idx != hisidx[type]);
6828 if (idx != hisidx[type] && --idx < 0)
6829 idx = hislen - 1;
6830 }
6831 for (i = 0; i < viminfo_hisidx[type]; i++)
6832 {
6833 vim_free(history[type][idx].hisstr);
6834 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6835 history[type][idx].viminfo = TRUE;
6836 history[type][idx].time_set = viminfo_history[type][i].time_set;
6837 if (--idx < 0)
6838 idx = hislen - 1;
6839 }
6840 idx += 1;
6841 idx %= hislen;
6842 for (i = 0; i < viminfo_hisidx[type]; i++)
6843 {
6844 history[type][idx++].hisnum = ++hisnum[type];
6845 idx %= hislen;
6846 }
6847}
6848
6849#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6850 static int
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006851sort_hist(const void *s1, const void *s2)
6852{
6853 histentry_T *p1 = *(histentry_T **)s1;
6854 histentry_T *p2 = *(histentry_T **)s2;
6855
6856 if (p1->time_set < p2->time_set) return -1;
6857 if (p1->time_set > p2->time_set) return 1;
6858 return 0;
6859}
6860#endif
6861
6862/*
6863 * Merge history lines from viminfo and lines typed in this Vim based on the
6864 * timestamp;
6865 */
6866 static void
6867merge_history(int type)
6868{
6869 int max_len;
6870 histentry_T **tot_hist;
6871 histentry_T *new_hist;
6872 int i;
6873 int len;
6874
6875 /* Make one long list with all entries. */
6876 max_len = hislen + viminfo_hisidx[type];
6877 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6878 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6879 if (tot_hist == NULL || new_hist == NULL)
6880 {
6881 vim_free(tot_hist);
6882 vim_free(new_hist);
6883 return;
6884 }
6885 for (i = 0; i < viminfo_hisidx[type]; i++)
6886 tot_hist[i] = &viminfo_history[type][i];
6887 len = i;
6888 for (i = 0; i < hislen; i++)
6889 if (history[type][i].hisstr != NULL)
6890 tot_hist[len++] = &history[type][i];
6891
6892 /* Sort the list on timestamp. */
6893 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6894
6895 /* Keep the newest ones. */
6896 for (i = 0; i < hislen; i++)
6897 {
6898 if (i < len)
6899 {
6900 new_hist[i] = *tot_hist[i];
6901 tot_hist[i]->hisstr = NULL;
6902 if (new_hist[i].hisnum == 0)
6903 new_hist[i].hisnum = ++hisnum[type];
6904 }
6905 else
6906 clear_hist_entry(&new_hist[i]);
6907 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006908 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006909
6910 /* Free what is not kept. */
6911 for (i = 0; i < viminfo_hisidx[type]; i++)
6912 vim_free(viminfo_history[type][i].hisstr);
6913 for (i = 0; i < hislen; i++)
6914 vim_free(history[type][i].hisstr);
6915 vim_free(history[type]);
6916 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006917 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006918}
6919
6920/*
6921 * Finish reading history lines from viminfo. Not used when writing viminfo.
6922 */
6923 void
6924finish_viminfo_history(vir_T *virp)
6925{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006927 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
6929 for (type = 0; type < HIST_COUNT; ++type)
6930 {
6931 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006932 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006933
6934 if (merge)
6935 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006937 concat_history(type);
6938
Bram Moolenaard23a8232018-02-10 18:45:26 +01006939 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006940 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 }
6942}
6943
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006944/*
6945 * Write history to viminfo file in "fp".
6946 * When "merge" is TRUE merge history lines with a previously read viminfo
6947 * file, data is in viminfo_history[].
6948 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6949 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006951write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952{
6953 int i;
6954 int type;
6955 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006956 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
6958 init_history();
6959 if (hislen == 0)
6960 return;
6961 for (type = 0; type < HIST_COUNT; ++type)
6962 {
6963 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6964 if (num_saved == 0)
6965 continue;
6966 if (num_saved < 0) /* Use default */
6967 num_saved = hislen;
6968 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6969 type == HIST_CMD ? _("Command Line") :
6970 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006971 type == HIST_EXPR ? _("Expression") :
6972 type == HIST_INPUT ? _("Input Line") :
6973 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974 if (num_saved > hislen)
6975 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006976
6977 /*
6978 * Merge typed and viminfo history:
6979 * round 1: history of typed commands.
6980 * round 2: history from recently read viminfo.
6981 */
6982 for (round = 1; round <= 2; ++round)
6983 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02006984 if (round == 1)
6985 /* start at newest entry, somewhere in the list */
6986 i = hisidx[type];
6987 else if (viminfo_hisidx[type] > 0)
6988 /* start at newest entry, first in the list */
6989 i = 0;
6990 else
6991 /* empty list */
6992 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006993 if (i >= 0)
6994 while (num_saved > 0
6995 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006997 char_u *p;
6998 time_t timestamp;
6999 int c = NUL;
7000
7001 if (round == 1)
7002 {
7003 p = history[type][i].hisstr;
7004 timestamp = history[type][i].time_set;
7005 }
7006 else
7007 {
7008 p = viminfo_history[type] == NULL ? NULL
7009 : viminfo_history[type][i].hisstr;
7010 timestamp = viminfo_history[type] == NULL ? 0
7011 : viminfo_history[type][i].time_set;
7012 }
7013
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007014 if (p != NULL && (round == 2
7015 || !merge
7016 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007018 --num_saved;
7019 fputc(hist_type2char(type, TRUE), fp);
7020 /* For the search history: put the separator in the
7021 * second column; use a space if there isn't one. */
7022 if (type == HIST_SEARCH)
7023 {
7024 c = p[STRLEN(p) + 1];
7025 putc(c == NUL ? ' ' : c, fp);
7026 }
7027 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007028
7029 {
7030 char cbuf[NUMBUFLEN];
7031
7032 /* New style history with a bar line. Format:
7033 * |{bartype},{histtype},{timestamp},{separator},"text" */
7034 if (c == NUL)
7035 cbuf[0] = NUL;
7036 else
7037 sprintf(cbuf, "%d", c);
7038 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7039 type, (long)timestamp, cbuf);
7040 barline_writestring(fp, p, LSIZE - 20);
7041 putc('\n', fp);
7042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007044 if (round == 1)
7045 {
7046 /* Decrement index, loop around and stop when back at
7047 * the start. */
7048 if (--i < 0)
7049 i = hislen - 1;
7050 if (i == hisidx[type])
7051 break;
7052 }
7053 else
7054 {
7055 /* Increment index. Stop at the end in the while. */
7056 ++i;
7057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007059 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007060 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007061 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007062 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007063 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007064 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 }
7066}
7067#endif /* FEAT_VIMINFO */
7068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069#if defined(FEAT_CMDWIN) || defined(PROTO)
7070/*
7071 * Open a window on the current command line and history. Allow editing in
7072 * the window. Returns when the window is closed.
7073 * Returns:
7074 * CR if the command is to be executed
7075 * Ctrl_C if it is to be abandoned
7076 * K_IGNORE if editing continues
7077 */
7078 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007079open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007081 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007083 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 win_T *wp;
7085 int i;
7086 linenr_T lnum;
7087 int histtype;
7088 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 int save_restart_edit = restart_edit;
7090 int save_State = State;
7091 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007092#ifdef FEAT_RIGHTLEFT
7093 int save_cmdmsg_rl = cmdmsg_rl;
7094#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007095#ifdef FEAT_FOLDING
7096 int save_KeyTyped;
7097#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007098
7099 /* Can't do this recursively. Can't do it when typing a password. */
7100 if (cmdwin_type != 0
7101# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7102 || cmdline_star > 0
7103# endif
7104 )
7105 {
7106 beep_flush();
7107 return K_IGNORE;
7108 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007109 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110
7111 /* Save current window sizes. */
7112 win_size_save(&winsizes);
7113
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007115 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007116
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007117#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007118 // When using completion in Insert mode with <C-R>=<C-F> one can open the
7119 // command line window, but we don't want the popup menu then.
7120 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007121#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007122
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007123 /* don't use a new tab page */
7124 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007125 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007126
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 /* Create a window for the command-line buffer. */
7128 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7129 {
7130 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007131 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132 return K_IGNORE;
7133 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007134 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135
7136 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007137 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007138 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007139 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007141#ifdef FEAT_FOLDING
7142 curwin->w_p_fen = FALSE;
7143#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007145 curwin->w_p_rl = cmdmsg_rl;
7146 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007148 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007151 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007152 /* But don't allow switching to another buffer. */
7153 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
Bram Moolenaar46152342005-09-07 21:18:43 +00007155 /* Showing the prompt may have set need_wait_return, reset it. */
7156 need_wait_return = FALSE;
7157
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007158 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7160 {
7161 if (p_wc == TAB)
7162 {
7163 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7164 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7165 }
7166 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7167 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007168 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007170 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7171 * sets 'textwidth' to 78). */
7172 curbuf->b_p_tw = 0;
7173
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174 /* Fill the buffer with the history. */
7175 init_history();
7176 if (hislen > 0)
7177 {
7178 i = hisidx[histtype];
7179 if (i >= 0)
7180 {
7181 lnum = 0;
7182 do
7183 {
7184 if (++i == hislen)
7185 i = 0;
7186 if (history[histtype][i].hisstr != NULL)
7187 ml_append(lnum++, history[histtype][i].hisstr,
7188 (colnr_T)0, FALSE);
7189 }
7190 while (i != hisidx[histtype]);
7191 }
7192 }
7193
7194 /* Replace the empty last line with the current command-line and put the
7195 * cursor there. */
7196 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7197 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7198 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007199 changed_line_abv_curs();
7200 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007201 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 /* No Ex mode here! */
7204 exmode_active = 0;
7205
7206 State = NORMAL;
7207# ifdef FEAT_MOUSE
7208 setmouse();
7209# endif
7210
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007212 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007213 if (restart_edit != 0) /* autocmd with ":startinsert" */
7214 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007215
7216 i = RedrawingDisabled;
7217 RedrawingDisabled = 0;
7218
7219 /*
7220 * Call the main loop until <CR> or CTRL-C is typed.
7221 */
7222 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007223 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
7225 RedrawingDisabled = i;
7226
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007227# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007228 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007229# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007230
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007232 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007233
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007234# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007235 /* Restore KeyTyped in case it is modified by autocommands */
7236 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237# endif
7238
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 exmode_active = save_exmode;
7241
Bram Moolenaarf9821062008-06-20 16:31:07 +00007242 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007244 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 {
7246 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007247 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248 }
7249 else
7250 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007251# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 /* autocmds may abort script processing */
7253 if (aborting() && cmdwin_result != K_IGNORE)
7254 cmdwin_result = Ctrl_C;
7255# endif
7256 /* Set the new command line from the cmdline buffer. */
7257 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007258 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007260 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7261
7262 if (histtype == HIST_CMD)
7263 {
7264 /* Execute the command directly. */
7265 ccline.cmdbuff = vim_strsave((char_u *)p);
7266 cmdwin_result = CAR;
7267 }
7268 else
7269 {
7270 /* First need to cancel what we were doing. */
7271 ccline.cmdbuff = NULL;
7272 stuffcharReadbuff(':');
7273 stuffReadbuff((char_u *)p);
7274 stuffcharReadbuff(CAR);
7275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007276 }
7277 else if (cmdwin_result == K_XF2) /* :qa typed */
7278 {
7279 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7280 cmdwin_result = CAR;
7281 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007282 else if (cmdwin_result == Ctrl_C)
7283 {
7284 /* :q or :close, don't execute any command
7285 * and don't modify the cmd window. */
7286 ccline.cmdbuff = NULL;
7287 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 else
7289 ccline.cmdbuff = vim_strsave(ml_get_curline());
7290 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007291 {
7292 ccline.cmdbuff = vim_strsave((char_u *)"");
7293 ccline.cmdlen = 0;
7294 ccline.cmdbufflen = 1;
7295 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 else
7299 {
7300 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7301 ccline.cmdbufflen = ccline.cmdlen + 1;
7302 ccline.cmdpos = curwin->w_cursor.col;
7303 if (ccline.cmdpos > ccline.cmdlen)
7304 ccline.cmdpos = ccline.cmdlen;
7305 if (cmdwin_result == K_IGNORE)
7306 {
7307 set_cmdspos_cursor();
7308 redrawcmd();
7309 }
7310 }
7311
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007313 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007314# ifdef FEAT_CONCEAL
7315 /* Avoid command-line window first character being concealed. */
7316 curwin->w_p_cole = 0;
7317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007319 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 win_goto(old_curwin);
7321 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007322
7323 /* win_close() may have already wiped the buffer when 'bh' is
7324 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007325 if (bufref_valid(&bufref))
7326 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327
7328 /* Restore window sizes. */
7329 win_size_restore(&winsizes);
7330
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007331 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 }
7333
7334 ga_clear(&winsizes);
7335 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007336# ifdef FEAT_RIGHTLEFT
7337 cmdmsg_rl = save_cmdmsg_rl;
7338# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339
7340 State = save_State;
7341# ifdef FEAT_MOUSE
7342 setmouse();
7343# endif
7344
7345 return cmdwin_result;
7346}
7347#endif /* FEAT_CMDWIN */
7348
7349/*
7350 * Used for commands that either take a simple command string argument, or:
7351 * cmd << endmarker
7352 * {script}
7353 * endmarker
7354 * Returns a pointer to allocated memory with {script} or NULL.
7355 */
7356 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007357script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358{
7359 char_u *theline;
7360 char *end_pattern = NULL;
7361 char dot[] = ".";
7362 garray_T ga;
7363
7364 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7365 return NULL;
7366
7367 ga_init2(&ga, 1, 0x400);
7368
7369 if (cmd[2] != NUL)
7370 end_pattern = (char *)skipwhite(cmd + 2);
7371 else
7372 end_pattern = dot;
7373
7374 for (;;)
7375 {
7376 theline = eap->getline(
7377#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007378 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379#endif
7380 NUL, eap->cookie, 0);
7381
7382 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007383 {
7384 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
7388 ga_concat(&ga, theline);
7389 ga_append(&ga, '\n');
7390 vim_free(theline);
7391 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007392 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393
7394 return (char_u *)ga.ga_data;
7395}