blob: bfda942783c126e710702ebd28d1819b5a1e674b [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
84#ifdef FEAT_FKMAP
85static int cmd_fkmap = 0; /* Farsi mapping during command line */
86#endif
87
Bram Moolenaar438d1762018-09-30 17:11:48 +020088static char_u *getcmdline_int(int firstc, long count, int indent, int init_ccline);
Bram Moolenaard25c16e2016-01-29 22:13:30 +010089static int cmdline_charsize(int idx);
90static void set_cmdspos(void);
91static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010093static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000094#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +010095static void alloc_cmdbuff(int len);
96static int realloc_cmdbuff(int len);
97static void draw_cmdline(int start, int len);
98static void save_cmdline(struct cmdline_info *ccp);
99static void restore_cmdline(struct cmdline_info *ccp);
100static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100102static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100104static void redrawcmdprompt(void);
105static void cursorcmd(void);
106static int ccheck_abbr(int);
107static int nextwild(expand_T *xp, int type, int options, int escape);
108static void escape_fname(char_u **pp);
109static int showmatches(expand_T *xp, int wildmenu);
110static void set_expand_context(expand_T *xp);
111static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
112static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100114static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100115static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100116static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200117# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100118static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200119# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100121static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
122static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123# endif
124#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200125#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200130static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131#endif
132
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200133#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
134static int
135#ifdef __BORLANDC__
136_RTLENTRYF
137#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100138sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200139#endif
140
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200141
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200142 static void
143trigger_cmd_autocmd(int typechar, int evt)
144{
145 char_u typestr[2];
146
147 typestr[0] = typechar;
148 typestr[1] = NUL;
149 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
150}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200151
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200153 * Abandon the command line.
154 */
155 static void
156abandon_cmdline(void)
157{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100158 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200159 if (msg_scrolled == 0)
160 compute_cmdrow();
161 MSG("");
162 redraw_cmdline = TRUE;
163}
164
Bram Moolenaaree219b02017-12-17 14:55:01 +0100165#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200166/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100167 * Guess that the pattern matches everything. Only finds specific cases, such
168 * as a trailing \|, which can happen while typing a pattern.
169 */
170 static int
171empty_pattern(char_u *p)
172{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100173 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100174
175 /* remove trailing \v and the like */
176 while (n >= 2 && p[n - 2] == '\\'
177 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
178 n -= 2;
179 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
180}
181
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200182// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200183typedef struct {
184 colnr_T vs_curswant;
185 colnr_T vs_leftcol;
186 linenr_T vs_topline;
187# ifdef FEAT_DIFF
188 int vs_topfill;
189# endif
190 linenr_T vs_botline;
191 linenr_T vs_empty_rows;
192} viewstate_T;
193
194 static void
195save_viewstate(viewstate_T *vs)
196{
197 vs->vs_curswant = curwin->w_curswant;
198 vs->vs_leftcol = curwin->w_leftcol;
199 vs->vs_topline = curwin->w_topline;
200# ifdef FEAT_DIFF
201 vs->vs_topfill = curwin->w_topfill;
202# endif
203 vs->vs_botline = curwin->w_botline;
204 vs->vs_empty_rows = curwin->w_empty_rows;
205}
206
207 static void
208restore_viewstate(viewstate_T *vs)
209{
210 curwin->w_curswant = vs->vs_curswant;
211 curwin->w_leftcol = vs->vs_leftcol;
212 curwin->w_topline = vs->vs_topline;
213# ifdef FEAT_DIFF
214 curwin->w_topfill = vs->vs_topfill;
215# endif
216 curwin->w_botline = vs->vs_botline;
217 curwin->w_empty_rows = vs->vs_empty_rows;
218}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200219
220// Struct to store the state of 'incsearch' highlighting.
221typedef struct {
222 pos_T search_start; // where 'incsearch' starts searching
223 pos_T save_cursor;
224 viewstate_T init_viewstate;
225 viewstate_T old_viewstate;
226 pos_T match_start;
227 pos_T match_end;
228 int did_incsearch;
229 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200230 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200231} incsearch_state_T;
232
233 static void
234init_incsearch_state(incsearch_state_T *is_state)
235{
236 is_state->match_start = curwin->w_cursor;
237 is_state->did_incsearch = FALSE;
238 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200239 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200240 CLEAR_POS(&is_state->match_end);
241 is_state->save_cursor = curwin->w_cursor; // may be restored later
242 is_state->search_start = curwin->w_cursor;
243 save_viewstate(&is_state->init_viewstate);
244 save_viewstate(&is_state->old_viewstate);
245}
246
247/*
248 * First move cursor to end of match, then to the start. This
249 * moves the whole match onto the screen when 'nowrap' is set.
250 */
251 static void
252set_search_match(pos_T *t)
253{
254 t->lnum += search_match_lines;
255 t->col = search_match_endcol;
256 if (t->lnum > curbuf->b_ml.ml_line_count)
257 {
258 t->lnum = curbuf->b_ml.ml_line_count;
259 coladvance((colnr_T)MAXCOL);
260 }
261}
262
263/*
264 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200265 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar198cb662018-09-06 21:44:17 +0200266 * May change the last search pattern.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200267 */
268 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200269do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
270 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200271{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200272 char_u *cmd;
273 cmdmod_T save_cmdmod = cmdmod;
274 char_u *p;
275 int delim_optional = FALSE;
276 int delim;
277 char_u *end;
278 char_u *dummy;
279 exarg_T ea;
280 pos_T save_cursor;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200281 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200282
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200283 *skiplen = 0;
284 *patlen = ccline.cmdlen;
285
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200286 if (!p_is || cmd_silent)
287 return FALSE;
288
289 // by default search all lines
290 search_first_line = 0;
291 search_last_line = MAXLNUM;
292
293 if (firstc == '/' || firstc == '?')
294 return TRUE;
295 if (firstc != ':')
296 return FALSE;
297
298 vim_memset(&ea, 0, sizeof(ea));
299 ea.line1 = 1;
300 ea.line2 = 1;
301 ea.cmd = ccline.cmdbuff;
302 ea.addr_type = ADDR_LINES;
303
304 parse_command_modifiers(&ea, &dummy, TRUE);
305 cmdmod = save_cmdmod;
306
307 cmd = skip_range(ea.cmd, NULL);
308 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
309 return FALSE;
310
311 // Skip over "substitute" to find the pattern separator.
312 for (p = cmd; ASCII_ISALPHA(*p); ++p)
313 ;
314 if (*skipwhite(p) == NUL)
315 return FALSE;
316
317 if (STRNCMP(cmd, "substitute", p - cmd) == 0
318 || STRNCMP(cmd, "smagic", p - cmd) == 0
319 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
320 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200321 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200322 if (*cmd == 's' && cmd[1] == 'm')
323 p_magic = TRUE;
324 else if (*cmd == 's' && cmd[1] == 'n')
325 p_magic = FALSE;
326 }
327 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
328 {
329 // skip over flags
330 while (ASCII_ISALPHA(*(p = skipwhite(p))))
331 ++p;
332 if (*p == NUL)
333 return FALSE;
334 }
335 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
336 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
337 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
338 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
339 || STRNCMP(cmd, "global", p - cmd) == 0)
340 {
341 // skip over "!"
342 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200343 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200344 p++;
345 if (*skipwhite(p) == NUL)
346 return FALSE;
347 }
348 if (*cmd != 'g')
349 delim_optional = TRUE;
350 }
351 else
352 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200353
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200354 p = skipwhite(p);
355 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
356 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200357
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200358 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200359
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200360 if (end == p && !use_last_pat)
361 return FALSE;
362
363 // Don't do 'hlsearch' highlighting if the pattern matches everything.
364 if (!use_last_pat)
365 {
366 char c = *end;
367 int empty;
368
369 *end = NUL;
370 empty = empty_pattern(p);
371 *end = c;
372 if (empty)
373 return FALSE;
374 }
375
376 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200377 *skiplen = (int)(p - ccline.cmdbuff);
378 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200379
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200380 // parse the address range
381 save_cursor = curwin->w_cursor;
382 curwin->w_cursor = is_state->search_start;
Bram Moolenaar50eb16c2018-09-15 15:42:40 +0200383 parse_cmd_address(&ea, &dummy, TRUE);
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200384 if (ea.addr_count > 0)
385 {
386 // Allow for reverse match.
387 if (ea.line2 < ea.line1)
388 {
389 search_first_line = ea.line2;
390 search_last_line = ea.line1;
391 }
392 else
393 {
394 search_first_line = ea.line1;
395 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200396 }
397 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200398 else if (cmd[0] == 's' && cmd[1] != 'o')
399 {
400 // :s defaults to the current line
401 search_first_line = curwin->w_cursor.lnum;
402 search_last_line = curwin->w_cursor.lnum;
403 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200404
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200405 curwin->w_cursor = save_cursor;
406 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200407}
408
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200409 static void
410finish_incsearch_highlighting(
411 int gotesc,
412 incsearch_state_T *is_state,
413 int call_update_screen)
414{
415 if (is_state->did_incsearch)
416 {
417 is_state->did_incsearch = FALSE;
418 if (gotesc)
419 curwin->w_cursor = is_state->save_cursor;
420 else
421 {
422 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
423 {
424 // put the '" mark at the original position
425 curwin->w_cursor = is_state->save_cursor;
426 setpcmark();
427 }
428 curwin->w_cursor = is_state->search_start;
429 }
430 restore_viewstate(&is_state->old_viewstate);
431 highlight_match = FALSE;
Bram Moolenaarf13daa42018-08-31 22:09:54 +0200432
433 // by default search all lines
434 search_first_line = 0;
435 search_last_line = MAXLNUM;
436
437 p_magic = is_state->magic_save;
438
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200439 validate_cursor(); /* needed for TAB */
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200440 redraw_all_later(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200441 if (call_update_screen)
442 update_screen(SOME_VALID);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 }
444}
445
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200446/*
447 * Do 'incsearch' highlighting if desired.
448 */
449 static void
450may_do_incsearch_highlighting(
451 int firstc,
452 long count,
453 incsearch_state_T *is_state)
454{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200455 int skiplen, patlen;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200456 int found; // do_search() result
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200457 pos_T end_pos;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200458#ifdef FEAT_RELTIME
459 proftime_T tm;
460#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200461 int next_char;
462 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200463
Bram Moolenaar198cb662018-09-06 21:44:17 +0200464 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100465 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200466 save_last_search_pattern();
467
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200468 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200469 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200470 restore_last_search_pattern();
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200471 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200473 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200474
475 // If there is a character waiting, search and redraw later.
476 if (char_avail())
477 {
Bram Moolenaar198cb662018-09-06 21:44:17 +0200478 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200479 is_state->incsearch_postponed = TRUE;
480 return;
481 }
482 is_state->incsearch_postponed = FALSE;
483
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200484 if (search_first_line == 0)
485 // start at the original cursor position
486 curwin->w_cursor = is_state->search_start;
Bram Moolenaar1c299432018-10-28 14:36:09 +0100487 else if (search_first_line > curbuf->b_ml.ml_line_count)
488 {
489 // start after the last line
490 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
491 curwin->w_cursor.col = MAXCOL;
492 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200493 else
494 {
495 // start at the first line in the range
496 curwin->w_cursor.lnum = search_first_line;
497 curwin->w_cursor.col = 0;
498 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200499
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200500 // Use the previous pattern for ":s//".
501 next_char = ccline.cmdbuff[skiplen + patlen];
502 use_last_pat = patlen == 0 && skiplen > 0
503 && ccline.cmdbuff[skiplen - 1] == next_char;
504
505 // If there is no pattern, don't do anything.
506 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200507 {
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200508 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200509 set_no_hlsearch(TRUE); // turn off previous highlight
510 redraw_all_later(SOME_VALID);
511 }
512 else
513 {
514 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
515
516 cursor_off(); // so the user knows we're busy
517 out_flush();
518 ++emsg_off; // so it doesn't beep if bad expr
519#ifdef FEAT_RELTIME
520 // Set the time limit to half a second.
521 profile_setlimit(500L, &tm);
522#endif
523 if (!p_hls)
524 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200525 if (search_first_line != 0)
526 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200527 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200528 found = do_search(NULL, firstc == ':' ? '/' : firstc,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200529 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200530#ifdef FEAT_RELTIME
531 &tm, NULL
532#else
533 NULL, NULL
534#endif
535 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200536 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200537 --emsg_off;
538
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200539 if (curwin->w_cursor.lnum < search_first_line
540 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200541 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200542 // match outside of address range
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200543 found = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200544 curwin->w_cursor = is_state->search_start;
545 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200546
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200547 // if interrupted while searching, behave like it failed
548 if (got_int)
549 {
550 (void)vpeekc(); // remove <C-C> from input stream
551 got_int = FALSE; // don't abandon the command line
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200552 found = 0;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200553 }
554 else if (char_avail())
555 // cancelled searching because a char was typed
556 is_state->incsearch_postponed = TRUE;
557 }
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200558 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200559 highlight_match = TRUE; // highlight position
560 else
561 highlight_match = FALSE; // remove highlight
562
563 // First restore the old curwin values, so the screen is positioned in the
564 // same way as the actual search command.
565 restore_viewstate(&is_state->old_viewstate);
566 changed_cline_bef_curs();
567 update_topline();
568
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200569 if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200570 {
571 pos_T save_pos = curwin->w_cursor;
572
573 is_state->match_start = curwin->w_cursor;
574 set_search_match(&curwin->w_cursor);
575 validate_cursor();
576 end_pos = curwin->w_cursor;
577 is_state->match_end = end_pos;
578 curwin->w_cursor = save_pos;
579 }
580 else
581 end_pos = curwin->w_cursor; // shutup gcc 4
582
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200583 // Disable 'hlsearch' highlighting if the pattern matches everything.
584 // Avoids a flash when typing "foo\|".
585 if (!use_last_pat)
586 {
587 next_char = ccline.cmdbuff[skiplen + patlen];
588 ccline.cmdbuff[skiplen + patlen] = NUL;
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200589 if (empty_pattern(ccline.cmdbuff) && !no_hlsearch)
590 {
591 redraw_all_later(SOME_VALID);
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200592 set_no_hlsearch(TRUE);
Bram Moolenaar65985ac2018-09-16 17:08:04 +0200593 }
Bram Moolenaar4edfe2d2018-08-23 20:55:45 +0200594 ccline.cmdbuff[skiplen + patlen] = next_char;
595 }
596
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200597 validate_cursor();
598 // May redraw the status line to show the cursor position.
599 if (p_ru && curwin->w_status_height > 0)
600 curwin->w_redr_status = TRUE;
601
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 update_screen(SOME_VALID);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200603 restore_last_search_pattern();
604
Bram Moolenaar99f043a2018-09-09 15:54:14 +0200605 // Leave it at the end to make CTRL-R CTRL-W work. But not when beyond the
606 // end of the pattern, e.g. for ":s/pat/".
607 if (ccline.cmdbuff[skiplen + patlen] != NUL)
608 curwin->w_cursor = is_state->search_start;
609 else if (found != 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610 curwin->w_cursor = end_pos;
611
612 msg_starthere();
613 redrawcmdline();
614 is_state->did_incsearch = TRUE;
615}
616
617/*
618 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
619 * or previous match.
620 * Returns FAIL when jumping to cmdline_not_changed;
621 */
622 static int
623may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200624 int firstc,
625 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200626 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200627 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200628{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200629 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200630 pos_T t;
631 char_u *pat;
632 int search_flags = SEARCH_NOOF;
633 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200634 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200635
Bram Moolenaar198cb662018-09-06 21:44:17 +0200636 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100637 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200638 save_last_search_pattern();
639
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200640 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200641 {
642 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200643 return OK;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200644 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200645 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar198cb662018-09-06 21:44:17 +0200646 {
647 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200648 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200649 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200650
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200651 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200652 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200654 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200655 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200656 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200657 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200658 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200659
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200660 cursor_off();
661 out_flush();
662 if (c == Ctrl_G)
663 {
664 t = is_state->match_end;
665 if (LT_POS(is_state->match_start, is_state->match_end))
666 // Start searching at the end of the match not at the beginning of
667 // the next column.
668 (void)decl(&t);
669 search_flags += SEARCH_COL;
670 }
671 else
672 t = is_state->match_start;
673 if (!p_hls)
674 search_flags += SEARCH_KEEP;
675 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200676 save = pat[patlen];
677 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200678 i = searchit(curwin, curbuf, &t,
679 c == Ctrl_G ? FORWARD : BACKWARD,
680 pat, count, search_flags,
681 RE_SEARCH, 0, NULL, NULL);
682 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200683 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200684 if (i)
685 {
686 is_state->search_start = is_state->match_start;
687 is_state->match_end = t;
688 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200689 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200690 {
691 // Move just before the current match, so that when nv_search
692 // finishes the cursor will be put back on the match.
693 is_state->search_start = t;
694 (void)decl(&is_state->search_start);
695 }
696 else if (c == Ctrl_G && firstc == '?')
697 {
698 // Move just after the current match, so that when nv_search
699 // finishes the cursor will be put back on the match.
700 is_state->search_start = t;
701 (void)incl(&is_state->search_start);
702 }
703 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
704 {
705 // wrap around
706 is_state->search_start = t;
707 if (firstc == '?')
708 (void)incl(&is_state->search_start);
709 else
710 (void)decl(&is_state->search_start);
711 }
712
713 set_search_match(&is_state->match_end);
714 curwin->w_cursor = is_state->match_start;
715 changed_cline_bef_curs();
716 update_topline();
717 validate_cursor();
718 highlight_match = TRUE;
719 save_viewstate(&is_state->old_viewstate);
720 update_screen(NOT_VALID);
721 redrawcmdline();
722 }
723 else
724 vim_beep(BO_ERROR);
725 restore_last_search_pattern();
726 return FAIL;
727}
728
729/*
730 * When CTRL-L typed: add character from the match to the pattern.
731 * May set "*c" to the added character.
732 * Return OK when jumping to cmdline_not_changed.
733 */
734 static int
735may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
736{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200737 int skiplen, patlen;
738
Bram Moolenaar198cb662018-09-06 21:44:17 +0200739 // Parsing range may already set the last search pattern.
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100740 // NOTE: must call restore_last_search_pattern() before returning!
Bram Moolenaar198cb662018-09-06 21:44:17 +0200741 save_last_search_pattern();
742
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200743 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar198cb662018-09-06 21:44:17 +0200744 {
745 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200746 return FAIL;
Bram Moolenaar198cb662018-09-06 21:44:17 +0200747 }
Bram Moolenaar01a060d2018-11-30 21:57:55 +0100748 restore_last_search_pattern();
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200749
750 // Add a character from under the cursor for 'incsearch'.
751 if (is_state->did_incsearch)
752 {
753 curwin->w_cursor = is_state->match_end;
754 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
755 {
756 *c = gchar_cursor();
757
758 // If 'ignorecase' and 'smartcase' are set and the
759 // command line has no uppercase characters, convert
760 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200761 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200762 *c = MB_TOLOWER(*c);
763 if (*c != NUL)
764 {
765 if (*c == firstc || vim_strchr((char_u *)(
766 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
767 {
768 // put a backslash before special characters
769 stuffcharReadbuff(*c);
770 *c = '\\';
771 }
772 return FAIL;
773 }
774 }
775 }
776 return OK;
777}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200778#endif
779
Bram Moolenaard3dc0622018-09-30 17:45:30 +0200780 void
781cmdline_init(void)
782{
783 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
784}
785
Bram Moolenaar66216052017-12-16 16:33:44 +0100786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 * getcmdline() - accept a command line starting with firstc.
788 *
789 * firstc == ':' get ":" command line.
790 * firstc == '/' or '?' get search pattern
791 * firstc == '=' get expression
792 * firstc == '@' get text for input() function
793 * firstc == '>' get text for debug mode
794 * firstc == NUL get text for :insert command
795 * firstc == -1 like NUL, and break on CTRL-C
796 *
797 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
798 * command line.
799 *
800 * Careful: getcmdline() can be called recursively!
801 *
802 * Return pointer to allocated string if there is a commandline, NULL
803 * otherwise.
804 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100806getcmdline(
807 int firstc,
Bram Moolenaar438d1762018-09-30 17:11:48 +0200808 long count, // only used for incremental search
809 int indent) // indent for inside conditionals
810{
811 return getcmdline_int(firstc, count, indent, TRUE);
812}
813
814 static char_u *
815getcmdline_int(
816 int firstc,
817 long count UNUSED, // only used for incremental search
818 int indent, // indent for inside conditionals
819 int init_ccline) // clear ccline first
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820{
821 int c;
822 int i;
823 int j;
824 int gotesc = FALSE; /* TRUE when <ESC> just typed */
825 int do_abbr; /* when TRUE check for abbr. */
826#ifdef FEAT_CMDHIST
827 char_u *lookfor = NULL; /* string to match */
828 int hiscnt; /* current history line in use */
829 int histype; /* history type to be used */
830#endif
831#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200832 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833#endif
834 int did_wild_list = FALSE; /* did wild_list() recently */
835 int wim_index = 0; /* index in wim_flags[] */
836 int res;
837 int save_msg_scroll = msg_scroll;
838 int save_State = State; /* remember State when called */
839 int some_key_typed = FALSE; /* one of the keys was typed */
840#ifdef FEAT_MOUSE
841 /* mouse drag and release events are ignored, unless they are
842 * preceded with a mouse down event */
843 int ignore_drag_release = TRUE;
844#endif
845#ifdef FEAT_EVAL
846 int break_ctrl_c = FALSE;
847#endif
848 expand_T xpc;
849 long *b_im_ptr = NULL;
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000850 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +0200851 int did_save_ccline = FALSE;
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200852 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853
Bram Moolenaar438d1762018-09-30 17:11:48 +0200854 if (ccline.cmdbuff != NULL)
855 {
856 // Being called recursively. Since ccline is global, we need to save
857 // the current buffer and restore it when returning.
858 save_cmdline(&save_ccline);
859 did_save_ccline = TRUE;
860 }
861 if (init_ccline)
862 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_EVAL
865 if (firstc == -1)
866 {
867 firstc = NUL;
868 break_ctrl_c = TRUE;
869 }
870#endif
871#ifdef FEAT_RIGHTLEFT
872 /* start without Hebrew mapping for a command line */
873 if (firstc == ':' || firstc == '=' || firstc == '>')
874 cmd_hkmap = 0;
875#endif
876
877 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200878
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200880 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881#endif
882
883 /*
884 * set some variables for redrawcmd()
885 */
886 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000887 ccline.cmdindent = (firstc > 0 ? indent : 0);
888
889 /* alloc initial ccline.cmdbuff */
890 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 if (ccline.cmdbuff == NULL)
Bram Moolenaar438d1762018-09-30 17:11:48 +0200892 goto theend; // out of memory
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 ccline.cmdlen = ccline.cmdpos = 0;
894 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100895 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000897 /* autoindent for :insert and :append */
898 if (firstc <= 0)
899 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200900 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000901 ccline.cmdbuff[indent] = NUL;
902 ccline.cmdpos = indent;
903 ccline.cmdspos = indent;
904 ccline.cmdlen = indent;
905 }
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000908 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909
910#ifdef FEAT_RIGHTLEFT
911 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
912 && (firstc == '/' || firstc == '?'))
913 cmdmsg_rl = TRUE;
914 else
915 cmdmsg_rl = FALSE;
916#endif
917
918 redir_off = TRUE; /* don't redirect the typed command */
919 if (!cmd_silent)
920 {
921 i = msg_scrolled;
922 msg_scrolled = 0; /* avoid wait_return message */
923 gotocmdline(TRUE);
924 msg_scrolled += i;
925 redrawcmdprompt(); /* draw prompt or indent */
926 set_cmdspos();
927 }
928 xpc.xp_context = EXPAND_NOTHING;
929 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000930#ifndef BACKSLASH_IN_FILENAME
931 xpc.xp_shell = FALSE;
932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000934#if defined(FEAT_EVAL)
935 if (ccline.input_fn)
936 {
937 xpc.xp_context = ccline.xp_context;
938 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000939# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000940 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000941# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000942 }
943#endif
944
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 /*
946 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
947 * doing ":@0" when register 0 doesn't contain a CR.
948 */
949 msg_scroll = FALSE;
950
951 State = CMDLINE;
952
953 if (firstc == '/' || firstc == '?' || firstc == '@')
954 {
955 /* Use ":lmap" mappings for search pattern and input(). */
956 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
957 b_im_ptr = &curbuf->b_p_iminsert;
958 else
959 b_im_ptr = &curbuf->b_p_imsearch;
960 if (*b_im_ptr == B_IMODE_LMAP)
961 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100962#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 im_set_active(*b_im_ptr == B_IMODE_IM);
964#endif
965 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100966#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 else if (p_imcmdline)
968 im_set_active(TRUE);
969#endif
970
971#ifdef FEAT_MOUSE
972 setmouse();
973#endif
974#ifdef CURSOR_SHAPE
975 ui_cursor_shape(); /* may show different cursor shape */
976#endif
977
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000978 /* When inside an autocommand for writing "exiting" may be set and
979 * terminal mode set to cooked. Need to set raw mode here then. */
980 settmode(TMODE_RAW);
981
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200982 /* Trigger CmdlineEnter autocommands. */
983 cmdline_type = firstc == NUL ? '-' : firstc;
984 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200985
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986#ifdef FEAT_CMDHIST
987 init_history();
988 hiscnt = hislen; /* set hiscnt to impossible history value */
989 histype = hist_char2type(firstc);
990#endif
991
992#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000993 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994#endif
995
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200996 /* If something above caused an error, reset the flags, we do want to type
997 * and execute commands. Display may be messed up a bit. */
998 if (did_emsg)
999 redrawcmd();
1000 did_emsg = FALSE;
1001 got_int = FALSE;
1002
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 /*
1004 * Collect the command string, handling editing keys.
1005 */
1006 for (;;)
1007 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +00001008 redir_off = TRUE; /* Don't redirect the typed command.
1009 Repeated, because a ":redir" inside
1010 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011#ifdef USE_ON_FLY_SCROLL
1012 dont_scroll = FALSE; /* allow scrolling here */
1013#endif
1014 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
1015
Bram Moolenaar72532d32018-04-07 19:09:09 +02001016 did_emsg = FALSE; /* There can't really be a reason why an error
1017 that occurs while typing a command should
1018 cause the command not to be executed. */
1019
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001021
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001022 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
1023 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001024 do
1025 {
1026 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +01001027 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +00001028
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 if (KeyTyped)
1030 {
1031 some_key_typed = TRUE;
1032#ifdef FEAT_RIGHTLEFT
1033 if (cmd_hkmap)
1034 c = hkmap(c);
1035# ifdef FEAT_FKMAP
1036 if (cmd_fkmap)
1037 c = cmdl_fkmap(c);
1038# endif
1039 if (cmdmsg_rl && !KeyStuffed)
1040 {
1041 /* Invert horizontal movements and operations. Only when
1042 * typed by the user directly, not when the result of a
1043 * mapping. */
1044 switch (c)
1045 {
1046 case K_RIGHT: c = K_LEFT; break;
1047 case K_S_RIGHT: c = K_S_LEFT; break;
1048 case K_C_RIGHT: c = K_C_LEFT; break;
1049 case K_LEFT: c = K_RIGHT; break;
1050 case K_S_LEFT: c = K_S_RIGHT; break;
1051 case K_C_LEFT: c = K_C_RIGHT; break;
1052 }
1053 }
1054#endif
1055 }
1056
1057 /*
1058 * Ignore got_int when CTRL-C was typed here.
1059 * Don't ignore it in :global, we really need to break then, e.g., for
1060 * ":g/pat/normal /pat" (without the <CR>).
1061 * Don't ignore it for the input() function.
1062 */
1063 if ((c == Ctrl_C
1064#ifdef UNIX
1065 || c == intr_char
1066#endif
1067 )
1068#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1069 && firstc != '@'
1070#endif
1071#ifdef FEAT_EVAL
1072 && !break_ctrl_c
1073#endif
1074 && !global_busy)
1075 got_int = FALSE;
1076
1077#ifdef FEAT_CMDHIST
1078 /* free old command line when finished moving around in the history
1079 * list */
1080 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001081 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001082 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 && c != K_PAGEDOWN && c != K_PAGEUP
1084 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001085 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001087 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089
1090 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001091 * When there are matching completions to select <S-Tab> works like
1092 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001094 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 c = Ctrl_P;
1096
1097#ifdef FEAT_WILDMENU
1098 /* Special translations for 'wildmenu' */
1099 if (did_wild_list && p_wmnu)
1100 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001101 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001103 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 c = Ctrl_N;
1105 }
1106 /* Hitting CR after "emenu Name.": complete submenu */
1107 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1108 && ccline.cmdpos > 1
1109 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1110 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1111 && (c == '\n' || c == '\r' || c == K_KENTER))
1112 c = K_DOWN;
1113#endif
1114
1115 /* free expanded names when finished walking through matches */
1116 if (xpc.xp_numfiles != -1
1117 && !(c == p_wc && KeyTyped) && c != p_wcm
1118 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1119 && c != Ctrl_L)
1120 {
1121 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1122 did_wild_list = FALSE;
1123#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001124 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125#endif
1126 xpc.xp_context = EXPAND_NOTHING;
1127 wim_index = 0;
1128#ifdef FEAT_WILDMENU
1129 if (p_wmnu && wild_menu_showing != 0)
1130 {
1131 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001132 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001133
1134 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001135 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
1137 if (wild_menu_showing == WM_SCROLLED)
1138 {
1139 /* Entered command line, move it up */
1140 cmdline_row--;
1141 redrawcmd();
1142 }
1143 else if (save_p_ls != -1)
1144 {
1145 /* restore 'laststatus' and 'winminheight' */
1146 p_ls = save_p_ls;
1147 p_wmh = save_p_wmh;
1148 last_status(FALSE);
1149 update_screen(VALID); /* redraw the screen NOW */
1150 redrawcmd();
1151 save_p_ls = -1;
1152 }
1153 else
1154 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 redraw_statuslines();
1157 }
1158 KeyTyped = skt;
1159 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001160 if (ccline.input_fn)
1161 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163#endif
1164 }
1165
1166#ifdef FEAT_WILDMENU
1167 /* Special translations for 'wildmenu' */
1168 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1169 {
1170 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001171 if (c == K_DOWN && ccline.cmdpos > 0
1172 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001174 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 {
1176 /* Hitting <Up>: Remove one submenu name in front of the
1177 * cursor */
1178 int found = FALSE;
1179
1180 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1181 i = 0;
1182 while (--j > 0)
1183 {
1184 /* check for start of menu name */
1185 if (ccline.cmdbuff[j] == ' '
1186 && ccline.cmdbuff[j - 1] != '\\')
1187 {
1188 i = j + 1;
1189 break;
1190 }
1191 /* check for start of submenu name */
1192 if (ccline.cmdbuff[j] == '.'
1193 && ccline.cmdbuff[j - 1] != '\\')
1194 {
1195 if (found)
1196 {
1197 i = j + 1;
1198 break;
1199 }
1200 else
1201 found = TRUE;
1202 }
1203 }
1204 if (i > 0)
1205 cmdline_del(i);
1206 c = p_wc;
1207 xpc.xp_context = EXPAND_NOTHING;
1208 }
1209 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001210 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001211 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001212 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 {
1214 char_u upseg[5];
1215
1216 upseg[0] = PATHSEP;
1217 upseg[1] = '.';
1218 upseg[2] = '.';
1219 upseg[3] = PATHSEP;
1220 upseg[4] = NUL;
1221
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001222 if (c == K_DOWN
1223 && ccline.cmdpos > 0
1224 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1225 && (ccline.cmdpos < 3
1226 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1228 {
1229 /* go down a directory */
1230 c = p_wc;
1231 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001232 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 {
1234 /* If in a direct ancestor, strip off one ../ to go down */
1235 int found = FALSE;
1236
1237 j = ccline.cmdpos;
1238 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1239 while (--j > i)
1240 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001241#ifdef FEAT_MBYTE
1242 if (has_mbyte)
1243 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 if (vim_ispathsep(ccline.cmdbuff[j]))
1246 {
1247 found = TRUE;
1248 break;
1249 }
1250 }
1251 if (found
1252 && ccline.cmdbuff[j - 1] == '.'
1253 && ccline.cmdbuff[j - 2] == '.'
1254 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1255 {
1256 cmdline_del(j - 2);
1257 c = p_wc;
1258 }
1259 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001260 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 {
1262 /* go up a directory */
1263 int found = FALSE;
1264
1265 j = ccline.cmdpos - 1;
1266 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1267 while (--j > i)
1268 {
1269#ifdef FEAT_MBYTE
1270 if (has_mbyte)
1271 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1272#endif
1273 if (vim_ispathsep(ccline.cmdbuff[j])
1274#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001275 && vim_strchr((char_u *)" *?[{`$%#",
1276 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277#endif
1278 )
1279 {
1280 if (found)
1281 {
1282 i = j + 1;
1283 break;
1284 }
1285 else
1286 found = TRUE;
1287 }
1288 }
1289
1290 if (!found)
1291 j = i;
1292 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1293 j += 4;
1294 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1295 && j == i)
1296 j += 3;
1297 else
1298 j = 0;
1299 if (j > 0)
1300 {
1301 /* TODO this is only for DOS/UNIX systems - need to put in
1302 * machine-specific stuff here and in upseg init */
1303 cmdline_del(j);
1304 put_on_cmdline(upseg + 1, 3, FALSE);
1305 }
1306 else if (ccline.cmdpos > i)
1307 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001308
1309 /* Now complete in the new directory. Set KeyTyped in case the
1310 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001312 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 }
1314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316#endif /* FEAT_WILDMENU */
1317
1318 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1319 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1320 if (c == Ctrl_BSL)
1321 {
1322 ++no_mapping;
1323 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001324 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 --no_mapping;
1326 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001327 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1328 * is in a mapping. */
1329 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001330 || (ccline.cmdfirstc == '=' && KeyTyped)
1331#ifdef FEAT_EVAL
Bram Moolenaaree91c332018-09-25 22:27:35 +02001332 || cmdline_star > 0
Bram Moolenaar31cbadf2018-09-25 20:48:57 +02001333#endif
1334 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 {
1336 vungetc(c);
1337 c = Ctrl_BSL;
1338 }
1339#ifdef FEAT_EVAL
1340 else if (c == 'e')
1341 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001342 char_u *p = NULL;
1343 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345 /*
1346 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001347 * Need to save and restore the current command line, to be
1348 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 */
1350 if (ccline.cmdpos == ccline.cmdlen)
1351 new_cmdpos = 99999; /* keep it at the end */
1352 else
1353 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 if (c == '=')
1357 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001358 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001359 * to avoid nasty things like going to another buffer when
1360 * evaluating an expression. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001361 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001363 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001364
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001365 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001367 len = (int)STRLEN(p);
1368 if (realloc_cmdbuff(len + 1) == OK)
1369 {
1370 ccline.cmdlen = len;
1371 STRCPY(ccline.cmdbuff, p);
1372 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001374 /* Restore the cursor or use the position set with
1375 * set_cmdline_pos(). */
1376 if (new_cmdpos > ccline.cmdlen)
1377 ccline.cmdpos = ccline.cmdlen;
1378 else
1379 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001381 KeyTyped = FALSE; /* Don't do p_wc completion. */
1382 redrawcmd();
1383 goto cmdline_changed;
1384 }
Bram Moolenaar4e303c82018-11-24 14:27:44 +01001385 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387 }
1388 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001389 got_int = FALSE; /* don't abandon the command line */
1390 did_emsg = FALSE;
1391 emsg_on_display = FALSE;
1392 redrawcmd();
1393 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395#endif
1396 else
1397 {
1398 if (c == Ctrl_G && p_im && restart_edit == 0)
1399 restart_edit = 'a';
1400 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1401 in history */
1402 goto returncmd; /* back to Normal mode */
1403 }
1404 }
1405
1406#ifdef FEAT_CMDWIN
1407 if (c == cedit_key || c == K_CMDWIN)
1408 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001409 if (ex_normal_busy == 0 && got_int == FALSE)
1410 {
1411 /*
1412 * Open a window to edit the command line (and history).
1413 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001414 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001415 some_key_typed = TRUE;
1416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418# ifdef FEAT_DIGRAPHS
1419 else
1420# endif
1421#endif
1422#ifdef FEAT_DIGRAPHS
1423 c = do_digraph(c);
1424#endif
1425
1426 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1427 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1428 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001429 /* In Ex mode a backslash escapes a newline. */
1430 if (exmode_active
1431 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001432 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001433 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001434 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001436 if (c == K_KENTER)
1437 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001439 else
1440 {
1441 gotesc = FALSE; /* Might have typed ESC previously, don't
1442 truncate the cmdline now. */
1443 if (ccheck_abbr(c + ABBR_OFF))
1444 goto cmdline_changed;
1445 if (!cmd_silent)
1446 {
1447 windgoto(msg_row, 0);
1448 out_flush();
1449 }
1450 break;
1451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 }
1453
1454 /*
1455 * Completion for 'wildchar' or 'wildcharm' key.
1456 * - hitting <ESC> twice means: abandon command line.
1457 * - wildcard expansion is only done when the 'wildchar' key is really
1458 * typed, not when it comes from a macro
1459 */
1460 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1461 {
1462 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1463 {
1464 /* if 'wildmode' contains "list" may still need to list */
1465 if (xpc.xp_numfiles > 1
1466 && !did_wild_list
1467 && (wim_flags[wim_index] & WIM_LIST))
1468 {
1469 (void)showmatches(&xpc, FALSE);
1470 redrawcmd();
1471 did_wild_list = TRUE;
1472 }
1473 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001474 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1475 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001477 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1478 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 else
1480 res = OK; /* don't insert 'wildchar' now */
1481 }
1482 else /* typed p_wc first time */
1483 {
1484 wim_index = 0;
1485 j = ccline.cmdpos;
1486 /* if 'wildmode' first contains "longest", get longest
1487 * common part */
1488 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001489 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1490 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001492 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1493 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 /* if interrupted while completing, behave like it failed */
1496 if (got_int)
1497 {
1498 (void)vpeekc(); /* remove <C-C> from input stream */
1499 got_int = FALSE; /* don't abandon the command line */
1500 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1501#ifdef FEAT_WILDMENU
1502 xpc.xp_context = EXPAND_NOTHING;
1503#endif
1504 goto cmdline_changed;
1505 }
1506
1507 /* when more than one match, and 'wildmode' first contains
1508 * "list", or no change and 'wildmode' contains "longest,list",
1509 * list all matches */
1510 if (res == OK && xpc.xp_numfiles > 1)
1511 {
1512 /* a "longest" that didn't do anything is skipped (but not
1513 * "list:longest") */
1514 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1515 wim_index = 1;
1516 if ((wim_flags[wim_index] & WIM_LIST)
1517#ifdef FEAT_WILDMENU
1518 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1519#endif
1520 )
1521 {
1522 if (!(wim_flags[0] & WIM_LONGEST))
1523 {
1524#ifdef FEAT_WILDMENU
1525 int p_wmnu_save = p_wmnu;
1526 p_wmnu = 0;
1527#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001528 /* remove match */
1529 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#ifdef FEAT_WILDMENU
1531 p_wmnu = p_wmnu_save;
1532#endif
1533 }
1534#ifdef FEAT_WILDMENU
1535 (void)showmatches(&xpc, p_wmnu
1536 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1537#else
1538 (void)showmatches(&xpc, FALSE);
1539#endif
1540 redrawcmd();
1541 did_wild_list = TRUE;
1542 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001543 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1544 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001546 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1547 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 }
1549 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001550 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 }
1552#ifdef FEAT_WILDMENU
1553 else if (xpc.xp_numfiles == -1)
1554 xpc.xp_context = EXPAND_NOTHING;
1555#endif
1556 }
1557 if (wim_index < 3)
1558 ++wim_index;
1559 if (c == ESC)
1560 gotesc = TRUE;
1561 if (res == OK)
1562 goto cmdline_changed;
1563 }
1564
1565 gotesc = FALSE;
1566
1567 /* <S-Tab> goes to last match, in a clumsy way */
1568 if (c == K_S_TAB && KeyTyped)
1569 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001570 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1571 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1572 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 goto cmdline_changed;
1574 }
1575
1576 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1577 c = NL;
1578
1579 do_abbr = TRUE; /* default: check for abbreviation */
1580
1581 /*
1582 * Big switch for a typed command line character.
1583 */
1584 switch (c)
1585 {
1586 case K_BS:
1587 case Ctrl_H:
1588 case K_DEL:
1589 case K_KDEL:
1590 case Ctrl_W:
1591#ifdef FEAT_FKMAP
1592 if (cmd_fkmap && c == K_BS)
1593 c = K_DEL;
1594#endif
1595 if (c == K_KDEL)
1596 c = K_DEL;
1597
1598 /*
1599 * delete current character is the same as backspace on next
1600 * character, except at end of line
1601 */
1602 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1603 ++ccline.cmdpos;
1604#ifdef FEAT_MBYTE
1605 if (has_mbyte && c == K_DEL)
1606 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1607 ccline.cmdbuff + ccline.cmdpos);
1608#endif
1609 if (ccline.cmdpos > 0)
1610 {
1611 char_u *p;
1612
1613 j = ccline.cmdpos;
1614 p = ccline.cmdbuff + j;
1615#ifdef FEAT_MBYTE
1616 if (has_mbyte)
1617 {
1618 p = mb_prevptr(ccline.cmdbuff, p);
1619 if (c == Ctrl_W)
1620 {
1621 while (p > ccline.cmdbuff && vim_isspace(*p))
1622 p = mb_prevptr(ccline.cmdbuff, p);
1623 i = mb_get_class(p);
1624 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1625 p = mb_prevptr(ccline.cmdbuff, p);
1626 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001627 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 }
1629 }
1630 else
1631#endif
1632 if (c == Ctrl_W)
1633 {
1634 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1635 --p;
1636 i = vim_iswordc(p[-1]);
1637 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1638 && vim_iswordc(p[-1]) == i)
1639 --p;
1640 }
1641 else
1642 --p;
1643 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1644 ccline.cmdlen -= j - ccline.cmdpos;
1645 i = ccline.cmdpos;
1646 while (i < ccline.cmdlen)
1647 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1648
1649 /* Truncate at the end, required for multi-byte chars. */
1650 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001651#ifdef FEAT_SEARCH_EXTRA
1652 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001653 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001654 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001655 /* save view settings, so that the screen
1656 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001657 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001658 }
1659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660 redrawcmd();
1661 }
1662 else if (ccline.cmdlen == 0 && c != Ctrl_W
1663 && ccline.cmdprompt == NULL && indent == 0)
1664 {
1665 /* In ex and debug mode it doesn't make sense to return. */
1666 if (exmode_active
1667#ifdef FEAT_EVAL
1668 || ccline.cmdfirstc == '>'
1669#endif
1670 )
1671 goto cmdline_not_changed;
1672
Bram Moolenaard23a8232018-02-10 18:45:26 +01001673 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 if (!cmd_silent)
1675 {
1676#ifdef FEAT_RIGHTLEFT
1677 if (cmdmsg_rl)
1678 msg_col = Columns;
1679 else
1680#endif
1681 msg_col = 0;
1682 msg_putchar(' '); /* delete ':' */
1683 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001684#ifdef FEAT_SEARCH_EXTRA
1685 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001686 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001687#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 redraw_cmdline = TRUE;
1689 goto returncmd; /* back to cmd mode */
1690 }
1691 goto cmdline_changed;
1692
1693 case K_INS:
1694 case K_KINS:
1695#ifdef FEAT_FKMAP
1696 /* if Farsi mode set, we are in reverse insert mode -
1697 Do not change the mode */
1698 if (cmd_fkmap)
1699 beep_flush();
1700 else
1701#endif
1702 ccline.overstrike = !ccline.overstrike;
1703#ifdef CURSOR_SHAPE
1704 ui_cursor_shape(); /* may show different cursor shape */
1705#endif
1706 goto cmdline_not_changed;
1707
1708 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001709 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 {
1711 /* ":lmap" mappings exists, toggle use of mappings. */
1712 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001713#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 im_set_active(FALSE); /* Disable input method */
1715#endif
1716 if (b_im_ptr != NULL)
1717 {
1718 if (State & LANGMAP)
1719 *b_im_ptr = B_IMODE_LMAP;
1720 else
1721 *b_im_ptr = B_IMODE_NONE;
1722 }
1723 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001724#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 else
1726 {
1727 /* There are no ":lmap" mappings, toggle IM. When
1728 * 'imdisable' is set don't try getting the status, it's
1729 * always off. */
1730 if ((p_imdisable && b_im_ptr != NULL)
1731 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1732 {
1733 im_set_active(FALSE); /* Disable input method */
1734 if (b_im_ptr != NULL)
1735 *b_im_ptr = B_IMODE_NONE;
1736 }
1737 else
1738 {
1739 im_set_active(TRUE); /* Enable input method */
1740 if (b_im_ptr != NULL)
1741 *b_im_ptr = B_IMODE_IM;
1742 }
1743 }
1744#endif
1745 if (b_im_ptr != NULL)
1746 {
1747 if (b_im_ptr == &curbuf->b_p_iminsert)
1748 set_iminsert_global();
1749 else
1750 set_imsearch_global();
1751 }
1752#ifdef CURSOR_SHAPE
1753 ui_cursor_shape(); /* may show different cursor shape */
1754#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001755#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 /* Show/unshow value of 'keymap' in status lines later. */
1757 status_redraw_curbuf();
1758#endif
1759 goto cmdline_not_changed;
1760
1761/* case '@': only in very old vi */
1762 case Ctrl_U:
1763 /* delete all characters left of the cursor */
1764 j = ccline.cmdpos;
1765 ccline.cmdlen -= j;
1766 i = ccline.cmdpos = 0;
1767 while (i < ccline.cmdlen)
1768 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1769 /* Truncate at the end, required for multi-byte chars. */
1770 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001771#ifdef FEAT_SEARCH_EXTRA
1772 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001773 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775 redrawcmd();
1776 goto cmdline_changed;
1777
1778#ifdef FEAT_CLIPBOARD
1779 case Ctrl_Y:
1780 /* Copy the modeless selection, if there is one. */
1781 if (clip_star.state != SELECT_CLEARED)
1782 {
1783 if (clip_star.state == SELECT_DONE)
1784 clip_copy_modeless_selection(TRUE);
1785 goto cmdline_not_changed;
1786 }
1787 break;
1788#endif
1789
1790 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1791 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001792 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001793 * ":normal" runs out of characters. */
1794 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001795 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 goto cmdline_not_changed;
1797
1798 gotesc = TRUE; /* will free ccline.cmdbuff after
1799 putting it in history */
1800 goto returncmd; /* back to cmd mode */
1801
1802 case Ctrl_R: /* insert register */
1803#ifdef USE_ON_FLY_SCROLL
1804 dont_scroll = TRUE; /* disallow scrolling here */
1805#endif
1806 putcmdline('"', TRUE);
1807 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001808 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (i == Ctrl_O)
1810 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1811 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001812 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001813 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 --no_mapping;
1815#ifdef FEAT_EVAL
1816 /*
1817 * Insert the result of an expression.
1818 * Need to save the current command line, to be able to enter
1819 * a new one...
1820 */
1821 new_cmdpos = -1;
1822 if (c == '=')
1823 {
Bram Moolenaaree91c332018-09-25 22:27:35 +02001824 if (ccline.cmdfirstc == '=' // can't do this recursively
1825 || cmdline_star > 0) // or when typing a password
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 {
1827 beep_flush();
1828 c = ESC;
1829 }
1830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 c = get_expr_register();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 }
1833#endif
1834 if (c != ESC) /* use ESC to cancel inserting register */
1835 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001836 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001837
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001838#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001839 /* When there was a serious error abort getting the
1840 * command line. */
1841 if (aborting())
1842 {
1843 gotesc = TRUE; /* will free ccline.cmdbuff after
1844 putting it in history */
1845 goto returncmd; /* back to cmd mode */
1846 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 KeyTyped = FALSE; /* Don't do p_wc completion. */
1849#ifdef FEAT_EVAL
1850 if (new_cmdpos >= 0)
1851 {
1852 /* set_cmdline_pos() was used */
1853 if (new_cmdpos > ccline.cmdlen)
1854 ccline.cmdpos = ccline.cmdlen;
1855 else
1856 ccline.cmdpos = new_cmdpos;
1857 }
1858#endif
1859 }
1860 redrawcmd();
1861 goto cmdline_changed;
1862
1863 case Ctrl_D:
1864 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1865 break; /* Use ^D as normal char instead */
1866
1867 redrawcmd();
1868 continue; /* don't do incremental search now */
1869
1870 case K_RIGHT:
1871 case K_S_RIGHT:
1872 case K_C_RIGHT:
1873 do
1874 {
1875 if (ccline.cmdpos >= ccline.cmdlen)
1876 break;
1877 i = cmdline_charsize(ccline.cmdpos);
1878 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1879 break;
1880 ccline.cmdspos += i;
1881#ifdef FEAT_MBYTE
1882 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001883 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 + ccline.cmdpos);
1885 else
1886#endif
1887 ++ccline.cmdpos;
1888 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001889 while ((c == K_S_RIGHT || c == K_C_RIGHT
1890 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1892#ifdef FEAT_MBYTE
1893 if (has_mbyte)
1894 set_cmdspos_cursor();
1895#endif
1896 goto cmdline_not_changed;
1897
1898 case K_LEFT:
1899 case K_S_LEFT:
1900 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001901 if (ccline.cmdpos == 0)
1902 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 do
1904 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 --ccline.cmdpos;
1906#ifdef FEAT_MBYTE
1907 if (has_mbyte) /* move to first byte of char */
1908 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1909 ccline.cmdbuff + ccline.cmdpos);
1910#endif
1911 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1912 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001913 while (ccline.cmdpos > 0
1914 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001915 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1917#ifdef FEAT_MBYTE
1918 if (has_mbyte)
1919 set_cmdspos_cursor();
1920#endif
1921 goto cmdline_not_changed;
1922
1923 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001924 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001925 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
Bram Moolenaar4770d092006-01-12 23:22:24 +00001927#ifdef FEAT_GUI_W32
1928 /* On Win32 ignore <M-F4>, we get it when closing the window was
1929 * cancelled. */
1930 case K_F4:
1931 if (mod_mask == MOD_MASK_ALT)
1932 {
1933 redrawcmd(); /* somehow the cmdline is cleared */
1934 goto cmdline_not_changed;
1935 }
1936 break;
1937#endif
1938
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939#ifdef FEAT_MOUSE
1940 case K_MIDDLEDRAG:
1941 case K_MIDDLERELEASE:
1942 goto cmdline_not_changed; /* Ignore mouse */
1943
1944 case K_MIDDLEMOUSE:
1945# ifdef FEAT_GUI
1946 /* When GUI is active, also paste when 'mouse' is empty */
1947 if (!gui.in_use)
1948# endif
1949 if (!mouse_has(MOUSE_COMMAND))
1950 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001951# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001953 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001955# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001956 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 redrawcmd();
1958 goto cmdline_changed;
1959
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001960# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001962 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 redrawcmd();
1964 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966
1967 case K_LEFTDRAG:
1968 case K_LEFTRELEASE:
1969 case K_RIGHTDRAG:
1970 case K_RIGHTRELEASE:
1971 /* Ignore drag and release events when the button-down wasn't
1972 * seen before. */
1973 if (ignore_drag_release)
1974 goto cmdline_not_changed;
1975 /* FALLTHROUGH */
1976 case K_LEFTMOUSE:
1977 case K_RIGHTMOUSE:
1978 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1979 ignore_drag_release = TRUE;
1980 else
1981 ignore_drag_release = FALSE;
1982# ifdef FEAT_GUI
1983 /* When GUI is active, also move when 'mouse' is empty */
1984 if (!gui.in_use)
1985# endif
1986 if (!mouse_has(MOUSE_COMMAND))
1987 goto cmdline_not_changed; /* Ignore mouse */
1988# ifdef FEAT_CLIPBOARD
1989 if (mouse_row < cmdline_row && clip_star.available)
1990 {
1991 int button, is_click, is_drag;
1992
1993 /*
1994 * Handle modeless selection.
1995 */
1996 button = get_mouse_button(KEY2TERMCAP1(c),
1997 &is_click, &is_drag);
1998 if (mouse_model_popup() && button == MOUSE_LEFT
1999 && (mod_mask & MOD_MASK_SHIFT))
2000 {
2001 /* Translate shift-left to right button. */
2002 button = MOUSE_RIGHT;
2003 mod_mask &= ~MOD_MASK_SHIFT;
2004 }
2005 clip_modeless(button, is_click, is_drag);
2006 goto cmdline_not_changed;
2007 }
2008# endif
2009
2010 set_cmdspos();
2011 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
2012 ++ccline.cmdpos)
2013 {
2014 i = cmdline_charsize(ccline.cmdpos);
2015 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
2016 && mouse_col < ccline.cmdspos % Columns + i)
2017 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002018# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 if (has_mbyte)
2020 {
2021 /* Count ">" for double-wide char that doesn't fit. */
2022 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002023 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 + ccline.cmdpos) - 1;
2025 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002026# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 ccline.cmdspos += i;
2028 }
2029 goto cmdline_not_changed;
2030
2031 /* Mouse scroll wheel: ignored here */
2032 case K_MOUSEDOWN:
2033 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002034 case K_MOUSELEFT:
2035 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 /* Alternate buttons ignored here */
2037 case K_X1MOUSE:
2038 case K_X1DRAG:
2039 case K_X1RELEASE:
2040 case K_X2MOUSE:
2041 case K_X2DRAG:
2042 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01002043 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 goto cmdline_not_changed;
2045
2046#endif /* FEAT_MOUSE */
2047
2048#ifdef FEAT_GUI
2049 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
2050 case K_LEFTRELEASE_NM:
2051 goto cmdline_not_changed;
2052
2053 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002054 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
2056 gui_do_scroll();
2057 redrawcmd();
2058 }
2059 goto cmdline_not_changed;
2060
2061 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002062 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002064 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 redrawcmd();
2066 }
2067 goto cmdline_not_changed;
2068#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002069#ifdef FEAT_GUI_TABLINE
2070 case K_TABLINE:
2071 case K_TABMENU:
2072 /* Don't want to change any tabs here. Make sure the same tab
2073 * is still selected. */
2074 if (gui_use_tabline())
2075 gui_mch_set_curtab(tabpage_index(curtab));
2076 goto cmdline_not_changed;
2077#endif
2078
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 case K_SELECT: /* end of Select mode mapping - ignore */
2080 goto cmdline_not_changed;
2081
2082 case Ctrl_B: /* begin of command line */
2083 case K_HOME:
2084 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 case K_S_HOME:
2086 case K_C_HOME:
2087 ccline.cmdpos = 0;
2088 set_cmdspos();
2089 goto cmdline_not_changed;
2090
2091 case Ctrl_E: /* end of command line */
2092 case K_END:
2093 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 case K_S_END:
2095 case K_C_END:
2096 ccline.cmdpos = ccline.cmdlen;
2097 set_cmdspos_cursor();
2098 goto cmdline_not_changed;
2099
2100 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002101 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 break;
2103 goto cmdline_changed;
2104
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002105 case Ctrl_L:
2106#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002107 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002108 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002109#endif
2110
2111 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002112 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 break;
2114 goto cmdline_changed;
2115
2116 case Ctrl_N: /* next match */
2117 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002118 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002120 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2121 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002123 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002126 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 case K_UP:
2128 case K_DOWN:
2129 case K_S_UP:
2130 case K_S_DOWN:
2131 case K_PAGEUP:
2132 case K_KPAGEUP:
2133 case K_PAGEDOWN:
2134 case K_KPAGEDOWN:
2135 if (hislen == 0 || firstc == NUL) /* no history */
2136 goto cmdline_not_changed;
2137
2138 i = hiscnt;
2139
2140 /* save current command string so it can be restored later */
2141 if (lookfor == NULL)
2142 {
2143 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2144 goto cmdline_not_changed;
2145 lookfor[ccline.cmdpos] = NUL;
2146 }
2147
2148 j = (int)STRLEN(lookfor);
2149 for (;;)
2150 {
2151 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002152 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002153 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 {
2155 if (hiscnt == hislen) /* first time */
2156 hiscnt = hisidx[histype];
2157 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2158 hiscnt = hislen - 1;
2159 else if (hiscnt != hisidx[histype] + 1)
2160 --hiscnt;
2161 else /* at top of list */
2162 {
2163 hiscnt = i;
2164 break;
2165 }
2166 }
2167 else /* one step forwards */
2168 {
2169 /* on last entry, clear the line */
2170 if (hiscnt == hisidx[histype])
2171 {
2172 hiscnt = hislen;
2173 break;
2174 }
2175
2176 /* not on a history line, nothing to do */
2177 if (hiscnt == hislen)
2178 break;
2179 if (hiscnt == hislen - 1) /* wrap around */
2180 hiscnt = 0;
2181 else
2182 ++hiscnt;
2183 }
2184 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2185 {
2186 hiscnt = i;
2187 break;
2188 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002189 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002190 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 || STRNCMP(history[histype][hiscnt].hisstr,
2192 lookfor, (size_t)j) == 0)
2193 break;
2194 }
2195
2196 if (hiscnt != i) /* jumped to other entry */
2197 {
2198 char_u *p;
2199 int len;
2200 int old_firstc;
2201
Bram Moolenaar438d1762018-09-30 17:11:48 +02002202 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002203 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 if (hiscnt == hislen)
2205 p = lookfor; /* back to the old one */
2206 else
2207 p = history[histype][hiscnt].hisstr;
2208
2209 if (histype == HIST_SEARCH
2210 && p != lookfor
2211 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2212 {
2213 /* Correct for the separator character used when
2214 * adding the history entry vs the one used now.
2215 * First loop: count length.
2216 * Second loop: copy the characters. */
2217 for (i = 0; i <= 1; ++i)
2218 {
2219 len = 0;
2220 for (j = 0; p[j] != NUL; ++j)
2221 {
2222 /* Replace old sep with new sep, unless it is
2223 * escaped. */
2224 if (p[j] == old_firstc
2225 && (j == 0 || p[j - 1] != '\\'))
2226 {
2227 if (i > 0)
2228 ccline.cmdbuff[len] = firstc;
2229 }
2230 else
2231 {
2232 /* Escape new sep, unless it is already
2233 * escaped. */
2234 if (p[j] == firstc
2235 && (j == 0 || p[j - 1] != '\\'))
2236 {
2237 if (i > 0)
2238 ccline.cmdbuff[len] = '\\';
2239 ++len;
2240 }
2241 if (i > 0)
2242 ccline.cmdbuff[len] = p[j];
2243 }
2244 ++len;
2245 }
2246 if (i == 0)
2247 {
2248 alloc_cmdbuff(len);
2249 if (ccline.cmdbuff == NULL)
2250 goto returncmd;
2251 }
2252 }
2253 ccline.cmdbuff[len] = NUL;
2254 }
2255 else
2256 {
2257 alloc_cmdbuff((int)STRLEN(p));
2258 if (ccline.cmdbuff == NULL)
2259 goto returncmd;
2260 STRCPY(ccline.cmdbuff, p);
2261 }
2262
2263 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2264 redrawcmd();
2265 goto cmdline_changed;
2266 }
2267 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002268#endif
2269 goto cmdline_not_changed;
2270
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002271#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002272 case Ctrl_G: /* next match */
2273 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002274 if (may_adjust_incsearch_highlighting(
2275 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002276 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002277 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278#endif
2279
2280 case Ctrl_V:
2281 case Ctrl_Q:
2282#ifdef FEAT_MOUSE
2283 ignore_drag_release = TRUE;
2284#endif
2285 putcmdline('^', TRUE);
2286 c = get_literal(); /* get next (two) character(s) */
2287 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002288 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289#ifdef FEAT_MBYTE
2290 /* may need to remove ^ when composing char was typed */
2291 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2292 {
2293 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2294 msg_putchar(' ');
2295 cursorcmd();
2296 }
2297#endif
2298 break;
2299
2300#ifdef FEAT_DIGRAPHS
2301 case Ctrl_K:
2302#ifdef FEAT_MOUSE
2303 ignore_drag_release = TRUE;
2304#endif
2305 putcmdline('?', TRUE);
2306#ifdef USE_ON_FLY_SCROLL
2307 dont_scroll = TRUE; /* disallow scrolling here */
2308#endif
2309 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002310 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 if (c != NUL)
2312 break;
2313
2314 redrawcmd();
2315 goto cmdline_not_changed;
2316#endif /* FEAT_DIGRAPHS */
2317
2318#ifdef FEAT_RIGHTLEFT
2319 case Ctrl__: /* CTRL-_: switch language mode */
2320 if (!p_ari)
2321 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002322# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (p_altkeymap)
2324 {
2325 cmd_fkmap = !cmd_fkmap;
2326 if (cmd_fkmap) /* in Farsi always in Insert mode */
2327 ccline.overstrike = FALSE;
2328 }
2329 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 cmd_hkmap = !cmd_hkmap;
2332 goto cmdline_not_changed;
2333#endif
2334
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002335 case K_PS:
2336 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2337 goto cmdline_changed;
2338
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 default:
2340#ifdef UNIX
2341 if (c == intr_char)
2342 {
2343 gotesc = TRUE; /* will free ccline.cmdbuff after
2344 putting it in history */
2345 goto returncmd; /* back to Normal mode */
2346 }
2347#endif
2348 /*
2349 * Normal character with no special meaning. Just set mod_mask
2350 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2351 * the string <S-Space>. This should only happen after ^V.
2352 */
2353 if (!IS_SPECIAL(c))
2354 mod_mask = 0x0;
2355 break;
2356 }
2357 /*
2358 * End of switch on command line character.
2359 * We come here if we have a normal character.
2360 */
2361
Bram Moolenaarede3e632013-06-23 16:16:19 +02002362 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363#ifdef FEAT_MBYTE
2364 /* Add ABBR_OFF for characters above 0x100, this is
2365 * what check_abbr() expects. */
2366 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2367#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002368 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 goto cmdline_changed;
2370
2371 /*
2372 * put the character in the command line
2373 */
2374 if (IS_SPECIAL(c) || mod_mask != 0)
2375 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2376 else
2377 {
2378#ifdef FEAT_MBYTE
2379 if (has_mbyte)
2380 {
2381 j = (*mb_char2bytes)(c, IObuff);
2382 IObuff[j] = NUL; /* exclude composing chars */
2383 put_on_cmdline(IObuff, j, TRUE);
2384 }
2385 else
2386#endif
2387 {
2388 IObuff[0] = c;
2389 put_on_cmdline(IObuff, 1, TRUE);
2390 }
2391 }
2392 goto cmdline_changed;
2393
2394/*
2395 * This part implements incremental searches for "/" and "?"
2396 * Jump to cmdline_not_changed when a character has been read but the command
2397 * line did not change. Then we only search and redraw if something changed in
2398 * the past.
2399 * Jump to cmdline_changed when the command line did change.
2400 * (Sorry for the goto's, I know it is ugly).
2401 */
2402cmdline_not_changed:
2403#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002404 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 continue;
2406#endif
2407
2408cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002409 /* Trigger CmdlineChanged autocommands. */
2410 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002411
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002413 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#endif
2415
2416#ifdef FEAT_RIGHTLEFT
2417 if (cmdmsg_rl
2418# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002419 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420# endif
2421 )
2422 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002423 * right-left typing. Not efficient, but it works.
2424 * Do it only when there are no characters left to read
2425 * to avoid useless intermediate redraws. */
2426 if (vpeekc() == NUL)
2427 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428#endif
2429 }
2430
2431returncmd:
2432
2433#ifdef FEAT_RIGHTLEFT
2434 cmdmsg_rl = FALSE;
2435#endif
2436
2437#ifdef FEAT_FKMAP
2438 cmd_fkmap = 0;
2439#endif
2440
2441 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002442 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002445 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446#endif
2447
2448 if (ccline.cmdbuff != NULL)
2449 {
2450 /*
2451 * Put line in history buffer (":" and "=" only when it was typed).
2452 */
2453#ifdef FEAT_CMDHIST
2454 if (ccline.cmdlen && firstc != NUL
2455 && (some_key_typed || histype == HIST_SEARCH))
2456 {
2457 add_to_history(histype, ccline.cmdbuff, TRUE,
2458 histype == HIST_SEARCH ? firstc : NUL);
2459 if (firstc == ':')
2460 {
2461 vim_free(new_last_cmdline);
2462 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2463 }
2464 }
2465#endif
2466
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002467 if (gotesc)
2468 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
2470
2471 /*
2472 * If the screen was shifted up, redraw the whole screen (later).
2473 * If the line is too long, clear it, so ruler and shown command do
2474 * not get printed in the middle of it.
2475 */
2476 msg_check();
2477 msg_scroll = save_msg_scroll;
2478 redir_off = FALSE;
2479
2480 /* When the command line was typed, no need for a wait-return prompt. */
2481 if (some_key_typed)
2482 need_wait_return = FALSE;
2483
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002484 /* Trigger CmdlineLeave autocommands. */
2485 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002486
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002488#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2490 im_save_status(b_im_ptr);
2491 im_set_active(FALSE);
2492#endif
2493#ifdef FEAT_MOUSE
2494 setmouse();
2495#endif
2496#ifdef CURSOR_SHAPE
2497 ui_cursor_shape(); /* may show different cursor shape */
2498#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002499 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
Bram Moolenaar438d1762018-09-30 17:11:48 +02002501theend:
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002502 {
2503 char_u *p = ccline.cmdbuff;
2504
Bram Moolenaar438d1762018-09-30 17:11:48 +02002505 if (did_save_ccline)
2506 restore_cmdline(&save_ccline);
2507 else
2508 ccline.cmdbuff = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002509 return p;
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511}
2512
2513#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2514/*
2515 * Get a command line with a prompt.
2516 * This is prepared to be called recursively from getcmdline() (e.g. by
2517 * f_input() when evaluating an expression from CTRL-R =).
2518 * Returns the command line in allocated memory, or NULL.
2519 */
2520 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002521getcmdline_prompt(
2522 int firstc,
2523 char_u *prompt, /* command line prompt */
2524 int attr, /* attributes for prompt */
2525 int xp_context, /* type of expansion */
2526 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527{
2528 char_u *s;
2529 struct cmdline_info save_ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002530 int did_save_ccline = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002532 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533
Bram Moolenaar438d1762018-09-30 17:11:48 +02002534 if (ccline.cmdbuff != NULL)
2535 {
2536 // Save the values of the current cmdline and restore them below.
2537 save_cmdline(&save_ccline);
2538 did_save_ccline = TRUE;
2539 }
2540
2541 vim_memset(&ccline, 0, sizeof(struct cmdline_info));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 ccline.cmdprompt = prompt;
2543 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002544# ifdef FEAT_EVAL
2545 ccline.xp_context = xp_context;
2546 ccline.xp_arg = xp_arg;
2547 ccline.input_fn = (firstc == '@');
2548# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002549 msg_silent = 0;
Bram Moolenaar438d1762018-09-30 17:11:48 +02002550 s = getcmdline_int(firstc, 1L, 0, FALSE);
2551
2552 if (did_save_ccline)
2553 restore_cmdline(&save_ccline);
2554
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002555 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002556 /* Restore msg_col, the prompt from input() may have changed it.
2557 * But only if called recursively and the commandline is therefore being
2558 * restored to an old one; if not, the input() prompt stays on the screen,
2559 * so we need its modified msg_col left intact. */
2560 if (ccline.cmdbuff != NULL)
2561 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562
2563 return s;
2564}
2565#endif
2566
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002567/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002568 * Return TRUE when the text must not be changed and we can't switch to
2569 * another window or buffer. Used when editing the command line, evaluating
2570 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002571 */
2572 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002573text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002574{
2575#ifdef FEAT_CMDWIN
2576 if (cmdwin_type != 0)
2577 return TRUE;
2578#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002579 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002580}
2581
2582/*
2583 * Give an error message for a command that isn't allowed while the cmdline
2584 * window is open or editing the cmdline in another way.
2585 */
2586 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002587text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002588{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002589 EMSG(_(get_text_locked_msg()));
2590}
2591
2592 char_u *
2593get_text_locked_msg(void)
2594{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002595#ifdef FEAT_CMDWIN
2596 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002597 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002598#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002599 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002600}
2601
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002602/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002603 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2604 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002605 */
2606 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002607curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002608{
2609 if (curbuf_lock > 0)
2610 {
2611 EMSG(_("E788: Not allowed to edit another buffer now"));
2612 return TRUE;
2613 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002614 return allbuf_locked();
2615}
2616
2617/*
2618 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2619 * message.
2620 */
2621 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002622allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002623{
2624 if (allbuf_lock > 0)
2625 {
2626 EMSG(_("E811: Not allowed to change buffer information now"));
2627 return TRUE;
2628 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002629 return FALSE;
2630}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002631
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002633cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634{
2635#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2636 if (cmdline_star > 0) /* showing '*', always 1 position */
2637 return 1;
2638#endif
2639 return ptr2cells(ccline.cmdbuff + idx);
2640}
2641
2642/*
2643 * Compute the offset of the cursor on the command line for the prompt and
2644 * indent.
2645 */
2646 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002647set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002649 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 ccline.cmdspos = 1 + ccline.cmdindent;
2651 else
2652 ccline.cmdspos = 0 + ccline.cmdindent;
2653}
2654
2655/*
2656 * Compute the screen position for the cursor on the command line.
2657 */
2658 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002659set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660{
2661 int i, m, c;
2662
2663 set_cmdspos();
2664 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002665 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002667 if (m < 0) /* overflow, Columns or Rows at weird value */
2668 m = MAXCOL;
2669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 else
2671 m = MAXCOL;
2672 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2673 {
2674 c = cmdline_charsize(i);
2675#ifdef FEAT_MBYTE
2676 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2677 if (has_mbyte)
2678 correct_cmdspos(i, c);
2679#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002680 /* If the cmdline doesn't fit, show cursor on last visible char.
2681 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 if ((ccline.cmdspos += c) >= m)
2683 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 ccline.cmdspos -= c;
2685 break;
2686 }
2687#ifdef FEAT_MBYTE
2688 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002689 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690#endif
2691 }
2692}
2693
2694#ifdef FEAT_MBYTE
2695/*
2696 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2697 * character that doesn't fit, so that a ">" must be displayed.
2698 */
2699 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002700correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002702 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2704 && ccline.cmdspos % Columns + cells > Columns)
2705 ccline.cmdspos++;
2706}
2707#endif
2708
2709/*
2710 * Get an Ex command line for the ":" command.
2711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002713getexline(
2714 int c, /* normally ':', NUL for ":append" */
2715 void *cookie UNUSED,
2716 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717{
2718 /* When executing a register, remove ':' that's in front of each line. */
2719 if (exec_from_reg && vpeekc() == ':')
2720 (void)vgetc();
2721 return getcmdline(c, 1L, indent);
2722}
2723
2724/*
2725 * Get an Ex command line for Ex mode.
2726 * In Ex mode we only use the OS supplied line editing features and no
2727 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002731getexmodeline(
2732 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002734 void *cookie UNUSED,
2735 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 garray_T line_ga;
2738 char_u *pend;
2739 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002740 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002741 int escaped = FALSE; /* CTRL-V typed */
2742 int vcol = 0;
2743 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002744 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002745 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746
2747 /* Switch cursor on now. This avoids that it happens after the "\n", which
2748 * confuses the system function that computes tabstops. */
2749 cursor_on();
2750
2751 /* always start in column 0; write a newline if necessary */
2752 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002753 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002755 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002757 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 if (p_prompt)
2759 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 while (indent-- > 0)
2761 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764
2765 ga_init2(&line_ga, 1, 30);
2766
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002767 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002768 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002769 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002770 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002771 while (indent >= 8)
2772 {
2773 ga_append(&line_ga, TAB);
2774 msg_puts((char_u *)" ");
2775 indent -= 8;
2776 }
2777 while (indent-- > 0)
2778 {
2779 ga_append(&line_ga, ' ');
2780 msg_putchar(' ');
2781 }
2782 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 ++no_mapping;
2784 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002785
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 /*
2787 * Get the line, one character at a time.
2788 */
2789 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002790 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002792 long sw;
2793 char_u *s;
2794
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 if (ga_grow(&line_ga, 40) == FAIL)
2796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002797
Bram Moolenaarba748c82017-02-26 14:00:07 +01002798 /*
2799 * Get one character at a time.
2800 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002801 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002802
2803 /* Check for a ":normal" command and no more characters left. */
2804 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2805 c1 = '\n';
2806 else
2807 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808
2809 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002810 * Handle line editing.
2811 * Previously this was left to the system, putting the terminal in
2812 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002814 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 msg_putchar('\n');
2817 break;
2818 }
2819
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002820 if (c1 == K_PS)
2821 {
2822 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2823 goto redraw;
2824 }
2825
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002826 if (!escaped)
2827 {
2828 /* CR typed means "enter", which is NL */
2829 if (c1 == '\r')
2830 c1 = '\n';
2831
2832 if (c1 == BS || c1 == K_BS
2833 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 if (line_ga.ga_len > 0)
2836 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002837#ifdef FEAT_MBYTE
2838 if (has_mbyte)
2839 {
2840 p = (char_u *)line_ga.ga_data;
2841 p[line_ga.ga_len] = NUL;
2842 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2843 line_ga.ga_len -= len;
2844 }
2845 else
2846#endif
2847 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 goto redraw;
2849 }
2850 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 }
2852
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002853 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002854 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002855 msg_col = startcol;
2856 msg_clr_eos();
2857 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002858 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 }
2860
2861 if (c1 == Ctrl_T)
2862 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002863 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864 p = (char_u *)line_ga.ga_data;
2865 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002866 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002867 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002868add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002869 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002871 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002872 p = (char_u *)line_ga.ga_data;
2873 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2875 *s = ' ';
2876 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002878redraw:
2879 /* redraw the line */
2880 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002881 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002882 p = (char_u *)line_ga.ga_data;
2883 p[line_ga.ga_len] = NUL;
2884 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002888 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002890 msg_putchar(' ');
2891 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002892 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002896 len = MB_PTR2LEN(p);
2897 msg_outtrans_len(p, len);
2898 vcol += ptr2cells(p);
2899 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002903 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002904 continue;
2905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002907 if (c1 == Ctrl_D)
2908 {
2909 /* Delete one shiftwidth. */
2910 p = (char_u *)line_ga.ga_data;
2911 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002913 if (prev_char == '^')
2914 ex_keep_indent = TRUE;
2915 indent = 0;
2916 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 }
2918 else
2919 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002920 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002921 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002922 if (indent > 0)
2923 {
2924 --indent;
2925 indent -= indent % get_sw_value(curbuf);
2926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002928 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002929 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002930 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002931 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2932 --line_ga.ga_len;
2933 }
2934 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002936
2937 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2938 {
2939 escaped = TRUE;
2940 continue;
2941 }
2942
2943 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2944 if (IS_SPECIAL(c1))
2945 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002947
2948 if (IS_SPECIAL(c1))
2949 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002950#ifdef FEAT_MBYTE
2951 if (has_mbyte)
2952 len = (*mb_char2bytes)(c1,
2953 (char_u *)line_ga.ga_data + line_ga.ga_len);
2954 else
2955#endif
2956 {
2957 len = 1;
2958 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2959 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002960 if (c1 == '\n')
2961 msg_putchar('\n');
2962 else if (c1 == TAB)
2963 {
2964 /* Don't use chartabsize(), 'ts' can be different */
2965 do
2966 {
2967 msg_putchar(' ');
2968 } while (++vcol % 8);
2969 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002972 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002973 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002974 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002976 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002977 escaped = FALSE;
2978
2979 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002980 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002981
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002982 /* We are done when a NL is entered, but not when it comes after an
2983 * odd number of backslashes, that results in a NUL. */
2984 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002986 int bcount = 0;
2987
2988 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2989 ++bcount;
2990
2991 if (bcount > 0)
2992 {
2993 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2994 * "\NL", etc. */
2995 line_ga.ga_len -= (bcount + 1) / 2;
2996 pend -= (bcount + 1) / 2;
2997 pend[-1] = '\n';
2998 }
2999
3000 if ((bcount & 1) == 0)
3001 {
3002 --line_ga.ga_len;
3003 --pend;
3004 *pend = NUL;
3005 break;
3006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 }
3008 }
3009
Bram Moolenaar26a60b42005-02-22 08:49:11 +00003010 --no_mapping;
3011 --allow_keys;
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 /* make following messages go to the next line */
3014 msg_didout = FALSE;
3015 msg_col = 0;
3016 if (msg_row < Rows - 1)
3017 ++msg_row;
3018 emsg_on_display = FALSE; /* don't want ui_delay() */
3019
3020 if (got_int)
3021 ga_clear(&line_ga);
3022
3023 return (char_u *)line_ga.ga_data;
3024}
3025
Bram Moolenaare344bea2005-09-01 20:46:49 +00003026# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
3027 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028/*
3029 * Return TRUE if ccline.overstrike is on.
3030 */
3031 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003032cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 return ccline.overstrike;
3035}
3036
3037/*
3038 * Return TRUE if the cursor is at the end of the cmdline.
3039 */
3040 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003041cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042{
3043 return (ccline.cmdpos >= ccline.cmdlen);
3044}
3045#endif
3046
Bram Moolenaar9372a112005-12-06 19:59:18 +00003047#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048/*
3049 * Return the virtual column number at the current cursor position.
3050 * This is used by the IM code to obtain the start of the preedit string.
3051 */
3052 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003053cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054{
3055 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
3056 return MAXCOL;
3057
3058# ifdef FEAT_MBYTE
3059 if (has_mbyte)
3060 {
3061 colnr_T col;
3062 int i = 0;
3063
3064 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003065 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
3067 return col;
3068 }
3069 else
3070# endif
3071 return ccline.cmdpos;
3072}
3073#endif
3074
3075#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3076/*
3077 * If part of the command line is an IM preedit string, redraw it with
3078 * IM feedback attributes. The cursor position is restored after drawing.
3079 */
3080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003081redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
3083 if ((State & CMDLINE)
3084 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003085 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 && !p_imdisable
3087 && im_is_preediting())
3088 {
3089 int cmdpos = 0;
3090 int cmdspos;
3091 int old_row;
3092 int old_col;
3093 colnr_T col;
3094
3095 old_row = msg_row;
3096 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003097 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098
3099# ifdef FEAT_MBYTE
3100 if (has_mbyte)
3101 {
3102 for (col = 0; col < preedit_start_col
3103 && cmdpos < ccline.cmdlen; ++col)
3104 {
3105 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003106 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108 }
3109 else
3110# endif
3111 {
3112 cmdspos += preedit_start_col;
3113 cmdpos += preedit_start_col;
3114 }
3115
3116 msg_row = cmdline_row + (cmdspos / (int)Columns);
3117 msg_col = cmdspos % (int)Columns;
3118 if (msg_row >= Rows)
3119 msg_row = Rows - 1;
3120
3121 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3122 {
3123 int char_len;
3124 int char_attr;
3125
3126 char_attr = im_get_feedback_attr(col);
3127 if (char_attr < 0)
3128 break; /* end of preedit string */
3129
3130# ifdef FEAT_MBYTE
3131 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003132 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 else
3134# endif
3135 char_len = 1;
3136
3137 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3138 cmdpos += char_len;
3139 }
3140
3141 msg_row = old_row;
3142 msg_col = old_col;
3143 }
3144}
3145#endif /* FEAT_XIM && FEAT_GUI_GTK */
3146
3147/*
3148 * Allocate a new command line buffer.
3149 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 */
3151 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003152alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154 /*
3155 * give some extra space to avoid having to allocate all the time
3156 */
3157 if (len < 80)
3158 len = 100;
3159 else
3160 len += 20;
3161
3162 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3163 ccline.cmdbufflen = len;
3164}
3165
3166/*
3167 * Re-allocate the command line to length len + something extra.
3168 * return FAIL for failure, OK otherwise
3169 */
3170 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003171realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172{
3173 char_u *p;
3174
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003175 if (len < ccline.cmdbufflen)
3176 return OK; /* no need to resize */
3177
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 p = ccline.cmdbuff;
3179 alloc_cmdbuff(len); /* will get some more */
3180 if (ccline.cmdbuff == NULL) /* out of memory */
3181 {
3182 ccline.cmdbuff = p; /* keep the old one */
3183 return FAIL;
3184 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003185 /* There isn't always a NUL after the command, but it may need to be
3186 * there, thus copy up to the NUL and add a NUL. */
3187 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3188 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003190
3191 if (ccline.xpc != NULL
3192 && ccline.xpc->xp_pattern != NULL
3193 && ccline.xpc->xp_context != EXPAND_NOTHING
3194 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3195 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003196 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003197
3198 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3199 * to point into the newly allocated memory. */
3200 if (i >= 0 && i <= ccline.cmdlen)
3201 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3202 }
3203
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 return OK;
3205}
3206
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003207#if defined(FEAT_ARABIC) || defined(PROTO)
3208static char_u *arshape_buf = NULL;
3209
3210# if defined(EXITFREE) || defined(PROTO)
3211 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003212free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003213{
3214 vim_free(arshape_buf);
3215}
3216# endif
3217#endif
3218
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219/*
3220 * Draw part of the cmdline at the current cursor position. But draw stars
3221 * when cmdline_star is TRUE.
3222 */
3223 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003224draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
3226#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3227 int i;
3228
3229 if (cmdline_star > 0)
3230 for (i = 0; i < len; ++i)
3231 {
3232 msg_putchar('*');
3233# ifdef FEAT_MBYTE
3234 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003235 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236# endif
3237 }
3238 else
3239#endif
3240#ifdef FEAT_ARABIC
3241 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 static int buflen = 0;
3244 char_u *p;
3245 int j;
3246 int newlen = 0;
3247 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003248 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 int prev_c = 0;
3250 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003251 int u8c;
3252 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254
3255 /*
3256 * Do arabic shaping into a temporary buffer. This is very
3257 * inefficient!
3258 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003259 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 {
3261 /* Re-allocate the buffer. We keep it around to avoid a lot of
3262 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003263 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003264 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003265 arshape_buf = alloc(buflen);
3266 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 return; /* out of memory */
3268 }
3269
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003270 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3271 {
3272 /* Prepend a space to draw the leading composing char on. */
3273 arshape_buf[0] = ' ';
3274 newlen = 1;
3275 }
3276
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 for (j = start; j < start + len; j += mb_l)
3278 {
3279 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003280 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003281 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (ARABIC_CHAR(u8c))
3283 {
3284 /* Do Arabic shaping. */
3285 if (cmdmsg_rl)
3286 {
3287 /* displaying from right to left */
3288 pc = prev_c;
3289 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003290 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (j + mb_l >= start + len)
3292 nc = NUL;
3293 else
3294 nc = utf_ptr2char(p + mb_l);
3295 }
3296 else
3297 {
3298 /* displaying from left to right */
3299 if (j + mb_l >= start + len)
3300 pc = NUL;
3301 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003302 {
3303 int pcc[MAX_MCO];
3304
3305 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003307 pc1 = pcc[0];
3308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 nc = prev_c;
3310 }
3311 prev_c = u8c;
3312
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003313 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003315 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003316 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003318 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3319 if (u8cc[1] != 0)
3320 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003321 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 }
3323 }
3324 else
3325 {
3326 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 newlen += mb_l;
3329 }
3330 }
3331
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003332 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 }
3334 else
3335#endif
3336 msg_outtrans_len(ccline.cmdbuff + start, len);
3337}
3338
3339/*
3340 * Put a character on the command line. Shifts the following text to the
3341 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3342 * "c" must be printable (fit in one display cell)!
3343 */
3344 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003345putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 if (cmd_silent)
3348 return;
3349 msg_no_more = TRUE;
3350 msg_putchar(c);
3351 if (shift)
3352 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3353 msg_no_more = FALSE;
3354 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003355 extra_char = c;
3356 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357}
3358
3359/*
3360 * Undo a putcmdline(c, FALSE).
3361 */
3362 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003363unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 if (cmd_silent)
3366 return;
3367 msg_no_more = TRUE;
3368 if (ccline.cmdlen == ccline.cmdpos)
3369 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003370#ifdef FEAT_MBYTE
3371 else if (has_mbyte)
3372 draw_cmdline(ccline.cmdpos,
3373 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 else
3376 draw_cmdline(ccline.cmdpos, 1);
3377 msg_no_more = FALSE;
3378 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003379 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380}
3381
3382/*
3383 * Put the given string, of the given length, onto the command line.
3384 * If len is -1, then STRLEN() is used to calculate the length.
3385 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3386 * part will be redrawn, otherwise it will not. If this function is called
3387 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3388 * called afterwards.
3389 */
3390 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003391put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 int retval;
3394 int i;
3395 int m;
3396 int c;
3397
3398 if (len < 0)
3399 len = (int)STRLEN(str);
3400
3401 /* Check if ccline.cmdbuff needs to be longer */
3402 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003403 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 else
3405 retval = OK;
3406 if (retval == OK)
3407 {
3408 if (!ccline.overstrike)
3409 {
3410 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3411 ccline.cmdbuff + ccline.cmdpos,
3412 (size_t)(ccline.cmdlen - ccline.cmdpos));
3413 ccline.cmdlen += len;
3414 }
3415 else
3416 {
3417#ifdef FEAT_MBYTE
3418 if (has_mbyte)
3419 {
3420 /* Count nr of characters in the new string. */
3421 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003422 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 ++m;
3424 /* Count nr of bytes in cmdline that are overwritten by these
3425 * characters. */
3426 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003427 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 --m;
3429 if (i < ccline.cmdlen)
3430 {
3431 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3432 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3433 ccline.cmdlen += ccline.cmdpos + len - i;
3434 }
3435 else
3436 ccline.cmdlen = ccline.cmdpos + len;
3437 }
3438 else
3439#endif
3440 if (ccline.cmdpos + len > ccline.cmdlen)
3441 ccline.cmdlen = ccline.cmdpos + len;
3442 }
3443 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3444 ccline.cmdbuff[ccline.cmdlen] = NUL;
3445
3446#ifdef FEAT_MBYTE
3447 if (enc_utf8)
3448 {
3449 /* When the inserted text starts with a composing character,
3450 * backup to the character before it. There could be two of them.
3451 */
3452 i = 0;
3453 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3454 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3455 {
3456 i = (*mb_head_off)(ccline.cmdbuff,
3457 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3458 ccline.cmdpos -= i;
3459 len += i;
3460 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3461 }
3462# ifdef FEAT_ARABIC
3463 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3464 {
3465 /* Check the previous character for Arabic combining pair. */
3466 i = (*mb_head_off)(ccline.cmdbuff,
3467 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3468 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3469 + ccline.cmdpos - i), c))
3470 {
3471 ccline.cmdpos -= i;
3472 len += i;
3473 }
3474 else
3475 i = 0;
3476 }
3477# endif
3478 if (i != 0)
3479 {
3480 /* Also backup the cursor position. */
3481 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3482 ccline.cmdspos -= i;
3483 msg_col -= i;
3484 if (msg_col < 0)
3485 {
3486 msg_col += Columns;
3487 --msg_row;
3488 }
3489 }
3490 }
3491#endif
3492
3493 if (redraw && !cmd_silent)
3494 {
3495 msg_no_more = TRUE;
3496 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003497 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3499 /* Avoid clearing the rest of the line too often. */
3500 if (cmdline_row != i || ccline.overstrike)
3501 msg_clr_eos();
3502 msg_no_more = FALSE;
3503 }
3504#ifdef FEAT_FKMAP
3505 /*
3506 * If we are in Farsi command mode, the character input must be in
3507 * Insert mode. So do not advance the cmdpos.
3508 */
3509 if (!cmd_fkmap)
3510#endif
3511 {
3512 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003515 if (m < 0) /* overflow, Columns or Rows at weird value */
3516 m = MAXCOL;
3517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 else
3519 m = MAXCOL;
3520 for (i = 0; i < len; ++i)
3521 {
3522 c = cmdline_charsize(ccline.cmdpos);
3523#ifdef FEAT_MBYTE
3524 /* count ">" for a double-wide char that doesn't fit. */
3525 if (has_mbyte)
3526 correct_cmdspos(ccline.cmdpos, c);
3527#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003528 /* Stop cursor at the end of the screen, but do increment the
3529 * insert position, so that entering a very long command
3530 * works, even though you can't see it. */
3531 if (ccline.cmdspos + c < m)
3532 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533#ifdef FEAT_MBYTE
3534 if (has_mbyte)
3535 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003536 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 if (c > len - i - 1)
3538 c = len - i - 1;
3539 ccline.cmdpos += c;
3540 i += c;
3541 }
3542#endif
3543 ++ccline.cmdpos;
3544 }
3545 }
3546 }
3547 if (redraw)
3548 msg_check();
3549 return retval;
3550}
3551
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003552static struct cmdline_info prev_ccline;
3553static int prev_ccline_used = FALSE;
3554
3555/*
3556 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3557 * and overwrite it. But get_cmdline_str() may need it, thus make it
3558 * available globally in prev_ccline.
3559 */
3560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003561save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003562{
3563 if (!prev_ccline_used)
3564 {
3565 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3566 prev_ccline_used = TRUE;
3567 }
3568 *ccp = prev_ccline;
3569 prev_ccline = ccline;
Bram Moolenaar438d1762018-09-30 17:11:48 +02003570 ccline.cmdbuff = NULL; // signal that ccline is not in use
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003571}
3572
3573/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003574 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003575 */
3576 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003577restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003578{
3579 ccline = prev_ccline;
3580 prev_ccline = *ccp;
3581}
3582
Bram Moolenaar8299df92004-07-10 09:47:34 +00003583/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003584 * Paste a yank register into the command line.
3585 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003586 * insert_reg() can't be used here, because special characters from the
3587 * register contents will be interpreted as commands.
3588 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003589 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003590 */
3591 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003592cmdline_paste(
3593 int regname,
3594 int literally, /* Insert text literally instead of "as typed" */
3595 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003596{
3597 long i;
3598 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003599 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003600 int allocated;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003601
3602 /* check for valid regname; also accept special characters for CTRL-R in
3603 * the command line */
3604 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003605 && regname != Ctrl_A && regname != Ctrl_L
3606 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003607 return FAIL;
3608
3609 /* A register containing CTRL-R can cause an endless loop. Allow using
3610 * CTRL-C to break the loop. */
3611 line_breakcheck();
3612 if (got_int)
3613 return FAIL;
3614
3615#ifdef FEAT_CLIPBOARD
3616 regname = may_get_selection(regname);
3617#endif
3618
Bram Moolenaar438d1762018-09-30 17:11:48 +02003619 // Need to set "textlock" to avoid nasty things like going to another
3620 // buffer when evaluating an expression.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003621 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003622 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003623 --textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003624
3625 if (i)
3626 {
3627 /* Got the value of a special register in "arg". */
3628 if (arg == NULL)
3629 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003630
3631 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3632 * part of the word. */
3633 p = arg;
3634 if (p_is && regname == Ctrl_W)
3635 {
3636 char_u *w;
3637 int len;
3638
3639 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003640 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003641 {
3642#ifdef FEAT_MBYTE
3643 if (has_mbyte)
3644 {
3645 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3646 if (!vim_iswordc(mb_ptr2char(w - len)))
3647 break;
3648 w -= len;
3649 }
3650 else
3651#endif
3652 {
3653 if (!vim_iswordc(w[-1]))
3654 break;
3655 --w;
3656 }
3657 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003658 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003659 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3660 p += len;
3661 }
3662
3663 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003664 if (allocated)
3665 vim_free(arg);
3666 return OK;
3667 }
3668
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003669 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003670}
3671
3672/*
3673 * Put a string on the command line.
3674 * When "literally" is TRUE, insert literally.
3675 * When "literally" is FALSE, insert as typed, but don't leave the command
3676 * line.
3677 */
3678 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003679cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003680{
3681 int c, cv;
3682
3683 if (literally)
3684 put_on_cmdline(s, -1, TRUE);
3685 else
3686 while (*s != NUL)
3687 {
3688 cv = *s;
3689 if (cv == Ctrl_V && s[1])
3690 ++s;
3691#ifdef FEAT_MBYTE
3692 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003693 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003694 else
3695#endif
3696 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003697 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3698 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003699#ifdef UNIX
3700 || c == intr_char
3701#endif
3702 || (c == Ctrl_BSL && *s == Ctrl_N))
3703 stuffcharReadbuff(Ctrl_V);
3704 stuffcharReadbuff(c);
3705 }
3706}
3707
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708#ifdef FEAT_WILDMENU
3709/*
3710 * Delete characters on the command line, from "from" to the current
3711 * position.
3712 */
3713 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003714cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3717 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3718 ccline.cmdlen -= ccline.cmdpos - from;
3719 ccline.cmdpos = from;
3720}
3721#endif
3722
3723/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003724 * This function is called when the screen size changes and with incremental
3725 * search and in other situations where the command line may have been
3726 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 */
3728 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003729redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003731 redrawcmdline_ex(TRUE);
3732}
3733
3734 void
3735redrawcmdline_ex(int do_compute_cmdrow)
3736{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 if (cmd_silent)
3738 return;
3739 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003740 if (do_compute_cmdrow)
3741 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 redrawcmd();
3743 cursorcmd();
3744}
3745
3746 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003747redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748{
3749 int i;
3750
3751 if (cmd_silent)
3752 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003753 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 msg_putchar(ccline.cmdfirstc);
3755 if (ccline.cmdprompt != NULL)
3756 {
3757 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3758 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3759 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003760 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 --ccline.cmdindent;
3762 }
3763 else
3764 for (i = ccline.cmdindent; i > 0; --i)
3765 msg_putchar(' ');
3766}
3767
3768/*
3769 * Redraw what is currently on the command line.
3770 */
3771 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003772redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
3774 if (cmd_silent)
3775 return;
3776
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003777 /* when 'incsearch' is set there may be no command line while redrawing */
3778 if (ccline.cmdbuff == NULL)
3779 {
3780 windgoto(cmdline_row, 0);
3781 msg_clr_eos();
3782 return;
3783 }
3784
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 msg_start();
3786 redrawcmdprompt();
3787
3788 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3789 msg_no_more = TRUE;
3790 draw_cmdline(0, ccline.cmdlen);
3791 msg_clr_eos();
3792 msg_no_more = FALSE;
3793
3794 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003795 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003796 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797
3798 /*
3799 * An emsg() before may have set msg_scroll. This is used in normal mode,
3800 * in cmdline mode we can reset them now.
3801 */
3802 msg_scroll = FALSE; /* next message overwrites cmdline */
3803
3804 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3805 * in cmdline mode */
3806 skip_redraw = FALSE;
3807}
3808
3809 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003810compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003812 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 cmdline_row = Rows - 1;
3814 else
3815 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003816 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817}
3818
3819 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003820cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821{
3822 if (cmd_silent)
3823 return;
3824
3825#ifdef FEAT_RIGHTLEFT
3826 if (cmdmsg_rl)
3827 {
3828 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3829 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3830 if (msg_row <= 0)
3831 msg_row = Rows - 1;
3832 }
3833 else
3834#endif
3835 {
3836 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3837 msg_col = ccline.cmdspos % (int)Columns;
3838 if (msg_row >= Rows)
3839 msg_row = Rows - 1;
3840 }
3841
3842 windgoto(msg_row, msg_col);
3843#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003844 if (p_imst == IM_ON_THE_SPOT)
3845 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846#endif
3847#ifdef MCH_CURSOR_SHAPE
3848 mch_update_cursor();
3849#endif
3850}
3851
3852 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003853gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 msg_start();
3856#ifdef FEAT_RIGHTLEFT
3857 if (cmdmsg_rl)
3858 msg_col = Columns - 1;
3859 else
3860#endif
3861 msg_col = 0; /* always start in column 0 */
3862 if (clr) /* clear the bottom line(s) */
3863 msg_clr_eos(); /* will reset clear_cmdline */
3864 windgoto(cmdline_row, 0);
3865}
3866
3867/*
3868 * Check the word in front of the cursor for an abbreviation.
3869 * Called when the non-id character "c" has been entered.
3870 * When an abbreviation is recognized it is removed from the text with
3871 * backspaces and the replacement string is inserted, followed by "c".
3872 */
3873 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003874ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003876 int spos = 0;
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3879 return FALSE;
3880
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003881 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3882 * Actually accepts any mark. */
3883 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3884 spos++;
3885 if (ccline.cmdlen - spos > 5
3886 && ccline.cmdbuff[spos] == '\''
3887 && ccline.cmdbuff[spos + 2] == ','
3888 && ccline.cmdbuff[spos + 3] == '\'')
3889 spos += 5;
3890 else
3891 /* check abbreviation from the beginning of the commandline */
3892 spos = 0;
3893
3894 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895}
3896
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003897#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3898 static int
3899#ifdef __BORLANDC__
3900_RTLENTRYF
3901#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003902sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003903{
3904 char_u *p1 = *(char_u **)s1;
3905 char_u *p2 = *(char_u **)s2;
3906
3907 if (*p1 != '<' && *p2 == '<') return -1;
3908 if (*p1 == '<' && *p2 != '<') return 1;
3909 return STRCMP(p1, p2);
3910}
3911#endif
3912
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913/*
3914 * Return FAIL if this is not an appropriate context in which to do
3915 * completion of anything, return OK if it is (even if there are no matches).
3916 * For the caller, this means that the character is just passed through like a
3917 * normal character (instead of being expanded). This allows :s/^I^D etc.
3918 */
3919 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003920nextwild(
3921 expand_T *xp,
3922 int type,
3923 int options, /* extra options for ExpandOne() */
3924 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925{
3926 int i, j;
3927 char_u *p1;
3928 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 int difflen;
3930 int v;
3931
3932 if (xp->xp_numfiles == -1)
3933 {
3934 set_expand_context(xp);
3935 cmd_showtail = expand_showtail(xp);
3936 }
3937
3938 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3939 {
3940 beep_flush();
3941 return OK; /* Something illegal on command line */
3942 }
3943 if (xp->xp_context == EXPAND_NOTHING)
3944 {
3945 /* Caller can use the character as a normal char instead */
3946 return FAIL;
3947 }
3948
3949 MSG_PUTS("..."); /* show that we are busy */
3950 out_flush();
3951
3952 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003953 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954
3955 if (type == WILD_NEXT || type == WILD_PREV)
3956 {
3957 /*
3958 * Get next/previous match for a previous expanded pattern.
3959 */
3960 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3961 }
3962 else
3963 {
3964 /*
3965 * Translate string into pattern and expand it.
3966 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003967 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3968 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 p2 = NULL;
3970 else
3971 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003972 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003973 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3974 if (escape)
3975 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003976
3977 if (p_wic)
3978 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003979 p2 = ExpandOne(xp, p1,
3980 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003981 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003983 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 if (p2 != NULL && type == WILD_LONGEST)
3985 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003986 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 if (ccline.cmdbuff[i + j] == '*'
3988 || ccline.cmdbuff[i + j] == '?')
3989 break;
3990 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003991 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 }
3993 }
3994 }
3995
3996 if (p2 != NULL && !got_int)
3997 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003998 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003999 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02004001 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 xp->xp_pattern = ccline.cmdbuff + i;
4003 }
4004 else
4005 v = OK;
4006 if (v == OK)
4007 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004008 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
4009 &ccline.cmdbuff[ccline.cmdpos],
4010 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
4011 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 ccline.cmdlen += difflen;
4013 ccline.cmdpos += difflen;
4014 }
4015 }
4016 vim_free(p2);
4017
4018 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00004019 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020
4021 /* When expanding a ":map" command and no matches are found, assume that
4022 * the key is supposed to be inserted literally */
4023 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
4024 return FAIL;
4025
4026 if (xp->xp_numfiles <= 0 && p2 == NULL)
4027 beep_flush();
4028 else if (xp->xp_numfiles == 1)
4029 /* free expanded pattern */
4030 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
4031
4032 return OK;
4033}
4034
4035/*
4036 * Do wildcard expansion on the string 'str'.
4037 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004038 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 * Return NULL for failure.
4040 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004041 * "orig" is the originally expanded string, copied to allocated memory. It
4042 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4043 * WILD_PREV "orig" should be NULL.
4044 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004045 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4046 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 *
4048 * mode = WILD_FREE: just free previously expanded matches
4049 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4050 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4051 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4052 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4053 * mode = WILD_ALL: return all matches concatenated
4054 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004055 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 *
4057 * options = WILD_LIST_NOTFOUND: list entries without a match
4058 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4059 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4060 * options = WILD_NO_BEEP: Don't beep for multiple matches
4061 * options = WILD_ADD_SLASH: add a slash after directory names
4062 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4063 * options = WILD_SILENT: don't print warning messages
4064 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004065 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 *
4067 * The variables xp->xp_context and xp->xp_backslash must have been set!
4068 */
4069 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004070ExpandOne(
4071 expand_T *xp,
4072 char_u *str,
4073 char_u *orig, /* allocated copy of original of expanded string */
4074 int options,
4075 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076{
4077 char_u *ss = NULL;
4078 static int findex;
4079 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004080 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 int i;
4082 long_u len;
4083 int non_suf_match; /* number without matching suffix */
4084
4085 /*
4086 * first handle the case of using an old match
4087 */
4088 if (mode == WILD_NEXT || mode == WILD_PREV)
4089 {
4090 if (xp->xp_numfiles > 0)
4091 {
4092 if (mode == WILD_PREV)
4093 {
4094 if (findex == -1)
4095 findex = xp->xp_numfiles;
4096 --findex;
4097 }
4098 else /* mode == WILD_NEXT */
4099 ++findex;
4100
4101 /*
4102 * When wrapping around, return the original string, set findex to
4103 * -1.
4104 */
4105 if (findex < 0)
4106 {
4107 if (orig_save == NULL)
4108 findex = xp->xp_numfiles - 1;
4109 else
4110 findex = -1;
4111 }
4112 if (findex >= xp->xp_numfiles)
4113 {
4114 if (orig_save == NULL)
4115 findex = 0;
4116 else
4117 findex = -1;
4118 }
4119#ifdef FEAT_WILDMENU
4120 if (p_wmnu)
4121 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4122 findex, cmd_showtail);
4123#endif
4124 if (findex == -1)
4125 return vim_strsave(orig_save);
4126 return vim_strsave(xp->xp_files[findex]);
4127 }
4128 else
4129 return NULL;
4130 }
4131
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004132 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4134 {
4135 FreeWild(xp->xp_numfiles, xp->xp_files);
4136 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004137 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 }
4139 findex = 0;
4140
4141 if (mode == WILD_FREE) /* only release file name */
4142 return NULL;
4143
4144 if (xp->xp_numfiles == -1)
4145 {
4146 vim_free(orig_save);
4147 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004148 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149
4150 /*
4151 * Do the expansion.
4152 */
4153 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4154 options) == FAIL)
4155 {
4156#ifdef FNAME_ILLEGAL
4157 /* Illegal file name has been silently skipped. But when there
4158 * are wildcards, the real problem is that there was no match,
4159 * causing the pattern to be added, which has illegal characters.
4160 */
4161 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4162 EMSG2(_(e_nomatch2), str);
4163#endif
4164 }
4165 else if (xp->xp_numfiles == 0)
4166 {
4167 if (!(options & WILD_SILENT))
4168 EMSG2(_(e_nomatch2), str);
4169 }
4170 else
4171 {
4172 /* Escape the matches for use on the command line. */
4173 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4174
4175 /*
4176 * Check for matching suffixes in file names.
4177 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004178 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4179 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
4181 if (xp->xp_numfiles)
4182 non_suf_match = xp->xp_numfiles;
4183 else
4184 non_suf_match = 1;
4185 if ((xp->xp_context == EXPAND_FILES
4186 || xp->xp_context == EXPAND_DIRECTORIES)
4187 && xp->xp_numfiles > 1)
4188 {
4189 /*
4190 * More than one match; check suffix.
4191 * The files will have been sorted on matching suffix in
4192 * expand_wildcards, only need to check the first two.
4193 */
4194 non_suf_match = 0;
4195 for (i = 0; i < 2; ++i)
4196 if (match_suffix(xp->xp_files[i]))
4197 ++non_suf_match;
4198 }
4199 if (non_suf_match != 1)
4200 {
4201 /* Can we ever get here unless it's while expanding
4202 * interactively? If not, we can get rid of this all
4203 * together. Don't really want to wait for this message
4204 * (and possibly have to hit return to continue!).
4205 */
4206 if (!(options & WILD_SILENT))
4207 EMSG(_(e_toomany));
4208 else if (!(options & WILD_NO_BEEP))
4209 beep_flush();
4210 }
4211 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4212 ss = vim_strsave(xp->xp_files[0]);
4213 }
4214 }
4215 }
4216
4217 /* Find longest common part */
4218 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4219 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004220 int mb_len = 1;
4221 int c0, ci;
4222
4223 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004225#ifdef FEAT_MBYTE
4226 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004228 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4229 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4230 }
4231 else
4232#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004233 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004234 for (i = 1; i < xp->xp_numfiles; ++i)
4235 {
4236#ifdef FEAT_MBYTE
4237 if (has_mbyte)
4238 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4239 else
4240#endif
4241 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004242 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004244 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004245 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004247 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 break;
4249 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004250 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 break;
4252 }
4253 if (i < xp->xp_numfiles)
4254 {
4255 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004256 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 break;
4258 }
4259 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004260
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 ss = alloc((unsigned)len + 1);
4262 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004263 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 findex = -1; /* next p_wc gets first one */
4265 }
4266
4267 /* Concatenate all matching names */
4268 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4269 {
4270 len = 0;
4271 for (i = 0; i < xp->xp_numfiles; ++i)
4272 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4273 ss = lalloc(len, TRUE);
4274 if (ss != NULL)
4275 {
4276 *ss = NUL;
4277 for (i = 0; i < xp->xp_numfiles; ++i)
4278 {
4279 STRCAT(ss, xp->xp_files[i]);
4280 if (i != xp->xp_numfiles - 1)
4281 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4282 }
4283 }
4284 }
4285
4286 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4287 ExpandCleanup(xp);
4288
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004289 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004290 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004291 vim_free(orig);
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return ss;
4294}
4295
4296/*
4297 * Prepare an expand structure for use.
4298 */
4299 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004300ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004302 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004303 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004305#ifndef BACKSLASH_IN_FILENAME
4306 xp->xp_shell = FALSE;
4307#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 xp->xp_numfiles = -1;
4309 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004310#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4311 xp->xp_arg = NULL;
4312#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004313 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314}
4315
4316/*
4317 * Cleanup an expand structure after use.
4318 */
4319 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004320ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321{
4322 if (xp->xp_numfiles >= 0)
4323 {
4324 FreeWild(xp->xp_numfiles, xp->xp_files);
4325 xp->xp_numfiles = -1;
4326 }
4327}
4328
4329 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004330ExpandEscape(
4331 expand_T *xp,
4332 char_u *str,
4333 int numfiles,
4334 char_u **files,
4335 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336{
4337 int i;
4338 char_u *p;
4339
4340 /*
4341 * May change home directory back to "~"
4342 */
4343 if (options & WILD_HOME_REPLACE)
4344 tilde_replace(str, numfiles, files);
4345
4346 if (options & WILD_ESCAPE)
4347 {
4348 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004349 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004350 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 || xp->xp_context == EXPAND_BUFFERS
4352 || xp->xp_context == EXPAND_DIRECTORIES)
4353 {
4354 /*
4355 * Insert a backslash into a file name before a space, \, %, #
4356 * and wildmatch characters, except '~'.
4357 */
4358 for (i = 0; i < numfiles; ++i)
4359 {
4360 /* for ":set path=" we need to escape spaces twice */
4361 if (xp->xp_backslash == XP_BS_THREE)
4362 {
4363 p = vim_strsave_escaped(files[i], (char_u *)" ");
4364 if (p != NULL)
4365 {
4366 vim_free(files[i]);
4367 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004368#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 p = vim_strsave_escaped(files[i], (char_u *)" ");
4370 if (p != NULL)
4371 {
4372 vim_free(files[i]);
4373 files[i] = p;
4374 }
4375#endif
4376 }
4377 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004378#ifdef BACKSLASH_IN_FILENAME
4379 p = vim_strsave_fnameescape(files[i], FALSE);
4380#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004381 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 if (p != NULL)
4384 {
4385 vim_free(files[i]);
4386 files[i] = p;
4387 }
4388
4389 /* If 'str' starts with "\~", replace "~" at start of
4390 * files[i] with "\~". */
4391 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004392 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004395
4396 /* If the first file starts with a '+' escape it. Otherwise it
4397 * could be seen as "+cmd". */
4398 if (*files[0] == '+')
4399 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 }
4401 else if (xp->xp_context == EXPAND_TAGS)
4402 {
4403 /*
4404 * Insert a backslash before characters in a tag name that
4405 * would terminate the ":tag" command.
4406 */
4407 for (i = 0; i < numfiles; ++i)
4408 {
4409 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4410 if (p != NULL)
4411 {
4412 vim_free(files[i]);
4413 files[i] = p;
4414 }
4415 }
4416 }
4417 }
4418}
4419
4420/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004421 * Escape special characters in "fname" for when used as a file name argument
4422 * after a Vim command, or, when "shell" is non-zero, a shell command.
4423 * Returns the result in allocated memory.
4424 */
4425 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004426vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004427{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004428 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004429#ifdef BACKSLASH_IN_FILENAME
4430 char_u buf[20];
4431 int j = 0;
4432
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004433 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004434 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004435 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004436 buf[j++] = *p;
4437 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004438 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004439#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004440 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4441 if (shell && csh_like_shell() && p != NULL)
4442 {
4443 char_u *s;
4444
4445 /* For csh and similar shells need to put two backslashes before '!'.
4446 * One is taken by Vim, one by the shell. */
4447 s = vim_strsave_escaped(p, (char_u *)"!");
4448 vim_free(p);
4449 p = s;
4450 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004451#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004452
4453 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4454 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004455 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004456 escape_fname(&p);
4457
4458 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004459}
4460
4461/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004462 * Put a backslash before the file name in "pp", which is in allocated memory.
4463 */
4464 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004465escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004466{
4467 char_u *p;
4468
4469 p = alloc((unsigned)(STRLEN(*pp) + 2));
4470 if (p != NULL)
4471 {
4472 p[0] = '\\';
4473 STRCPY(p + 1, *pp);
4474 vim_free(*pp);
4475 *pp = p;
4476 }
4477}
4478
4479/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 * For each file name in files[num_files]:
4481 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4482 */
4483 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004484tilde_replace(
4485 char_u *orig_pat,
4486 int num_files,
4487 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488{
4489 int i;
4490 char_u *p;
4491
4492 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4493 {
4494 for (i = 0; i < num_files; ++i)
4495 {
4496 p = home_replace_save(NULL, files[i]);
4497 if (p != NULL)
4498 {
4499 vim_free(files[i]);
4500 files[i] = p;
4501 }
4502 }
4503 }
4504}
4505
4506/*
4507 * Show all matches for completion on the command line.
4508 * Returns EXPAND_NOTHING when the character that triggered expansion should
4509 * be inserted like a normal character.
4510 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004512showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513{
4514#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4515 int num_files;
4516 char_u **files_found;
4517 int i, j, k;
4518 int maxlen;
4519 int lines;
4520 int columns;
4521 char_u *p;
4522 int lastlen;
4523 int attr;
4524 int showtail;
4525
4526 if (xp->xp_numfiles == -1)
4527 {
4528 set_expand_context(xp);
4529 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4530 &num_files, &files_found);
4531 showtail = expand_showtail(xp);
4532 if (i != EXPAND_OK)
4533 return i;
4534
4535 }
4536 else
4537 {
4538 num_files = xp->xp_numfiles;
4539 files_found = xp->xp_files;
4540 showtail = cmd_showtail;
4541 }
4542
4543#ifdef FEAT_WILDMENU
4544 if (!wildmenu)
4545 {
4546#endif
4547 msg_didany = FALSE; /* lines_left will be set */
4548 msg_start(); /* prepare for paging */
4549 msg_putchar('\n');
4550 out_flush();
4551 cmdline_row = msg_row;
4552 msg_didany = FALSE; /* lines_left will be set again */
4553 msg_start(); /* prepare for paging */
4554#ifdef FEAT_WILDMENU
4555 }
4556#endif
4557
4558 if (got_int)
4559 got_int = FALSE; /* only int. the completion, not the cmd line */
4560#ifdef FEAT_WILDMENU
4561 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004562 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563#endif
4564 else
4565 {
4566 /* find the length of the longest file name */
4567 maxlen = 0;
4568 for (i = 0; i < num_files; ++i)
4569 {
4570 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004571 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 || xp->xp_context == EXPAND_BUFFERS))
4573 {
4574 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4575 j = vim_strsize(NameBuff);
4576 }
4577 else
4578 j = vim_strsize(L_SHOWFILE(i));
4579 if (j > maxlen)
4580 maxlen = j;
4581 }
4582
4583 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4584 lines = num_files;
4585 else
4586 {
4587 /* compute the number of columns and lines for the listing */
4588 maxlen += 2; /* two spaces between file names */
4589 columns = ((int)Columns + 2) / maxlen;
4590 if (columns < 1)
4591 columns = 1;
4592 lines = (num_files + columns - 1) / columns;
4593 }
4594
Bram Moolenaar8820b482017-03-16 17:23:31 +01004595 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4598 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004599 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 msg_clr_eos();
4601 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004602 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604
4605 /* list the files line by line */
4606 for (i = 0; i < lines; ++i)
4607 {
4608 lastlen = 999;
4609 for (k = i; k < num_files; k += lines)
4610 {
4611 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4612 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004613 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 p = files_found[k] + STRLEN(files_found[k]) + 1;
4615 msg_advance(maxlen + 1);
4616 msg_puts(p);
4617 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004618 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 break;
4620 }
4621 for (j = maxlen - lastlen; --j >= 0; )
4622 msg_putchar(' ');
4623 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004624 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 || xp->xp_context == EXPAND_BUFFERS)
4626 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004627 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004628 if (xp->xp_numfiles != -1)
4629 {
4630 char_u *halved_slash;
4631 char_u *exp_path;
4632
4633 /* Expansion was done before and special characters
4634 * were escaped, need to halve backslashes. Also
4635 * $HOME has been replaced with ~/. */
4636 exp_path = expand_env_save_opt(files_found[k], TRUE);
4637 halved_slash = backslash_halve_save(
4638 exp_path != NULL ? exp_path : files_found[k]);
4639 j = mch_isdir(halved_slash != NULL ? halved_slash
4640 : files_found[k]);
4641 vim_free(exp_path);
4642 vim_free(halved_slash);
4643 }
4644 else
4645 /* Expansion was done here, file names are literal. */
4646 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 if (showtail)
4648 p = L_SHOWFILE(k);
4649 else
4650 {
4651 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4652 TRUE);
4653 p = NameBuff;
4654 }
4655 }
4656 else
4657 {
4658 j = FALSE;
4659 p = L_SHOWFILE(k);
4660 }
4661 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4662 }
4663 if (msg_col > 0) /* when not wrapped around */
4664 {
4665 msg_clr_eos();
4666 msg_putchar('\n');
4667 }
4668 out_flush(); /* show one line at a time */
4669 if (got_int)
4670 {
4671 got_int = FALSE;
4672 break;
4673 }
4674 }
4675
4676 /*
4677 * we redraw the command below the lines that we have just listed
4678 * This is a bit tricky, but it saves a lot of screen updating.
4679 */
4680 cmdline_row = msg_row; /* will put it back later */
4681 }
4682
4683 if (xp->xp_numfiles == -1)
4684 FreeWild(num_files, files_found);
4685
4686 return EXPAND_OK;
4687}
4688
4689/*
4690 * Private gettail for showmatches() (and win_redr_status_matches()):
4691 * Find tail of file name path, but ignore trailing "/".
4692 */
4693 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004694sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 char_u *p;
4697 char_u *t = s;
4698 int had_sep = FALSE;
4699
4700 for (p = s; *p != NUL; )
4701 {
4702 if (vim_ispathsep(*p)
4703#ifdef BACKSLASH_IN_FILENAME
4704 && !rem_backslash(p)
4705#endif
4706 )
4707 had_sep = TRUE;
4708 else if (had_sep)
4709 {
4710 t = p;
4711 had_sep = FALSE;
4712 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004713 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715 return t;
4716}
4717
4718/*
4719 * Return TRUE if we only need to show the tail of completion matches.
4720 * When not completing file names or there is a wildcard in the path FALSE is
4721 * returned.
4722 */
4723 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004724expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725{
4726 char_u *s;
4727 char_u *end;
4728
4729 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004730 if (xp->xp_context != EXPAND_FILES
4731 && xp->xp_context != EXPAND_SHELLCMD
4732 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 return FALSE;
4734
4735 end = gettail(xp->xp_pattern);
4736 if (end == xp->xp_pattern) /* there is no path separator */
4737 return FALSE;
4738
4739 for (s = xp->xp_pattern; s < end; s++)
4740 {
4741 /* Skip escaped wildcards. Only when the backslash is not a path
4742 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4743 if (rem_backslash(s))
4744 ++s;
4745 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4746 return FALSE;
4747 }
4748 return TRUE;
4749}
4750
4751/*
4752 * Prepare a string for expansion.
4753 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004754 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 * When expanding other names: The string will be used with regcomp(). Copy
4756 * the name into allocated memory and prepend "^".
4757 */
4758 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004759addstar(
4760 char_u *fname,
4761 int len,
4762 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 char_u *retval;
4765 int i, j;
4766 int new_len;
4767 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004768 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004770 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004771 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004772 && context != EXPAND_SHELLCMD
4773 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 /*
4776 * Matching will be done internally (on something other than files).
4777 * So we convert the file-matching-type wildcards into our kind for
4778 * use with vim_regcomp(). First work out how long it will be:
4779 */
4780
4781 /* For help tags the translation is done in find_help_tags().
4782 * For a tag pattern starting with "/" no translation is needed. */
4783 if (context == EXPAND_HELP
4784 || context == EXPAND_COLORS
4785 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004786 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004787 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004788 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004789 || ((context == EXPAND_TAGS_LISTFILES
4790 || context == EXPAND_TAGS)
4791 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 retval = vim_strnsave(fname, len);
4793 else
4794 {
4795 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4796 for (i = 0; i < len; i++)
4797 {
4798 if (fname[i] == '*' || fname[i] == '~')
4799 new_len++; /* '*' needs to be replaced by ".*"
4800 '~' needs to be replaced by "\~" */
4801
4802 /* Buffer names are like file names. "." should be literal */
4803 if (context == EXPAND_BUFFERS && fname[i] == '.')
4804 new_len++; /* "." becomes "\." */
4805
4806 /* Custom expansion takes care of special things, match
4807 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004808 if ((context == EXPAND_USER_DEFINED
4809 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 new_len++; /* '\' becomes "\\" */
4811 }
4812 retval = alloc(new_len);
4813 if (retval != NULL)
4814 {
4815 retval[0] = '^';
4816 j = 1;
4817 for (i = 0; i < len; i++, j++)
4818 {
4819 /* Skip backslash. But why? At least keep it for custom
4820 * expansion. */
4821 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004822 && context != EXPAND_USER_LIST
4823 && fname[i] == '\\'
4824 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 break;
4826
4827 switch (fname[i])
4828 {
4829 case '*': retval[j++] = '.';
4830 break;
4831 case '~': retval[j++] = '\\';
4832 break;
4833 case '?': retval[j] = '.';
4834 continue;
4835 case '.': if (context == EXPAND_BUFFERS)
4836 retval[j++] = '\\';
4837 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004838 case '\\': if (context == EXPAND_USER_DEFINED
4839 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 retval[j++] = '\\';
4841 break;
4842 }
4843 retval[j] = fname[i];
4844 }
4845 retval[j] = NUL;
4846 }
4847 }
4848 }
4849 else
4850 {
4851 retval = alloc(len + 4);
4852 if (retval != NULL)
4853 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004854 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004857 * Don't add a star to *, ~, ~user, $var or `cmd`.
4858 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 * ~ would be at the start of the file name, but not the tail.
4860 * $ could be anywhere in the tail.
4861 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004862 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 */
4864 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004865 ends_in_star = (len > 0 && retval[len - 1] == '*');
4866#ifndef BACKSLASH_IN_FILENAME
4867 for (i = len - 2; i >= 0; --i)
4868 {
4869 if (retval[i] != '\\')
4870 break;
4871 ends_in_star = !ends_in_star;
4872 }
4873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004875 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 && vim_strchr(tail, '$') == NULL
4877 && vim_strchr(retval, '`') == NULL)
4878 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004879 else if (len > 0 && retval[len - 1] == '$')
4880 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 retval[len] = NUL;
4882 }
4883 }
4884 return retval;
4885}
4886
4887/*
4888 * Must parse the command line so far to work out what context we are in.
4889 * Completion can then be done based on that context.
4890 * This routine sets the variables:
4891 * xp->xp_pattern The start of the pattern to be expanded within
4892 * the command line (ends at the cursor).
4893 * xp->xp_context The type of thing to expand. Will be one of:
4894 *
4895 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4896 * the command line, like an unknown command. Caller
4897 * should beep.
4898 * EXPAND_NOTHING Unrecognised context for completion, use char like
4899 * a normal char, rather than for completion. eg
4900 * :s/^I/
4901 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4902 * it.
4903 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4904 * EXPAND_FILES After command with XFILE set, or after setting
4905 * with P_EXPAND set. eg :e ^I, :w>>^I
4906 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4907 * when we know only directories are of interest. eg
4908 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004909 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4911 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4912 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4913 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4914 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4915 * EXPAND_EVENTS Complete event names
4916 * EXPAND_SYNTAX Complete :syntax command arguments
4917 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4918 * EXPAND_AUGROUP Complete autocommand group names
4919 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4920 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4921 * eg :unmap a^I , :cunab x^I
4922 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4923 * eg :call sub^I
4924 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4925 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4926 * names in expressions, eg :while s^I
4927 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004928 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 */
4930 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004931set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004933 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 if (ccline.cmdfirstc != ':'
4935#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004936 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004937 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938#endif
4939 )
4940 {
4941 xp->xp_context = EXPAND_NOTHING;
4942 return;
4943 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004944 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945}
4946
4947 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004948set_cmd_context(
4949 expand_T *xp,
4950 char_u *str, /* start of command line */
4951 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004952 int col, /* position of cursor */
4953 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 int old_char = NUL;
4956 char_u *nextcomm;
4957
4958 /*
4959 * Avoid a UMR warning from Purify, only save the character if it has been
4960 * written before.
4961 */
4962 if (col < len)
4963 old_char = str[col];
4964 str[col] = NUL;
4965 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004966
4967#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004968 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004969 {
4970# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004971 /* pass CMD_SIZE because there is no real command */
4972 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004973# endif
4974 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004975 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004976 {
4977 xp->xp_context = ccline.xp_context;
4978 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004979# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004980 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004981# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004982 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004983 else
4984#endif
4985 while (nextcomm != NULL)
4986 nextcomm = set_one_cmd_context(xp, nextcomm);
4987
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004988 /* Store the string here so that call_user_expand_func() can get to them
4989 * easily. */
4990 xp->xp_line = str;
4991 xp->xp_col = col;
4992
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 str[col] = old_char;
4994}
4995
4996/*
4997 * Expand the command line "str" from context "xp".
4998 * "xp" must have been set by set_cmd_context().
4999 * xp->xp_pattern points into "str", to where the text that is to be expanded
5000 * starts.
5001 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
5002 * cursor.
5003 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
5004 * key that triggered expansion literally.
5005 * Returns EXPAND_OK otherwise.
5006 */
5007 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005008expand_cmdline(
5009 expand_T *xp,
5010 char_u *str, /* start of command line */
5011 int col, /* position of cursor */
5012 int *matchcount, /* return: nr of matches */
5013 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014{
5015 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005016 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017
5018 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
5019 {
5020 beep_flush();
5021 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
5022 }
5023 if (xp->xp_context == EXPAND_NOTHING)
5024 {
5025 /* Caller can use the character as a normal char instead */
5026 return EXPAND_NOTHING;
5027 }
5028
5029 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00005030 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
5031 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 if (file_str == NULL)
5033 return EXPAND_UNSUCCESSFUL;
5034
Bram Moolenaar94950a92010-12-02 16:01:29 +01005035 if (p_wic)
5036 options += WILD_ICASE;
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005039 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
5041 *matchcount = 0;
5042 *matches = NULL;
5043 }
5044 vim_free(file_str);
5045
5046 return EXPAND_OK;
5047}
5048
5049#ifdef FEAT_MULTI_LANG
5050/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005051 * Cleanup matches for help tags:
5052 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5053 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005056cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057{
5058 int i, j;
5059 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005060 char_u buf[4];
5061 char_u *p = buf;
5062
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005063 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005064 {
5065 *p++ = '@';
5066 *p++ = p_hlg[0];
5067 *p++ = p_hlg[1];
5068 }
5069 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 for (i = 0; i < num_file; ++i)
5072 {
5073 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005074 if (len <= 0)
5075 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005076 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 /* Sorting on priority means the same item in another language may
5079 * be anywhere. Search all items for a match up to the "@en". */
5080 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005081 if (j != i && (int)STRLEN(file[j]) == len + 3
5082 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005085 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 file[i][len] = NUL;
5087 }
5088 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005089
5090 if (*buf != NUL)
5091 for (i = 0; i < num_file; ++i)
5092 {
5093 len = (int)STRLEN(file[i]) - 3;
5094 if (len <= 0)
5095 continue;
5096 if (STRCMP(file[i] + len, buf) == 0)
5097 {
5098 /* remove the default language */
5099 file[i][len] = NUL;
5100 }
5101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102}
5103#endif
5104
5105/*
5106 * Do the expansion based on xp->xp_context and "pat".
5107 */
5108 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005109ExpandFromContext(
5110 expand_T *xp,
5111 char_u *pat,
5112 int *num_file,
5113 char_u ***file,
5114 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
5116#ifdef FEAT_CMDL_COMPL
5117 regmatch_T regmatch;
5118#endif
5119 int ret;
5120 int flags;
5121
5122 flags = EW_DIR; /* include directories */
5123 if (options & WILD_LIST_NOTFOUND)
5124 flags |= EW_NOTFOUND;
5125 if (options & WILD_ADD_SLASH)
5126 flags |= EW_ADDSLASH;
5127 if (options & WILD_KEEP_ALL)
5128 flags |= EW_KEEPALL;
5129 if (options & WILD_SILENT)
5130 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005131 if (options & WILD_ALLLINKS)
5132 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005134 if (xp->xp_context == EXPAND_FILES
5135 || xp->xp_context == EXPAND_DIRECTORIES
5136 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 /*
5139 * Expand file or directory names.
5140 */
5141 int free_pat = FALSE;
5142 int i;
5143
5144 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5145 * space */
5146 if (xp->xp_backslash != XP_BS_NONE)
5147 {
5148 free_pat = TRUE;
5149 pat = vim_strsave(pat);
5150 for (i = 0; pat[i]; ++i)
5151 if (pat[i] == '\\')
5152 {
5153 if (xp->xp_backslash == XP_BS_THREE
5154 && pat[i + 1] == '\\'
5155 && pat[i + 2] == '\\'
5156 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005157 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 if (xp->xp_backslash == XP_BS_ONE
5159 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005160 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
5162 }
5163
5164 if (xp->xp_context == EXPAND_FILES)
5165 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005166 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5167 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 else
5169 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005170 if (options & WILD_ICASE)
5171 flags |= EW_ICASE;
5172
Bram Moolenaard7834d32009-12-02 16:14:36 +00005173 /* Expand wildcards, supporting %:h and the like. */
5174 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 if (free_pat)
5176 vim_free(pat);
5177 return ret;
5178 }
5179
5180 *file = (char_u **)"";
5181 *num_file = 0;
5182 if (xp->xp_context == EXPAND_HELP)
5183 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005184 /* With an empty argument we would get all the help tags, which is
5185 * very slow. Get matches for "help" instead. */
5186 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5187 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189#ifdef FEAT_MULTI_LANG
5190 cleanup_help_tags(*num_file, *file);
5191#endif
5192 return OK;
5193 }
5194 return FAIL;
5195 }
5196
5197#ifndef FEAT_CMDL_COMPL
5198 return FAIL;
5199#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005200 if (xp->xp_context == EXPAND_SHELLCMD)
5201 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 if (xp->xp_context == EXPAND_OLD_SETTING)
5203 return ExpandOldSetting(num_file, file);
5204 if (xp->xp_context == EXPAND_BUFFERS)
5205 return ExpandBufnames(pat, num_file, file, options);
5206 if (xp->xp_context == EXPAND_TAGS
5207 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5208 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5209 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005210 {
5211 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005212 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5213 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005216 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005217 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005218 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005219 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005220 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005221 {
5222 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005223 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005224 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005225 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005226 {
5227 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005228 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005229 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005230# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5231 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005232 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005233# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005234 if (xp->xp_context == EXPAND_PACKADD)
5235 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
5237 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5238 if (regmatch.regprog == NULL)
5239 return FAIL;
5240
5241 /* set ignore-case according to p_ic, p_scs and pat */
5242 regmatch.rm_ic = ignorecase(pat);
5243
5244 if (xp->xp_context == EXPAND_SETTINGS
5245 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5246 ret = ExpandSettings(xp, &regmatch, num_file, file);
5247 else if (xp->xp_context == EXPAND_MAPPINGS)
5248 ret = ExpandMappings(&regmatch, num_file, file);
5249# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5250 else if (xp->xp_context == EXPAND_USER_DEFINED)
5251 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5252# endif
5253 else
5254 {
5255 static struct expgen
5256 {
5257 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005258 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005260 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 } tab[] =
5262 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005263 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5264 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005265 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005266 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005267#ifdef FEAT_CMDHIST
5268 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005271 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005272 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005273 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5274 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5275 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#endif
5277#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005278 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5279 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5280 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5281 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282#endif
5283#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005284 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5285 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286#endif
5287#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005288 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005290#ifdef FEAT_PROFILE
5291 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5292#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005293 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005294 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5295 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005296#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005297 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005298#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005299#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005300 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005301#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005302#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005303 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5306 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005307 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5308 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005310 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005311 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005312 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 };
5314 int i;
5315
5316 /*
5317 * Find a context in the table and call the ExpandGeneric() with the
5318 * right function to do the expansion.
5319 */
5320 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005321 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 if (xp->xp_context == tab[i].context)
5323 {
5324 if (tab[i].ic)
5325 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005326 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005327 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 break;
5329 }
5330 }
5331
Bram Moolenaar473de612013-06-08 18:19:48 +02005332 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333
5334 return ret;
5335#endif /* FEAT_CMDL_COMPL */
5336}
5337
5338#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5339/*
5340 * Expand a list of names.
5341 *
5342 * Generic function for command line completion. It calls a function to
5343 * obtain strings, one by one. The strings are matched against a regexp
5344 * program. Matching strings are copied into an array, which is returned.
5345 *
5346 * Returns OK when no problems encountered, FAIL for error (out of memory).
5347 */
5348 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005349ExpandGeneric(
5350 expand_T *xp,
5351 regmatch_T *regmatch,
5352 int *num_file,
5353 char_u ***file,
5354 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005356 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357{
5358 int i;
5359 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005360 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 char_u *str;
5362
5363 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005364 * round == 0: count the number of matching names
5365 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005367 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 for (i = 0; ; ++i)
5370 {
5371 str = (*func)(xp, i);
5372 if (str == NULL) /* end of list */
5373 break;
5374 if (*str == NUL) /* skip empty strings */
5375 continue;
5376
5377 if (vim_regexec(regmatch, str, (colnr_T)0))
5378 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005379 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005381 if (escaped)
5382 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5383 else
5384 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 (*file)[count] = str;
5386#ifdef FEAT_MENU
5387 if (func == get_menu_names && str != NULL)
5388 {
5389 /* test for separator added by get_menu_names() */
5390 str += STRLEN(str) - 1;
5391 if (*str == '\001')
5392 *str = '.';
5393 }
5394#endif
5395 }
5396 ++count;
5397 }
5398 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005399 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 {
5401 if (count == 0)
5402 return OK;
5403 *num_file = count;
5404 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5405 if (*file == NULL)
5406 {
5407 *file = (char_u **)"";
5408 return FAIL;
5409 }
5410 count = 0;
5411 }
5412 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005413
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005414 /* Sort the results. Keep menu's in the specified order. */
5415 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005416 {
5417 if (xp->xp_context == EXPAND_EXPRESSION
5418 || xp->xp_context == EXPAND_FUNCTIONS
5419 || xp->xp_context == EXPAND_USER_FUNC)
5420 /* <SNR> functions should be sorted to the end. */
5421 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5422 sort_func_compare);
5423 else
5424 sort_strings(*file, *num_file);
5425 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005426
Bram Moolenaar4f688582007-07-24 12:34:30 +00005427#ifdef FEAT_CMDL_COMPL
5428 /* Reset the variables used for special highlight names expansion, so that
5429 * they don't show up when getting normal highlight names by ID. */
5430 reset_expand_highlight();
5431#endif
5432
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 return OK;
5434}
5435
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005436/*
5437 * Complete a shell command.
5438 * Returns FAIL or OK;
5439 */
5440 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005441expand_shellcmd(
5442 char_u *filepat, /* pattern to match with command names */
5443 int *num_file, /* return: number of matches */
5444 char_u ***file, /* return: array with matches */
5445 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005446{
5447 char_u *pat;
5448 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005449 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005450 int mustfree = FALSE;
5451 garray_T ga;
5452 char_u *buf = alloc(MAXPATHL);
5453 size_t l;
5454 char_u *s, *e;
5455 int flags = flagsarg;
5456 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005457 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005458 hashtab_T found_ht;
5459 hashitem_T *hi;
5460 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005461
5462 if (buf == NULL)
5463 return FAIL;
5464
5465 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5466 * space */
5467 pat = vim_strsave(filepat);
5468 for (i = 0; pat[i]; ++i)
5469 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005470 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005471
Bram Moolenaarb5971142015-03-21 17:32:19 +01005472 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005473
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005474 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5475 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005476 path = (char_u *)".";
5477 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005478 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005479 /* For an absolute name we don't use $PATH. */
5480 if (!mch_isFullName(pat))
5481 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005482 if (path == NULL)
5483 path = (char_u *)"";
5484 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005485
5486 /*
5487 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005488 * collect them in "ga". When "." is not in $PATH also expand for the
5489 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005490 */
5491 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005492 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005493 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005494 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005495#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005496 e = vim_strchr(s, ';');
5497#else
5498 e = vim_strchr(s, ':');
5499#endif
5500 if (e == NULL)
5501 e = s + STRLEN(s);
5502
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005503 if (*s == NUL)
5504 {
5505 if (did_curdir)
5506 break;
5507 // Find directories in the current directory, path is empty.
5508 did_curdir = TRUE;
5509 flags |= EW_DIR;
5510 }
5511 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5512 {
5513 did_curdir = TRUE;
5514 flags |= EW_DIR;
5515 }
5516 else
5517 // Do not match directories inside a $PATH item.
5518 flags &= ~EW_DIR;
5519
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005520 l = e - s;
5521 if (l > MAXPATHL - 5)
5522 break;
5523 vim_strncpy(buf, s, l);
5524 add_pathsep(buf);
5525 l = STRLEN(buf);
5526 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5527
5528 /* Expand matches in one directory of $PATH. */
5529 ret = expand_wildcards(1, &buf, num_file, file, flags);
5530 if (ret == OK)
5531 {
5532 if (ga_grow(&ga, *num_file) == FAIL)
5533 FreeWild(*num_file, *file);
5534 else
5535 {
5536 for (i = 0; i < *num_file; ++i)
5537 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005538 char_u *name = (*file)[i];
5539
5540 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005541 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005542 // Check if this name was already found.
5543 hash = hash_hash(name + l);
5544 hi = hash_lookup(&found_ht, name + l, hash);
5545 if (HASHITEM_EMPTY(hi))
5546 {
5547 // Remove the path that was prepended.
5548 STRMOVE(name, name + l);
5549 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5550 hash_add_item(&found_ht, hi, name, hash);
5551 name = NULL;
5552 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005553 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005554 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005555 }
5556 vim_free(*file);
5557 }
5558 }
5559 if (*e != NUL)
5560 ++e;
5561 }
5562 *file = ga.ga_data;
5563 *num_file = ga.ga_len;
5564
5565 vim_free(buf);
5566 vim_free(pat);
5567 if (mustfree)
5568 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005569 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005570 return OK;
5571}
5572
5573
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5575/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005576 * Call "user_expand_func()" to invoke a user defined Vim script function and
5577 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005579 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005580call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005581 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005582 expand_T *xp,
5583 int *num_file,
5584 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005586 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005587 typval_T args[4];
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005588 sctx_T save_current_sctx = current_sctx;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005589 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005590 void *ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaarb7515462013-06-29 12:58:33 +02005592 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005593 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 *num_file = 0;
5595 *file = NULL;
5596
Bram Moolenaarb7515462013-06-29 12:58:33 +02005597 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005598 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005599 keep = ccline.cmdbuff[ccline.cmdlen];
5600 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005601 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005602
Bram Moolenaarffa96842018-06-12 22:05:14 +02005603 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5604
5605 args[0].v_type = VAR_STRING;
5606 args[0].vval.v_string = pat;
5607 args[1].v_type = VAR_STRING;
5608 args[1].vval.v_string = xp->xp_line;
5609 args[2].v_type = VAR_NUMBER;
5610 args[2].vval.v_number = xp->xp_col;
5611 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005613 current_sctx = xp->xp_script_ctx;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005614
Bram Moolenaarded27a12018-08-01 19:06:03 +02005615 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005616
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02005617 current_sctx = save_current_sctx;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005618 if (ccline.cmdbuff != NULL)
5619 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005620
Bram Moolenaarffa96842018-06-12 22:05:14 +02005621 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005622 return ret;
5623}
5624
5625/*
5626 * Expand names with a function defined by the user.
5627 */
5628 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005629ExpandUserDefined(
5630 expand_T *xp,
5631 regmatch_T *regmatch,
5632 int *num_file,
5633 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005634{
5635 char_u *retstr;
5636 char_u *s;
5637 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005638 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005639 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005640 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005641
5642 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5643 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 return FAIL;
5645
5646 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005647 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 e = vim_strchr(s, '\n');
5650 if (e == NULL)
5651 e = s + STRLEN(s);
5652 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005653 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005655 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5656 *e = keep;
5657
5658 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005660 if (ga_grow(&ga, 1) == FAIL)
5661 break;
5662 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5663 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 }
5665
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 if (*e != NUL)
5667 ++e;
5668 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005669 vim_free(retstr);
5670 *file = ga.ga_data;
5671 *num_file = ga.ga_len;
5672 return OK;
5673}
5674
5675/*
5676 * Expand names with a list returned by a function defined by the user.
5677 */
5678 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005679ExpandUserList(
5680 expand_T *xp,
5681 int *num_file,
5682 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005683{
5684 list_T *retlist;
5685 listitem_T *li;
5686 garray_T ga;
5687
5688 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5689 if (retlist == NULL)
5690 return FAIL;
5691
5692 ga_init2(&ga, (int)sizeof(char *), 3);
5693 /* Loop over the items in the list. */
5694 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5695 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005696 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5697 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005698
5699 if (ga_grow(&ga, 1) == FAIL)
5700 break;
5701
5702 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005703 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005704 ++ga.ga_len;
5705 }
5706 list_unref(retlist);
5707
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 *file = ga.ga_data;
5709 *num_file = ga.ga_len;
5710 return OK;
5711}
5712#endif
5713
5714/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005715 * Expand color scheme, compiler or filetype names.
5716 * Search from 'runtimepath':
5717 * 'runtimepath'/{dirnames}/{pat}.vim
5718 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5719 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5720 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5721 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005722 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 */
5724 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005725ExpandRTDir(
5726 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005727 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005728 int *num_file,
5729 char_u ***file,
5730 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 char_u *s;
5733 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005734 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005736 int i;
5737 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738
5739 *num_file = 0;
5740 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005741 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005742 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005744 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005745 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005746 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5747 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005749 ga_clear_strings(&ga);
5750 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005751 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005752 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005753 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005754 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005756
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005757 if (flags & DIP_START) {
5758 for (i = 0; dirnames[i] != NULL; ++i)
5759 {
5760 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5761 if (s == NULL)
5762 {
5763 ga_clear_strings(&ga);
5764 return FAIL;
5765 }
5766 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5767 globpath(p_pp, s, &ga, 0);
5768 vim_free(s);
5769 }
5770 }
5771
5772 if (flags & DIP_OPT) {
5773 for (i = 0; dirnames[i] != NULL; ++i)
5774 {
5775 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5776 if (s == NULL)
5777 {
5778 ga_clear_strings(&ga);
5779 return FAIL;
5780 }
5781 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5782 globpath(p_pp, s, &ga, 0);
5783 vim_free(s);
5784 }
5785 }
5786
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005787 for (i = 0; i < ga.ga_len; ++i)
5788 {
5789 match = ((char_u **)ga.ga_data)[i];
5790 s = match;
5791 e = s + STRLEN(s);
5792 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5793 {
5794 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005795 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005796 if (s < match || vim_ispathsep(*s))
5797 break;
5798 ++s;
5799 *e = NUL;
5800 mch_memmove(match, s, e - s + 1);
5801 }
5802 }
5803
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005804 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005805 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005806
5807 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005808 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005809 remove_duplicates(&ga);
5810
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 *file = ga.ga_data;
5812 *num_file = ga.ga_len;
5813 return OK;
5814}
5815
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005816/*
5817 * Expand loadplugin names:
5818 * 'packpath'/pack/ * /opt/{pat}
5819 */
5820 static int
5821ExpandPackAddDir(
5822 char_u *pat,
5823 int *num_file,
5824 char_u ***file)
5825{
5826 char_u *s;
5827 char_u *e;
5828 char_u *match;
5829 garray_T ga;
5830 int i;
5831 int pat_len;
5832
5833 *num_file = 0;
5834 *file = NULL;
5835 pat_len = (int)STRLEN(pat);
5836 ga_init2(&ga, (int)sizeof(char *), 10);
5837
5838 s = alloc((unsigned)(pat_len + 26));
5839 if (s == NULL)
5840 {
5841 ga_clear_strings(&ga);
5842 return FAIL;
5843 }
5844 sprintf((char *)s, "pack/*/opt/%s*", pat);
5845 globpath(p_pp, s, &ga, 0);
5846 vim_free(s);
5847
5848 for (i = 0; i < ga.ga_len; ++i)
5849 {
5850 match = ((char_u **)ga.ga_data)[i];
5851 s = gettail(match);
5852 e = s + STRLEN(s);
5853 mch_memmove(match, s, e - s + 1);
5854 }
5855
5856 if (ga.ga_len == 0)
5857 return FAIL;
5858
5859 /* Sort and remove duplicates which can happen when specifying multiple
5860 * directories in dirnames. */
5861 remove_duplicates(&ga);
5862
5863 *file = ga.ga_data;
5864 *num_file = ga.ga_len;
5865 return OK;
5866}
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868#endif
5869
5870#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5871/*
5872 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005873 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005875 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005876globpath(
5877 char_u *path,
5878 char_u *file,
5879 garray_T *ga,
5880 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881{
5882 expand_T xpc;
5883 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 int num_p;
5886 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005887
5888 buf = alloc(MAXPATHL);
5889 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005890 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005892 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005894
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 /* Loop over all entries in {path}. */
5896 while (*path != NUL)
5897 {
5898 /* Copy one item of the path to buf[] and concatenate the file name. */
5899 copy_option_part(&path, buf, MAXPATHL, ",");
5900 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5901 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005902# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005903 /* Using the platform's path separator (\) makes vim incorrectly
5904 * treat it as an escape character, use '/' instead. */
5905 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5906 STRCAT(buf, "/");
5907# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005909# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005911 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5912 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005914 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005916 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918 for (i = 0; i < num_p; ++i)
5919 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005920 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005921 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005922 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005925
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 FreeWild(num_p, p);
5927 }
5928 }
5929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930
5931 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932}
5933
5934#endif
5935
5936#if defined(FEAT_CMDHIST) || defined(PROTO)
5937
5938/*********************************
5939 * Command line history stuff *
5940 *********************************/
5941
5942/*
5943 * Translate a history character to the associated type number.
5944 */
5945 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005946hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947{
5948 if (c == ':')
5949 return HIST_CMD;
5950 if (c == '=')
5951 return HIST_EXPR;
5952 if (c == '@')
5953 return HIST_INPUT;
5954 if (c == '>')
5955 return HIST_DEBUG;
5956 return HIST_SEARCH; /* must be '?' or '/' */
5957}
5958
5959/*
5960 * Table of history names.
5961 * These names are used in :history and various hist...() functions.
5962 * It is sufficient to give the significant prefix of a history name.
5963 */
5964
5965static char *(history_names[]) =
5966{
5967 "cmd",
5968 "search",
5969 "expr",
5970 "input",
5971 "debug",
5972 NULL
5973};
5974
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005975#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5976/*
5977 * Function given to ExpandGeneric() to obtain the possible first
5978 * arguments of the ":history command.
5979 */
5980 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005981get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005982{
5983 static char_u compl[2] = { NUL, NUL };
5984 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005985 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005986 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5987
5988 if (idx < short_names_count)
5989 {
5990 compl[0] = (char_u)short_names[idx];
5991 return compl;
5992 }
5993 if (idx < short_names_count + history_name_count)
5994 return (char_u *)history_names[idx - short_names_count];
5995 if (idx == short_names_count + history_name_count)
5996 return (char_u *)"all";
5997 return NULL;
5998}
5999#endif
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001/*
6002 * init_history() - Initialize the command line history.
6003 * Also used to re-allocate the history when the size changes.
6004 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00006005 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006006init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 int newlen; /* new length of history table */
6009 histentry_T *temp;
6010 int i;
6011 int j;
6012 int type;
6013
6014 /*
6015 * If size of history table changed, reallocate it
6016 */
6017 newlen = (int)p_hi;
6018 if (newlen != hislen) /* history length changed */
6019 {
6020 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
6021 {
6022 if (newlen)
6023 {
6024 temp = (histentry_T *)lalloc(
6025 (long_u)(newlen * sizeof(histentry_T)), TRUE);
6026 if (temp == NULL) /* out of memory! */
6027 {
6028 if (type == 0) /* first one: just keep the old length */
6029 {
6030 newlen = hislen;
6031 break;
6032 }
6033 /* Already changed one table, now we can only have zero
6034 * length for all tables. */
6035 newlen = 0;
6036 type = -1;
6037 continue;
6038 }
6039 }
6040 else
6041 temp = NULL;
6042 if (newlen == 0 || temp != NULL)
6043 {
6044 if (hisidx[type] < 0) /* there are no entries yet */
6045 {
6046 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006047 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 }
6049 else if (newlen > hislen) /* array becomes bigger */
6050 {
6051 for (i = 0; i <= hisidx[type]; ++i)
6052 temp[i] = history[type][i];
6053 j = i;
6054 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006055 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 for ( ; j < hislen; ++i, ++j)
6057 temp[i] = history[type][j];
6058 }
6059 else /* array becomes smaller or 0 */
6060 {
6061 j = hisidx[type];
6062 for (i = newlen - 1; ; --i)
6063 {
6064 if (i >= 0) /* copy newest entries */
6065 temp[i] = history[type][j];
6066 else /* remove older entries */
6067 vim_free(history[type][j].hisstr);
6068 if (--j < 0)
6069 j = hislen - 1;
6070 if (j == hisidx[type])
6071 break;
6072 }
6073 hisidx[type] = newlen - 1;
6074 }
6075 vim_free(history[type]);
6076 history[type] = temp;
6077 }
6078 }
6079 hislen = newlen;
6080 }
6081}
6082
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006083 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006084clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006085{
6086 hisptr->hisnum = 0;
6087 hisptr->viminfo = FALSE;
6088 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006089 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006090}
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092/*
6093 * Check if command line 'str' is already in history.
6094 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6095 */
6096 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006097in_history(
6098 int type,
6099 char_u *str,
6100 int move_to_front, /* Move the entry to the front if it exists */
6101 int sep,
6102 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
6104 int i;
6105 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006106 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006107
6108 if (hisidx[type] < 0)
6109 return FALSE;
6110 i = hisidx[type];
6111 do
6112 {
6113 if (history[type][i].hisstr == NULL)
6114 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006115
6116 /* For search history, check that the separator character matches as
6117 * well. */
6118 p = history[type][i].hisstr;
6119 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006120 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006121 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122 {
6123 if (!move_to_front)
6124 return TRUE;
6125 last_i = i;
6126 break;
6127 }
6128 if (--i < 0)
6129 i = hislen - 1;
6130 } while (i != hisidx[type]);
6131
6132 if (last_i >= 0)
6133 {
6134 str = history[type][i].hisstr;
6135 while (i != hisidx[type])
6136 {
6137 if (++i >= hislen)
6138 i = 0;
6139 history[type][last_i] = history[type][i];
6140 last_i = i;
6141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006143 history[type][i].viminfo = FALSE;
6144 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006145 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 return TRUE;
6147 }
6148 return FALSE;
6149}
6150
6151/*
6152 * Convert history name (from table above) to its HIST_ equivalent.
6153 * When "name" is empty, return "cmd" history.
6154 * Returns -1 for unknown history name.
6155 */
6156 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006157get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 int i;
6160 int len = (int)STRLEN(name);
6161
6162 /* No argument: use current history. */
6163 if (len == 0)
6164 return hist_char2type(ccline.cmdfirstc);
6165
6166 for (i = 0; history_names[i] != NULL; ++i)
6167 if (STRNICMP(name, history_names[i], len) == 0)
6168 return i;
6169
6170 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6171 return hist_char2type(name[0]);
6172
6173 return -1;
6174}
6175
6176static int last_maptick = -1; /* last seen maptick */
6177
6178/*
6179 * Add the given string to the given history. If the string is already in the
6180 * history then it is moved to the front. "histype" may be one of he HIST_
6181 * values.
6182 */
6183 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006184add_to_history(
6185 int histype,
6186 char_u *new_entry,
6187 int in_map, /* consider maptick when inside a mapping */
6188 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189{
6190 histentry_T *hisptr;
6191 int len;
6192
6193 if (hislen == 0) /* no history */
6194 return;
6195
Bram Moolenaara939e432013-11-09 05:30:26 +01006196 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6197 return;
6198
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 /*
6200 * Searches inside the same mapping overwrite each other, so that only
6201 * the last line is kept. Be careful not to remove a line that was moved
6202 * down, only lines that were added.
6203 */
6204 if (histype == HIST_SEARCH && in_map)
6205 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006206 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 {
6208 /* Current line is from the same mapping, remove it */
6209 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6210 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006211 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212 --hisnum[histype];
6213 if (--hisidx[HIST_SEARCH] < 0)
6214 hisidx[HIST_SEARCH] = hislen - 1;
6215 }
6216 last_maptick = -1;
6217 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006218 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 {
6220 if (++hisidx[histype] == hislen)
6221 hisidx[histype] = 0;
6222 hisptr = &history[histype][hisidx[histype]];
6223 vim_free(hisptr->hisstr);
6224
6225 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006226 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6228 if (hisptr->hisstr != NULL)
6229 hisptr->hisstr[len + 1] = sep;
6230
6231 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006232 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006233 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (histype == HIST_SEARCH && in_map)
6235 last_maptick = maptick;
6236 }
6237}
6238
6239#if defined(FEAT_EVAL) || defined(PROTO)
6240
6241/*
6242 * Get identifier of newest history entry.
6243 * "histype" may be one of the HIST_ values.
6244 */
6245 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006246get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247{
6248 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6249 || hisidx[histype] < 0)
6250 return -1;
6251
6252 return history[histype][hisidx[histype]].hisnum;
6253}
6254
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006255/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006256 * Calculate history index from a number:
6257 * num > 0: seen as identifying number of a history entry
6258 * num < 0: relative position in history wrt newest entry
6259 * "histype" may be one of the HIST_ values.
6260 */
6261 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006262calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 int i;
6265 histentry_T *hist;
6266 int wrapped = FALSE;
6267
6268 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6269 || (i = hisidx[histype]) < 0 || num == 0)
6270 return -1;
6271
6272 hist = history[histype];
6273 if (num > 0)
6274 {
6275 while (hist[i].hisnum > num)
6276 if (--i < 0)
6277 {
6278 if (wrapped)
6279 break;
6280 i += hislen;
6281 wrapped = TRUE;
6282 }
6283 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6284 return i;
6285 }
6286 else if (-num <= hislen)
6287 {
6288 i += num + 1;
6289 if (i < 0)
6290 i += hislen;
6291 if (hist[i].hisstr != NULL)
6292 return i;
6293 }
6294 return -1;
6295}
6296
6297/*
6298 * Get a history entry by its index.
6299 * "histype" may be one of the HIST_ values.
6300 */
6301 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006302get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303{
6304 idx = calc_hist_idx(histype, idx);
6305 if (idx >= 0)
6306 return history[histype][idx].hisstr;
6307 else
6308 return (char_u *)"";
6309}
6310
6311/*
6312 * Clear all entries of a history.
6313 * "histype" may be one of the HIST_ values.
6314 */
6315 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006316clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317{
6318 int i;
6319 histentry_T *hisptr;
6320
6321 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6322 {
6323 hisptr = history[histype];
6324 for (i = hislen; i--;)
6325 {
6326 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006327 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006328 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329 }
6330 hisidx[histype] = -1; /* mark history as cleared */
6331 hisnum[histype] = 0; /* reset identifier counter */
6332 return OK;
6333 }
6334 return FAIL;
6335}
6336
6337/*
6338 * Remove all entries matching {str} from a history.
6339 * "histype" may be one of the HIST_ values.
6340 */
6341 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006342del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343{
6344 regmatch_T regmatch;
6345 histentry_T *hisptr;
6346 int idx;
6347 int i;
6348 int last;
6349 int found = FALSE;
6350
6351 regmatch.regprog = NULL;
6352 regmatch.rm_ic = FALSE; /* always match case */
6353 if (hislen != 0
6354 && histype >= 0
6355 && histype < HIST_COUNT
6356 && *str != NUL
6357 && (idx = hisidx[histype]) >= 0
6358 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6359 != NULL)
6360 {
6361 i = last = idx;
6362 do
6363 {
6364 hisptr = &history[histype][i];
6365 if (hisptr->hisstr == NULL)
6366 break;
6367 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6368 {
6369 found = TRUE;
6370 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006371 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 }
6373 else
6374 {
6375 if (i != last)
6376 {
6377 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006378 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 }
6380 if (--last < 0)
6381 last += hislen;
6382 }
6383 if (--i < 0)
6384 i += hislen;
6385 } while (i != idx);
6386 if (history[histype][idx].hisstr == NULL)
6387 hisidx[histype] = -1;
6388 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006389 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 return found;
6391}
6392
6393/*
6394 * Remove an indexed entry from a history.
6395 * "histype" may be one of the HIST_ values.
6396 */
6397 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006398del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399{
6400 int i, j;
6401
6402 i = calc_hist_idx(histype, idx);
6403 if (i < 0)
6404 return FALSE;
6405 idx = hisidx[histype];
6406 vim_free(history[histype][i].hisstr);
6407
6408 /* When deleting the last added search string in a mapping, reset
6409 * last_maptick, so that the last added search string isn't deleted again.
6410 */
6411 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6412 last_maptick = -1;
6413
6414 while (i != idx)
6415 {
6416 j = (i + 1) % hislen;
6417 history[histype][i] = history[histype][j];
6418 i = j;
6419 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006420 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (--i < 0)
6422 i += hislen;
6423 hisidx[histype] = i;
6424 return TRUE;
6425}
6426
6427#endif /* FEAT_EVAL */
6428
6429#if defined(FEAT_CRYPT) || defined(PROTO)
6430/*
6431 * Very specific function to remove the value in ":set key=val" from the
6432 * history.
6433 */
6434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006435remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 char_u *p;
6438 int i;
6439
6440 i = hisidx[HIST_CMD];
6441 if (i < 0)
6442 return;
6443 p = history[HIST_CMD][i].hisstr;
6444 if (p != NULL)
6445 for ( ; *p; ++p)
6446 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6447 {
6448 p = vim_strchr(p + 3, '=');
6449 if (p == NULL)
6450 break;
6451 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006452 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 if (p[i] == '\\' && p[i + 1])
6454 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006455 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006456 --p;
6457 }
6458}
6459#endif
6460
6461#endif /* FEAT_CMDHIST */
6462
Bram Moolenaar064154c2016-03-19 22:50:43 +01006463#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006464/*
Bram Moolenaar438d1762018-09-30 17:11:48 +02006465 * Get pointer to the command line info to use. save_ccline() may clear
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006466 * ccline and put the previous value in prev_ccline.
6467 */
6468 static struct cmdline_info *
6469get_ccline_ptr(void)
6470{
6471 if ((State & CMDLINE) == 0)
6472 return NULL;
6473 if (ccline.cmdbuff != NULL)
6474 return &ccline;
6475 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6476 return &prev_ccline;
6477 return NULL;
6478}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006479#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006480
Bram Moolenaar064154c2016-03-19 22:50:43 +01006481#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006482/*
6483 * Get the current command line in allocated memory.
6484 * Only works when the command line is being edited.
6485 * Returns NULL when something is wrong.
6486 */
6487 char_u *
6488get_cmdline_str(void)
6489{
Bram Moolenaaree91c332018-09-25 22:27:35 +02006490 struct cmdline_info *p;
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006491
Bram Moolenaaree91c332018-09-25 22:27:35 +02006492 if (cmdline_star > 0)
6493 return NULL;
6494 p = get_ccline_ptr();
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006495 if (p == NULL)
6496 return NULL;
6497 return vim_strnsave(p->cmdbuff, p->cmdlen);
6498}
6499
6500/*
6501 * Get the current command line position, counted in bytes.
6502 * Zero is the first position.
6503 * Only works when the command line is being edited.
6504 * Returns -1 when something is wrong.
6505 */
6506 int
6507get_cmdline_pos(void)
6508{
6509 struct cmdline_info *p = get_ccline_ptr();
6510
6511 if (p == NULL)
6512 return -1;
6513 return p->cmdpos;
6514}
6515
6516/*
6517 * Set the command line byte position to "pos". Zero is the first position.
6518 * Only works when the command line is being edited.
6519 * Returns 1 when failed, 0 when OK.
6520 */
6521 int
6522set_cmdline_pos(
6523 int pos)
6524{
6525 struct cmdline_info *p = get_ccline_ptr();
6526
6527 if (p == NULL)
6528 return 1;
6529
6530 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6531 * changed the command line. */
6532 if (pos < 0)
6533 new_cmdpos = 0;
6534 else
6535 new_cmdpos = pos;
6536 return 0;
6537}
6538#endif
6539
6540#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6541/*
6542 * Get the current command-line type.
6543 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6544 * Only works when the command line is being edited.
6545 * Returns NUL when something is wrong.
6546 */
6547 int
6548get_cmdline_type(void)
6549{
6550 struct cmdline_info *p = get_ccline_ptr();
6551
6552 if (p == NULL)
6553 return NUL;
6554 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006555 return
6556# ifdef FEAT_EVAL
6557 (p->input_fn) ? '@' :
6558# endif
6559 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006560 return p->cmdfirstc;
6561}
6562#endif
6563
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6565/*
6566 * Get indices "num1,num2" that specify a range within a list (not a range of
6567 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6568 * Returns OK if parsed successfully, otherwise FAIL.
6569 */
6570 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006571get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572{
6573 int len;
6574 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006575 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 *str = skipwhite(*str);
6578 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6579 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006580 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 *str += len;
6582 *num1 = (int)num;
6583 first = TRUE;
6584 }
6585 *str = skipwhite(*str);
6586 if (**str == ',') /* parse "to" part of range */
6587 {
6588 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006589 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 if (len > 0)
6591 {
6592 *num2 = (int)num;
6593 *str = skipwhite(*str + len);
6594 }
6595 else if (!first) /* no number given at all */
6596 return FAIL;
6597 }
6598 else if (first) /* only one number given */
6599 *num2 = *num1;
6600 return OK;
6601}
6602#endif
6603
6604#if defined(FEAT_CMDHIST) || defined(PROTO)
6605/*
6606 * :history command - print a history
6607 */
6608 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006609ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 histentry_T *hist;
6612 int histype1 = HIST_CMD;
6613 int histype2 = HIST_CMD;
6614 int hisidx1 = 1;
6615 int hisidx2 = -1;
6616 int idx;
6617 int i, j, k;
6618 char_u *end;
6619 char_u *arg = eap->arg;
6620
6621 if (hislen == 0)
6622 {
6623 MSG(_("'history' option is zero"));
6624 return;
6625 }
6626
6627 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6628 {
6629 end = arg;
6630 while (ASCII_ISALPHA(*end)
6631 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6632 end++;
6633 i = *end;
6634 *end = NUL;
6635 histype1 = get_histtype(arg);
6636 if (histype1 == -1)
6637 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006638 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 {
6640 histype1 = 0;
6641 histype2 = HIST_COUNT-1;
6642 }
6643 else
6644 {
6645 *end = i;
6646 EMSG(_(e_trailing));
6647 return;
6648 }
6649 }
6650 else
6651 histype2 = histype1;
6652 *end = i;
6653 }
6654 else
6655 end = arg;
6656 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6657 {
6658 EMSG(_(e_trailing));
6659 return;
6660 }
6661
6662 for (; !got_int && histype1 <= histype2; ++histype1)
6663 {
6664 STRCPY(IObuff, "\n # ");
6665 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6666 MSG_PUTS_TITLE(IObuff);
6667 idx = hisidx[histype1];
6668 hist = history[histype1];
6669 j = hisidx1;
6670 k = hisidx2;
6671 if (j < 0)
6672 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6673 if (k < 0)
6674 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6675 if (idx >= 0 && j <= k)
6676 for (i = idx + 1; !got_int; ++i)
6677 {
6678 if (i == hislen)
6679 i = 0;
6680 if (hist[i].hisstr != NULL
6681 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6682 {
6683 msg_putchar('\n');
6684 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6685 hist[i].hisnum);
6686 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6687 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006688 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 else
6690 STRCAT(IObuff, hist[i].hisstr);
6691 msg_outtrans(IObuff);
6692 out_flush();
6693 }
6694 if (i == idx)
6695 break;
6696 }
6697 }
6698}
6699#endif
6700
6701#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006702/*
6703 * Buffers for history read from a viminfo file. Only valid while reading.
6704 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006705static histentry_T *viminfo_history[HIST_COUNT] =
6706 {NULL, NULL, NULL, NULL, NULL};
6707static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6708static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709static int viminfo_add_at_front = FALSE;
6710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
6712 * Translate a history type number to the associated character.
6713 */
6714 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006715hist_type2char(
6716 int type,
6717 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718{
6719 if (type == HIST_CMD)
6720 return ':';
6721 if (type == HIST_SEARCH)
6722 {
6723 if (use_question)
6724 return '?';
6725 else
6726 return '/';
6727 }
6728 if (type == HIST_EXPR)
6729 return '=';
6730 return '@';
6731}
6732
6733/*
6734 * Prepare for reading the history from the viminfo file.
6735 * This allocates history arrays to store the read history lines.
6736 */
6737 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006738prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739{
6740 int i;
6741 int num;
6742 int type;
6743 int len;
6744
6745 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006746 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 if (asklen > hislen)
6748 asklen = hislen;
6749
6750 for (type = 0; type < HIST_COUNT; ++type)
6751 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006752 /* Count the number of empty spaces in the history list. Entries read
6753 * from viminfo previously are also considered empty. If there are
6754 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006756 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757 num++;
6758 len = asklen;
6759 if (num > len)
6760 len = num;
6761 if (len <= 0)
6762 viminfo_history[type] = NULL;
6763 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006764 viminfo_history[type] = (histentry_T *)lalloc(
6765 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 if (viminfo_history[type] == NULL)
6767 len = 0;
6768 viminfo_hislen[type] = len;
6769 viminfo_hisidx[type] = 0;
6770 }
6771}
6772
6773/*
6774 * Accept a line from the viminfo, store it in the history array when it's
6775 * new.
6776 */
6777 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006778read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 int type;
6781 long_u len;
6782 char_u *val;
6783 char_u *p;
6784
6785 type = hist_char2type(virp->vir_line[0]);
6786 if (viminfo_hisidx[type] < viminfo_hislen[type])
6787 {
6788 val = viminfo_readstring(virp, 1, TRUE);
6789 if (val != NULL && *val != NUL)
6790 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006791 int sep = (*val == ' ' ? NUL : *val);
6792
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006794 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 {
6796 /* Need to re-allocate to append the separator byte. */
6797 len = STRLEN(val);
6798 p = lalloc(len + 2, TRUE);
6799 if (p != NULL)
6800 {
6801 if (type == HIST_SEARCH)
6802 {
6803 /* Search entry: Move the separator from the first
6804 * column to after the NUL. */
6805 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006806 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808 else
6809 {
6810 /* Not a search entry: No separator in the viminfo
6811 * file, add a NUL separator. */
6812 mch_memmove(p, val, (size_t)len + 1);
6813 p[len + 1] = NUL;
6814 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006815 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6816 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006817 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6818 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006819 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006820 }
6821 }
6822 }
6823 vim_free(val);
6824 }
6825 return viminfo_readline(virp);
6826}
6827
Bram Moolenaar07219f92013-04-14 23:19:36 +02006828/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006829 * Accept a new style history line from the viminfo, store it in the history
6830 * array when it's new.
6831 */
6832 void
6833handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006834 garray_T *values,
6835 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006836{
6837 int type;
6838 long_u len;
6839 char_u *val;
6840 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006841 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842
6843 /* Check the format:
6844 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006845 if (values->ga_len < 4
6846 || vp[0].bv_type != BVAL_NR
6847 || vp[1].bv_type != BVAL_NR
6848 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6849 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006850 return;
6851
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006852 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006853 if (type >= HIST_COUNT)
6854 return;
6855 if (viminfo_hisidx[type] < viminfo_hislen[type])
6856 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006857 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006858 if (val != NULL && *val != NUL)
6859 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006860 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6861 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006862 int idx;
6863 int overwrite = FALSE;
6864
6865 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6866 {
6867 /* If lines were written by an older Vim we need to avoid
6868 * getting duplicates. See if the entry already exists. */
6869 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6870 {
6871 p = viminfo_history[type][idx].hisstr;
6872 if (STRCMP(val, p) == 0
6873 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6874 {
6875 overwrite = TRUE;
6876 break;
6877 }
6878 }
6879
6880 if (!overwrite)
6881 {
6882 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006883 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006884 p = lalloc(len + 2, TRUE);
6885 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006886 else
6887 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006888 if (p != NULL)
6889 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006890 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006891 if (!overwrite)
6892 {
6893 mch_memmove(p, val, (size_t)len + 1);
6894 /* Put the separator after the NUL. */
6895 p[len + 1] = sep;
6896 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006897 viminfo_history[type][idx].hisnum = 0;
6898 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006899 viminfo_hisidx[type]++;
6900 }
6901 }
6902 }
6903 }
6904 }
6905}
6906
6907/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006908 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006909 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006910 static void
6911concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006912{
6913 int idx;
6914 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006915
6916 idx = hisidx[type] + viminfo_hisidx[type];
6917 if (idx >= hislen)
6918 idx -= hislen;
6919 else if (idx < 0)
6920 idx = hislen - 1;
6921 if (viminfo_add_at_front)
6922 hisidx[type] = idx;
6923 else
6924 {
6925 if (hisidx[type] == -1)
6926 hisidx[type] = hislen - 1;
6927 do
6928 {
6929 if (history[type][idx].hisstr != NULL
6930 || history[type][idx].viminfo)
6931 break;
6932 if (++idx == hislen)
6933 idx = 0;
6934 } while (idx != hisidx[type]);
6935 if (idx != hisidx[type] && --idx < 0)
6936 idx = hislen - 1;
6937 }
6938 for (i = 0; i < viminfo_hisidx[type]; i++)
6939 {
6940 vim_free(history[type][idx].hisstr);
6941 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6942 history[type][idx].viminfo = TRUE;
6943 history[type][idx].time_set = viminfo_history[type][i].time_set;
6944 if (--idx < 0)
6945 idx = hislen - 1;
6946 }
6947 idx += 1;
6948 idx %= hislen;
6949 for (i = 0; i < viminfo_hisidx[type]; i++)
6950 {
6951 history[type][idx++].hisnum = ++hisnum[type];
6952 idx %= hislen;
6953 }
6954}
6955
6956#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6957 static int
6958#ifdef __BORLANDC__
6959_RTLENTRYF
6960#endif
6961sort_hist(const void *s1, const void *s2)
6962{
6963 histentry_T *p1 = *(histentry_T **)s1;
6964 histentry_T *p2 = *(histentry_T **)s2;
6965
6966 if (p1->time_set < p2->time_set) return -1;
6967 if (p1->time_set > p2->time_set) return 1;
6968 return 0;
6969}
6970#endif
6971
6972/*
6973 * Merge history lines from viminfo and lines typed in this Vim based on the
6974 * timestamp;
6975 */
6976 static void
6977merge_history(int type)
6978{
6979 int max_len;
6980 histentry_T **tot_hist;
6981 histentry_T *new_hist;
6982 int i;
6983 int len;
6984
6985 /* Make one long list with all entries. */
6986 max_len = hislen + viminfo_hisidx[type];
6987 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6988 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6989 if (tot_hist == NULL || new_hist == NULL)
6990 {
6991 vim_free(tot_hist);
6992 vim_free(new_hist);
6993 return;
6994 }
6995 for (i = 0; i < viminfo_hisidx[type]; i++)
6996 tot_hist[i] = &viminfo_history[type][i];
6997 len = i;
6998 for (i = 0; i < hislen; i++)
6999 if (history[type][i].hisstr != NULL)
7000 tot_hist[len++] = &history[type][i];
7001
7002 /* Sort the list on timestamp. */
7003 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
7004
7005 /* Keep the newest ones. */
7006 for (i = 0; i < hislen; i++)
7007 {
7008 if (i < len)
7009 {
7010 new_hist[i] = *tot_hist[i];
7011 tot_hist[i]->hisstr = NULL;
7012 if (new_hist[i].hisnum == 0)
7013 new_hist[i].hisnum = ++hisnum[type];
7014 }
7015 else
7016 clear_hist_entry(&new_hist[i]);
7017 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02007018 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007019
7020 /* Free what is not kept. */
7021 for (i = 0; i < viminfo_hisidx[type]; i++)
7022 vim_free(viminfo_history[type][i].hisstr);
7023 for (i = 0; i < hislen; i++)
7024 vim_free(history[type][i].hisstr);
7025 vim_free(history[type]);
7026 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02007027 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007028}
7029
7030/*
7031 * Finish reading history lines from viminfo. Not used when writing viminfo.
7032 */
7033 void
7034finish_viminfo_history(vir_T *virp)
7035{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007037 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038
7039 for (type = 0; type < HIST_COUNT; ++type)
7040 {
7041 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007042 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007043
7044 if (merge)
7045 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007047 concat_history(type);
7048
Bram Moolenaard23a8232018-02-10 18:45:26 +01007049 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007050 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 }
7052}
7053
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007054/*
7055 * Write history to viminfo file in "fp".
7056 * When "merge" is TRUE merge history lines with a previously read viminfo
7057 * file, data is in viminfo_history[].
7058 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007061write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062{
7063 int i;
7064 int type;
7065 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007066 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
7068 init_history();
7069 if (hislen == 0)
7070 return;
7071 for (type = 0; type < HIST_COUNT; ++type)
7072 {
7073 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7074 if (num_saved == 0)
7075 continue;
7076 if (num_saved < 0) /* Use default */
7077 num_saved = hislen;
7078 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7079 type == HIST_CMD ? _("Command Line") :
7080 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007081 type == HIST_EXPR ? _("Expression") :
7082 type == HIST_INPUT ? _("Input Line") :
7083 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084 if (num_saved > hislen)
7085 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007086
7087 /*
7088 * Merge typed and viminfo history:
7089 * round 1: history of typed commands.
7090 * round 2: history from recently read viminfo.
7091 */
7092 for (round = 1; round <= 2; ++round)
7093 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007094 if (round == 1)
7095 /* start at newest entry, somewhere in the list */
7096 i = hisidx[type];
7097 else if (viminfo_hisidx[type] > 0)
7098 /* start at newest entry, first in the list */
7099 i = 0;
7100 else
7101 /* empty list */
7102 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007103 if (i >= 0)
7104 while (num_saved > 0
7105 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007107 char_u *p;
7108 time_t timestamp;
7109 int c = NUL;
7110
7111 if (round == 1)
7112 {
7113 p = history[type][i].hisstr;
7114 timestamp = history[type][i].time_set;
7115 }
7116 else
7117 {
7118 p = viminfo_history[type] == NULL ? NULL
7119 : viminfo_history[type][i].hisstr;
7120 timestamp = viminfo_history[type] == NULL ? 0
7121 : viminfo_history[type][i].time_set;
7122 }
7123
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007124 if (p != NULL && (round == 2
7125 || !merge
7126 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007128 --num_saved;
7129 fputc(hist_type2char(type, TRUE), fp);
7130 /* For the search history: put the separator in the
7131 * second column; use a space if there isn't one. */
7132 if (type == HIST_SEARCH)
7133 {
7134 c = p[STRLEN(p) + 1];
7135 putc(c == NUL ? ' ' : c, fp);
7136 }
7137 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007138
7139 {
7140 char cbuf[NUMBUFLEN];
7141
7142 /* New style history with a bar line. Format:
7143 * |{bartype},{histtype},{timestamp},{separator},"text" */
7144 if (c == NUL)
7145 cbuf[0] = NUL;
7146 else
7147 sprintf(cbuf, "%d", c);
7148 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7149 type, (long)timestamp, cbuf);
7150 barline_writestring(fp, p, LSIZE - 20);
7151 putc('\n', fp);
7152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007154 if (round == 1)
7155 {
7156 /* Decrement index, loop around and stop when back at
7157 * the start. */
7158 if (--i < 0)
7159 i = hislen - 1;
7160 if (i == hisidx[type])
7161 break;
7162 }
7163 else
7164 {
7165 /* Increment index. Stop at the end in the while. */
7166 ++i;
7167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007169 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007170 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007171 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007172 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007173 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007174 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 }
7176}
7177#endif /* FEAT_VIMINFO */
7178
7179#if defined(FEAT_FKMAP) || defined(PROTO)
7180/*
7181 * Write a character at the current cursor+offset position.
7182 * It is directly written into the command buffer block.
7183 */
7184 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007185cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7188 {
7189 EMSG(_("E198: cmd_pchar beyond the command length"));
7190 return;
7191 }
7192 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7193 ccline.cmdbuff[ccline.cmdlen] = NUL;
7194}
7195
7196 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007197cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198{
7199 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7200 {
7201 /* EMSG(_("cmd_gchar beyond the command length")); */
7202 return NUL;
7203 }
7204 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7205}
7206#endif
7207
7208#if defined(FEAT_CMDWIN) || defined(PROTO)
7209/*
7210 * Open a window on the current command line and history. Allow editing in
7211 * the window. Returns when the window is closed.
7212 * Returns:
7213 * CR if the command is to be executed
7214 * Ctrl_C if it is to be abandoned
7215 * K_IGNORE if editing continues
7216 */
7217 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007218open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219{
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007220 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007222 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 win_T *wp;
7224 int i;
7225 linenr_T lnum;
7226 int histtype;
7227 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 int save_restart_edit = restart_edit;
7229 int save_State = State;
7230 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007231#ifdef FEAT_RIGHTLEFT
7232 int save_cmdmsg_rl = cmdmsg_rl;
7233#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007234#ifdef FEAT_FOLDING
7235 int save_KeyTyped;
7236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237
7238 /* Can't do this recursively. Can't do it when typing a password. */
7239 if (cmdwin_type != 0
7240# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7241 || cmdline_star > 0
7242# endif
7243 )
7244 {
7245 beep_flush();
7246 return K_IGNORE;
7247 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007248 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249
7250 /* Save current window sizes. */
7251 win_size_save(&winsizes);
7252
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007254 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007255
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007256 /* don't use a new tab page */
7257 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007258 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007259
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 /* Create a window for the command-line buffer. */
7261 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7262 {
7263 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007264 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 return K_IGNORE;
7266 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007267 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007268
7269 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007270 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007271 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007274#ifdef FEAT_FOLDING
7275 curwin->w_p_fen = FALSE;
7276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007278 curwin->w_p_rl = cmdmsg_rl;
7279 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007281 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007284 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007285 /* But don't allow switching to another buffer. */
7286 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287
Bram Moolenaar46152342005-09-07 21:18:43 +00007288 /* Showing the prompt may have set need_wait_return, reset it. */
7289 need_wait_return = FALSE;
7290
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007291 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7293 {
7294 if (p_wc == TAB)
7295 {
7296 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7297 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7298 }
7299 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7300 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007301 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007303 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7304 * sets 'textwidth' to 78). */
7305 curbuf->b_p_tw = 0;
7306
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307 /* Fill the buffer with the history. */
7308 init_history();
7309 if (hislen > 0)
7310 {
7311 i = hisidx[histtype];
7312 if (i >= 0)
7313 {
7314 lnum = 0;
7315 do
7316 {
7317 if (++i == hislen)
7318 i = 0;
7319 if (history[histtype][i].hisstr != NULL)
7320 ml_append(lnum++, history[histtype][i].hisstr,
7321 (colnr_T)0, FALSE);
7322 }
7323 while (i != hisidx[histtype]);
7324 }
7325 }
7326
7327 /* Replace the empty last line with the current command-line and put the
7328 * cursor there. */
7329 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7330 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7331 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007332 changed_line_abv_curs();
7333 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007334 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 /* No Ex mode here! */
7337 exmode_active = 0;
7338
7339 State = NORMAL;
7340# ifdef FEAT_MOUSE
7341 setmouse();
7342# endif
7343
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007345 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007346 if (restart_edit != 0) /* autocmd with ":startinsert" */
7347 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348
7349 i = RedrawingDisabled;
7350 RedrawingDisabled = 0;
7351
7352 /*
7353 * Call the main loop until <CR> or CTRL-C is typed.
7354 */
7355 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007356 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357
7358 RedrawingDisabled = i;
7359
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007360# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007361 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007362# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007363
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007365 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007366
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007367# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007368 /* Restore KeyTyped in case it is modified by autocommands */
7369 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370# endif
7371
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 cmdwin_type = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007373 exmode_active = save_exmode;
7374
Bram Moolenaarf9821062008-06-20 16:31:07 +00007375 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007377 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378 {
7379 cmdwin_result = Ctrl_C;
7380 EMSG(_("E199: Active window or buffer deleted"));
7381 }
7382 else
7383 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007384# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007385 /* autocmds may abort script processing */
7386 if (aborting() && cmdwin_result != K_IGNORE)
7387 cmdwin_result = Ctrl_C;
7388# endif
7389 /* Set the new command line from the cmdline buffer. */
7390 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007391 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007393 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7394
7395 if (histtype == HIST_CMD)
7396 {
7397 /* Execute the command directly. */
7398 ccline.cmdbuff = vim_strsave((char_u *)p);
7399 cmdwin_result = CAR;
7400 }
7401 else
7402 {
7403 /* First need to cancel what we were doing. */
7404 ccline.cmdbuff = NULL;
7405 stuffcharReadbuff(':');
7406 stuffReadbuff((char_u *)p);
7407 stuffcharReadbuff(CAR);
7408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409 }
7410 else if (cmdwin_result == K_XF2) /* :qa typed */
7411 {
7412 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7413 cmdwin_result = CAR;
7414 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007415 else if (cmdwin_result == Ctrl_C)
7416 {
7417 /* :q or :close, don't execute any command
7418 * and don't modify the cmd window. */
7419 ccline.cmdbuff = NULL;
7420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 else
7422 ccline.cmdbuff = vim_strsave(ml_get_curline());
7423 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007424 {
7425 ccline.cmdbuff = vim_strsave((char_u *)"");
7426 ccline.cmdlen = 0;
7427 ccline.cmdbufflen = 1;
7428 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007429 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431 else
7432 {
7433 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7434 ccline.cmdbufflen = ccline.cmdlen + 1;
7435 ccline.cmdpos = curwin->w_cursor.col;
7436 if (ccline.cmdpos > ccline.cmdlen)
7437 ccline.cmdpos = ccline.cmdlen;
7438 if (cmdwin_result == K_IGNORE)
7439 {
7440 set_cmdspos_cursor();
7441 redrawcmd();
7442 }
7443 }
7444
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007446 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007447# ifdef FEAT_CONCEAL
7448 /* Avoid command-line window first character being concealed. */
7449 curwin->w_p_cole = 0;
7450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007452 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007453 win_goto(old_curwin);
7454 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007455
7456 /* win_close() may have already wiped the buffer when 'bh' is
7457 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007458 if (bufref_valid(&bufref))
7459 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460
7461 /* Restore window sizes. */
7462 win_size_restore(&winsizes);
7463
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007464 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465 }
7466
7467 ga_clear(&winsizes);
7468 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007469# ifdef FEAT_RIGHTLEFT
7470 cmdmsg_rl = save_cmdmsg_rl;
7471# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007472
7473 State = save_State;
7474# ifdef FEAT_MOUSE
7475 setmouse();
7476# endif
7477
7478 return cmdwin_result;
7479}
7480#endif /* FEAT_CMDWIN */
7481
7482/*
7483 * Used for commands that either take a simple command string argument, or:
7484 * cmd << endmarker
7485 * {script}
7486 * endmarker
7487 * Returns a pointer to allocated memory with {script} or NULL.
7488 */
7489 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007490script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491{
7492 char_u *theline;
7493 char *end_pattern = NULL;
7494 char dot[] = ".";
7495 garray_T ga;
7496
7497 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7498 return NULL;
7499
7500 ga_init2(&ga, 1, 0x400);
7501
7502 if (cmd[2] != NUL)
7503 end_pattern = (char *)skipwhite(cmd + 2);
7504 else
7505 end_pattern = dot;
7506
7507 for (;;)
7508 {
7509 theline = eap->getline(
7510#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007511 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512#endif
7513 NUL, eap->cookie, 0);
7514
7515 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007516 {
7517 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 ga_concat(&ga, theline);
7522 ga_append(&ga, '\n');
7523 vim_free(theline);
7524 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007525 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007526
7527 return (char_u *)ga.ga_data;
7528}