blob: b0da5d80eb10bb7433b6425b67c8b1eb48f5896c [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 Moolenaard6e7cc62008-09-14 12:42:29 +000047/* 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().
50 * TODO: make it local to getcmdline() and pass it around. */
51static struct cmdline_info ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53static int cmd_showtail; /* Only show path tail in lists ? */
54
55#ifdef FEAT_EVAL
56static int new_cmdpos; /* position set by set_cmdline_pos() */
57#endif
58
Bram Moolenaara92522f2017-07-15 15:21:38 +020059static int extra_char = NUL; /* extra character to display when redrawing
60 * the command line */
Bram Moolenaar6a77d262017-07-16 15:24:01 +020061static int extra_char_shift;
62
Bram Moolenaar071d4272004-06-13 20:20:40 +000063#ifdef FEAT_CMDHIST
64typedef struct hist_entry
65{
66 int hisnum; /* identifying number */
Bram Moolenaarb3049f42013-04-05 18:58:47 +020067 int viminfo; /* when TRUE hisstr comes from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 char_u *hisstr; /* actual entry, separator char after the NUL */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +020069 time_t time_set; /* when it was typed, zero if unknown */
Bram Moolenaar071d4272004-06-13 20:20:40 +000070} histentry_T;
71
72static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL};
73static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */
74static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0};
75 /* identifying (unique) number of newest history entry */
76static int hislen = 0; /* actual length of history tables */
77
Bram Moolenaard25c16e2016-01-29 22:13:30 +010078static int hist_char2type(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +000079
Bram Moolenaard25c16e2016-01-29 22:13:30 +010080static int in_history(int, char_u *, int, int, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +000081# ifdef FEAT_EVAL
Bram Moolenaard25c16e2016-01-29 22:13:30 +010082static int calc_hist_idx(int histype, int num);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083# endif
84#endif
85
86#ifdef FEAT_RIGHTLEFT
87static int cmd_hkmap = 0; /* Hebrew mapping during command line */
88#endif
89
90#ifdef FEAT_FKMAP
91static int cmd_fkmap = 0; /* Farsi mapping during command line */
92#endif
93
Bram Moolenaard25c16e2016-01-29 22:13:30 +010094static int cmdline_charsize(int idx);
95static void set_cmdspos(void);
96static void set_cmdspos_cursor(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +000097#ifdef FEAT_MBYTE
Bram Moolenaard25c16e2016-01-29 22:13:30 +010098static void correct_cmdspos(int idx, int cells);
Bram Moolenaar071d4272004-06-13 20:20:40 +000099#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100100static void alloc_cmdbuff(int len);
101static int realloc_cmdbuff(int len);
102static void draw_cmdline(int start, int len);
103static void save_cmdline(struct cmdline_info *ccp);
104static void restore_cmdline(struct cmdline_info *ccp);
105static int cmdline_paste(int regname, int literally, int remcr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100107static void redrawcmd_preedit(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108#endif
109#ifdef FEAT_WILDMENU
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100110static void cmdline_del(int from);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100112static void redrawcmdprompt(void);
113static void cursorcmd(void);
114static int ccheck_abbr(int);
115static int nextwild(expand_T *xp, int type, int options, int escape);
116static void escape_fname(char_u **pp);
117static int showmatches(expand_T *xp, int wildmenu);
118static void set_expand_context(expand_T *xp);
119static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int);
120static int expand_showtail(expand_T *xp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121#ifdef FEAT_CMDL_COMPL
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100122static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg);
Bram Moolenaar52f9c192016-03-13 13:24:45 +0100123static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]);
Bram Moolenaar35ca0e72016-03-05 17:41:49 +0100124static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200125# ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100126static char_u *get_history_arg(expand_T *xp, int idx);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +0200127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100129static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file);
130static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131# endif
132#endif
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200133#ifdef FEAT_CMDHIST
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100134static void clear_hist_entry(histentry_T *hisptr);
Bram Moolenaar25a6df92013-04-06 14:29:00 +0200135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136
137#ifdef FEAT_CMDWIN
Bram Moolenaar3bab9392017-04-07 15:42:25 +0200138static int open_cmdwin(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139#endif
140
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200141#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
142static int
143#ifdef __BORLANDC__
144_RTLENTRYF
145#endif
Bram Moolenaard25c16e2016-01-29 22:13:30 +0100146sort_func_compare(const void *s1, const void *s2);
Bram Moolenaardb710ed2011-10-26 22:02:15 +0200147#endif
148
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200149
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200150 static void
151trigger_cmd_autocmd(int typechar, int evt)
152{
153 char_u typestr[2];
154
155 typestr[0] = typechar;
156 typestr[1] = NUL;
157 apply_autocmds(evt, typestr, typestr, FALSE, curbuf);
158}
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200161 * Abandon the command line.
162 */
163 static void
164abandon_cmdline(void)
165{
Bram Moolenaard23a8232018-02-10 18:45:26 +0100166 VIM_CLEAR(ccline.cmdbuff);
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200167 if (msg_scrolled == 0)
168 compute_cmdrow();
169 MSG("");
170 redraw_cmdline = TRUE;
171}
172
Bram Moolenaaree219b02017-12-17 14:55:01 +0100173#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarf8e8c062017-10-22 14:44:17 +0200174/*
Bram Moolenaar66216052017-12-16 16:33:44 +0100175 * Guess that the pattern matches everything. Only finds specific cases, such
176 * as a trailing \|, which can happen while typing a pattern.
177 */
178 static int
179empty_pattern(char_u *p)
180{
Bram Moolenaar200ea8f2018-01-02 15:37:46 +0100181 size_t n = STRLEN(p);
Bram Moolenaar66216052017-12-16 16:33:44 +0100182
183 /* remove trailing \v and the like */
184 while (n >= 2 && p[n - 2] == '\\'
185 && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL)
186 n -= 2;
187 return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|');
188}
189
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200190// Struct to store the viewstate during 'incsearch' highlighting.
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200191typedef struct {
192 colnr_T vs_curswant;
193 colnr_T vs_leftcol;
194 linenr_T vs_topline;
195# ifdef FEAT_DIFF
196 int vs_topfill;
197# endif
198 linenr_T vs_botline;
199 linenr_T vs_empty_rows;
200} viewstate_T;
201
202 static void
203save_viewstate(viewstate_T *vs)
204{
205 vs->vs_curswant = curwin->w_curswant;
206 vs->vs_leftcol = curwin->w_leftcol;
207 vs->vs_topline = curwin->w_topline;
208# ifdef FEAT_DIFF
209 vs->vs_topfill = curwin->w_topfill;
210# endif
211 vs->vs_botline = curwin->w_botline;
212 vs->vs_empty_rows = curwin->w_empty_rows;
213}
214
215 static void
216restore_viewstate(viewstate_T *vs)
217{
218 curwin->w_curswant = vs->vs_curswant;
219 curwin->w_leftcol = vs->vs_leftcol;
220 curwin->w_topline = vs->vs_topline;
221# ifdef FEAT_DIFF
222 curwin->w_topfill = vs->vs_topfill;
223# endif
224 curwin->w_botline = vs->vs_botline;
225 curwin->w_empty_rows = vs->vs_empty_rows;
226}
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200227
228// Struct to store the state of 'incsearch' highlighting.
229typedef struct {
230 pos_T search_start; // where 'incsearch' starts searching
231 pos_T save_cursor;
232 viewstate_T init_viewstate;
233 viewstate_T old_viewstate;
234 pos_T match_start;
235 pos_T match_end;
236 int did_incsearch;
237 int incsearch_postponed;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200238 int magic_save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200239} incsearch_state_T;
240
241 static void
242init_incsearch_state(incsearch_state_T *is_state)
243{
244 is_state->match_start = curwin->w_cursor;
245 is_state->did_incsearch = FALSE;
246 is_state->incsearch_postponed = FALSE;
Bram Moolenaar167ae422018-08-14 21:32:21 +0200247 is_state->magic_save = p_magic;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200248 CLEAR_POS(&is_state->match_end);
249 is_state->save_cursor = curwin->w_cursor; // may be restored later
250 is_state->search_start = curwin->w_cursor;
251 save_viewstate(&is_state->init_viewstate);
252 save_viewstate(&is_state->old_viewstate);
253}
254
255/*
256 * First move cursor to end of match, then to the start. This
257 * moves the whole match onto the screen when 'nowrap' is set.
258 */
259 static void
260set_search_match(pos_T *t)
261{
262 t->lnum += search_match_lines;
263 t->col = search_match_endcol;
264 if (t->lnum > curbuf->b_ml.ml_line_count)
265 {
266 t->lnum = curbuf->b_ml.ml_line_count;
267 coladvance((colnr_T)MAXCOL);
268 }
269}
270
271/*
272 * Return TRUE when 'incsearch' highlighting is to be done.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200273 * Sets search_first_line and search_last_line to the address range.
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200274 */
275 static int
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200276do_incsearch_highlighting(int firstc, incsearch_state_T *is_state,
277 int *skiplen, int *patlen)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200278{
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200279 char_u *cmd;
280 cmdmod_T save_cmdmod = cmdmod;
281 char_u *p;
282 int delim_optional = FALSE;
283 int delim;
284 char_u *end;
285 char_u *dummy;
286 exarg_T ea;
287 pos_T save_cursor;
288
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200289 *skiplen = 0;
290 *patlen = ccline.cmdlen;
291
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200292 if (!p_is || cmd_silent)
293 return FALSE;
294
295 // by default search all lines
296 search_first_line = 0;
297 search_last_line = MAXLNUM;
298
299 if (firstc == '/' || firstc == '?')
300 return TRUE;
301 if (firstc != ':')
302 return FALSE;
303
304 vim_memset(&ea, 0, sizeof(ea));
305 ea.line1 = 1;
306 ea.line2 = 1;
307 ea.cmd = ccline.cmdbuff;
308 ea.addr_type = ADDR_LINES;
309
310 parse_command_modifiers(&ea, &dummy, TRUE);
311 cmdmod = save_cmdmod;
312
313 cmd = skip_range(ea.cmd, NULL);
314 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
315 return FALSE;
316
317 // Skip over "substitute" to find the pattern separator.
318 for (p = cmd; ASCII_ISALPHA(*p); ++p)
319 ;
320 if (*skipwhite(p) == NUL)
321 return FALSE;
322
323 if (STRNCMP(cmd, "substitute", p - cmd) == 0
324 || STRNCMP(cmd, "smagic", p - cmd) == 0
325 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
326 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200327 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200328 if (*cmd == 's' && cmd[1] == 'm')
329 p_magic = TRUE;
330 else if (*cmd == 's' && cmd[1] == 'n')
331 p_magic = FALSE;
332 }
333 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
334 {
335 // skip over flags
336 while (ASCII_ISALPHA(*(p = skipwhite(p))))
337 ++p;
338 if (*p == NUL)
339 return FALSE;
340 }
341 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
342 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
343 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
344 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
345 || STRNCMP(cmd, "global", p - cmd) == 0)
346 {
347 // skip over "!"
348 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200349 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200350 p++;
351 if (*skipwhite(p) == NUL)
352 return FALSE;
353 }
354 if (*cmd != 'g')
355 delim_optional = TRUE;
356 }
357 else
358 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200359
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200360 p = skipwhite(p);
361 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
362 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200363
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200364 if (end == p && *end != delim)
365 return FALSE;
366 // found a non-empty pattern or //
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200367
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200368 *skiplen = (int)(p - ccline.cmdbuff);
369 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200370
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200371 // parse the address range
372 save_cursor = curwin->w_cursor;
373 curwin->w_cursor = is_state->search_start;
374 parse_cmd_address(&ea, &dummy);
375 if (ea.addr_count > 0)
376 {
377 // Allow for reverse match.
378 if (ea.line2 < ea.line1)
379 {
380 search_first_line = ea.line2;
381 search_last_line = ea.line1;
382 }
383 else
384 {
385 search_first_line = ea.line1;
386 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200387 }
388 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200389 else if (cmd[0] == 's' && cmd[1] != 'o')
390 {
391 // :s defaults to the current line
392 search_first_line = curwin->w_cursor.lnum;
393 search_last_line = curwin->w_cursor.lnum;
394 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200395
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200396 curwin->w_cursor = save_cursor;
397 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200398}
399
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200400 static void
401finish_incsearch_highlighting(
402 int gotesc,
403 incsearch_state_T *is_state,
404 int call_update_screen)
405{
406 if (is_state->did_incsearch)
407 {
408 is_state->did_incsearch = FALSE;
409 if (gotesc)
410 curwin->w_cursor = is_state->save_cursor;
411 else
412 {
413 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
414 {
415 // put the '" mark at the original position
416 curwin->w_cursor = is_state->save_cursor;
417 setpcmark();
418 }
419 curwin->w_cursor = is_state->search_start;
420 }
421 restore_viewstate(&is_state->old_viewstate);
422 highlight_match = FALSE;
423 validate_cursor(); /* needed for TAB */
424 if (call_update_screen)
425 update_screen(SOME_VALID);
426 else
427 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200428 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200429 }
430}
431
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200432/*
433 * Do 'incsearch' highlighting if desired.
434 */
435 static void
436may_do_incsearch_highlighting(
437 int firstc,
438 long count,
439 incsearch_state_T *is_state)
440{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200441 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200442 int i;
443 pos_T end_pos;
444 struct cmdline_info save_ccline;
445#ifdef FEAT_RELTIME
446 proftime_T tm;
447#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200448 int next_char;
449 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200450
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200451 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200452 {
453 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200454 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200455 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200456
457 // If there is a character waiting, search and redraw later.
458 if (char_avail())
459 {
460 is_state->incsearch_postponed = TRUE;
461 return;
462 }
463 is_state->incsearch_postponed = FALSE;
464
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200465 if (search_first_line == 0)
466 // start at the original cursor position
467 curwin->w_cursor = is_state->search_start;
468 else
469 {
470 // start at the first line in the range
471 curwin->w_cursor.lnum = search_first_line;
472 curwin->w_cursor.col = 0;
473 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200474 save_last_search_pattern();
475
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200476 // Use the previous pattern for ":s//".
477 next_char = ccline.cmdbuff[skiplen + patlen];
478 use_last_pat = patlen == 0 && skiplen > 0
479 && ccline.cmdbuff[skiplen - 1] == next_char;
480
481 // If there is no pattern, don't do anything.
482 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200483 {
484 i = 0;
485 set_no_hlsearch(TRUE); // turn off previous highlight
486 redraw_all_later(SOME_VALID);
487 }
488 else
489 {
490 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
491
492 cursor_off(); // so the user knows we're busy
493 out_flush();
494 ++emsg_off; // so it doesn't beep if bad expr
495#ifdef FEAT_RELTIME
496 // Set the time limit to half a second.
497 profile_setlimit(500L, &tm);
498#endif
499 if (!p_hls)
500 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200501 if (search_first_line != 0)
502 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200503 ccline.cmdbuff[skiplen + patlen] = NUL;
504 i = do_search(NULL, firstc == ':' ? '/' : firstc,
505 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200506#ifdef FEAT_RELTIME
507 &tm, NULL
508#else
509 NULL, NULL
510#endif
511 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200512 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200513 --emsg_off;
514
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200515 if (curwin->w_cursor.lnum < search_first_line
516 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200517 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200518 // match outside of address range
519 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200520 curwin->w_cursor = is_state->search_start;
521 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200522
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200523 // if interrupted while searching, behave like it failed
524 if (got_int)
525 {
526 (void)vpeekc(); // remove <C-C> from input stream
527 got_int = FALSE; // don't abandon the command line
528 i = 0;
529 }
530 else if (char_avail())
531 // cancelled searching because a char was typed
532 is_state->incsearch_postponed = TRUE;
533 }
534 if (i != 0)
535 highlight_match = TRUE; // highlight position
536 else
537 highlight_match = FALSE; // remove highlight
538
539 // First restore the old curwin values, so the screen is positioned in the
540 // same way as the actual search command.
541 restore_viewstate(&is_state->old_viewstate);
542 changed_cline_bef_curs();
543 update_topline();
544
545 if (i != 0)
546 {
547 pos_T save_pos = curwin->w_cursor;
548
549 is_state->match_start = curwin->w_cursor;
550 set_search_match(&curwin->w_cursor);
551 validate_cursor();
552 end_pos = curwin->w_cursor;
553 is_state->match_end = end_pos;
554 curwin->w_cursor = save_pos;
555 }
556 else
557 end_pos = curwin->w_cursor; // shutup gcc 4
558
559 // Disable 'hlsearch' highlighting if the pattern matches everything.
560 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200561 if (!use_last_pat)
562 {
563 next_char = ccline.cmdbuff[skiplen + patlen];
564 ccline.cmdbuff[skiplen + patlen] = NUL;
565 if (empty_pattern(ccline.cmdbuff))
566 set_no_hlsearch(TRUE);
567 ccline.cmdbuff[skiplen + patlen] = next_char;
568 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200569
570 validate_cursor();
571 // May redraw the status line to show the cursor position.
572 if (p_ru && curwin->w_status_height > 0)
573 curwin->w_redr_status = TRUE;
574
575 save_cmdline(&save_ccline);
576 update_screen(SOME_VALID);
577 restore_cmdline(&save_ccline);
578 restore_last_search_pattern();
579
580 // Leave it at the end to make CTRL-R CTRL-W work.
581 if (i != 0)
582 curwin->w_cursor = end_pos;
583
584 msg_starthere();
585 redrawcmdline();
586 is_state->did_incsearch = TRUE;
587}
588
589/*
590 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
591 * or previous match.
592 * Returns FAIL when jumping to cmdline_not_changed;
593 */
594 static int
595may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200596 int firstc,
597 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200599 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200600{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200601 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 pos_T t;
603 char_u *pat;
604 int search_flags = SEARCH_NOOF;
605 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200606 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200608 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200609 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200610 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200611 return FAIL;
612
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200613 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200614 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200615 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200616 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200617 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200618 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200619 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200620 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200621
622 save_last_search_pattern();
623 cursor_off();
624 out_flush();
625 if (c == Ctrl_G)
626 {
627 t = is_state->match_end;
628 if (LT_POS(is_state->match_start, is_state->match_end))
629 // Start searching at the end of the match not at the beginning of
630 // the next column.
631 (void)decl(&t);
632 search_flags += SEARCH_COL;
633 }
634 else
635 t = is_state->match_start;
636 if (!p_hls)
637 search_flags += SEARCH_KEEP;
638 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200639 save = pat[patlen];
640 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200641 i = searchit(curwin, curbuf, &t,
642 c == Ctrl_G ? FORWARD : BACKWARD,
643 pat, count, search_flags,
644 RE_SEARCH, 0, NULL, NULL);
645 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200646 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200647 if (i)
648 {
649 is_state->search_start = is_state->match_start;
650 is_state->match_end = t;
651 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200652 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200653 {
654 // Move just before the current match, so that when nv_search
655 // finishes the cursor will be put back on the match.
656 is_state->search_start = t;
657 (void)decl(&is_state->search_start);
658 }
659 else if (c == Ctrl_G && firstc == '?')
660 {
661 // Move just after the current match, so that when nv_search
662 // finishes the cursor will be put back on the match.
663 is_state->search_start = t;
664 (void)incl(&is_state->search_start);
665 }
666 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
667 {
668 // wrap around
669 is_state->search_start = t;
670 if (firstc == '?')
671 (void)incl(&is_state->search_start);
672 else
673 (void)decl(&is_state->search_start);
674 }
675
676 set_search_match(&is_state->match_end);
677 curwin->w_cursor = is_state->match_start;
678 changed_cline_bef_curs();
679 update_topline();
680 validate_cursor();
681 highlight_match = TRUE;
682 save_viewstate(&is_state->old_viewstate);
683 update_screen(NOT_VALID);
684 redrawcmdline();
685 }
686 else
687 vim_beep(BO_ERROR);
688 restore_last_search_pattern();
689 return FAIL;
690}
691
692/*
693 * When CTRL-L typed: add character from the match to the pattern.
694 * May set "*c" to the added character.
695 * Return OK when jumping to cmdline_not_changed.
696 */
697 static int
698may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
699{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200700 int skiplen, patlen;
701
702 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200703 return FAIL;
704
705 // Add a character from under the cursor for 'incsearch'.
706 if (is_state->did_incsearch)
707 {
708 curwin->w_cursor = is_state->match_end;
709 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
710 {
711 *c = gchar_cursor();
712
713 // If 'ignorecase' and 'smartcase' are set and the
714 // command line has no uppercase characters, convert
715 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200716 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200717 *c = MB_TOLOWER(*c);
718 if (*c != NUL)
719 {
720 if (*c == firstc || vim_strchr((char_u *)(
721 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
722 {
723 // put a backslash before special characters
724 stuffcharReadbuff(*c);
725 *c = '\\';
726 }
727 return FAIL;
728 }
729 }
730 }
731 return OK;
732}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200733#endif
734
Bram Moolenaar66216052017-12-16 16:33:44 +0100735/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 * getcmdline() - accept a command line starting with firstc.
737 *
738 * firstc == ':' get ":" command line.
739 * firstc == '/' or '?' get search pattern
740 * firstc == '=' get expression
741 * firstc == '@' get text for input() function
742 * firstc == '>' get text for debug mode
743 * firstc == NUL get text for :insert command
744 * firstc == -1 like NUL, and break on CTRL-C
745 *
746 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
747 * command line.
748 *
749 * Careful: getcmdline() can be called recursively!
750 *
751 * Return pointer to allocated string if there is a commandline, NULL
752 * otherwise.
753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100755getcmdline(
756 int firstc,
757 long count UNUSED, /* only used for incremental search */
758 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759{
760 int c;
761 int i;
762 int j;
763 int gotesc = FALSE; /* TRUE when <ESC> just typed */
764 int do_abbr; /* when TRUE check for abbr. */
765#ifdef FEAT_CMDHIST
766 char_u *lookfor = NULL; /* string to match */
767 int hiscnt; /* current history line in use */
768 int histype; /* history type to be used */
769#endif
770#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200771 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772#endif
773 int did_wild_list = FALSE; /* did wild_list() recently */
774 int wim_index = 0; /* index in wim_flags[] */
775 int res;
776 int save_msg_scroll = msg_scroll;
777 int save_State = State; /* remember State when called */
778 int some_key_typed = FALSE; /* one of the keys was typed */
779#ifdef FEAT_MOUSE
780 /* mouse drag and release events are ignored, unless they are
781 * preceded with a mouse down event */
782 int ignore_drag_release = TRUE;
783#endif
784#ifdef FEAT_EVAL
785 int break_ctrl_c = FALSE;
786#endif
787 expand_T xpc;
788 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200789#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000790 /* Everything that may work recursively should save and restore the
791 * current command line in save_ccline. That includes update_screen(), a
792 * custom status line may invoke ":normal". */
793 struct cmdline_info save_ccline;
794#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200795 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797#ifdef FEAT_EVAL
798 if (firstc == -1)
799 {
800 firstc = NUL;
801 break_ctrl_c = TRUE;
802 }
803#endif
804#ifdef FEAT_RIGHTLEFT
805 /* start without Hebrew mapping for a command line */
806 if (firstc == ':' || firstc == '=' || firstc == '>')
807 cmd_hkmap = 0;
808#endif
809
810 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200811
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200813 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814#endif
815
816 /*
817 * set some variables for redrawcmd()
818 */
819 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000820 ccline.cmdindent = (firstc > 0 ? indent : 0);
821
822 /* alloc initial ccline.cmdbuff */
823 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (ccline.cmdbuff == NULL)
825 return NULL; /* out of memory */
826 ccline.cmdlen = ccline.cmdpos = 0;
827 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100828 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000830 /* autoindent for :insert and :append */
831 if (firstc <= 0)
832 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200833 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000834 ccline.cmdbuff[indent] = NUL;
835 ccline.cmdpos = indent;
836 ccline.cmdspos = indent;
837 ccline.cmdlen = indent;
838 }
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000841 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842
843#ifdef FEAT_RIGHTLEFT
844 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
845 && (firstc == '/' || firstc == '?'))
846 cmdmsg_rl = TRUE;
847 else
848 cmdmsg_rl = FALSE;
849#endif
850
851 redir_off = TRUE; /* don't redirect the typed command */
852 if (!cmd_silent)
853 {
854 i = msg_scrolled;
855 msg_scrolled = 0; /* avoid wait_return message */
856 gotocmdline(TRUE);
857 msg_scrolled += i;
858 redrawcmdprompt(); /* draw prompt or indent */
859 set_cmdspos();
860 }
861 xpc.xp_context = EXPAND_NOTHING;
862 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000863#ifndef BACKSLASH_IN_FILENAME
864 xpc.xp_shell = FALSE;
865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000867#if defined(FEAT_EVAL)
868 if (ccline.input_fn)
869 {
870 xpc.xp_context = ccline.xp_context;
871 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000872# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000873 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000874# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000875 }
876#endif
877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 /*
879 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
880 * doing ":@0" when register 0 doesn't contain a CR.
881 */
882 msg_scroll = FALSE;
883
884 State = CMDLINE;
885
886 if (firstc == '/' || firstc == '?' || firstc == '@')
887 {
888 /* Use ":lmap" mappings for search pattern and input(). */
889 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
890 b_im_ptr = &curbuf->b_p_iminsert;
891 else
892 b_im_ptr = &curbuf->b_p_imsearch;
893 if (*b_im_ptr == B_IMODE_LMAP)
894 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100895#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 im_set_active(*b_im_ptr == B_IMODE_IM);
897#endif
898 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100899#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else if (p_imcmdline)
901 im_set_active(TRUE);
902#endif
903
904#ifdef FEAT_MOUSE
905 setmouse();
906#endif
907#ifdef CURSOR_SHAPE
908 ui_cursor_shape(); /* may show different cursor shape */
909#endif
910
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000911 /* When inside an autocommand for writing "exiting" may be set and
912 * terminal mode set to cooked. Need to set raw mode here then. */
913 settmode(TMODE_RAW);
914
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200915 /* Trigger CmdlineEnter autocommands. */
916 cmdline_type = firstc == NUL ? '-' : firstc;
917 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200918
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919#ifdef FEAT_CMDHIST
920 init_history();
921 hiscnt = hislen; /* set hiscnt to impossible history value */
922 histype = hist_char2type(firstc);
923#endif
924
925#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000926 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927#endif
928
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200929 /* If something above caused an error, reset the flags, we do want to type
930 * and execute commands. Display may be messed up a bit. */
931 if (did_emsg)
932 redrawcmd();
933 did_emsg = FALSE;
934 got_int = FALSE;
935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 /*
937 * Collect the command string, handling editing keys.
938 */
939 for (;;)
940 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000941 redir_off = TRUE; /* Don't redirect the typed command.
942 Repeated, because a ":redir" inside
943 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944#ifdef USE_ON_FLY_SCROLL
945 dont_scroll = FALSE; /* allow scrolling here */
946#endif
947 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
948
Bram Moolenaar72532d32018-04-07 19:09:09 +0200949 did_emsg = FALSE; /* There can't really be a reason why an error
950 that occurs while typing a command should
951 cause the command not to be executed. */
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000954
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100955 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
956 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000957 do
958 {
959 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100960 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 if (KeyTyped)
963 {
964 some_key_typed = TRUE;
965#ifdef FEAT_RIGHTLEFT
966 if (cmd_hkmap)
967 c = hkmap(c);
968# ifdef FEAT_FKMAP
969 if (cmd_fkmap)
970 c = cmdl_fkmap(c);
971# endif
972 if (cmdmsg_rl && !KeyStuffed)
973 {
974 /* Invert horizontal movements and operations. Only when
975 * typed by the user directly, not when the result of a
976 * mapping. */
977 switch (c)
978 {
979 case K_RIGHT: c = K_LEFT; break;
980 case K_S_RIGHT: c = K_S_LEFT; break;
981 case K_C_RIGHT: c = K_C_LEFT; break;
982 case K_LEFT: c = K_RIGHT; break;
983 case K_S_LEFT: c = K_S_RIGHT; break;
984 case K_C_LEFT: c = K_C_RIGHT; break;
985 }
986 }
987#endif
988 }
989
990 /*
991 * Ignore got_int when CTRL-C was typed here.
992 * Don't ignore it in :global, we really need to break then, e.g., for
993 * ":g/pat/normal /pat" (without the <CR>).
994 * Don't ignore it for the input() function.
995 */
996 if ((c == Ctrl_C
997#ifdef UNIX
998 || c == intr_char
999#endif
1000 )
1001#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1002 && firstc != '@'
1003#endif
1004#ifdef FEAT_EVAL
1005 && !break_ctrl_c
1006#endif
1007 && !global_busy)
1008 got_int = FALSE;
1009
1010#ifdef FEAT_CMDHIST
1011 /* free old command line when finished moving around in the history
1012 * list */
1013 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001014 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001015 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 && c != K_PAGEDOWN && c != K_PAGEUP
1017 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001018 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001020 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021#endif
1022
1023 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001024 * When there are matching completions to select <S-Tab> works like
1025 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001027 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 c = Ctrl_P;
1029
1030#ifdef FEAT_WILDMENU
1031 /* Special translations for 'wildmenu' */
1032 if (did_wild_list && p_wmnu)
1033 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001034 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001036 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 c = Ctrl_N;
1038 }
1039 /* Hitting CR after "emenu Name.": complete submenu */
1040 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1041 && ccline.cmdpos > 1
1042 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1043 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1044 && (c == '\n' || c == '\r' || c == K_KENTER))
1045 c = K_DOWN;
1046#endif
1047
1048 /* free expanded names when finished walking through matches */
1049 if (xpc.xp_numfiles != -1
1050 && !(c == p_wc && KeyTyped) && c != p_wcm
1051 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1052 && c != Ctrl_L)
1053 {
1054 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1055 did_wild_list = FALSE;
1056#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001057 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058#endif
1059 xpc.xp_context = EXPAND_NOTHING;
1060 wim_index = 0;
1061#ifdef FEAT_WILDMENU
1062 if (p_wmnu && wild_menu_showing != 0)
1063 {
1064 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001065 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001066
1067 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001068 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069
1070 if (wild_menu_showing == WM_SCROLLED)
1071 {
1072 /* Entered command line, move it up */
1073 cmdline_row--;
1074 redrawcmd();
1075 }
1076 else if (save_p_ls != -1)
1077 {
1078 /* restore 'laststatus' and 'winminheight' */
1079 p_ls = save_p_ls;
1080 p_wmh = save_p_wmh;
1081 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001082 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001084 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 redrawcmd();
1086 save_p_ls = -1;
1087 }
1088 else
1089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 redraw_statuslines();
1092 }
1093 KeyTyped = skt;
1094 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001095 if (ccline.input_fn)
1096 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098#endif
1099 }
1100
1101#ifdef FEAT_WILDMENU
1102 /* Special translations for 'wildmenu' */
1103 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1104 {
1105 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001106 if (c == K_DOWN && ccline.cmdpos > 0
1107 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001109 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 {
1111 /* Hitting <Up>: Remove one submenu name in front of the
1112 * cursor */
1113 int found = FALSE;
1114
1115 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1116 i = 0;
1117 while (--j > 0)
1118 {
1119 /* check for start of menu name */
1120 if (ccline.cmdbuff[j] == ' '
1121 && ccline.cmdbuff[j - 1] != '\\')
1122 {
1123 i = j + 1;
1124 break;
1125 }
1126 /* check for start of submenu name */
1127 if (ccline.cmdbuff[j] == '.'
1128 && ccline.cmdbuff[j - 1] != '\\')
1129 {
1130 if (found)
1131 {
1132 i = j + 1;
1133 break;
1134 }
1135 else
1136 found = TRUE;
1137 }
1138 }
1139 if (i > 0)
1140 cmdline_del(i);
1141 c = p_wc;
1142 xpc.xp_context = EXPAND_NOTHING;
1143 }
1144 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001145 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001146 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001147 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 {
1149 char_u upseg[5];
1150
1151 upseg[0] = PATHSEP;
1152 upseg[1] = '.';
1153 upseg[2] = '.';
1154 upseg[3] = PATHSEP;
1155 upseg[4] = NUL;
1156
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001157 if (c == K_DOWN
1158 && ccline.cmdpos > 0
1159 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1160 && (ccline.cmdpos < 3
1161 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1163 {
1164 /* go down a directory */
1165 c = p_wc;
1166 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001167 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 {
1169 /* If in a direct ancestor, strip off one ../ to go down */
1170 int found = FALSE;
1171
1172 j = ccline.cmdpos;
1173 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1174 while (--j > i)
1175 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001176#ifdef FEAT_MBYTE
1177 if (has_mbyte)
1178 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 if (vim_ispathsep(ccline.cmdbuff[j]))
1181 {
1182 found = TRUE;
1183 break;
1184 }
1185 }
1186 if (found
1187 && ccline.cmdbuff[j - 1] == '.'
1188 && ccline.cmdbuff[j - 2] == '.'
1189 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1190 {
1191 cmdline_del(j - 2);
1192 c = p_wc;
1193 }
1194 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001195 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 {
1197 /* go up a directory */
1198 int found = FALSE;
1199
1200 j = ccline.cmdpos - 1;
1201 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1202 while (--j > i)
1203 {
1204#ifdef FEAT_MBYTE
1205 if (has_mbyte)
1206 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1207#endif
1208 if (vim_ispathsep(ccline.cmdbuff[j])
1209#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001210 && vim_strchr((char_u *)" *?[{`$%#",
1211 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#endif
1213 )
1214 {
1215 if (found)
1216 {
1217 i = j + 1;
1218 break;
1219 }
1220 else
1221 found = TRUE;
1222 }
1223 }
1224
1225 if (!found)
1226 j = i;
1227 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1228 j += 4;
1229 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1230 && j == i)
1231 j += 3;
1232 else
1233 j = 0;
1234 if (j > 0)
1235 {
1236 /* TODO this is only for DOS/UNIX systems - need to put in
1237 * machine-specific stuff here and in upseg init */
1238 cmdline_del(j);
1239 put_on_cmdline(upseg + 1, 3, FALSE);
1240 }
1241 else if (ccline.cmdpos > i)
1242 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001243
1244 /* Now complete in the new directory. Set KeyTyped in case the
1245 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001247 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 }
1249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250
1251#endif /* FEAT_WILDMENU */
1252
1253 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1254 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1255 if (c == Ctrl_BSL)
1256 {
1257 ++no_mapping;
1258 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001259 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 --no_mapping;
1261 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001262 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1263 * is in a mapping. */
1264 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1265 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 {
1267 vungetc(c);
1268 c = Ctrl_BSL;
1269 }
1270#ifdef FEAT_EVAL
1271 else if (c == 'e')
1272 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001273 char_u *p = NULL;
1274 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275
1276 /*
1277 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001278 * Need to save and restore the current command line, to be
1279 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 */
1281 if (ccline.cmdpos == ccline.cmdlen)
1282 new_cmdpos = 99999; /* keep it at the end */
1283 else
1284 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001285
1286 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001288 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 if (c == '=')
1290 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001291 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001292 * to avoid nasty things like going to another buffer when
1293 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001294 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001295 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001297 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001298 restore_cmdline(&save_ccline);
1299
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001300 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001302 len = (int)STRLEN(p);
1303 if (realloc_cmdbuff(len + 1) == OK)
1304 {
1305 ccline.cmdlen = len;
1306 STRCPY(ccline.cmdbuff, p);
1307 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001309 /* Restore the cursor or use the position set with
1310 * set_cmdline_pos(). */
1311 if (new_cmdpos > ccline.cmdlen)
1312 ccline.cmdpos = ccline.cmdlen;
1313 else
1314 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001316 KeyTyped = FALSE; /* Don't do p_wc completion. */
1317 redrawcmd();
1318 goto cmdline_changed;
1319 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320 }
1321 }
1322 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001323 got_int = FALSE; /* don't abandon the command line */
1324 did_emsg = FALSE;
1325 emsg_on_display = FALSE;
1326 redrawcmd();
1327 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329#endif
1330 else
1331 {
1332 if (c == Ctrl_G && p_im && restart_edit == 0)
1333 restart_edit = 'a';
1334 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1335 in history */
1336 goto returncmd; /* back to Normal mode */
1337 }
1338 }
1339
1340#ifdef FEAT_CMDWIN
1341 if (c == cedit_key || c == K_CMDWIN)
1342 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001343 if (ex_normal_busy == 0 && got_int == FALSE)
1344 {
1345 /*
1346 * Open a window to edit the command line (and history).
1347 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001348 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001349 some_key_typed = TRUE;
1350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 }
1352# ifdef FEAT_DIGRAPHS
1353 else
1354# endif
1355#endif
1356#ifdef FEAT_DIGRAPHS
1357 c = do_digraph(c);
1358#endif
1359
1360 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1361 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1362 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001363 /* In Ex mode a backslash escapes a newline. */
1364 if (exmode_active
1365 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001366 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001367 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001368 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001370 if (c == K_KENTER)
1371 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001373 else
1374 {
1375 gotesc = FALSE; /* Might have typed ESC previously, don't
1376 truncate the cmdline now. */
1377 if (ccheck_abbr(c + ABBR_OFF))
1378 goto cmdline_changed;
1379 if (!cmd_silent)
1380 {
1381 windgoto(msg_row, 0);
1382 out_flush();
1383 }
1384 break;
1385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 }
1387
1388 /*
1389 * Completion for 'wildchar' or 'wildcharm' key.
1390 * - hitting <ESC> twice means: abandon command line.
1391 * - wildcard expansion is only done when the 'wildchar' key is really
1392 * typed, not when it comes from a macro
1393 */
1394 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1395 {
1396 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1397 {
1398 /* if 'wildmode' contains "list" may still need to list */
1399 if (xpc.xp_numfiles > 1
1400 && !did_wild_list
1401 && (wim_flags[wim_index] & WIM_LIST))
1402 {
1403 (void)showmatches(&xpc, FALSE);
1404 redrawcmd();
1405 did_wild_list = TRUE;
1406 }
1407 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001408 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1409 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001411 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1412 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 else
1414 res = OK; /* don't insert 'wildchar' now */
1415 }
1416 else /* typed p_wc first time */
1417 {
1418 wim_index = 0;
1419 j = ccline.cmdpos;
1420 /* if 'wildmode' first contains "longest", get longest
1421 * common part */
1422 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001423 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1424 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001426 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1427 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
1429 /* if interrupted while completing, behave like it failed */
1430 if (got_int)
1431 {
1432 (void)vpeekc(); /* remove <C-C> from input stream */
1433 got_int = FALSE; /* don't abandon the command line */
1434 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1435#ifdef FEAT_WILDMENU
1436 xpc.xp_context = EXPAND_NOTHING;
1437#endif
1438 goto cmdline_changed;
1439 }
1440
1441 /* when more than one match, and 'wildmode' first contains
1442 * "list", or no change and 'wildmode' contains "longest,list",
1443 * list all matches */
1444 if (res == OK && xpc.xp_numfiles > 1)
1445 {
1446 /* a "longest" that didn't do anything is skipped (but not
1447 * "list:longest") */
1448 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1449 wim_index = 1;
1450 if ((wim_flags[wim_index] & WIM_LIST)
1451#ifdef FEAT_WILDMENU
1452 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1453#endif
1454 )
1455 {
1456 if (!(wim_flags[0] & WIM_LONGEST))
1457 {
1458#ifdef FEAT_WILDMENU
1459 int p_wmnu_save = p_wmnu;
1460 p_wmnu = 0;
1461#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001462 /* remove match */
1463 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464#ifdef FEAT_WILDMENU
1465 p_wmnu = p_wmnu_save;
1466#endif
1467 }
1468#ifdef FEAT_WILDMENU
1469 (void)showmatches(&xpc, p_wmnu
1470 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1471#else
1472 (void)showmatches(&xpc, FALSE);
1473#endif
1474 redrawcmd();
1475 did_wild_list = TRUE;
1476 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001477 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1478 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001480 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1481 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 }
1483 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001484 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 }
1486#ifdef FEAT_WILDMENU
1487 else if (xpc.xp_numfiles == -1)
1488 xpc.xp_context = EXPAND_NOTHING;
1489#endif
1490 }
1491 if (wim_index < 3)
1492 ++wim_index;
1493 if (c == ESC)
1494 gotesc = TRUE;
1495 if (res == OK)
1496 goto cmdline_changed;
1497 }
1498
1499 gotesc = FALSE;
1500
1501 /* <S-Tab> goes to last match, in a clumsy way */
1502 if (c == K_S_TAB && KeyTyped)
1503 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001504 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1505 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1506 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 goto cmdline_changed;
1508 }
1509
1510 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1511 c = NL;
1512
1513 do_abbr = TRUE; /* default: check for abbreviation */
1514
1515 /*
1516 * Big switch for a typed command line character.
1517 */
1518 switch (c)
1519 {
1520 case K_BS:
1521 case Ctrl_H:
1522 case K_DEL:
1523 case K_KDEL:
1524 case Ctrl_W:
1525#ifdef FEAT_FKMAP
1526 if (cmd_fkmap && c == K_BS)
1527 c = K_DEL;
1528#endif
1529 if (c == K_KDEL)
1530 c = K_DEL;
1531
1532 /*
1533 * delete current character is the same as backspace on next
1534 * character, except at end of line
1535 */
1536 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1537 ++ccline.cmdpos;
1538#ifdef FEAT_MBYTE
1539 if (has_mbyte && c == K_DEL)
1540 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1541 ccline.cmdbuff + ccline.cmdpos);
1542#endif
1543 if (ccline.cmdpos > 0)
1544 {
1545 char_u *p;
1546
1547 j = ccline.cmdpos;
1548 p = ccline.cmdbuff + j;
1549#ifdef FEAT_MBYTE
1550 if (has_mbyte)
1551 {
1552 p = mb_prevptr(ccline.cmdbuff, p);
1553 if (c == Ctrl_W)
1554 {
1555 while (p > ccline.cmdbuff && vim_isspace(*p))
1556 p = mb_prevptr(ccline.cmdbuff, p);
1557 i = mb_get_class(p);
1558 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1559 p = mb_prevptr(ccline.cmdbuff, p);
1560 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001561 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 }
1563 }
1564 else
1565#endif
1566 if (c == Ctrl_W)
1567 {
1568 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1569 --p;
1570 i = vim_iswordc(p[-1]);
1571 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1572 && vim_iswordc(p[-1]) == i)
1573 --p;
1574 }
1575 else
1576 --p;
1577 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1578 ccline.cmdlen -= j - ccline.cmdpos;
1579 i = ccline.cmdpos;
1580 while (i < ccline.cmdlen)
1581 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1582
1583 /* Truncate at the end, required for multi-byte chars. */
1584 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001585#ifdef FEAT_SEARCH_EXTRA
1586 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001587 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001588 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001589 /* save view settings, so that the screen
1590 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001591 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001592 }
1593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 redrawcmd();
1595 }
1596 else if (ccline.cmdlen == 0 && c != Ctrl_W
1597 && ccline.cmdprompt == NULL && indent == 0)
1598 {
1599 /* In ex and debug mode it doesn't make sense to return. */
1600 if (exmode_active
1601#ifdef FEAT_EVAL
1602 || ccline.cmdfirstc == '>'
1603#endif
1604 )
1605 goto cmdline_not_changed;
1606
Bram Moolenaard23a8232018-02-10 18:45:26 +01001607 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 if (!cmd_silent)
1609 {
1610#ifdef FEAT_RIGHTLEFT
1611 if (cmdmsg_rl)
1612 msg_col = Columns;
1613 else
1614#endif
1615 msg_col = 0;
1616 msg_putchar(' '); /* delete ':' */
1617 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001618#ifdef FEAT_SEARCH_EXTRA
1619 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001620 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 redraw_cmdline = TRUE;
1623 goto returncmd; /* back to cmd mode */
1624 }
1625 goto cmdline_changed;
1626
1627 case K_INS:
1628 case K_KINS:
1629#ifdef FEAT_FKMAP
1630 /* if Farsi mode set, we are in reverse insert mode -
1631 Do not change the mode */
1632 if (cmd_fkmap)
1633 beep_flush();
1634 else
1635#endif
1636 ccline.overstrike = !ccline.overstrike;
1637#ifdef CURSOR_SHAPE
1638 ui_cursor_shape(); /* may show different cursor shape */
1639#endif
1640 goto cmdline_not_changed;
1641
1642 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001643 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 {
1645 /* ":lmap" mappings exists, toggle use of mappings. */
1646 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001647#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 im_set_active(FALSE); /* Disable input method */
1649#endif
1650 if (b_im_ptr != NULL)
1651 {
1652 if (State & LANGMAP)
1653 *b_im_ptr = B_IMODE_LMAP;
1654 else
1655 *b_im_ptr = B_IMODE_NONE;
1656 }
1657 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001658#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 else
1660 {
1661 /* There are no ":lmap" mappings, toggle IM. When
1662 * 'imdisable' is set don't try getting the status, it's
1663 * always off. */
1664 if ((p_imdisable && b_im_ptr != NULL)
1665 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1666 {
1667 im_set_active(FALSE); /* Disable input method */
1668 if (b_im_ptr != NULL)
1669 *b_im_ptr = B_IMODE_NONE;
1670 }
1671 else
1672 {
1673 im_set_active(TRUE); /* Enable input method */
1674 if (b_im_ptr != NULL)
1675 *b_im_ptr = B_IMODE_IM;
1676 }
1677 }
1678#endif
1679 if (b_im_ptr != NULL)
1680 {
1681 if (b_im_ptr == &curbuf->b_p_iminsert)
1682 set_iminsert_global();
1683 else
1684 set_imsearch_global();
1685 }
1686#ifdef CURSOR_SHAPE
1687 ui_cursor_shape(); /* may show different cursor shape */
1688#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001689#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 /* Show/unshow value of 'keymap' in status lines later. */
1691 status_redraw_curbuf();
1692#endif
1693 goto cmdline_not_changed;
1694
1695/* case '@': only in very old vi */
1696 case Ctrl_U:
1697 /* delete all characters left of the cursor */
1698 j = ccline.cmdpos;
1699 ccline.cmdlen -= j;
1700 i = ccline.cmdpos = 0;
1701 while (i < ccline.cmdlen)
1702 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1703 /* Truncate at the end, required for multi-byte chars. */
1704 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001705#ifdef FEAT_SEARCH_EXTRA
1706 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001707 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 redrawcmd();
1710 goto cmdline_changed;
1711
1712#ifdef FEAT_CLIPBOARD
1713 case Ctrl_Y:
1714 /* Copy the modeless selection, if there is one. */
1715 if (clip_star.state != SELECT_CLEARED)
1716 {
1717 if (clip_star.state == SELECT_DONE)
1718 clip_copy_modeless_selection(TRUE);
1719 goto cmdline_not_changed;
1720 }
1721 break;
1722#endif
1723
1724 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1725 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001726 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001727 * ":normal" runs out of characters. */
1728 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001729 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 goto cmdline_not_changed;
1731
1732 gotesc = TRUE; /* will free ccline.cmdbuff after
1733 putting it in history */
1734 goto returncmd; /* back to cmd mode */
1735
1736 case Ctrl_R: /* insert register */
1737#ifdef USE_ON_FLY_SCROLL
1738 dont_scroll = TRUE; /* disallow scrolling here */
1739#endif
1740 putcmdline('"', TRUE);
1741 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001742 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 if (i == Ctrl_O)
1744 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1745 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001746 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001747 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 --no_mapping;
1749#ifdef FEAT_EVAL
1750 /*
1751 * Insert the result of an expression.
1752 * Need to save the current command line, to be able to enter
1753 * a new one...
1754 */
1755 new_cmdpos = -1;
1756 if (c == '=')
1757 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1759 {
1760 beep_flush();
1761 c = ESC;
1762 }
1763 else
1764 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001765 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001767 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 }
1769 }
1770#endif
1771 if (c != ESC) /* use ESC to cancel inserting register */
1772 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001773 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001774
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001775#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001776 /* When there was a serious error abort getting the
1777 * command line. */
1778 if (aborting())
1779 {
1780 gotesc = TRUE; /* will free ccline.cmdbuff after
1781 putting it in history */
1782 goto returncmd; /* back to cmd mode */
1783 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001784#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 KeyTyped = FALSE; /* Don't do p_wc completion. */
1786#ifdef FEAT_EVAL
1787 if (new_cmdpos >= 0)
1788 {
1789 /* set_cmdline_pos() was used */
1790 if (new_cmdpos > ccline.cmdlen)
1791 ccline.cmdpos = ccline.cmdlen;
1792 else
1793 ccline.cmdpos = new_cmdpos;
1794 }
1795#endif
1796 }
1797 redrawcmd();
1798 goto cmdline_changed;
1799
1800 case Ctrl_D:
1801 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1802 break; /* Use ^D as normal char instead */
1803
1804 redrawcmd();
1805 continue; /* don't do incremental search now */
1806
1807 case K_RIGHT:
1808 case K_S_RIGHT:
1809 case K_C_RIGHT:
1810 do
1811 {
1812 if (ccline.cmdpos >= ccline.cmdlen)
1813 break;
1814 i = cmdline_charsize(ccline.cmdpos);
1815 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1816 break;
1817 ccline.cmdspos += i;
1818#ifdef FEAT_MBYTE
1819 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001820 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 + ccline.cmdpos);
1822 else
1823#endif
1824 ++ccline.cmdpos;
1825 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001826 while ((c == K_S_RIGHT || c == K_C_RIGHT
1827 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1829#ifdef FEAT_MBYTE
1830 if (has_mbyte)
1831 set_cmdspos_cursor();
1832#endif
1833 goto cmdline_not_changed;
1834
1835 case K_LEFT:
1836 case K_S_LEFT:
1837 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001838 if (ccline.cmdpos == 0)
1839 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 do
1841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 --ccline.cmdpos;
1843#ifdef FEAT_MBYTE
1844 if (has_mbyte) /* move to first byte of char */
1845 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1846 ccline.cmdbuff + ccline.cmdpos);
1847#endif
1848 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1849 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001850 while (ccline.cmdpos > 0
1851 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001852 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1854#ifdef FEAT_MBYTE
1855 if (has_mbyte)
1856 set_cmdspos_cursor();
1857#endif
1858 goto cmdline_not_changed;
1859
1860 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001861 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001862 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863
Bram Moolenaar4770d092006-01-12 23:22:24 +00001864#ifdef FEAT_GUI_W32
1865 /* On Win32 ignore <M-F4>, we get it when closing the window was
1866 * cancelled. */
1867 case K_F4:
1868 if (mod_mask == MOD_MASK_ALT)
1869 {
1870 redrawcmd(); /* somehow the cmdline is cleared */
1871 goto cmdline_not_changed;
1872 }
1873 break;
1874#endif
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#ifdef FEAT_MOUSE
1877 case K_MIDDLEDRAG:
1878 case K_MIDDLERELEASE:
1879 goto cmdline_not_changed; /* Ignore mouse */
1880
1881 case K_MIDDLEMOUSE:
1882# ifdef FEAT_GUI
1883 /* When GUI is active, also paste when 'mouse' is empty */
1884 if (!gui.in_use)
1885# endif
1886 if (!mouse_has(MOUSE_COMMAND))
1887 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001888# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001890 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001892# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001893 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 redrawcmd();
1895 goto cmdline_changed;
1896
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001897# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001899 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 redrawcmd();
1901 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001902# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
1904 case K_LEFTDRAG:
1905 case K_LEFTRELEASE:
1906 case K_RIGHTDRAG:
1907 case K_RIGHTRELEASE:
1908 /* Ignore drag and release events when the button-down wasn't
1909 * seen before. */
1910 if (ignore_drag_release)
1911 goto cmdline_not_changed;
1912 /* FALLTHROUGH */
1913 case K_LEFTMOUSE:
1914 case K_RIGHTMOUSE:
1915 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1916 ignore_drag_release = TRUE;
1917 else
1918 ignore_drag_release = FALSE;
1919# ifdef FEAT_GUI
1920 /* When GUI is active, also move when 'mouse' is empty */
1921 if (!gui.in_use)
1922# endif
1923 if (!mouse_has(MOUSE_COMMAND))
1924 goto cmdline_not_changed; /* Ignore mouse */
1925# ifdef FEAT_CLIPBOARD
1926 if (mouse_row < cmdline_row && clip_star.available)
1927 {
1928 int button, is_click, is_drag;
1929
1930 /*
1931 * Handle modeless selection.
1932 */
1933 button = get_mouse_button(KEY2TERMCAP1(c),
1934 &is_click, &is_drag);
1935 if (mouse_model_popup() && button == MOUSE_LEFT
1936 && (mod_mask & MOD_MASK_SHIFT))
1937 {
1938 /* Translate shift-left to right button. */
1939 button = MOUSE_RIGHT;
1940 mod_mask &= ~MOD_MASK_SHIFT;
1941 }
1942 clip_modeless(button, is_click, is_drag);
1943 goto cmdline_not_changed;
1944 }
1945# endif
1946
1947 set_cmdspos();
1948 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1949 ++ccline.cmdpos)
1950 {
1951 i = cmdline_charsize(ccline.cmdpos);
1952 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1953 && mouse_col < ccline.cmdspos % Columns + i)
1954 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001955# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (has_mbyte)
1957 {
1958 /* Count ">" for double-wide char that doesn't fit. */
1959 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001960 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 + ccline.cmdpos) - 1;
1962 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001963# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 ccline.cmdspos += i;
1965 }
1966 goto cmdline_not_changed;
1967
1968 /* Mouse scroll wheel: ignored here */
1969 case K_MOUSEDOWN:
1970 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001971 case K_MOUSELEFT:
1972 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973 /* Alternate buttons ignored here */
1974 case K_X1MOUSE:
1975 case K_X1DRAG:
1976 case K_X1RELEASE:
1977 case K_X2MOUSE:
1978 case K_X2DRAG:
1979 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001980 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 goto cmdline_not_changed;
1982
1983#endif /* FEAT_MOUSE */
1984
1985#ifdef FEAT_GUI
1986 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1987 case K_LEFTRELEASE_NM:
1988 goto cmdline_not_changed;
1989
1990 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001991 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 {
1993 gui_do_scroll();
1994 redrawcmd();
1995 }
1996 goto cmdline_not_changed;
1997
1998 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001999 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002001 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 redrawcmd();
2003 }
2004 goto cmdline_not_changed;
2005#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002006#ifdef FEAT_GUI_TABLINE
2007 case K_TABLINE:
2008 case K_TABMENU:
2009 /* Don't want to change any tabs here. Make sure the same tab
2010 * is still selected. */
2011 if (gui_use_tabline())
2012 gui_mch_set_curtab(tabpage_index(curtab));
2013 goto cmdline_not_changed;
2014#endif
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 case K_SELECT: /* end of Select mode mapping - ignore */
2017 goto cmdline_not_changed;
2018
2019 case Ctrl_B: /* begin of command line */
2020 case K_HOME:
2021 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 case K_S_HOME:
2023 case K_C_HOME:
2024 ccline.cmdpos = 0;
2025 set_cmdspos();
2026 goto cmdline_not_changed;
2027
2028 case Ctrl_E: /* end of command line */
2029 case K_END:
2030 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 case K_S_END:
2032 case K_C_END:
2033 ccline.cmdpos = ccline.cmdlen;
2034 set_cmdspos_cursor();
2035 goto cmdline_not_changed;
2036
2037 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002038 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 break;
2040 goto cmdline_changed;
2041
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002042 case Ctrl_L:
2043#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002044 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002045 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002046#endif
2047
2048 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002049 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 break;
2051 goto cmdline_changed;
2052
2053 case Ctrl_N: /* next match */
2054 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002055 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002057 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2058 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002060 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002063 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 case K_UP:
2065 case K_DOWN:
2066 case K_S_UP:
2067 case K_S_DOWN:
2068 case K_PAGEUP:
2069 case K_KPAGEUP:
2070 case K_PAGEDOWN:
2071 case K_KPAGEDOWN:
2072 if (hislen == 0 || firstc == NUL) /* no history */
2073 goto cmdline_not_changed;
2074
2075 i = hiscnt;
2076
2077 /* save current command string so it can be restored later */
2078 if (lookfor == NULL)
2079 {
2080 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2081 goto cmdline_not_changed;
2082 lookfor[ccline.cmdpos] = NUL;
2083 }
2084
2085 j = (int)STRLEN(lookfor);
2086 for (;;)
2087 {
2088 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002089 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002090 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
2092 if (hiscnt == hislen) /* first time */
2093 hiscnt = hisidx[histype];
2094 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2095 hiscnt = hislen - 1;
2096 else if (hiscnt != hisidx[histype] + 1)
2097 --hiscnt;
2098 else /* at top of list */
2099 {
2100 hiscnt = i;
2101 break;
2102 }
2103 }
2104 else /* one step forwards */
2105 {
2106 /* on last entry, clear the line */
2107 if (hiscnt == hisidx[histype])
2108 {
2109 hiscnt = hislen;
2110 break;
2111 }
2112
2113 /* not on a history line, nothing to do */
2114 if (hiscnt == hislen)
2115 break;
2116 if (hiscnt == hislen - 1) /* wrap around */
2117 hiscnt = 0;
2118 else
2119 ++hiscnt;
2120 }
2121 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2122 {
2123 hiscnt = i;
2124 break;
2125 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002126 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002127 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 || STRNCMP(history[histype][hiscnt].hisstr,
2129 lookfor, (size_t)j) == 0)
2130 break;
2131 }
2132
2133 if (hiscnt != i) /* jumped to other entry */
2134 {
2135 char_u *p;
2136 int len;
2137 int old_firstc;
2138
2139 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002140 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 if (hiscnt == hislen)
2142 p = lookfor; /* back to the old one */
2143 else
2144 p = history[histype][hiscnt].hisstr;
2145
2146 if (histype == HIST_SEARCH
2147 && p != lookfor
2148 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2149 {
2150 /* Correct for the separator character used when
2151 * adding the history entry vs the one used now.
2152 * First loop: count length.
2153 * Second loop: copy the characters. */
2154 for (i = 0; i <= 1; ++i)
2155 {
2156 len = 0;
2157 for (j = 0; p[j] != NUL; ++j)
2158 {
2159 /* Replace old sep with new sep, unless it is
2160 * escaped. */
2161 if (p[j] == old_firstc
2162 && (j == 0 || p[j - 1] != '\\'))
2163 {
2164 if (i > 0)
2165 ccline.cmdbuff[len] = firstc;
2166 }
2167 else
2168 {
2169 /* Escape new sep, unless it is already
2170 * escaped. */
2171 if (p[j] == firstc
2172 && (j == 0 || p[j - 1] != '\\'))
2173 {
2174 if (i > 0)
2175 ccline.cmdbuff[len] = '\\';
2176 ++len;
2177 }
2178 if (i > 0)
2179 ccline.cmdbuff[len] = p[j];
2180 }
2181 ++len;
2182 }
2183 if (i == 0)
2184 {
2185 alloc_cmdbuff(len);
2186 if (ccline.cmdbuff == NULL)
2187 goto returncmd;
2188 }
2189 }
2190 ccline.cmdbuff[len] = NUL;
2191 }
2192 else
2193 {
2194 alloc_cmdbuff((int)STRLEN(p));
2195 if (ccline.cmdbuff == NULL)
2196 goto returncmd;
2197 STRCPY(ccline.cmdbuff, p);
2198 }
2199
2200 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2201 redrawcmd();
2202 goto cmdline_changed;
2203 }
2204 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002205#endif
2206 goto cmdline_not_changed;
2207
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002208#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002209 case Ctrl_G: /* next match */
2210 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002211 if (may_adjust_incsearch_highlighting(
2212 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002213 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215#endif
2216
2217 case Ctrl_V:
2218 case Ctrl_Q:
2219#ifdef FEAT_MOUSE
2220 ignore_drag_release = TRUE;
2221#endif
2222 putcmdline('^', TRUE);
2223 c = get_literal(); /* get next (two) character(s) */
2224 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002225 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226#ifdef FEAT_MBYTE
2227 /* may need to remove ^ when composing char was typed */
2228 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2229 {
2230 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2231 msg_putchar(' ');
2232 cursorcmd();
2233 }
2234#endif
2235 break;
2236
2237#ifdef FEAT_DIGRAPHS
2238 case Ctrl_K:
2239#ifdef FEAT_MOUSE
2240 ignore_drag_release = TRUE;
2241#endif
2242 putcmdline('?', TRUE);
2243#ifdef USE_ON_FLY_SCROLL
2244 dont_scroll = TRUE; /* disallow scrolling here */
2245#endif
2246 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002247 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 if (c != NUL)
2249 break;
2250
2251 redrawcmd();
2252 goto cmdline_not_changed;
2253#endif /* FEAT_DIGRAPHS */
2254
2255#ifdef FEAT_RIGHTLEFT
2256 case Ctrl__: /* CTRL-_: switch language mode */
2257 if (!p_ari)
2258 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002259# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 if (p_altkeymap)
2261 {
2262 cmd_fkmap = !cmd_fkmap;
2263 if (cmd_fkmap) /* in Farsi always in Insert mode */
2264 ccline.overstrike = FALSE;
2265 }
2266 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 cmd_hkmap = !cmd_hkmap;
2269 goto cmdline_not_changed;
2270#endif
2271
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002272 case K_PS:
2273 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2274 goto cmdline_changed;
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 default:
2277#ifdef UNIX
2278 if (c == intr_char)
2279 {
2280 gotesc = TRUE; /* will free ccline.cmdbuff after
2281 putting it in history */
2282 goto returncmd; /* back to Normal mode */
2283 }
2284#endif
2285 /*
2286 * Normal character with no special meaning. Just set mod_mask
2287 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2288 * the string <S-Space>. This should only happen after ^V.
2289 */
2290 if (!IS_SPECIAL(c))
2291 mod_mask = 0x0;
2292 break;
2293 }
2294 /*
2295 * End of switch on command line character.
2296 * We come here if we have a normal character.
2297 */
2298
Bram Moolenaarede3e632013-06-23 16:16:19 +02002299 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300#ifdef FEAT_MBYTE
2301 /* Add ABBR_OFF for characters above 0x100, this is
2302 * what check_abbr() expects. */
2303 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2304#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002305 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 goto cmdline_changed;
2307
2308 /*
2309 * put the character in the command line
2310 */
2311 if (IS_SPECIAL(c) || mod_mask != 0)
2312 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2313 else
2314 {
2315#ifdef FEAT_MBYTE
2316 if (has_mbyte)
2317 {
2318 j = (*mb_char2bytes)(c, IObuff);
2319 IObuff[j] = NUL; /* exclude composing chars */
2320 put_on_cmdline(IObuff, j, TRUE);
2321 }
2322 else
2323#endif
2324 {
2325 IObuff[0] = c;
2326 put_on_cmdline(IObuff, 1, TRUE);
2327 }
2328 }
2329 goto cmdline_changed;
2330
2331/*
2332 * This part implements incremental searches for "/" and "?"
2333 * Jump to cmdline_not_changed when a character has been read but the command
2334 * line did not change. Then we only search and redraw if something changed in
2335 * the past.
2336 * Jump to cmdline_changed when the command line did change.
2337 * (Sorry for the goto's, I know it is ugly).
2338 */
2339cmdline_not_changed:
2340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002341 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 continue;
2343#endif
2344
2345cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002346 /* Trigger CmdlineChanged autocommands. */
2347 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002348
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002350 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351#endif
2352
2353#ifdef FEAT_RIGHTLEFT
2354 if (cmdmsg_rl
2355# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002356 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357# endif
2358 )
2359 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002360 * right-left typing. Not efficient, but it works.
2361 * Do it only when there are no characters left to read
2362 * to avoid useless intermediate redraws. */
2363 if (vpeekc() == NUL)
2364 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365#endif
2366 }
2367
2368returncmd:
2369
2370#ifdef FEAT_RIGHTLEFT
2371 cmdmsg_rl = FALSE;
2372#endif
2373
2374#ifdef FEAT_FKMAP
2375 cmd_fkmap = 0;
2376#endif
2377
2378 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002379 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
2381#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002382 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#endif
2384
2385 if (ccline.cmdbuff != NULL)
2386 {
2387 /*
2388 * Put line in history buffer (":" and "=" only when it was typed).
2389 */
2390#ifdef FEAT_CMDHIST
2391 if (ccline.cmdlen && firstc != NUL
2392 && (some_key_typed || histype == HIST_SEARCH))
2393 {
2394 add_to_history(histype, ccline.cmdbuff, TRUE,
2395 histype == HIST_SEARCH ? firstc : NUL);
2396 if (firstc == ':')
2397 {
2398 vim_free(new_last_cmdline);
2399 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2400 }
2401 }
2402#endif
2403
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002404 if (gotesc)
2405 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 }
2407
2408 /*
2409 * If the screen was shifted up, redraw the whole screen (later).
2410 * If the line is too long, clear it, so ruler and shown command do
2411 * not get printed in the middle of it.
2412 */
2413 msg_check();
2414 msg_scroll = save_msg_scroll;
2415 redir_off = FALSE;
2416
2417 /* When the command line was typed, no need for a wait-return prompt. */
2418 if (some_key_typed)
2419 need_wait_return = FALSE;
2420
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002421 /* Trigger CmdlineLeave autocommands. */
2422 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002425#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2427 im_save_status(b_im_ptr);
2428 im_set_active(FALSE);
2429#endif
2430#ifdef FEAT_MOUSE
2431 setmouse();
2432#endif
2433#ifdef CURSOR_SHAPE
2434 ui_cursor_shape(); /* may show different cursor shape */
2435#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002436 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002438 {
2439 char_u *p = ccline.cmdbuff;
2440
2441 /* Make ccline empty, getcmdline() may try to use it. */
2442 ccline.cmdbuff = NULL;
2443 return p;
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445}
2446
2447#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2448/*
2449 * Get a command line with a prompt.
2450 * This is prepared to be called recursively from getcmdline() (e.g. by
2451 * f_input() when evaluating an expression from CTRL-R =).
2452 * Returns the command line in allocated memory, or NULL.
2453 */
2454 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002455getcmdline_prompt(
2456 int firstc,
2457 char_u *prompt, /* command line prompt */
2458 int attr, /* attributes for prompt */
2459 int xp_context, /* type of expansion */
2460 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 char_u *s;
2463 struct cmdline_info save_ccline;
2464 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002465 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002467 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 ccline.cmdprompt = prompt;
2469 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002470# ifdef FEAT_EVAL
2471 ccline.xp_context = xp_context;
2472 ccline.xp_arg = xp_arg;
2473 ccline.input_fn = (firstc == '@');
2474# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002475 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002477 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002478 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002479 /* Restore msg_col, the prompt from input() may have changed it.
2480 * But only if called recursively and the commandline is therefore being
2481 * restored to an old one; if not, the input() prompt stays on the screen,
2482 * so we need its modified msg_col left intact. */
2483 if (ccline.cmdbuff != NULL)
2484 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 return s;
2487}
2488#endif
2489
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002490/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002491 * Return TRUE when the text must not be changed and we can't switch to
2492 * another window or buffer. Used when editing the command line, evaluating
2493 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002494 */
2495 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002496text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002497{
2498#ifdef FEAT_CMDWIN
2499 if (cmdwin_type != 0)
2500 return TRUE;
2501#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002502 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002503}
2504
2505/*
2506 * Give an error message for a command that isn't allowed while the cmdline
2507 * window is open or editing the cmdline in another way.
2508 */
2509 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002510text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002511{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002512 EMSG(_(get_text_locked_msg()));
2513}
2514
2515 char_u *
2516get_text_locked_msg(void)
2517{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002518#ifdef FEAT_CMDWIN
2519 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002520 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002521#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002522 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002523}
2524
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002525/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002526 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2527 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002528 */
2529 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002530curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002531{
2532 if (curbuf_lock > 0)
2533 {
2534 EMSG(_("E788: Not allowed to edit another buffer now"));
2535 return TRUE;
2536 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002537 return allbuf_locked();
2538}
2539
2540/*
2541 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2542 * message.
2543 */
2544 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002545allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002546{
2547 if (allbuf_lock > 0)
2548 {
2549 EMSG(_("E811: Not allowed to change buffer information now"));
2550 return TRUE;
2551 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002552 return FALSE;
2553}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002554
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002556cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2559 if (cmdline_star > 0) /* showing '*', always 1 position */
2560 return 1;
2561#endif
2562 return ptr2cells(ccline.cmdbuff + idx);
2563}
2564
2565/*
2566 * Compute the offset of the cursor on the command line for the prompt and
2567 * indent.
2568 */
2569 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002570set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002572 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 ccline.cmdspos = 1 + ccline.cmdindent;
2574 else
2575 ccline.cmdspos = 0 + ccline.cmdindent;
2576}
2577
2578/*
2579 * Compute the screen position for the cursor on the command line.
2580 */
2581 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002582set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583{
2584 int i, m, c;
2585
2586 set_cmdspos();
2587 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002588 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002590 if (m < 0) /* overflow, Columns or Rows at weird value */
2591 m = MAXCOL;
2592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 else
2594 m = MAXCOL;
2595 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2596 {
2597 c = cmdline_charsize(i);
2598#ifdef FEAT_MBYTE
2599 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2600 if (has_mbyte)
2601 correct_cmdspos(i, c);
2602#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002603 /* If the cmdline doesn't fit, show cursor on last visible char.
2604 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 if ((ccline.cmdspos += c) >= m)
2606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 ccline.cmdspos -= c;
2608 break;
2609 }
2610#ifdef FEAT_MBYTE
2611 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002612 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#endif
2614 }
2615}
2616
2617#ifdef FEAT_MBYTE
2618/*
2619 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2620 * character that doesn't fit, so that a ">" must be displayed.
2621 */
2622 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002623correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002625 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2627 && ccline.cmdspos % Columns + cells > Columns)
2628 ccline.cmdspos++;
2629}
2630#endif
2631
2632/*
2633 * Get an Ex command line for the ":" command.
2634 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002636getexline(
2637 int c, /* normally ':', NUL for ":append" */
2638 void *cookie UNUSED,
2639 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640{
2641 /* When executing a register, remove ':' that's in front of each line. */
2642 if (exec_from_reg && vpeekc() == ':')
2643 (void)vgetc();
2644 return getcmdline(c, 1L, indent);
2645}
2646
2647/*
2648 * Get an Ex command line for Ex mode.
2649 * In Ex mode we only use the OS supplied line editing features and no
2650 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002651 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002654getexmodeline(
2655 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002656 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002657 void *cookie UNUSED,
2658 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002660 garray_T line_ga;
2661 char_u *pend;
2662 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002663 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002664 int escaped = FALSE; /* CTRL-V typed */
2665 int vcol = 0;
2666 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002667 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002668 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 /* Switch cursor on now. This avoids that it happens after the "\n", which
2671 * confuses the system function that computes tabstops. */
2672 cursor_on();
2673
2674 /* always start in column 0; write a newline if necessary */
2675 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002676 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002678 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002680 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 if (p_prompt)
2682 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 while (indent-- > 0)
2684 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 }
2687
2688 ga_init2(&line_ga, 1, 30);
2689
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002690 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002691 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002692 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002693 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002694 while (indent >= 8)
2695 {
2696 ga_append(&line_ga, TAB);
2697 msg_puts((char_u *)" ");
2698 indent -= 8;
2699 }
2700 while (indent-- > 0)
2701 {
2702 ga_append(&line_ga, ' ');
2703 msg_putchar(' ');
2704 }
2705 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002706 ++no_mapping;
2707 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 /*
2710 * Get the line, one character at a time.
2711 */
2712 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002713 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002715 long sw;
2716 char_u *s;
2717
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 if (ga_grow(&line_ga, 40) == FAIL)
2719 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
Bram Moolenaarba748c82017-02-26 14:00:07 +01002721 /*
2722 * Get one character at a time.
2723 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002724 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002725
2726 /* Check for a ":normal" command and no more characters left. */
2727 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2728 c1 = '\n';
2729 else
2730 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
2732 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002733 * Handle line editing.
2734 * Previously this was left to the system, putting the terminal in
2735 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002737 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002739 msg_putchar('\n');
2740 break;
2741 }
2742
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002743 if (c1 == K_PS)
2744 {
2745 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2746 goto redraw;
2747 }
2748
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 if (!escaped)
2750 {
2751 /* CR typed means "enter", which is NL */
2752 if (c1 == '\r')
2753 c1 = '\n';
2754
2755 if (c1 == BS || c1 == K_BS
2756 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002758 if (line_ga.ga_len > 0)
2759 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002760#ifdef FEAT_MBYTE
2761 if (has_mbyte)
2762 {
2763 p = (char_u *)line_ga.ga_data;
2764 p[line_ga.ga_len] = NUL;
2765 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2766 line_ga.ga_len -= len;
2767 }
2768 else
2769#endif
2770 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002771 goto redraw;
2772 }
2773 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 msg_col = startcol;
2779 msg_clr_eos();
2780 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002781 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002782 }
2783
2784 if (c1 == Ctrl_T)
2785 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002786 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 p = (char_u *)line_ga.ga_data;
2788 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002789 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002790 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002791add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002792 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002794 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002795 p = (char_u *)line_ga.ga_data;
2796 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002797 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2798 *s = ' ';
2799 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002801redraw:
2802 /* redraw the line */
2803 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002805 p = (char_u *)line_ga.ga_data;
2806 p[line_ga.ga_len] = NUL;
2807 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002811 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002813 msg_putchar(' ');
2814 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002815 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002817 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002819 len = MB_PTR2LEN(p);
2820 msg_outtrans_len(p, len);
2821 vcol += ptr2cells(p);
2822 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002825 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002826 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 continue;
2828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 if (c1 == Ctrl_D)
2831 {
2832 /* Delete one shiftwidth. */
2833 p = (char_u *)line_ga.ga_data;
2834 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002836 if (prev_char == '^')
2837 ex_keep_indent = TRUE;
2838 indent = 0;
2839 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 }
2841 else
2842 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002844 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002845 if (indent > 0)
2846 {
2847 --indent;
2848 indent -= indent % get_sw_value(curbuf);
2849 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002851 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002852 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002853 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002854 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2855 --line_ga.ga_len;
2856 }
2857 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859
2860 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2861 {
2862 escaped = TRUE;
2863 continue;
2864 }
2865
2866 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2867 if (IS_SPECIAL(c1))
2868 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002870
2871 if (IS_SPECIAL(c1))
2872 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002873#ifdef FEAT_MBYTE
2874 if (has_mbyte)
2875 len = (*mb_char2bytes)(c1,
2876 (char_u *)line_ga.ga_data + line_ga.ga_len);
2877 else
2878#endif
2879 {
2880 len = 1;
2881 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2882 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002883 if (c1 == '\n')
2884 msg_putchar('\n');
2885 else if (c1 == TAB)
2886 {
2887 /* Don't use chartabsize(), 'ts' can be different */
2888 do
2889 {
2890 msg_putchar(' ');
2891 } while (++vcol % 8);
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002896 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002897 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002899 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900 escaped = FALSE;
2901
2902 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002903 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002904
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002905 /* We are done when a NL is entered, but not when it comes after an
2906 * odd number of backslashes, that results in a NUL. */
2907 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002909 int bcount = 0;
2910
2911 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2912 ++bcount;
2913
2914 if (bcount > 0)
2915 {
2916 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2917 * "\NL", etc. */
2918 line_ga.ga_len -= (bcount + 1) / 2;
2919 pend -= (bcount + 1) / 2;
2920 pend[-1] = '\n';
2921 }
2922
2923 if ((bcount & 1) == 0)
2924 {
2925 --line_ga.ga_len;
2926 --pend;
2927 *pend = NUL;
2928 break;
2929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 }
2931 }
2932
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002933 --no_mapping;
2934 --allow_keys;
2935
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 /* make following messages go to the next line */
2937 msg_didout = FALSE;
2938 msg_col = 0;
2939 if (msg_row < Rows - 1)
2940 ++msg_row;
2941 emsg_on_display = FALSE; /* don't want ui_delay() */
2942
2943 if (got_int)
2944 ga_clear(&line_ga);
2945
2946 return (char_u *)line_ga.ga_data;
2947}
2948
Bram Moolenaare344bea2005-09-01 20:46:49 +00002949# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2950 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951/*
2952 * Return TRUE if ccline.overstrike is on.
2953 */
2954 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002955cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 return ccline.overstrike;
2958}
2959
2960/*
2961 * Return TRUE if the cursor is at the end of the cmdline.
2962 */
2963 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002964cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965{
2966 return (ccline.cmdpos >= ccline.cmdlen);
2967}
2968#endif
2969
Bram Moolenaar9372a112005-12-06 19:59:18 +00002970#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971/*
2972 * Return the virtual column number at the current cursor position.
2973 * This is used by the IM code to obtain the start of the preedit string.
2974 */
2975 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002976cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977{
2978 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2979 return MAXCOL;
2980
2981# ifdef FEAT_MBYTE
2982 if (has_mbyte)
2983 {
2984 colnr_T col;
2985 int i = 0;
2986
2987 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002988 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 return col;
2991 }
2992 else
2993# endif
2994 return ccline.cmdpos;
2995}
2996#endif
2997
2998#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2999/*
3000 * If part of the command line is an IM preedit string, redraw it with
3001 * IM feedback attributes. The cursor position is restored after drawing.
3002 */
3003 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003004redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005{
3006 if ((State & CMDLINE)
3007 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003008 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 && !p_imdisable
3010 && im_is_preediting())
3011 {
3012 int cmdpos = 0;
3013 int cmdspos;
3014 int old_row;
3015 int old_col;
3016 colnr_T col;
3017
3018 old_row = msg_row;
3019 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003020 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021
3022# ifdef FEAT_MBYTE
3023 if (has_mbyte)
3024 {
3025 for (col = 0; col < preedit_start_col
3026 && cmdpos < ccline.cmdlen; ++col)
3027 {
3028 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003029 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 }
3031 }
3032 else
3033# endif
3034 {
3035 cmdspos += preedit_start_col;
3036 cmdpos += preedit_start_col;
3037 }
3038
3039 msg_row = cmdline_row + (cmdspos / (int)Columns);
3040 msg_col = cmdspos % (int)Columns;
3041 if (msg_row >= Rows)
3042 msg_row = Rows - 1;
3043
3044 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3045 {
3046 int char_len;
3047 int char_attr;
3048
3049 char_attr = im_get_feedback_attr(col);
3050 if (char_attr < 0)
3051 break; /* end of preedit string */
3052
3053# ifdef FEAT_MBYTE
3054 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003055 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 else
3057# endif
3058 char_len = 1;
3059
3060 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3061 cmdpos += char_len;
3062 }
3063
3064 msg_row = old_row;
3065 msg_col = old_col;
3066 }
3067}
3068#endif /* FEAT_XIM && FEAT_GUI_GTK */
3069
3070/*
3071 * Allocate a new command line buffer.
3072 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3073 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3074 */
3075 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003076alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077{
3078 /*
3079 * give some extra space to avoid having to allocate all the time
3080 */
3081 if (len < 80)
3082 len = 100;
3083 else
3084 len += 20;
3085
3086 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3087 ccline.cmdbufflen = len;
3088}
3089
3090/*
3091 * Re-allocate the command line to length len + something extra.
3092 * return FAIL for failure, OK otherwise
3093 */
3094 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003095realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096{
3097 char_u *p;
3098
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003099 if (len < ccline.cmdbufflen)
3100 return OK; /* no need to resize */
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 p = ccline.cmdbuff;
3103 alloc_cmdbuff(len); /* will get some more */
3104 if (ccline.cmdbuff == NULL) /* out of memory */
3105 {
3106 ccline.cmdbuff = p; /* keep the old one */
3107 return FAIL;
3108 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003109 /* There isn't always a NUL after the command, but it may need to be
3110 * there, thus copy up to the NUL and add a NUL. */
3111 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3112 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003114
3115 if (ccline.xpc != NULL
3116 && ccline.xpc->xp_pattern != NULL
3117 && ccline.xpc->xp_context != EXPAND_NOTHING
3118 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3119 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003120 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003121
3122 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3123 * to point into the newly allocated memory. */
3124 if (i >= 0 && i <= ccline.cmdlen)
3125 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3126 }
3127
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 return OK;
3129}
3130
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003131#if defined(FEAT_ARABIC) || defined(PROTO)
3132static char_u *arshape_buf = NULL;
3133
3134# if defined(EXITFREE) || defined(PROTO)
3135 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003136free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003137{
3138 vim_free(arshape_buf);
3139}
3140# endif
3141#endif
3142
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143/*
3144 * Draw part of the cmdline at the current cursor position. But draw stars
3145 * when cmdline_star is TRUE.
3146 */
3147 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003148draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3151 int i;
3152
3153 if (cmdline_star > 0)
3154 for (i = 0; i < len; ++i)
3155 {
3156 msg_putchar('*');
3157# ifdef FEAT_MBYTE
3158 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003159 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160# endif
3161 }
3162 else
3163#endif
3164#ifdef FEAT_ARABIC
3165 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3166 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 static int buflen = 0;
3168 char_u *p;
3169 int j;
3170 int newlen = 0;
3171 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003172 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 int prev_c = 0;
3174 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003175 int u8c;
3176 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178
3179 /*
3180 * Do arabic shaping into a temporary buffer. This is very
3181 * inefficient!
3182 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003183 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 {
3185 /* Re-allocate the buffer. We keep it around to avoid a lot of
3186 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003187 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003188 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003189 arshape_buf = alloc(buflen);
3190 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 return; /* out of memory */
3192 }
3193
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003194 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3195 {
3196 /* Prepend a space to draw the leading composing char on. */
3197 arshape_buf[0] = ' ';
3198 newlen = 1;
3199 }
3200
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 for (j = start; j < start + len; j += mb_l)
3202 {
3203 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003204 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003205 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (ARABIC_CHAR(u8c))
3207 {
3208 /* Do Arabic shaping. */
3209 if (cmdmsg_rl)
3210 {
3211 /* displaying from right to left */
3212 pc = prev_c;
3213 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003214 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 if (j + mb_l >= start + len)
3216 nc = NUL;
3217 else
3218 nc = utf_ptr2char(p + mb_l);
3219 }
3220 else
3221 {
3222 /* displaying from left to right */
3223 if (j + mb_l >= start + len)
3224 pc = NUL;
3225 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003226 {
3227 int pcc[MAX_MCO];
3228
3229 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003231 pc1 = pcc[0];
3232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 nc = prev_c;
3234 }
3235 prev_c = u8c;
3236
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003237 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003239 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003240 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3243 if (u8cc[1] != 0)
3244 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003245 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 }
3248 else
3249 {
3250 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003251 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 newlen += mb_l;
3253 }
3254 }
3255
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 }
3258 else
3259#endif
3260 msg_outtrans_len(ccline.cmdbuff + start, len);
3261}
3262
3263/*
3264 * Put a character on the command line. Shifts the following text to the
3265 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3266 * "c" must be printable (fit in one display cell)!
3267 */
3268 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003269putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 if (cmd_silent)
3272 return;
3273 msg_no_more = TRUE;
3274 msg_putchar(c);
3275 if (shift)
3276 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3277 msg_no_more = FALSE;
3278 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003279 extra_char = c;
3280 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281}
3282
3283/*
3284 * Undo a putcmdline(c, FALSE).
3285 */
3286 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003287unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288{
3289 if (cmd_silent)
3290 return;
3291 msg_no_more = TRUE;
3292 if (ccline.cmdlen == ccline.cmdpos)
3293 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003294#ifdef FEAT_MBYTE
3295 else if (has_mbyte)
3296 draw_cmdline(ccline.cmdpos,
3297 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299 else
3300 draw_cmdline(ccline.cmdpos, 1);
3301 msg_no_more = FALSE;
3302 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003303 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304}
3305
3306/*
3307 * Put the given string, of the given length, onto the command line.
3308 * If len is -1, then STRLEN() is used to calculate the length.
3309 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3310 * part will be redrawn, otherwise it will not. If this function is called
3311 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3312 * called afterwards.
3313 */
3314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003315put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316{
3317 int retval;
3318 int i;
3319 int m;
3320 int c;
3321
3322 if (len < 0)
3323 len = (int)STRLEN(str);
3324
3325 /* Check if ccline.cmdbuff needs to be longer */
3326 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003327 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 else
3329 retval = OK;
3330 if (retval == OK)
3331 {
3332 if (!ccline.overstrike)
3333 {
3334 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3335 ccline.cmdbuff + ccline.cmdpos,
3336 (size_t)(ccline.cmdlen - ccline.cmdpos));
3337 ccline.cmdlen += len;
3338 }
3339 else
3340 {
3341#ifdef FEAT_MBYTE
3342 if (has_mbyte)
3343 {
3344 /* Count nr of characters in the new string. */
3345 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003346 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 ++m;
3348 /* Count nr of bytes in cmdline that are overwritten by these
3349 * characters. */
3350 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003351 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 --m;
3353 if (i < ccline.cmdlen)
3354 {
3355 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3356 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3357 ccline.cmdlen += ccline.cmdpos + len - i;
3358 }
3359 else
3360 ccline.cmdlen = ccline.cmdpos + len;
3361 }
3362 else
3363#endif
3364 if (ccline.cmdpos + len > ccline.cmdlen)
3365 ccline.cmdlen = ccline.cmdpos + len;
3366 }
3367 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3368 ccline.cmdbuff[ccline.cmdlen] = NUL;
3369
3370#ifdef FEAT_MBYTE
3371 if (enc_utf8)
3372 {
3373 /* When the inserted text starts with a composing character,
3374 * backup to the character before it. There could be two of them.
3375 */
3376 i = 0;
3377 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3378 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3379 {
3380 i = (*mb_head_off)(ccline.cmdbuff,
3381 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3382 ccline.cmdpos -= i;
3383 len += i;
3384 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3385 }
3386# ifdef FEAT_ARABIC
3387 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3388 {
3389 /* Check the previous character for Arabic combining pair. */
3390 i = (*mb_head_off)(ccline.cmdbuff,
3391 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3392 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3393 + ccline.cmdpos - i), c))
3394 {
3395 ccline.cmdpos -= i;
3396 len += i;
3397 }
3398 else
3399 i = 0;
3400 }
3401# endif
3402 if (i != 0)
3403 {
3404 /* Also backup the cursor position. */
3405 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3406 ccline.cmdspos -= i;
3407 msg_col -= i;
3408 if (msg_col < 0)
3409 {
3410 msg_col += Columns;
3411 --msg_row;
3412 }
3413 }
3414 }
3415#endif
3416
3417 if (redraw && !cmd_silent)
3418 {
3419 msg_no_more = TRUE;
3420 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003421 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3423 /* Avoid clearing the rest of the line too often. */
3424 if (cmdline_row != i || ccline.overstrike)
3425 msg_clr_eos();
3426 msg_no_more = FALSE;
3427 }
3428#ifdef FEAT_FKMAP
3429 /*
3430 * If we are in Farsi command mode, the character input must be in
3431 * Insert mode. So do not advance the cmdpos.
3432 */
3433 if (!cmd_fkmap)
3434#endif
3435 {
3436 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003437 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003439 if (m < 0) /* overflow, Columns or Rows at weird value */
3440 m = MAXCOL;
3441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 else
3443 m = MAXCOL;
3444 for (i = 0; i < len; ++i)
3445 {
3446 c = cmdline_charsize(ccline.cmdpos);
3447#ifdef FEAT_MBYTE
3448 /* count ">" for a double-wide char that doesn't fit. */
3449 if (has_mbyte)
3450 correct_cmdspos(ccline.cmdpos, c);
3451#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003452 /* Stop cursor at the end of the screen, but do increment the
3453 * insert position, so that entering a very long command
3454 * works, even though you can't see it. */
3455 if (ccline.cmdspos + c < m)
3456 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457#ifdef FEAT_MBYTE
3458 if (has_mbyte)
3459 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003460 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 if (c > len - i - 1)
3462 c = len - i - 1;
3463 ccline.cmdpos += c;
3464 i += c;
3465 }
3466#endif
3467 ++ccline.cmdpos;
3468 }
3469 }
3470 }
3471 if (redraw)
3472 msg_check();
3473 return retval;
3474}
3475
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003476static struct cmdline_info prev_ccline;
3477static int prev_ccline_used = FALSE;
3478
3479/*
3480 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3481 * and overwrite it. But get_cmdline_str() may need it, thus make it
3482 * available globally in prev_ccline.
3483 */
3484 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003485save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003486{
3487 if (!prev_ccline_used)
3488 {
3489 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3490 prev_ccline_used = TRUE;
3491 }
3492 *ccp = prev_ccline;
3493 prev_ccline = ccline;
3494 ccline.cmdbuff = NULL;
3495 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003496 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003497}
3498
3499/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003500 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003501 */
3502 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003503restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003504{
3505 ccline = prev_ccline;
3506 prev_ccline = *ccp;
3507}
3508
Bram Moolenaar5a305422006-04-28 22:38:25 +00003509#if defined(FEAT_EVAL) || defined(PROTO)
3510/*
3511 * Save the command line into allocated memory. Returns a pointer to be
3512 * passed to restore_cmdline_alloc() later.
3513 * Returns NULL when failed.
3514 */
3515 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003516save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003517{
3518 struct cmdline_info *p;
3519
3520 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3521 if (p != NULL)
3522 save_cmdline(p);
3523 return (char_u *)p;
3524}
3525
3526/*
3527 * Restore the command line from the return value of save_cmdline_alloc().
3528 */
3529 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003530restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003531{
3532 if (p != NULL)
3533 {
3534 restore_cmdline((struct cmdline_info *)p);
3535 vim_free(p);
3536 }
3537}
3538#endif
3539
Bram Moolenaar8299df92004-07-10 09:47:34 +00003540/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003541 * Paste a yank register into the command line.
3542 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003543 * insert_reg() can't be used here, because special characters from the
3544 * register contents will be interpreted as commands.
3545 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003546 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003547 */
3548 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003549cmdline_paste(
3550 int regname,
3551 int literally, /* Insert text literally instead of "as typed" */
3552 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003553{
3554 long i;
3555 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003556 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003557 int allocated;
3558 struct cmdline_info save_ccline;
3559
3560 /* check for valid regname; also accept special characters for CTRL-R in
3561 * the command line */
3562 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003563 && regname != Ctrl_A && regname != Ctrl_L
3564 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003565 return FAIL;
3566
3567 /* A register containing CTRL-R can cause an endless loop. Allow using
3568 * CTRL-C to break the loop. */
3569 line_breakcheck();
3570 if (got_int)
3571 return FAIL;
3572
3573#ifdef FEAT_CLIPBOARD
3574 regname = may_get_selection(regname);
3575#endif
3576
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003577 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003578 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003579 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003580 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003581 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003582 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003583 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003584
3585 if (i)
3586 {
3587 /* Got the value of a special register in "arg". */
3588 if (arg == NULL)
3589 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003590
3591 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3592 * part of the word. */
3593 p = arg;
3594 if (p_is && regname == Ctrl_W)
3595 {
3596 char_u *w;
3597 int len;
3598
3599 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003600 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003601 {
3602#ifdef FEAT_MBYTE
3603 if (has_mbyte)
3604 {
3605 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3606 if (!vim_iswordc(mb_ptr2char(w - len)))
3607 break;
3608 w -= len;
3609 }
3610 else
3611#endif
3612 {
3613 if (!vim_iswordc(w[-1]))
3614 break;
3615 --w;
3616 }
3617 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003618 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003619 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3620 p += len;
3621 }
3622
3623 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003624 if (allocated)
3625 vim_free(arg);
3626 return OK;
3627 }
3628
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003629 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003630}
3631
3632/*
3633 * Put a string on the command line.
3634 * When "literally" is TRUE, insert literally.
3635 * When "literally" is FALSE, insert as typed, but don't leave the command
3636 * line.
3637 */
3638 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003639cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003640{
3641 int c, cv;
3642
3643 if (literally)
3644 put_on_cmdline(s, -1, TRUE);
3645 else
3646 while (*s != NUL)
3647 {
3648 cv = *s;
3649 if (cv == Ctrl_V && s[1])
3650 ++s;
3651#ifdef FEAT_MBYTE
3652 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003653 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003654 else
3655#endif
3656 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003657 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3658 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003659#ifdef UNIX
3660 || c == intr_char
3661#endif
3662 || (c == Ctrl_BSL && *s == Ctrl_N))
3663 stuffcharReadbuff(Ctrl_V);
3664 stuffcharReadbuff(c);
3665 }
3666}
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668#ifdef FEAT_WILDMENU
3669/*
3670 * Delete characters on the command line, from "from" to the current
3671 * position.
3672 */
3673 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003674cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3677 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3678 ccline.cmdlen -= ccline.cmdpos - from;
3679 ccline.cmdpos = from;
3680}
3681#endif
3682
3683/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003684 * This function is called when the screen size changes and with incremental
3685 * search and in other situations where the command line may have been
3686 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 */
3688 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003689redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003691 redrawcmdline_ex(TRUE);
3692}
3693
3694 void
3695redrawcmdline_ex(int do_compute_cmdrow)
3696{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (cmd_silent)
3698 return;
3699 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003700 if (do_compute_cmdrow)
3701 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 redrawcmd();
3703 cursorcmd();
3704}
3705
3706 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003707redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708{
3709 int i;
3710
3711 if (cmd_silent)
3712 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003713 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 msg_putchar(ccline.cmdfirstc);
3715 if (ccline.cmdprompt != NULL)
3716 {
3717 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3718 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3719 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003720 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 --ccline.cmdindent;
3722 }
3723 else
3724 for (i = ccline.cmdindent; i > 0; --i)
3725 msg_putchar(' ');
3726}
3727
3728/*
3729 * Redraw what is currently on the command line.
3730 */
3731 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003732redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
3734 if (cmd_silent)
3735 return;
3736
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003737 /* when 'incsearch' is set there may be no command line while redrawing */
3738 if (ccline.cmdbuff == NULL)
3739 {
3740 windgoto(cmdline_row, 0);
3741 msg_clr_eos();
3742 return;
3743 }
3744
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 msg_start();
3746 redrawcmdprompt();
3747
3748 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3749 msg_no_more = TRUE;
3750 draw_cmdline(0, ccline.cmdlen);
3751 msg_clr_eos();
3752 msg_no_more = FALSE;
3753
3754 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003755 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003756 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
3758 /*
3759 * An emsg() before may have set msg_scroll. This is used in normal mode,
3760 * in cmdline mode we can reset them now.
3761 */
3762 msg_scroll = FALSE; /* next message overwrites cmdline */
3763
3764 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3765 * in cmdline mode */
3766 skip_redraw = FALSE;
3767}
3768
3769 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003770compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003772 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 cmdline_row = Rows - 1;
3774 else
3775 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003776 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777}
3778
3779 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003780cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781{
3782 if (cmd_silent)
3783 return;
3784
3785#ifdef FEAT_RIGHTLEFT
3786 if (cmdmsg_rl)
3787 {
3788 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3789 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3790 if (msg_row <= 0)
3791 msg_row = Rows - 1;
3792 }
3793 else
3794#endif
3795 {
3796 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3797 msg_col = ccline.cmdspos % (int)Columns;
3798 if (msg_row >= Rows)
3799 msg_row = Rows - 1;
3800 }
3801
3802 windgoto(msg_row, msg_col);
3803#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003804 if (p_imst == IM_ON_THE_SPOT)
3805 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806#endif
3807#ifdef MCH_CURSOR_SHAPE
3808 mch_update_cursor();
3809#endif
3810}
3811
3812 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003813gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814{
3815 msg_start();
3816#ifdef FEAT_RIGHTLEFT
3817 if (cmdmsg_rl)
3818 msg_col = Columns - 1;
3819 else
3820#endif
3821 msg_col = 0; /* always start in column 0 */
3822 if (clr) /* clear the bottom line(s) */
3823 msg_clr_eos(); /* will reset clear_cmdline */
3824 windgoto(cmdline_row, 0);
3825}
3826
3827/*
3828 * Check the word in front of the cursor for an abbreviation.
3829 * Called when the non-id character "c" has been entered.
3830 * When an abbreviation is recognized it is removed from the text with
3831 * backspaces and the replacement string is inserted, followed by "c".
3832 */
3833 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003834ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003836 int spos = 0;
3837
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3839 return FALSE;
3840
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003841 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3842 * Actually accepts any mark. */
3843 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3844 spos++;
3845 if (ccline.cmdlen - spos > 5
3846 && ccline.cmdbuff[spos] == '\''
3847 && ccline.cmdbuff[spos + 2] == ','
3848 && ccline.cmdbuff[spos + 3] == '\'')
3849 spos += 5;
3850 else
3851 /* check abbreviation from the beginning of the commandline */
3852 spos = 0;
3853
3854 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855}
3856
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003857#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3858 static int
3859#ifdef __BORLANDC__
3860_RTLENTRYF
3861#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003862sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003863{
3864 char_u *p1 = *(char_u **)s1;
3865 char_u *p2 = *(char_u **)s2;
3866
3867 if (*p1 != '<' && *p2 == '<') return -1;
3868 if (*p1 == '<' && *p2 != '<') return 1;
3869 return STRCMP(p1, p2);
3870}
3871#endif
3872
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873/*
3874 * Return FAIL if this is not an appropriate context in which to do
3875 * completion of anything, return OK if it is (even if there are no matches).
3876 * For the caller, this means that the character is just passed through like a
3877 * normal character (instead of being expanded). This allows :s/^I^D etc.
3878 */
3879 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003880nextwild(
3881 expand_T *xp,
3882 int type,
3883 int options, /* extra options for ExpandOne() */
3884 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885{
3886 int i, j;
3887 char_u *p1;
3888 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 int difflen;
3890 int v;
3891
3892 if (xp->xp_numfiles == -1)
3893 {
3894 set_expand_context(xp);
3895 cmd_showtail = expand_showtail(xp);
3896 }
3897
3898 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3899 {
3900 beep_flush();
3901 return OK; /* Something illegal on command line */
3902 }
3903 if (xp->xp_context == EXPAND_NOTHING)
3904 {
3905 /* Caller can use the character as a normal char instead */
3906 return FAIL;
3907 }
3908
3909 MSG_PUTS("..."); /* show that we are busy */
3910 out_flush();
3911
3912 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003913 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 if (type == WILD_NEXT || type == WILD_PREV)
3916 {
3917 /*
3918 * Get next/previous match for a previous expanded pattern.
3919 */
3920 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3921 }
3922 else
3923 {
3924 /*
3925 * Translate string into pattern and expand it.
3926 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003927 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3928 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 p2 = NULL;
3930 else
3931 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003932 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003933 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3934 if (escape)
3935 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003936
3937 if (p_wic)
3938 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003939 p2 = ExpandOne(xp, p1,
3940 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003941 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003943 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 if (p2 != NULL && type == WILD_LONGEST)
3945 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003946 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 if (ccline.cmdbuff[i + j] == '*'
3948 || ccline.cmdbuff[i + j] == '?')
3949 break;
3950 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003951 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 }
3953 }
3954 }
3955
3956 if (p2 != NULL && !got_int)
3957 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003958 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003959 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003961 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 xp->xp_pattern = ccline.cmdbuff + i;
3963 }
3964 else
3965 v = OK;
3966 if (v == OK)
3967 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003968 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3969 &ccline.cmdbuff[ccline.cmdpos],
3970 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3971 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 ccline.cmdlen += difflen;
3973 ccline.cmdpos += difflen;
3974 }
3975 }
3976 vim_free(p2);
3977
3978 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003979 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 /* When expanding a ":map" command and no matches are found, assume that
3982 * the key is supposed to be inserted literally */
3983 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3984 return FAIL;
3985
3986 if (xp->xp_numfiles <= 0 && p2 == NULL)
3987 beep_flush();
3988 else if (xp->xp_numfiles == 1)
3989 /* free expanded pattern */
3990 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3991
3992 return OK;
3993}
3994
3995/*
3996 * Do wildcard expansion on the string 'str'.
3997 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003998 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * Return NULL for failure.
4000 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004001 * "orig" is the originally expanded string, copied to allocated memory. It
4002 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4003 * WILD_PREV "orig" should be NULL.
4004 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004005 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4006 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *
4008 * mode = WILD_FREE: just free previously expanded matches
4009 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4010 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4011 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4012 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4013 * mode = WILD_ALL: return all matches concatenated
4014 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004015 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *
4017 * options = WILD_LIST_NOTFOUND: list entries without a match
4018 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4019 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4020 * options = WILD_NO_BEEP: Don't beep for multiple matches
4021 * options = WILD_ADD_SLASH: add a slash after directory names
4022 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4023 * options = WILD_SILENT: don't print warning messages
4024 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004025 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 *
4027 * The variables xp->xp_context and xp->xp_backslash must have been set!
4028 */
4029 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004030ExpandOne(
4031 expand_T *xp,
4032 char_u *str,
4033 char_u *orig, /* allocated copy of original of expanded string */
4034 int options,
4035 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036{
4037 char_u *ss = NULL;
4038 static int findex;
4039 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004040 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 int i;
4042 long_u len;
4043 int non_suf_match; /* number without matching suffix */
4044
4045 /*
4046 * first handle the case of using an old match
4047 */
4048 if (mode == WILD_NEXT || mode == WILD_PREV)
4049 {
4050 if (xp->xp_numfiles > 0)
4051 {
4052 if (mode == WILD_PREV)
4053 {
4054 if (findex == -1)
4055 findex = xp->xp_numfiles;
4056 --findex;
4057 }
4058 else /* mode == WILD_NEXT */
4059 ++findex;
4060
4061 /*
4062 * When wrapping around, return the original string, set findex to
4063 * -1.
4064 */
4065 if (findex < 0)
4066 {
4067 if (orig_save == NULL)
4068 findex = xp->xp_numfiles - 1;
4069 else
4070 findex = -1;
4071 }
4072 if (findex >= xp->xp_numfiles)
4073 {
4074 if (orig_save == NULL)
4075 findex = 0;
4076 else
4077 findex = -1;
4078 }
4079#ifdef FEAT_WILDMENU
4080 if (p_wmnu)
4081 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4082 findex, cmd_showtail);
4083#endif
4084 if (findex == -1)
4085 return vim_strsave(orig_save);
4086 return vim_strsave(xp->xp_files[findex]);
4087 }
4088 else
4089 return NULL;
4090 }
4091
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004092 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4094 {
4095 FreeWild(xp->xp_numfiles, xp->xp_files);
4096 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004097 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099 findex = 0;
4100
4101 if (mode == WILD_FREE) /* only release file name */
4102 return NULL;
4103
4104 if (xp->xp_numfiles == -1)
4105 {
4106 vim_free(orig_save);
4107 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004108 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109
4110 /*
4111 * Do the expansion.
4112 */
4113 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4114 options) == FAIL)
4115 {
4116#ifdef FNAME_ILLEGAL
4117 /* Illegal file name has been silently skipped. But when there
4118 * are wildcards, the real problem is that there was no match,
4119 * causing the pattern to be added, which has illegal characters.
4120 */
4121 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4122 EMSG2(_(e_nomatch2), str);
4123#endif
4124 }
4125 else if (xp->xp_numfiles == 0)
4126 {
4127 if (!(options & WILD_SILENT))
4128 EMSG2(_(e_nomatch2), str);
4129 }
4130 else
4131 {
4132 /* Escape the matches for use on the command line. */
4133 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4134
4135 /*
4136 * Check for matching suffixes in file names.
4137 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004138 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4139 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 {
4141 if (xp->xp_numfiles)
4142 non_suf_match = xp->xp_numfiles;
4143 else
4144 non_suf_match = 1;
4145 if ((xp->xp_context == EXPAND_FILES
4146 || xp->xp_context == EXPAND_DIRECTORIES)
4147 && xp->xp_numfiles > 1)
4148 {
4149 /*
4150 * More than one match; check suffix.
4151 * The files will have been sorted on matching suffix in
4152 * expand_wildcards, only need to check the first two.
4153 */
4154 non_suf_match = 0;
4155 for (i = 0; i < 2; ++i)
4156 if (match_suffix(xp->xp_files[i]))
4157 ++non_suf_match;
4158 }
4159 if (non_suf_match != 1)
4160 {
4161 /* Can we ever get here unless it's while expanding
4162 * interactively? If not, we can get rid of this all
4163 * together. Don't really want to wait for this message
4164 * (and possibly have to hit return to continue!).
4165 */
4166 if (!(options & WILD_SILENT))
4167 EMSG(_(e_toomany));
4168 else if (!(options & WILD_NO_BEEP))
4169 beep_flush();
4170 }
4171 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4172 ss = vim_strsave(xp->xp_files[0]);
4173 }
4174 }
4175 }
4176
4177 /* Find longest common part */
4178 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4179 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004180 int mb_len = 1;
4181 int c0, ci;
4182
4183 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004185#ifdef FEAT_MBYTE
4186 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004188 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4189 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4190 }
4191 else
4192#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004193 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004194 for (i = 1; i < xp->xp_numfiles; ++i)
4195 {
4196#ifdef FEAT_MBYTE
4197 if (has_mbyte)
4198 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4199 else
4200#endif
4201 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004202 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004204 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004205 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004207 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 break;
4209 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004210 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 break;
4212 }
4213 if (i < xp->xp_numfiles)
4214 {
4215 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004216 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 break;
4218 }
4219 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004220
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 ss = alloc((unsigned)len + 1);
4222 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004223 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 findex = -1; /* next p_wc gets first one */
4225 }
4226
4227 /* Concatenate all matching names */
4228 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4229 {
4230 len = 0;
4231 for (i = 0; i < xp->xp_numfiles; ++i)
4232 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4233 ss = lalloc(len, TRUE);
4234 if (ss != NULL)
4235 {
4236 *ss = NUL;
4237 for (i = 0; i < xp->xp_numfiles; ++i)
4238 {
4239 STRCAT(ss, xp->xp_files[i]);
4240 if (i != xp->xp_numfiles - 1)
4241 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4242 }
4243 }
4244 }
4245
4246 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4247 ExpandCleanup(xp);
4248
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004249 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004250 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004251 vim_free(orig);
4252
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 return ss;
4254}
4255
4256/*
4257 * Prepare an expand structure for use.
4258 */
4259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004260ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004262 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004263 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004265#ifndef BACKSLASH_IN_FILENAME
4266 xp->xp_shell = FALSE;
4267#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 xp->xp_numfiles = -1;
4269 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004270#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4271 xp->xp_arg = NULL;
4272#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004273 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274}
4275
4276/*
4277 * Cleanup an expand structure after use.
4278 */
4279 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004280ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281{
4282 if (xp->xp_numfiles >= 0)
4283 {
4284 FreeWild(xp->xp_numfiles, xp->xp_files);
4285 xp->xp_numfiles = -1;
4286 }
4287}
4288
4289 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004290ExpandEscape(
4291 expand_T *xp,
4292 char_u *str,
4293 int numfiles,
4294 char_u **files,
4295 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296{
4297 int i;
4298 char_u *p;
4299
4300 /*
4301 * May change home directory back to "~"
4302 */
4303 if (options & WILD_HOME_REPLACE)
4304 tilde_replace(str, numfiles, files);
4305
4306 if (options & WILD_ESCAPE)
4307 {
4308 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004309 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004310 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 || xp->xp_context == EXPAND_BUFFERS
4312 || xp->xp_context == EXPAND_DIRECTORIES)
4313 {
4314 /*
4315 * Insert a backslash into a file name before a space, \, %, #
4316 * and wildmatch characters, except '~'.
4317 */
4318 for (i = 0; i < numfiles; ++i)
4319 {
4320 /* for ":set path=" we need to escape spaces twice */
4321 if (xp->xp_backslash == XP_BS_THREE)
4322 {
4323 p = vim_strsave_escaped(files[i], (char_u *)" ");
4324 if (p != NULL)
4325 {
4326 vim_free(files[i]);
4327 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004328#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 p = vim_strsave_escaped(files[i], (char_u *)" ");
4330 if (p != NULL)
4331 {
4332 vim_free(files[i]);
4333 files[i] = p;
4334 }
4335#endif
4336 }
4337 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004338#ifdef BACKSLASH_IN_FILENAME
4339 p = vim_strsave_fnameescape(files[i], FALSE);
4340#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004341 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004342#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 if (p != NULL)
4344 {
4345 vim_free(files[i]);
4346 files[i] = p;
4347 }
4348
4349 /* If 'str' starts with "\~", replace "~" at start of
4350 * files[i] with "\~". */
4351 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004352 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004355
4356 /* If the first file starts with a '+' escape it. Otherwise it
4357 * could be seen as "+cmd". */
4358 if (*files[0] == '+')
4359 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 else if (xp->xp_context == EXPAND_TAGS)
4362 {
4363 /*
4364 * Insert a backslash before characters in a tag name that
4365 * would terminate the ":tag" command.
4366 */
4367 for (i = 0; i < numfiles; ++i)
4368 {
4369 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4370 if (p != NULL)
4371 {
4372 vim_free(files[i]);
4373 files[i] = p;
4374 }
4375 }
4376 }
4377 }
4378}
4379
4380/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004381 * Escape special characters in "fname" for when used as a file name argument
4382 * after a Vim command, or, when "shell" is non-zero, a shell command.
4383 * Returns the result in allocated memory.
4384 */
4385 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004386vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004387{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004388 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004389#ifdef BACKSLASH_IN_FILENAME
4390 char_u buf[20];
4391 int j = 0;
4392
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004393 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004394 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004395 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004396 buf[j++] = *p;
4397 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004398 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004399#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004400 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4401 if (shell && csh_like_shell() && p != NULL)
4402 {
4403 char_u *s;
4404
4405 /* For csh and similar shells need to put two backslashes before '!'.
4406 * One is taken by Vim, one by the shell. */
4407 s = vim_strsave_escaped(p, (char_u *)"!");
4408 vim_free(p);
4409 p = s;
4410 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004411#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004412
4413 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4414 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004415 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004416 escape_fname(&p);
4417
4418 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004419}
4420
4421/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004422 * Put a backslash before the file name in "pp", which is in allocated memory.
4423 */
4424 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004425escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004426{
4427 char_u *p;
4428
4429 p = alloc((unsigned)(STRLEN(*pp) + 2));
4430 if (p != NULL)
4431 {
4432 p[0] = '\\';
4433 STRCPY(p + 1, *pp);
4434 vim_free(*pp);
4435 *pp = p;
4436 }
4437}
4438
4439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * For each file name in files[num_files]:
4441 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4442 */
4443 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004444tilde_replace(
4445 char_u *orig_pat,
4446 int num_files,
4447 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
4449 int i;
4450 char_u *p;
4451
4452 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4453 {
4454 for (i = 0; i < num_files; ++i)
4455 {
4456 p = home_replace_save(NULL, files[i]);
4457 if (p != NULL)
4458 {
4459 vim_free(files[i]);
4460 files[i] = p;
4461 }
4462 }
4463 }
4464}
4465
4466/*
4467 * Show all matches for completion on the command line.
4468 * Returns EXPAND_NOTHING when the character that triggered expansion should
4469 * be inserted like a normal character.
4470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004472showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473{
4474#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4475 int num_files;
4476 char_u **files_found;
4477 int i, j, k;
4478 int maxlen;
4479 int lines;
4480 int columns;
4481 char_u *p;
4482 int lastlen;
4483 int attr;
4484 int showtail;
4485
4486 if (xp->xp_numfiles == -1)
4487 {
4488 set_expand_context(xp);
4489 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4490 &num_files, &files_found);
4491 showtail = expand_showtail(xp);
4492 if (i != EXPAND_OK)
4493 return i;
4494
4495 }
4496 else
4497 {
4498 num_files = xp->xp_numfiles;
4499 files_found = xp->xp_files;
4500 showtail = cmd_showtail;
4501 }
4502
4503#ifdef FEAT_WILDMENU
4504 if (!wildmenu)
4505 {
4506#endif
4507 msg_didany = FALSE; /* lines_left will be set */
4508 msg_start(); /* prepare for paging */
4509 msg_putchar('\n');
4510 out_flush();
4511 cmdline_row = msg_row;
4512 msg_didany = FALSE; /* lines_left will be set again */
4513 msg_start(); /* prepare for paging */
4514#ifdef FEAT_WILDMENU
4515 }
4516#endif
4517
4518 if (got_int)
4519 got_int = FALSE; /* only int. the completion, not the cmd line */
4520#ifdef FEAT_WILDMENU
4521 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004522 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523#endif
4524 else
4525 {
4526 /* find the length of the longest file name */
4527 maxlen = 0;
4528 for (i = 0; i < num_files; ++i)
4529 {
4530 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004531 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 || xp->xp_context == EXPAND_BUFFERS))
4533 {
4534 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4535 j = vim_strsize(NameBuff);
4536 }
4537 else
4538 j = vim_strsize(L_SHOWFILE(i));
4539 if (j > maxlen)
4540 maxlen = j;
4541 }
4542
4543 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4544 lines = num_files;
4545 else
4546 {
4547 /* compute the number of columns and lines for the listing */
4548 maxlen += 2; /* two spaces between file names */
4549 columns = ((int)Columns + 2) / maxlen;
4550 if (columns < 1)
4551 columns = 1;
4552 lines = (num_files + columns - 1) / columns;
4553 }
4554
Bram Moolenaar8820b482017-03-16 17:23:31 +01004555 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556
4557 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4558 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004559 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 msg_clr_eos();
4561 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004562 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 }
4564
4565 /* list the files line by line */
4566 for (i = 0; i < lines; ++i)
4567 {
4568 lastlen = 999;
4569 for (k = i; k < num_files; k += lines)
4570 {
4571 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4572 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004573 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 p = files_found[k] + STRLEN(files_found[k]) + 1;
4575 msg_advance(maxlen + 1);
4576 msg_puts(p);
4577 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004578 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 break;
4580 }
4581 for (j = maxlen - lastlen; --j >= 0; )
4582 msg_putchar(' ');
4583 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004584 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 || xp->xp_context == EXPAND_BUFFERS)
4586 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004587 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004588 if (xp->xp_numfiles != -1)
4589 {
4590 char_u *halved_slash;
4591 char_u *exp_path;
4592
4593 /* Expansion was done before and special characters
4594 * were escaped, need to halve backslashes. Also
4595 * $HOME has been replaced with ~/. */
4596 exp_path = expand_env_save_opt(files_found[k], TRUE);
4597 halved_slash = backslash_halve_save(
4598 exp_path != NULL ? exp_path : files_found[k]);
4599 j = mch_isdir(halved_slash != NULL ? halved_slash
4600 : files_found[k]);
4601 vim_free(exp_path);
4602 vim_free(halved_slash);
4603 }
4604 else
4605 /* Expansion was done here, file names are literal. */
4606 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 if (showtail)
4608 p = L_SHOWFILE(k);
4609 else
4610 {
4611 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4612 TRUE);
4613 p = NameBuff;
4614 }
4615 }
4616 else
4617 {
4618 j = FALSE;
4619 p = L_SHOWFILE(k);
4620 }
4621 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4622 }
4623 if (msg_col > 0) /* when not wrapped around */
4624 {
4625 msg_clr_eos();
4626 msg_putchar('\n');
4627 }
4628 out_flush(); /* show one line at a time */
4629 if (got_int)
4630 {
4631 got_int = FALSE;
4632 break;
4633 }
4634 }
4635
4636 /*
4637 * we redraw the command below the lines that we have just listed
4638 * This is a bit tricky, but it saves a lot of screen updating.
4639 */
4640 cmdline_row = msg_row; /* will put it back later */
4641 }
4642
4643 if (xp->xp_numfiles == -1)
4644 FreeWild(num_files, files_found);
4645
4646 return EXPAND_OK;
4647}
4648
4649/*
4650 * Private gettail for showmatches() (and win_redr_status_matches()):
4651 * Find tail of file name path, but ignore trailing "/".
4652 */
4653 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004654sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655{
4656 char_u *p;
4657 char_u *t = s;
4658 int had_sep = FALSE;
4659
4660 for (p = s; *p != NUL; )
4661 {
4662 if (vim_ispathsep(*p)
4663#ifdef BACKSLASH_IN_FILENAME
4664 && !rem_backslash(p)
4665#endif
4666 )
4667 had_sep = TRUE;
4668 else if (had_sep)
4669 {
4670 t = p;
4671 had_sep = FALSE;
4672 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004673 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 return t;
4676}
4677
4678/*
4679 * Return TRUE if we only need to show the tail of completion matches.
4680 * When not completing file names or there is a wildcard in the path FALSE is
4681 * returned.
4682 */
4683 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004684expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 char_u *s;
4687 char_u *end;
4688
4689 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004690 if (xp->xp_context != EXPAND_FILES
4691 && xp->xp_context != EXPAND_SHELLCMD
4692 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 return FALSE;
4694
4695 end = gettail(xp->xp_pattern);
4696 if (end == xp->xp_pattern) /* there is no path separator */
4697 return FALSE;
4698
4699 for (s = xp->xp_pattern; s < end; s++)
4700 {
4701 /* Skip escaped wildcards. Only when the backslash is not a path
4702 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4703 if (rem_backslash(s))
4704 ++s;
4705 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4706 return FALSE;
4707 }
4708 return TRUE;
4709}
4710
4711/*
4712 * Prepare a string for expansion.
4713 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004714 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 * When expanding other names: The string will be used with regcomp(). Copy
4716 * the name into allocated memory and prepend "^".
4717 */
4718 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004719addstar(
4720 char_u *fname,
4721 int len,
4722 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723{
4724 char_u *retval;
4725 int i, j;
4726 int new_len;
4727 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004728 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004730 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004731 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004732 && context != EXPAND_SHELLCMD
4733 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
4735 /*
4736 * Matching will be done internally (on something other than files).
4737 * So we convert the file-matching-type wildcards into our kind for
4738 * use with vim_regcomp(). First work out how long it will be:
4739 */
4740
4741 /* For help tags the translation is done in find_help_tags().
4742 * For a tag pattern starting with "/" no translation is needed. */
4743 if (context == EXPAND_HELP
4744 || context == EXPAND_COLORS
4745 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004746 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004747 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004748 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004749 || ((context == EXPAND_TAGS_LISTFILES
4750 || context == EXPAND_TAGS)
4751 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 retval = vim_strnsave(fname, len);
4753 else
4754 {
4755 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4756 for (i = 0; i < len; i++)
4757 {
4758 if (fname[i] == '*' || fname[i] == '~')
4759 new_len++; /* '*' needs to be replaced by ".*"
4760 '~' needs to be replaced by "\~" */
4761
4762 /* Buffer names are like file names. "." should be literal */
4763 if (context == EXPAND_BUFFERS && fname[i] == '.')
4764 new_len++; /* "." becomes "\." */
4765
4766 /* Custom expansion takes care of special things, match
4767 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004768 if ((context == EXPAND_USER_DEFINED
4769 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 new_len++; /* '\' becomes "\\" */
4771 }
4772 retval = alloc(new_len);
4773 if (retval != NULL)
4774 {
4775 retval[0] = '^';
4776 j = 1;
4777 for (i = 0; i < len; i++, j++)
4778 {
4779 /* Skip backslash. But why? At least keep it for custom
4780 * expansion. */
4781 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004782 && context != EXPAND_USER_LIST
4783 && fname[i] == '\\'
4784 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 break;
4786
4787 switch (fname[i])
4788 {
4789 case '*': retval[j++] = '.';
4790 break;
4791 case '~': retval[j++] = '\\';
4792 break;
4793 case '?': retval[j] = '.';
4794 continue;
4795 case '.': if (context == EXPAND_BUFFERS)
4796 retval[j++] = '\\';
4797 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004798 case '\\': if (context == EXPAND_USER_DEFINED
4799 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 retval[j++] = '\\';
4801 break;
4802 }
4803 retval[j] = fname[i];
4804 }
4805 retval[j] = NUL;
4806 }
4807 }
4808 }
4809 else
4810 {
4811 retval = alloc(len + 4);
4812 if (retval != NULL)
4813 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004814 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004817 * Don't add a star to *, ~, ~user, $var or `cmd`.
4818 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 * ~ would be at the start of the file name, but not the tail.
4820 * $ could be anywhere in the tail.
4821 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004822 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 */
4824 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004825 ends_in_star = (len > 0 && retval[len - 1] == '*');
4826#ifndef BACKSLASH_IN_FILENAME
4827 for (i = len - 2; i >= 0; --i)
4828 {
4829 if (retval[i] != '\\')
4830 break;
4831 ends_in_star = !ends_in_star;
4832 }
4833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004835 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 && vim_strchr(tail, '$') == NULL
4837 && vim_strchr(retval, '`') == NULL)
4838 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004839 else if (len > 0 && retval[len - 1] == '$')
4840 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 retval[len] = NUL;
4842 }
4843 }
4844 return retval;
4845}
4846
4847/*
4848 * Must parse the command line so far to work out what context we are in.
4849 * Completion can then be done based on that context.
4850 * This routine sets the variables:
4851 * xp->xp_pattern The start of the pattern to be expanded within
4852 * the command line (ends at the cursor).
4853 * xp->xp_context The type of thing to expand. Will be one of:
4854 *
4855 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4856 * the command line, like an unknown command. Caller
4857 * should beep.
4858 * EXPAND_NOTHING Unrecognised context for completion, use char like
4859 * a normal char, rather than for completion. eg
4860 * :s/^I/
4861 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4862 * it.
4863 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4864 * EXPAND_FILES After command with XFILE set, or after setting
4865 * with P_EXPAND set. eg :e ^I, :w>>^I
4866 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4867 * when we know only directories are of interest. eg
4868 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004869 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4871 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4872 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4873 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4874 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4875 * EXPAND_EVENTS Complete event names
4876 * EXPAND_SYNTAX Complete :syntax command arguments
4877 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4878 * EXPAND_AUGROUP Complete autocommand group names
4879 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4880 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4881 * eg :unmap a^I , :cunab x^I
4882 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4883 * eg :call sub^I
4884 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4885 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4886 * names in expressions, eg :while s^I
4887 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004888 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 */
4890 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004891set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004893 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (ccline.cmdfirstc != ':'
4895#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004896 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004897 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898#endif
4899 )
4900 {
4901 xp->xp_context = EXPAND_NOTHING;
4902 return;
4903 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004904 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905}
4906
4907 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004908set_cmd_context(
4909 expand_T *xp,
4910 char_u *str, /* start of command line */
4911 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004912 int col, /* position of cursor */
4913 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 int old_char = NUL;
4916 char_u *nextcomm;
4917
4918 /*
4919 * Avoid a UMR warning from Purify, only save the character if it has been
4920 * written before.
4921 */
4922 if (col < len)
4923 old_char = str[col];
4924 str[col] = NUL;
4925 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004926
4927#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004928 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004929 {
4930# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004931 /* pass CMD_SIZE because there is no real command */
4932 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004933# endif
4934 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004935 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004936 {
4937 xp->xp_context = ccline.xp_context;
4938 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004939# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004940 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004941# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004942 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004943 else
4944#endif
4945 while (nextcomm != NULL)
4946 nextcomm = set_one_cmd_context(xp, nextcomm);
4947
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004948 /* Store the string here so that call_user_expand_func() can get to them
4949 * easily. */
4950 xp->xp_line = str;
4951 xp->xp_col = col;
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 str[col] = old_char;
4954}
4955
4956/*
4957 * Expand the command line "str" from context "xp".
4958 * "xp" must have been set by set_cmd_context().
4959 * xp->xp_pattern points into "str", to where the text that is to be expanded
4960 * starts.
4961 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4962 * cursor.
4963 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4964 * key that triggered expansion literally.
4965 * Returns EXPAND_OK otherwise.
4966 */
4967 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004968expand_cmdline(
4969 expand_T *xp,
4970 char_u *str, /* start of command line */
4971 int col, /* position of cursor */
4972 int *matchcount, /* return: nr of matches */
4973 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004976 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4979 {
4980 beep_flush();
4981 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4982 }
4983 if (xp->xp_context == EXPAND_NOTHING)
4984 {
4985 /* Caller can use the character as a normal char instead */
4986 return EXPAND_NOTHING;
4987 }
4988
4989 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004990 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4991 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 if (file_str == NULL)
4993 return EXPAND_UNSUCCESSFUL;
4994
Bram Moolenaar94950a92010-12-02 16:01:29 +01004995 if (p_wic)
4996 options += WILD_ICASE;
4997
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004999 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
5001 *matchcount = 0;
5002 *matches = NULL;
5003 }
5004 vim_free(file_str);
5005
5006 return EXPAND_OK;
5007}
5008
5009#ifdef FEAT_MULTI_LANG
5010/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005011 * Cleanup matches for help tags:
5012 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5013 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005015static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016
5017 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005018cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019{
5020 int i, j;
5021 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005022 char_u buf[4];
5023 char_u *p = buf;
5024
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005025 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005026 {
5027 *p++ = '@';
5028 *p++ = p_hlg[0];
5029 *p++ = p_hlg[1];
5030 }
5031 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
5033 for (i = 0; i < num_file; ++i)
5034 {
5035 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005036 if (len <= 0)
5037 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005038 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
5040 /* Sorting on priority means the same item in another language may
5041 * be anywhere. Search all items for a match up to the "@en". */
5042 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005043 if (j != i && (int)STRLEN(file[j]) == len + 3
5044 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 break;
5046 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005047 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 file[i][len] = NUL;
5049 }
5050 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005051
5052 if (*buf != NUL)
5053 for (i = 0; i < num_file; ++i)
5054 {
5055 len = (int)STRLEN(file[i]) - 3;
5056 if (len <= 0)
5057 continue;
5058 if (STRCMP(file[i] + len, buf) == 0)
5059 {
5060 /* remove the default language */
5061 file[i][len] = NUL;
5062 }
5063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064}
5065#endif
5066
5067/*
5068 * Do the expansion based on xp->xp_context and "pat".
5069 */
5070 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005071ExpandFromContext(
5072 expand_T *xp,
5073 char_u *pat,
5074 int *num_file,
5075 char_u ***file,
5076 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077{
5078#ifdef FEAT_CMDL_COMPL
5079 regmatch_T regmatch;
5080#endif
5081 int ret;
5082 int flags;
5083
5084 flags = EW_DIR; /* include directories */
5085 if (options & WILD_LIST_NOTFOUND)
5086 flags |= EW_NOTFOUND;
5087 if (options & WILD_ADD_SLASH)
5088 flags |= EW_ADDSLASH;
5089 if (options & WILD_KEEP_ALL)
5090 flags |= EW_KEEPALL;
5091 if (options & WILD_SILENT)
5092 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005093 if (options & WILD_ALLLINKS)
5094 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005096 if (xp->xp_context == EXPAND_FILES
5097 || xp->xp_context == EXPAND_DIRECTORIES
5098 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
5100 /*
5101 * Expand file or directory names.
5102 */
5103 int free_pat = FALSE;
5104 int i;
5105
5106 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5107 * space */
5108 if (xp->xp_backslash != XP_BS_NONE)
5109 {
5110 free_pat = TRUE;
5111 pat = vim_strsave(pat);
5112 for (i = 0; pat[i]; ++i)
5113 if (pat[i] == '\\')
5114 {
5115 if (xp->xp_backslash == XP_BS_THREE
5116 && pat[i + 1] == '\\'
5117 && pat[i + 2] == '\\'
5118 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005119 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (xp->xp_backslash == XP_BS_ONE
5121 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005122 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 }
5125
5126 if (xp->xp_context == EXPAND_FILES)
5127 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005128 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5129 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 else
5131 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005132 if (options & WILD_ICASE)
5133 flags |= EW_ICASE;
5134
Bram Moolenaard7834d32009-12-02 16:14:36 +00005135 /* Expand wildcards, supporting %:h and the like. */
5136 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (free_pat)
5138 vim_free(pat);
5139 return ret;
5140 }
5141
5142 *file = (char_u **)"";
5143 *num_file = 0;
5144 if (xp->xp_context == EXPAND_HELP)
5145 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005146 /* With an empty argument we would get all the help tags, which is
5147 * very slow. Get matches for "help" instead. */
5148 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5149 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151#ifdef FEAT_MULTI_LANG
5152 cleanup_help_tags(*num_file, *file);
5153#endif
5154 return OK;
5155 }
5156 return FAIL;
5157 }
5158
5159#ifndef FEAT_CMDL_COMPL
5160 return FAIL;
5161#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005162 if (xp->xp_context == EXPAND_SHELLCMD)
5163 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 if (xp->xp_context == EXPAND_OLD_SETTING)
5165 return ExpandOldSetting(num_file, file);
5166 if (xp->xp_context == EXPAND_BUFFERS)
5167 return ExpandBufnames(pat, num_file, file, options);
5168 if (xp->xp_context == EXPAND_TAGS
5169 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5170 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5171 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005172 {
5173 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005174 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5175 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005178 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005179 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005180 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005181 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005182 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005183 {
5184 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005185 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005186 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005187 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005188 {
5189 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005190 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005191 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005192# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5193 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005194 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005195# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005196 if (xp->xp_context == EXPAND_PACKADD)
5197 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198
5199 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5200 if (regmatch.regprog == NULL)
5201 return FAIL;
5202
5203 /* set ignore-case according to p_ic, p_scs and pat */
5204 regmatch.rm_ic = ignorecase(pat);
5205
5206 if (xp->xp_context == EXPAND_SETTINGS
5207 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5208 ret = ExpandSettings(xp, &regmatch, num_file, file);
5209 else if (xp->xp_context == EXPAND_MAPPINGS)
5210 ret = ExpandMappings(&regmatch, num_file, file);
5211# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5212 else if (xp->xp_context == EXPAND_USER_DEFINED)
5213 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5214# endif
5215 else
5216 {
5217 static struct expgen
5218 {
5219 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005220 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005222 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 } tab[] =
5224 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005225 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5226 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005227 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005228 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005229#ifdef FEAT_CMDHIST
5230 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005233 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005234 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005235 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5236 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5237 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238#endif
5239#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005240 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5241 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5242 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5243 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244#endif
5245#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005246 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5247 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248#endif
5249#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005250 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005252#ifdef FEAT_PROFILE
5253 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5254#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005255 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005256 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5257 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005258#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005259 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005260#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005261#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005262 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005263#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005264#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005265 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005266#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5268 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005269 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5270 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005272 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005273 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005274 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 };
5276 int i;
5277
5278 /*
5279 * Find a context in the table and call the ExpandGeneric() with the
5280 * right function to do the expansion.
5281 */
5282 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005283 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 if (xp->xp_context == tab[i].context)
5285 {
5286 if (tab[i].ic)
5287 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005288 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005289 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 break;
5291 }
5292 }
5293
Bram Moolenaar473de612013-06-08 18:19:48 +02005294 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295
5296 return ret;
5297#endif /* FEAT_CMDL_COMPL */
5298}
5299
5300#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5301/*
5302 * Expand a list of names.
5303 *
5304 * Generic function for command line completion. It calls a function to
5305 * obtain strings, one by one. The strings are matched against a regexp
5306 * program. Matching strings are copied into an array, which is returned.
5307 *
5308 * Returns OK when no problems encountered, FAIL for error (out of memory).
5309 */
5310 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005311ExpandGeneric(
5312 expand_T *xp,
5313 regmatch_T *regmatch,
5314 int *num_file,
5315 char_u ***file,
5316 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005318 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319{
5320 int i;
5321 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005322 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 char_u *str;
5324
5325 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005326 * round == 0: count the number of matching names
5327 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005329 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 {
5331 for (i = 0; ; ++i)
5332 {
5333 str = (*func)(xp, i);
5334 if (str == NULL) /* end of list */
5335 break;
5336 if (*str == NUL) /* skip empty strings */
5337 continue;
5338
5339 if (vim_regexec(regmatch, str, (colnr_T)0))
5340 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005341 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005343 if (escaped)
5344 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5345 else
5346 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 (*file)[count] = str;
5348#ifdef FEAT_MENU
5349 if (func == get_menu_names && str != NULL)
5350 {
5351 /* test for separator added by get_menu_names() */
5352 str += STRLEN(str) - 1;
5353 if (*str == '\001')
5354 *str = '.';
5355 }
5356#endif
5357 }
5358 ++count;
5359 }
5360 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005361 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 {
5363 if (count == 0)
5364 return OK;
5365 *num_file = count;
5366 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5367 if (*file == NULL)
5368 {
5369 *file = (char_u **)"";
5370 return FAIL;
5371 }
5372 count = 0;
5373 }
5374 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005375
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005376 /* Sort the results. Keep menu's in the specified order. */
5377 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005378 {
5379 if (xp->xp_context == EXPAND_EXPRESSION
5380 || xp->xp_context == EXPAND_FUNCTIONS
5381 || xp->xp_context == EXPAND_USER_FUNC)
5382 /* <SNR> functions should be sorted to the end. */
5383 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5384 sort_func_compare);
5385 else
5386 sort_strings(*file, *num_file);
5387 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005388
Bram Moolenaar4f688582007-07-24 12:34:30 +00005389#ifdef FEAT_CMDL_COMPL
5390 /* Reset the variables used for special highlight names expansion, so that
5391 * they don't show up when getting normal highlight names by ID. */
5392 reset_expand_highlight();
5393#endif
5394
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 return OK;
5396}
5397
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005398/*
5399 * Complete a shell command.
5400 * Returns FAIL or OK;
5401 */
5402 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005403expand_shellcmd(
5404 char_u *filepat, /* pattern to match with command names */
5405 int *num_file, /* return: number of matches */
5406 char_u ***file, /* return: array with matches */
5407 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005408{
5409 char_u *pat;
5410 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005411 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005412 int mustfree = FALSE;
5413 garray_T ga;
5414 char_u *buf = alloc(MAXPATHL);
5415 size_t l;
5416 char_u *s, *e;
5417 int flags = flagsarg;
5418 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005419 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005420 hashtab_T found_ht;
5421 hashitem_T *hi;
5422 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005423
5424 if (buf == NULL)
5425 return FAIL;
5426
5427 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5428 * space */
5429 pat = vim_strsave(filepat);
5430 for (i = 0; pat[i]; ++i)
5431 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005432 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005433
Bram Moolenaarb5971142015-03-21 17:32:19 +01005434 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005435
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005436 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5437 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005438 path = (char_u *)".";
5439 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005440 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005441 /* For an absolute name we don't use $PATH. */
5442 if (!mch_isFullName(pat))
5443 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005444 if (path == NULL)
5445 path = (char_u *)"";
5446 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005447
5448 /*
5449 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005450 * collect them in "ga". When "." is not in $PATH also expand for the
5451 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005452 */
5453 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005454 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005455 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005456 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005457#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005458 e = vim_strchr(s, ';');
5459#else
5460 e = vim_strchr(s, ':');
5461#endif
5462 if (e == NULL)
5463 e = s + STRLEN(s);
5464
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005465 if (*s == NUL)
5466 {
5467 if (did_curdir)
5468 break;
5469 // Find directories in the current directory, path is empty.
5470 did_curdir = TRUE;
5471 flags |= EW_DIR;
5472 }
5473 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5474 {
5475 did_curdir = TRUE;
5476 flags |= EW_DIR;
5477 }
5478 else
5479 // Do not match directories inside a $PATH item.
5480 flags &= ~EW_DIR;
5481
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005482 l = e - s;
5483 if (l > MAXPATHL - 5)
5484 break;
5485 vim_strncpy(buf, s, l);
5486 add_pathsep(buf);
5487 l = STRLEN(buf);
5488 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5489
5490 /* Expand matches in one directory of $PATH. */
5491 ret = expand_wildcards(1, &buf, num_file, file, flags);
5492 if (ret == OK)
5493 {
5494 if (ga_grow(&ga, *num_file) == FAIL)
5495 FreeWild(*num_file, *file);
5496 else
5497 {
5498 for (i = 0; i < *num_file; ++i)
5499 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005500 char_u *name = (*file)[i];
5501
5502 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005503 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005504 // Check if this name was already found.
5505 hash = hash_hash(name + l);
5506 hi = hash_lookup(&found_ht, name + l, hash);
5507 if (HASHITEM_EMPTY(hi))
5508 {
5509 // Remove the path that was prepended.
5510 STRMOVE(name, name + l);
5511 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5512 hash_add_item(&found_ht, hi, name, hash);
5513 name = NULL;
5514 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005515 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005516 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005517 }
5518 vim_free(*file);
5519 }
5520 }
5521 if (*e != NUL)
5522 ++e;
5523 }
5524 *file = ga.ga_data;
5525 *num_file = ga.ga_len;
5526
5527 vim_free(buf);
5528 vim_free(pat);
5529 if (mustfree)
5530 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005531 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005532 return OK;
5533}
5534
5535
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5537/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005538 * Call "user_expand_func()" to invoke a user defined Vim script function and
5539 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005541 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005542call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005543 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005544 expand_T *xp,
5545 int *num_file,
5546 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005548 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005549 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005551 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005552 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005553 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554
Bram Moolenaarb7515462013-06-29 12:58:33 +02005555 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005556 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 *num_file = 0;
5558 *file = NULL;
5559
Bram Moolenaarb7515462013-06-29 12:58:33 +02005560 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005561 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005562 keep = ccline.cmdbuff[ccline.cmdlen];
5563 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005564 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005565
Bram Moolenaarffa96842018-06-12 22:05:14 +02005566 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5567
5568 args[0].v_type = VAR_STRING;
5569 args[0].vval.v_string = pat;
5570 args[1].v_type = VAR_STRING;
5571 args[1].vval.v_string = xp->xp_line;
5572 args[2].v_type = VAR_NUMBER;
5573 args[2].vval.v_number = xp->xp_col;
5574 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005576 /* Save the cmdline, we don't know what the function may do. */
5577 save_ccline = ccline;
5578 ccline.cmdbuff = NULL;
5579 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005581
Bram Moolenaarded27a12018-08-01 19:06:03 +02005582 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005583
5584 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005586 if (ccline.cmdbuff != NULL)
5587 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005588
Bram Moolenaarffa96842018-06-12 22:05:14 +02005589 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005590 return ret;
5591}
5592
5593/*
5594 * Expand names with a function defined by the user.
5595 */
5596 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005597ExpandUserDefined(
5598 expand_T *xp,
5599 regmatch_T *regmatch,
5600 int *num_file,
5601 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005602{
5603 char_u *retstr;
5604 char_u *s;
5605 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005606 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005607 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005608 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005609
5610 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5611 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 return FAIL;
5613
5614 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005615 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 e = vim_strchr(s, '\n');
5618 if (e == NULL)
5619 e = s + STRLEN(s);
5620 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005621 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005623 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5624 *e = keep;
5625
5626 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005628 if (ga_grow(&ga, 1) == FAIL)
5629 break;
5630 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5631 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 }
5633
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 if (*e != NUL)
5635 ++e;
5636 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005637 vim_free(retstr);
5638 *file = ga.ga_data;
5639 *num_file = ga.ga_len;
5640 return OK;
5641}
5642
5643/*
5644 * Expand names with a list returned by a function defined by the user.
5645 */
5646 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005647ExpandUserList(
5648 expand_T *xp,
5649 int *num_file,
5650 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005651{
5652 list_T *retlist;
5653 listitem_T *li;
5654 garray_T ga;
5655
5656 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5657 if (retlist == NULL)
5658 return FAIL;
5659
5660 ga_init2(&ga, (int)sizeof(char *), 3);
5661 /* Loop over the items in the list. */
5662 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5663 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005664 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5665 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005666
5667 if (ga_grow(&ga, 1) == FAIL)
5668 break;
5669
5670 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005671 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005672 ++ga.ga_len;
5673 }
5674 list_unref(retlist);
5675
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 *file = ga.ga_data;
5677 *num_file = ga.ga_len;
5678 return OK;
5679}
5680#endif
5681
5682/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005683 * Expand color scheme, compiler or filetype names.
5684 * Search from 'runtimepath':
5685 * 'runtimepath'/{dirnames}/{pat}.vim
5686 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5687 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5688 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5689 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005690 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 */
5692 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005693ExpandRTDir(
5694 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005695 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005696 int *num_file,
5697 char_u ***file,
5698 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 char_u *s;
5701 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005702 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005704 int i;
5705 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706
5707 *num_file = 0;
5708 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005709 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005710 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005712 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005714 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5715 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005717 ga_clear_strings(&ga);
5718 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005720 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005721 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005722 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005724
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005725 if (flags & DIP_START) {
5726 for (i = 0; dirnames[i] != NULL; ++i)
5727 {
5728 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5729 if (s == NULL)
5730 {
5731 ga_clear_strings(&ga);
5732 return FAIL;
5733 }
5734 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5735 globpath(p_pp, s, &ga, 0);
5736 vim_free(s);
5737 }
5738 }
5739
5740 if (flags & DIP_OPT) {
5741 for (i = 0; dirnames[i] != NULL; ++i)
5742 {
5743 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5744 if (s == NULL)
5745 {
5746 ga_clear_strings(&ga);
5747 return FAIL;
5748 }
5749 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5750 globpath(p_pp, s, &ga, 0);
5751 vim_free(s);
5752 }
5753 }
5754
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005755 for (i = 0; i < ga.ga_len; ++i)
5756 {
5757 match = ((char_u **)ga.ga_data)[i];
5758 s = match;
5759 e = s + STRLEN(s);
5760 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5761 {
5762 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005763 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005764 if (s < match || vim_ispathsep(*s))
5765 break;
5766 ++s;
5767 *e = NUL;
5768 mch_memmove(match, s, e - s + 1);
5769 }
5770 }
5771
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005772 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005773 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005774
5775 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005776 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005777 remove_duplicates(&ga);
5778
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 *file = ga.ga_data;
5780 *num_file = ga.ga_len;
5781 return OK;
5782}
5783
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005784/*
5785 * Expand loadplugin names:
5786 * 'packpath'/pack/ * /opt/{pat}
5787 */
5788 static int
5789ExpandPackAddDir(
5790 char_u *pat,
5791 int *num_file,
5792 char_u ***file)
5793{
5794 char_u *s;
5795 char_u *e;
5796 char_u *match;
5797 garray_T ga;
5798 int i;
5799 int pat_len;
5800
5801 *num_file = 0;
5802 *file = NULL;
5803 pat_len = (int)STRLEN(pat);
5804 ga_init2(&ga, (int)sizeof(char *), 10);
5805
5806 s = alloc((unsigned)(pat_len + 26));
5807 if (s == NULL)
5808 {
5809 ga_clear_strings(&ga);
5810 return FAIL;
5811 }
5812 sprintf((char *)s, "pack/*/opt/%s*", pat);
5813 globpath(p_pp, s, &ga, 0);
5814 vim_free(s);
5815
5816 for (i = 0; i < ga.ga_len; ++i)
5817 {
5818 match = ((char_u **)ga.ga_data)[i];
5819 s = gettail(match);
5820 e = s + STRLEN(s);
5821 mch_memmove(match, s, e - s + 1);
5822 }
5823
5824 if (ga.ga_len == 0)
5825 return FAIL;
5826
5827 /* Sort and remove duplicates which can happen when specifying multiple
5828 * directories in dirnames. */
5829 remove_duplicates(&ga);
5830
5831 *file = ga.ga_data;
5832 *num_file = ga.ga_len;
5833 return OK;
5834}
5835
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#endif
5837
5838#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5839/*
5840 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005841 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005843 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005844globpath(
5845 char_u *path,
5846 char_u *file,
5847 garray_T *ga,
5848 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849{
5850 expand_T xpc;
5851 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 int num_p;
5854 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856 buf = alloc(MAXPATHL);
5857 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005858 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005860 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005862
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 /* Loop over all entries in {path}. */
5864 while (*path != NUL)
5865 {
5866 /* Copy one item of the path to buf[] and concatenate the file name. */
5867 copy_option_part(&path, buf, MAXPATHL, ",");
5868 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5869 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005870# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005871 /* Using the platform's path separator (\) makes vim incorrectly
5872 * treat it as an escape character, use '/' instead. */
5873 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5874 STRCAT(buf, "/");
5875# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005877# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005879 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5880 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005882 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005884 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 for (i = 0; i < num_p; ++i)
5887 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005888 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005889 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005890 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 FreeWild(num_p, p);
5895 }
5896 }
5897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898
5899 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900}
5901
5902#endif
5903
5904#if defined(FEAT_CMDHIST) || defined(PROTO)
5905
5906/*********************************
5907 * Command line history stuff *
5908 *********************************/
5909
5910/*
5911 * Translate a history character to the associated type number.
5912 */
5913 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005914hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915{
5916 if (c == ':')
5917 return HIST_CMD;
5918 if (c == '=')
5919 return HIST_EXPR;
5920 if (c == '@')
5921 return HIST_INPUT;
5922 if (c == '>')
5923 return HIST_DEBUG;
5924 return HIST_SEARCH; /* must be '?' or '/' */
5925}
5926
5927/*
5928 * Table of history names.
5929 * These names are used in :history and various hist...() functions.
5930 * It is sufficient to give the significant prefix of a history name.
5931 */
5932
5933static char *(history_names[]) =
5934{
5935 "cmd",
5936 "search",
5937 "expr",
5938 "input",
5939 "debug",
5940 NULL
5941};
5942
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005943#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5944/*
5945 * Function given to ExpandGeneric() to obtain the possible first
5946 * arguments of the ":history command.
5947 */
5948 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005949get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005950{
5951 static char_u compl[2] = { NUL, NUL };
5952 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005953 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005954 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5955
5956 if (idx < short_names_count)
5957 {
5958 compl[0] = (char_u)short_names[idx];
5959 return compl;
5960 }
5961 if (idx < short_names_count + history_name_count)
5962 return (char_u *)history_names[idx - short_names_count];
5963 if (idx == short_names_count + history_name_count)
5964 return (char_u *)"all";
5965 return NULL;
5966}
5967#endif
5968
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969/*
5970 * init_history() - Initialize the command line history.
5971 * Also used to re-allocate the history when the size changes.
5972 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005973 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005974init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 int newlen; /* new length of history table */
5977 histentry_T *temp;
5978 int i;
5979 int j;
5980 int type;
5981
5982 /*
5983 * If size of history table changed, reallocate it
5984 */
5985 newlen = (int)p_hi;
5986 if (newlen != hislen) /* history length changed */
5987 {
5988 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5989 {
5990 if (newlen)
5991 {
5992 temp = (histentry_T *)lalloc(
5993 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5994 if (temp == NULL) /* out of memory! */
5995 {
5996 if (type == 0) /* first one: just keep the old length */
5997 {
5998 newlen = hislen;
5999 break;
6000 }
6001 /* Already changed one table, now we can only have zero
6002 * length for all tables. */
6003 newlen = 0;
6004 type = -1;
6005 continue;
6006 }
6007 }
6008 else
6009 temp = NULL;
6010 if (newlen == 0 || temp != NULL)
6011 {
6012 if (hisidx[type] < 0) /* there are no entries yet */
6013 {
6014 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006015 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016 }
6017 else if (newlen > hislen) /* array becomes bigger */
6018 {
6019 for (i = 0; i <= hisidx[type]; ++i)
6020 temp[i] = history[type][i];
6021 j = i;
6022 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006023 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 for ( ; j < hislen; ++i, ++j)
6025 temp[i] = history[type][j];
6026 }
6027 else /* array becomes smaller or 0 */
6028 {
6029 j = hisidx[type];
6030 for (i = newlen - 1; ; --i)
6031 {
6032 if (i >= 0) /* copy newest entries */
6033 temp[i] = history[type][j];
6034 else /* remove older entries */
6035 vim_free(history[type][j].hisstr);
6036 if (--j < 0)
6037 j = hislen - 1;
6038 if (j == hisidx[type])
6039 break;
6040 }
6041 hisidx[type] = newlen - 1;
6042 }
6043 vim_free(history[type]);
6044 history[type] = temp;
6045 }
6046 }
6047 hislen = newlen;
6048 }
6049}
6050
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006051 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006052clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006053{
6054 hisptr->hisnum = 0;
6055 hisptr->viminfo = FALSE;
6056 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006057 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006058}
6059
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060/*
6061 * Check if command line 'str' is already in history.
6062 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6063 */
6064 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006065in_history(
6066 int type,
6067 char_u *str,
6068 int move_to_front, /* Move the entry to the front if it exists */
6069 int sep,
6070 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071{
6072 int i;
6073 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006074 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075
6076 if (hisidx[type] < 0)
6077 return FALSE;
6078 i = hisidx[type];
6079 do
6080 {
6081 if (history[type][i].hisstr == NULL)
6082 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006083
6084 /* For search history, check that the separator character matches as
6085 * well. */
6086 p = history[type][i].hisstr;
6087 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006088 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006089 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090 {
6091 if (!move_to_front)
6092 return TRUE;
6093 last_i = i;
6094 break;
6095 }
6096 if (--i < 0)
6097 i = hislen - 1;
6098 } while (i != hisidx[type]);
6099
6100 if (last_i >= 0)
6101 {
6102 str = history[type][i].hisstr;
6103 while (i != hisidx[type])
6104 {
6105 if (++i >= hislen)
6106 i = 0;
6107 history[type][last_i] = history[type][i];
6108 last_i = i;
6109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006111 history[type][i].viminfo = FALSE;
6112 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006113 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 return TRUE;
6115 }
6116 return FALSE;
6117}
6118
6119/*
6120 * Convert history name (from table above) to its HIST_ equivalent.
6121 * When "name" is empty, return "cmd" history.
6122 * Returns -1 for unknown history name.
6123 */
6124 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006125get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006126{
6127 int i;
6128 int len = (int)STRLEN(name);
6129
6130 /* No argument: use current history. */
6131 if (len == 0)
6132 return hist_char2type(ccline.cmdfirstc);
6133
6134 for (i = 0; history_names[i] != NULL; ++i)
6135 if (STRNICMP(name, history_names[i], len) == 0)
6136 return i;
6137
6138 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6139 return hist_char2type(name[0]);
6140
6141 return -1;
6142}
6143
6144static int last_maptick = -1; /* last seen maptick */
6145
6146/*
6147 * Add the given string to the given history. If the string is already in the
6148 * history then it is moved to the front. "histype" may be one of he HIST_
6149 * values.
6150 */
6151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006152add_to_history(
6153 int histype,
6154 char_u *new_entry,
6155 int in_map, /* consider maptick when inside a mapping */
6156 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157{
6158 histentry_T *hisptr;
6159 int len;
6160
6161 if (hislen == 0) /* no history */
6162 return;
6163
Bram Moolenaara939e432013-11-09 05:30:26 +01006164 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6165 return;
6166
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 /*
6168 * Searches inside the same mapping overwrite each other, so that only
6169 * the last line is kept. Be careful not to remove a line that was moved
6170 * down, only lines that were added.
6171 */
6172 if (histype == HIST_SEARCH && in_map)
6173 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006174 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 {
6176 /* Current line is from the same mapping, remove it */
6177 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6178 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006179 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 --hisnum[histype];
6181 if (--hisidx[HIST_SEARCH] < 0)
6182 hisidx[HIST_SEARCH] = hislen - 1;
6183 }
6184 last_maptick = -1;
6185 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006186 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 {
6188 if (++hisidx[histype] == hislen)
6189 hisidx[histype] = 0;
6190 hisptr = &history[histype][hisidx[histype]];
6191 vim_free(hisptr->hisstr);
6192
6193 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006194 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006195 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6196 if (hisptr->hisstr != NULL)
6197 hisptr->hisstr[len + 1] = sep;
6198
6199 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006200 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006201 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 if (histype == HIST_SEARCH && in_map)
6203 last_maptick = maptick;
6204 }
6205}
6206
6207#if defined(FEAT_EVAL) || defined(PROTO)
6208
6209/*
6210 * Get identifier of newest history entry.
6211 * "histype" may be one of the HIST_ values.
6212 */
6213 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006214get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215{
6216 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6217 || hisidx[histype] < 0)
6218 return -1;
6219
6220 return history[histype][hisidx[histype]].hisnum;
6221}
6222
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 * Calculate history index from a number:
6225 * num > 0: seen as identifying number of a history entry
6226 * num < 0: relative position in history wrt newest entry
6227 * "histype" may be one of the HIST_ values.
6228 */
6229 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006230calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 int i;
6233 histentry_T *hist;
6234 int wrapped = FALSE;
6235
6236 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6237 || (i = hisidx[histype]) < 0 || num == 0)
6238 return -1;
6239
6240 hist = history[histype];
6241 if (num > 0)
6242 {
6243 while (hist[i].hisnum > num)
6244 if (--i < 0)
6245 {
6246 if (wrapped)
6247 break;
6248 i += hislen;
6249 wrapped = TRUE;
6250 }
6251 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6252 return i;
6253 }
6254 else if (-num <= hislen)
6255 {
6256 i += num + 1;
6257 if (i < 0)
6258 i += hislen;
6259 if (hist[i].hisstr != NULL)
6260 return i;
6261 }
6262 return -1;
6263}
6264
6265/*
6266 * Get a history entry by its index.
6267 * "histype" may be one of the HIST_ values.
6268 */
6269 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006270get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271{
6272 idx = calc_hist_idx(histype, idx);
6273 if (idx >= 0)
6274 return history[histype][idx].hisstr;
6275 else
6276 return (char_u *)"";
6277}
6278
6279/*
6280 * Clear all entries of a history.
6281 * "histype" may be one of the HIST_ values.
6282 */
6283 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006284clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 int i;
6287 histentry_T *hisptr;
6288
6289 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6290 {
6291 hisptr = history[histype];
6292 for (i = hislen; i--;)
6293 {
6294 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006295 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006296 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297 }
6298 hisidx[histype] = -1; /* mark history as cleared */
6299 hisnum[histype] = 0; /* reset identifier counter */
6300 return OK;
6301 }
6302 return FAIL;
6303}
6304
6305/*
6306 * Remove all entries matching {str} from a history.
6307 * "histype" may be one of the HIST_ values.
6308 */
6309 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006310del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311{
6312 regmatch_T regmatch;
6313 histentry_T *hisptr;
6314 int idx;
6315 int i;
6316 int last;
6317 int found = FALSE;
6318
6319 regmatch.regprog = NULL;
6320 regmatch.rm_ic = FALSE; /* always match case */
6321 if (hislen != 0
6322 && histype >= 0
6323 && histype < HIST_COUNT
6324 && *str != NUL
6325 && (idx = hisidx[histype]) >= 0
6326 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6327 != NULL)
6328 {
6329 i = last = idx;
6330 do
6331 {
6332 hisptr = &history[histype][i];
6333 if (hisptr->hisstr == NULL)
6334 break;
6335 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6336 {
6337 found = TRUE;
6338 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006339 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 }
6341 else
6342 {
6343 if (i != last)
6344 {
6345 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006346 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006347 }
6348 if (--last < 0)
6349 last += hislen;
6350 }
6351 if (--i < 0)
6352 i += hislen;
6353 } while (i != idx);
6354 if (history[histype][idx].hisstr == NULL)
6355 hisidx[histype] = -1;
6356 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006357 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 return found;
6359}
6360
6361/*
6362 * Remove an indexed entry from a history.
6363 * "histype" may be one of the HIST_ values.
6364 */
6365 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006366del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367{
6368 int i, j;
6369
6370 i = calc_hist_idx(histype, idx);
6371 if (i < 0)
6372 return FALSE;
6373 idx = hisidx[histype];
6374 vim_free(history[histype][i].hisstr);
6375
6376 /* When deleting the last added search string in a mapping, reset
6377 * last_maptick, so that the last added search string isn't deleted again.
6378 */
6379 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6380 last_maptick = -1;
6381
6382 while (i != idx)
6383 {
6384 j = (i + 1) % hislen;
6385 history[histype][i] = history[histype][j];
6386 i = j;
6387 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006388 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 if (--i < 0)
6390 i += hislen;
6391 hisidx[histype] = i;
6392 return TRUE;
6393}
6394
6395#endif /* FEAT_EVAL */
6396
6397#if defined(FEAT_CRYPT) || defined(PROTO)
6398/*
6399 * Very specific function to remove the value in ":set key=val" from the
6400 * history.
6401 */
6402 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006403remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 char_u *p;
6406 int i;
6407
6408 i = hisidx[HIST_CMD];
6409 if (i < 0)
6410 return;
6411 p = history[HIST_CMD][i].hisstr;
6412 if (p != NULL)
6413 for ( ; *p; ++p)
6414 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6415 {
6416 p = vim_strchr(p + 3, '=');
6417 if (p == NULL)
6418 break;
6419 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006420 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (p[i] == '\\' && p[i + 1])
6422 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006423 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006424 --p;
6425 }
6426}
6427#endif
6428
6429#endif /* FEAT_CMDHIST */
6430
Bram Moolenaar064154c2016-03-19 22:50:43 +01006431#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006432/*
6433 * Get pointer to the command line info to use. cmdline_paste() may clear
6434 * ccline and put the previous value in prev_ccline.
6435 */
6436 static struct cmdline_info *
6437get_ccline_ptr(void)
6438{
6439 if ((State & CMDLINE) == 0)
6440 return NULL;
6441 if (ccline.cmdbuff != NULL)
6442 return &ccline;
6443 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6444 return &prev_ccline;
6445 return NULL;
6446}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006447#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006448
Bram Moolenaar064154c2016-03-19 22:50:43 +01006449#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006450/*
6451 * Get the current command line in allocated memory.
6452 * Only works when the command line is being edited.
6453 * Returns NULL when something is wrong.
6454 */
6455 char_u *
6456get_cmdline_str(void)
6457{
6458 struct cmdline_info *p = get_ccline_ptr();
6459
6460 if (p == NULL)
6461 return NULL;
6462 return vim_strnsave(p->cmdbuff, p->cmdlen);
6463}
6464
6465/*
6466 * Get the current command line position, counted in bytes.
6467 * Zero is the first position.
6468 * Only works when the command line is being edited.
6469 * Returns -1 when something is wrong.
6470 */
6471 int
6472get_cmdline_pos(void)
6473{
6474 struct cmdline_info *p = get_ccline_ptr();
6475
6476 if (p == NULL)
6477 return -1;
6478 return p->cmdpos;
6479}
6480
6481/*
6482 * Set the command line byte position to "pos". Zero is the first position.
6483 * Only works when the command line is being edited.
6484 * Returns 1 when failed, 0 when OK.
6485 */
6486 int
6487set_cmdline_pos(
6488 int pos)
6489{
6490 struct cmdline_info *p = get_ccline_ptr();
6491
6492 if (p == NULL)
6493 return 1;
6494
6495 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6496 * changed the command line. */
6497 if (pos < 0)
6498 new_cmdpos = 0;
6499 else
6500 new_cmdpos = pos;
6501 return 0;
6502}
6503#endif
6504
6505#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6506/*
6507 * Get the current command-line type.
6508 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6509 * Only works when the command line is being edited.
6510 * Returns NUL when something is wrong.
6511 */
6512 int
6513get_cmdline_type(void)
6514{
6515 struct cmdline_info *p = get_ccline_ptr();
6516
6517 if (p == NULL)
6518 return NUL;
6519 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006520 return
6521# ifdef FEAT_EVAL
6522 (p->input_fn) ? '@' :
6523# endif
6524 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006525 return p->cmdfirstc;
6526}
6527#endif
6528
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6530/*
6531 * Get indices "num1,num2" that specify a range within a list (not a range of
6532 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6533 * Returns OK if parsed successfully, otherwise FAIL.
6534 */
6535 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006536get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537{
6538 int len;
6539 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006540 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541
6542 *str = skipwhite(*str);
6543 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6544 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006545 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 *str += len;
6547 *num1 = (int)num;
6548 first = TRUE;
6549 }
6550 *str = skipwhite(*str);
6551 if (**str == ',') /* parse "to" part of range */
6552 {
6553 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006554 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (len > 0)
6556 {
6557 *num2 = (int)num;
6558 *str = skipwhite(*str + len);
6559 }
6560 else if (!first) /* no number given at all */
6561 return FAIL;
6562 }
6563 else if (first) /* only one number given */
6564 *num2 = *num1;
6565 return OK;
6566}
6567#endif
6568
6569#if defined(FEAT_CMDHIST) || defined(PROTO)
6570/*
6571 * :history command - print a history
6572 */
6573 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006574ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
6576 histentry_T *hist;
6577 int histype1 = HIST_CMD;
6578 int histype2 = HIST_CMD;
6579 int hisidx1 = 1;
6580 int hisidx2 = -1;
6581 int idx;
6582 int i, j, k;
6583 char_u *end;
6584 char_u *arg = eap->arg;
6585
6586 if (hislen == 0)
6587 {
6588 MSG(_("'history' option is zero"));
6589 return;
6590 }
6591
6592 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6593 {
6594 end = arg;
6595 while (ASCII_ISALPHA(*end)
6596 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6597 end++;
6598 i = *end;
6599 *end = NUL;
6600 histype1 = get_histtype(arg);
6601 if (histype1 == -1)
6602 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006603 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604 {
6605 histype1 = 0;
6606 histype2 = HIST_COUNT-1;
6607 }
6608 else
6609 {
6610 *end = i;
6611 EMSG(_(e_trailing));
6612 return;
6613 }
6614 }
6615 else
6616 histype2 = histype1;
6617 *end = i;
6618 }
6619 else
6620 end = arg;
6621 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6622 {
6623 EMSG(_(e_trailing));
6624 return;
6625 }
6626
6627 for (; !got_int && histype1 <= histype2; ++histype1)
6628 {
6629 STRCPY(IObuff, "\n # ");
6630 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6631 MSG_PUTS_TITLE(IObuff);
6632 idx = hisidx[histype1];
6633 hist = history[histype1];
6634 j = hisidx1;
6635 k = hisidx2;
6636 if (j < 0)
6637 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6638 if (k < 0)
6639 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6640 if (idx >= 0 && j <= k)
6641 for (i = idx + 1; !got_int; ++i)
6642 {
6643 if (i == hislen)
6644 i = 0;
6645 if (hist[i].hisstr != NULL
6646 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6647 {
6648 msg_putchar('\n');
6649 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6650 hist[i].hisnum);
6651 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6652 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006653 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 else
6655 STRCAT(IObuff, hist[i].hisstr);
6656 msg_outtrans(IObuff);
6657 out_flush();
6658 }
6659 if (i == idx)
6660 break;
6661 }
6662 }
6663}
6664#endif
6665
6666#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006667/*
6668 * Buffers for history read from a viminfo file. Only valid while reading.
6669 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006670static histentry_T *viminfo_history[HIST_COUNT] =
6671 {NULL, NULL, NULL, NULL, NULL};
6672static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6673static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674static int viminfo_add_at_front = FALSE;
6675
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006676static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677
6678/*
6679 * Translate a history type number to the associated character.
6680 */
6681 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006682hist_type2char(
6683 int type,
6684 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685{
6686 if (type == HIST_CMD)
6687 return ':';
6688 if (type == HIST_SEARCH)
6689 {
6690 if (use_question)
6691 return '?';
6692 else
6693 return '/';
6694 }
6695 if (type == HIST_EXPR)
6696 return '=';
6697 return '@';
6698}
6699
6700/*
6701 * Prepare for reading the history from the viminfo file.
6702 * This allocates history arrays to store the read history lines.
6703 */
6704 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006705prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 int i;
6708 int num;
6709 int type;
6710 int len;
6711
6712 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006713 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 if (asklen > hislen)
6715 asklen = hislen;
6716
6717 for (type = 0; type < HIST_COUNT; ++type)
6718 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006719 /* Count the number of empty spaces in the history list. Entries read
6720 * from viminfo previously are also considered empty. If there are
6721 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006723 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 num++;
6725 len = asklen;
6726 if (num > len)
6727 len = num;
6728 if (len <= 0)
6729 viminfo_history[type] = NULL;
6730 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006731 viminfo_history[type] = (histentry_T *)lalloc(
6732 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733 if (viminfo_history[type] == NULL)
6734 len = 0;
6735 viminfo_hislen[type] = len;
6736 viminfo_hisidx[type] = 0;
6737 }
6738}
6739
6740/*
6741 * Accept a line from the viminfo, store it in the history array when it's
6742 * new.
6743 */
6744 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006745read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746{
6747 int type;
6748 long_u len;
6749 char_u *val;
6750 char_u *p;
6751
6752 type = hist_char2type(virp->vir_line[0]);
6753 if (viminfo_hisidx[type] < viminfo_hislen[type])
6754 {
6755 val = viminfo_readstring(virp, 1, TRUE);
6756 if (val != NULL && *val != NUL)
6757 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006758 int sep = (*val == ' ' ? NUL : *val);
6759
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006761 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 {
6763 /* Need to re-allocate to append the separator byte. */
6764 len = STRLEN(val);
6765 p = lalloc(len + 2, TRUE);
6766 if (p != NULL)
6767 {
6768 if (type == HIST_SEARCH)
6769 {
6770 /* Search entry: Move the separator from the first
6771 * column to after the NUL. */
6772 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006773 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 }
6775 else
6776 {
6777 /* Not a search entry: No separator in the viminfo
6778 * file, add a NUL separator. */
6779 mch_memmove(p, val, (size_t)len + 1);
6780 p[len + 1] = NUL;
6781 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006782 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6783 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006784 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6785 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006786 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 }
6788 }
6789 }
6790 vim_free(val);
6791 }
6792 return viminfo_readline(virp);
6793}
6794
Bram Moolenaar07219f92013-04-14 23:19:36 +02006795/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006796 * Accept a new style history line from the viminfo, store it in the history
6797 * array when it's new.
6798 */
6799 void
6800handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006801 garray_T *values,
6802 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006803{
6804 int type;
6805 long_u len;
6806 char_u *val;
6807 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006808 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006809
6810 /* Check the format:
6811 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006812 if (values->ga_len < 4
6813 || vp[0].bv_type != BVAL_NR
6814 || vp[1].bv_type != BVAL_NR
6815 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6816 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006817 return;
6818
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006819 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006820 if (type >= HIST_COUNT)
6821 return;
6822 if (viminfo_hisidx[type] < viminfo_hislen[type])
6823 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006824 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825 if (val != NULL && *val != NUL)
6826 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006827 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6828 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006829 int idx;
6830 int overwrite = FALSE;
6831
6832 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6833 {
6834 /* If lines were written by an older Vim we need to avoid
6835 * getting duplicates. See if the entry already exists. */
6836 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6837 {
6838 p = viminfo_history[type][idx].hisstr;
6839 if (STRCMP(val, p) == 0
6840 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6841 {
6842 overwrite = TRUE;
6843 break;
6844 }
6845 }
6846
6847 if (!overwrite)
6848 {
6849 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006850 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006851 p = lalloc(len + 2, TRUE);
6852 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006853 else
6854 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006855 if (p != NULL)
6856 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006857 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006858 if (!overwrite)
6859 {
6860 mch_memmove(p, val, (size_t)len + 1);
6861 /* Put the separator after the NUL. */
6862 p[len + 1] = sep;
6863 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006864 viminfo_history[type][idx].hisnum = 0;
6865 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006866 viminfo_hisidx[type]++;
6867 }
6868 }
6869 }
6870 }
6871 }
6872}
6873
6874/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006875 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006876 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006877 static void
6878concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006879{
6880 int idx;
6881 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006882
6883 idx = hisidx[type] + viminfo_hisidx[type];
6884 if (idx >= hislen)
6885 idx -= hislen;
6886 else if (idx < 0)
6887 idx = hislen - 1;
6888 if (viminfo_add_at_front)
6889 hisidx[type] = idx;
6890 else
6891 {
6892 if (hisidx[type] == -1)
6893 hisidx[type] = hislen - 1;
6894 do
6895 {
6896 if (history[type][idx].hisstr != NULL
6897 || history[type][idx].viminfo)
6898 break;
6899 if (++idx == hislen)
6900 idx = 0;
6901 } while (idx != hisidx[type]);
6902 if (idx != hisidx[type] && --idx < 0)
6903 idx = hislen - 1;
6904 }
6905 for (i = 0; i < viminfo_hisidx[type]; i++)
6906 {
6907 vim_free(history[type][idx].hisstr);
6908 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6909 history[type][idx].viminfo = TRUE;
6910 history[type][idx].time_set = viminfo_history[type][i].time_set;
6911 if (--idx < 0)
6912 idx = hislen - 1;
6913 }
6914 idx += 1;
6915 idx %= hislen;
6916 for (i = 0; i < viminfo_hisidx[type]; i++)
6917 {
6918 history[type][idx++].hisnum = ++hisnum[type];
6919 idx %= hislen;
6920 }
6921}
6922
6923#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6924 static int
6925#ifdef __BORLANDC__
6926_RTLENTRYF
6927#endif
6928sort_hist(const void *s1, const void *s2)
6929{
6930 histentry_T *p1 = *(histentry_T **)s1;
6931 histentry_T *p2 = *(histentry_T **)s2;
6932
6933 if (p1->time_set < p2->time_set) return -1;
6934 if (p1->time_set > p2->time_set) return 1;
6935 return 0;
6936}
6937#endif
6938
6939/*
6940 * Merge history lines from viminfo and lines typed in this Vim based on the
6941 * timestamp;
6942 */
6943 static void
6944merge_history(int type)
6945{
6946 int max_len;
6947 histentry_T **tot_hist;
6948 histentry_T *new_hist;
6949 int i;
6950 int len;
6951
6952 /* Make one long list with all entries. */
6953 max_len = hislen + viminfo_hisidx[type];
6954 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6955 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6956 if (tot_hist == NULL || new_hist == NULL)
6957 {
6958 vim_free(tot_hist);
6959 vim_free(new_hist);
6960 return;
6961 }
6962 for (i = 0; i < viminfo_hisidx[type]; i++)
6963 tot_hist[i] = &viminfo_history[type][i];
6964 len = i;
6965 for (i = 0; i < hislen; i++)
6966 if (history[type][i].hisstr != NULL)
6967 tot_hist[len++] = &history[type][i];
6968
6969 /* Sort the list on timestamp. */
6970 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6971
6972 /* Keep the newest ones. */
6973 for (i = 0; i < hislen; i++)
6974 {
6975 if (i < len)
6976 {
6977 new_hist[i] = *tot_hist[i];
6978 tot_hist[i]->hisstr = NULL;
6979 if (new_hist[i].hisnum == 0)
6980 new_hist[i].hisnum = ++hisnum[type];
6981 }
6982 else
6983 clear_hist_entry(&new_hist[i]);
6984 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006985 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006986
6987 /* Free what is not kept. */
6988 for (i = 0; i < viminfo_hisidx[type]; i++)
6989 vim_free(viminfo_history[type][i].hisstr);
6990 for (i = 0; i < hislen; i++)
6991 vim_free(history[type][i].hisstr);
6992 vim_free(history[type]);
6993 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006994 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006995}
6996
6997/*
6998 * Finish reading history lines from viminfo. Not used when writing viminfo.
6999 */
7000 void
7001finish_viminfo_history(vir_T *virp)
7002{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007004 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005
7006 for (type = 0; type < HIST_COUNT; ++type)
7007 {
7008 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007009 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007010
7011 if (merge)
7012 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007014 concat_history(type);
7015
Bram Moolenaard23a8232018-02-10 18:45:26 +01007016 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007017 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 }
7019}
7020
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007021/*
7022 * Write history to viminfo file in "fp".
7023 * When "merge" is TRUE merge history lines with a previously read viminfo
7024 * file, data is in viminfo_history[].
7025 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7026 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007028write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029{
7030 int i;
7031 int type;
7032 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007033 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034
7035 init_history();
7036 if (hislen == 0)
7037 return;
7038 for (type = 0; type < HIST_COUNT; ++type)
7039 {
7040 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7041 if (num_saved == 0)
7042 continue;
7043 if (num_saved < 0) /* Use default */
7044 num_saved = hislen;
7045 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7046 type == HIST_CMD ? _("Command Line") :
7047 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007048 type == HIST_EXPR ? _("Expression") :
7049 type == HIST_INPUT ? _("Input Line") :
7050 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 if (num_saved > hislen)
7052 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007053
7054 /*
7055 * Merge typed and viminfo history:
7056 * round 1: history of typed commands.
7057 * round 2: history from recently read viminfo.
7058 */
7059 for (round = 1; round <= 2; ++round)
7060 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007061 if (round == 1)
7062 /* start at newest entry, somewhere in the list */
7063 i = hisidx[type];
7064 else if (viminfo_hisidx[type] > 0)
7065 /* start at newest entry, first in the list */
7066 i = 0;
7067 else
7068 /* empty list */
7069 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007070 if (i >= 0)
7071 while (num_saved > 0
7072 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007074 char_u *p;
7075 time_t timestamp;
7076 int c = NUL;
7077
7078 if (round == 1)
7079 {
7080 p = history[type][i].hisstr;
7081 timestamp = history[type][i].time_set;
7082 }
7083 else
7084 {
7085 p = viminfo_history[type] == NULL ? NULL
7086 : viminfo_history[type][i].hisstr;
7087 timestamp = viminfo_history[type] == NULL ? 0
7088 : viminfo_history[type][i].time_set;
7089 }
7090
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007091 if (p != NULL && (round == 2
7092 || !merge
7093 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007095 --num_saved;
7096 fputc(hist_type2char(type, TRUE), fp);
7097 /* For the search history: put the separator in the
7098 * second column; use a space if there isn't one. */
7099 if (type == HIST_SEARCH)
7100 {
7101 c = p[STRLEN(p) + 1];
7102 putc(c == NUL ? ' ' : c, fp);
7103 }
7104 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007105
7106 {
7107 char cbuf[NUMBUFLEN];
7108
7109 /* New style history with a bar line. Format:
7110 * |{bartype},{histtype},{timestamp},{separator},"text" */
7111 if (c == NUL)
7112 cbuf[0] = NUL;
7113 else
7114 sprintf(cbuf, "%d", c);
7115 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7116 type, (long)timestamp, cbuf);
7117 barline_writestring(fp, p, LSIZE - 20);
7118 putc('\n', fp);
7119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007120 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007121 if (round == 1)
7122 {
7123 /* Decrement index, loop around and stop when back at
7124 * the start. */
7125 if (--i < 0)
7126 i = hislen - 1;
7127 if (i == hisidx[type])
7128 break;
7129 }
7130 else
7131 {
7132 /* Increment index. Stop at the end in the while. */
7133 ++i;
7134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007136 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007137 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007138 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007139 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007140 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007141 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 }
7143}
7144#endif /* FEAT_VIMINFO */
7145
7146#if defined(FEAT_FKMAP) || defined(PROTO)
7147/*
7148 * Write a character at the current cursor+offset position.
7149 * It is directly written into the command buffer block.
7150 */
7151 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007152cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153{
7154 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7155 {
7156 EMSG(_("E198: cmd_pchar beyond the command length"));
7157 return;
7158 }
7159 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7160 ccline.cmdbuff[ccline.cmdlen] = NUL;
7161}
7162
7163 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007164cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007165{
7166 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7167 {
7168 /* EMSG(_("cmd_gchar beyond the command length")); */
7169 return NUL;
7170 }
7171 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7172}
7173#endif
7174
7175#if defined(FEAT_CMDWIN) || defined(PROTO)
7176/*
7177 * Open a window on the current command line and history. Allow editing in
7178 * the window. Returns when the window is closed.
7179 * Returns:
7180 * CR if the command is to be executed
7181 * Ctrl_C if it is to be abandoned
7182 * K_IGNORE if editing continues
7183 */
7184 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007185open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007188 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007190 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 win_T *wp;
7192 int i;
7193 linenr_T lnum;
7194 int histtype;
7195 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 int save_restart_edit = restart_edit;
7197 int save_State = State;
7198 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007199#ifdef FEAT_RIGHTLEFT
7200 int save_cmdmsg_rl = cmdmsg_rl;
7201#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007202#ifdef FEAT_FOLDING
7203 int save_KeyTyped;
7204#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205
7206 /* Can't do this recursively. Can't do it when typing a password. */
7207 if (cmdwin_type != 0
7208# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7209 || cmdline_star > 0
7210# endif
7211 )
7212 {
7213 beep_flush();
7214 return K_IGNORE;
7215 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007216 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217
7218 /* Save current window sizes. */
7219 win_size_save(&winsizes);
7220
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007222 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007223
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007224 /* don't use a new tab page */
7225 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007226 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007227
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228 /* Create a window for the command-line buffer. */
7229 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7230 {
7231 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007232 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 return K_IGNORE;
7234 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007235 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236
7237 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007238 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007239 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007242#ifdef FEAT_FOLDING
7243 curwin->w_p_fen = FALSE;
7244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007246 curwin->w_p_rl = cmdmsg_rl;
7247 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007249 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007252 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007253 /* But don't allow switching to another buffer. */
7254 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
Bram Moolenaar46152342005-09-07 21:18:43 +00007256 /* Showing the prompt may have set need_wait_return, reset it. */
7257 need_wait_return = FALSE;
7258
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007259 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7261 {
7262 if (p_wc == TAB)
7263 {
7264 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7265 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7266 }
7267 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7268 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007269 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007271 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7272 * sets 'textwidth' to 78). */
7273 curbuf->b_p_tw = 0;
7274
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 /* Fill the buffer with the history. */
7276 init_history();
7277 if (hislen > 0)
7278 {
7279 i = hisidx[histtype];
7280 if (i >= 0)
7281 {
7282 lnum = 0;
7283 do
7284 {
7285 if (++i == hislen)
7286 i = 0;
7287 if (history[histtype][i].hisstr != NULL)
7288 ml_append(lnum++, history[histtype][i].hisstr,
7289 (colnr_T)0, FALSE);
7290 }
7291 while (i != hisidx[histtype]);
7292 }
7293 }
7294
7295 /* Replace the empty last line with the current command-line and put the
7296 * cursor there. */
7297 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7298 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7299 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007300 changed_line_abv_curs();
7301 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007302 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007303
7304 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007305 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306
7307 /* No Ex mode here! */
7308 exmode_active = 0;
7309
7310 State = NORMAL;
7311# ifdef FEAT_MOUSE
7312 setmouse();
7313# endif
7314
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007316 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007317 if (restart_edit != 0) /* autocmd with ":startinsert" */
7318 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 i = RedrawingDisabled;
7321 RedrawingDisabled = 0;
7322
7323 /*
7324 * Call the main loop until <CR> or CTRL-C is typed.
7325 */
7326 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007327 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328
7329 RedrawingDisabled = i;
7330
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007331# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007332 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007333# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007334
Bram Moolenaar071d4272004-06-13 20:20:40 +00007335 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007336 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007337
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007338# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007339 /* Restore KeyTyped in case it is modified by autocommands */
7340 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341# endif
7342
Bram Moolenaarccc18222007-05-10 18:25:20 +00007343 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007344 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007345 cmdwin_type = 0;
7346
7347 exmode_active = save_exmode;
7348
Bram Moolenaarf9821062008-06-20 16:31:07 +00007349 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007351 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 {
7353 cmdwin_result = Ctrl_C;
7354 EMSG(_("E199: Active window or buffer deleted"));
7355 }
7356 else
7357 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007358# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 /* autocmds may abort script processing */
7360 if (aborting() && cmdwin_result != K_IGNORE)
7361 cmdwin_result = Ctrl_C;
7362# endif
7363 /* Set the new command line from the cmdline buffer. */
7364 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007365 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007367 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7368
7369 if (histtype == HIST_CMD)
7370 {
7371 /* Execute the command directly. */
7372 ccline.cmdbuff = vim_strsave((char_u *)p);
7373 cmdwin_result = CAR;
7374 }
7375 else
7376 {
7377 /* First need to cancel what we were doing. */
7378 ccline.cmdbuff = NULL;
7379 stuffcharReadbuff(':');
7380 stuffReadbuff((char_u *)p);
7381 stuffcharReadbuff(CAR);
7382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007383 }
7384 else if (cmdwin_result == K_XF2) /* :qa typed */
7385 {
7386 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7387 cmdwin_result = CAR;
7388 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007389 else if (cmdwin_result == Ctrl_C)
7390 {
7391 /* :q or :close, don't execute any command
7392 * and don't modify the cmd window. */
7393 ccline.cmdbuff = NULL;
7394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007395 else
7396 ccline.cmdbuff = vim_strsave(ml_get_curline());
7397 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007398 {
7399 ccline.cmdbuff = vim_strsave((char_u *)"");
7400 ccline.cmdlen = 0;
7401 ccline.cmdbufflen = 1;
7402 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007403 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007405 else
7406 {
7407 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7408 ccline.cmdbufflen = ccline.cmdlen + 1;
7409 ccline.cmdpos = curwin->w_cursor.col;
7410 if (ccline.cmdpos > ccline.cmdlen)
7411 ccline.cmdpos = ccline.cmdlen;
7412 if (cmdwin_result == K_IGNORE)
7413 {
7414 set_cmdspos_cursor();
7415 redrawcmd();
7416 }
7417 }
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007420 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007421# ifdef FEAT_CONCEAL
7422 /* Avoid command-line window first character being concealed. */
7423 curwin->w_p_cole = 0;
7424# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007426 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427 win_goto(old_curwin);
7428 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007429
7430 /* win_close() may have already wiped the buffer when 'bh' is
7431 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007432 if (bufref_valid(&bufref))
7433 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007434
7435 /* Restore window sizes. */
7436 win_size_restore(&winsizes);
7437
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007438 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439 }
7440
7441 ga_clear(&winsizes);
7442 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007443# ifdef FEAT_RIGHTLEFT
7444 cmdmsg_rl = save_cmdmsg_rl;
7445# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446
7447 State = save_State;
7448# ifdef FEAT_MOUSE
7449 setmouse();
7450# endif
7451
7452 return cmdwin_result;
7453}
7454#endif /* FEAT_CMDWIN */
7455
7456/*
7457 * Used for commands that either take a simple command string argument, or:
7458 * cmd << endmarker
7459 * {script}
7460 * endmarker
7461 * Returns a pointer to allocated memory with {script} or NULL.
7462 */
7463 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007464script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007465{
7466 char_u *theline;
7467 char *end_pattern = NULL;
7468 char dot[] = ".";
7469 garray_T ga;
7470
7471 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7472 return NULL;
7473
7474 ga_init2(&ga, 1, 0x400);
7475
7476 if (cmd[2] != NUL)
7477 end_pattern = (char *)skipwhite(cmd + 2);
7478 else
7479 end_pattern = dot;
7480
7481 for (;;)
7482 {
7483 theline = eap->getline(
7484#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007485 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486#endif
7487 NUL, eap->cookie, 0);
7488
7489 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007490 {
7491 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007494
7495 ga_concat(&ga, theline);
7496 ga_append(&ga, '\n');
7497 vim_free(theline);
7498 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007499 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007500
7501 return (char_u *)ga.ga_data;
7502}