blob: 3d38d2c7423e4ece224e6a084612720728fe8efd [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * ex_getln.c: Functions for entering and editing an Ex command line.
12 */
13
14#include "vim.h"
15
Bram Moolenaar37b15562018-08-14 22:08:25 +020016#ifndef MAX
17# define MAX(x,y) ((x) > (y) ? (x) : (y))
18#endif
19
Bram Moolenaar071d4272004-06-13 20:20:40 +000020/*
21 * Variables shared between getcmdline(), redrawcmdline() and others.
22 * These need to be saved when using CTRL-R |, that's why they are in a
23 * structure.
24 */
25struct cmdline_info
26{
27 char_u *cmdbuff; /* pointer to command line buffer */
28 int cmdbufflen; /* length of cmdbuff */
29 int cmdlen; /* number of chars in command line */
30 int cmdpos; /* current cursor position */
31 int cmdspos; /* cursor column on screen */
Bram Moolenaar5ae636b2012-04-30 18:48:53 +020032 int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +000033 int cmdindent; /* number of spaces before cmdline */
34 char_u *cmdprompt; /* message in front of cmdline */
35 int cmdattr; /* attributes for prompt */
36 int overstrike; /* Typing mode on the command line. Shared by
37 getcmdline() and put_on_cmdline(). */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000038 expand_T *xpc; /* struct being used for expansion, xp_pattern
39 may point into cmdbuff */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000040 int xp_context; /* type of expansion */
41# ifdef FEAT_EVAL
42 char_u *xp_arg; /* user-defined expansion arg */
Bram Moolenaar93db9752006-11-21 10:29:45 +000043 int input_fn; /* when TRUE Invoked for input() function */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000045};
46
Bram Moolenaar438d1762018-09-30 17:11:48 +020047// The current cmdline_info. It is initialized in getcmdline() and after that
48// used by other functions. When invoking getcmdline() recursively it needs
49// to be saved with save_cmdline() and restored with restore_cmdline().
Bram Moolenaard6e7cc62008-09-14 12:42:29 +000050static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
Bram Moolenaar438d1762018-09-30 17:11:48 +020052static int cmd_showtail; /* Only show path tail in lists ? */
Bram Moolenaar071d4272004-06-13 20:20:40 +000053
54#ifdef FEAT_EVAL
55static int new_cmdpos; /* position set by set_cmdline_pos() */
56#endif
57
Bram Moolenaara92522f2017-07-15 15:21:38 +020058static int extra_char = NUL; /* extra character to display when redrawing
59 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020060static int extra_char_shift;
61
Bram Moolenaar071d4272004-06-13 20:20:40 +000062#ifdef FEAT_CMDHIST
63typedef struct hist_entry
64{
65 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020066 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000067 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020068 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000069} histentry_T;
70
71static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
72static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
73static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
74 /* identifying (unique) number of newest history entry */
75static int hislen = 0; /* actual length of history tables */
76
Bram Moolenaard25c16e2016-01-29 22:13:30 +010077static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#endif
79
80#ifdef FEAT_RIGHTLEFT
81static int cmd_hkmap = 0; /* Hebrew mapping during command line */
82#endif
83
Bram Moolenaar438d1762018-09-30 17:11:48 +020084static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010085static int cmdline_charsize(int idx);
86static void set_cmdspos(void);
87static void set_cmdspos_cursor(void);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010088static void correct_cmdspos(int idx, int cells);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static void alloc_cmdbuff(int len);
90static int realloc_cmdbuff(int len);
91static void draw_cmdline(int start, int len);
92static void save_cmdline(struct cmdline_info *ccp);
93static void restore_cmdline(struct cmdline_info *ccp);
94static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000095#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +010096static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void redrawcmdprompt(void);
99static void cursorcmd(void);
100static int ccheck_abbr(int);
101static int nextwild(expand_T *xp, int type, int options, int escape);
102static void escape_fname(char_u **pp);
103static int showmatches(expand_T *xp, int wildmenu);
104static void set_expand_context(expand_T *xp);
105static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
106static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100108static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100109static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100110static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200111# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200113# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100115static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
116static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117# endif
118#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200119#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100120static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122
123#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200124static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000125#endif
126
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200127#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
128static int
129#ifdef __BORLANDC__
130_RTLENTRYF
131#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100132sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#endif
134
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200135
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200136 static void
137trigger_cmd_autocmd(int typechar, int evt)
138{
139 char_u typestr[2];
140
141 typestr[0] = typechar;
142 typestr[1] = NUL;
143 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
144}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200145
Bram Moolenaar071d4272004-06-13 20:20:40 +0000146/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200147 * Abandon the command line.
148 */
149 static void
150abandon_cmdline(void)
151{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100152 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200153 if (msg_scrolled == 0)
154 compute_cmdrow();
Bram Moolenaar32526b32019-01-19 17:43:09 +0100155 msg("");
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200156 redraw_cmdline = TRUE;
157}
158
Bram Moolenaaree219b02017-12-17 14:55:01 +0100159#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200160/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100161 * Guess that the pattern matches everything. Only finds specific cases, such
162 * as a trailing \|, which can happen while typing a pattern.
163 */
164 static int
165empty_pattern(char_u *p)
166{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100167 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100168
169 /* remove trailing \v and the like */
170 while (n >= 2 && p[n - 2] == '\\'
171 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
172 n -= 2;
173 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
174}
175
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200176// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200177typedef struct {
178 colnr_T vs_curswant;
179 colnr_T vs_leftcol;
180 linenr_T vs_topline;
181# ifdef FEAT_DIFF
182 int vs_topfill;
183# endif
184 linenr_T vs_botline;
185 linenr_T vs_empty_rows;
186} viewstate_T;
187
188 static void
189save_viewstate(viewstate_T *vs)
190{
191 vs->vs_curswant = curwin->w_curswant;
192 vs->vs_leftcol = curwin->w_leftcol;
193 vs->vs_topline = curwin->w_topline;
194# ifdef FEAT_DIFF
195 vs->vs_topfill = curwin->w_topfill;
196# endif
197 vs->vs_botline = curwin->w_botline;
198 vs->vs_empty_rows = curwin->w_empty_rows;
199}
200
201 static void
202restore_viewstate(viewstate_T *vs)
203{
204 curwin->w_curswant = vs->vs_curswant;
205 curwin->w_leftcol = vs->vs_leftcol;
206 curwin->w_topline = vs->vs_topline;
207# ifdef FEAT_DIFF
208 curwin->w_topfill = vs->vs_topfill;
209# endif
210 curwin->w_botline = vs->vs_botline;
211 curwin->w_empty_rows = vs->vs_empty_rows;
212}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200213
214// Struct to store the state of 'incsearch' highlighting.
215typedef struct {
216 pos_T search_start; // where 'incsearch' starts searching
217 pos_T save_cursor;
218 viewstate_T init_viewstate;
219 viewstate_T old_viewstate;
220 pos_T match_start;
221 pos_T match_end;
222 int did_incsearch;
223 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200224 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200225} incsearch_state_T;
226
227 static void
228init_incsearch_state(incsearch_state_T *is_state)
229{
230 is_state->match_start = curwin->w_cursor;
231 is_state->did_incsearch = FALSE;
232 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200233 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200234 CLEAR_POS(&is_state->match_end);
235 is_state->save_cursor = curwin->w_cursor; // may be restored later
236 is_state->search_start = curwin->w_cursor;
237 save_viewstate(&is_state->init_viewstate);
238 save_viewstate(&is_state->old_viewstate);
239}
240
241/*
242 * First move cursor to end of match, then to the start. This
243 * moves the whole match onto the screen when 'nowrap' is set.
244 */
245 static void
246set_search_match(pos_T *t)
247{
248 t->lnum += search_match_lines;
249 t->col = search_match_endcol;
250 if (t->lnum > curbuf->b_ml.ml_line_count)
251 {
252 t->lnum = curbuf->b_ml.ml_line_count;
253 coladvance((colnr_T)MAXCOL);
254 }
255}
256
257/*
258 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200259 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200260 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200261 */
262 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200263do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
264 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200265{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200266 char_u *cmd;
267 cmdmod_T save_cmdmod = cmdmod;
268 char_u *p;
269 int delim_optional = FALSE;
270 int delim;
271 char_u *end;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100272 char *dummy;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200273 exarg_T ea;
274 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200275 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200276
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200277 *skiplen = 0;
278 *patlen = ccline.cmdlen;
279
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200280 if (!p_is || cmd_silent)
281 return FALSE;
282
283 // by default search all lines
284 search_first_line = 0;
285 search_last_line = MAXLNUM;
286
287 if (firstc == '/' || firstc == '?')
288 return TRUE;
289 if (firstc != ':')
290 return FALSE;
291
292 vim_memset(&ea, 0, sizeof(ea));
293 ea.line1 = 1;
294 ea.line2 = 1;
295 ea.cmd = ccline.cmdbuff;
296 ea.addr_type = ADDR_LINES;
297
298 parse_command_modifiers(&ea, &dummy, TRUE);
299 cmdmod = save_cmdmod;
300
301 cmd = skip_range(ea.cmd, NULL);
302 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
303 return FALSE;
304
305 // Skip over "substitute" to find the pattern separator.
306 for (p = cmd; ASCII_ISALPHA(*p); ++p)
307 ;
308 if (*skipwhite(p) == NUL)
309 return FALSE;
310
311 if (STRNCMP(cmd, "substitute", p - cmd) == 0
312 || STRNCMP(cmd, "smagic", p - cmd) == 0
313 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
314 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200315 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200316 if (*cmd == 's' && cmd[1] == 'm')
317 p_magic = TRUE;
318 else if (*cmd == 's' && cmd[1] == 'n')
319 p_magic = FALSE;
320 }
321 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
322 {
323 // skip over flags
324 while (ASCII_ISALPHA(*(p = skipwhite(p))))
325 ++p;
326 if (*p == NUL)
327 return FALSE;
328 }
329 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
330 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
331 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
332 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
333 || STRNCMP(cmd, "global", p - cmd) == 0)
334 {
335 // skip over "!"
336 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200337 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200338 p++;
339 if (*skipwhite(p) == NUL)
340 return FALSE;
341 }
342 if (*cmd != 'g')
343 delim_optional = TRUE;
344 }
345 else
346 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200347
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200348 p = skipwhite(p);
349 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
350 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200351
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200352 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200353
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200354 if (end == p && !use_last_pat)
355 return FALSE;
356
357 // Don't do 'hlsearch' highlighting if the pattern matches everything.
358 if (!use_last_pat)
359 {
360 char c = *end;
361 int empty;
362
363 *end = NUL;
364 empty = empty_pattern(p);
365 *end = c;
366 if (empty)
367 return FALSE;
368 }
369
370 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200371 *skiplen = (int)(p - ccline.cmdbuff);
372 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200373
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200374 // parse the address range
375 save_cursor = curwin->w_cursor;
376 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200377 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200378 if (ea.addr_count > 0)
379 {
380 // Allow for reverse match.
381 if (ea.line2 < ea.line1)
382 {
383 search_first_line = ea.line2;
384 search_last_line = ea.line1;
385 }
386 else
387 {
388 search_first_line = ea.line1;
389 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200390 }
391 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200392 else if (cmd[0] == 's' && cmd[1] != 'o')
393 {
394 // :s defaults to the current line
395 search_first_line = curwin->w_cursor.lnum;
396 search_last_line = curwin->w_cursor.lnum;
397 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200398
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200399 curwin->w_cursor = save_cursor;
400 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200401}
402
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200403 static void
404finish_incsearch_highlighting(
405 int gotesc,
406 incsearch_state_T *is_state,
407 int call_update_screen)
408{
409 if (is_state->did_incsearch)
410 {
411 is_state->did_incsearch = FALSE;
412 if (gotesc)
413 curwin->w_cursor = is_state->save_cursor;
414 else
415 {
416 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
417 {
418 // put the '" mark at the original position
419 curwin->w_cursor = is_state->save_cursor;
420 setpcmark();
421 }
422 curwin->w_cursor = is_state->search_start;
423 }
424 restore_viewstate(&is_state->old_viewstate);
425 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200426
427 // by default search all lines
428 search_first_line = 0;
429 search_last_line = MAXLNUM;
430
431 p_magic = is_state->magic_save;
432
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200433 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200434 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200435 if (call_update_screen)
436 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200437 }
438}
439
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200440/*
441 * Do 'incsearch' highlighting if desired.
442 */
443 static void
444may_do_incsearch_highlighting(
445 int firstc,
446 long count,
447 incsearch_state_T *is_state)
448{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200449 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200450 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200451 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200452#ifdef FEAT_RELTIME
453 proftime_T tm;
454#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200455 int next_char;
456 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457
Bram Moolenaar198cb662018-09-06 21:44:17 +0200458 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100459 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200460 save_last_search_pattern();
461
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200462 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200463 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200464 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200465 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200467 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200468
469 // If there is a character waiting, search and redraw later.
470 if (char_avail())
471 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200472 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200473 is_state->incsearch_postponed = TRUE;
474 return;
475 }
476 is_state->incsearch_postponed = FALSE;
477
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200478 if (search_first_line == 0)
479 // start at the original cursor position
480 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100481 else if (search_first_line > curbuf->b_ml.ml_line_count)
482 {
483 // start after the last line
484 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
485 curwin->w_cursor.col = MAXCOL;
486 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200487 else
488 {
489 // start at the first line in the range
490 curwin->w_cursor.lnum = search_first_line;
491 curwin->w_cursor.col = 0;
492 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200493
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200494 // Use the previous pattern for ":s//".
495 next_char = ccline.cmdbuff[skiplen + patlen];
496 use_last_pat = patlen == 0 && skiplen > 0
497 && ccline.cmdbuff[skiplen - 1] == next_char;
498
499 // If there is no pattern, don't do anything.
500 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200501 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200502 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200503 set_no_hlsearch(TRUE); // turn off previous highlight
504 redraw_all_later(SOME_VALID);
505 }
506 else
507 {
508 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
509
510 cursor_off(); // so the user knows we're busy
511 out_flush();
512 ++emsg_off; // so it doesn't beep if bad expr
513#ifdef FEAT_RELTIME
514 // Set the time limit to half a second.
515 profile_setlimit(500L, &tm);
516#endif
517 if (!p_hls)
518 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200519 if (search_first_line != 0)
520 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200521 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200522 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200523 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200524#ifdef FEAT_RELTIME
525 &tm, NULL
526#else
527 NULL, NULL
528#endif
529 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200530 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200531 --emsg_off;
532
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200533 if (curwin->w_cursor.lnum < search_first_line
534 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200535 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200536 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200537 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200538 curwin->w_cursor = is_state->search_start;
539 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200540
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200541 // if interrupted while searching, behave like it failed
542 if (got_int)
543 {
544 (void)vpeekc(); // remove <C-C> from input stream
545 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200546 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547 }
548 else if (char_avail())
549 // cancelled searching because a char was typed
550 is_state->incsearch_postponed = TRUE;
551 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200552 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200553 highlight_match = TRUE; // highlight position
554 else
555 highlight_match = FALSE; // remove highlight
556
557 // First restore the old curwin values, so the screen is positioned in the
558 // same way as the actual search command.
559 restore_viewstate(&is_state->old_viewstate);
560 changed_cline_bef_curs();
561 update_topline();
562
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200563 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200564 {
565 pos_T save_pos = curwin->w_cursor;
566
567 is_state->match_start = curwin->w_cursor;
568 set_search_match(&curwin->w_cursor);
569 validate_cursor();
570 end_pos = curwin->w_cursor;
571 is_state->match_end = end_pos;
572 curwin->w_cursor = save_pos;
573 }
574 else
575 end_pos = curwin->w_cursor; // shutup gcc 4
576
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200577 // Disable 'hlsearch' highlighting if the pattern matches everything.
578 // Avoids a flash when typing "foo\|".
579 if (!use_last_pat)
580 {
581 next_char = ccline.cmdbuff[skiplen + patlen];
582 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200583 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
584 {
585 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200586 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200587 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200588 ccline.cmdbuff[skiplen + patlen] = next_char;
589 }
590
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591 validate_cursor();
592 // May redraw the status line to show the cursor position.
593 if (p_ru && curwin->w_status_height > 0)
594 curwin->w_redr_status = TRUE;
595
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200596 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 restore_last_search_pattern();
598
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200599 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
600 // end of the pattern, e.g. for ":s/pat/".
601 if (ccline.cmdbuff[skiplen + patlen] != NUL)
602 curwin->w_cursor = is_state->search_start;
603 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200604 curwin->w_cursor = end_pos;
605
606 msg_starthere();
607 redrawcmdline();
608 is_state->did_incsearch = TRUE;
609}
610
611/*
612 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
613 * or previous match.
614 * Returns FAIL when jumping to cmdline_not_changed;
615 */
616 static int
617may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200618 int firstc,
619 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200621 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200622{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200623 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 pos_T t;
625 char_u *pat;
626 int search_flags = SEARCH_NOOF;
627 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200628 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200629
Bram Moolenaar198cb662018-09-06 21:44:17 +0200630 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100631 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200632 save_last_search_pattern();
633
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200634 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200635 {
636 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200637 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200638 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200639 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200640 {
641 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200642 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200643 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200644
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200645 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200646 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200647 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200648 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200649 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200650 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200651 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200652 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200654 cursor_off();
655 out_flush();
656 if (c == Ctrl_G)
657 {
658 t = is_state->match_end;
659 if (LT_POS(is_state->match_start, is_state->match_end))
660 // Start searching at the end of the match not at the beginning of
661 // the next column.
662 (void)decl(&t);
663 search_flags += SEARCH_COL;
664 }
665 else
666 t = is_state->match_start;
667 if (!p_hls)
668 search_flags += SEARCH_KEEP;
669 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200670 save = pat[patlen];
671 pat[patlen] = NUL;
Bram Moolenaar5d24a222018-12-23 19:10:09 +0100672 i = searchit(curwin, curbuf, &t, NULL,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200673 c == Ctrl_G ? FORWARD : BACKWARD,
674 pat, count, search_flags,
675 RE_SEARCH, 0, NULL, NULL);
676 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200677 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678 if (i)
679 {
680 is_state->search_start = is_state->match_start;
681 is_state->match_end = t;
682 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200683 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 {
685 // Move just before the current match, so that when nv_search
686 // finishes the cursor will be put back on the match.
687 is_state->search_start = t;
688 (void)decl(&is_state->search_start);
689 }
690 else if (c == Ctrl_G && firstc == '?')
691 {
692 // Move just after the current match, so that when nv_search
693 // finishes the cursor will be put back on the match.
694 is_state->search_start = t;
695 (void)incl(&is_state->search_start);
696 }
697 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
698 {
699 // wrap around
700 is_state->search_start = t;
701 if (firstc == '?')
702 (void)incl(&is_state->search_start);
703 else
704 (void)decl(&is_state->search_start);
705 }
706
707 set_search_match(&is_state->match_end);
708 curwin->w_cursor = is_state->match_start;
709 changed_cline_bef_curs();
710 update_topline();
711 validate_cursor();
712 highlight_match = TRUE;
713 save_viewstate(&is_state->old_viewstate);
714 update_screen(NOT_VALID);
715 redrawcmdline();
716 }
717 else
718 vim_beep(BO_ERROR);
719 restore_last_search_pattern();
720 return FAIL;
721}
722
723/*
724 * When CTRL-L typed: add character from the match to the pattern.
725 * May set "*c" to the added character.
726 * Return OK when jumping to cmdline_not_changed.
727 */
728 static int
729may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
730{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200731 int skiplen, patlen;
732
Bram Moolenaar198cb662018-09-06 21:44:17 +0200733 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100734 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200735 save_last_search_pattern();
736
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200737 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200738 {
739 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200740 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200741 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100742 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200743
744 // Add a character from under the cursor for 'incsearch'.
745 if (is_state->did_incsearch)
746 {
747 curwin->w_cursor = is_state->match_end;
748 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
749 {
750 *c = gchar_cursor();
751
752 // If 'ignorecase' and 'smartcase' are set and the
753 // command line has no uppercase characters, convert
754 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200755 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200756 *c = MB_TOLOWER(*c);
757 if (*c != NUL)
758 {
759 if (*c == firstc || vim_strchr((char_u *)(
760 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
761 {
762 // put a backslash before special characters
763 stuffcharReadbuff(*c);
764 *c = '\\';
765 }
Bram Moolenaar98aefe72018-12-13 22:20:09 +0100766 // add any composing characters
767 if (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
768 {
769 int save_c = *c;
770
771 while (mb_char2len(*c) != mb_ptr2len(ml_get_cursor()))
772 {
773 curwin->w_cursor.col += mb_char2len(*c);
774 *c = gchar_cursor();
775 stuffcharReadbuff(*c);
776 }
777 *c = save_c;
778 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200779 return FAIL;
780 }
781 }
782 }
783 return OK;
784}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200785#endif
786
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200787 void
788cmdline_init(void)
789{
790 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
791}
792
Bram Moolenaar66216052017-12-16 16:33:44 +0100793/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 * getcmdline() - accept a command line starting with firstc.
795 *
796 * firstc == ':' get ":" command line.
797 * firstc == '/' or '?' get search pattern
798 * firstc == '=' get expression
799 * firstc == '@' get text for input() function
800 * firstc == '>' get text for debug mode
801 * firstc == NUL get text for :insert command
802 * firstc == -1 like NUL, and break on CTRL-C
803 *
804 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
805 * command line.
806 *
807 * Careful: getcmdline() can be called recursively!
808 *
809 * Return pointer to allocated string if there is a commandline, NULL
810 * otherwise.
811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100813getcmdline(
814 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200815 long count, // only used for incremental search
816 int indent) // indent for inside conditionals
817{
818 return getcmdline_int(firstc, count, indent, TRUE);
819}
820
821 static char_u *
822getcmdline_int(
823 int firstc,
824 long count UNUSED, // only used for incremental search
825 int indent, // indent for inside conditionals
826 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827{
828 int c;
829 int i;
830 int j;
831 int gotesc = FALSE; /* TRUE when <ESC> just typed */
832 int do_abbr; /* when TRUE check for abbr. */
833#ifdef FEAT_CMDHIST
834 char_u *lookfor = NULL; /* string to match */
835 int hiscnt; /* current history line in use */
836 int histype; /* history type to be used */
837#endif
838#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200839 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840#endif
841 int did_wild_list = FALSE; /* did wild_list() recently */
842 int wim_index = 0; /* index in wim_flags[] */
843 int res;
844 int save_msg_scroll = msg_scroll;
845 int save_State = State; /* remember State when called */
846 int some_key_typed = FALSE; /* one of the keys was typed */
847#ifdef FEAT_MOUSE
848 /* mouse drag and release events are ignored, unless they are
849 * preceded with a mouse down event */
850 int ignore_drag_release = TRUE;
851#endif
852#ifdef FEAT_EVAL
853 int break_ctrl_c = FALSE;
854#endif
855 expand_T xpc;
856 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000857 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200858 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200859 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860
Bram Moolenaar438d1762018-09-30 17:11:48 +0200861 if (ccline.cmdbuff != NULL)
862 {
863 // Being called recursively. Since ccline is global, we need to save
864 // the current buffer and restore it when returning.
865 save_cmdline(&save_ccline);
866 did_save_ccline = TRUE;
867 }
868 if (init_ccline)
869 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871#ifdef FEAT_EVAL
872 if (firstc == -1)
873 {
874 firstc = NUL;
875 break_ctrl_c = TRUE;
876 }
877#endif
878#ifdef FEAT_RIGHTLEFT
879 /* start without Hebrew mapping for a command line */
880 if (firstc == ':' || firstc == '=' || firstc == '>')
881 cmd_hkmap = 0;
882#endif
883
884 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200885
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200887 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888#endif
889
890 /*
891 * set some variables for redrawcmd()
892 */
893 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000894 ccline.cmdindent = (firstc > 0 ? indent : 0);
895
896 /* alloc initial ccline.cmdbuff */
897 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200899 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 ccline.cmdlen = ccline.cmdpos = 0;
901 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100902 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000904 /* autoindent for :insert and :append */
905 if (firstc <= 0)
906 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200907 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000908 ccline.cmdbuff[indent] = NUL;
909 ccline.cmdpos = indent;
910 ccline.cmdspos = indent;
911 ccline.cmdlen = indent;
912 }
913
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000915 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916
917#ifdef FEAT_RIGHTLEFT
918 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
919 && (firstc == '/' || firstc == '?'))
920 cmdmsg_rl = TRUE;
921 else
922 cmdmsg_rl = FALSE;
923#endif
924
925 redir_off = TRUE; /* don't redirect the typed command */
926 if (!cmd_silent)
927 {
928 i = msg_scrolled;
929 msg_scrolled = 0; /* avoid wait_return message */
930 gotocmdline(TRUE);
931 msg_scrolled += i;
932 redrawcmdprompt(); /* draw prompt or indent */
933 set_cmdspos();
934 }
935 xpc.xp_context = EXPAND_NOTHING;
936 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000937#ifndef BACKSLASH_IN_FILENAME
938 xpc.xp_shell = FALSE;
939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000941#if defined(FEAT_EVAL)
942 if (ccline.input_fn)
943 {
944 xpc.xp_context = ccline.xp_context;
945 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000946# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000947 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000948# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000949 }
950#endif
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 /*
953 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
954 * doing ":@0" when register 0 doesn't contain a CR.
955 */
956 msg_scroll = FALSE;
957
958 State = CMDLINE;
959
960 if (firstc == '/' || firstc == '?' || firstc == '@')
961 {
962 /* Use ":lmap" mappings for search pattern and input(). */
963 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
964 b_im_ptr = &curbuf->b_p_iminsert;
965 else
966 b_im_ptr = &curbuf->b_p_imsearch;
967 if (*b_im_ptr == B_IMODE_LMAP)
968 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100969#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 im_set_active(*b_im_ptr == B_IMODE_IM);
971#endif
972 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100973#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 else if (p_imcmdline)
975 im_set_active(TRUE);
976#endif
977
978#ifdef FEAT_MOUSE
979 setmouse();
980#endif
981#ifdef CURSOR_SHAPE
982 ui_cursor_shape(); /* may show different cursor shape */
983#endif
984
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000985 /* When inside an autocommand for writing "exiting" may be set and
986 * terminal mode set to cooked. Need to set raw mode here then. */
987 settmode(TMODE_RAW);
988
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200989 /* Trigger CmdlineEnter autocommands. */
990 cmdline_type = firstc == NUL ? '-' : firstc;
991 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200992
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993#ifdef FEAT_CMDHIST
994 init_history();
995 hiscnt = hislen; /* set hiscnt to impossible history value */
996 histype = hist_char2type(firstc);
997#endif
998
999#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +00001000 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001#endif
1002
Bram Moolenaar15a35c42014-06-25 12:26:46 +02001003 /* If something above caused an error, reset the flags, we do want to type
1004 * and execute commands. Display may be messed up a bit. */
1005 if (did_emsg)
1006 redrawcmd();
1007 did_emsg = FALSE;
1008 got_int = FALSE;
1009
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 /*
1011 * Collect the command string, handling editing keys.
1012 */
1013 for (;;)
1014 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001015 redir_off = TRUE; /* Don't redirect the typed command.
1016 Repeated, because a ":redir" inside
1017 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018#ifdef USE_ON_FLY_SCROLL
1019 dont_scroll = FALSE; /* allow scrolling here */
1020#endif
1021 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1022
Bram Moolenaar72532d32018-04-07 19:09:09 +02001023 did_emsg = FALSE; /* There can't really be a reason why an error
1024 that occurs while typing a command should
1025 cause the command not to be executed. */
1026
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001028
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001029 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1030 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001031 do
1032 {
1033 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001034 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001035
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 if (KeyTyped)
1037 {
1038 some_key_typed = TRUE;
1039#ifdef FEAT_RIGHTLEFT
1040 if (cmd_hkmap)
1041 c = hkmap(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 if (cmdmsg_rl && !KeyStuffed)
1043 {
1044 /* Invert horizontal movements and operations. Only when
1045 * typed by the user directly, not when the result of a
1046 * mapping. */
1047 switch (c)
1048 {
1049 case K_RIGHT: c = K_LEFT; break;
1050 case K_S_RIGHT: c = K_S_LEFT; break;
1051 case K_C_RIGHT: c = K_C_LEFT; break;
1052 case K_LEFT: c = K_RIGHT; break;
1053 case K_S_LEFT: c = K_S_RIGHT; break;
1054 case K_C_LEFT: c = K_C_RIGHT; break;
1055 }
1056 }
1057#endif
1058 }
1059
1060 /*
1061 * Ignore got_int when CTRL-C was typed here.
1062 * Don't ignore it in :global, we really need to break then, e.g., for
1063 * ":g/pat/normal /pat" (without the <CR>).
1064 * Don't ignore it for the input() function.
1065 */
1066 if ((c == Ctrl_C
1067#ifdef UNIX
1068 || c == intr_char
1069#endif
1070 )
1071#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1072 && firstc != '@'
1073#endif
1074#ifdef FEAT_EVAL
1075 && !break_ctrl_c
1076#endif
1077 && !global_busy)
1078 got_int = FALSE;
1079
1080#ifdef FEAT_CMDHIST
1081 /* free old command line when finished moving around in the history
1082 * list */
1083 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001084 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001085 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 && c != K_PAGEDOWN && c != K_PAGEUP
1087 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001088 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001090 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091#endif
1092
1093 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001094 * When there are matching completions to select <S-Tab> works like
1095 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001097 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 c = Ctrl_P;
1099
1100#ifdef FEAT_WILDMENU
1101 /* Special translations for 'wildmenu' */
1102 if (did_wild_list && p_wmnu)
1103 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001104 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001106 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 c = Ctrl_N;
1108 }
1109 /* Hitting CR after "emenu Name.": complete submenu */
1110 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1111 && ccline.cmdpos > 1
1112 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1113 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1114 && (c == '\n' || c == '\r' || c == K_KENTER))
1115 c = K_DOWN;
1116#endif
1117
1118 /* free expanded names when finished walking through matches */
1119 if (xpc.xp_numfiles != -1
1120 && !(c == p_wc && KeyTyped) && c != p_wcm
1121 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1122 && c != Ctrl_L)
1123 {
1124 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1125 did_wild_list = FALSE;
1126#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001127 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128#endif
1129 xpc.xp_context = EXPAND_NOTHING;
1130 wim_index = 0;
1131#ifdef FEAT_WILDMENU
1132 if (p_wmnu && wild_menu_showing != 0)
1133 {
1134 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001135 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001136
1137 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001138 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139
1140 if (wild_menu_showing == WM_SCROLLED)
1141 {
1142 /* Entered command line, move it up */
1143 cmdline_row--;
1144 redrawcmd();
1145 }
1146 else if (save_p_ls != -1)
1147 {
1148 /* restore 'laststatus' and 'winminheight' */
1149 p_ls = save_p_ls;
1150 p_wmh = save_p_wmh;
1151 last_status(FALSE);
1152 update_screen(VALID); /* redraw the screen NOW */
1153 redrawcmd();
1154 save_p_ls = -1;
1155 }
1156 else
1157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 redraw_statuslines();
1160 }
1161 KeyTyped = skt;
1162 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001163 if (ccline.input_fn)
1164 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 }
1166#endif
1167 }
1168
1169#ifdef FEAT_WILDMENU
1170 /* Special translations for 'wildmenu' */
1171 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1172 {
1173 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001174 if (c == K_DOWN && ccline.cmdpos > 0
1175 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001177 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 {
1179 /* Hitting <Up>: Remove one submenu name in front of the
1180 * cursor */
1181 int found = FALSE;
1182
1183 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1184 i = 0;
1185 while (--j > 0)
1186 {
1187 /* check for start of menu name */
1188 if (ccline.cmdbuff[j] == ' '
1189 && ccline.cmdbuff[j - 1] != '\\')
1190 {
1191 i = j + 1;
1192 break;
1193 }
1194 /* check for start of submenu name */
1195 if (ccline.cmdbuff[j] == '.'
1196 && ccline.cmdbuff[j - 1] != '\\')
1197 {
1198 if (found)
1199 {
1200 i = j + 1;
1201 break;
1202 }
1203 else
1204 found = TRUE;
1205 }
1206 }
1207 if (i > 0)
1208 cmdline_del(i);
1209 c = p_wc;
1210 xpc.xp_context = EXPAND_NOTHING;
1211 }
1212 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001213 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001214 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001215 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 {
1217 char_u upseg[5];
1218
1219 upseg[0] = PATHSEP;
1220 upseg[1] = '.';
1221 upseg[2] = '.';
1222 upseg[3] = PATHSEP;
1223 upseg[4] = NUL;
1224
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001225 if (c == K_DOWN
1226 && ccline.cmdpos > 0
1227 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1228 && (ccline.cmdpos < 3
1229 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1231 {
1232 /* go down a directory */
1233 c = p_wc;
1234 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001235 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 {
1237 /* If in a direct ancestor, strip off one ../ to go down */
1238 int found = FALSE;
1239
1240 j = ccline.cmdpos;
1241 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1242 while (--j > i)
1243 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001244 if (has_mbyte)
1245 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 if (vim_ispathsep(ccline.cmdbuff[j]))
1247 {
1248 found = TRUE;
1249 break;
1250 }
1251 }
1252 if (found
1253 && ccline.cmdbuff[j - 1] == '.'
1254 && ccline.cmdbuff[j - 2] == '.'
1255 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1256 {
1257 cmdline_del(j - 2);
1258 c = p_wc;
1259 }
1260 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001261 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 {
1263 /* go up a directory */
1264 int found = FALSE;
1265
1266 j = ccline.cmdpos - 1;
1267 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1268 while (--j > i)
1269 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 if (has_mbyte)
1271 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 if (vim_ispathsep(ccline.cmdbuff[j])
1273#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001274 && vim_strchr((char_u *)" *?[{`$%#",
1275 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277 )
1278 {
1279 if (found)
1280 {
1281 i = j + 1;
1282 break;
1283 }
1284 else
1285 found = TRUE;
1286 }
1287 }
1288
1289 if (!found)
1290 j = i;
1291 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1292 j += 4;
1293 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1294 && j == i)
1295 j += 3;
1296 else
1297 j = 0;
1298 if (j > 0)
1299 {
1300 /* TODO this is only for DOS/UNIX systems - need to put in
1301 * machine-specific stuff here and in upseg init */
1302 cmdline_del(j);
1303 put_on_cmdline(upseg + 1, 3, FALSE);
1304 }
1305 else if (ccline.cmdpos > i)
1306 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001307
1308 /* Now complete in the new directory. Set KeyTyped in case the
1309 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001311 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 }
1313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314
1315#endif /* FEAT_WILDMENU */
1316
1317 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1318 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1319 if (c == Ctrl_BSL)
1320 {
1321 ++no_mapping;
1322 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001323 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 --no_mapping;
1325 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001326 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1327 * is in a mapping. */
1328 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001329 || (ccline.cmdfirstc == '=' && KeyTyped)
1330#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001331 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001332#endif
1333 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 {
1335 vungetc(c);
1336 c = Ctrl_BSL;
1337 }
1338#ifdef FEAT_EVAL
1339 else if (c == 'e')
1340 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001341 char_u *p = NULL;
1342 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343
1344 /*
1345 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001346 * Need to save and restore the current command line, to be
1347 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 */
1349 if (ccline.cmdpos == ccline.cmdlen)
1350 new_cmdpos = 99999; /* keep it at the end */
1351 else
1352 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 if (c == '=')
1356 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001357 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001358 * to avoid nasty things like going to another buffer when
1359 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001360 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001362 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001363
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001364 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001366 len = (int)STRLEN(p);
1367 if (realloc_cmdbuff(len + 1) == OK)
1368 {
1369 ccline.cmdlen = len;
1370 STRCPY(ccline.cmdbuff, p);
1371 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001373 /* Restore the cursor or use the position set with
1374 * set_cmdline_pos(). */
1375 if (new_cmdpos > ccline.cmdlen)
1376 ccline.cmdpos = ccline.cmdlen;
1377 else
1378 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001380 KeyTyped = FALSE; /* Don't do p_wc completion. */
1381 redrawcmd();
1382 goto cmdline_changed;
1383 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001384 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 }
1387 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001388 got_int = FALSE; /* don't abandon the command line */
1389 did_emsg = FALSE;
1390 emsg_on_display = FALSE;
1391 redrawcmd();
1392 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 }
1394#endif
1395 else
1396 {
1397 if (c == Ctrl_G && p_im && restart_edit == 0)
1398 restart_edit = 'a';
1399 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1400 in history */
1401 goto returncmd; /* back to Normal mode */
1402 }
1403 }
1404
1405#ifdef FEAT_CMDWIN
1406 if (c == cedit_key || c == K_CMDWIN)
1407 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001408 if (ex_normal_busy == 0 && got_int == FALSE)
1409 {
1410 /*
1411 * Open a window to edit the command line (and history).
1412 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001413 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001414 some_key_typed = TRUE;
1415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 }
1417# ifdef FEAT_DIGRAPHS
1418 else
1419# endif
1420#endif
1421#ifdef FEAT_DIGRAPHS
1422 c = do_digraph(c);
1423#endif
1424
1425 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1426 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1427 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001428 /* In Ex mode a backslash escapes a newline. */
1429 if (exmode_active
1430 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001431 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001432 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001433 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001434 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001435 if (c == K_KENTER)
1436 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001438 else
1439 {
1440 gotesc = FALSE; /* Might have typed ESC previously, don't
1441 truncate the cmdline now. */
1442 if (ccheck_abbr(c + ABBR_OFF))
1443 goto cmdline_changed;
1444 if (!cmd_silent)
1445 {
1446 windgoto(msg_row, 0);
1447 out_flush();
1448 }
1449 break;
1450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 }
1452
1453 /*
1454 * Completion for 'wildchar' or 'wildcharm' key.
1455 * - hitting <ESC> twice means: abandon command line.
1456 * - wildcard expansion is only done when the 'wildchar' key is really
1457 * typed, not when it comes from a macro
1458 */
1459 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1460 {
1461 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1462 {
1463 /* if 'wildmode' contains "list" may still need to list */
1464 if (xpc.xp_numfiles > 1
1465 && !did_wild_list
1466 && (wim_flags[wim_index] & WIM_LIST))
1467 {
1468 (void)showmatches(&xpc, FALSE);
1469 redrawcmd();
1470 did_wild_list = TRUE;
1471 }
1472 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001473 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1474 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001476 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1477 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 else
1479 res = OK; /* don't insert 'wildchar' now */
1480 }
1481 else /* typed p_wc first time */
1482 {
1483 wim_index = 0;
1484 j = ccline.cmdpos;
1485 /* if 'wildmode' first contains "longest", get longest
1486 * common part */
1487 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001488 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1489 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001491 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1492 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494 /* if interrupted while completing, behave like it failed */
1495 if (got_int)
1496 {
1497 (void)vpeekc(); /* remove <C-C> from input stream */
1498 got_int = FALSE; /* don't abandon the command line */
1499 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1500#ifdef FEAT_WILDMENU
1501 xpc.xp_context = EXPAND_NOTHING;
1502#endif
1503 goto cmdline_changed;
1504 }
1505
1506 /* when more than one match, and 'wildmode' first contains
1507 * "list", or no change and 'wildmode' contains "longest,list",
1508 * list all matches */
1509 if (res == OK && xpc.xp_numfiles > 1)
1510 {
1511 /* a "longest" that didn't do anything is skipped (but not
1512 * "list:longest") */
1513 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1514 wim_index = 1;
1515 if ((wim_flags[wim_index] & WIM_LIST)
1516#ifdef FEAT_WILDMENU
1517 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1518#endif
1519 )
1520 {
1521 if (!(wim_flags[0] & WIM_LONGEST))
1522 {
1523#ifdef FEAT_WILDMENU
1524 int p_wmnu_save = p_wmnu;
1525 p_wmnu = 0;
1526#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001527 /* remove match */
1528 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529#ifdef FEAT_WILDMENU
1530 p_wmnu = p_wmnu_save;
1531#endif
1532 }
1533#ifdef FEAT_WILDMENU
1534 (void)showmatches(&xpc, p_wmnu
1535 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1536#else
1537 (void)showmatches(&xpc, FALSE);
1538#endif
1539 redrawcmd();
1540 did_wild_list = TRUE;
1541 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001542 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1543 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001545 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1546 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 }
1548 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001549 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 }
1551#ifdef FEAT_WILDMENU
1552 else if (xpc.xp_numfiles == -1)
1553 xpc.xp_context = EXPAND_NOTHING;
1554#endif
1555 }
1556 if (wim_index < 3)
1557 ++wim_index;
1558 if (c == ESC)
1559 gotesc = TRUE;
1560 if (res == OK)
1561 goto cmdline_changed;
1562 }
1563
1564 gotesc = FALSE;
1565
1566 /* <S-Tab> goes to last match, in a clumsy way */
1567 if (c == K_S_TAB && KeyTyped)
1568 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001569 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1570 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1571 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 goto cmdline_changed;
1573 }
1574
1575 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1576 c = NL;
1577
1578 do_abbr = TRUE; /* default: check for abbreviation */
1579
1580 /*
1581 * Big switch for a typed command line character.
1582 */
1583 switch (c)
1584 {
1585 case K_BS:
1586 case Ctrl_H:
1587 case K_DEL:
1588 case K_KDEL:
1589 case Ctrl_W:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 if (c == K_KDEL)
1591 c = K_DEL;
1592
1593 /*
1594 * delete current character is the same as backspace on next
1595 * character, except at end of line
1596 */
1597 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1598 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (has_mbyte && c == K_DEL)
1600 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1601 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602 if (ccline.cmdpos > 0)
1603 {
1604 char_u *p;
1605
1606 j = ccline.cmdpos;
1607 p = ccline.cmdbuff + j;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (has_mbyte)
1609 {
1610 p = mb_prevptr(ccline.cmdbuff, p);
1611 if (c == Ctrl_W)
1612 {
1613 while (p > ccline.cmdbuff && vim_isspace(*p))
1614 p = mb_prevptr(ccline.cmdbuff, p);
1615 i = mb_get_class(p);
1616 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1617 p = mb_prevptr(ccline.cmdbuff, p);
1618 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001619 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 }
1621 }
Bram Moolenaar13505972019-01-24 15:04:48 +01001622 else if (c == Ctrl_W)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1625 --p;
1626 i = vim_iswordc(p[-1]);
1627 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1628 && vim_iswordc(p[-1]) == i)
1629 --p;
1630 }
1631 else
1632 --p;
1633 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1634 ccline.cmdlen -= j - ccline.cmdpos;
1635 i = ccline.cmdpos;
1636 while (i < ccline.cmdlen)
1637 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1638
1639 /* Truncate at the end, required for multi-byte chars. */
1640 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001641#ifdef FEAT_SEARCH_EXTRA
1642 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001643 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001644 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001645 /* save view settings, so that the screen
1646 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001647 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001648 }
1649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 redrawcmd();
1651 }
1652 else if (ccline.cmdlen == 0 && c != Ctrl_W
1653 && ccline.cmdprompt == NULL && indent == 0)
1654 {
1655 /* In ex and debug mode it doesn't make sense to return. */
1656 if (exmode_active
1657#ifdef FEAT_EVAL
1658 || ccline.cmdfirstc == '>'
1659#endif
1660 )
1661 goto cmdline_not_changed;
1662
Bram Moolenaard23a8232018-02-10 18:45:26 +01001663 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (!cmd_silent)
1665 {
1666#ifdef FEAT_RIGHTLEFT
1667 if (cmdmsg_rl)
1668 msg_col = Columns;
1669 else
1670#endif
1671 msg_col = 0;
1672 msg_putchar(' '); /* delete ':' */
1673 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001674#ifdef FEAT_SEARCH_EXTRA
1675 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001676 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 redraw_cmdline = TRUE;
1679 goto returncmd; /* back to cmd mode */
1680 }
1681 goto cmdline_changed;
1682
1683 case K_INS:
1684 case K_KINS:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 ccline.overstrike = !ccline.overstrike;
1686#ifdef CURSOR_SHAPE
1687 ui_cursor_shape(); /* may show different cursor shape */
1688#endif
1689 goto cmdline_not_changed;
1690
1691 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001692 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 {
1694 /* ":lmap" mappings exists, toggle use of mappings. */
1695 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001696#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 im_set_active(FALSE); /* Disable input method */
1698#endif
1699 if (b_im_ptr != NULL)
1700 {
1701 if (State & LANGMAP)
1702 *b_im_ptr = B_IMODE_LMAP;
1703 else
1704 *b_im_ptr = B_IMODE_NONE;
1705 }
1706 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001707#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708 else
1709 {
1710 /* There are no ":lmap" mappings, toggle IM. When
1711 * 'imdisable' is set don't try getting the status, it's
1712 * always off. */
1713 if ((p_imdisable && b_im_ptr != NULL)
1714 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1715 {
1716 im_set_active(FALSE); /* Disable input method */
1717 if (b_im_ptr != NULL)
1718 *b_im_ptr = B_IMODE_NONE;
1719 }
1720 else
1721 {
1722 im_set_active(TRUE); /* Enable input method */
1723 if (b_im_ptr != NULL)
1724 *b_im_ptr = B_IMODE_IM;
1725 }
1726 }
1727#endif
1728 if (b_im_ptr != NULL)
1729 {
1730 if (b_im_ptr == &curbuf->b_p_iminsert)
1731 set_iminsert_global();
1732 else
1733 set_imsearch_global();
1734 }
1735#ifdef CURSOR_SHAPE
1736 ui_cursor_shape(); /* may show different cursor shape */
1737#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001738#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /* Show/unshow value of 'keymap' in status lines later. */
1740 status_redraw_curbuf();
1741#endif
1742 goto cmdline_not_changed;
1743
1744/* case '@': only in very old vi */
1745 case Ctrl_U:
1746 /* delete all characters left of the cursor */
1747 j = ccline.cmdpos;
1748 ccline.cmdlen -= j;
1749 i = ccline.cmdpos = 0;
1750 while (i < ccline.cmdlen)
1751 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1752 /* Truncate at the end, required for multi-byte chars. */
1753 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001754#ifdef FEAT_SEARCH_EXTRA
1755 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001756 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 redrawcmd();
1759 goto cmdline_changed;
1760
1761#ifdef FEAT_CLIPBOARD
1762 case Ctrl_Y:
1763 /* Copy the modeless selection, if there is one. */
1764 if (clip_star.state != SELECT_CLEARED)
1765 {
1766 if (clip_star.state == SELECT_DONE)
1767 clip_copy_modeless_selection(TRUE);
1768 goto cmdline_not_changed;
1769 }
1770 break;
1771#endif
1772
1773 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1774 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001775 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001776 * ":normal" runs out of characters. */
1777 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001778 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 goto cmdline_not_changed;
1780
1781 gotesc = TRUE; /* will free ccline.cmdbuff after
1782 putting it in history */
1783 goto returncmd; /* back to cmd mode */
1784
1785 case Ctrl_R: /* insert register */
1786#ifdef USE_ON_FLY_SCROLL
1787 dont_scroll = TRUE; /* disallow scrolling here */
1788#endif
1789 putcmdline('"', TRUE);
1790 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001791 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (i == Ctrl_O)
1793 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1794 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001795 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001796 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 --no_mapping;
1798#ifdef FEAT_EVAL
1799 /*
1800 * Insert the result of an expression.
1801 * Need to save the current command line, to be able to enter
1802 * a new one...
1803 */
1804 new_cmdpos = -1;
1805 if (c == '=')
1806 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001807 if (ccline.cmdfirstc == '=' // can't do this recursively
1808 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 {
1810 beep_flush();
1811 c = ESC;
1812 }
1813 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 }
1816#endif
1817 if (c != ESC) /* use ESC to cancel inserting register */
1818 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001819 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001820
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001821#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001822 /* When there was a serious error abort getting the
1823 * command line. */
1824 if (aborting())
1825 {
1826 gotesc = TRUE; /* will free ccline.cmdbuff after
1827 putting it in history */
1828 goto returncmd; /* back to cmd mode */
1829 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 KeyTyped = FALSE; /* Don't do p_wc completion. */
1832#ifdef FEAT_EVAL
1833 if (new_cmdpos >= 0)
1834 {
1835 /* set_cmdline_pos() was used */
1836 if (new_cmdpos > ccline.cmdlen)
1837 ccline.cmdpos = ccline.cmdlen;
1838 else
1839 ccline.cmdpos = new_cmdpos;
1840 }
1841#endif
1842 }
1843 redrawcmd();
1844 goto cmdline_changed;
1845
1846 case Ctrl_D:
1847 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1848 break; /* Use ^D as normal char instead */
1849
1850 redrawcmd();
1851 continue; /* don't do incremental search now */
1852
1853 case K_RIGHT:
1854 case K_S_RIGHT:
1855 case K_C_RIGHT:
1856 do
1857 {
1858 if (ccline.cmdpos >= ccline.cmdlen)
1859 break;
1860 i = cmdline_charsize(ccline.cmdpos);
1861 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1862 break;
1863 ccline.cmdspos += i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001865 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 + ccline.cmdpos);
1867 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 ++ccline.cmdpos;
1869 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001870 while ((c == K_S_RIGHT || c == K_C_RIGHT
1871 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 && ccline.cmdbuff[ccline.cmdpos] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (has_mbyte)
1874 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 goto cmdline_not_changed;
1876
1877 case K_LEFT:
1878 case K_S_LEFT:
1879 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001880 if (ccline.cmdpos == 0)
1881 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 do
1883 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 --ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 if (has_mbyte) /* move to first byte of char */
1886 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1887 ccline.cmdbuff + ccline.cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1889 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001890 while (ccline.cmdpos > 0
1891 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001892 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (has_mbyte)
1895 set_cmdspos_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 goto cmdline_not_changed;
1897
1898 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001899 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001900 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901
Bram Moolenaar4f974752019-02-17 17:44:42 +01001902#ifdef FEAT_GUI_MSWIN
1903 /* On MS-Windows ignore <M-F4>, we get it when closing the window
1904 * was cancelled. */
Bram Moolenaar4770d092006-01-12 23:22:24 +00001905 case K_F4:
1906 if (mod_mask == MOD_MASK_ALT)
1907 {
1908 redrawcmd(); /* somehow the cmdline is cleared */
1909 goto cmdline_not_changed;
1910 }
1911 break;
1912#endif
1913
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914#ifdef FEAT_MOUSE
1915 case K_MIDDLEDRAG:
1916 case K_MIDDLERELEASE:
1917 goto cmdline_not_changed; /* Ignore mouse */
1918
1919 case K_MIDDLEMOUSE:
1920# ifdef FEAT_GUI
1921 /* When GUI is active, also paste when 'mouse' is empty */
1922 if (!gui.in_use)
1923# endif
1924 if (!mouse_has(MOUSE_COMMAND))
1925 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001926# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001928 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001930# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001931 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 redrawcmd();
1933 goto cmdline_changed;
1934
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001935# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001937 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 redrawcmd();
1939 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001940# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941
1942 case K_LEFTDRAG:
1943 case K_LEFTRELEASE:
1944 case K_RIGHTDRAG:
1945 case K_RIGHTRELEASE:
1946 /* Ignore drag and release events when the button-down wasn't
1947 * seen before. */
1948 if (ignore_drag_release)
1949 goto cmdline_not_changed;
1950 /* FALLTHROUGH */
1951 case K_LEFTMOUSE:
1952 case K_RIGHTMOUSE:
1953 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1954 ignore_drag_release = TRUE;
1955 else
1956 ignore_drag_release = FALSE;
1957# ifdef FEAT_GUI
1958 /* When GUI is active, also move when 'mouse' is empty */
1959 if (!gui.in_use)
1960# endif
1961 if (!mouse_has(MOUSE_COMMAND))
1962 goto cmdline_not_changed; /* Ignore mouse */
1963# ifdef FEAT_CLIPBOARD
1964 if (mouse_row < cmdline_row && clip_star.available)
1965 {
1966 int button, is_click, is_drag;
1967
1968 /*
1969 * Handle modeless selection.
1970 */
1971 button = get_mouse_button(KEY2TERMCAP1(c),
1972 &is_click, &is_drag);
1973 if (mouse_model_popup() && button == MOUSE_LEFT
1974 && (mod_mask & MOD_MASK_SHIFT))
1975 {
1976 /* Translate shift-left to right button. */
1977 button = MOUSE_RIGHT;
1978 mod_mask &= ~MOD_MASK_SHIFT;
1979 }
1980 clip_modeless(button, is_click, is_drag);
1981 goto cmdline_not_changed;
1982 }
1983# endif
1984
1985 set_cmdspos();
1986 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1987 ++ccline.cmdpos)
1988 {
1989 i = cmdline_charsize(ccline.cmdpos);
1990 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1991 && mouse_col < ccline.cmdspos % Columns + i)
1992 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (has_mbyte)
1994 {
1995 /* Count ">" for double-wide char that doesn't fit. */
1996 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001997 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 + ccline.cmdpos) - 1;
1999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 ccline.cmdspos += i;
2001 }
2002 goto cmdline_not_changed;
2003
2004 /* Mouse scroll wheel: ignored here */
2005 case K_MOUSEDOWN:
2006 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002007 case K_MOUSELEFT:
2008 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 /* Alternate buttons ignored here */
2010 case K_X1MOUSE:
2011 case K_X1DRAG:
2012 case K_X1RELEASE:
2013 case K_X2MOUSE:
2014 case K_X2DRAG:
2015 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002016 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 goto cmdline_not_changed;
2018
2019#endif /* FEAT_MOUSE */
2020
2021#ifdef FEAT_GUI
2022 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2023 case K_LEFTRELEASE_NM:
2024 goto cmdline_not_changed;
2025
2026 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002027 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 gui_do_scroll();
2030 redrawcmd();
2031 }
2032 goto cmdline_not_changed;
2033
2034 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002035 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002037 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 redrawcmd();
2039 }
2040 goto cmdline_not_changed;
2041#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002042#ifdef FEAT_GUI_TABLINE
2043 case K_TABLINE:
2044 case K_TABMENU:
2045 /* Don't want to change any tabs here. Make sure the same tab
2046 * is still selected. */
2047 if (gui_use_tabline())
2048 gui_mch_set_curtab(tabpage_index(curtab));
2049 goto cmdline_not_changed;
2050#endif
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 case K_SELECT: /* end of Select mode mapping - ignore */
2053 goto cmdline_not_changed;
2054
2055 case Ctrl_B: /* begin of command line */
2056 case K_HOME:
2057 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 case K_S_HOME:
2059 case K_C_HOME:
2060 ccline.cmdpos = 0;
2061 set_cmdspos();
2062 goto cmdline_not_changed;
2063
2064 case Ctrl_E: /* end of command line */
2065 case K_END:
2066 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 case K_S_END:
2068 case K_C_END:
2069 ccline.cmdpos = ccline.cmdlen;
2070 set_cmdspos_cursor();
2071 goto cmdline_not_changed;
2072
2073 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002074 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 break;
2076 goto cmdline_changed;
2077
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002078 case Ctrl_L:
2079#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002080 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002081 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002082#endif
2083
2084 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002085 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 break;
2087 goto cmdline_changed;
2088
2089 case Ctrl_N: /* next match */
2090 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002091 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002093 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2094 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002096 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002099 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 case K_UP:
2101 case K_DOWN:
2102 case K_S_UP:
2103 case K_S_DOWN:
2104 case K_PAGEUP:
2105 case K_KPAGEUP:
2106 case K_PAGEDOWN:
2107 case K_KPAGEDOWN:
2108 if (hislen == 0 || firstc == NUL) /* no history */
2109 goto cmdline_not_changed;
2110
2111 i = hiscnt;
2112
2113 /* save current command string so it can be restored later */
2114 if (lookfor == NULL)
2115 {
2116 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2117 goto cmdline_not_changed;
2118 lookfor[ccline.cmdpos] = NUL;
2119 }
2120
2121 j = (int)STRLEN(lookfor);
2122 for (;;)
2123 {
2124 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002125 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002126 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 {
2128 if (hiscnt == hislen) /* first time */
2129 hiscnt = hisidx[histype];
2130 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2131 hiscnt = hislen - 1;
2132 else if (hiscnt != hisidx[histype] + 1)
2133 --hiscnt;
2134 else /* at top of list */
2135 {
2136 hiscnt = i;
2137 break;
2138 }
2139 }
2140 else /* one step forwards */
2141 {
2142 /* on last entry, clear the line */
2143 if (hiscnt == hisidx[histype])
2144 {
2145 hiscnt = hislen;
2146 break;
2147 }
2148
2149 /* not on a history line, nothing to do */
2150 if (hiscnt == hislen)
2151 break;
2152 if (hiscnt == hislen - 1) /* wrap around */
2153 hiscnt = 0;
2154 else
2155 ++hiscnt;
2156 }
2157 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2158 {
2159 hiscnt = i;
2160 break;
2161 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002162 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002163 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 || STRNCMP(history[histype][hiscnt].hisstr,
2165 lookfor, (size_t)j) == 0)
2166 break;
2167 }
2168
2169 if (hiscnt != i) /* jumped to other entry */
2170 {
2171 char_u *p;
2172 int len;
2173 int old_firstc;
2174
Bram Moolenaar438d1762018-09-30 17:11:48 +02002175 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002176 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 if (hiscnt == hislen)
2178 p = lookfor; /* back to the old one */
2179 else
2180 p = history[histype][hiscnt].hisstr;
2181
2182 if (histype == HIST_SEARCH
2183 && p != lookfor
2184 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2185 {
2186 /* Correct for the separator character used when
2187 * adding the history entry vs the one used now.
2188 * First loop: count length.
2189 * Second loop: copy the characters. */
2190 for (i = 0; i <= 1; ++i)
2191 {
2192 len = 0;
2193 for (j = 0; p[j] != NUL; ++j)
2194 {
2195 /* Replace old sep with new sep, unless it is
2196 * escaped. */
2197 if (p[j] == old_firstc
2198 && (j == 0 || p[j - 1] != '\\'))
2199 {
2200 if (i > 0)
2201 ccline.cmdbuff[len] = firstc;
2202 }
2203 else
2204 {
2205 /* Escape new sep, unless it is already
2206 * escaped. */
2207 if (p[j] == firstc
2208 && (j == 0 || p[j - 1] != '\\'))
2209 {
2210 if (i > 0)
2211 ccline.cmdbuff[len] = '\\';
2212 ++len;
2213 }
2214 if (i > 0)
2215 ccline.cmdbuff[len] = p[j];
2216 }
2217 ++len;
2218 }
2219 if (i == 0)
2220 {
2221 alloc_cmdbuff(len);
2222 if (ccline.cmdbuff == NULL)
2223 goto returncmd;
2224 }
2225 }
2226 ccline.cmdbuff[len] = NUL;
2227 }
2228 else
2229 {
2230 alloc_cmdbuff((int)STRLEN(p));
2231 if (ccline.cmdbuff == NULL)
2232 goto returncmd;
2233 STRCPY(ccline.cmdbuff, p);
2234 }
2235
2236 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2237 redrawcmd();
2238 goto cmdline_changed;
2239 }
2240 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002241#endif
2242 goto cmdline_not_changed;
2243
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002244#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002245 case Ctrl_G: /* next match */
2246 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002247 if (may_adjust_incsearch_highlighting(
2248 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002249 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002250 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251#endif
2252
2253 case Ctrl_V:
2254 case Ctrl_Q:
2255#ifdef FEAT_MOUSE
2256 ignore_drag_release = TRUE;
2257#endif
2258 putcmdline('^', TRUE);
2259 c = get_literal(); /* get next (two) character(s) */
2260 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002261 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 /* may need to remove ^ when composing char was typed */
2263 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2264 {
2265 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2266 msg_putchar(' ');
2267 cursorcmd();
2268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 break;
2270
2271#ifdef FEAT_DIGRAPHS
2272 case Ctrl_K:
2273#ifdef FEAT_MOUSE
2274 ignore_drag_release = TRUE;
2275#endif
2276 putcmdline('?', TRUE);
2277#ifdef USE_ON_FLY_SCROLL
2278 dont_scroll = TRUE; /* disallow scrolling here */
2279#endif
2280 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002281 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 if (c != NUL)
2283 break;
2284
2285 redrawcmd();
2286 goto cmdline_not_changed;
2287#endif /* FEAT_DIGRAPHS */
2288
2289#ifdef FEAT_RIGHTLEFT
2290 case Ctrl__: /* CTRL-_: switch language mode */
2291 if (!p_ari)
2292 break;
Bram Moolenaar14184a32019-02-16 15:10:30 +01002293 cmd_hkmap = !cmd_hkmap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294 goto cmdline_not_changed;
2295#endif
2296
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002297 case K_PS:
2298 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2299 goto cmdline_changed;
2300
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 default:
2302#ifdef UNIX
2303 if (c == intr_char)
2304 {
2305 gotesc = TRUE; /* will free ccline.cmdbuff after
2306 putting it in history */
2307 goto returncmd; /* back to Normal mode */
2308 }
2309#endif
2310 /*
2311 * Normal character with no special meaning. Just set mod_mask
2312 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2313 * the string <S-Space>. This should only happen after ^V.
2314 */
2315 if (!IS_SPECIAL(c))
2316 mod_mask = 0x0;
2317 break;
2318 }
2319 /*
2320 * End of switch on command line character.
2321 * We come here if we have a normal character.
2322 */
2323
Bram Moolenaar13505972019-01-24 15:04:48 +01002324 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c))
2325 && (ccheck_abbr(
2326 // Add ABBR_OFF for characters above 0x100, this is
2327 // what check_abbr() expects.
2328 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : c)
2329 || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 goto cmdline_changed;
2331
2332 /*
2333 * put the character in the command line
2334 */
2335 if (IS_SPECIAL(c) || mod_mask != 0)
2336 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2337 else
2338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 if (has_mbyte)
2340 {
2341 j = (*mb_char2bytes)(c, IObuff);
2342 IObuff[j] = NUL; /* exclude composing chars */
2343 put_on_cmdline(IObuff, j, TRUE);
2344 }
2345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 {
2347 IObuff[0] = c;
2348 put_on_cmdline(IObuff, 1, TRUE);
2349 }
2350 }
2351 goto cmdline_changed;
2352
2353/*
2354 * This part implements incremental searches for "/" and "?"
2355 * Jump to cmdline_not_changed when a character has been read but the command
2356 * line did not change. Then we only search and redraw if something changed in
2357 * the past.
2358 * Jump to cmdline_changed when the command line did change.
2359 * (Sorry for the goto's, I know it is ugly).
2360 */
2361cmdline_not_changed:
2362#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002363 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 continue;
2365#endif
2366
2367cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002368 /* Trigger CmdlineChanged autocommands. */
2369 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002370
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002372 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373#endif
2374
2375#ifdef FEAT_RIGHTLEFT
2376 if (cmdmsg_rl
2377# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002378 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379# endif
2380 )
2381 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002382 * right-left typing. Not efficient, but it works.
2383 * Do it only when there are no characters left to read
2384 * to avoid useless intermediate redraws. */
2385 if (vpeekc() == NUL)
2386 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387#endif
2388 }
2389
2390returncmd:
2391
2392#ifdef FEAT_RIGHTLEFT
2393 cmdmsg_rl = FALSE;
2394#endif
2395
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002397 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002400 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401#endif
2402
2403 if (ccline.cmdbuff != NULL)
2404 {
2405 /*
2406 * Put line in history buffer (":" and "=" only when it was typed).
2407 */
2408#ifdef FEAT_CMDHIST
2409 if (ccline.cmdlen && firstc != NUL
2410 && (some_key_typed || histype == HIST_SEARCH))
2411 {
2412 add_to_history(histype, ccline.cmdbuff, TRUE,
2413 histype == HIST_SEARCH ? firstc : NUL);
2414 if (firstc == ':')
2415 {
2416 vim_free(new_last_cmdline);
2417 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2418 }
2419 }
2420#endif
2421
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002422 if (gotesc)
2423 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
2425
2426 /*
2427 * If the screen was shifted up, redraw the whole screen (later).
2428 * If the line is too long, clear it, so ruler and shown command do
2429 * not get printed in the middle of it.
2430 */
2431 msg_check();
2432 msg_scroll = save_msg_scroll;
2433 redir_off = FALSE;
2434
2435 /* When the command line was typed, no need for a wait-return prompt. */
2436 if (some_key_typed)
2437 need_wait_return = FALSE;
2438
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002439 /* Trigger CmdlineLeave autocommands. */
2440 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002441
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002443#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2445 im_save_status(b_im_ptr);
2446 im_set_active(FALSE);
2447#endif
2448#ifdef FEAT_MOUSE
2449 setmouse();
2450#endif
2451#ifdef CURSOR_SHAPE
2452 ui_cursor_shape(); /* may show different cursor shape */
2453#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002454 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455
Bram Moolenaar438d1762018-09-30 17:11:48 +02002456theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002457 {
2458 char_u *p = ccline.cmdbuff;
2459
Bram Moolenaar438d1762018-09-30 17:11:48 +02002460 if (did_save_ccline)
2461 restore_cmdline(&save_ccline);
2462 else
2463 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002464 return p;
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466}
2467
2468#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2469/*
2470 * Get a command line with a prompt.
2471 * This is prepared to be called recursively from getcmdline() (e.g. by
2472 * f_input() when evaluating an expression from CTRL-R =).
2473 * Returns the command line in allocated memory, or NULL.
2474 */
2475 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002476getcmdline_prompt(
2477 int firstc,
2478 char_u *prompt, /* command line prompt */
2479 int attr, /* attributes for prompt */
2480 int xp_context, /* type of expansion */
2481 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 char_u *s;
2484 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002485 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002487 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaar438d1762018-09-30 17:11:48 +02002489 if (ccline.cmdbuff != NULL)
2490 {
2491 // Save the values of the current cmdline and restore them below.
2492 save_cmdline(&save_ccline);
2493 did_save_ccline = TRUE;
2494 }
2495
2496 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 ccline.cmdprompt = prompt;
2498 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002499# ifdef FEAT_EVAL
2500 ccline.xp_context = xp_context;
2501 ccline.xp_arg = xp_arg;
2502 ccline.input_fn = (firstc == '@');
2503# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002504 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002505 s = getcmdline_int(firstc, 1L, 0, FALSE);
2506
2507 if (did_save_ccline)
2508 restore_cmdline(&save_ccline);
2509
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002510 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002511 /* Restore msg_col, the prompt from input() may have changed it.
2512 * But only if called recursively and the commandline is therefore being
2513 * restored to an old one; if not, the input() prompt stays on the screen,
2514 * so we need its modified msg_col left intact. */
2515 if (ccline.cmdbuff != NULL)
2516 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 return s;
2519}
2520#endif
2521
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002522/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002523 * Return TRUE when the text must not be changed and we can't switch to
2524 * another window or buffer. Used when editing the command line, evaluating
2525 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002526 */
2527 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002528text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002529{
2530#ifdef FEAT_CMDWIN
2531 if (cmdwin_type != 0)
2532 return TRUE;
2533#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002534 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002535}
2536
2537/*
2538 * Give an error message for a command that isn't allowed while the cmdline
2539 * window is open or editing the cmdline in another way.
2540 */
2541 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002542text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002543{
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002544 emsg(_(get_text_locked_msg()));
Bram Moolenaar5a497892016-09-03 16:29:04 +02002545}
2546
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002547 char *
Bram Moolenaar5a497892016-09-03 16:29:04 +02002548get_text_locked_msg(void)
2549{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002550#ifdef FEAT_CMDWIN
2551 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002552 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002553#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002554 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002555}
2556
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002557/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002558 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2559 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002560 */
2561 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002562curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002563{
2564 if (curbuf_lock > 0)
2565 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002566 emsg(_("E788: Not allowed to edit another buffer now"));
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002567 return TRUE;
2568 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002569 return allbuf_locked();
2570}
2571
2572/*
2573 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2574 * message.
2575 */
2576 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002577allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002578{
2579 if (allbuf_lock > 0)
2580 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002581 emsg(_("E811: Not allowed to change buffer information now"));
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002582 return TRUE;
2583 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002584 return FALSE;
2585}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002586
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002588cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2591 if (cmdline_star > 0) /* showing '*', always 1 position */
2592 return 1;
2593#endif
2594 return ptr2cells(ccline.cmdbuff + idx);
2595}
2596
2597/*
2598 * Compute the offset of the cursor on the command line for the prompt and
2599 * indent.
2600 */
2601 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002602set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002604 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 ccline.cmdspos = 1 + ccline.cmdindent;
2606 else
2607 ccline.cmdspos = 0 + ccline.cmdindent;
2608}
2609
2610/*
2611 * Compute the screen position for the cursor on the command line.
2612 */
2613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
2616 int i, m, c;
2617
2618 set_cmdspos();
2619 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002620 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002622 if (m < 0) /* overflow, Columns or Rows at weird value */
2623 m = MAXCOL;
2624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 else
2626 m = MAXCOL;
2627 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2628 {
2629 c = cmdline_charsize(i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2631 if (has_mbyte)
2632 correct_cmdspos(i, c);
Bram Moolenaarf9821062008-06-20 16:31:07 +00002633 /* If the cmdline doesn't fit, show cursor on last visible char.
2634 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if ((ccline.cmdspos += c) >= m)
2636 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 ccline.cmdspos -= c;
2638 break;
2639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002641 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643}
2644
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645/*
2646 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2647 * character that doesn't fit, so that a ">" must be displayed.
2648 */
2649 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002650correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002652 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2654 && ccline.cmdspos % Columns + cells > Columns)
2655 ccline.cmdspos++;
2656}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657
2658/*
2659 * Get an Ex command line for the ":" command.
2660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002662getexline(
2663 int c, /* normally ':', NUL for ":append" */
2664 void *cookie UNUSED,
2665 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 /* When executing a register, remove ':' that's in front of each line. */
2668 if (exec_from_reg && vpeekc() == ':')
2669 (void)vgetc();
2670 return getcmdline(c, 1L, indent);
2671}
2672
2673/*
2674 * Get an Ex command line for Ex mode.
2675 * In Ex mode we only use the OS supplied line editing features and no
2676 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002677 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002680getexmodeline(
2681 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002682 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002683 void *cookie UNUSED,
2684 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 garray_T line_ga;
2687 char_u *pend;
2688 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002689 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002690 int escaped = FALSE; /* CTRL-V typed */
2691 int vcol = 0;
2692 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002693 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002694 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695
2696 /* Switch cursor on now. This avoids that it happens after the "\n", which
2697 * confuses the system function that computes tabstops. */
2698 cursor_on();
2699
2700 /* always start in column 0; write a newline if necessary */
2701 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002702 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002706 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002707 if (p_prompt)
2708 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 while (indent-- > 0)
2710 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713
2714 ga_init2(&line_ga, 1, 30);
2715
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002716 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002717 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002718 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002719 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002720 while (indent >= 8)
2721 {
2722 ga_append(&line_ga, TAB);
Bram Moolenaar32526b32019-01-19 17:43:09 +01002723 msg_puts(" ");
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002724 indent -= 8;
2725 }
2726 while (indent-- > 0)
2727 {
2728 ga_append(&line_ga, ' ');
2729 msg_putchar(' ');
2730 }
2731 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002732 ++no_mapping;
2733 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002734
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 /*
2736 * Get the line, one character at a time.
2737 */
2738 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002739 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002741 long sw;
2742 char_u *s;
2743
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (ga_grow(&line_ga, 40) == FAIL)
2745 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
Bram Moolenaarba748c82017-02-26 14:00:07 +01002747 /*
2748 * Get one character at a time.
2749 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002750 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002751
2752 /* Check for a ":normal" command and no more characters left. */
2753 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2754 c1 = '\n';
2755 else
2756 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757
2758 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002759 * Handle line editing.
2760 * Previously this was left to the system, putting the terminal in
2761 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002765 msg_putchar('\n');
2766 break;
2767 }
2768
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002769 if (c1 == K_PS)
2770 {
2771 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2772 goto redraw;
2773 }
2774
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002775 if (!escaped)
2776 {
2777 /* CR typed means "enter", which is NL */
2778 if (c1 == '\r')
2779 c1 = '\n';
2780
2781 if (c1 == BS || c1 == K_BS
2782 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002784 if (line_ga.ga_len > 0)
2785 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002786 if (has_mbyte)
2787 {
2788 p = (char_u *)line_ga.ga_data;
2789 p[line_ga.ga_len] = NUL;
2790 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2791 line_ga.ga_len -= len;
2792 }
2793 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002794 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 goto redraw;
2796 }
2797 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 }
2799
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 msg_col = startcol;
2803 msg_clr_eos();
2804 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002805 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806 }
2807
2808 if (c1 == Ctrl_T)
2809 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002810 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 p = (char_u *)line_ga.ga_data;
2812 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002813 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002814 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002815add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002816 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002818 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002819 p = (char_u *)line_ga.ga_data;
2820 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002821 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2822 *s = ' ';
2823 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002825redraw:
2826 /* redraw the line */
2827 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002828 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002829 p = (char_u *)line_ga.ga_data;
2830 p[line_ga.ga_len] = NUL;
2831 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002833 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002836 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002837 msg_putchar(' ');
2838 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002839 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002843 len = MB_PTR2LEN(p);
2844 msg_outtrans_len(p, len);
2845 vcol += ptr2cells(p);
2846 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 }
2848 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002849 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002850 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002851 continue;
2852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 if (c1 == Ctrl_D)
2855 {
2856 /* Delete one shiftwidth. */
2857 p = (char_u *)line_ga.ga_data;
2858 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002860 if (prev_char == '^')
2861 ex_keep_indent = TRUE;
2862 indent = 0;
2863 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865 else
2866 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002867 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002868 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002869 if (indent > 0)
2870 {
2871 --indent;
2872 indent -= indent % get_sw_value(curbuf);
2873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002875 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002876 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002877 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2879 --line_ga.ga_len;
2880 }
2881 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883
2884 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2885 {
2886 escaped = TRUE;
2887 continue;
2888 }
2889
2890 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2891 if (IS_SPECIAL(c1))
2892 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002894
2895 if (IS_SPECIAL(c1))
2896 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002897 if (has_mbyte)
2898 len = (*mb_char2bytes)(c1,
2899 (char_u *)line_ga.ga_data + line_ga.ga_len);
2900 else
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002901 {
2902 len = 1;
2903 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2904 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905 if (c1 == '\n')
2906 msg_putchar('\n');
2907 else if (c1 == TAB)
2908 {
2909 /* Don't use chartabsize(), 'ts' can be different */
2910 do
2911 {
2912 msg_putchar(' ');
2913 } while (++vcol % 8);
2914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002917 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002918 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002919 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002921 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002922 escaped = FALSE;
2923
2924 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002925 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002926
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002927 /* We are done when a NL is entered, but not when it comes after an
2928 * odd number of backslashes, that results in a NUL. */
2929 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002931 int bcount = 0;
2932
2933 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2934 ++bcount;
2935
2936 if (bcount > 0)
2937 {
2938 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2939 * "\NL", etc. */
2940 line_ga.ga_len -= (bcount + 1) / 2;
2941 pend -= (bcount + 1) / 2;
2942 pend[-1] = '\n';
2943 }
2944
2945 if ((bcount & 1) == 0)
2946 {
2947 --line_ga.ga_len;
2948 --pend;
2949 *pend = NUL;
2950 break;
2951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952 }
2953 }
2954
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002955 --no_mapping;
2956 --allow_keys;
2957
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958 /* make following messages go to the next line */
2959 msg_didout = FALSE;
2960 msg_col = 0;
2961 if (msg_row < Rows - 1)
2962 ++msg_row;
2963 emsg_on_display = FALSE; /* don't want ui_delay() */
2964
2965 if (got_int)
2966 ga_clear(&line_ga);
2967
2968 return (char_u *)line_ga.ga_data;
2969}
2970
Bram Moolenaare344bea2005-09-01 20:46:49 +00002971# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2972 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973/*
2974 * Return TRUE if ccline.overstrike is on.
2975 */
2976 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002977cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978{
2979 return ccline.overstrike;
2980}
2981
2982/*
2983 * Return TRUE if the cursor is at the end of the cmdline.
2984 */
2985 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002986cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987{
2988 return (ccline.cmdpos >= ccline.cmdlen);
2989}
2990#endif
2991
Bram Moolenaar9372a112005-12-06 19:59:18 +00002992#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993/*
2994 * Return the virtual column number at the current cursor position.
2995 * This is used by the IM code to obtain the start of the preedit string.
2996 */
2997 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002998cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999{
3000 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3001 return MAXCOL;
3002
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003 if (has_mbyte)
3004 {
3005 colnr_T col;
3006 int i = 0;
3007
3008 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003009 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010
3011 return col;
3012 }
3013 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 return ccline.cmdpos;
3015}
3016#endif
3017
3018#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3019/*
3020 * If part of the command line is an IM preedit string, redraw it with
3021 * IM feedback attributes. The cursor position is restored after drawing.
3022 */
3023 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003024redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
3026 if ((State & CMDLINE)
3027 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003028 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 && !p_imdisable
3030 && im_is_preediting())
3031 {
3032 int cmdpos = 0;
3033 int cmdspos;
3034 int old_row;
3035 int old_col;
3036 colnr_T col;
3037
3038 old_row = msg_row;
3039 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003040 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 if (has_mbyte)
3043 {
3044 for (col = 0; col < preedit_start_col
3045 && cmdpos < ccline.cmdlen; ++col)
3046 {
3047 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003048 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 }
3050 }
3051 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 {
3053 cmdspos += preedit_start_col;
3054 cmdpos += preedit_start_col;
3055 }
3056
3057 msg_row = cmdline_row + (cmdspos / (int)Columns);
3058 msg_col = cmdspos % (int)Columns;
3059 if (msg_row >= Rows)
3060 msg_row = Rows - 1;
3061
3062 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3063 {
3064 int char_len;
3065 int char_attr;
3066
3067 char_attr = im_get_feedback_attr(col);
3068 if (char_attr < 0)
3069 break; /* end of preedit string */
3070
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003072 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 char_len = 1;
3075
3076 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3077 cmdpos += char_len;
3078 }
3079
3080 msg_row = old_row;
3081 msg_col = old_col;
3082 }
3083}
3084#endif /* FEAT_XIM && FEAT_GUI_GTK */
3085
3086/*
3087 * Allocate a new command line buffer.
3088 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 */
3090 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003091alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092{
3093 /*
3094 * give some extra space to avoid having to allocate all the time
3095 */
3096 if (len < 80)
3097 len = 100;
3098 else
3099 len += 20;
3100
3101 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3102 ccline.cmdbufflen = len;
3103}
3104
3105/*
3106 * Re-allocate the command line to length len + something extra.
3107 * return FAIL for failure, OK otherwise
3108 */
3109 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003110realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
3112 char_u *p;
3113
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003114 if (len < ccline.cmdbufflen)
3115 return OK; /* no need to resize */
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 p = ccline.cmdbuff;
3118 alloc_cmdbuff(len); /* will get some more */
3119 if (ccline.cmdbuff == NULL) /* out of memory */
3120 {
3121 ccline.cmdbuff = p; /* keep the old one */
3122 return FAIL;
3123 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003124 /* There isn't always a NUL after the command, but it may need to be
3125 * there, thus copy up to the NUL and add a NUL. */
3126 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3127 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003129
3130 if (ccline.xpc != NULL
3131 && ccline.xpc->xp_pattern != NULL
3132 && ccline.xpc->xp_context != EXPAND_NOTHING
3133 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3134 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003135 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003136
3137 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3138 * to point into the newly allocated memory. */
3139 if (i >= 0 && i <= ccline.cmdlen)
3140 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3141 }
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 return OK;
3144}
3145
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003146#if defined(FEAT_ARABIC) || defined(PROTO)
3147static char_u *arshape_buf = NULL;
3148
3149# if defined(EXITFREE) || defined(PROTO)
3150 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003151free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003152{
3153 vim_free(arshape_buf);
3154}
3155# endif
3156#endif
3157
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158/*
3159 * Draw part of the cmdline at the current cursor position. But draw stars
3160 * when cmdline_star is TRUE.
3161 */
3162 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003163draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164{
3165#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3166 int i;
3167
3168 if (cmdline_star > 0)
3169 for (i = 0; i < len; ++i)
3170 {
3171 msg_putchar('*');
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003173 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 }
3175 else
3176#endif
3177#ifdef FEAT_ARABIC
3178 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3179 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 static int buflen = 0;
3181 char_u *p;
3182 int j;
3183 int newlen = 0;
3184 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003185 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 int prev_c = 0;
3187 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003188 int u8c;
3189 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191
3192 /*
3193 * Do arabic shaping into a temporary buffer. This is very
3194 * inefficient!
3195 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003196 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 {
3198 /* Re-allocate the buffer. We keep it around to avoid a lot of
3199 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003201 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 arshape_buf = alloc(buflen);
3203 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 return; /* out of memory */
3205 }
3206
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003207 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3208 {
3209 /* Prepend a space to draw the leading composing char on. */
3210 arshape_buf[0] = ' ';
3211 newlen = 1;
3212 }
3213
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 for (j = start; j < start + len; j += mb_l)
3215 {
3216 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003217 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003218 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 if (ARABIC_CHAR(u8c))
3220 {
3221 /* Do Arabic shaping. */
3222 if (cmdmsg_rl)
3223 {
3224 /* displaying from right to left */
3225 pc = prev_c;
3226 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003227 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 if (j + mb_l >= start + len)
3229 nc = NUL;
3230 else
3231 nc = utf_ptr2char(p + mb_l);
3232 }
3233 else
3234 {
3235 /* displaying from left to right */
3236 if (j + mb_l >= start + len)
3237 pc = NUL;
3238 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003239 {
3240 int pcc[MAX_MCO];
3241
3242 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003244 pc1 = pcc[0];
3245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 nc = prev_c;
3247 }
3248 prev_c = u8c;
3249
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003250 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003252 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003253 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003255 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3256 if (u8cc[1] != 0)
3257 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 }
3260 }
3261 else
3262 {
3263 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003264 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 newlen += mb_l;
3266 }
3267 }
3268
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003269 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 }
3271 else
3272#endif
3273 msg_outtrans_len(ccline.cmdbuff + start, len);
3274}
3275
3276/*
3277 * Put a character on the command line. Shifts the following text to the
3278 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3279 * "c" must be printable (fit in one display cell)!
3280 */
3281 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003282putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 if (cmd_silent)
3285 return;
3286 msg_no_more = TRUE;
3287 msg_putchar(c);
3288 if (shift)
3289 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3290 msg_no_more = FALSE;
3291 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003292 extra_char = c;
3293 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294}
3295
3296/*
3297 * Undo a putcmdline(c, FALSE).
3298 */
3299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003300unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301{
3302 if (cmd_silent)
3303 return;
3304 msg_no_more = TRUE;
3305 if (ccline.cmdlen == ccline.cmdpos)
3306 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003307 else if (has_mbyte)
3308 draw_cmdline(ccline.cmdpos,
3309 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 else
3311 draw_cmdline(ccline.cmdpos, 1);
3312 msg_no_more = FALSE;
3313 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003314 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315}
3316
3317/*
3318 * Put the given string, of the given length, onto the command line.
3319 * If len is -1, then STRLEN() is used to calculate the length.
3320 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3321 * part will be redrawn, otherwise it will not. If this function is called
3322 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3323 * called afterwards.
3324 */
3325 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003326put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327{
3328 int retval;
3329 int i;
3330 int m;
3331 int c;
3332
3333 if (len < 0)
3334 len = (int)STRLEN(str);
3335
3336 /* Check if ccline.cmdbuff needs to be longer */
3337 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003338 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339 else
3340 retval = OK;
3341 if (retval == OK)
3342 {
3343 if (!ccline.overstrike)
3344 {
3345 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3346 ccline.cmdbuff + ccline.cmdpos,
3347 (size_t)(ccline.cmdlen - ccline.cmdpos));
3348 ccline.cmdlen += len;
3349 }
3350 else
3351 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 if (has_mbyte)
3353 {
3354 /* Count nr of characters in the new string. */
3355 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003356 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 ++m;
3358 /* Count nr of bytes in cmdline that are overwritten by these
3359 * characters. */
3360 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003361 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 --m;
3363 if (i < ccline.cmdlen)
3364 {
3365 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3366 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3367 ccline.cmdlen += ccline.cmdpos + len - i;
3368 }
3369 else
3370 ccline.cmdlen = ccline.cmdpos + len;
3371 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003372 else if (ccline.cmdpos + len > ccline.cmdlen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 ccline.cmdlen = ccline.cmdpos + len;
3374 }
3375 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3376 ccline.cmdbuff[ccline.cmdlen] = NUL;
3377
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 if (enc_utf8)
3379 {
3380 /* When the inserted text starts with a composing character,
3381 * backup to the character before it. There could be two of them.
3382 */
3383 i = 0;
3384 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3385 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3386 {
3387 i = (*mb_head_off)(ccline.cmdbuff,
3388 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3389 ccline.cmdpos -= i;
3390 len += i;
3391 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3392 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003393#ifdef FEAT_ARABIC
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3395 {
3396 /* Check the previous character for Arabic combining pair. */
3397 i = (*mb_head_off)(ccline.cmdbuff,
3398 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3399 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3400 + ccline.cmdpos - i), c))
3401 {
3402 ccline.cmdpos -= i;
3403 len += i;
3404 }
3405 else
3406 i = 0;
3407 }
Bram Moolenaar13505972019-01-24 15:04:48 +01003408#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 if (i != 0)
3410 {
3411 /* Also backup the cursor position. */
3412 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3413 ccline.cmdspos -= i;
3414 msg_col -= i;
3415 if (msg_col < 0)
3416 {
3417 msg_col += Columns;
3418 --msg_row;
3419 }
3420 }
3421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422
3423 if (redraw && !cmd_silent)
3424 {
3425 msg_no_more = TRUE;
3426 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003427 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3429 /* Avoid clearing the rest of the line too often. */
3430 if (cmdline_row != i || ccline.overstrike)
3431 msg_clr_eos();
3432 msg_no_more = FALSE;
3433 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003434 if (KeyTyped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 {
Bram Moolenaar14184a32019-02-16 15:10:30 +01003436 m = Columns * Rows;
3437 if (m < 0) /* overflow, Columns or Rows at weird value */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 m = MAXCOL;
Bram Moolenaar14184a32019-02-16 15:10:30 +01003439 }
3440 else
3441 m = MAXCOL;
3442 for (i = 0; i < len; ++i)
3443 {
3444 c = cmdline_charsize(ccline.cmdpos);
3445 /* count ">" for a double-wide char that doesn't fit. */
3446 if (has_mbyte)
3447 correct_cmdspos(ccline.cmdpos, c);
3448 /* Stop cursor at the end of the screen, but do increment the
3449 * insert position, so that entering a very long command
3450 * works, even though you can't see it. */
3451 if (ccline.cmdspos + c < m)
3452 ccline.cmdspos += c;
Bram Moolenaar13505972019-01-24 15:04:48 +01003453
Bram Moolenaar14184a32019-02-16 15:10:30 +01003454 if (has_mbyte)
3455 {
3456 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
3457 if (c > len - i - 1)
3458 c = len - i - 1;
3459 ccline.cmdpos += c;
3460 i += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
Bram Moolenaar14184a32019-02-16 15:10:30 +01003462 ++ccline.cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 }
3464 }
3465 if (redraw)
3466 msg_check();
3467 return retval;
3468}
3469
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003470static struct cmdline_info prev_ccline;
3471static int prev_ccline_used = FALSE;
3472
3473/*
3474 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3475 * and overwrite it. But get_cmdline_str() may need it, thus make it
3476 * available globally in prev_ccline.
3477 */
3478 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003479save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003480{
3481 if (!prev_ccline_used)
3482 {
3483 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3484 prev_ccline_used = TRUE;
3485 }
3486 *ccp = prev_ccline;
3487 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003488 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003489}
3490
3491/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003492 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003493 */
3494 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003495restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003496{
3497 ccline = prev_ccline;
3498 prev_ccline = *ccp;
3499}
3500
Bram Moolenaar8299df92004-07-10 09:47:34 +00003501/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003502 * Paste a yank register into the command line.
3503 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003504 * insert_reg() can't be used here, because special characters from the
3505 * register contents will be interpreted as commands.
3506 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003507 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003508 */
3509 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003510cmdline_paste(
3511 int regname,
3512 int literally, /* Insert text literally instead of "as typed" */
3513 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003514{
3515 long i;
3516 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003517 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003518 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003519
3520 /* check for valid regname; also accept special characters for CTRL-R in
3521 * the command line */
3522 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003523 && regname != Ctrl_A && regname != Ctrl_L
3524 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003525 return FAIL;
3526
3527 /* A register containing CTRL-R can cause an endless loop. Allow using
3528 * CTRL-C to break the loop. */
3529 line_breakcheck();
3530 if (got_int)
3531 return FAIL;
3532
3533#ifdef FEAT_CLIPBOARD
3534 regname = may_get_selection(regname);
3535#endif
3536
Bram Moolenaar438d1762018-09-30 17:11:48 +02003537 // Need to set "textlock" to avoid nasty things like going to another
3538 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003539 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003540 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003541 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003542
3543 if (i)
3544 {
3545 /* Got the value of a special register in "arg". */
3546 if (arg == NULL)
3547 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003548
3549 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3550 * part of the word. */
3551 p = arg;
3552 if (p_is && regname == Ctrl_W)
3553 {
3554 char_u *w;
3555 int len;
3556
3557 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003558 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003559 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003560 if (has_mbyte)
3561 {
3562 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3563 if (!vim_iswordc(mb_ptr2char(w - len)))
3564 break;
3565 w -= len;
3566 }
3567 else
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003568 {
3569 if (!vim_iswordc(w[-1]))
3570 break;
3571 --w;
3572 }
3573 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003574 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003575 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3576 p += len;
3577 }
3578
3579 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003580 if (allocated)
3581 vim_free(arg);
3582 return OK;
3583 }
3584
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003585 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003586}
3587
3588/*
3589 * Put a string on the command line.
3590 * When "literally" is TRUE, insert literally.
3591 * When "literally" is FALSE, insert as typed, but don't leave the command
3592 * line.
3593 */
3594 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003595cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596{
3597 int c, cv;
3598
3599 if (literally)
3600 put_on_cmdline(s, -1, TRUE);
3601 else
3602 while (*s != NUL)
3603 {
3604 cv = *s;
3605 if (cv == Ctrl_V && s[1])
3606 ++s;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003607 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003608 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003609 else
Bram Moolenaar8299df92004-07-10 09:47:34 +00003610 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003611 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3612 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003613#ifdef UNIX
3614 || c == intr_char
3615#endif
3616 || (c == Ctrl_BSL && *s == Ctrl_N))
3617 stuffcharReadbuff(Ctrl_V);
3618 stuffcharReadbuff(c);
3619 }
3620}
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#ifdef FEAT_WILDMENU
3623/*
3624 * Delete characters on the command line, from "from" to the current
3625 * position.
3626 */
3627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003628cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629{
3630 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3631 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3632 ccline.cmdlen -= ccline.cmdpos - from;
3633 ccline.cmdpos = from;
3634}
3635#endif
3636
3637/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003638 * This function is called when the screen size changes and with incremental
3639 * search and in other situations where the command line may have been
3640 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 */
3642 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003643redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003645 redrawcmdline_ex(TRUE);
3646}
3647
3648 void
3649redrawcmdline_ex(int do_compute_cmdrow)
3650{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 if (cmd_silent)
3652 return;
3653 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003654 if (do_compute_cmdrow)
3655 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 redrawcmd();
3657 cursorcmd();
3658}
3659
3660 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003661redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662{
3663 int i;
3664
3665 if (cmd_silent)
3666 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003667 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 msg_putchar(ccline.cmdfirstc);
3669 if (ccline.cmdprompt != NULL)
3670 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01003671 msg_puts_attr((char *)ccline.cmdprompt, ccline.cmdattr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3673 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003674 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 --ccline.cmdindent;
3676 }
3677 else
3678 for (i = ccline.cmdindent; i > 0; --i)
3679 msg_putchar(' ');
3680}
3681
3682/*
3683 * Redraw what is currently on the command line.
3684 */
3685 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003686redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687{
3688 if (cmd_silent)
3689 return;
3690
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003691 /* when 'incsearch' is set there may be no command line while redrawing */
3692 if (ccline.cmdbuff == NULL)
3693 {
3694 windgoto(cmdline_row, 0);
3695 msg_clr_eos();
3696 return;
3697 }
3698
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 msg_start();
3700 redrawcmdprompt();
3701
3702 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3703 msg_no_more = TRUE;
3704 draw_cmdline(0, ccline.cmdlen);
3705 msg_clr_eos();
3706 msg_no_more = FALSE;
3707
3708 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003709 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003710 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711
3712 /*
3713 * An emsg() before may have set msg_scroll. This is used in normal mode,
3714 * in cmdline mode we can reset them now.
3715 */
3716 msg_scroll = FALSE; /* next message overwrites cmdline */
3717
3718 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3719 * in cmdline mode */
3720 skip_redraw = FALSE;
3721}
3722
3723 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003724compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003726 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 cmdline_row = Rows - 1;
3728 else
3729 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003730 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731}
3732
3733 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003734cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
3736 if (cmd_silent)
3737 return;
3738
3739#ifdef FEAT_RIGHTLEFT
3740 if (cmdmsg_rl)
3741 {
3742 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3743 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3744 if (msg_row <= 0)
3745 msg_row = Rows - 1;
3746 }
3747 else
3748#endif
3749 {
3750 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3751 msg_col = ccline.cmdspos % (int)Columns;
3752 if (msg_row >= Rows)
3753 msg_row = Rows - 1;
3754 }
3755
3756 windgoto(msg_row, msg_col);
3757#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003758 if (p_imst == IM_ON_THE_SPOT)
3759 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760#endif
3761#ifdef MCH_CURSOR_SHAPE
3762 mch_update_cursor();
3763#endif
3764}
3765
3766 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003767gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768{
3769 msg_start();
3770#ifdef FEAT_RIGHTLEFT
3771 if (cmdmsg_rl)
3772 msg_col = Columns - 1;
3773 else
3774#endif
3775 msg_col = 0; /* always start in column 0 */
3776 if (clr) /* clear the bottom line(s) */
3777 msg_clr_eos(); /* will reset clear_cmdline */
3778 windgoto(cmdline_row, 0);
3779}
3780
3781/*
3782 * Check the word in front of the cursor for an abbreviation.
3783 * Called when the non-id character "c" has been entered.
3784 * When an abbreviation is recognized it is removed from the text with
3785 * backspaces and the replacement string is inserted, followed by "c".
3786 */
3787 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003788ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003790 int spos = 0;
3791
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3793 return FALSE;
3794
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003795 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3796 * Actually accepts any mark. */
3797 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3798 spos++;
3799 if (ccline.cmdlen - spos > 5
3800 && ccline.cmdbuff[spos] == '\''
3801 && ccline.cmdbuff[spos + 2] == ','
3802 && ccline.cmdbuff[spos + 3] == '\'')
3803 spos += 5;
3804 else
3805 /* check abbreviation from the beginning of the commandline */
3806 spos = 0;
3807
3808 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809}
3810
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003811#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3812 static int
3813#ifdef __BORLANDC__
3814_RTLENTRYF
3815#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003816sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003817{
3818 char_u *p1 = *(char_u **)s1;
3819 char_u *p2 = *(char_u **)s2;
3820
3821 if (*p1 != '<' && *p2 == '<') return -1;
3822 if (*p1 == '<' && *p2 != '<') return 1;
3823 return STRCMP(p1, p2);
3824}
3825#endif
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827/*
3828 * Return FAIL if this is not an appropriate context in which to do
3829 * completion of anything, return OK if it is (even if there are no matches).
3830 * For the caller, this means that the character is just passed through like a
3831 * normal character (instead of being expanded). This allows :s/^I^D etc.
3832 */
3833 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003834nextwild(
3835 expand_T *xp,
3836 int type,
3837 int options, /* extra options for ExpandOne() */
3838 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839{
3840 int i, j;
3841 char_u *p1;
3842 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 int difflen;
3844 int v;
3845
3846 if (xp->xp_numfiles == -1)
3847 {
3848 set_expand_context(xp);
3849 cmd_showtail = expand_showtail(xp);
3850 }
3851
3852 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3853 {
3854 beep_flush();
3855 return OK; /* Something illegal on command line */
3856 }
3857 if (xp->xp_context == EXPAND_NOTHING)
3858 {
3859 /* Caller can use the character as a normal char instead */
3860 return FAIL;
3861 }
3862
Bram Moolenaar32526b32019-01-19 17:43:09 +01003863 msg_puts("..."); /* show that we are busy */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 out_flush();
3865
3866 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003867 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868
3869 if (type == WILD_NEXT || type == WILD_PREV)
3870 {
3871 /*
3872 * Get next/previous match for a previous expanded pattern.
3873 */
3874 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3875 }
3876 else
3877 {
3878 /*
3879 * Translate string into pattern and expand it.
3880 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003881 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3882 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 p2 = NULL;
3884 else
3885 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003886 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003887 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3888 if (escape)
3889 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003890
3891 if (p_wic)
3892 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003893 p2 = ExpandOne(xp, p1,
3894 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003895 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003897 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 if (p2 != NULL && type == WILD_LONGEST)
3899 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003900 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (ccline.cmdbuff[i + j] == '*'
3902 || ccline.cmdbuff[i + j] == '?')
3903 break;
3904 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003905 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 }
3907 }
3908 }
3909
3910 if (p2 != NULL && !got_int)
3911 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003912 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003913 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003915 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 xp->xp_pattern = ccline.cmdbuff + i;
3917 }
3918 else
3919 v = OK;
3920 if (v == OK)
3921 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003922 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3923 &ccline.cmdbuff[ccline.cmdpos],
3924 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3925 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 ccline.cmdlen += difflen;
3927 ccline.cmdpos += difflen;
3928 }
3929 }
3930 vim_free(p2);
3931
3932 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003933 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934
3935 /* When expanding a ":map" command and no matches are found, assume that
3936 * the key is supposed to be inserted literally */
3937 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3938 return FAIL;
3939
3940 if (xp->xp_numfiles <= 0 && p2 == NULL)
3941 beep_flush();
3942 else if (xp->xp_numfiles == 1)
3943 /* free expanded pattern */
3944 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3945
3946 return OK;
3947}
3948
3949/*
3950 * Do wildcard expansion on the string 'str'.
3951 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003952 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 * Return NULL for failure.
3954 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003955 * "orig" is the originally expanded string, copied to allocated memory. It
3956 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3957 * WILD_PREV "orig" should be NULL.
3958 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003959 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3960 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 *
3962 * mode = WILD_FREE: just free previously expanded matches
3963 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
3964 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
3965 * mode = WILD_NEXT: use next match in multiple match, wrap to first
3966 * mode = WILD_PREV: use previous match in multiple match, wrap to first
3967 * mode = WILD_ALL: return all matches concatenated
3968 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01003969 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 *
3971 * options = WILD_LIST_NOTFOUND: list entries without a match
3972 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
3973 * options = WILD_USE_NL: Use '\n' for WILD_ALL
3974 * options = WILD_NO_BEEP: Don't beep for multiple matches
3975 * options = WILD_ADD_SLASH: add a slash after directory names
3976 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
3977 * options = WILD_SILENT: don't print warning messages
3978 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01003979 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 *
3981 * The variables xp->xp_context and xp->xp_backslash must have been set!
3982 */
3983 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003984ExpandOne(
3985 expand_T *xp,
3986 char_u *str,
3987 char_u *orig, /* allocated copy of original of expanded string */
3988 int options,
3989 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990{
3991 char_u *ss = NULL;
3992 static int findex;
3993 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00003994 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 int i;
3996 long_u len;
3997 int non_suf_match; /* number without matching suffix */
3998
3999 /*
4000 * first handle the case of using an old match
4001 */
4002 if (mode == WILD_NEXT || mode == WILD_PREV)
4003 {
4004 if (xp->xp_numfiles > 0)
4005 {
4006 if (mode == WILD_PREV)
4007 {
4008 if (findex == -1)
4009 findex = xp->xp_numfiles;
4010 --findex;
4011 }
4012 else /* mode == WILD_NEXT */
4013 ++findex;
4014
4015 /*
4016 * When wrapping around, return the original string, set findex to
4017 * -1.
4018 */
4019 if (findex < 0)
4020 {
4021 if (orig_save == NULL)
4022 findex = xp->xp_numfiles - 1;
4023 else
4024 findex = -1;
4025 }
4026 if (findex >= xp->xp_numfiles)
4027 {
4028 if (orig_save == NULL)
4029 findex = 0;
4030 else
4031 findex = -1;
4032 }
4033#ifdef FEAT_WILDMENU
4034 if (p_wmnu)
4035 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4036 findex, cmd_showtail);
4037#endif
4038 if (findex == -1)
4039 return vim_strsave(orig_save);
4040 return vim_strsave(xp->xp_files[findex]);
4041 }
4042 else
4043 return NULL;
4044 }
4045
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004046 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4048 {
4049 FreeWild(xp->xp_numfiles, xp->xp_files);
4050 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004051 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 }
4053 findex = 0;
4054
4055 if (mode == WILD_FREE) /* only release file name */
4056 return NULL;
4057
4058 if (xp->xp_numfiles == -1)
4059 {
4060 vim_free(orig_save);
4061 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004062 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 /*
4065 * Do the expansion.
4066 */
4067 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4068 options) == FAIL)
4069 {
4070#ifdef FNAME_ILLEGAL
4071 /* Illegal file name has been silently skipped. But when there
4072 * are wildcards, the real problem is that there was no match,
4073 * causing the pattern to be added, which has illegal characters.
4074 */
4075 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004076 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077#endif
4078 }
4079 else if (xp->xp_numfiles == 0)
4080 {
4081 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004082 semsg(_(e_nomatch2), str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 else
4085 {
4086 /* Escape the matches for use on the command line. */
4087 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4088
4089 /*
4090 * Check for matching suffixes in file names.
4091 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004092 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4093 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 {
4095 if (xp->xp_numfiles)
4096 non_suf_match = xp->xp_numfiles;
4097 else
4098 non_suf_match = 1;
4099 if ((xp->xp_context == EXPAND_FILES
4100 || xp->xp_context == EXPAND_DIRECTORIES)
4101 && xp->xp_numfiles > 1)
4102 {
4103 /*
4104 * More than one match; check suffix.
4105 * The files will have been sorted on matching suffix in
4106 * expand_wildcards, only need to check the first two.
4107 */
4108 non_suf_match = 0;
4109 for (i = 0; i < 2; ++i)
4110 if (match_suffix(xp->xp_files[i]))
4111 ++non_suf_match;
4112 }
4113 if (non_suf_match != 1)
4114 {
4115 /* Can we ever get here unless it's while expanding
4116 * interactively? If not, we can get rid of this all
4117 * together. Don't really want to wait for this message
4118 * (and possibly have to hit return to continue!).
4119 */
4120 if (!(options & WILD_SILENT))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004121 emsg(_(e_toomany));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 else if (!(options & WILD_NO_BEEP))
4123 beep_flush();
4124 }
4125 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4126 ss = vim_strsave(xp->xp_files[0]);
4127 }
4128 }
4129 }
4130
4131 /* Find longest common part */
4132 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4133 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004134 int mb_len = 1;
4135 int c0, ci;
4136
4137 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004139 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004141 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4142 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4143 }
4144 else
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004145 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004146 for (i = 1; i < xp->xp_numfiles; ++i)
4147 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004148 if (has_mbyte)
4149 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4150 else
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004151 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004152 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004154 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004155 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004157 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 break;
4159 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004160 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 break;
4162 }
4163 if (i < xp->xp_numfiles)
4164 {
4165 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004166 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 break;
4168 }
4169 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004170
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 ss = alloc((unsigned)len + 1);
4172 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004173 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 findex = -1; /* next p_wc gets first one */
4175 }
4176
4177 /* Concatenate all matching names */
4178 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4179 {
4180 len = 0;
4181 for (i = 0; i < xp->xp_numfiles; ++i)
4182 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4183 ss = lalloc(len, TRUE);
4184 if (ss != NULL)
4185 {
4186 *ss = NUL;
4187 for (i = 0; i < xp->xp_numfiles; ++i)
4188 {
4189 STRCAT(ss, xp->xp_files[i]);
4190 if (i != xp->xp_numfiles - 1)
4191 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4192 }
4193 }
4194 }
4195
4196 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4197 ExpandCleanup(xp);
4198
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004199 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004200 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004201 vim_free(orig);
4202
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 return ss;
4204}
4205
4206/*
4207 * Prepare an expand structure for use.
4208 */
4209 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004210ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004212 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004213 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004215#ifndef BACKSLASH_IN_FILENAME
4216 xp->xp_shell = FALSE;
4217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 xp->xp_numfiles = -1;
4219 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004220#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4221 xp->xp_arg = NULL;
4222#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004223 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224}
4225
4226/*
4227 * Cleanup an expand structure after use.
4228 */
4229 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004230ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231{
4232 if (xp->xp_numfiles >= 0)
4233 {
4234 FreeWild(xp->xp_numfiles, xp->xp_files);
4235 xp->xp_numfiles = -1;
4236 }
4237}
4238
4239 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004240ExpandEscape(
4241 expand_T *xp,
4242 char_u *str,
4243 int numfiles,
4244 char_u **files,
4245 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246{
4247 int i;
4248 char_u *p;
4249
4250 /*
4251 * May change home directory back to "~"
4252 */
4253 if (options & WILD_HOME_REPLACE)
4254 tilde_replace(str, numfiles, files);
4255
4256 if (options & WILD_ESCAPE)
4257 {
4258 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004259 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004260 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 || xp->xp_context == EXPAND_BUFFERS
4262 || xp->xp_context == EXPAND_DIRECTORIES)
4263 {
4264 /*
4265 * Insert a backslash into a file name before a space, \, %, #
4266 * and wildmatch characters, except '~'.
4267 */
4268 for (i = 0; i < numfiles; ++i)
4269 {
4270 /* for ":set path=" we need to escape spaces twice */
4271 if (xp->xp_backslash == XP_BS_THREE)
4272 {
4273 p = vim_strsave_escaped(files[i], (char_u *)" ");
4274 if (p != NULL)
4275 {
4276 vim_free(files[i]);
4277 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004278#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 p = vim_strsave_escaped(files[i], (char_u *)" ");
4280 if (p != NULL)
4281 {
4282 vim_free(files[i]);
4283 files[i] = p;
4284 }
4285#endif
4286 }
4287 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004288#ifdef BACKSLASH_IN_FILENAME
4289 p = vim_strsave_fnameescape(files[i], FALSE);
4290#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004291 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 if (p != NULL)
4294 {
4295 vim_free(files[i]);
4296 files[i] = p;
4297 }
4298
4299 /* If 'str' starts with "\~", replace "~" at start of
4300 * files[i] with "\~". */
4301 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004302 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004305
4306 /* If the first file starts with a '+' escape it. Otherwise it
4307 * could be seen as "+cmd". */
4308 if (*files[0] == '+')
4309 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 }
4311 else if (xp->xp_context == EXPAND_TAGS)
4312 {
4313 /*
4314 * Insert a backslash before characters in a tag name that
4315 * would terminate the ":tag" command.
4316 */
4317 for (i = 0; i < numfiles; ++i)
4318 {
4319 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4320 if (p != NULL)
4321 {
4322 vim_free(files[i]);
4323 files[i] = p;
4324 }
4325 }
4326 }
4327 }
4328}
4329
4330/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004331 * Escape special characters in "fname" for when used as a file name argument
4332 * after a Vim command, or, when "shell" is non-zero, a shell command.
4333 * Returns the result in allocated memory.
4334 */
4335 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004336vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004337{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004338 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004339#ifdef BACKSLASH_IN_FILENAME
4340 char_u buf[20];
4341 int j = 0;
4342
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004343 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004344 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004345 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004346 buf[j++] = *p;
4347 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004348 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004349#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004350 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4351 if (shell && csh_like_shell() && p != NULL)
4352 {
4353 char_u *s;
4354
4355 /* For csh and similar shells need to put two backslashes before '!'.
4356 * One is taken by Vim, one by the shell. */
4357 s = vim_strsave_escaped(p, (char_u *)"!");
4358 vim_free(p);
4359 p = s;
4360 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004361#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004362
4363 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4364 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004365 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004366 escape_fname(&p);
4367
4368 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004369}
4370
4371/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004372 * Put a backslash before the file name in "pp", which is in allocated memory.
4373 */
4374 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004375escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004376{
4377 char_u *p;
4378
4379 p = alloc((unsigned)(STRLEN(*pp) + 2));
4380 if (p != NULL)
4381 {
4382 p[0] = '\\';
4383 STRCPY(p + 1, *pp);
4384 vim_free(*pp);
4385 *pp = p;
4386 }
4387}
4388
4389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 * For each file name in files[num_files]:
4391 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4392 */
4393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004394tilde_replace(
4395 char_u *orig_pat,
4396 int num_files,
4397 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398{
4399 int i;
4400 char_u *p;
4401
4402 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4403 {
4404 for (i = 0; i < num_files; ++i)
4405 {
4406 p = home_replace_save(NULL, files[i]);
4407 if (p != NULL)
4408 {
4409 vim_free(files[i]);
4410 files[i] = p;
4411 }
4412 }
4413 }
4414}
4415
4416/*
4417 * Show all matches for completion on the command line.
4418 * Returns EXPAND_NOTHING when the character that triggered expansion should
4419 * be inserted like a normal character.
4420 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004422showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423{
4424#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4425 int num_files;
4426 char_u **files_found;
4427 int i, j, k;
4428 int maxlen;
4429 int lines;
4430 int columns;
4431 char_u *p;
4432 int lastlen;
4433 int attr;
4434 int showtail;
4435
4436 if (xp->xp_numfiles == -1)
4437 {
4438 set_expand_context(xp);
4439 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4440 &num_files, &files_found);
4441 showtail = expand_showtail(xp);
4442 if (i != EXPAND_OK)
4443 return i;
4444
4445 }
4446 else
4447 {
4448 num_files = xp->xp_numfiles;
4449 files_found = xp->xp_files;
4450 showtail = cmd_showtail;
4451 }
4452
4453#ifdef FEAT_WILDMENU
4454 if (!wildmenu)
4455 {
4456#endif
4457 msg_didany = FALSE; /* lines_left will be set */
4458 msg_start(); /* prepare for paging */
4459 msg_putchar('\n');
4460 out_flush();
4461 cmdline_row = msg_row;
4462 msg_didany = FALSE; /* lines_left will be set again */
4463 msg_start(); /* prepare for paging */
4464#ifdef FEAT_WILDMENU
4465 }
4466#endif
4467
4468 if (got_int)
4469 got_int = FALSE; /* only int. the completion, not the cmd line */
4470#ifdef FEAT_WILDMENU
4471 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004472 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473#endif
4474 else
4475 {
4476 /* find the length of the longest file name */
4477 maxlen = 0;
4478 for (i = 0; i < num_files; ++i)
4479 {
4480 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004481 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 || xp->xp_context == EXPAND_BUFFERS))
4483 {
4484 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4485 j = vim_strsize(NameBuff);
4486 }
4487 else
4488 j = vim_strsize(L_SHOWFILE(i));
4489 if (j > maxlen)
4490 maxlen = j;
4491 }
4492
4493 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4494 lines = num_files;
4495 else
4496 {
4497 /* compute the number of columns and lines for the listing */
4498 maxlen += 2; /* two spaces between file names */
4499 columns = ((int)Columns + 2) / maxlen;
4500 if (columns < 1)
4501 columns = 1;
4502 lines = (num_files + columns - 1) / columns;
4503 }
4504
Bram Moolenaar8820b482017-03-16 17:23:31 +01004505 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506
4507 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4508 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004509 msg_puts_attr(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 msg_clr_eos();
4511 msg_advance(maxlen - 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004512 msg_puts_attr(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 }
4514
4515 /* list the files line by line */
4516 for (i = 0; i < lines; ++i)
4517 {
4518 lastlen = 999;
4519 for (k = i; k < num_files; k += lines)
4520 {
4521 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4522 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004523 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 p = files_found[k] + STRLEN(files_found[k]) + 1;
4525 msg_advance(maxlen + 1);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004526 msg_puts((char *)p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 msg_advance(maxlen + 3);
Bram Moolenaar32526b32019-01-19 17:43:09 +01004528 msg_outtrans_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 break;
4530 }
4531 for (j = maxlen - lastlen; --j >= 0; )
4532 msg_putchar(' ');
4533 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004534 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 || xp->xp_context == EXPAND_BUFFERS)
4536 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004537 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004538 if (xp->xp_numfiles != -1)
4539 {
4540 char_u *halved_slash;
4541 char_u *exp_path;
4542
4543 /* Expansion was done before and special characters
4544 * were escaped, need to halve backslashes. Also
4545 * $HOME has been replaced with ~/. */
4546 exp_path = expand_env_save_opt(files_found[k], TRUE);
4547 halved_slash = backslash_halve_save(
4548 exp_path != NULL ? exp_path : files_found[k]);
4549 j = mch_isdir(halved_slash != NULL ? halved_slash
4550 : files_found[k]);
4551 vim_free(exp_path);
4552 vim_free(halved_slash);
4553 }
4554 else
4555 /* Expansion was done here, file names are literal. */
4556 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 if (showtail)
4558 p = L_SHOWFILE(k);
4559 else
4560 {
4561 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4562 TRUE);
4563 p = NameBuff;
4564 }
4565 }
4566 else
4567 {
4568 j = FALSE;
4569 p = L_SHOWFILE(k);
4570 }
4571 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4572 }
4573 if (msg_col > 0) /* when not wrapped around */
4574 {
4575 msg_clr_eos();
4576 msg_putchar('\n');
4577 }
4578 out_flush(); /* show one line at a time */
4579 if (got_int)
4580 {
4581 got_int = FALSE;
4582 break;
4583 }
4584 }
4585
4586 /*
4587 * we redraw the command below the lines that we have just listed
4588 * This is a bit tricky, but it saves a lot of screen updating.
4589 */
4590 cmdline_row = msg_row; /* will put it back later */
4591 }
4592
4593 if (xp->xp_numfiles == -1)
4594 FreeWild(num_files, files_found);
4595
4596 return EXPAND_OK;
4597}
4598
4599/*
4600 * Private gettail for showmatches() (and win_redr_status_matches()):
4601 * Find tail of file name path, but ignore trailing "/".
4602 */
4603 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004604sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605{
4606 char_u *p;
4607 char_u *t = s;
4608 int had_sep = FALSE;
4609
4610 for (p = s; *p != NUL; )
4611 {
4612 if (vim_ispathsep(*p)
4613#ifdef BACKSLASH_IN_FILENAME
4614 && !rem_backslash(p)
4615#endif
4616 )
4617 had_sep = TRUE;
4618 else if (had_sep)
4619 {
4620 t = p;
4621 had_sep = FALSE;
4622 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004623 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 return t;
4626}
4627
4628/*
4629 * Return TRUE if we only need to show the tail of completion matches.
4630 * When not completing file names or there is a wildcard in the path FALSE is
4631 * returned.
4632 */
4633 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004634expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635{
4636 char_u *s;
4637 char_u *end;
4638
4639 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004640 if (xp->xp_context != EXPAND_FILES
4641 && xp->xp_context != EXPAND_SHELLCMD
4642 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 return FALSE;
4644
4645 end = gettail(xp->xp_pattern);
4646 if (end == xp->xp_pattern) /* there is no path separator */
4647 return FALSE;
4648
4649 for (s = xp->xp_pattern; s < end; s++)
4650 {
4651 /* Skip escaped wildcards. Only when the backslash is not a path
4652 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4653 if (rem_backslash(s))
4654 ++s;
4655 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4656 return FALSE;
4657 }
4658 return TRUE;
4659}
4660
4661/*
4662 * Prepare a string for expansion.
4663 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004664 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 * When expanding other names: The string will be used with regcomp(). Copy
4666 * the name into allocated memory and prepend "^".
4667 */
4668 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004669addstar(
4670 char_u *fname,
4671 int len,
4672 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673{
4674 char_u *retval;
4675 int i, j;
4676 int new_len;
4677 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004678 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004680 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004681 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004682 && context != EXPAND_SHELLCMD
4683 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
4685 /*
4686 * Matching will be done internally (on something other than files).
4687 * So we convert the file-matching-type wildcards into our kind for
4688 * use with vim_regcomp(). First work out how long it will be:
4689 */
4690
4691 /* For help tags the translation is done in find_help_tags().
4692 * For a tag pattern starting with "/" no translation is needed. */
4693 if (context == EXPAND_HELP
4694 || context == EXPAND_COLORS
4695 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004696 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004697 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004698 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004699 || ((context == EXPAND_TAGS_LISTFILES
4700 || context == EXPAND_TAGS)
4701 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 retval = vim_strnsave(fname, len);
4703 else
4704 {
4705 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4706 for (i = 0; i < len; i++)
4707 {
4708 if (fname[i] == '*' || fname[i] == '~')
4709 new_len++; /* '*' needs to be replaced by ".*"
4710 '~' needs to be replaced by "\~" */
4711
4712 /* Buffer names are like file names. "." should be literal */
4713 if (context == EXPAND_BUFFERS && fname[i] == '.')
4714 new_len++; /* "." becomes "\." */
4715
4716 /* Custom expansion takes care of special things, match
4717 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004718 if ((context == EXPAND_USER_DEFINED
4719 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 new_len++; /* '\' becomes "\\" */
4721 }
4722 retval = alloc(new_len);
4723 if (retval != NULL)
4724 {
4725 retval[0] = '^';
4726 j = 1;
4727 for (i = 0; i < len; i++, j++)
4728 {
4729 /* Skip backslash. But why? At least keep it for custom
4730 * expansion. */
4731 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004732 && context != EXPAND_USER_LIST
4733 && fname[i] == '\\'
4734 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 break;
4736
4737 switch (fname[i])
4738 {
4739 case '*': retval[j++] = '.';
4740 break;
4741 case '~': retval[j++] = '\\';
4742 break;
4743 case '?': retval[j] = '.';
4744 continue;
4745 case '.': if (context == EXPAND_BUFFERS)
4746 retval[j++] = '\\';
4747 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004748 case '\\': if (context == EXPAND_USER_DEFINED
4749 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 retval[j++] = '\\';
4751 break;
4752 }
4753 retval[j] = fname[i];
4754 }
4755 retval[j] = NUL;
4756 }
4757 }
4758 }
4759 else
4760 {
4761 retval = alloc(len + 4);
4762 if (retval != NULL)
4763 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004764 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004767 * Don't add a star to *, ~, ~user, $var or `cmd`.
4768 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 * ~ would be at the start of the file name, but not the tail.
4770 * $ could be anywhere in the tail.
4771 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004772 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 */
4774 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004775 ends_in_star = (len > 0 && retval[len - 1] == '*');
4776#ifndef BACKSLASH_IN_FILENAME
4777 for (i = len - 2; i >= 0; --i)
4778 {
4779 if (retval[i] != '\\')
4780 break;
4781 ends_in_star = !ends_in_star;
4782 }
4783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004785 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 && vim_strchr(tail, '$') == NULL
4787 && vim_strchr(retval, '`') == NULL)
4788 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004789 else if (len > 0 && retval[len - 1] == '$')
4790 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 retval[len] = NUL;
4792 }
4793 }
4794 return retval;
4795}
4796
4797/*
4798 * Must parse the command line so far to work out what context we are in.
4799 * Completion can then be done based on that context.
4800 * This routine sets the variables:
4801 * xp->xp_pattern The start of the pattern to be expanded within
4802 * the command line (ends at the cursor).
4803 * xp->xp_context The type of thing to expand. Will be one of:
4804 *
4805 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4806 * the command line, like an unknown command. Caller
4807 * should beep.
4808 * EXPAND_NOTHING Unrecognised context for completion, use char like
4809 * a normal char, rather than for completion. eg
4810 * :s/^I/
4811 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4812 * it.
4813 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4814 * EXPAND_FILES After command with XFILE set, or after setting
4815 * with P_EXPAND set. eg :e ^I, :w>>^I
4816 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4817 * when we know only directories are of interest. eg
4818 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004819 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4821 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4822 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4823 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4824 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4825 * EXPAND_EVENTS Complete event names
4826 * EXPAND_SYNTAX Complete :syntax command arguments
4827 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4828 * EXPAND_AUGROUP Complete autocommand group names
4829 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4830 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4831 * eg :unmap a^I , :cunab x^I
4832 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4833 * eg :call sub^I
4834 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4835 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4836 * names in expressions, eg :while s^I
4837 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004838 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004841set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004843 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 if (ccline.cmdfirstc != ':'
4845#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004846 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004847 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848#endif
4849 )
4850 {
4851 xp->xp_context = EXPAND_NOTHING;
4852 return;
4853 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004854 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855}
4856
4857 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004858set_cmd_context(
4859 expand_T *xp,
4860 char_u *str, /* start of command line */
4861 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004862 int col, /* position of cursor */
4863 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864{
4865 int old_char = NUL;
4866 char_u *nextcomm;
4867
4868 /*
4869 * Avoid a UMR warning from Purify, only save the character if it has been
4870 * written before.
4871 */
4872 if (col < len)
4873 old_char = str[col];
4874 str[col] = NUL;
4875 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004876
4877#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004878 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004879 {
4880# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004881 /* pass CMD_SIZE because there is no real command */
4882 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004883# endif
4884 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004885 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004886 {
4887 xp->xp_context = ccline.xp_context;
4888 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004889# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004890 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004891# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004892 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004893 else
4894#endif
4895 while (nextcomm != NULL)
4896 nextcomm = set_one_cmd_context(xp, nextcomm);
4897
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004898 /* Store the string here so that call_user_expand_func() can get to them
4899 * easily. */
4900 xp->xp_line = str;
4901 xp->xp_col = col;
4902
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 str[col] = old_char;
4904}
4905
4906/*
4907 * Expand the command line "str" from context "xp".
4908 * "xp" must have been set by set_cmd_context().
4909 * xp->xp_pattern points into "str", to where the text that is to be expanded
4910 * starts.
4911 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4912 * cursor.
4913 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4914 * key that triggered expansion literally.
4915 * Returns EXPAND_OK otherwise.
4916 */
4917 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004918expand_cmdline(
4919 expand_T *xp,
4920 char_u *str, /* start of command line */
4921 int col, /* position of cursor */
4922 int *matchcount, /* return: nr of matches */
4923 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924{
4925 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004926 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4929 {
4930 beep_flush();
4931 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4932 }
4933 if (xp->xp_context == EXPAND_NOTHING)
4934 {
4935 /* Caller can use the character as a normal char instead */
4936 return EXPAND_NOTHING;
4937 }
4938
4939 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004940 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4941 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (file_str == NULL)
4943 return EXPAND_UNSUCCESSFUL;
4944
Bram Moolenaar94950a92010-12-02 16:01:29 +01004945 if (p_wic)
4946 options += WILD_ICASE;
4947
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004949 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 *matchcount = 0;
4952 *matches = NULL;
4953 }
4954 vim_free(file_str);
4955
4956 return EXPAND_OK;
4957}
4958
4959#ifdef FEAT_MULTI_LANG
4960/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02004961 * Cleanup matches for help tags:
4962 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
4963 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004966cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
4968 int i, j;
4969 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004970 char_u buf[4];
4971 char_u *p = buf;
4972
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004973 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02004974 {
4975 *p++ = '@';
4976 *p++ = p_hlg[0];
4977 *p++ = p_hlg[1];
4978 }
4979 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981 for (i = 0; i < num_file; ++i)
4982 {
4983 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02004984 if (len <= 0)
4985 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004986 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 {
4988 /* Sorting on priority means the same item in another language may
4989 * be anywhere. Search all items for a match up to the "@en". */
4990 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004991 if (j != i && (int)STRLEN(file[j]) == len + 3
4992 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 break;
4994 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02004995 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 file[i][len] = NUL;
4997 }
4998 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02004999
5000 if (*buf != NUL)
5001 for (i = 0; i < num_file; ++i)
5002 {
5003 len = (int)STRLEN(file[i]) - 3;
5004 if (len <= 0)
5005 continue;
5006 if (STRCMP(file[i] + len, buf) == 0)
5007 {
5008 /* remove the default language */
5009 file[i][len] = NUL;
5010 }
5011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012}
5013#endif
5014
5015/*
5016 * Do the expansion based on xp->xp_context and "pat".
5017 */
5018 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005019ExpandFromContext(
5020 expand_T *xp,
5021 char_u *pat,
5022 int *num_file,
5023 char_u ***file,
5024 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025{
5026#ifdef FEAT_CMDL_COMPL
5027 regmatch_T regmatch;
5028#endif
5029 int ret;
5030 int flags;
5031
5032 flags = EW_DIR; /* include directories */
5033 if (options & WILD_LIST_NOTFOUND)
5034 flags |= EW_NOTFOUND;
5035 if (options & WILD_ADD_SLASH)
5036 flags |= EW_ADDSLASH;
5037 if (options & WILD_KEEP_ALL)
5038 flags |= EW_KEEPALL;
5039 if (options & WILD_SILENT)
5040 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005041 if (options & WILD_ALLLINKS)
5042 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005044 if (xp->xp_context == EXPAND_FILES
5045 || xp->xp_context == EXPAND_DIRECTORIES
5046 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 /*
5049 * Expand file or directory names.
5050 */
5051 int free_pat = FALSE;
5052 int i;
5053
5054 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5055 * space */
5056 if (xp->xp_backslash != XP_BS_NONE)
5057 {
5058 free_pat = TRUE;
5059 pat = vim_strsave(pat);
5060 for (i = 0; pat[i]; ++i)
5061 if (pat[i] == '\\')
5062 {
5063 if (xp->xp_backslash == XP_BS_THREE
5064 && pat[i + 1] == '\\'
5065 && pat[i + 2] == '\\'
5066 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005067 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 if (xp->xp_backslash == XP_BS_ONE
5069 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005070 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 }
5073
5074 if (xp->xp_context == EXPAND_FILES)
5075 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005076 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5077 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 else
5079 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005080 if (options & WILD_ICASE)
5081 flags |= EW_ICASE;
5082
Bram Moolenaard7834d32009-12-02 16:14:36 +00005083 /* Expand wildcards, supporting %:h and the like. */
5084 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 if (free_pat)
5086 vim_free(pat);
5087 return ret;
5088 }
5089
5090 *file = (char_u **)"";
5091 *num_file = 0;
5092 if (xp->xp_context == EXPAND_HELP)
5093 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005094 /* With an empty argument we would get all the help tags, which is
5095 * very slow. Get matches for "help" instead. */
5096 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5097 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099#ifdef FEAT_MULTI_LANG
5100 cleanup_help_tags(*num_file, *file);
5101#endif
5102 return OK;
5103 }
5104 return FAIL;
5105 }
5106
5107#ifndef FEAT_CMDL_COMPL
5108 return FAIL;
5109#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005110 if (xp->xp_context == EXPAND_SHELLCMD)
5111 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 if (xp->xp_context == EXPAND_OLD_SETTING)
5113 return ExpandOldSetting(num_file, file);
5114 if (xp->xp_context == EXPAND_BUFFERS)
5115 return ExpandBufnames(pat, num_file, file, options);
5116 if (xp->xp_context == EXPAND_TAGS
5117 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5118 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5119 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005120 {
5121 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005122 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5123 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005126 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005127 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005128 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005129 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005130 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005131 {
5132 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005133 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005134 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005135 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005136 {
5137 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005138 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005139 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005140# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5141 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005142 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005143# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005144 if (xp->xp_context == EXPAND_PACKADD)
5145 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146
5147 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5148 if (regmatch.regprog == NULL)
5149 return FAIL;
5150
5151 /* set ignore-case according to p_ic, p_scs and pat */
5152 regmatch.rm_ic = ignorecase(pat);
5153
5154 if (xp->xp_context == EXPAND_SETTINGS
5155 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5156 ret = ExpandSettings(xp, &regmatch, num_file, file);
5157 else if (xp->xp_context == EXPAND_MAPPINGS)
5158 ret = ExpandMappings(&regmatch, num_file, file);
5159# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5160 else if (xp->xp_context == EXPAND_USER_DEFINED)
5161 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5162# endif
5163 else
5164 {
5165 static struct expgen
5166 {
5167 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005168 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005170 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 } tab[] =
5172 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005173 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5174 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005175 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005176 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005177#ifdef FEAT_CMDHIST
5178 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005181 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005182 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005183 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5184 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5185 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#endif
5187#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005188 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5189 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5190 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5191 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192#endif
5193#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005194 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5195 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196#endif
5197#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005198 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005200#ifdef FEAT_PROFILE
5201 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5202#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005203 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005204 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5205 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005206#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005207 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005208#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005209#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005210 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005211#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005212#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005214#endif
Bram Moolenaarfc3abf42019-01-24 15:54:21 +01005215#if defined(HAVE_LOCALE_H) || defined(X_LOCALE)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5217 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005219 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005220 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005221 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 };
5223 int i;
5224
5225 /*
5226 * Find a context in the table and call the ExpandGeneric() with the
5227 * right function to do the expansion.
5228 */
5229 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005230 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 if (xp->xp_context == tab[i].context)
5232 {
5233 if (tab[i].ic)
5234 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005236 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 break;
5238 }
5239 }
5240
Bram Moolenaar473de612013-06-08 18:19:48 +02005241 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242
5243 return ret;
5244#endif /* FEAT_CMDL_COMPL */
5245}
5246
5247#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5248/*
5249 * Expand a list of names.
5250 *
5251 * Generic function for command line completion. It calls a function to
5252 * obtain strings, one by one. The strings are matched against a regexp
5253 * program. Matching strings are copied into an array, which is returned.
5254 *
5255 * Returns OK when no problems encountered, FAIL for error (out of memory).
5256 */
5257 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005258ExpandGeneric(
5259 expand_T *xp,
5260 regmatch_T *regmatch,
5261 int *num_file,
5262 char_u ***file,
5263 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005265 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266{
5267 int i;
5268 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005269 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 char_u *str;
5271
5272 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005273 * round == 0: count the number of matching names
5274 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005276 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 {
5278 for (i = 0; ; ++i)
5279 {
5280 str = (*func)(xp, i);
5281 if (str == NULL) /* end of list */
5282 break;
5283 if (*str == NUL) /* skip empty strings */
5284 continue;
5285
5286 if (vim_regexec(regmatch, str, (colnr_T)0))
5287 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005288 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005290 if (escaped)
5291 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5292 else
5293 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 (*file)[count] = str;
5295#ifdef FEAT_MENU
5296 if (func == get_menu_names && str != NULL)
5297 {
5298 /* test for separator added by get_menu_names() */
5299 str += STRLEN(str) - 1;
5300 if (*str == '\001')
5301 *str = '.';
5302 }
5303#endif
5304 }
5305 ++count;
5306 }
5307 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005308 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 {
5310 if (count == 0)
5311 return OK;
5312 *num_file = count;
5313 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5314 if (*file == NULL)
5315 {
5316 *file = (char_u **)"";
5317 return FAIL;
5318 }
5319 count = 0;
5320 }
5321 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005322
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005323 /* Sort the results. Keep menu's in the specified order. */
5324 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005325 {
5326 if (xp->xp_context == EXPAND_EXPRESSION
5327 || xp->xp_context == EXPAND_FUNCTIONS
5328 || xp->xp_context == EXPAND_USER_FUNC)
5329 /* <SNR> functions should be sorted to the end. */
5330 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5331 sort_func_compare);
5332 else
5333 sort_strings(*file, *num_file);
5334 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005335
Bram Moolenaar4f688582007-07-24 12:34:30 +00005336#ifdef FEAT_CMDL_COMPL
5337 /* Reset the variables used for special highlight names expansion, so that
5338 * they don't show up when getting normal highlight names by ID. */
5339 reset_expand_highlight();
5340#endif
5341
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 return OK;
5343}
5344
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005345/*
5346 * Complete a shell command.
5347 * Returns FAIL or OK;
5348 */
5349 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005350expand_shellcmd(
5351 char_u *filepat, /* pattern to match with command names */
5352 int *num_file, /* return: number of matches */
5353 char_u ***file, /* return: array with matches */
5354 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005355{
5356 char_u *pat;
5357 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005358 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005359 int mustfree = FALSE;
5360 garray_T ga;
5361 char_u *buf = alloc(MAXPATHL);
5362 size_t l;
5363 char_u *s, *e;
5364 int flags = flagsarg;
5365 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005366 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005367 hashtab_T found_ht;
5368 hashitem_T *hi;
5369 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005370
5371 if (buf == NULL)
5372 return FAIL;
5373
5374 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5375 * space */
5376 pat = vim_strsave(filepat);
5377 for (i = 0; pat[i]; ++i)
5378 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005379 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005380
Bram Moolenaarb5971142015-03-21 17:32:19 +01005381 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005382
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005383 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5384 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005385 path = (char_u *)".";
5386 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005387 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005388 /* For an absolute name we don't use $PATH. */
5389 if (!mch_isFullName(pat))
5390 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005391 if (path == NULL)
5392 path = (char_u *)"";
5393 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005394
5395 /*
5396 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005397 * collect them in "ga". When "." is not in $PATH also expand for the
5398 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005399 */
5400 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005401 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005402 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005403 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005404#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005405 e = vim_strchr(s, ';');
5406#else
5407 e = vim_strchr(s, ':');
5408#endif
5409 if (e == NULL)
5410 e = s + STRLEN(s);
5411
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005412 if (*s == NUL)
5413 {
5414 if (did_curdir)
5415 break;
5416 // Find directories in the current directory, path is empty.
5417 did_curdir = TRUE;
5418 flags |= EW_DIR;
5419 }
5420 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5421 {
5422 did_curdir = TRUE;
5423 flags |= EW_DIR;
5424 }
5425 else
5426 // Do not match directories inside a $PATH item.
5427 flags &= ~EW_DIR;
5428
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005429 l = e - s;
5430 if (l > MAXPATHL - 5)
5431 break;
5432 vim_strncpy(buf, s, l);
5433 add_pathsep(buf);
5434 l = STRLEN(buf);
5435 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5436
5437 /* Expand matches in one directory of $PATH. */
5438 ret = expand_wildcards(1, &buf, num_file, file, flags);
5439 if (ret == OK)
5440 {
5441 if (ga_grow(&ga, *num_file) == FAIL)
5442 FreeWild(*num_file, *file);
5443 else
5444 {
5445 for (i = 0; i < *num_file; ++i)
5446 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005447 char_u *name = (*file)[i];
5448
5449 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005450 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005451 // Check if this name was already found.
5452 hash = hash_hash(name + l);
5453 hi = hash_lookup(&found_ht, name + l, hash);
5454 if (HASHITEM_EMPTY(hi))
5455 {
5456 // Remove the path that was prepended.
5457 STRMOVE(name, name + l);
5458 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5459 hash_add_item(&found_ht, hi, name, hash);
5460 name = NULL;
5461 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005462 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005463 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005464 }
5465 vim_free(*file);
5466 }
5467 }
5468 if (*e != NUL)
5469 ++e;
5470 }
5471 *file = ga.ga_data;
5472 *num_file = ga.ga_len;
5473
5474 vim_free(buf);
5475 vim_free(pat);
5476 if (mustfree)
5477 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005478 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005479 return OK;
5480}
5481
5482
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5484/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005485 * Call "user_expand_func()" to invoke a user defined Vim script function and
5486 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005488 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005489call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005490 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005491 expand_T *xp,
5492 int *num_file,
5493 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005495 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005496 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005497 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005498 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005499 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
Bram Moolenaarb7515462013-06-29 12:58:33 +02005501 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005502 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 *num_file = 0;
5504 *file = NULL;
5505
Bram Moolenaarb7515462013-06-29 12:58:33 +02005506 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005507 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005508 keep = ccline.cmdbuff[ccline.cmdlen];
5509 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005510 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005511
Bram Moolenaarffa96842018-06-12 22:05:14 +02005512 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5513
5514 args[0].v_type = VAR_STRING;
5515 args[0].vval.v_string = pat;
5516 args[1].v_type = VAR_STRING;
5517 args[1].vval.v_string = xp->xp_line;
5518 args[2].v_type = VAR_NUMBER;
5519 args[2].vval.v_number = xp->xp_col;
5520 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005522 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005523
Bram Moolenaarded27a12018-08-01 19:06:03 +02005524 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005525
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005526 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005527 if (ccline.cmdbuff != NULL)
5528 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005529
Bram Moolenaarffa96842018-06-12 22:05:14 +02005530 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005531 return ret;
5532}
5533
5534/*
5535 * Expand names with a function defined by the user.
5536 */
5537 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005538ExpandUserDefined(
5539 expand_T *xp,
5540 regmatch_T *regmatch,
5541 int *num_file,
5542 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005543{
5544 char_u *retstr;
5545 char_u *s;
5546 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005547 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005548 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005549 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005550
5551 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5552 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 return FAIL;
5554
5555 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005556 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
5558 e = vim_strchr(s, '\n');
5559 if (e == NULL)
5560 e = s + STRLEN(s);
5561 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005562 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005564 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5565 *e = keep;
5566
5567 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005569 if (ga_grow(&ga, 1) == FAIL)
5570 break;
5571 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5572 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 if (*e != NUL)
5576 ++e;
5577 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005578 vim_free(retstr);
5579 *file = ga.ga_data;
5580 *num_file = ga.ga_len;
5581 return OK;
5582}
5583
5584/*
5585 * Expand names with a list returned by a function defined by the user.
5586 */
5587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588ExpandUserList(
5589 expand_T *xp,
5590 int *num_file,
5591 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005592{
5593 list_T *retlist;
5594 listitem_T *li;
5595 garray_T ga;
5596
5597 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5598 if (retlist == NULL)
5599 return FAIL;
5600
5601 ga_init2(&ga, (int)sizeof(char *), 3);
5602 /* Loop over the items in the list. */
5603 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5604 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005605 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5606 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005607
5608 if (ga_grow(&ga, 1) == FAIL)
5609 break;
5610
5611 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005612 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005613 ++ga.ga_len;
5614 }
5615 list_unref(retlist);
5616
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 *file = ga.ga_data;
5618 *num_file = ga.ga_len;
5619 return OK;
5620}
5621#endif
5622
5623/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005624 * Expand color scheme, compiler or filetype names.
5625 * Search from 'runtimepath':
5626 * 'runtimepath'/{dirnames}/{pat}.vim
5627 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5628 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5629 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5630 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005631 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 */
5633 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005634ExpandRTDir(
5635 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005636 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005637 int *num_file,
5638 char_u ***file,
5639 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 char_u *s;
5642 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005643 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005645 int i;
5646 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
5648 *num_file = 0;
5649 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005650 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005651 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005653 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005655 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5656 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005658 ga_clear_strings(&ga);
5659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005661 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005662 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005663 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005665
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005666 if (flags & DIP_START) {
5667 for (i = 0; dirnames[i] != NULL; ++i)
5668 {
5669 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5670 if (s == NULL)
5671 {
5672 ga_clear_strings(&ga);
5673 return FAIL;
5674 }
5675 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5676 globpath(p_pp, s, &ga, 0);
5677 vim_free(s);
5678 }
5679 }
5680
5681 if (flags & DIP_OPT) {
5682 for (i = 0; dirnames[i] != NULL; ++i)
5683 {
5684 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5685 if (s == NULL)
5686 {
5687 ga_clear_strings(&ga);
5688 return FAIL;
5689 }
5690 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5691 globpath(p_pp, s, &ga, 0);
5692 vim_free(s);
5693 }
5694 }
5695
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005696 for (i = 0; i < ga.ga_len; ++i)
5697 {
5698 match = ((char_u **)ga.ga_data)[i];
5699 s = match;
5700 e = s + STRLEN(s);
5701 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5702 {
5703 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005704 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005705 if (s < match || vim_ispathsep(*s))
5706 break;
5707 ++s;
5708 *e = NUL;
5709 mch_memmove(match, s, e - s + 1);
5710 }
5711 }
5712
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005713 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005714 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005715
5716 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005717 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005718 remove_duplicates(&ga);
5719
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 *file = ga.ga_data;
5721 *num_file = ga.ga_len;
5722 return OK;
5723}
5724
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005725/*
5726 * Expand loadplugin names:
5727 * 'packpath'/pack/ * /opt/{pat}
5728 */
5729 static int
5730ExpandPackAddDir(
5731 char_u *pat,
5732 int *num_file,
5733 char_u ***file)
5734{
5735 char_u *s;
5736 char_u *e;
5737 char_u *match;
5738 garray_T ga;
5739 int i;
5740 int pat_len;
5741
5742 *num_file = 0;
5743 *file = NULL;
5744 pat_len = (int)STRLEN(pat);
5745 ga_init2(&ga, (int)sizeof(char *), 10);
5746
5747 s = alloc((unsigned)(pat_len + 26));
5748 if (s == NULL)
5749 {
5750 ga_clear_strings(&ga);
5751 return FAIL;
5752 }
5753 sprintf((char *)s, "pack/*/opt/%s*", pat);
5754 globpath(p_pp, s, &ga, 0);
5755 vim_free(s);
5756
5757 for (i = 0; i < ga.ga_len; ++i)
5758 {
5759 match = ((char_u **)ga.ga_data)[i];
5760 s = gettail(match);
5761 e = s + STRLEN(s);
5762 mch_memmove(match, s, e - s + 1);
5763 }
5764
5765 if (ga.ga_len == 0)
5766 return FAIL;
5767
5768 /* Sort and remove duplicates which can happen when specifying multiple
5769 * directories in dirnames. */
5770 remove_duplicates(&ga);
5771
5772 *file = ga.ga_data;
5773 *num_file = ga.ga_len;
5774 return OK;
5775}
5776
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#endif
5778
5779#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5780/*
5781 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005782 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005784 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005785globpath(
5786 char_u *path,
5787 char_u *file,
5788 garray_T *ga,
5789 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 expand_T xpc;
5792 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 int num_p;
5795 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 buf = alloc(MAXPATHL);
5798 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005799 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005801 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005803
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 /* Loop over all entries in {path}. */
5805 while (*path != NUL)
5806 {
5807 /* Copy one item of the path to buf[] and concatenate the file name. */
5808 copy_option_part(&path, buf, MAXPATHL, ",");
5809 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5810 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005811# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005812 /* Using the platform's path separator (\) makes vim incorrectly
5813 * treat it as an escape character, use '/' instead. */
5814 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5815 STRCAT(buf, "/");
5816# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005820 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5821 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005823 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005825 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 for (i = 0; i < num_p; ++i)
5828 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005829 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005830 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005831 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 FreeWild(num_p, p);
5836 }
5837 }
5838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839
5840 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841}
5842
5843#endif
5844
5845#if defined(FEAT_CMDHIST) || defined(PROTO)
5846
5847/*********************************
5848 * Command line history stuff *
5849 *********************************/
5850
5851/*
5852 * Translate a history character to the associated type number.
5853 */
5854 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005855hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 if (c == ':')
5858 return HIST_CMD;
5859 if (c == '=')
5860 return HIST_EXPR;
5861 if (c == '@')
5862 return HIST_INPUT;
5863 if (c == '>')
5864 return HIST_DEBUG;
5865 return HIST_SEARCH; /* must be '?' or '/' */
5866}
5867
5868/*
5869 * Table of history names.
5870 * These names are used in :history and various hist...() functions.
5871 * It is sufficient to give the significant prefix of a history name.
5872 */
5873
5874static char *(history_names[]) =
5875{
5876 "cmd",
5877 "search",
5878 "expr",
5879 "input",
5880 "debug",
5881 NULL
5882};
5883
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005884#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5885/*
5886 * Function given to ExpandGeneric() to obtain the possible first
5887 * arguments of the ":history command.
5888 */
5889 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005890get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005891{
5892 static char_u compl[2] = { NUL, NUL };
5893 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005894 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005895 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5896
5897 if (idx < short_names_count)
5898 {
5899 compl[0] = (char_u)short_names[idx];
5900 return compl;
5901 }
5902 if (idx < short_names_count + history_name_count)
5903 return (char_u *)history_names[idx - short_names_count];
5904 if (idx == short_names_count + history_name_count)
5905 return (char_u *)"all";
5906 return NULL;
5907}
5908#endif
5909
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910/*
5911 * init_history() - Initialize the command line history.
5912 * Also used to re-allocate the history when the size changes.
5913 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005914 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005915init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916{
5917 int newlen; /* new length of history table */
5918 histentry_T *temp;
5919 int i;
5920 int j;
5921 int type;
5922
5923 /*
5924 * If size of history table changed, reallocate it
5925 */
5926 newlen = (int)p_hi;
5927 if (newlen != hislen) /* history length changed */
5928 {
5929 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5930 {
5931 if (newlen)
5932 {
5933 temp = (histentry_T *)lalloc(
5934 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5935 if (temp == NULL) /* out of memory! */
5936 {
5937 if (type == 0) /* first one: just keep the old length */
5938 {
5939 newlen = hislen;
5940 break;
5941 }
5942 /* Already changed one table, now we can only have zero
5943 * length for all tables. */
5944 newlen = 0;
5945 type = -1;
5946 continue;
5947 }
5948 }
5949 else
5950 temp = NULL;
5951 if (newlen == 0 || temp != NULL)
5952 {
5953 if (hisidx[type] < 0) /* there are no entries yet */
5954 {
5955 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005956 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 }
5958 else if (newlen > hislen) /* array becomes bigger */
5959 {
5960 for (i = 0; i <= hisidx[type]; ++i)
5961 temp[i] = history[type][i];
5962 j = i;
5963 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005964 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 for ( ; j < hislen; ++i, ++j)
5966 temp[i] = history[type][j];
5967 }
5968 else /* array becomes smaller or 0 */
5969 {
5970 j = hisidx[type];
5971 for (i = newlen - 1; ; --i)
5972 {
5973 if (i >= 0) /* copy newest entries */
5974 temp[i] = history[type][j];
5975 else /* remove older entries */
5976 vim_free(history[type][j].hisstr);
5977 if (--j < 0)
5978 j = hislen - 1;
5979 if (j == hisidx[type])
5980 break;
5981 }
5982 hisidx[type] = newlen - 1;
5983 }
5984 vim_free(history[type]);
5985 history[type] = temp;
5986 }
5987 }
5988 hislen = newlen;
5989 }
5990}
5991
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005992 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005993clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005994{
5995 hisptr->hisnum = 0;
5996 hisptr->viminfo = FALSE;
5997 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02005998 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02005999}
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001/*
6002 * Check if command line 'str' is already in history.
6003 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6004 */
6005 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006in_history(
6007 int type,
6008 char_u *str,
6009 int move_to_front, /* Move the entry to the front if it exists */
6010 int sep,
6011 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006012{
6013 int i;
6014 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006015 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016
6017 if (hisidx[type] < 0)
6018 return FALSE;
6019 i = hisidx[type];
6020 do
6021 {
6022 if (history[type][i].hisstr == NULL)
6023 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006024
6025 /* For search history, check that the separator character matches as
6026 * well. */
6027 p = history[type][i].hisstr;
6028 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006029 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006030 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 {
6032 if (!move_to_front)
6033 return TRUE;
6034 last_i = i;
6035 break;
6036 }
6037 if (--i < 0)
6038 i = hislen - 1;
6039 } while (i != hisidx[type]);
6040
6041 if (last_i >= 0)
6042 {
6043 str = history[type][i].hisstr;
6044 while (i != hisidx[type])
6045 {
6046 if (++i >= hislen)
6047 i = 0;
6048 history[type][last_i] = history[type][i];
6049 last_i = i;
6050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006052 history[type][i].viminfo = FALSE;
6053 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006054 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055 return TRUE;
6056 }
6057 return FALSE;
6058}
6059
6060/*
6061 * Convert history name (from table above) to its HIST_ equivalent.
6062 * When "name" is empty, return "cmd" history.
6063 * Returns -1 for unknown history name.
6064 */
6065 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006066get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067{
6068 int i;
6069 int len = (int)STRLEN(name);
6070
6071 /* No argument: use current history. */
6072 if (len == 0)
6073 return hist_char2type(ccline.cmdfirstc);
6074
6075 for (i = 0; history_names[i] != NULL; ++i)
6076 if (STRNICMP(name, history_names[i], len) == 0)
6077 return i;
6078
6079 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6080 return hist_char2type(name[0]);
6081
6082 return -1;
6083}
6084
6085static int last_maptick = -1; /* last seen maptick */
6086
6087/*
6088 * Add the given string to the given history. If the string is already in the
6089 * history then it is moved to the front. "histype" may be one of he HIST_
6090 * values.
6091 */
6092 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006093add_to_history(
6094 int histype,
6095 char_u *new_entry,
6096 int in_map, /* consider maptick when inside a mapping */
6097 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098{
6099 histentry_T *hisptr;
6100 int len;
6101
6102 if (hislen == 0) /* no history */
6103 return;
6104
Bram Moolenaara939e432013-11-09 05:30:26 +01006105 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6106 return;
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 /*
6109 * Searches inside the same mapping overwrite each other, so that only
6110 * the last line is kept. Be careful not to remove a line that was moved
6111 * down, only lines that were added.
6112 */
6113 if (histype == HIST_SEARCH && in_map)
6114 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006115 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116 {
6117 /* Current line is from the same mapping, remove it */
6118 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6119 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006120 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 --hisnum[histype];
6122 if (--hisidx[HIST_SEARCH] < 0)
6123 hisidx[HIST_SEARCH] = hislen - 1;
6124 }
6125 last_maptick = -1;
6126 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006127 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 {
6129 if (++hisidx[histype] == hislen)
6130 hisidx[histype] = 0;
6131 hisptr = &history[histype][hisidx[histype]];
6132 vim_free(hisptr->hisstr);
6133
6134 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006135 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6137 if (hisptr->hisstr != NULL)
6138 hisptr->hisstr[len + 1] = sep;
6139
6140 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006141 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006142 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 if (histype == HIST_SEARCH && in_map)
6144 last_maptick = maptick;
6145 }
6146}
6147
6148#if defined(FEAT_EVAL) || defined(PROTO)
6149
6150/*
6151 * Get identifier of newest history entry.
6152 * "histype" may be one of the HIST_ values.
6153 */
6154 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006155get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156{
6157 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6158 || hisidx[histype] < 0)
6159 return -1;
6160
6161 return history[histype][hisidx[histype]].hisnum;
6162}
6163
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 * Calculate history index from a number:
6166 * num > 0: seen as identifying number of a history entry
6167 * num < 0: relative position in history wrt newest entry
6168 * "histype" may be one of the HIST_ values.
6169 */
6170 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006171calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172{
6173 int i;
6174 histentry_T *hist;
6175 int wrapped = FALSE;
6176
6177 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6178 || (i = hisidx[histype]) < 0 || num == 0)
6179 return -1;
6180
6181 hist = history[histype];
6182 if (num > 0)
6183 {
6184 while (hist[i].hisnum > num)
6185 if (--i < 0)
6186 {
6187 if (wrapped)
6188 break;
6189 i += hislen;
6190 wrapped = TRUE;
6191 }
6192 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6193 return i;
6194 }
6195 else if (-num <= hislen)
6196 {
6197 i += num + 1;
6198 if (i < 0)
6199 i += hislen;
6200 if (hist[i].hisstr != NULL)
6201 return i;
6202 }
6203 return -1;
6204}
6205
6206/*
6207 * Get a history entry by its index.
6208 * "histype" may be one of the HIST_ values.
6209 */
6210 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006211get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
6213 idx = calc_hist_idx(histype, idx);
6214 if (idx >= 0)
6215 return history[histype][idx].hisstr;
6216 else
6217 return (char_u *)"";
6218}
6219
6220/*
6221 * Clear all entries of a history.
6222 * "histype" may be one of the HIST_ values.
6223 */
6224 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006225clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
6227 int i;
6228 histentry_T *hisptr;
6229
6230 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6231 {
6232 hisptr = history[histype];
6233 for (i = hislen; i--;)
6234 {
6235 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006236 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006237 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006238 }
6239 hisidx[histype] = -1; /* mark history as cleared */
6240 hisnum[histype] = 0; /* reset identifier counter */
6241 return OK;
6242 }
6243 return FAIL;
6244}
6245
6246/*
6247 * Remove all entries matching {str} from a history.
6248 * "histype" may be one of the HIST_ values.
6249 */
6250 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006251del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006252{
6253 regmatch_T regmatch;
6254 histentry_T *hisptr;
6255 int idx;
6256 int i;
6257 int last;
6258 int found = FALSE;
6259
6260 regmatch.regprog = NULL;
6261 regmatch.rm_ic = FALSE; /* always match case */
6262 if (hislen != 0
6263 && histype >= 0
6264 && histype < HIST_COUNT
6265 && *str != NUL
6266 && (idx = hisidx[histype]) >= 0
6267 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6268 != NULL)
6269 {
6270 i = last = idx;
6271 do
6272 {
6273 hisptr = &history[histype][i];
6274 if (hisptr->hisstr == NULL)
6275 break;
6276 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6277 {
6278 found = TRUE;
6279 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006280 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 }
6282 else
6283 {
6284 if (i != last)
6285 {
6286 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006287 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 if (--last < 0)
6290 last += hislen;
6291 }
6292 if (--i < 0)
6293 i += hislen;
6294 } while (i != idx);
6295 if (history[histype][idx].hisstr == NULL)
6296 hisidx[histype] = -1;
6297 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006298 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299 return found;
6300}
6301
6302/*
6303 * Remove an indexed entry from a history.
6304 * "histype" may be one of the HIST_ values.
6305 */
6306 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006307del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308{
6309 int i, j;
6310
6311 i = calc_hist_idx(histype, idx);
6312 if (i < 0)
6313 return FALSE;
6314 idx = hisidx[histype];
6315 vim_free(history[histype][i].hisstr);
6316
6317 /* When deleting the last added search string in a mapping, reset
6318 * last_maptick, so that the last added search string isn't deleted again.
6319 */
6320 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6321 last_maptick = -1;
6322
6323 while (i != idx)
6324 {
6325 j = (i + 1) % hislen;
6326 history[histype][i] = history[histype][j];
6327 i = j;
6328 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006329 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330 if (--i < 0)
6331 i += hislen;
6332 hisidx[histype] = i;
6333 return TRUE;
6334}
6335
6336#endif /* FEAT_EVAL */
6337
6338#if defined(FEAT_CRYPT) || defined(PROTO)
6339/*
6340 * Very specific function to remove the value in ":set key=val" from the
6341 * history.
6342 */
6343 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006344remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345{
6346 char_u *p;
6347 int i;
6348
6349 i = hisidx[HIST_CMD];
6350 if (i < 0)
6351 return;
6352 p = history[HIST_CMD][i].hisstr;
6353 if (p != NULL)
6354 for ( ; *p; ++p)
6355 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6356 {
6357 p = vim_strchr(p + 3, '=');
6358 if (p == NULL)
6359 break;
6360 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006361 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 if (p[i] == '\\' && p[i + 1])
6363 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006364 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006365 --p;
6366 }
6367}
6368#endif
6369
6370#endif /* FEAT_CMDHIST */
6371
Bram Moolenaar064154c2016-03-19 22:50:43 +01006372#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006373/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006374 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006375 * ccline and put the previous value in prev_ccline.
6376 */
6377 static struct cmdline_info *
6378get_ccline_ptr(void)
6379{
6380 if ((State & CMDLINE) == 0)
6381 return NULL;
6382 if (ccline.cmdbuff != NULL)
6383 return &ccline;
6384 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6385 return &prev_ccline;
6386 return NULL;
6387}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006388#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006389
Bram Moolenaar064154c2016-03-19 22:50:43 +01006390#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006391/*
6392 * Get the current command line in allocated memory.
6393 * Only works when the command line is being edited.
6394 * Returns NULL when something is wrong.
6395 */
6396 char_u *
6397get_cmdline_str(void)
6398{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006399 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006400
Bram Moolenaaree91c332018-09-25 22:27:35 +02006401 if (cmdline_star > 0)
6402 return NULL;
6403 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006404 if (p == NULL)
6405 return NULL;
6406 return vim_strnsave(p->cmdbuff, p->cmdlen);
6407}
6408
6409/*
6410 * Get the current command line position, counted in bytes.
6411 * Zero is the first position.
6412 * Only works when the command line is being edited.
6413 * Returns -1 when something is wrong.
6414 */
6415 int
6416get_cmdline_pos(void)
6417{
6418 struct cmdline_info *p = get_ccline_ptr();
6419
6420 if (p == NULL)
6421 return -1;
6422 return p->cmdpos;
6423}
6424
6425/*
6426 * Set the command line byte position to "pos". Zero is the first position.
6427 * Only works when the command line is being edited.
6428 * Returns 1 when failed, 0 when OK.
6429 */
6430 int
6431set_cmdline_pos(
6432 int pos)
6433{
6434 struct cmdline_info *p = get_ccline_ptr();
6435
6436 if (p == NULL)
6437 return 1;
6438
6439 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6440 * changed the command line. */
6441 if (pos < 0)
6442 new_cmdpos = 0;
6443 else
6444 new_cmdpos = pos;
6445 return 0;
6446}
6447#endif
6448
6449#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6450/*
6451 * Get the current command-line type.
6452 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6453 * Only works when the command line is being edited.
6454 * Returns NUL when something is wrong.
6455 */
6456 int
6457get_cmdline_type(void)
6458{
6459 struct cmdline_info *p = get_ccline_ptr();
6460
6461 if (p == NULL)
6462 return NUL;
6463 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006464 return
6465# ifdef FEAT_EVAL
6466 (p->input_fn) ? '@' :
6467# endif
6468 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006469 return p->cmdfirstc;
6470}
6471#endif
6472
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6474/*
6475 * Get indices "num1,num2" that specify a range within a list (not a range of
6476 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6477 * Returns OK if parsed successfully, otherwise FAIL.
6478 */
6479 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006480get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481{
6482 int len;
6483 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006484 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485
6486 *str = skipwhite(*str);
6487 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6488 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006489 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 *str += len;
6491 *num1 = (int)num;
6492 first = TRUE;
6493 }
6494 *str = skipwhite(*str);
6495 if (**str == ',') /* parse "to" part of range */
6496 {
6497 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006498 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 if (len > 0)
6500 {
6501 *num2 = (int)num;
6502 *str = skipwhite(*str + len);
6503 }
6504 else if (!first) /* no number given at all */
6505 return FAIL;
6506 }
6507 else if (first) /* only one number given */
6508 *num2 = *num1;
6509 return OK;
6510}
6511#endif
6512
6513#if defined(FEAT_CMDHIST) || defined(PROTO)
6514/*
6515 * :history command - print a history
6516 */
6517 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006518ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519{
6520 histentry_T *hist;
6521 int histype1 = HIST_CMD;
6522 int histype2 = HIST_CMD;
6523 int hisidx1 = 1;
6524 int hisidx2 = -1;
6525 int idx;
6526 int i, j, k;
6527 char_u *end;
6528 char_u *arg = eap->arg;
6529
6530 if (hislen == 0)
6531 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006532 msg(_("'history' option is zero"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 return;
6534 }
6535
6536 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6537 {
6538 end = arg;
6539 while (ASCII_ISALPHA(*end)
6540 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6541 end++;
6542 i = *end;
6543 *end = NUL;
6544 histype1 = get_histtype(arg);
6545 if (histype1 == -1)
6546 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006547 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 {
6549 histype1 = 0;
6550 histype2 = HIST_COUNT-1;
6551 }
6552 else
6553 {
6554 *end = i;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006555 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 return;
6557 }
6558 }
6559 else
6560 histype2 = histype1;
6561 *end = i;
6562 }
6563 else
6564 end = arg;
6565 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6566 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006567 emsg(_(e_trailing));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 return;
6569 }
6570
6571 for (; !got_int && histype1 <= histype2; ++histype1)
6572 {
6573 STRCPY(IObuff, "\n # ");
6574 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
Bram Moolenaar32526b32019-01-19 17:43:09 +01006575 msg_puts_title((char *)IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 idx = hisidx[histype1];
6577 hist = history[histype1];
6578 j = hisidx1;
6579 k = hisidx2;
6580 if (j < 0)
6581 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6582 if (k < 0)
6583 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6584 if (idx >= 0 && j <= k)
6585 for (i = idx + 1; !got_int; ++i)
6586 {
6587 if (i == hislen)
6588 i = 0;
6589 if (hist[i].hisstr != NULL
6590 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6591 {
6592 msg_putchar('\n');
6593 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6594 hist[i].hisnum);
6595 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6596 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006597 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006598 else
6599 STRCAT(IObuff, hist[i].hisstr);
6600 msg_outtrans(IObuff);
6601 out_flush();
6602 }
6603 if (i == idx)
6604 break;
6605 }
6606 }
6607}
6608#endif
6609
6610#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006611/*
6612 * Buffers for history read from a viminfo file. Only valid while reading.
6613 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006614static histentry_T *viminfo_history[HIST_COUNT] =
6615 {NULL, NULL, NULL, NULL, NULL};
6616static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6617static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618static int viminfo_add_at_front = FALSE;
6619
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620/*
6621 * Translate a history type number to the associated character.
6622 */
6623 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006624hist_type2char(
6625 int type,
6626 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627{
6628 if (type == HIST_CMD)
6629 return ':';
6630 if (type == HIST_SEARCH)
6631 {
6632 if (use_question)
6633 return '?';
6634 else
6635 return '/';
6636 }
6637 if (type == HIST_EXPR)
6638 return '=';
6639 return '@';
6640}
6641
6642/*
6643 * Prepare for reading the history from the viminfo file.
6644 * This allocates history arrays to store the read history lines.
6645 */
6646 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006647prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 int i;
6650 int num;
6651 int type;
6652 int len;
6653
6654 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006655 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006656 if (asklen > hislen)
6657 asklen = hislen;
6658
6659 for (type = 0; type < HIST_COUNT; ++type)
6660 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006661 /* Count the number of empty spaces in the history list. Entries read
6662 * from viminfo previously are also considered empty. If there are
6663 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006665 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 num++;
6667 len = asklen;
6668 if (num > len)
6669 len = num;
6670 if (len <= 0)
6671 viminfo_history[type] = NULL;
6672 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006673 viminfo_history[type] = (histentry_T *)lalloc(
6674 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 if (viminfo_history[type] == NULL)
6676 len = 0;
6677 viminfo_hislen[type] = len;
6678 viminfo_hisidx[type] = 0;
6679 }
6680}
6681
6682/*
6683 * Accept a line from the viminfo, store it in the history array when it's
6684 * new.
6685 */
6686 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006687read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688{
6689 int type;
6690 long_u len;
6691 char_u *val;
6692 char_u *p;
6693
6694 type = hist_char2type(virp->vir_line[0]);
6695 if (viminfo_hisidx[type] < viminfo_hislen[type])
6696 {
6697 val = viminfo_readstring(virp, 1, TRUE);
6698 if (val != NULL && *val != NUL)
6699 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006700 int sep = (*val == ' ' ? NUL : *val);
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006703 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006704 {
6705 /* Need to re-allocate to append the separator byte. */
6706 len = STRLEN(val);
6707 p = lalloc(len + 2, TRUE);
6708 if (p != NULL)
6709 {
6710 if (type == HIST_SEARCH)
6711 {
6712 /* Search entry: Move the separator from the first
6713 * column to after the NUL. */
6714 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006715 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 }
6717 else
6718 {
6719 /* Not a search entry: No separator in the viminfo
6720 * file, add a NUL separator. */
6721 mch_memmove(p, val, (size_t)len + 1);
6722 p[len + 1] = NUL;
6723 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006724 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6725 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006726 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6727 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006728 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 }
6730 }
6731 }
6732 vim_free(val);
6733 }
6734 return viminfo_readline(virp);
6735}
6736
Bram Moolenaar07219f92013-04-14 23:19:36 +02006737/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006738 * Accept a new style history line from the viminfo, store it in the history
6739 * array when it's new.
6740 */
6741 void
6742handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006743 garray_T *values,
6744 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006745{
6746 int type;
6747 long_u len;
6748 char_u *val;
6749 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006750 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006751
6752 /* Check the format:
6753 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006754 if (values->ga_len < 4
6755 || vp[0].bv_type != BVAL_NR
6756 || vp[1].bv_type != BVAL_NR
6757 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6758 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006759 return;
6760
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006761 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006762 if (type >= HIST_COUNT)
6763 return;
6764 if (viminfo_hisidx[type] < viminfo_hislen[type])
6765 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006766 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006767 if (val != NULL && *val != NUL)
6768 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006769 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6770 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006771 int idx;
6772 int overwrite = FALSE;
6773
6774 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6775 {
6776 /* If lines were written by an older Vim we need to avoid
6777 * getting duplicates. See if the entry already exists. */
6778 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6779 {
6780 p = viminfo_history[type][idx].hisstr;
6781 if (STRCMP(val, p) == 0
6782 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6783 {
6784 overwrite = TRUE;
6785 break;
6786 }
6787 }
6788
6789 if (!overwrite)
6790 {
6791 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006792 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006793 p = lalloc(len + 2, TRUE);
6794 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006795 else
6796 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006797 if (p != NULL)
6798 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006799 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006800 if (!overwrite)
6801 {
6802 mch_memmove(p, val, (size_t)len + 1);
6803 /* Put the separator after the NUL. */
6804 p[len + 1] = sep;
6805 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006806 viminfo_history[type][idx].hisnum = 0;
6807 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 viminfo_hisidx[type]++;
6809 }
6810 }
6811 }
6812 }
6813 }
6814}
6815
6816/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006817 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006818 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006819 static void
6820concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821{
6822 int idx;
6823 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006824
6825 idx = hisidx[type] + viminfo_hisidx[type];
6826 if (idx >= hislen)
6827 idx -= hislen;
6828 else if (idx < 0)
6829 idx = hislen - 1;
6830 if (viminfo_add_at_front)
6831 hisidx[type] = idx;
6832 else
6833 {
6834 if (hisidx[type] == -1)
6835 hisidx[type] = hislen - 1;
6836 do
6837 {
6838 if (history[type][idx].hisstr != NULL
6839 || history[type][idx].viminfo)
6840 break;
6841 if (++idx == hislen)
6842 idx = 0;
6843 } while (idx != hisidx[type]);
6844 if (idx != hisidx[type] && --idx < 0)
6845 idx = hislen - 1;
6846 }
6847 for (i = 0; i < viminfo_hisidx[type]; i++)
6848 {
6849 vim_free(history[type][idx].hisstr);
6850 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6851 history[type][idx].viminfo = TRUE;
6852 history[type][idx].time_set = viminfo_history[type][i].time_set;
6853 if (--idx < 0)
6854 idx = hislen - 1;
6855 }
6856 idx += 1;
6857 idx %= hislen;
6858 for (i = 0; i < viminfo_hisidx[type]; i++)
6859 {
6860 history[type][idx++].hisnum = ++hisnum[type];
6861 idx %= hislen;
6862 }
6863}
6864
6865#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6866 static int
6867#ifdef __BORLANDC__
6868_RTLENTRYF
6869#endif
6870sort_hist(const void *s1, const void *s2)
6871{
6872 histentry_T *p1 = *(histentry_T **)s1;
6873 histentry_T *p2 = *(histentry_T **)s2;
6874
6875 if (p1->time_set < p2->time_set) return -1;
6876 if (p1->time_set > p2->time_set) return 1;
6877 return 0;
6878}
6879#endif
6880
6881/*
6882 * Merge history lines from viminfo and lines typed in this Vim based on the
6883 * timestamp;
6884 */
6885 static void
6886merge_history(int type)
6887{
6888 int max_len;
6889 histentry_T **tot_hist;
6890 histentry_T *new_hist;
6891 int i;
6892 int len;
6893
6894 /* Make one long list with all entries. */
6895 max_len = hislen + viminfo_hisidx[type];
6896 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6897 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6898 if (tot_hist == NULL || new_hist == NULL)
6899 {
6900 vim_free(tot_hist);
6901 vim_free(new_hist);
6902 return;
6903 }
6904 for (i = 0; i < viminfo_hisidx[type]; i++)
6905 tot_hist[i] = &viminfo_history[type][i];
6906 len = i;
6907 for (i = 0; i < hislen; i++)
6908 if (history[type][i].hisstr != NULL)
6909 tot_hist[len++] = &history[type][i];
6910
6911 /* Sort the list on timestamp. */
6912 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6913
6914 /* Keep the newest ones. */
6915 for (i = 0; i < hislen; i++)
6916 {
6917 if (i < len)
6918 {
6919 new_hist[i] = *tot_hist[i];
6920 tot_hist[i]->hisstr = NULL;
6921 if (new_hist[i].hisnum == 0)
6922 new_hist[i].hisnum = ++hisnum[type];
6923 }
6924 else
6925 clear_hist_entry(&new_hist[i]);
6926 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006927 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006928
6929 /* Free what is not kept. */
6930 for (i = 0; i < viminfo_hisidx[type]; i++)
6931 vim_free(viminfo_history[type][i].hisstr);
6932 for (i = 0; i < hislen; i++)
6933 vim_free(history[type][i].hisstr);
6934 vim_free(history[type]);
6935 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006936 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006937}
6938
6939/*
6940 * Finish reading history lines from viminfo. Not used when writing viminfo.
6941 */
6942 void
6943finish_viminfo_history(vir_T *virp)
6944{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006946 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
6948 for (type = 0; type < HIST_COUNT; ++type)
6949 {
6950 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006951 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006952
6953 if (merge)
6954 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006956 concat_history(type);
6957
Bram Moolenaard23a8232018-02-10 18:45:26 +01006958 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02006959 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961}
6962
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006963/*
6964 * Write history to viminfo file in "fp".
6965 * When "merge" is TRUE merge history lines with a previously read viminfo
6966 * file, data is in viminfo_history[].
6967 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
6968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006970write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971{
6972 int i;
6973 int type;
6974 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006975 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977 init_history();
6978 if (hislen == 0)
6979 return;
6980 for (type = 0; type < HIST_COUNT; ++type)
6981 {
6982 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
6983 if (num_saved == 0)
6984 continue;
6985 if (num_saved < 0) /* Use default */
6986 num_saved = hislen;
6987 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
6988 type == HIST_CMD ? _("Command Line") :
6989 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006990 type == HIST_EXPR ? _("Expression") :
6991 type == HIST_INPUT ? _("Input Line") :
6992 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 if (num_saved > hislen)
6994 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02006995
6996 /*
6997 * Merge typed and viminfo history:
6998 * round 1: history of typed commands.
6999 * round 2: history from recently read viminfo.
7000 */
7001 for (round = 1; round <= 2; ++round)
7002 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007003 if (round == 1)
7004 /* start at newest entry, somewhere in the list */
7005 i = hisidx[type];
7006 else if (viminfo_hisidx[type] > 0)
7007 /* start at newest entry, first in the list */
7008 i = 0;
7009 else
7010 /* empty list */
7011 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007012 if (i >= 0)
7013 while (num_saved > 0
7014 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007016 char_u *p;
7017 time_t timestamp;
7018 int c = NUL;
7019
7020 if (round == 1)
7021 {
7022 p = history[type][i].hisstr;
7023 timestamp = history[type][i].time_set;
7024 }
7025 else
7026 {
7027 p = viminfo_history[type] == NULL ? NULL
7028 : viminfo_history[type][i].hisstr;
7029 timestamp = viminfo_history[type] == NULL ? 0
7030 : viminfo_history[type][i].time_set;
7031 }
7032
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007033 if (p != NULL && (round == 2
7034 || !merge
7035 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007037 --num_saved;
7038 fputc(hist_type2char(type, TRUE), fp);
7039 /* For the search history: put the separator in the
7040 * second column; use a space if there isn't one. */
7041 if (type == HIST_SEARCH)
7042 {
7043 c = p[STRLEN(p) + 1];
7044 putc(c == NUL ? ' ' : c, fp);
7045 }
7046 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007047
7048 {
7049 char cbuf[NUMBUFLEN];
7050
7051 /* New style history with a bar line. Format:
7052 * |{bartype},{histtype},{timestamp},{separator},"text" */
7053 if (c == NUL)
7054 cbuf[0] = NUL;
7055 else
7056 sprintf(cbuf, "%d", c);
7057 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7058 type, (long)timestamp, cbuf);
7059 barline_writestring(fp, p, LSIZE - 20);
7060 putc('\n', fp);
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007063 if (round == 1)
7064 {
7065 /* Decrement index, loop around and stop when back at
7066 * the start. */
7067 if (--i < 0)
7068 i = hislen - 1;
7069 if (i == hisidx[type])
7070 break;
7071 }
7072 else
7073 {
7074 /* Increment index. Stop at the end in the while. */
7075 ++i;
7076 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007078 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007079 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007080 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007081 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007082 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007083 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 }
7085}
7086#endif /* FEAT_VIMINFO */
7087
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088#if defined(FEAT_CMDWIN) || defined(PROTO)
7089/*
7090 * Open a window on the current command line and history. Allow editing in
7091 * the window. Returns when the window is closed.
7092 * Returns:
7093 * CR if the command is to be executed
7094 * Ctrl_C if it is to be abandoned
7095 * K_IGNORE if editing continues
7096 */
7097 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007098open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007100 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007102 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103 win_T *wp;
7104 int i;
7105 linenr_T lnum;
7106 int histtype;
7107 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108 int save_restart_edit = restart_edit;
7109 int save_State = State;
7110 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007111#ifdef FEAT_RIGHTLEFT
7112 int save_cmdmsg_rl = cmdmsg_rl;
7113#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007114#ifdef FEAT_FOLDING
7115 int save_KeyTyped;
7116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118 /* Can't do this recursively. Can't do it when typing a password. */
7119 if (cmdwin_type != 0
7120# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7121 || cmdline_star > 0
7122# endif
7123 )
7124 {
7125 beep_flush();
7126 return K_IGNORE;
7127 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007128 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007129
7130 /* Save current window sizes. */
7131 win_size_save(&winsizes);
7132
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007134 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007135
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007136#if defined(FEAT_INS_EXPAND)
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007137 // When using completion in Insert mode with <C-R>=<C-F> one can open the
7138 // command line window, but we don't want the popup menu then.
7139 pum_undisplay();
Bram Moolenaarf88af6e2019-01-22 22:55:00 +01007140#endif
Bram Moolenaar9e26f7d2019-01-22 22:08:09 +01007141
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007142 /* don't use a new tab page */
7143 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007144 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007145
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 /* Create a window for the command-line buffer. */
7147 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7148 {
7149 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007150 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 return K_IGNORE;
7152 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007153 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007154
7155 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007156 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007157 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007160#ifdef FEAT_FOLDING
7161 curwin->w_p_fen = FALSE;
7162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007164 curwin->w_p_rl = cmdmsg_rl;
7165 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007167 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007170 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007171 /* But don't allow switching to another buffer. */
7172 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaar46152342005-09-07 21:18:43 +00007174 /* Showing the prompt may have set need_wait_return, reset it. */
7175 need_wait_return = FALSE;
7176
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007177 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7179 {
7180 if (p_wc == TAB)
7181 {
7182 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7183 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7184 }
7185 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7186 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007187 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007188
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007189 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7190 * sets 'textwidth' to 78). */
7191 curbuf->b_p_tw = 0;
7192
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 /* Fill the buffer with the history. */
7194 init_history();
7195 if (hislen > 0)
7196 {
7197 i = hisidx[histtype];
7198 if (i >= 0)
7199 {
7200 lnum = 0;
7201 do
7202 {
7203 if (++i == hislen)
7204 i = 0;
7205 if (history[histtype][i].hisstr != NULL)
7206 ml_append(lnum++, history[histtype][i].hisstr,
7207 (colnr_T)0, FALSE);
7208 }
7209 while (i != hisidx[histtype]);
7210 }
7211 }
7212
7213 /* Replace the empty last line with the current command-line and put the
7214 * cursor there. */
7215 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7216 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7217 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007218 changed_line_abv_curs();
7219 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007220 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222 /* No Ex mode here! */
7223 exmode_active = 0;
7224
7225 State = NORMAL;
7226# ifdef FEAT_MOUSE
7227 setmouse();
7228# endif
7229
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007231 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007232 if (restart_edit != 0) /* autocmd with ":startinsert" */
7233 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234
7235 i = RedrawingDisabled;
7236 RedrawingDisabled = 0;
7237
7238 /*
7239 * Call the main loop until <CR> or CTRL-C is typed.
7240 */
7241 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007242 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243
7244 RedrawingDisabled = i;
7245
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007246# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007247 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007248# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007249
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007251 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007252
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007253# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007254 /* Restore KeyTyped in case it is modified by autocommands */
7255 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256# endif
7257
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259 exmode_active = save_exmode;
7260
Bram Moolenaarf9821062008-06-20 16:31:07 +00007261 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007263 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 {
7265 cmdwin_result = Ctrl_C;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007266 emsg(_("E199: Active window or buffer deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007267 }
7268 else
7269 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007270# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271 /* autocmds may abort script processing */
7272 if (aborting() && cmdwin_result != K_IGNORE)
7273 cmdwin_result = Ctrl_C;
7274# endif
7275 /* Set the new command line from the cmdline buffer. */
7276 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007277 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007278 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007279 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7280
7281 if (histtype == HIST_CMD)
7282 {
7283 /* Execute the command directly. */
7284 ccline.cmdbuff = vim_strsave((char_u *)p);
7285 cmdwin_result = CAR;
7286 }
7287 else
7288 {
7289 /* First need to cancel what we were doing. */
7290 ccline.cmdbuff = NULL;
7291 stuffcharReadbuff(':');
7292 stuffReadbuff((char_u *)p);
7293 stuffcharReadbuff(CAR);
7294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007295 }
7296 else if (cmdwin_result == K_XF2) /* :qa typed */
7297 {
7298 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7299 cmdwin_result = CAR;
7300 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007301 else if (cmdwin_result == Ctrl_C)
7302 {
7303 /* :q or :close, don't execute any command
7304 * and don't modify the cmd window. */
7305 ccline.cmdbuff = NULL;
7306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 else
7308 ccline.cmdbuff = vim_strsave(ml_get_curline());
7309 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007310 {
7311 ccline.cmdbuff = vim_strsave((char_u *)"");
7312 ccline.cmdlen = 0;
7313 ccline.cmdbufflen = 1;
7314 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 else
7318 {
7319 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7320 ccline.cmdbufflen = ccline.cmdlen + 1;
7321 ccline.cmdpos = curwin->w_cursor.col;
7322 if (ccline.cmdpos > ccline.cmdlen)
7323 ccline.cmdpos = ccline.cmdlen;
7324 if (cmdwin_result == K_IGNORE)
7325 {
7326 set_cmdspos_cursor();
7327 redrawcmd();
7328 }
7329 }
7330
Bram Moolenaar071d4272004-06-13 20:20:40 +00007331 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007332 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007333# ifdef FEAT_CONCEAL
7334 /* Avoid command-line window first character being concealed. */
7335 curwin->w_p_cole = 0;
7336# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007337 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007338 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007339 win_goto(old_curwin);
7340 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007341
7342 /* win_close() may have already wiped the buffer when 'bh' is
7343 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007344 if (bufref_valid(&bufref))
7345 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346
7347 /* Restore window sizes. */
7348 win_size_restore(&winsizes);
7349
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007350 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 }
7352
7353 ga_clear(&winsizes);
7354 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007355# ifdef FEAT_RIGHTLEFT
7356 cmdmsg_rl = save_cmdmsg_rl;
7357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007358
7359 State = save_State;
7360# ifdef FEAT_MOUSE
7361 setmouse();
7362# endif
7363
7364 return cmdwin_result;
7365}
7366#endif /* FEAT_CMDWIN */
7367
7368/*
7369 * Used for commands that either take a simple command string argument, or:
7370 * cmd << endmarker
7371 * {script}
7372 * endmarker
7373 * Returns a pointer to allocated memory with {script} or NULL.
7374 */
7375 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007376script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007377{
7378 char_u *theline;
7379 char *end_pattern = NULL;
7380 char dot[] = ".";
7381 garray_T ga;
7382
7383 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7384 return NULL;
7385
7386 ga_init2(&ga, 1, 0x400);
7387
7388 if (cmd[2] != NUL)
7389 end_pattern = (char *)skipwhite(cmd + 2);
7390 else
7391 end_pattern = dot;
7392
7393 for (;;)
7394 {
7395 theline = eap->getline(
7396#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007397 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398#endif
7399 NUL, eap->cookie, 0);
7400
7401 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007402 {
7403 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406
7407 ga_concat(&ga, theline);
7408 ga_append(&ga, '\n');
7409 vim_free(theline);
7410 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007411 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007412
7413 return (char_u *)ga.ga_data;
7414}