blob: 1cb3c8b7a1cfab630bca7d5ec1765d47dcb0a35b [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;
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200288 int use_last_pat;
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200289
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200290 *skiplen = 0;
291 *patlen = ccline.cmdlen;
292
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200293 if (!p_is || cmd_silent)
294 return FALSE;
295
296 // by default search all lines
297 search_first_line = 0;
298 search_last_line = MAXLNUM;
299
300 if (firstc == '/' || firstc == '?')
301 return TRUE;
302 if (firstc != ':')
303 return FALSE;
304
305 vim_memset(&ea, 0, sizeof(ea));
306 ea.line1 = 1;
307 ea.line2 = 1;
308 ea.cmd = ccline.cmdbuff;
309 ea.addr_type = ADDR_LINES;
310
311 parse_command_modifiers(&ea, &dummy, TRUE);
312 cmdmod = save_cmdmod;
313
314 cmd = skip_range(ea.cmd, NULL);
315 if (vim_strchr((char_u *)"sgvl", *cmd) == NULL)
316 return FALSE;
317
318 // Skip over "substitute" to find the pattern separator.
319 for (p = cmd; ASCII_ISALPHA(*p); ++p)
320 ;
321 if (*skipwhite(p) == NUL)
322 return FALSE;
323
324 if (STRNCMP(cmd, "substitute", p - cmd) == 0
325 || STRNCMP(cmd, "smagic", p - cmd) == 0
326 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
327 || STRNCMP(cmd, "vglobal", p - cmd) == 0)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200328 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200329 if (*cmd == 's' && cmd[1] == 'm')
330 p_magic = TRUE;
331 else if (*cmd == 's' && cmd[1] == 'n')
332 p_magic = FALSE;
333 }
334 else if (STRNCMP(cmd, "sort", MAX(p - cmd, 3)) == 0)
335 {
336 // skip over flags
337 while (ASCII_ISALPHA(*(p = skipwhite(p))))
338 ++p;
339 if (*p == NUL)
340 return FALSE;
341 }
342 else if (STRNCMP(cmd, "vimgrep", MAX(p - cmd, 3)) == 0
343 || STRNCMP(cmd, "vimgrepadd", MAX(p - cmd, 8)) == 0
344 || STRNCMP(cmd, "lvimgrep", MAX(p - cmd, 2)) == 0
345 || STRNCMP(cmd, "lvimgrepadd", MAX(p - cmd, 9)) == 0
346 || STRNCMP(cmd, "global", p - cmd) == 0)
347 {
348 // skip over "!"
349 if (*p == '!')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350 {
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200351 p++;
352 if (*skipwhite(p) == NUL)
353 return FALSE;
354 }
355 if (*cmd != 'g')
356 delim_optional = TRUE;
357 }
358 else
359 return FALSE;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200360
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200361 p = skipwhite(p);
362 delim = (delim_optional && vim_isIDc(*p)) ? ' ' : *p++;
363 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200364
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200365 use_last_pat = end == p && *end == delim;
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200366
Bram Moolenaar8b0d5ce2018-08-22 23:05:44 +0200367 if (end == p && !use_last_pat)
368 return FALSE;
369
370 // Don't do 'hlsearch' highlighting if the pattern matches everything.
371 if (!use_last_pat)
372 {
373 char c = *end;
374 int empty;
375
376 *end = NUL;
377 empty = empty_pattern(p);
378 *end = c;
379 if (empty)
380 return FALSE;
381 }
382
383 // found a non-empty pattern or //
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200384 *skiplen = (int)(p - ccline.cmdbuff);
385 *patlen = (int)(end - p);
Bram Moolenaar264cf5c2018-08-18 21:05:31 +0200386
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200387 // parse the address range
388 save_cursor = curwin->w_cursor;
389 curwin->w_cursor = is_state->search_start;
390 parse_cmd_address(&ea, &dummy);
391 if (ea.addr_count > 0)
392 {
393 // Allow for reverse match.
394 if (ea.line2 < ea.line1)
395 {
396 search_first_line = ea.line2;
397 search_last_line = ea.line1;
398 }
399 else
400 {
401 search_first_line = ea.line1;
402 search_last_line = ea.line2;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200403 }
404 }
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200405 else if (cmd[0] == 's' && cmd[1] != 'o')
406 {
407 // :s defaults to the current line
408 search_first_line = curwin->w_cursor.lnum;
409 search_last_line = curwin->w_cursor.lnum;
410 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200411
Bram Moolenaar111bbd62018-08-18 21:23:05 +0200412 curwin->w_cursor = save_cursor;
413 return TRUE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200414}
415
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200416 static void
417finish_incsearch_highlighting(
418 int gotesc,
419 incsearch_state_T *is_state,
420 int call_update_screen)
421{
422 if (is_state->did_incsearch)
423 {
424 is_state->did_incsearch = FALSE;
425 if (gotesc)
426 curwin->w_cursor = is_state->save_cursor;
427 else
428 {
429 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
430 {
431 // put the '" mark at the original position
432 curwin->w_cursor = is_state->save_cursor;
433 setpcmark();
434 }
435 curwin->w_cursor = is_state->search_start;
436 }
437 restore_viewstate(&is_state->old_viewstate);
438 highlight_match = FALSE;
439 validate_cursor(); /* needed for TAB */
440 if (call_update_screen)
441 update_screen(SOME_VALID);
442 else
443 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200444 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200445 }
446}
447
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200448/*
449 * Do 'incsearch' highlighting if desired.
450 */
451 static void
452may_do_incsearch_highlighting(
453 int firstc,
454 long count,
455 incsearch_state_T *is_state)
456{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200457 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200458 int i;
459 pos_T end_pos;
460 struct cmdline_info save_ccline;
461#ifdef FEAT_RELTIME
462 proftime_T tm;
463#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200464 int next_char;
465 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200466
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200467 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200468 {
469 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200470 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200471 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200472
473 // If there is a character waiting, search and redraw later.
474 if (char_avail())
475 {
476 is_state->incsearch_postponed = TRUE;
477 return;
478 }
479 is_state->incsearch_postponed = FALSE;
480
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200481 if (search_first_line == 0)
482 // start at the original cursor position
483 curwin->w_cursor = is_state->search_start;
484 else
485 {
486 // start at the first line in the range
487 curwin->w_cursor.lnum = search_first_line;
488 curwin->w_cursor.col = 0;
489 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200490 save_last_search_pattern();
491
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200492 // Use the previous pattern for ":s//".
493 next_char = ccline.cmdbuff[skiplen + patlen];
494 use_last_pat = patlen == 0 && skiplen > 0
495 && ccline.cmdbuff[skiplen - 1] == next_char;
496
497 // If there is no pattern, don't do anything.
498 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200499 {
500 i = 0;
501 set_no_hlsearch(TRUE); // turn off previous highlight
502 redraw_all_later(SOME_VALID);
503 }
504 else
505 {
506 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
507
508 cursor_off(); // so the user knows we're busy
509 out_flush();
510 ++emsg_off; // so it doesn't beep if bad expr
511#ifdef FEAT_RELTIME
512 // Set the time limit to half a second.
513 profile_setlimit(500L, &tm);
514#endif
515 if (!p_hls)
516 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200517 if (search_first_line != 0)
518 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200519 ccline.cmdbuff[skiplen + patlen] = NUL;
520 i = do_search(NULL, firstc == ':' ? '/' : firstc,
521 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200522#ifdef FEAT_RELTIME
523 &tm, NULL
524#else
525 NULL, NULL
526#endif
527 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200528 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200529 --emsg_off;
530
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200531 if (curwin->w_cursor.lnum < search_first_line
532 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200533 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200534 // match outside of address range
535 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200536 curwin->w_cursor = is_state->search_start;
537 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200538
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200539 // if interrupted while searching, behave like it failed
540 if (got_int)
541 {
542 (void)vpeekc(); // remove <C-C> from input stream
543 got_int = FALSE; // don't abandon the command line
544 i = 0;
545 }
546 else if (char_avail())
547 // cancelled searching because a char was typed
548 is_state->incsearch_postponed = TRUE;
549 }
550 if (i != 0)
551 highlight_match = TRUE; // highlight position
552 else
553 highlight_match = FALSE; // remove highlight
554
555 // First restore the old curwin values, so the screen is positioned in the
556 // same way as the actual search command.
557 restore_viewstate(&is_state->old_viewstate);
558 changed_cline_bef_curs();
559 update_topline();
560
561 if (i != 0)
562 {
563 pos_T save_pos = curwin->w_cursor;
564
565 is_state->match_start = curwin->w_cursor;
566 set_search_match(&curwin->w_cursor);
567 validate_cursor();
568 end_pos = curwin->w_cursor;
569 is_state->match_end = end_pos;
570 curwin->w_cursor = save_pos;
571 }
572 else
573 end_pos = curwin->w_cursor; // shutup gcc 4
574
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200575 validate_cursor();
576 // May redraw the status line to show the cursor position.
577 if (p_ru && curwin->w_status_height > 0)
578 curwin->w_redr_status = TRUE;
579
580 save_cmdline(&save_ccline);
581 update_screen(SOME_VALID);
582 restore_cmdline(&save_ccline);
583 restore_last_search_pattern();
584
585 // Leave it at the end to make CTRL-R CTRL-W work.
586 if (i != 0)
587 curwin->w_cursor = end_pos;
588
589 msg_starthere();
590 redrawcmdline();
591 is_state->did_incsearch = TRUE;
592}
593
594/*
595 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
596 * or previous match.
597 * Returns FAIL when jumping to cmdline_not_changed;
598 */
599 static int
600may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200601 int firstc,
602 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200603 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200604 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200605{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200606 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200607 pos_T t;
608 char_u *pat;
609 int search_flags = SEARCH_NOOF;
610 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200611 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200612
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200613 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200614 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200615 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200616 return FAIL;
617
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200618 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200619 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200620 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200621 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200622 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200623 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200624 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200625 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200626
627 save_last_search_pattern();
628 cursor_off();
629 out_flush();
630 if (c == Ctrl_G)
631 {
632 t = is_state->match_end;
633 if (LT_POS(is_state->match_start, is_state->match_end))
634 // Start searching at the end of the match not at the beginning of
635 // the next column.
636 (void)decl(&t);
637 search_flags += SEARCH_COL;
638 }
639 else
640 t = is_state->match_start;
641 if (!p_hls)
642 search_flags += SEARCH_KEEP;
643 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200644 save = pat[patlen];
645 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200646 i = searchit(curwin, curbuf, &t,
647 c == Ctrl_G ? FORWARD : BACKWARD,
648 pat, count, search_flags,
649 RE_SEARCH, 0, NULL, NULL);
650 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200651 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200652 if (i)
653 {
654 is_state->search_start = is_state->match_start;
655 is_state->match_end = t;
656 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200657 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200658 {
659 // Move just before the current match, so that when nv_search
660 // finishes the cursor will be put back on the match.
661 is_state->search_start = t;
662 (void)decl(&is_state->search_start);
663 }
664 else if (c == Ctrl_G && firstc == '?')
665 {
666 // Move just after the current match, so that when nv_search
667 // finishes the cursor will be put back on the match.
668 is_state->search_start = t;
669 (void)incl(&is_state->search_start);
670 }
671 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
672 {
673 // wrap around
674 is_state->search_start = t;
675 if (firstc == '?')
676 (void)incl(&is_state->search_start);
677 else
678 (void)decl(&is_state->search_start);
679 }
680
681 set_search_match(&is_state->match_end);
682 curwin->w_cursor = is_state->match_start;
683 changed_cline_bef_curs();
684 update_topline();
685 validate_cursor();
686 highlight_match = TRUE;
687 save_viewstate(&is_state->old_viewstate);
688 update_screen(NOT_VALID);
689 redrawcmdline();
690 }
691 else
692 vim_beep(BO_ERROR);
693 restore_last_search_pattern();
694 return FAIL;
695}
696
697/*
698 * When CTRL-L typed: add character from the match to the pattern.
699 * May set "*c" to the added character.
700 * Return OK when jumping to cmdline_not_changed.
701 */
702 static int
703may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
704{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200705 int skiplen, patlen;
706
707 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200708 return FAIL;
709
710 // Add a character from under the cursor for 'incsearch'.
711 if (is_state->did_incsearch)
712 {
713 curwin->w_cursor = is_state->match_end;
714 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
715 {
716 *c = gchar_cursor();
717
718 // If 'ignorecase' and 'smartcase' are set and the
719 // command line has no uppercase characters, convert
720 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200721 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200722 *c = MB_TOLOWER(*c);
723 if (*c != NUL)
724 {
725 if (*c == firstc || vim_strchr((char_u *)(
726 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
727 {
728 // put a backslash before special characters
729 stuffcharReadbuff(*c);
730 *c = '\\';
731 }
732 return FAIL;
733 }
734 }
735 }
736 return OK;
737}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200738#endif
739
Bram Moolenaar66216052017-12-16 16:33:44 +0100740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000741 * getcmdline() - accept a command line starting with firstc.
742 *
743 * firstc == ':' get ":" command line.
744 * firstc == '/' or '?' get search pattern
745 * firstc == '=' get expression
746 * firstc == '@' get text for input() function
747 * firstc == '>' get text for debug mode
748 * firstc == NUL get text for :insert command
749 * firstc == -1 like NUL, and break on CTRL-C
750 *
751 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
752 * command line.
753 *
754 * Careful: getcmdline() can be called recursively!
755 *
756 * Return pointer to allocated string if there is a commandline, NULL
757 * otherwise.
758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100760getcmdline(
761 int firstc,
762 long count UNUSED, /* only used for incremental search */
763 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000764{
765 int c;
766 int i;
767 int j;
768 int gotesc = FALSE; /* TRUE when <ESC> just typed */
769 int do_abbr; /* when TRUE check for abbr. */
770#ifdef FEAT_CMDHIST
771 char_u *lookfor = NULL; /* string to match */
772 int hiscnt; /* current history line in use */
773 int histype; /* history type to be used */
774#endif
775#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200776 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777#endif
778 int did_wild_list = FALSE; /* did wild_list() recently */
779 int wim_index = 0; /* index in wim_flags[] */
780 int res;
781 int save_msg_scroll = msg_scroll;
782 int save_State = State; /* remember State when called */
783 int some_key_typed = FALSE; /* one of the keys was typed */
784#ifdef FEAT_MOUSE
785 /* mouse drag and release events are ignored, unless they are
786 * preceded with a mouse down event */
787 int ignore_drag_release = TRUE;
788#endif
789#ifdef FEAT_EVAL
790 int break_ctrl_c = FALSE;
791#endif
792 expand_T xpc;
793 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200794#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000795 /* Everything that may work recursively should save and restore the
796 * current command line in save_ccline. That includes update_screen(), a
797 * custom status line may invoke ":normal". */
798 struct cmdline_info save_ccline;
799#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200800 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_EVAL
803 if (firstc == -1)
804 {
805 firstc = NUL;
806 break_ctrl_c = TRUE;
807 }
808#endif
809#ifdef FEAT_RIGHTLEFT
810 /* start without Hebrew mapping for a command line */
811 if (firstc == ':' || firstc == '=' || firstc == '>')
812 cmd_hkmap = 0;
813#endif
814
815 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200818 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#endif
820
821 /*
822 * set some variables for redrawcmd()
823 */
824 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000825 ccline.cmdindent = (firstc > 0 ? indent : 0);
826
827 /* alloc initial ccline.cmdbuff */
828 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 if (ccline.cmdbuff == NULL)
830 return NULL; /* out of memory */
831 ccline.cmdlen = ccline.cmdpos = 0;
832 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100833 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000835 /* autoindent for :insert and :append */
836 if (firstc <= 0)
837 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200838 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000839 ccline.cmdbuff[indent] = NUL;
840 ccline.cmdpos = indent;
841 ccline.cmdspos = indent;
842 ccline.cmdlen = indent;
843 }
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000846 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847
848#ifdef FEAT_RIGHTLEFT
849 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
850 && (firstc == '/' || firstc == '?'))
851 cmdmsg_rl = TRUE;
852 else
853 cmdmsg_rl = FALSE;
854#endif
855
856 redir_off = TRUE; /* don't redirect the typed command */
857 if (!cmd_silent)
858 {
859 i = msg_scrolled;
860 msg_scrolled = 0; /* avoid wait_return message */
861 gotocmdline(TRUE);
862 msg_scrolled += i;
863 redrawcmdprompt(); /* draw prompt or indent */
864 set_cmdspos();
865 }
866 xpc.xp_context = EXPAND_NOTHING;
867 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000868#ifndef BACKSLASH_IN_FILENAME
869 xpc.xp_shell = FALSE;
870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000872#if defined(FEAT_EVAL)
873 if (ccline.input_fn)
874 {
875 xpc.xp_context = ccline.xp_context;
876 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000877# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000878 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000879# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000880 }
881#endif
882
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 /*
884 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
885 * doing ":@0" when register 0 doesn't contain a CR.
886 */
887 msg_scroll = FALSE;
888
889 State = CMDLINE;
890
891 if (firstc == '/' || firstc == '?' || firstc == '@')
892 {
893 /* Use ":lmap" mappings for search pattern and input(). */
894 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
895 b_im_ptr = &curbuf->b_p_iminsert;
896 else
897 b_im_ptr = &curbuf->b_p_imsearch;
898 if (*b_im_ptr == B_IMODE_LMAP)
899 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100900#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 im_set_active(*b_im_ptr == B_IMODE_IM);
902#endif
903 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100904#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 else if (p_imcmdline)
906 im_set_active(TRUE);
907#endif
908
909#ifdef FEAT_MOUSE
910 setmouse();
911#endif
912#ifdef CURSOR_SHAPE
913 ui_cursor_shape(); /* may show different cursor shape */
914#endif
915
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000916 /* When inside an autocommand for writing "exiting" may be set and
917 * terminal mode set to cooked. Need to set raw mode here then. */
918 settmode(TMODE_RAW);
919
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200920 /* Trigger CmdlineEnter autocommands. */
921 cmdline_type = firstc == NUL ? '-' : firstc;
922 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200923
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#ifdef FEAT_CMDHIST
925 init_history();
926 hiscnt = hislen; /* set hiscnt to impossible history value */
927 histype = hist_char2type(firstc);
928#endif
929
930#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000931 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932#endif
933
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200934 /* If something above caused an error, reset the flags, we do want to type
935 * and execute commands. Display may be messed up a bit. */
936 if (did_emsg)
937 redrawcmd();
938 did_emsg = FALSE;
939 got_int = FALSE;
940
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 /*
942 * Collect the command string, handling editing keys.
943 */
944 for (;;)
945 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000946 redir_off = TRUE; /* Don't redirect the typed command.
947 Repeated, because a ":redir" inside
948 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#ifdef USE_ON_FLY_SCROLL
950 dont_scroll = FALSE; /* allow scrolling here */
951#endif
952 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
953
Bram Moolenaar72532d32018-04-07 19:09:09 +0200954 did_emsg = FALSE; /* There can't really be a reason why an error
955 that occurs while typing a command should
956 cause the command not to be executed. */
957
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000959
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100960 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
961 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000962 do
963 {
964 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100965 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000966
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (KeyTyped)
968 {
969 some_key_typed = TRUE;
970#ifdef FEAT_RIGHTLEFT
971 if (cmd_hkmap)
972 c = hkmap(c);
973# ifdef FEAT_FKMAP
974 if (cmd_fkmap)
975 c = cmdl_fkmap(c);
976# endif
977 if (cmdmsg_rl && !KeyStuffed)
978 {
979 /* Invert horizontal movements and operations. Only when
980 * typed by the user directly, not when the result of a
981 * mapping. */
982 switch (c)
983 {
984 case K_RIGHT: c = K_LEFT; break;
985 case K_S_RIGHT: c = K_S_LEFT; break;
986 case K_C_RIGHT: c = K_C_LEFT; break;
987 case K_LEFT: c = K_RIGHT; break;
988 case K_S_LEFT: c = K_S_RIGHT; break;
989 case K_C_LEFT: c = K_C_RIGHT; break;
990 }
991 }
992#endif
993 }
994
995 /*
996 * Ignore got_int when CTRL-C was typed here.
997 * Don't ignore it in :global, we really need to break then, e.g., for
998 * ":g/pat/normal /pat" (without the <CR>).
999 * Don't ignore it for the input() function.
1000 */
1001 if ((c == Ctrl_C
1002#ifdef UNIX
1003 || c == intr_char
1004#endif
1005 )
1006#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
1007 && firstc != '@'
1008#endif
1009#ifdef FEAT_EVAL
1010 && !break_ctrl_c
1011#endif
1012 && !global_busy)
1013 got_int = FALSE;
1014
1015#ifdef FEAT_CMDHIST
1016 /* free old command line when finished moving around in the history
1017 * list */
1018 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001019 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001020 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 && c != K_PAGEDOWN && c != K_PAGEUP
1022 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001023 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001025 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026#endif
1027
1028 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001029 * When there are matching completions to select <S-Tab> works like
1030 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001032 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 c = Ctrl_P;
1034
1035#ifdef FEAT_WILDMENU
1036 /* Special translations for 'wildmenu' */
1037 if (did_wild_list && p_wmnu)
1038 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001039 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001041 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042 c = Ctrl_N;
1043 }
1044 /* Hitting CR after "emenu Name.": complete submenu */
1045 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1046 && ccline.cmdpos > 1
1047 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1048 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1049 && (c == '\n' || c == '\r' || c == K_KENTER))
1050 c = K_DOWN;
1051#endif
1052
1053 /* free expanded names when finished walking through matches */
1054 if (xpc.xp_numfiles != -1
1055 && !(c == p_wc && KeyTyped) && c != p_wcm
1056 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1057 && c != Ctrl_L)
1058 {
1059 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1060 did_wild_list = FALSE;
1061#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001062 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063#endif
1064 xpc.xp_context = EXPAND_NOTHING;
1065 wim_index = 0;
1066#ifdef FEAT_WILDMENU
1067 if (p_wmnu && wild_menu_showing != 0)
1068 {
1069 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001070 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001071
1072 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001073 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074
1075 if (wild_menu_showing == WM_SCROLLED)
1076 {
1077 /* Entered command line, move it up */
1078 cmdline_row--;
1079 redrawcmd();
1080 }
1081 else if (save_p_ls != -1)
1082 {
1083 /* restore 'laststatus' and 'winminheight' */
1084 p_ls = save_p_ls;
1085 p_wmh = save_p_wmh;
1086 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001087 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001089 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 redrawcmd();
1091 save_p_ls = -1;
1092 }
1093 else
1094 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 redraw_statuslines();
1097 }
1098 KeyTyped = skt;
1099 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001100 if (ccline.input_fn)
1101 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103#endif
1104 }
1105
1106#ifdef FEAT_WILDMENU
1107 /* Special translations for 'wildmenu' */
1108 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1109 {
1110 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001111 if (c == K_DOWN && ccline.cmdpos > 0
1112 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001114 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 {
1116 /* Hitting <Up>: Remove one submenu name in front of the
1117 * cursor */
1118 int found = FALSE;
1119
1120 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1121 i = 0;
1122 while (--j > 0)
1123 {
1124 /* check for start of menu name */
1125 if (ccline.cmdbuff[j] == ' '
1126 && ccline.cmdbuff[j - 1] != '\\')
1127 {
1128 i = j + 1;
1129 break;
1130 }
1131 /* check for start of submenu name */
1132 if (ccline.cmdbuff[j] == '.'
1133 && ccline.cmdbuff[j - 1] != '\\')
1134 {
1135 if (found)
1136 {
1137 i = j + 1;
1138 break;
1139 }
1140 else
1141 found = TRUE;
1142 }
1143 }
1144 if (i > 0)
1145 cmdline_del(i);
1146 c = p_wc;
1147 xpc.xp_context = EXPAND_NOTHING;
1148 }
1149 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001150 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001151 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001152 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 {
1154 char_u upseg[5];
1155
1156 upseg[0] = PATHSEP;
1157 upseg[1] = '.';
1158 upseg[2] = '.';
1159 upseg[3] = PATHSEP;
1160 upseg[4] = NUL;
1161
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001162 if (c == K_DOWN
1163 && ccline.cmdpos > 0
1164 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1165 && (ccline.cmdpos < 3
1166 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1168 {
1169 /* go down a directory */
1170 c = p_wc;
1171 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001172 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 /* If in a direct ancestor, strip off one ../ to go down */
1175 int found = FALSE;
1176
1177 j = ccline.cmdpos;
1178 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1179 while (--j > i)
1180 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001181#ifdef FEAT_MBYTE
1182 if (has_mbyte)
1183 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 if (vim_ispathsep(ccline.cmdbuff[j]))
1186 {
1187 found = TRUE;
1188 break;
1189 }
1190 }
1191 if (found
1192 && ccline.cmdbuff[j - 1] == '.'
1193 && ccline.cmdbuff[j - 2] == '.'
1194 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1195 {
1196 cmdline_del(j - 2);
1197 c = p_wc;
1198 }
1199 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001200 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 {
1202 /* go up a directory */
1203 int found = FALSE;
1204
1205 j = ccline.cmdpos - 1;
1206 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1207 while (--j > i)
1208 {
1209#ifdef FEAT_MBYTE
1210 if (has_mbyte)
1211 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1212#endif
1213 if (vim_ispathsep(ccline.cmdbuff[j])
1214#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001215 && vim_strchr((char_u *)" *?[{`$%#",
1216 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#endif
1218 )
1219 {
1220 if (found)
1221 {
1222 i = j + 1;
1223 break;
1224 }
1225 else
1226 found = TRUE;
1227 }
1228 }
1229
1230 if (!found)
1231 j = i;
1232 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1233 j += 4;
1234 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1235 && j == i)
1236 j += 3;
1237 else
1238 j = 0;
1239 if (j > 0)
1240 {
1241 /* TODO this is only for DOS/UNIX systems - need to put in
1242 * machine-specific stuff here and in upseg init */
1243 cmdline_del(j);
1244 put_on_cmdline(upseg + 1, 3, FALSE);
1245 }
1246 else if (ccline.cmdpos > i)
1247 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001248
1249 /* Now complete in the new directory. Set KeyTyped in case the
1250 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001252 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 }
1254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255
1256#endif /* FEAT_WILDMENU */
1257
1258 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1259 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1260 if (c == Ctrl_BSL)
1261 {
1262 ++no_mapping;
1263 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001264 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 --no_mapping;
1266 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001267 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1268 * is in a mapping. */
1269 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1270 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 {
1272 vungetc(c);
1273 c = Ctrl_BSL;
1274 }
1275#ifdef FEAT_EVAL
1276 else if (c == 'e')
1277 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001278 char_u *p = NULL;
1279 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280
1281 /*
1282 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001283 * Need to save and restore the current command line, to be
1284 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 */
1286 if (ccline.cmdpos == ccline.cmdlen)
1287 new_cmdpos = 99999; /* keep it at the end */
1288 else
1289 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001290
1291 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001293 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 if (c == '=')
1295 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001296 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001297 * to avoid nasty things like going to another buffer when
1298 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001299 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001300 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001302 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001303 restore_cmdline(&save_ccline);
1304
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001305 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001307 len = (int)STRLEN(p);
1308 if (realloc_cmdbuff(len + 1) == OK)
1309 {
1310 ccline.cmdlen = len;
1311 STRCPY(ccline.cmdbuff, p);
1312 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001314 /* Restore the cursor or use the position set with
1315 * set_cmdline_pos(). */
1316 if (new_cmdpos > ccline.cmdlen)
1317 ccline.cmdpos = ccline.cmdlen;
1318 else
1319 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001321 KeyTyped = FALSE; /* Don't do p_wc completion. */
1322 redrawcmd();
1323 goto cmdline_changed;
1324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 }
1326 }
1327 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001328 got_int = FALSE; /* don't abandon the command line */
1329 did_emsg = FALSE;
1330 emsg_on_display = FALSE;
1331 redrawcmd();
1332 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 }
1334#endif
1335 else
1336 {
1337 if (c == Ctrl_G && p_im && restart_edit == 0)
1338 restart_edit = 'a';
1339 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1340 in history */
1341 goto returncmd; /* back to Normal mode */
1342 }
1343 }
1344
1345#ifdef FEAT_CMDWIN
1346 if (c == cedit_key || c == K_CMDWIN)
1347 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001348 if (ex_normal_busy == 0 && got_int == FALSE)
1349 {
1350 /*
1351 * Open a window to edit the command line (and history).
1352 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001353 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001354 some_key_typed = TRUE;
1355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 }
1357# ifdef FEAT_DIGRAPHS
1358 else
1359# endif
1360#endif
1361#ifdef FEAT_DIGRAPHS
1362 c = do_digraph(c);
1363#endif
1364
1365 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1366 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1367 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001368 /* In Ex mode a backslash escapes a newline. */
1369 if (exmode_active
1370 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001371 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001372 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001373 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001375 if (c == K_KENTER)
1376 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001378 else
1379 {
1380 gotesc = FALSE; /* Might have typed ESC previously, don't
1381 truncate the cmdline now. */
1382 if (ccheck_abbr(c + ABBR_OFF))
1383 goto cmdline_changed;
1384 if (!cmd_silent)
1385 {
1386 windgoto(msg_row, 0);
1387 out_flush();
1388 }
1389 break;
1390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 }
1392
1393 /*
1394 * Completion for 'wildchar' or 'wildcharm' key.
1395 * - hitting <ESC> twice means: abandon command line.
1396 * - wildcard expansion is only done when the 'wildchar' key is really
1397 * typed, not when it comes from a macro
1398 */
1399 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1400 {
1401 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1402 {
1403 /* if 'wildmode' contains "list" may still need to list */
1404 if (xpc.xp_numfiles > 1
1405 && !did_wild_list
1406 && (wim_flags[wim_index] & WIM_LIST))
1407 {
1408 (void)showmatches(&xpc, FALSE);
1409 redrawcmd();
1410 did_wild_list = TRUE;
1411 }
1412 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001413 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1414 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001416 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1417 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 else
1419 res = OK; /* don't insert 'wildchar' now */
1420 }
1421 else /* typed p_wc first time */
1422 {
1423 wim_index = 0;
1424 j = ccline.cmdpos;
1425 /* if 'wildmode' first contains "longest", get longest
1426 * common part */
1427 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001428 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1429 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001431 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1432 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
1434 /* if interrupted while completing, behave like it failed */
1435 if (got_int)
1436 {
1437 (void)vpeekc(); /* remove <C-C> from input stream */
1438 got_int = FALSE; /* don't abandon the command line */
1439 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1440#ifdef FEAT_WILDMENU
1441 xpc.xp_context = EXPAND_NOTHING;
1442#endif
1443 goto cmdline_changed;
1444 }
1445
1446 /* when more than one match, and 'wildmode' first contains
1447 * "list", or no change and 'wildmode' contains "longest,list",
1448 * list all matches */
1449 if (res == OK && xpc.xp_numfiles > 1)
1450 {
1451 /* a "longest" that didn't do anything is skipped (but not
1452 * "list:longest") */
1453 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1454 wim_index = 1;
1455 if ((wim_flags[wim_index] & WIM_LIST)
1456#ifdef FEAT_WILDMENU
1457 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1458#endif
1459 )
1460 {
1461 if (!(wim_flags[0] & WIM_LONGEST))
1462 {
1463#ifdef FEAT_WILDMENU
1464 int p_wmnu_save = p_wmnu;
1465 p_wmnu = 0;
1466#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001467 /* remove match */
1468 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#ifdef FEAT_WILDMENU
1470 p_wmnu = p_wmnu_save;
1471#endif
1472 }
1473#ifdef FEAT_WILDMENU
1474 (void)showmatches(&xpc, p_wmnu
1475 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1476#else
1477 (void)showmatches(&xpc, FALSE);
1478#endif
1479 redrawcmd();
1480 did_wild_list = TRUE;
1481 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001482 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1483 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001485 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1486 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 }
1488 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001489 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 }
1491#ifdef FEAT_WILDMENU
1492 else if (xpc.xp_numfiles == -1)
1493 xpc.xp_context = EXPAND_NOTHING;
1494#endif
1495 }
1496 if (wim_index < 3)
1497 ++wim_index;
1498 if (c == ESC)
1499 gotesc = TRUE;
1500 if (res == OK)
1501 goto cmdline_changed;
1502 }
1503
1504 gotesc = FALSE;
1505
1506 /* <S-Tab> goes to last match, in a clumsy way */
1507 if (c == K_S_TAB && KeyTyped)
1508 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001509 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1510 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1511 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 goto cmdline_changed;
1513 }
1514
1515 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1516 c = NL;
1517
1518 do_abbr = TRUE; /* default: check for abbreviation */
1519
1520 /*
1521 * Big switch for a typed command line character.
1522 */
1523 switch (c)
1524 {
1525 case K_BS:
1526 case Ctrl_H:
1527 case K_DEL:
1528 case K_KDEL:
1529 case Ctrl_W:
1530#ifdef FEAT_FKMAP
1531 if (cmd_fkmap && c == K_BS)
1532 c = K_DEL;
1533#endif
1534 if (c == K_KDEL)
1535 c = K_DEL;
1536
1537 /*
1538 * delete current character is the same as backspace on next
1539 * character, except at end of line
1540 */
1541 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1542 ++ccline.cmdpos;
1543#ifdef FEAT_MBYTE
1544 if (has_mbyte && c == K_DEL)
1545 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1546 ccline.cmdbuff + ccline.cmdpos);
1547#endif
1548 if (ccline.cmdpos > 0)
1549 {
1550 char_u *p;
1551
1552 j = ccline.cmdpos;
1553 p = ccline.cmdbuff + j;
1554#ifdef FEAT_MBYTE
1555 if (has_mbyte)
1556 {
1557 p = mb_prevptr(ccline.cmdbuff, p);
1558 if (c == Ctrl_W)
1559 {
1560 while (p > ccline.cmdbuff && vim_isspace(*p))
1561 p = mb_prevptr(ccline.cmdbuff, p);
1562 i = mb_get_class(p);
1563 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1564 p = mb_prevptr(ccline.cmdbuff, p);
1565 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001566 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 }
1568 }
1569 else
1570#endif
1571 if (c == Ctrl_W)
1572 {
1573 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1574 --p;
1575 i = vim_iswordc(p[-1]);
1576 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1577 && vim_iswordc(p[-1]) == i)
1578 --p;
1579 }
1580 else
1581 --p;
1582 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1583 ccline.cmdlen -= j - ccline.cmdpos;
1584 i = ccline.cmdpos;
1585 while (i < ccline.cmdlen)
1586 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1587
1588 /* Truncate at the end, required for multi-byte chars. */
1589 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001590#ifdef FEAT_SEARCH_EXTRA
1591 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001592 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001593 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001594 /* save view settings, so that the screen
1595 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001596 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001597 }
1598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 redrawcmd();
1600 }
1601 else if (ccline.cmdlen == 0 && c != Ctrl_W
1602 && ccline.cmdprompt == NULL && indent == 0)
1603 {
1604 /* In ex and debug mode it doesn't make sense to return. */
1605 if (exmode_active
1606#ifdef FEAT_EVAL
1607 || ccline.cmdfirstc == '>'
1608#endif
1609 )
1610 goto cmdline_not_changed;
1611
Bram Moolenaard23a8232018-02-10 18:45:26 +01001612 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 if (!cmd_silent)
1614 {
1615#ifdef FEAT_RIGHTLEFT
1616 if (cmdmsg_rl)
1617 msg_col = Columns;
1618 else
1619#endif
1620 msg_col = 0;
1621 msg_putchar(' '); /* delete ':' */
1622 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001623#ifdef FEAT_SEARCH_EXTRA
1624 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001625 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 redraw_cmdline = TRUE;
1628 goto returncmd; /* back to cmd mode */
1629 }
1630 goto cmdline_changed;
1631
1632 case K_INS:
1633 case K_KINS:
1634#ifdef FEAT_FKMAP
1635 /* if Farsi mode set, we are in reverse insert mode -
1636 Do not change the mode */
1637 if (cmd_fkmap)
1638 beep_flush();
1639 else
1640#endif
1641 ccline.overstrike = !ccline.overstrike;
1642#ifdef CURSOR_SHAPE
1643 ui_cursor_shape(); /* may show different cursor shape */
1644#endif
1645 goto cmdline_not_changed;
1646
1647 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001648 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 {
1650 /* ":lmap" mappings exists, toggle use of mappings. */
1651 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001652#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 im_set_active(FALSE); /* Disable input method */
1654#endif
1655 if (b_im_ptr != NULL)
1656 {
1657 if (State & LANGMAP)
1658 *b_im_ptr = B_IMODE_LMAP;
1659 else
1660 *b_im_ptr = B_IMODE_NONE;
1661 }
1662 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 else
1665 {
1666 /* There are no ":lmap" mappings, toggle IM. When
1667 * 'imdisable' is set don't try getting the status, it's
1668 * always off. */
1669 if ((p_imdisable && b_im_ptr != NULL)
1670 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1671 {
1672 im_set_active(FALSE); /* Disable input method */
1673 if (b_im_ptr != NULL)
1674 *b_im_ptr = B_IMODE_NONE;
1675 }
1676 else
1677 {
1678 im_set_active(TRUE); /* Enable input method */
1679 if (b_im_ptr != NULL)
1680 *b_im_ptr = B_IMODE_IM;
1681 }
1682 }
1683#endif
1684 if (b_im_ptr != NULL)
1685 {
1686 if (b_im_ptr == &curbuf->b_p_iminsert)
1687 set_iminsert_global();
1688 else
1689 set_imsearch_global();
1690 }
1691#ifdef CURSOR_SHAPE
1692 ui_cursor_shape(); /* may show different cursor shape */
1693#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001694#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 /* Show/unshow value of 'keymap' in status lines later. */
1696 status_redraw_curbuf();
1697#endif
1698 goto cmdline_not_changed;
1699
1700/* case '@': only in very old vi */
1701 case Ctrl_U:
1702 /* delete all characters left of the cursor */
1703 j = ccline.cmdpos;
1704 ccline.cmdlen -= j;
1705 i = ccline.cmdpos = 0;
1706 while (i < ccline.cmdlen)
1707 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1708 /* Truncate at the end, required for multi-byte chars. */
1709 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001710#ifdef FEAT_SEARCH_EXTRA
1711 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001712 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001713#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 redrawcmd();
1715 goto cmdline_changed;
1716
1717#ifdef FEAT_CLIPBOARD
1718 case Ctrl_Y:
1719 /* Copy the modeless selection, if there is one. */
1720 if (clip_star.state != SELECT_CLEARED)
1721 {
1722 if (clip_star.state == SELECT_DONE)
1723 clip_copy_modeless_selection(TRUE);
1724 goto cmdline_not_changed;
1725 }
1726 break;
1727#endif
1728
1729 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1730 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001731 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001732 * ":normal" runs out of characters. */
1733 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001734 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735 goto cmdline_not_changed;
1736
1737 gotesc = TRUE; /* will free ccline.cmdbuff after
1738 putting it in history */
1739 goto returncmd; /* back to cmd mode */
1740
1741 case Ctrl_R: /* insert register */
1742#ifdef USE_ON_FLY_SCROLL
1743 dont_scroll = TRUE; /* disallow scrolling here */
1744#endif
1745 putcmdline('"', TRUE);
1746 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001747 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 if (i == Ctrl_O)
1749 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1750 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001751 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001752 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 --no_mapping;
1754#ifdef FEAT_EVAL
1755 /*
1756 * Insert the result of an expression.
1757 * Need to save the current command line, to be able to enter
1758 * a new one...
1759 */
1760 new_cmdpos = -1;
1761 if (c == '=')
1762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1764 {
1765 beep_flush();
1766 c = ESC;
1767 }
1768 else
1769 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001770 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001772 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 }
1774 }
1775#endif
1776 if (c != ESC) /* use ESC to cancel inserting register */
1777 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001778 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001779
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001780#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001781 /* When there was a serious error abort getting the
1782 * command line. */
1783 if (aborting())
1784 {
1785 gotesc = TRUE; /* will free ccline.cmdbuff after
1786 putting it in history */
1787 goto returncmd; /* back to cmd mode */
1788 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 KeyTyped = FALSE; /* Don't do p_wc completion. */
1791#ifdef FEAT_EVAL
1792 if (new_cmdpos >= 0)
1793 {
1794 /* set_cmdline_pos() was used */
1795 if (new_cmdpos > ccline.cmdlen)
1796 ccline.cmdpos = ccline.cmdlen;
1797 else
1798 ccline.cmdpos = new_cmdpos;
1799 }
1800#endif
1801 }
1802 redrawcmd();
1803 goto cmdline_changed;
1804
1805 case Ctrl_D:
1806 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1807 break; /* Use ^D as normal char instead */
1808
1809 redrawcmd();
1810 continue; /* don't do incremental search now */
1811
1812 case K_RIGHT:
1813 case K_S_RIGHT:
1814 case K_C_RIGHT:
1815 do
1816 {
1817 if (ccline.cmdpos >= ccline.cmdlen)
1818 break;
1819 i = cmdline_charsize(ccline.cmdpos);
1820 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1821 break;
1822 ccline.cmdspos += i;
1823#ifdef FEAT_MBYTE
1824 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001825 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 + ccline.cmdpos);
1827 else
1828#endif
1829 ++ccline.cmdpos;
1830 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001831 while ((c == K_S_RIGHT || c == K_C_RIGHT
1832 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1834#ifdef FEAT_MBYTE
1835 if (has_mbyte)
1836 set_cmdspos_cursor();
1837#endif
1838 goto cmdline_not_changed;
1839
1840 case K_LEFT:
1841 case K_S_LEFT:
1842 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001843 if (ccline.cmdpos == 0)
1844 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 do
1846 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 --ccline.cmdpos;
1848#ifdef FEAT_MBYTE
1849 if (has_mbyte) /* move to first byte of char */
1850 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1851 ccline.cmdbuff + ccline.cmdpos);
1852#endif
1853 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1854 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001855 while (ccline.cmdpos > 0
1856 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001857 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1859#ifdef FEAT_MBYTE
1860 if (has_mbyte)
1861 set_cmdspos_cursor();
1862#endif
1863 goto cmdline_not_changed;
1864
1865 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001866 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001867 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868
Bram Moolenaar4770d092006-01-12 23:22:24 +00001869#ifdef FEAT_GUI_W32
1870 /* On Win32 ignore <M-F4>, we get it when closing the window was
1871 * cancelled. */
1872 case K_F4:
1873 if (mod_mask == MOD_MASK_ALT)
1874 {
1875 redrawcmd(); /* somehow the cmdline is cleared */
1876 goto cmdline_not_changed;
1877 }
1878 break;
1879#endif
1880
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881#ifdef FEAT_MOUSE
1882 case K_MIDDLEDRAG:
1883 case K_MIDDLERELEASE:
1884 goto cmdline_not_changed; /* Ignore mouse */
1885
1886 case K_MIDDLEMOUSE:
1887# ifdef FEAT_GUI
1888 /* When GUI is active, also paste when 'mouse' is empty */
1889 if (!gui.in_use)
1890# endif
1891 if (!mouse_has(MOUSE_COMMAND))
1892 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001893# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001895 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001897# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001898 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 redrawcmd();
1900 goto cmdline_changed;
1901
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001902# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001904 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 redrawcmd();
1906 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001907# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908
1909 case K_LEFTDRAG:
1910 case K_LEFTRELEASE:
1911 case K_RIGHTDRAG:
1912 case K_RIGHTRELEASE:
1913 /* Ignore drag and release events when the button-down wasn't
1914 * seen before. */
1915 if (ignore_drag_release)
1916 goto cmdline_not_changed;
1917 /* FALLTHROUGH */
1918 case K_LEFTMOUSE:
1919 case K_RIGHTMOUSE:
1920 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1921 ignore_drag_release = TRUE;
1922 else
1923 ignore_drag_release = FALSE;
1924# ifdef FEAT_GUI
1925 /* When GUI is active, also move when 'mouse' is empty */
1926 if (!gui.in_use)
1927# endif
1928 if (!mouse_has(MOUSE_COMMAND))
1929 goto cmdline_not_changed; /* Ignore mouse */
1930# ifdef FEAT_CLIPBOARD
1931 if (mouse_row < cmdline_row && clip_star.available)
1932 {
1933 int button, is_click, is_drag;
1934
1935 /*
1936 * Handle modeless selection.
1937 */
1938 button = get_mouse_button(KEY2TERMCAP1(c),
1939 &is_click, &is_drag);
1940 if (mouse_model_popup() && button == MOUSE_LEFT
1941 && (mod_mask & MOD_MASK_SHIFT))
1942 {
1943 /* Translate shift-left to right button. */
1944 button = MOUSE_RIGHT;
1945 mod_mask &= ~MOD_MASK_SHIFT;
1946 }
1947 clip_modeless(button, is_click, is_drag);
1948 goto cmdline_not_changed;
1949 }
1950# endif
1951
1952 set_cmdspos();
1953 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1954 ++ccline.cmdpos)
1955 {
1956 i = cmdline_charsize(ccline.cmdpos);
1957 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1958 && mouse_col < ccline.cmdspos % Columns + i)
1959 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001960# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 if (has_mbyte)
1962 {
1963 /* Count ">" for double-wide char that doesn't fit. */
1964 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001965 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 + ccline.cmdpos) - 1;
1967 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001968# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 ccline.cmdspos += i;
1970 }
1971 goto cmdline_not_changed;
1972
1973 /* Mouse scroll wheel: ignored here */
1974 case K_MOUSEDOWN:
1975 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001976 case K_MOUSELEFT:
1977 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 /* Alternate buttons ignored here */
1979 case K_X1MOUSE:
1980 case K_X1DRAG:
1981 case K_X1RELEASE:
1982 case K_X2MOUSE:
1983 case K_X2DRAG:
1984 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001985 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 goto cmdline_not_changed;
1987
1988#endif /* FEAT_MOUSE */
1989
1990#ifdef FEAT_GUI
1991 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1992 case K_LEFTRELEASE_NM:
1993 goto cmdline_not_changed;
1994
1995 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001996 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 gui_do_scroll();
1999 redrawcmd();
2000 }
2001 goto cmdline_not_changed;
2002
2003 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002004 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02002006 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 redrawcmd();
2008 }
2009 goto cmdline_not_changed;
2010#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00002011#ifdef FEAT_GUI_TABLINE
2012 case K_TABLINE:
2013 case K_TABMENU:
2014 /* Don't want to change any tabs here. Make sure the same tab
2015 * is still selected. */
2016 if (gui_use_tabline())
2017 gui_mch_set_curtab(tabpage_index(curtab));
2018 goto cmdline_not_changed;
2019#endif
2020
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 case K_SELECT: /* end of Select mode mapping - ignore */
2022 goto cmdline_not_changed;
2023
2024 case Ctrl_B: /* begin of command line */
2025 case K_HOME:
2026 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 case K_S_HOME:
2028 case K_C_HOME:
2029 ccline.cmdpos = 0;
2030 set_cmdspos();
2031 goto cmdline_not_changed;
2032
2033 case Ctrl_E: /* end of command line */
2034 case K_END:
2035 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036 case K_S_END:
2037 case K_C_END:
2038 ccline.cmdpos = ccline.cmdlen;
2039 set_cmdspos_cursor();
2040 goto cmdline_not_changed;
2041
2042 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002043 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 break;
2045 goto cmdline_changed;
2046
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002047 case Ctrl_L:
2048#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002049 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002050 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002051#endif
2052
2053 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002054 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 break;
2056 goto cmdline_changed;
2057
2058 case Ctrl_N: /* next match */
2059 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002060 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002062 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2063 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002065 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002068 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 case K_UP:
2070 case K_DOWN:
2071 case K_S_UP:
2072 case K_S_DOWN:
2073 case K_PAGEUP:
2074 case K_KPAGEUP:
2075 case K_PAGEDOWN:
2076 case K_KPAGEDOWN:
2077 if (hislen == 0 || firstc == NUL) /* no history */
2078 goto cmdline_not_changed;
2079
2080 i = hiscnt;
2081
2082 /* save current command string so it can be restored later */
2083 if (lookfor == NULL)
2084 {
2085 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2086 goto cmdline_not_changed;
2087 lookfor[ccline.cmdpos] = NUL;
2088 }
2089
2090 j = (int)STRLEN(lookfor);
2091 for (;;)
2092 {
2093 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002094 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002095 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
2097 if (hiscnt == hislen) /* first time */
2098 hiscnt = hisidx[histype];
2099 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2100 hiscnt = hislen - 1;
2101 else if (hiscnt != hisidx[histype] + 1)
2102 --hiscnt;
2103 else /* at top of list */
2104 {
2105 hiscnt = i;
2106 break;
2107 }
2108 }
2109 else /* one step forwards */
2110 {
2111 /* on last entry, clear the line */
2112 if (hiscnt == hisidx[histype])
2113 {
2114 hiscnt = hislen;
2115 break;
2116 }
2117
2118 /* not on a history line, nothing to do */
2119 if (hiscnt == hislen)
2120 break;
2121 if (hiscnt == hislen - 1) /* wrap around */
2122 hiscnt = 0;
2123 else
2124 ++hiscnt;
2125 }
2126 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2127 {
2128 hiscnt = i;
2129 break;
2130 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002131 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002132 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 || STRNCMP(history[histype][hiscnt].hisstr,
2134 lookfor, (size_t)j) == 0)
2135 break;
2136 }
2137
2138 if (hiscnt != i) /* jumped to other entry */
2139 {
2140 char_u *p;
2141 int len;
2142 int old_firstc;
2143
2144 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002145 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 if (hiscnt == hislen)
2147 p = lookfor; /* back to the old one */
2148 else
2149 p = history[histype][hiscnt].hisstr;
2150
2151 if (histype == HIST_SEARCH
2152 && p != lookfor
2153 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2154 {
2155 /* Correct for the separator character used when
2156 * adding the history entry vs the one used now.
2157 * First loop: count length.
2158 * Second loop: copy the characters. */
2159 for (i = 0; i <= 1; ++i)
2160 {
2161 len = 0;
2162 for (j = 0; p[j] != NUL; ++j)
2163 {
2164 /* Replace old sep with new sep, unless it is
2165 * escaped. */
2166 if (p[j] == old_firstc
2167 && (j == 0 || p[j - 1] != '\\'))
2168 {
2169 if (i > 0)
2170 ccline.cmdbuff[len] = firstc;
2171 }
2172 else
2173 {
2174 /* Escape new sep, unless it is already
2175 * escaped. */
2176 if (p[j] == firstc
2177 && (j == 0 || p[j - 1] != '\\'))
2178 {
2179 if (i > 0)
2180 ccline.cmdbuff[len] = '\\';
2181 ++len;
2182 }
2183 if (i > 0)
2184 ccline.cmdbuff[len] = p[j];
2185 }
2186 ++len;
2187 }
2188 if (i == 0)
2189 {
2190 alloc_cmdbuff(len);
2191 if (ccline.cmdbuff == NULL)
2192 goto returncmd;
2193 }
2194 }
2195 ccline.cmdbuff[len] = NUL;
2196 }
2197 else
2198 {
2199 alloc_cmdbuff((int)STRLEN(p));
2200 if (ccline.cmdbuff == NULL)
2201 goto returncmd;
2202 STRCPY(ccline.cmdbuff, p);
2203 }
2204
2205 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2206 redrawcmd();
2207 goto cmdline_changed;
2208 }
2209 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002210#endif
2211 goto cmdline_not_changed;
2212
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002213#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002214 case Ctrl_G: /* next match */
2215 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002216 if (may_adjust_incsearch_highlighting(
2217 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002218 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002219 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#endif
2221
2222 case Ctrl_V:
2223 case Ctrl_Q:
2224#ifdef FEAT_MOUSE
2225 ignore_drag_release = TRUE;
2226#endif
2227 putcmdline('^', TRUE);
2228 c = get_literal(); /* get next (two) character(s) */
2229 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002230 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231#ifdef FEAT_MBYTE
2232 /* may need to remove ^ when composing char was typed */
2233 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2234 {
2235 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2236 msg_putchar(' ');
2237 cursorcmd();
2238 }
2239#endif
2240 break;
2241
2242#ifdef FEAT_DIGRAPHS
2243 case Ctrl_K:
2244#ifdef FEAT_MOUSE
2245 ignore_drag_release = TRUE;
2246#endif
2247 putcmdline('?', TRUE);
2248#ifdef USE_ON_FLY_SCROLL
2249 dont_scroll = TRUE; /* disallow scrolling here */
2250#endif
2251 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002252 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (c != NUL)
2254 break;
2255
2256 redrawcmd();
2257 goto cmdline_not_changed;
2258#endif /* FEAT_DIGRAPHS */
2259
2260#ifdef FEAT_RIGHTLEFT
2261 case Ctrl__: /* CTRL-_: switch language mode */
2262 if (!p_ari)
2263 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002264# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (p_altkeymap)
2266 {
2267 cmd_fkmap = !cmd_fkmap;
2268 if (cmd_fkmap) /* in Farsi always in Insert mode */
2269 ccline.overstrike = FALSE;
2270 }
2271 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002272# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 cmd_hkmap = !cmd_hkmap;
2274 goto cmdline_not_changed;
2275#endif
2276
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002277 case K_PS:
2278 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2279 goto cmdline_changed;
2280
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 default:
2282#ifdef UNIX
2283 if (c == intr_char)
2284 {
2285 gotesc = TRUE; /* will free ccline.cmdbuff after
2286 putting it in history */
2287 goto returncmd; /* back to Normal mode */
2288 }
2289#endif
2290 /*
2291 * Normal character with no special meaning. Just set mod_mask
2292 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2293 * the string <S-Space>. This should only happen after ^V.
2294 */
2295 if (!IS_SPECIAL(c))
2296 mod_mask = 0x0;
2297 break;
2298 }
2299 /*
2300 * End of switch on command line character.
2301 * We come here if we have a normal character.
2302 */
2303
Bram Moolenaarede3e632013-06-23 16:16:19 +02002304 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305#ifdef FEAT_MBYTE
2306 /* Add ABBR_OFF for characters above 0x100, this is
2307 * what check_abbr() expects. */
2308 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2309#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002310 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 goto cmdline_changed;
2312
2313 /*
2314 * put the character in the command line
2315 */
2316 if (IS_SPECIAL(c) || mod_mask != 0)
2317 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2318 else
2319 {
2320#ifdef FEAT_MBYTE
2321 if (has_mbyte)
2322 {
2323 j = (*mb_char2bytes)(c, IObuff);
2324 IObuff[j] = NUL; /* exclude composing chars */
2325 put_on_cmdline(IObuff, j, TRUE);
2326 }
2327 else
2328#endif
2329 {
2330 IObuff[0] = c;
2331 put_on_cmdline(IObuff, 1, TRUE);
2332 }
2333 }
2334 goto cmdline_changed;
2335
2336/*
2337 * This part implements incremental searches for "/" and "?"
2338 * Jump to cmdline_not_changed when a character has been read but the command
2339 * line did not change. Then we only search and redraw if something changed in
2340 * the past.
2341 * Jump to cmdline_changed when the command line did change.
2342 * (Sorry for the goto's, I know it is ugly).
2343 */
2344cmdline_not_changed:
2345#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002346 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 continue;
2348#endif
2349
2350cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002351 /* Trigger CmdlineChanged autocommands. */
2352 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002353
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002355 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357
2358#ifdef FEAT_RIGHTLEFT
2359 if (cmdmsg_rl
2360# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002361 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362# endif
2363 )
2364 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002365 * right-left typing. Not efficient, but it works.
2366 * Do it only when there are no characters left to read
2367 * to avoid useless intermediate redraws. */
2368 if (vpeekc() == NUL)
2369 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370#endif
2371 }
2372
2373returncmd:
2374
2375#ifdef FEAT_RIGHTLEFT
2376 cmdmsg_rl = FALSE;
2377#endif
2378
2379#ifdef FEAT_FKMAP
2380 cmd_fkmap = 0;
2381#endif
2382
2383 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002384 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385
2386#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002387 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388#endif
2389
2390 if (ccline.cmdbuff != NULL)
2391 {
2392 /*
2393 * Put line in history buffer (":" and "=" only when it was typed).
2394 */
2395#ifdef FEAT_CMDHIST
2396 if (ccline.cmdlen && firstc != NUL
2397 && (some_key_typed || histype == HIST_SEARCH))
2398 {
2399 add_to_history(histype, ccline.cmdbuff, TRUE,
2400 histype == HIST_SEARCH ? firstc : NUL);
2401 if (firstc == ':')
2402 {
2403 vim_free(new_last_cmdline);
2404 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2405 }
2406 }
2407#endif
2408
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002409 if (gotesc)
2410 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412
2413 /*
2414 * If the screen was shifted up, redraw the whole screen (later).
2415 * If the line is too long, clear it, so ruler and shown command do
2416 * not get printed in the middle of it.
2417 */
2418 msg_check();
2419 msg_scroll = save_msg_scroll;
2420 redir_off = FALSE;
2421
2422 /* When the command line was typed, no need for a wait-return prompt. */
2423 if (some_key_typed)
2424 need_wait_return = FALSE;
2425
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002426 /* Trigger CmdlineLeave autocommands. */
2427 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002428
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002430#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2432 im_save_status(b_im_ptr);
2433 im_set_active(FALSE);
2434#endif
2435#ifdef FEAT_MOUSE
2436 setmouse();
2437#endif
2438#ifdef CURSOR_SHAPE
2439 ui_cursor_shape(); /* may show different cursor shape */
2440#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002441 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002443 {
2444 char_u *p = ccline.cmdbuff;
2445
2446 /* Make ccline empty, getcmdline() may try to use it. */
2447 ccline.cmdbuff = NULL;
2448 return p;
2449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450}
2451
2452#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2453/*
2454 * Get a command line with a prompt.
2455 * This is prepared to be called recursively from getcmdline() (e.g. by
2456 * f_input() when evaluating an expression from CTRL-R =).
2457 * Returns the command line in allocated memory, or NULL.
2458 */
2459 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002460getcmdline_prompt(
2461 int firstc,
2462 char_u *prompt, /* command line prompt */
2463 int attr, /* attributes for prompt */
2464 int xp_context, /* type of expansion */
2465 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 char_u *s;
2468 struct cmdline_info save_ccline;
2469 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002470 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002472 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 ccline.cmdprompt = prompt;
2474 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002475# ifdef FEAT_EVAL
2476 ccline.xp_context = xp_context;
2477 ccline.xp_arg = xp_arg;
2478 ccline.input_fn = (firstc == '@');
2479# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002480 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002482 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002483 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002484 /* Restore msg_col, the prompt from input() may have changed it.
2485 * But only if called recursively and the commandline is therefore being
2486 * restored to an old one; if not, the input() prompt stays on the screen,
2487 * so we need its modified msg_col left intact. */
2488 if (ccline.cmdbuff != NULL)
2489 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490
2491 return s;
2492}
2493#endif
2494
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002495/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002496 * Return TRUE when the text must not be changed and we can't switch to
2497 * another window or buffer. Used when editing the command line, evaluating
2498 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002499 */
2500 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002501text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002502{
2503#ifdef FEAT_CMDWIN
2504 if (cmdwin_type != 0)
2505 return TRUE;
2506#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002507 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002508}
2509
2510/*
2511 * Give an error message for a command that isn't allowed while the cmdline
2512 * window is open or editing the cmdline in another way.
2513 */
2514 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002515text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002516{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002517 EMSG(_(get_text_locked_msg()));
2518}
2519
2520 char_u *
2521get_text_locked_msg(void)
2522{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002523#ifdef FEAT_CMDWIN
2524 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002525 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002526#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002527 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002528}
2529
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002530/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002531 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2532 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002533 */
2534 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002535curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002536{
2537 if (curbuf_lock > 0)
2538 {
2539 EMSG(_("E788: Not allowed to edit another buffer now"));
2540 return TRUE;
2541 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002542 return allbuf_locked();
2543}
2544
2545/*
2546 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2547 * message.
2548 */
2549 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002550allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002551{
2552 if (allbuf_lock > 0)
2553 {
2554 EMSG(_("E811: Not allowed to change buffer information now"));
2555 return TRUE;
2556 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002557 return FALSE;
2558}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002561cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2564 if (cmdline_star > 0) /* showing '*', always 1 position */
2565 return 1;
2566#endif
2567 return ptr2cells(ccline.cmdbuff + idx);
2568}
2569
2570/*
2571 * Compute the offset of the cursor on the command line for the prompt and
2572 * indent.
2573 */
2574 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002575set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002577 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 ccline.cmdspos = 1 + ccline.cmdindent;
2579 else
2580 ccline.cmdspos = 0 + ccline.cmdindent;
2581}
2582
2583/*
2584 * Compute the screen position for the cursor on the command line.
2585 */
2586 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002587set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589 int i, m, c;
2590
2591 set_cmdspos();
2592 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002593 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002595 if (m < 0) /* overflow, Columns or Rows at weird value */
2596 m = MAXCOL;
2597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 else
2599 m = MAXCOL;
2600 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2601 {
2602 c = cmdline_charsize(i);
2603#ifdef FEAT_MBYTE
2604 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2605 if (has_mbyte)
2606 correct_cmdspos(i, c);
2607#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002608 /* If the cmdline doesn't fit, show cursor on last visible char.
2609 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if ((ccline.cmdspos += c) >= m)
2611 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 ccline.cmdspos -= c;
2613 break;
2614 }
2615#ifdef FEAT_MBYTE
2616 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002617 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618#endif
2619 }
2620}
2621
2622#ifdef FEAT_MBYTE
2623/*
2624 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2625 * character that doesn't fit, so that a ">" must be displayed.
2626 */
2627 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002628correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002630 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2632 && ccline.cmdspos % Columns + cells > Columns)
2633 ccline.cmdspos++;
2634}
2635#endif
2636
2637/*
2638 * Get an Ex command line for the ":" command.
2639 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002641getexline(
2642 int c, /* normally ':', NUL for ":append" */
2643 void *cookie UNUSED,
2644 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645{
2646 /* When executing a register, remove ':' that's in front of each line. */
2647 if (exec_from_reg && vpeekc() == ':')
2648 (void)vgetc();
2649 return getcmdline(c, 1L, indent);
2650}
2651
2652/*
2653 * Get an Ex command line for Ex mode.
2654 * In Ex mode we only use the OS supplied line editing features and no
2655 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002656 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002659getexmodeline(
2660 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002661 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002662 void *cookie UNUSED,
2663 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002665 garray_T line_ga;
2666 char_u *pend;
2667 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002668 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002669 int escaped = FALSE; /* CTRL-V typed */
2670 int vcol = 0;
2671 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002672 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002673 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674
2675 /* Switch cursor on now. This avoids that it happens after the "\n", which
2676 * confuses the system function that computes tabstops. */
2677 cursor_on();
2678
2679 /* always start in column 0; write a newline if necessary */
2680 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002681 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002683 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002685 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002686 if (p_prompt)
2687 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 while (indent-- > 0)
2689 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 }
2692
2693 ga_init2(&line_ga, 1, 30);
2694
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002695 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002696 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002697 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002698 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002699 while (indent >= 8)
2700 {
2701 ga_append(&line_ga, TAB);
2702 msg_puts((char_u *)" ");
2703 indent -= 8;
2704 }
2705 while (indent-- > 0)
2706 {
2707 ga_append(&line_ga, ' ');
2708 msg_putchar(' ');
2709 }
2710 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002711 ++no_mapping;
2712 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002713
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 /*
2715 * Get the line, one character at a time.
2716 */
2717 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002718 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002720 long sw;
2721 char_u *s;
2722
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (ga_grow(&line_ga, 40) == FAIL)
2724 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
Bram Moolenaarba748c82017-02-26 14:00:07 +01002726 /*
2727 * Get one character at a time.
2728 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002729 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002730
2731 /* Check for a ":normal" command and no more characters left. */
2732 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2733 c1 = '\n';
2734 else
2735 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736
2737 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002738 * Handle line editing.
2739 * Previously this was left to the system, putting the terminal in
2740 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002742 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002744 msg_putchar('\n');
2745 break;
2746 }
2747
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002748 if (c1 == K_PS)
2749 {
2750 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2751 goto redraw;
2752 }
2753
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002754 if (!escaped)
2755 {
2756 /* CR typed means "enter", which is NL */
2757 if (c1 == '\r')
2758 c1 = '\n';
2759
2760 if (c1 == BS || c1 == K_BS
2761 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002763 if (line_ga.ga_len > 0)
2764 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002765#ifdef FEAT_MBYTE
2766 if (has_mbyte)
2767 {
2768 p = (char_u *)line_ga.ga_data;
2769 p[line_ga.ga_len] = NUL;
2770 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2771 line_ga.ga_len -= len;
2772 }
2773 else
2774#endif
2775 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002776 goto redraw;
2777 }
2778 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 }
2780
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002781 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002783 msg_col = startcol;
2784 msg_clr_eos();
2785 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002786 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002787 }
2788
2789 if (c1 == Ctrl_T)
2790 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002791 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792 p = (char_u *)line_ga.ga_data;
2793 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002794 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002795 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002796add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002797 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002799 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002800 p = (char_u *)line_ga.ga_data;
2801 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2803 *s = ' ';
2804 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002806redraw:
2807 /* redraw the line */
2808 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002809 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002810 p = (char_u *)line_ga.ga_data;
2811 p[line_ga.ga_len] = NUL;
2812 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002813 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002814 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818 msg_putchar(' ');
2819 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002820 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002824 len = MB_PTR2LEN(p);
2825 msg_outtrans_len(p, len);
2826 vcol += ptr2cells(p);
2827 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 }
2829 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002830 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002831 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002832 continue;
2833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002835 if (c1 == Ctrl_D)
2836 {
2837 /* Delete one shiftwidth. */
2838 p = (char_u *)line_ga.ga_data;
2839 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002841 if (prev_char == '^')
2842 ex_keep_indent = TRUE;
2843 indent = 0;
2844 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 }
2846 else
2847 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002848 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002849 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002850 if (indent > 0)
2851 {
2852 --indent;
2853 indent -= indent % get_sw_value(curbuf);
2854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002856 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002857 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002858 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002859 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2860 --line_ga.ga_len;
2861 }
2862 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002864
2865 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2866 {
2867 escaped = TRUE;
2868 continue;
2869 }
2870
2871 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2872 if (IS_SPECIAL(c1))
2873 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002875
2876 if (IS_SPECIAL(c1))
2877 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002878#ifdef FEAT_MBYTE
2879 if (has_mbyte)
2880 len = (*mb_char2bytes)(c1,
2881 (char_u *)line_ga.ga_data + line_ga.ga_len);
2882 else
2883#endif
2884 {
2885 len = 1;
2886 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2887 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002888 if (c1 == '\n')
2889 msg_putchar('\n');
2890 else if (c1 == TAB)
2891 {
2892 /* Don't use chartabsize(), 'ts' can be different */
2893 do
2894 {
2895 msg_putchar(' ');
2896 } while (++vcol % 8);
2897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002900 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002901 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002902 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002904 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002905 escaped = FALSE;
2906
2907 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002908 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002909
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002910 /* We are done when a NL is entered, but not when it comes after an
2911 * odd number of backslashes, that results in a NUL. */
2912 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002914 int bcount = 0;
2915
2916 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2917 ++bcount;
2918
2919 if (bcount > 0)
2920 {
2921 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2922 * "\NL", etc. */
2923 line_ga.ga_len -= (bcount + 1) / 2;
2924 pend -= (bcount + 1) / 2;
2925 pend[-1] = '\n';
2926 }
2927
2928 if ((bcount & 1) == 0)
2929 {
2930 --line_ga.ga_len;
2931 --pend;
2932 *pend = NUL;
2933 break;
2934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 }
2936 }
2937
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002938 --no_mapping;
2939 --allow_keys;
2940
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 /* make following messages go to the next line */
2942 msg_didout = FALSE;
2943 msg_col = 0;
2944 if (msg_row < Rows - 1)
2945 ++msg_row;
2946 emsg_on_display = FALSE; /* don't want ui_delay() */
2947
2948 if (got_int)
2949 ga_clear(&line_ga);
2950
2951 return (char_u *)line_ga.ga_data;
2952}
2953
Bram Moolenaare344bea2005-09-01 20:46:49 +00002954# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2955 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956/*
2957 * Return TRUE if ccline.overstrike is on.
2958 */
2959 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002960cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 return ccline.overstrike;
2963}
2964
2965/*
2966 * Return TRUE if the cursor is at the end of the cmdline.
2967 */
2968 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002969cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 return (ccline.cmdpos >= ccline.cmdlen);
2972}
2973#endif
2974
Bram Moolenaar9372a112005-12-06 19:59:18 +00002975#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976/*
2977 * Return the virtual column number at the current cursor position.
2978 * This is used by the IM code to obtain the start of the preedit string.
2979 */
2980 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002981cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982{
2983 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2984 return MAXCOL;
2985
2986# ifdef FEAT_MBYTE
2987 if (has_mbyte)
2988 {
2989 colnr_T col;
2990 int i = 0;
2991
2992 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002993 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 return col;
2996 }
2997 else
2998# endif
2999 return ccline.cmdpos;
3000}
3001#endif
3002
3003#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
3004/*
3005 * If part of the command line is an IM preedit string, redraw it with
3006 * IM feedback attributes. The cursor position is restored after drawing.
3007 */
3008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003009redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
3011 if ((State & CMDLINE)
3012 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00003013 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 && !p_imdisable
3015 && im_is_preediting())
3016 {
3017 int cmdpos = 0;
3018 int cmdspos;
3019 int old_row;
3020 int old_col;
3021 colnr_T col;
3022
3023 old_row = msg_row;
3024 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003025 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
3027# ifdef FEAT_MBYTE
3028 if (has_mbyte)
3029 {
3030 for (col = 0; col < preedit_start_col
3031 && cmdpos < ccline.cmdlen; ++col)
3032 {
3033 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003034 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 }
3036 }
3037 else
3038# endif
3039 {
3040 cmdspos += preedit_start_col;
3041 cmdpos += preedit_start_col;
3042 }
3043
3044 msg_row = cmdline_row + (cmdspos / (int)Columns);
3045 msg_col = cmdspos % (int)Columns;
3046 if (msg_row >= Rows)
3047 msg_row = Rows - 1;
3048
3049 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3050 {
3051 int char_len;
3052 int char_attr;
3053
3054 char_attr = im_get_feedback_attr(col);
3055 if (char_attr < 0)
3056 break; /* end of preedit string */
3057
3058# ifdef FEAT_MBYTE
3059 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003060 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 else
3062# endif
3063 char_len = 1;
3064
3065 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3066 cmdpos += char_len;
3067 }
3068
3069 msg_row = old_row;
3070 msg_col = old_col;
3071 }
3072}
3073#endif /* FEAT_XIM && FEAT_GUI_GTK */
3074
3075/*
3076 * Allocate a new command line buffer.
3077 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3078 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3079 */
3080 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003081alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082{
3083 /*
3084 * give some extra space to avoid having to allocate all the time
3085 */
3086 if (len < 80)
3087 len = 100;
3088 else
3089 len += 20;
3090
3091 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3092 ccline.cmdbufflen = len;
3093}
3094
3095/*
3096 * Re-allocate the command line to length len + something extra.
3097 * return FAIL for failure, OK otherwise
3098 */
3099 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003100realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101{
3102 char_u *p;
3103
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003104 if (len < ccline.cmdbufflen)
3105 return OK; /* no need to resize */
3106
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 p = ccline.cmdbuff;
3108 alloc_cmdbuff(len); /* will get some more */
3109 if (ccline.cmdbuff == NULL) /* out of memory */
3110 {
3111 ccline.cmdbuff = p; /* keep the old one */
3112 return FAIL;
3113 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003114 /* There isn't always a NUL after the command, but it may need to be
3115 * there, thus copy up to the NUL and add a NUL. */
3116 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3117 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003119
3120 if (ccline.xpc != NULL
3121 && ccline.xpc->xp_pattern != NULL
3122 && ccline.xpc->xp_context != EXPAND_NOTHING
3123 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3124 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003125 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003126
3127 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3128 * to point into the newly allocated memory. */
3129 if (i >= 0 && i <= ccline.cmdlen)
3130 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3131 }
3132
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 return OK;
3134}
3135
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003136#if defined(FEAT_ARABIC) || defined(PROTO)
3137static char_u *arshape_buf = NULL;
3138
3139# if defined(EXITFREE) || defined(PROTO)
3140 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003141free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003142{
3143 vim_free(arshape_buf);
3144}
3145# endif
3146#endif
3147
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148/*
3149 * Draw part of the cmdline at the current cursor position. But draw stars
3150 * when cmdline_star is TRUE.
3151 */
3152 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003153draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154{
3155#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3156 int i;
3157
3158 if (cmdline_star > 0)
3159 for (i = 0; i < len; ++i)
3160 {
3161 msg_putchar('*');
3162# ifdef FEAT_MBYTE
3163 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003164 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165# endif
3166 }
3167 else
3168#endif
3169#ifdef FEAT_ARABIC
3170 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 static int buflen = 0;
3173 char_u *p;
3174 int j;
3175 int newlen = 0;
3176 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003177 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 int prev_c = 0;
3179 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003180 int u8c;
3181 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183
3184 /*
3185 * Do arabic shaping into a temporary buffer. This is very
3186 * inefficient!
3187 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003188 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 {
3190 /* Re-allocate the buffer. We keep it around to avoid a lot of
3191 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003192 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003193 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003194 arshape_buf = alloc(buflen);
3195 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 return; /* out of memory */
3197 }
3198
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003199 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3200 {
3201 /* Prepend a space to draw the leading composing char on. */
3202 arshape_buf[0] = ' ';
3203 newlen = 1;
3204 }
3205
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 for (j = start; j < start + len; j += mb_l)
3207 {
3208 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003209 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003210 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 if (ARABIC_CHAR(u8c))
3212 {
3213 /* Do Arabic shaping. */
3214 if (cmdmsg_rl)
3215 {
3216 /* displaying from right to left */
3217 pc = prev_c;
3218 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003219 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 if (j + mb_l >= start + len)
3221 nc = NUL;
3222 else
3223 nc = utf_ptr2char(p + mb_l);
3224 }
3225 else
3226 {
3227 /* displaying from left to right */
3228 if (j + mb_l >= start + len)
3229 pc = NUL;
3230 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003231 {
3232 int pcc[MAX_MCO];
3233
3234 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003236 pc1 = pcc[0];
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 nc = prev_c;
3239 }
3240 prev_c = u8c;
3241
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003242 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003244 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003245 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003247 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3248 if (u8cc[1] != 0)
3249 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003250 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 }
3252 }
3253 else
3254 {
3255 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003256 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 newlen += mb_l;
3258 }
3259 }
3260
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003261 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 }
3263 else
3264#endif
3265 msg_outtrans_len(ccline.cmdbuff + start, len);
3266}
3267
3268/*
3269 * Put a character on the command line. Shifts the following text to the
3270 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3271 * "c" must be printable (fit in one display cell)!
3272 */
3273 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003274putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275{
3276 if (cmd_silent)
3277 return;
3278 msg_no_more = TRUE;
3279 msg_putchar(c);
3280 if (shift)
3281 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3282 msg_no_more = FALSE;
3283 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003284 extra_char = c;
3285 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286}
3287
3288/*
3289 * Undo a putcmdline(c, FALSE).
3290 */
3291 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003292unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293{
3294 if (cmd_silent)
3295 return;
3296 msg_no_more = TRUE;
3297 if (ccline.cmdlen == ccline.cmdpos)
3298 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003299#ifdef FEAT_MBYTE
3300 else if (has_mbyte)
3301 draw_cmdline(ccline.cmdpos,
3302 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 else
3305 draw_cmdline(ccline.cmdpos, 1);
3306 msg_no_more = FALSE;
3307 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003308 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309}
3310
3311/*
3312 * Put the given string, of the given length, onto the command line.
3313 * If len is -1, then STRLEN() is used to calculate the length.
3314 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3315 * part will be redrawn, otherwise it will not. If this function is called
3316 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3317 * called afterwards.
3318 */
3319 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003320put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322 int retval;
3323 int i;
3324 int m;
3325 int c;
3326
3327 if (len < 0)
3328 len = (int)STRLEN(str);
3329
3330 /* Check if ccline.cmdbuff needs to be longer */
3331 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003332 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 else
3334 retval = OK;
3335 if (retval == OK)
3336 {
3337 if (!ccline.overstrike)
3338 {
3339 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3340 ccline.cmdbuff + ccline.cmdpos,
3341 (size_t)(ccline.cmdlen - ccline.cmdpos));
3342 ccline.cmdlen += len;
3343 }
3344 else
3345 {
3346#ifdef FEAT_MBYTE
3347 if (has_mbyte)
3348 {
3349 /* Count nr of characters in the new string. */
3350 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003351 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352 ++m;
3353 /* Count nr of bytes in cmdline that are overwritten by these
3354 * characters. */
3355 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003356 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 --m;
3358 if (i < ccline.cmdlen)
3359 {
3360 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3361 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3362 ccline.cmdlen += ccline.cmdpos + len - i;
3363 }
3364 else
3365 ccline.cmdlen = ccline.cmdpos + len;
3366 }
3367 else
3368#endif
3369 if (ccline.cmdpos + len > ccline.cmdlen)
3370 ccline.cmdlen = ccline.cmdpos + len;
3371 }
3372 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3373 ccline.cmdbuff[ccline.cmdlen] = NUL;
3374
3375#ifdef FEAT_MBYTE
3376 if (enc_utf8)
3377 {
3378 /* When the inserted text starts with a composing character,
3379 * backup to the character before it. There could be two of them.
3380 */
3381 i = 0;
3382 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3383 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3384 {
3385 i = (*mb_head_off)(ccline.cmdbuff,
3386 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3387 ccline.cmdpos -= i;
3388 len += i;
3389 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3390 }
3391# ifdef FEAT_ARABIC
3392 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3393 {
3394 /* Check the previous character for Arabic combining pair. */
3395 i = (*mb_head_off)(ccline.cmdbuff,
3396 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3397 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3398 + ccline.cmdpos - i), c))
3399 {
3400 ccline.cmdpos -= i;
3401 len += i;
3402 }
3403 else
3404 i = 0;
3405 }
3406# endif
3407 if (i != 0)
3408 {
3409 /* Also backup the cursor position. */
3410 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3411 ccline.cmdspos -= i;
3412 msg_col -= i;
3413 if (msg_col < 0)
3414 {
3415 msg_col += Columns;
3416 --msg_row;
3417 }
3418 }
3419 }
3420#endif
3421
3422 if (redraw && !cmd_silent)
3423 {
3424 msg_no_more = TRUE;
3425 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003426 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3428 /* Avoid clearing the rest of the line too often. */
3429 if (cmdline_row != i || ccline.overstrike)
3430 msg_clr_eos();
3431 msg_no_more = FALSE;
3432 }
3433#ifdef FEAT_FKMAP
3434 /*
3435 * If we are in Farsi command mode, the character input must be in
3436 * Insert mode. So do not advance the cmdpos.
3437 */
3438 if (!cmd_fkmap)
3439#endif
3440 {
3441 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003444 if (m < 0) /* overflow, Columns or Rows at weird value */
3445 m = MAXCOL;
3446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 else
3448 m = MAXCOL;
3449 for (i = 0; i < len; ++i)
3450 {
3451 c = cmdline_charsize(ccline.cmdpos);
3452#ifdef FEAT_MBYTE
3453 /* count ">" for a double-wide char that doesn't fit. */
3454 if (has_mbyte)
3455 correct_cmdspos(ccline.cmdpos, c);
3456#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003457 /* Stop cursor at the end of the screen, but do increment the
3458 * insert position, so that entering a very long command
3459 * works, even though you can't see it. */
3460 if (ccline.cmdspos + c < m)
3461 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462#ifdef FEAT_MBYTE
3463 if (has_mbyte)
3464 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003465 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (c > len - i - 1)
3467 c = len - i - 1;
3468 ccline.cmdpos += c;
3469 i += c;
3470 }
3471#endif
3472 ++ccline.cmdpos;
3473 }
3474 }
3475 }
3476 if (redraw)
3477 msg_check();
3478 return retval;
3479}
3480
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003481static struct cmdline_info prev_ccline;
3482static int prev_ccline_used = FALSE;
3483
3484/*
3485 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3486 * and overwrite it. But get_cmdline_str() may need it, thus make it
3487 * available globally in prev_ccline.
3488 */
3489 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003490save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003491{
3492 if (!prev_ccline_used)
3493 {
3494 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3495 prev_ccline_used = TRUE;
3496 }
3497 *ccp = prev_ccline;
3498 prev_ccline = ccline;
3499 ccline.cmdbuff = NULL;
3500 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003501 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003502}
3503
3504/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003505 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003506 */
3507 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003508restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003509{
3510 ccline = prev_ccline;
3511 prev_ccline = *ccp;
3512}
3513
Bram Moolenaar5a305422006-04-28 22:38:25 +00003514#if defined(FEAT_EVAL) || defined(PROTO)
3515/*
3516 * Save the command line into allocated memory. Returns a pointer to be
3517 * passed to restore_cmdline_alloc() later.
3518 * Returns NULL when failed.
3519 */
3520 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003521save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003522{
3523 struct cmdline_info *p;
3524
3525 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3526 if (p != NULL)
3527 save_cmdline(p);
3528 return (char_u *)p;
3529}
3530
3531/*
3532 * Restore the command line from the return value of save_cmdline_alloc().
3533 */
3534 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003535restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003536{
3537 if (p != NULL)
3538 {
3539 restore_cmdline((struct cmdline_info *)p);
3540 vim_free(p);
3541 }
3542}
3543#endif
3544
Bram Moolenaar8299df92004-07-10 09:47:34 +00003545/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003546 * Paste a yank register into the command line.
3547 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003548 * insert_reg() can't be used here, because special characters from the
3549 * register contents will be interpreted as commands.
3550 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003551 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003552 */
3553 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003554cmdline_paste(
3555 int regname,
3556 int literally, /* Insert text literally instead of "as typed" */
3557 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003558{
3559 long i;
3560 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003561 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003562 int allocated;
3563 struct cmdline_info save_ccline;
3564
3565 /* check for valid regname; also accept special characters for CTRL-R in
3566 * the command line */
3567 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003568 && regname != Ctrl_A && regname != Ctrl_L
3569 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003570 return FAIL;
3571
3572 /* A register containing CTRL-R can cause an endless loop. Allow using
3573 * CTRL-C to break the loop. */
3574 line_breakcheck();
3575 if (got_int)
3576 return FAIL;
3577
3578#ifdef FEAT_CLIPBOARD
3579 regname = may_get_selection(regname);
3580#endif
3581
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003582 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003583 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003584 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003585 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003586 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003587 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003588 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003589
3590 if (i)
3591 {
3592 /* Got the value of a special register in "arg". */
3593 if (arg == NULL)
3594 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003595
3596 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3597 * part of the word. */
3598 p = arg;
3599 if (p_is && regname == Ctrl_W)
3600 {
3601 char_u *w;
3602 int len;
3603
3604 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003605 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003606 {
3607#ifdef FEAT_MBYTE
3608 if (has_mbyte)
3609 {
3610 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3611 if (!vim_iswordc(mb_ptr2char(w - len)))
3612 break;
3613 w -= len;
3614 }
3615 else
3616#endif
3617 {
3618 if (!vim_iswordc(w[-1]))
3619 break;
3620 --w;
3621 }
3622 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003623 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003624 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3625 p += len;
3626 }
3627
3628 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003629 if (allocated)
3630 vim_free(arg);
3631 return OK;
3632 }
3633
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003634 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003635}
3636
3637/*
3638 * Put a string on the command line.
3639 * When "literally" is TRUE, insert literally.
3640 * When "literally" is FALSE, insert as typed, but don't leave the command
3641 * line.
3642 */
3643 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003644cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003645{
3646 int c, cv;
3647
3648 if (literally)
3649 put_on_cmdline(s, -1, TRUE);
3650 else
3651 while (*s != NUL)
3652 {
3653 cv = *s;
3654 if (cv == Ctrl_V && s[1])
3655 ++s;
3656#ifdef FEAT_MBYTE
3657 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003658 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003659 else
3660#endif
3661 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003662 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3663 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003664#ifdef UNIX
3665 || c == intr_char
3666#endif
3667 || (c == Ctrl_BSL && *s == Ctrl_N))
3668 stuffcharReadbuff(Ctrl_V);
3669 stuffcharReadbuff(c);
3670 }
3671}
3672
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673#ifdef FEAT_WILDMENU
3674/*
3675 * Delete characters on the command line, from "from" to the current
3676 * position.
3677 */
3678 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003679cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680{
3681 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3682 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3683 ccline.cmdlen -= ccline.cmdpos - from;
3684 ccline.cmdpos = from;
3685}
3686#endif
3687
3688/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003689 * This function is called when the screen size changes and with incremental
3690 * search and in other situations where the command line may have been
3691 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 */
3693 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003694redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003696 redrawcmdline_ex(TRUE);
3697}
3698
3699 void
3700redrawcmdline_ex(int do_compute_cmdrow)
3701{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (cmd_silent)
3703 return;
3704 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003705 if (do_compute_cmdrow)
3706 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 redrawcmd();
3708 cursorcmd();
3709}
3710
3711 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003712redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713{
3714 int i;
3715
3716 if (cmd_silent)
3717 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003718 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 msg_putchar(ccline.cmdfirstc);
3720 if (ccline.cmdprompt != NULL)
3721 {
3722 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3723 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3724 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003725 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 --ccline.cmdindent;
3727 }
3728 else
3729 for (i = ccline.cmdindent; i > 0; --i)
3730 msg_putchar(' ');
3731}
3732
3733/*
3734 * Redraw what is currently on the command line.
3735 */
3736 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003737redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
3739 if (cmd_silent)
3740 return;
3741
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003742 /* when 'incsearch' is set there may be no command line while redrawing */
3743 if (ccline.cmdbuff == NULL)
3744 {
3745 windgoto(cmdline_row, 0);
3746 msg_clr_eos();
3747 return;
3748 }
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 msg_start();
3751 redrawcmdprompt();
3752
3753 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3754 msg_no_more = TRUE;
3755 draw_cmdline(0, ccline.cmdlen);
3756 msg_clr_eos();
3757 msg_no_more = FALSE;
3758
3759 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003760 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003761 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762
3763 /*
3764 * An emsg() before may have set msg_scroll. This is used in normal mode,
3765 * in cmdline mode we can reset them now.
3766 */
3767 msg_scroll = FALSE; /* next message overwrites cmdline */
3768
3769 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3770 * in cmdline mode */
3771 skip_redraw = FALSE;
3772}
3773
3774 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003775compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003777 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 cmdline_row = Rows - 1;
3779 else
3780 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003781 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782}
3783
3784 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003785cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786{
3787 if (cmd_silent)
3788 return;
3789
3790#ifdef FEAT_RIGHTLEFT
3791 if (cmdmsg_rl)
3792 {
3793 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3794 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3795 if (msg_row <= 0)
3796 msg_row = Rows - 1;
3797 }
3798 else
3799#endif
3800 {
3801 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3802 msg_col = ccline.cmdspos % (int)Columns;
3803 if (msg_row >= Rows)
3804 msg_row = Rows - 1;
3805 }
3806
3807 windgoto(msg_row, msg_col);
3808#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003809 if (p_imst == IM_ON_THE_SPOT)
3810 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811#endif
3812#ifdef MCH_CURSOR_SHAPE
3813 mch_update_cursor();
3814#endif
3815}
3816
3817 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003818gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819{
3820 msg_start();
3821#ifdef FEAT_RIGHTLEFT
3822 if (cmdmsg_rl)
3823 msg_col = Columns - 1;
3824 else
3825#endif
3826 msg_col = 0; /* always start in column 0 */
3827 if (clr) /* clear the bottom line(s) */
3828 msg_clr_eos(); /* will reset clear_cmdline */
3829 windgoto(cmdline_row, 0);
3830}
3831
3832/*
3833 * Check the word in front of the cursor for an abbreviation.
3834 * Called when the non-id character "c" has been entered.
3835 * When an abbreviation is recognized it is removed from the text with
3836 * backspaces and the replacement string is inserted, followed by "c".
3837 */
3838 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003839ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003841 int spos = 0;
3842
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3844 return FALSE;
3845
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003846 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3847 * Actually accepts any mark. */
3848 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3849 spos++;
3850 if (ccline.cmdlen - spos > 5
3851 && ccline.cmdbuff[spos] == '\''
3852 && ccline.cmdbuff[spos + 2] == ','
3853 && ccline.cmdbuff[spos + 3] == '\'')
3854 spos += 5;
3855 else
3856 /* check abbreviation from the beginning of the commandline */
3857 spos = 0;
3858
3859 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860}
3861
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003862#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3863 static int
3864#ifdef __BORLANDC__
3865_RTLENTRYF
3866#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003867sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003868{
3869 char_u *p1 = *(char_u **)s1;
3870 char_u *p2 = *(char_u **)s2;
3871
3872 if (*p1 != '<' && *p2 == '<') return -1;
3873 if (*p1 == '<' && *p2 != '<') return 1;
3874 return STRCMP(p1, p2);
3875}
3876#endif
3877
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878/*
3879 * Return FAIL if this is not an appropriate context in which to do
3880 * completion of anything, return OK if it is (even if there are no matches).
3881 * For the caller, this means that the character is just passed through like a
3882 * normal character (instead of being expanded). This allows :s/^I^D etc.
3883 */
3884 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003885nextwild(
3886 expand_T *xp,
3887 int type,
3888 int options, /* extra options for ExpandOne() */
3889 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890{
3891 int i, j;
3892 char_u *p1;
3893 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 int difflen;
3895 int v;
3896
3897 if (xp->xp_numfiles == -1)
3898 {
3899 set_expand_context(xp);
3900 cmd_showtail = expand_showtail(xp);
3901 }
3902
3903 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3904 {
3905 beep_flush();
3906 return OK; /* Something illegal on command line */
3907 }
3908 if (xp->xp_context == EXPAND_NOTHING)
3909 {
3910 /* Caller can use the character as a normal char instead */
3911 return FAIL;
3912 }
3913
3914 MSG_PUTS("..."); /* show that we are busy */
3915 out_flush();
3916
3917 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003918 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 if (type == WILD_NEXT || type == WILD_PREV)
3921 {
3922 /*
3923 * Get next/previous match for a previous expanded pattern.
3924 */
3925 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3926 }
3927 else
3928 {
3929 /*
3930 * Translate string into pattern and expand it.
3931 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003932 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3933 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 p2 = NULL;
3935 else
3936 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003937 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003938 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3939 if (escape)
3940 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003941
3942 if (p_wic)
3943 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003944 p2 = ExpandOne(xp, p1,
3945 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003946 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003948 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 if (p2 != NULL && type == WILD_LONGEST)
3950 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003951 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 if (ccline.cmdbuff[i + j] == '*'
3953 || ccline.cmdbuff[i + j] == '?')
3954 break;
3955 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003956 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 }
3958 }
3959 }
3960
3961 if (p2 != NULL && !got_int)
3962 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003963 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003964 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003966 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 xp->xp_pattern = ccline.cmdbuff + i;
3968 }
3969 else
3970 v = OK;
3971 if (v == OK)
3972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003973 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3974 &ccline.cmdbuff[ccline.cmdpos],
3975 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3976 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 ccline.cmdlen += difflen;
3978 ccline.cmdpos += difflen;
3979 }
3980 }
3981 vim_free(p2);
3982
3983 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003984 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985
3986 /* When expanding a ":map" command and no matches are found, assume that
3987 * the key is supposed to be inserted literally */
3988 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3989 return FAIL;
3990
3991 if (xp->xp_numfiles <= 0 && p2 == NULL)
3992 beep_flush();
3993 else if (xp->xp_numfiles == 1)
3994 /* free expanded pattern */
3995 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3996
3997 return OK;
3998}
3999
4000/*
4001 * Do wildcard expansion on the string 'str'.
4002 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00004003 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 * Return NULL for failure.
4005 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004006 * "orig" is the originally expanded string, copied to allocated memory. It
4007 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
4008 * WILD_PREV "orig" should be NULL.
4009 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00004010 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
4011 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 *
4013 * mode = WILD_FREE: just free previously expanded matches
4014 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4015 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4016 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4017 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4018 * mode = WILD_ALL: return all matches concatenated
4019 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004020 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 *
4022 * options = WILD_LIST_NOTFOUND: list entries without a match
4023 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4024 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4025 * options = WILD_NO_BEEP: Don't beep for multiple matches
4026 * options = WILD_ADD_SLASH: add a slash after directory names
4027 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4028 * options = WILD_SILENT: don't print warning messages
4029 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004030 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 *
4032 * The variables xp->xp_context and xp->xp_backslash must have been set!
4033 */
4034 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004035ExpandOne(
4036 expand_T *xp,
4037 char_u *str,
4038 char_u *orig, /* allocated copy of original of expanded string */
4039 int options,
4040 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041{
4042 char_u *ss = NULL;
4043 static int findex;
4044 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004045 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 int i;
4047 long_u len;
4048 int non_suf_match; /* number without matching suffix */
4049
4050 /*
4051 * first handle the case of using an old match
4052 */
4053 if (mode == WILD_NEXT || mode == WILD_PREV)
4054 {
4055 if (xp->xp_numfiles > 0)
4056 {
4057 if (mode == WILD_PREV)
4058 {
4059 if (findex == -1)
4060 findex = xp->xp_numfiles;
4061 --findex;
4062 }
4063 else /* mode == WILD_NEXT */
4064 ++findex;
4065
4066 /*
4067 * When wrapping around, return the original string, set findex to
4068 * -1.
4069 */
4070 if (findex < 0)
4071 {
4072 if (orig_save == NULL)
4073 findex = xp->xp_numfiles - 1;
4074 else
4075 findex = -1;
4076 }
4077 if (findex >= xp->xp_numfiles)
4078 {
4079 if (orig_save == NULL)
4080 findex = 0;
4081 else
4082 findex = -1;
4083 }
4084#ifdef FEAT_WILDMENU
4085 if (p_wmnu)
4086 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4087 findex, cmd_showtail);
4088#endif
4089 if (findex == -1)
4090 return vim_strsave(orig_save);
4091 return vim_strsave(xp->xp_files[findex]);
4092 }
4093 else
4094 return NULL;
4095 }
4096
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004097 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4099 {
4100 FreeWild(xp->xp_numfiles, xp->xp_files);
4101 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004102 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 findex = 0;
4105
4106 if (mode == WILD_FREE) /* only release file name */
4107 return NULL;
4108
4109 if (xp->xp_numfiles == -1)
4110 {
4111 vim_free(orig_save);
4112 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004113 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
4115 /*
4116 * Do the expansion.
4117 */
4118 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4119 options) == FAIL)
4120 {
4121#ifdef FNAME_ILLEGAL
4122 /* Illegal file name has been silently skipped. But when there
4123 * are wildcards, the real problem is that there was no match,
4124 * causing the pattern to be added, which has illegal characters.
4125 */
4126 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4127 EMSG2(_(e_nomatch2), str);
4128#endif
4129 }
4130 else if (xp->xp_numfiles == 0)
4131 {
4132 if (!(options & WILD_SILENT))
4133 EMSG2(_(e_nomatch2), str);
4134 }
4135 else
4136 {
4137 /* Escape the matches for use on the command line. */
4138 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4139
4140 /*
4141 * Check for matching suffixes in file names.
4142 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004143 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4144 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
4146 if (xp->xp_numfiles)
4147 non_suf_match = xp->xp_numfiles;
4148 else
4149 non_suf_match = 1;
4150 if ((xp->xp_context == EXPAND_FILES
4151 || xp->xp_context == EXPAND_DIRECTORIES)
4152 && xp->xp_numfiles > 1)
4153 {
4154 /*
4155 * More than one match; check suffix.
4156 * The files will have been sorted on matching suffix in
4157 * expand_wildcards, only need to check the first two.
4158 */
4159 non_suf_match = 0;
4160 for (i = 0; i < 2; ++i)
4161 if (match_suffix(xp->xp_files[i]))
4162 ++non_suf_match;
4163 }
4164 if (non_suf_match != 1)
4165 {
4166 /* Can we ever get here unless it's while expanding
4167 * interactively? If not, we can get rid of this all
4168 * together. Don't really want to wait for this message
4169 * (and possibly have to hit return to continue!).
4170 */
4171 if (!(options & WILD_SILENT))
4172 EMSG(_(e_toomany));
4173 else if (!(options & WILD_NO_BEEP))
4174 beep_flush();
4175 }
4176 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4177 ss = vim_strsave(xp->xp_files[0]);
4178 }
4179 }
4180 }
4181
4182 /* Find longest common part */
4183 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4184 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004185 int mb_len = 1;
4186 int c0, ci;
4187
4188 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004190#ifdef FEAT_MBYTE
4191 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004193 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4194 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4195 }
4196 else
4197#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004198 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004199 for (i = 1; i < xp->xp_numfiles; ++i)
4200 {
4201#ifdef FEAT_MBYTE
4202 if (has_mbyte)
4203 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4204 else
4205#endif
4206 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004207 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004209 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004210 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004212 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 break;
4214 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004215 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 break;
4217 }
4218 if (i < xp->xp_numfiles)
4219 {
4220 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004221 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 break;
4223 }
4224 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004225
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 ss = alloc((unsigned)len + 1);
4227 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004228 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 findex = -1; /* next p_wc gets first one */
4230 }
4231
4232 /* Concatenate all matching names */
4233 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4234 {
4235 len = 0;
4236 for (i = 0; i < xp->xp_numfiles; ++i)
4237 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4238 ss = lalloc(len, TRUE);
4239 if (ss != NULL)
4240 {
4241 *ss = NUL;
4242 for (i = 0; i < xp->xp_numfiles; ++i)
4243 {
4244 STRCAT(ss, xp->xp_files[i]);
4245 if (i != xp->xp_numfiles - 1)
4246 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4247 }
4248 }
4249 }
4250
4251 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4252 ExpandCleanup(xp);
4253
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004254 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004255 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004256 vim_free(orig);
4257
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return ss;
4259}
4260
4261/*
4262 * Prepare an expand structure for use.
4263 */
4264 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004265ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004267 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004268 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004270#ifndef BACKSLASH_IN_FILENAME
4271 xp->xp_shell = FALSE;
4272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 xp->xp_numfiles = -1;
4274 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004275#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4276 xp->xp_arg = NULL;
4277#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004278 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279}
4280
4281/*
4282 * Cleanup an expand structure after use.
4283 */
4284 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004285ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286{
4287 if (xp->xp_numfiles >= 0)
4288 {
4289 FreeWild(xp->xp_numfiles, xp->xp_files);
4290 xp->xp_numfiles = -1;
4291 }
4292}
4293
4294 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004295ExpandEscape(
4296 expand_T *xp,
4297 char_u *str,
4298 int numfiles,
4299 char_u **files,
4300 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301{
4302 int i;
4303 char_u *p;
4304
4305 /*
4306 * May change home directory back to "~"
4307 */
4308 if (options & WILD_HOME_REPLACE)
4309 tilde_replace(str, numfiles, files);
4310
4311 if (options & WILD_ESCAPE)
4312 {
4313 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004314 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004315 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 || xp->xp_context == EXPAND_BUFFERS
4317 || xp->xp_context == EXPAND_DIRECTORIES)
4318 {
4319 /*
4320 * Insert a backslash into a file name before a space, \, %, #
4321 * and wildmatch characters, except '~'.
4322 */
4323 for (i = 0; i < numfiles; ++i)
4324 {
4325 /* for ":set path=" we need to escape spaces twice */
4326 if (xp->xp_backslash == XP_BS_THREE)
4327 {
4328 p = vim_strsave_escaped(files[i], (char_u *)" ");
4329 if (p != NULL)
4330 {
4331 vim_free(files[i]);
4332 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004333#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 p = vim_strsave_escaped(files[i], (char_u *)" ");
4335 if (p != NULL)
4336 {
4337 vim_free(files[i]);
4338 files[i] = p;
4339 }
4340#endif
4341 }
4342 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004343#ifdef BACKSLASH_IN_FILENAME
4344 p = vim_strsave_fnameescape(files[i], FALSE);
4345#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004346 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 if (p != NULL)
4349 {
4350 vim_free(files[i]);
4351 files[i] = p;
4352 }
4353
4354 /* If 'str' starts with "\~", replace "~" at start of
4355 * files[i] with "\~". */
4356 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004357 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004360
4361 /* If the first file starts with a '+' escape it. Otherwise it
4362 * could be seen as "+cmd". */
4363 if (*files[0] == '+')
4364 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 else if (xp->xp_context == EXPAND_TAGS)
4367 {
4368 /*
4369 * Insert a backslash before characters in a tag name that
4370 * would terminate the ":tag" command.
4371 */
4372 for (i = 0; i < numfiles; ++i)
4373 {
4374 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4375 if (p != NULL)
4376 {
4377 vim_free(files[i]);
4378 files[i] = p;
4379 }
4380 }
4381 }
4382 }
4383}
4384
4385/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004386 * Escape special characters in "fname" for when used as a file name argument
4387 * after a Vim command, or, when "shell" is non-zero, a shell command.
4388 * Returns the result in allocated memory.
4389 */
4390 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004391vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004392{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004393 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004394#ifdef BACKSLASH_IN_FILENAME
4395 char_u buf[20];
4396 int j = 0;
4397
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004398 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004399 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004400 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004401 buf[j++] = *p;
4402 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004403 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004404#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004405 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4406 if (shell && csh_like_shell() && p != NULL)
4407 {
4408 char_u *s;
4409
4410 /* For csh and similar shells need to put two backslashes before '!'.
4411 * One is taken by Vim, one by the shell. */
4412 s = vim_strsave_escaped(p, (char_u *)"!");
4413 vim_free(p);
4414 p = s;
4415 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004416#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004417
4418 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4419 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004420 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004421 escape_fname(&p);
4422
4423 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004424}
4425
4426/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004427 * Put a backslash before the file name in "pp", which is in allocated memory.
4428 */
4429 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004430escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004431{
4432 char_u *p;
4433
4434 p = alloc((unsigned)(STRLEN(*pp) + 2));
4435 if (p != NULL)
4436 {
4437 p[0] = '\\';
4438 STRCPY(p + 1, *pp);
4439 vim_free(*pp);
4440 *pp = p;
4441 }
4442}
4443
4444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 * For each file name in files[num_files]:
4446 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4447 */
4448 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004449tilde_replace(
4450 char_u *orig_pat,
4451 int num_files,
4452 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453{
4454 int i;
4455 char_u *p;
4456
4457 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4458 {
4459 for (i = 0; i < num_files; ++i)
4460 {
4461 p = home_replace_save(NULL, files[i]);
4462 if (p != NULL)
4463 {
4464 vim_free(files[i]);
4465 files[i] = p;
4466 }
4467 }
4468 }
4469}
4470
4471/*
4472 * Show all matches for completion on the command line.
4473 * Returns EXPAND_NOTHING when the character that triggered expansion should
4474 * be inserted like a normal character.
4475 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004477showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478{
4479#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4480 int num_files;
4481 char_u **files_found;
4482 int i, j, k;
4483 int maxlen;
4484 int lines;
4485 int columns;
4486 char_u *p;
4487 int lastlen;
4488 int attr;
4489 int showtail;
4490
4491 if (xp->xp_numfiles == -1)
4492 {
4493 set_expand_context(xp);
4494 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4495 &num_files, &files_found);
4496 showtail = expand_showtail(xp);
4497 if (i != EXPAND_OK)
4498 return i;
4499
4500 }
4501 else
4502 {
4503 num_files = xp->xp_numfiles;
4504 files_found = xp->xp_files;
4505 showtail = cmd_showtail;
4506 }
4507
4508#ifdef FEAT_WILDMENU
4509 if (!wildmenu)
4510 {
4511#endif
4512 msg_didany = FALSE; /* lines_left will be set */
4513 msg_start(); /* prepare for paging */
4514 msg_putchar('\n');
4515 out_flush();
4516 cmdline_row = msg_row;
4517 msg_didany = FALSE; /* lines_left will be set again */
4518 msg_start(); /* prepare for paging */
4519#ifdef FEAT_WILDMENU
4520 }
4521#endif
4522
4523 if (got_int)
4524 got_int = FALSE; /* only int. the completion, not the cmd line */
4525#ifdef FEAT_WILDMENU
4526 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004527 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528#endif
4529 else
4530 {
4531 /* find the length of the longest file name */
4532 maxlen = 0;
4533 for (i = 0; i < num_files; ++i)
4534 {
4535 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004536 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 || xp->xp_context == EXPAND_BUFFERS))
4538 {
4539 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4540 j = vim_strsize(NameBuff);
4541 }
4542 else
4543 j = vim_strsize(L_SHOWFILE(i));
4544 if (j > maxlen)
4545 maxlen = j;
4546 }
4547
4548 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4549 lines = num_files;
4550 else
4551 {
4552 /* compute the number of columns and lines for the listing */
4553 maxlen += 2; /* two spaces between file names */
4554 columns = ((int)Columns + 2) / maxlen;
4555 if (columns < 1)
4556 columns = 1;
4557 lines = (num_files + columns - 1) / columns;
4558 }
4559
Bram Moolenaar8820b482017-03-16 17:23:31 +01004560 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
4562 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4563 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004564 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 msg_clr_eos();
4566 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004567 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 }
4569
4570 /* list the files line by line */
4571 for (i = 0; i < lines; ++i)
4572 {
4573 lastlen = 999;
4574 for (k = i; k < num_files; k += lines)
4575 {
4576 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4577 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004578 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 p = files_found[k] + STRLEN(files_found[k]) + 1;
4580 msg_advance(maxlen + 1);
4581 msg_puts(p);
4582 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004583 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585 }
4586 for (j = maxlen - lastlen; --j >= 0; )
4587 msg_putchar(' ');
4588 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004589 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 || xp->xp_context == EXPAND_BUFFERS)
4591 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004592 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004593 if (xp->xp_numfiles != -1)
4594 {
4595 char_u *halved_slash;
4596 char_u *exp_path;
4597
4598 /* Expansion was done before and special characters
4599 * were escaped, need to halve backslashes. Also
4600 * $HOME has been replaced with ~/. */
4601 exp_path = expand_env_save_opt(files_found[k], TRUE);
4602 halved_slash = backslash_halve_save(
4603 exp_path != NULL ? exp_path : files_found[k]);
4604 j = mch_isdir(halved_slash != NULL ? halved_slash
4605 : files_found[k]);
4606 vim_free(exp_path);
4607 vim_free(halved_slash);
4608 }
4609 else
4610 /* Expansion was done here, file names are literal. */
4611 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 if (showtail)
4613 p = L_SHOWFILE(k);
4614 else
4615 {
4616 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4617 TRUE);
4618 p = NameBuff;
4619 }
4620 }
4621 else
4622 {
4623 j = FALSE;
4624 p = L_SHOWFILE(k);
4625 }
4626 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4627 }
4628 if (msg_col > 0) /* when not wrapped around */
4629 {
4630 msg_clr_eos();
4631 msg_putchar('\n');
4632 }
4633 out_flush(); /* show one line at a time */
4634 if (got_int)
4635 {
4636 got_int = FALSE;
4637 break;
4638 }
4639 }
4640
4641 /*
4642 * we redraw the command below the lines that we have just listed
4643 * This is a bit tricky, but it saves a lot of screen updating.
4644 */
4645 cmdline_row = msg_row; /* will put it back later */
4646 }
4647
4648 if (xp->xp_numfiles == -1)
4649 FreeWild(num_files, files_found);
4650
4651 return EXPAND_OK;
4652}
4653
4654/*
4655 * Private gettail for showmatches() (and win_redr_status_matches()):
4656 * Find tail of file name path, but ignore trailing "/".
4657 */
4658 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004659sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660{
4661 char_u *p;
4662 char_u *t = s;
4663 int had_sep = FALSE;
4664
4665 for (p = s; *p != NUL; )
4666 {
4667 if (vim_ispathsep(*p)
4668#ifdef BACKSLASH_IN_FILENAME
4669 && !rem_backslash(p)
4670#endif
4671 )
4672 had_sep = TRUE;
4673 else if (had_sep)
4674 {
4675 t = p;
4676 had_sep = FALSE;
4677 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004678 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680 return t;
4681}
4682
4683/*
4684 * Return TRUE if we only need to show the tail of completion matches.
4685 * When not completing file names or there is a wildcard in the path FALSE is
4686 * returned.
4687 */
4688 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004689expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690{
4691 char_u *s;
4692 char_u *end;
4693
4694 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004695 if (xp->xp_context != EXPAND_FILES
4696 && xp->xp_context != EXPAND_SHELLCMD
4697 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 return FALSE;
4699
4700 end = gettail(xp->xp_pattern);
4701 if (end == xp->xp_pattern) /* there is no path separator */
4702 return FALSE;
4703
4704 for (s = xp->xp_pattern; s < end; s++)
4705 {
4706 /* Skip escaped wildcards. Only when the backslash is not a path
4707 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4708 if (rem_backslash(s))
4709 ++s;
4710 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4711 return FALSE;
4712 }
4713 return TRUE;
4714}
4715
4716/*
4717 * Prepare a string for expansion.
4718 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004719 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 * When expanding other names: The string will be used with regcomp(). Copy
4721 * the name into allocated memory and prepend "^".
4722 */
4723 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004724addstar(
4725 char_u *fname,
4726 int len,
4727 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
4729 char_u *retval;
4730 int i, j;
4731 int new_len;
4732 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004733 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004735 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004736 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004737 && context != EXPAND_SHELLCMD
4738 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 /*
4741 * Matching will be done internally (on something other than files).
4742 * So we convert the file-matching-type wildcards into our kind for
4743 * use with vim_regcomp(). First work out how long it will be:
4744 */
4745
4746 /* For help tags the translation is done in find_help_tags().
4747 * For a tag pattern starting with "/" no translation is needed. */
4748 if (context == EXPAND_HELP
4749 || context == EXPAND_COLORS
4750 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004751 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004752 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004753 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004754 || ((context == EXPAND_TAGS_LISTFILES
4755 || context == EXPAND_TAGS)
4756 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 retval = vim_strnsave(fname, len);
4758 else
4759 {
4760 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4761 for (i = 0; i < len; i++)
4762 {
4763 if (fname[i] == '*' || fname[i] == '~')
4764 new_len++; /* '*' needs to be replaced by ".*"
4765 '~' needs to be replaced by "\~" */
4766
4767 /* Buffer names are like file names. "." should be literal */
4768 if (context == EXPAND_BUFFERS && fname[i] == '.')
4769 new_len++; /* "." becomes "\." */
4770
4771 /* Custom expansion takes care of special things, match
4772 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004773 if ((context == EXPAND_USER_DEFINED
4774 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 new_len++; /* '\' becomes "\\" */
4776 }
4777 retval = alloc(new_len);
4778 if (retval != NULL)
4779 {
4780 retval[0] = '^';
4781 j = 1;
4782 for (i = 0; i < len; i++, j++)
4783 {
4784 /* Skip backslash. But why? At least keep it for custom
4785 * expansion. */
4786 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004787 && context != EXPAND_USER_LIST
4788 && fname[i] == '\\'
4789 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 break;
4791
4792 switch (fname[i])
4793 {
4794 case '*': retval[j++] = '.';
4795 break;
4796 case '~': retval[j++] = '\\';
4797 break;
4798 case '?': retval[j] = '.';
4799 continue;
4800 case '.': if (context == EXPAND_BUFFERS)
4801 retval[j++] = '\\';
4802 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004803 case '\\': if (context == EXPAND_USER_DEFINED
4804 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 retval[j++] = '\\';
4806 break;
4807 }
4808 retval[j] = fname[i];
4809 }
4810 retval[j] = NUL;
4811 }
4812 }
4813 }
4814 else
4815 {
4816 retval = alloc(len + 4);
4817 if (retval != NULL)
4818 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004819 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820
4821 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004822 * Don't add a star to *, ~, ~user, $var or `cmd`.
4823 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 * ~ would be at the start of the file name, but not the tail.
4825 * $ could be anywhere in the tail.
4826 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004827 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 */
4829 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004830 ends_in_star = (len > 0 && retval[len - 1] == '*');
4831#ifndef BACKSLASH_IN_FILENAME
4832 for (i = len - 2; i >= 0; --i)
4833 {
4834 if (retval[i] != '\\')
4835 break;
4836 ends_in_star = !ends_in_star;
4837 }
4838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004840 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 && vim_strchr(tail, '$') == NULL
4842 && vim_strchr(retval, '`') == NULL)
4843 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004844 else if (len > 0 && retval[len - 1] == '$')
4845 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 retval[len] = NUL;
4847 }
4848 }
4849 return retval;
4850}
4851
4852/*
4853 * Must parse the command line so far to work out what context we are in.
4854 * Completion can then be done based on that context.
4855 * This routine sets the variables:
4856 * xp->xp_pattern The start of the pattern to be expanded within
4857 * the command line (ends at the cursor).
4858 * xp->xp_context The type of thing to expand. Will be one of:
4859 *
4860 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4861 * the command line, like an unknown command. Caller
4862 * should beep.
4863 * EXPAND_NOTHING Unrecognised context for completion, use char like
4864 * a normal char, rather than for completion. eg
4865 * :s/^I/
4866 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4867 * it.
4868 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4869 * EXPAND_FILES After command with XFILE set, or after setting
4870 * with P_EXPAND set. eg :e ^I, :w>>^I
4871 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4872 * when we know only directories are of interest. eg
4873 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004874 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4876 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4877 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4878 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4879 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4880 * EXPAND_EVENTS Complete event names
4881 * EXPAND_SYNTAX Complete :syntax command arguments
4882 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4883 * EXPAND_AUGROUP Complete autocommand group names
4884 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4885 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4886 * eg :unmap a^I , :cunab x^I
4887 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4888 * eg :call sub^I
4889 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4890 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4891 * names in expressions, eg :while s^I
4892 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004893 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 */
4895 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004896set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004898 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (ccline.cmdfirstc != ':'
4900#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004901 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004902 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#endif
4904 )
4905 {
4906 xp->xp_context = EXPAND_NOTHING;
4907 return;
4908 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004909 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910}
4911
4912 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004913set_cmd_context(
4914 expand_T *xp,
4915 char_u *str, /* start of command line */
4916 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004917 int col, /* position of cursor */
4918 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919{
4920 int old_char = NUL;
4921 char_u *nextcomm;
4922
4923 /*
4924 * Avoid a UMR warning from Purify, only save the character if it has been
4925 * written before.
4926 */
4927 if (col < len)
4928 old_char = str[col];
4929 str[col] = NUL;
4930 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004931
4932#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004933 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004934 {
4935# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004936 /* pass CMD_SIZE because there is no real command */
4937 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004938# endif
4939 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004940 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004941 {
4942 xp->xp_context = ccline.xp_context;
4943 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004944# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004945 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004946# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004947 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004948 else
4949#endif
4950 while (nextcomm != NULL)
4951 nextcomm = set_one_cmd_context(xp, nextcomm);
4952
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004953 /* Store the string here so that call_user_expand_func() can get to them
4954 * easily. */
4955 xp->xp_line = str;
4956 xp->xp_col = col;
4957
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 str[col] = old_char;
4959}
4960
4961/*
4962 * Expand the command line "str" from context "xp".
4963 * "xp" must have been set by set_cmd_context().
4964 * xp->xp_pattern points into "str", to where the text that is to be expanded
4965 * starts.
4966 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4967 * cursor.
4968 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4969 * key that triggered expansion literally.
4970 * Returns EXPAND_OK otherwise.
4971 */
4972 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004973expand_cmdline(
4974 expand_T *xp,
4975 char_u *str, /* start of command line */
4976 int col, /* position of cursor */
4977 int *matchcount, /* return: nr of matches */
4978 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979{
4980 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004981 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982
4983 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4984 {
4985 beep_flush();
4986 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4987 }
4988 if (xp->xp_context == EXPAND_NOTHING)
4989 {
4990 /* Caller can use the character as a normal char instead */
4991 return EXPAND_NOTHING;
4992 }
4993
4994 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004995 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4996 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 if (file_str == NULL)
4998 return EXPAND_UNSUCCESSFUL;
4999
Bram Moolenaar94950a92010-12-02 16:01:29 +01005000 if (p_wic)
5001 options += WILD_ICASE;
5002
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01005004 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 *matchcount = 0;
5007 *matches = NULL;
5008 }
5009 vim_free(file_str);
5010
5011 return EXPAND_OK;
5012}
5013
5014#ifdef FEAT_MULTI_LANG
5015/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005016 * Cleanup matches for help tags:
5017 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5018 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005020static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021
5022 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005023cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024{
5025 int i, j;
5026 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005027 char_u buf[4];
5028 char_u *p = buf;
5029
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005030 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005031 {
5032 *p++ = '@';
5033 *p++ = p_hlg[0];
5034 *p++ = p_hlg[1];
5035 }
5036 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 for (i = 0; i < num_file; ++i)
5039 {
5040 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005041 if (len <= 0)
5042 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005043 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 /* Sorting on priority means the same item in another language may
5046 * be anywhere. Search all items for a match up to the "@en". */
5047 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005048 if (j != i && (int)STRLEN(file[j]) == len + 3
5049 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 break;
5051 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005052 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 file[i][len] = NUL;
5054 }
5055 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005056
5057 if (*buf != NUL)
5058 for (i = 0; i < num_file; ++i)
5059 {
5060 len = (int)STRLEN(file[i]) - 3;
5061 if (len <= 0)
5062 continue;
5063 if (STRCMP(file[i] + len, buf) == 0)
5064 {
5065 /* remove the default language */
5066 file[i][len] = NUL;
5067 }
5068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069}
5070#endif
5071
5072/*
5073 * Do the expansion based on xp->xp_context and "pat".
5074 */
5075 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005076ExpandFromContext(
5077 expand_T *xp,
5078 char_u *pat,
5079 int *num_file,
5080 char_u ***file,
5081 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082{
5083#ifdef FEAT_CMDL_COMPL
5084 regmatch_T regmatch;
5085#endif
5086 int ret;
5087 int flags;
5088
5089 flags = EW_DIR; /* include directories */
5090 if (options & WILD_LIST_NOTFOUND)
5091 flags |= EW_NOTFOUND;
5092 if (options & WILD_ADD_SLASH)
5093 flags |= EW_ADDSLASH;
5094 if (options & WILD_KEEP_ALL)
5095 flags |= EW_KEEPALL;
5096 if (options & WILD_SILENT)
5097 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005098 if (options & WILD_ALLLINKS)
5099 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005101 if (xp->xp_context == EXPAND_FILES
5102 || xp->xp_context == EXPAND_DIRECTORIES
5103 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
5105 /*
5106 * Expand file or directory names.
5107 */
5108 int free_pat = FALSE;
5109 int i;
5110
5111 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5112 * space */
5113 if (xp->xp_backslash != XP_BS_NONE)
5114 {
5115 free_pat = TRUE;
5116 pat = vim_strsave(pat);
5117 for (i = 0; pat[i]; ++i)
5118 if (pat[i] == '\\')
5119 {
5120 if (xp->xp_backslash == XP_BS_THREE
5121 && pat[i + 1] == '\\'
5122 && pat[i + 2] == '\\'
5123 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005124 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 if (xp->xp_backslash == XP_BS_ONE
5126 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005127 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129 }
5130
5131 if (xp->xp_context == EXPAND_FILES)
5132 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005133 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5134 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 else
5136 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005137 if (options & WILD_ICASE)
5138 flags |= EW_ICASE;
5139
Bram Moolenaard7834d32009-12-02 16:14:36 +00005140 /* Expand wildcards, supporting %:h and the like. */
5141 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 if (free_pat)
5143 vim_free(pat);
5144 return ret;
5145 }
5146
5147 *file = (char_u **)"";
5148 *num_file = 0;
5149 if (xp->xp_context == EXPAND_HELP)
5150 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005151 /* With an empty argument we would get all the help tags, which is
5152 * very slow. Get matches for "help" instead. */
5153 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5154 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
5156#ifdef FEAT_MULTI_LANG
5157 cleanup_help_tags(*num_file, *file);
5158#endif
5159 return OK;
5160 }
5161 return FAIL;
5162 }
5163
5164#ifndef FEAT_CMDL_COMPL
5165 return FAIL;
5166#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005167 if (xp->xp_context == EXPAND_SHELLCMD)
5168 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 if (xp->xp_context == EXPAND_OLD_SETTING)
5170 return ExpandOldSetting(num_file, file);
5171 if (xp->xp_context == EXPAND_BUFFERS)
5172 return ExpandBufnames(pat, num_file, file, options);
5173 if (xp->xp_context == EXPAND_TAGS
5174 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5175 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5176 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005177 {
5178 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005179 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5180 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005183 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005184 char *directories[] = {"compiler", 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_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005188 {
5189 char *directories[] = {"syntax", 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 Moolenaar1587a1e2010-07-29 20:59:59 +02005192 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005193 {
5194 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005195 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005196 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005197# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5198 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005199 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005200# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005201 if (xp->xp_context == EXPAND_PACKADD)
5202 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203
5204 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5205 if (regmatch.regprog == NULL)
5206 return FAIL;
5207
5208 /* set ignore-case according to p_ic, p_scs and pat */
5209 regmatch.rm_ic = ignorecase(pat);
5210
5211 if (xp->xp_context == EXPAND_SETTINGS
5212 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5213 ret = ExpandSettings(xp, &regmatch, num_file, file);
5214 else if (xp->xp_context == EXPAND_MAPPINGS)
5215 ret = ExpandMappings(&regmatch, num_file, file);
5216# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5217 else if (xp->xp_context == EXPAND_USER_DEFINED)
5218 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5219# endif
5220 else
5221 {
5222 static struct expgen
5223 {
5224 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005225 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005227 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 } tab[] =
5229 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005230 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5231 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005232 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005233 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005234#ifdef FEAT_CMDHIST
5235 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005238 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005239 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005240 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5241 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5242 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243#endif
5244#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005245 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5246 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5247 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5248 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249#endif
5250#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005251 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5252 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253#endif
5254#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005255 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005257#ifdef FEAT_PROFILE
5258 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5259#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005260 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005261 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5262 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005263#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005264 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005265#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005266#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005267 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005268#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005269#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005270 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5273 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005274 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5275 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005277 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005278 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005279 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 };
5281 int i;
5282
5283 /*
5284 * Find a context in the table and call the ExpandGeneric() with the
5285 * right function to do the expansion.
5286 */
5287 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005288 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 if (xp->xp_context == tab[i].context)
5290 {
5291 if (tab[i].ic)
5292 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005293 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005294 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 break;
5296 }
5297 }
5298
Bram Moolenaar473de612013-06-08 18:19:48 +02005299 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 return ret;
5302#endif /* FEAT_CMDL_COMPL */
5303}
5304
5305#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5306/*
5307 * Expand a list of names.
5308 *
5309 * Generic function for command line completion. It calls a function to
5310 * obtain strings, one by one. The strings are matched against a regexp
5311 * program. Matching strings are copied into an array, which is returned.
5312 *
5313 * Returns OK when no problems encountered, FAIL for error (out of memory).
5314 */
5315 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005316ExpandGeneric(
5317 expand_T *xp,
5318 regmatch_T *regmatch,
5319 int *num_file,
5320 char_u ***file,
5321 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005323 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324{
5325 int i;
5326 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005327 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 char_u *str;
5329
5330 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005331 * round == 0: count the number of matching names
5332 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005334 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 {
5336 for (i = 0; ; ++i)
5337 {
5338 str = (*func)(xp, i);
5339 if (str == NULL) /* end of list */
5340 break;
5341 if (*str == NUL) /* skip empty strings */
5342 continue;
5343
5344 if (vim_regexec(regmatch, str, (colnr_T)0))
5345 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005346 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005348 if (escaped)
5349 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5350 else
5351 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 (*file)[count] = str;
5353#ifdef FEAT_MENU
5354 if (func == get_menu_names && str != NULL)
5355 {
5356 /* test for separator added by get_menu_names() */
5357 str += STRLEN(str) - 1;
5358 if (*str == '\001')
5359 *str = '.';
5360 }
5361#endif
5362 }
5363 ++count;
5364 }
5365 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005366 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 {
5368 if (count == 0)
5369 return OK;
5370 *num_file = count;
5371 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5372 if (*file == NULL)
5373 {
5374 *file = (char_u **)"";
5375 return FAIL;
5376 }
5377 count = 0;
5378 }
5379 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005380
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005381 /* Sort the results. Keep menu's in the specified order. */
5382 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005383 {
5384 if (xp->xp_context == EXPAND_EXPRESSION
5385 || xp->xp_context == EXPAND_FUNCTIONS
5386 || xp->xp_context == EXPAND_USER_FUNC)
5387 /* <SNR> functions should be sorted to the end. */
5388 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5389 sort_func_compare);
5390 else
5391 sort_strings(*file, *num_file);
5392 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005393
Bram Moolenaar4f688582007-07-24 12:34:30 +00005394#ifdef FEAT_CMDL_COMPL
5395 /* Reset the variables used for special highlight names expansion, so that
5396 * they don't show up when getting normal highlight names by ID. */
5397 reset_expand_highlight();
5398#endif
5399
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 return OK;
5401}
5402
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005403/*
5404 * Complete a shell command.
5405 * Returns FAIL or OK;
5406 */
5407 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005408expand_shellcmd(
5409 char_u *filepat, /* pattern to match with command names */
5410 int *num_file, /* return: number of matches */
5411 char_u ***file, /* return: array with matches */
5412 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005413{
5414 char_u *pat;
5415 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005416 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005417 int mustfree = FALSE;
5418 garray_T ga;
5419 char_u *buf = alloc(MAXPATHL);
5420 size_t l;
5421 char_u *s, *e;
5422 int flags = flagsarg;
5423 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005424 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005425 hashtab_T found_ht;
5426 hashitem_T *hi;
5427 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005428
5429 if (buf == NULL)
5430 return FAIL;
5431
5432 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5433 * space */
5434 pat = vim_strsave(filepat);
5435 for (i = 0; pat[i]; ++i)
5436 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005437 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005438
Bram Moolenaarb5971142015-03-21 17:32:19 +01005439 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005440
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005441 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5442 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005443 path = (char_u *)".";
5444 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005445 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005446 /* For an absolute name we don't use $PATH. */
5447 if (!mch_isFullName(pat))
5448 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005449 if (path == NULL)
5450 path = (char_u *)"";
5451 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005452
5453 /*
5454 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005455 * collect them in "ga". When "." is not in $PATH also expand for the
5456 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005457 */
5458 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005459 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005460 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005461 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005462#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005463 e = vim_strchr(s, ';');
5464#else
5465 e = vim_strchr(s, ':');
5466#endif
5467 if (e == NULL)
5468 e = s + STRLEN(s);
5469
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005470 if (*s == NUL)
5471 {
5472 if (did_curdir)
5473 break;
5474 // Find directories in the current directory, path is empty.
5475 did_curdir = TRUE;
5476 flags |= EW_DIR;
5477 }
5478 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5479 {
5480 did_curdir = TRUE;
5481 flags |= EW_DIR;
5482 }
5483 else
5484 // Do not match directories inside a $PATH item.
5485 flags &= ~EW_DIR;
5486
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005487 l = e - s;
5488 if (l > MAXPATHL - 5)
5489 break;
5490 vim_strncpy(buf, s, l);
5491 add_pathsep(buf);
5492 l = STRLEN(buf);
5493 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5494
5495 /* Expand matches in one directory of $PATH. */
5496 ret = expand_wildcards(1, &buf, num_file, file, flags);
5497 if (ret == OK)
5498 {
5499 if (ga_grow(&ga, *num_file) == FAIL)
5500 FreeWild(*num_file, *file);
5501 else
5502 {
5503 for (i = 0; i < *num_file; ++i)
5504 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005505 char_u *name = (*file)[i];
5506
5507 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005508 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005509 // Check if this name was already found.
5510 hash = hash_hash(name + l);
5511 hi = hash_lookup(&found_ht, name + l, hash);
5512 if (HASHITEM_EMPTY(hi))
5513 {
5514 // Remove the path that was prepended.
5515 STRMOVE(name, name + l);
5516 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5517 hash_add_item(&found_ht, hi, name, hash);
5518 name = NULL;
5519 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005520 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005521 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005522 }
5523 vim_free(*file);
5524 }
5525 }
5526 if (*e != NUL)
5527 ++e;
5528 }
5529 *file = ga.ga_data;
5530 *num_file = ga.ga_len;
5531
5532 vim_free(buf);
5533 vim_free(pat);
5534 if (mustfree)
5535 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005536 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005537 return OK;
5538}
5539
5540
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5542/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005543 * Call "user_expand_func()" to invoke a user defined Vim script function and
5544 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005546 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005547call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005548 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005549 expand_T *xp,
5550 int *num_file,
5551 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005553 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005554 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005556 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005557 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005558 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
Bram Moolenaarb7515462013-06-29 12:58:33 +02005560 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005561 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 *num_file = 0;
5563 *file = NULL;
5564
Bram Moolenaarb7515462013-06-29 12:58:33 +02005565 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005566 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005567 keep = ccline.cmdbuff[ccline.cmdlen];
5568 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005569 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005570
Bram Moolenaarffa96842018-06-12 22:05:14 +02005571 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5572
5573 args[0].v_type = VAR_STRING;
5574 args[0].vval.v_string = pat;
5575 args[1].v_type = VAR_STRING;
5576 args[1].vval.v_string = xp->xp_line;
5577 args[2].v_type = VAR_NUMBER;
5578 args[2].vval.v_number = xp->xp_col;
5579 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005581 /* Save the cmdline, we don't know what the function may do. */
5582 save_ccline = ccline;
5583 ccline.cmdbuff = NULL;
5584 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005586
Bram Moolenaarded27a12018-08-01 19:06:03 +02005587 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005588
5589 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005591 if (ccline.cmdbuff != NULL)
5592 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005593
Bram Moolenaarffa96842018-06-12 22:05:14 +02005594 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005595 return ret;
5596}
5597
5598/*
5599 * Expand names with a function defined by the user.
5600 */
5601 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005602ExpandUserDefined(
5603 expand_T *xp,
5604 regmatch_T *regmatch,
5605 int *num_file,
5606 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005607{
5608 char_u *retstr;
5609 char_u *s;
5610 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005611 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005612 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005613 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005614
5615 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5616 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 return FAIL;
5618
5619 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005620 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 {
5622 e = vim_strchr(s, '\n');
5623 if (e == NULL)
5624 e = s + STRLEN(s);
5625 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005626 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005628 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5629 *e = keep;
5630
5631 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005633 if (ga_grow(&ga, 1) == FAIL)
5634 break;
5635 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5636 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
5638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (*e != NUL)
5640 ++e;
5641 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005642 vim_free(retstr);
5643 *file = ga.ga_data;
5644 *num_file = ga.ga_len;
5645 return OK;
5646}
5647
5648/*
5649 * Expand names with a list returned by a function defined by the user.
5650 */
5651 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005652ExpandUserList(
5653 expand_T *xp,
5654 int *num_file,
5655 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005656{
5657 list_T *retlist;
5658 listitem_T *li;
5659 garray_T ga;
5660
5661 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5662 if (retlist == NULL)
5663 return FAIL;
5664
5665 ga_init2(&ga, (int)sizeof(char *), 3);
5666 /* Loop over the items in the list. */
5667 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5668 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005669 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5670 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005671
5672 if (ga_grow(&ga, 1) == FAIL)
5673 break;
5674
5675 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005676 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005677 ++ga.ga_len;
5678 }
5679 list_unref(retlist);
5680
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 *file = ga.ga_data;
5682 *num_file = ga.ga_len;
5683 return OK;
5684}
5685#endif
5686
5687/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005688 * Expand color scheme, compiler or filetype names.
5689 * Search from 'runtimepath':
5690 * 'runtimepath'/{dirnames}/{pat}.vim
5691 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5692 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5693 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5694 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005695 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 */
5697 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005698ExpandRTDir(
5699 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005700 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005701 int *num_file,
5702 char_u ***file,
5703 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 char_u *s;
5706 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005707 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005709 int i;
5710 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711
5712 *num_file = 0;
5713 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005714 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005715 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005717 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005719 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5720 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005722 ga_clear_strings(&ga);
5723 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005725 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005726 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005727 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005729
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005730 if (flags & DIP_START) {
5731 for (i = 0; dirnames[i] != NULL; ++i)
5732 {
5733 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5734 if (s == NULL)
5735 {
5736 ga_clear_strings(&ga);
5737 return FAIL;
5738 }
5739 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5740 globpath(p_pp, s, &ga, 0);
5741 vim_free(s);
5742 }
5743 }
5744
5745 if (flags & DIP_OPT) {
5746 for (i = 0; dirnames[i] != NULL; ++i)
5747 {
5748 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5749 if (s == NULL)
5750 {
5751 ga_clear_strings(&ga);
5752 return FAIL;
5753 }
5754 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5755 globpath(p_pp, s, &ga, 0);
5756 vim_free(s);
5757 }
5758 }
5759
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005760 for (i = 0; i < ga.ga_len; ++i)
5761 {
5762 match = ((char_u **)ga.ga_data)[i];
5763 s = match;
5764 e = s + STRLEN(s);
5765 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5766 {
5767 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005768 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005769 if (s < match || vim_ispathsep(*s))
5770 break;
5771 ++s;
5772 *e = NUL;
5773 mch_memmove(match, s, e - s + 1);
5774 }
5775 }
5776
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005777 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005778 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005779
5780 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005781 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005782 remove_duplicates(&ga);
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 *file = ga.ga_data;
5785 *num_file = ga.ga_len;
5786 return OK;
5787}
5788
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005789/*
5790 * Expand loadplugin names:
5791 * 'packpath'/pack/ * /opt/{pat}
5792 */
5793 static int
5794ExpandPackAddDir(
5795 char_u *pat,
5796 int *num_file,
5797 char_u ***file)
5798{
5799 char_u *s;
5800 char_u *e;
5801 char_u *match;
5802 garray_T ga;
5803 int i;
5804 int pat_len;
5805
5806 *num_file = 0;
5807 *file = NULL;
5808 pat_len = (int)STRLEN(pat);
5809 ga_init2(&ga, (int)sizeof(char *), 10);
5810
5811 s = alloc((unsigned)(pat_len + 26));
5812 if (s == NULL)
5813 {
5814 ga_clear_strings(&ga);
5815 return FAIL;
5816 }
5817 sprintf((char *)s, "pack/*/opt/%s*", pat);
5818 globpath(p_pp, s, &ga, 0);
5819 vim_free(s);
5820
5821 for (i = 0; i < ga.ga_len; ++i)
5822 {
5823 match = ((char_u **)ga.ga_data)[i];
5824 s = gettail(match);
5825 e = s + STRLEN(s);
5826 mch_memmove(match, s, e - s + 1);
5827 }
5828
5829 if (ga.ga_len == 0)
5830 return FAIL;
5831
5832 /* Sort and remove duplicates which can happen when specifying multiple
5833 * directories in dirnames. */
5834 remove_duplicates(&ga);
5835
5836 *file = ga.ga_data;
5837 *num_file = ga.ga_len;
5838 return OK;
5839}
5840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#endif
5842
5843#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5844/*
5845 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005846 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005848 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005849globpath(
5850 char_u *path,
5851 char_u *file,
5852 garray_T *ga,
5853 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 expand_T xpc;
5856 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 int num_p;
5859 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
5861 buf = alloc(MAXPATHL);
5862 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005863 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005865 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 /* Loop over all entries in {path}. */
5869 while (*path != NUL)
5870 {
5871 /* Copy one item of the path to buf[] and concatenate the file name. */
5872 copy_option_part(&path, buf, MAXPATHL, ",");
5873 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5874 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005875# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005876 /* Using the platform's path separator (\) makes vim incorrectly
5877 * treat it as an escape character, use '/' instead. */
5878 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5879 STRCAT(buf, "/");
5880# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005881 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005882# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005884 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5885 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005887 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005889 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 for (i = 0; i < num_p; ++i)
5892 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005893 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005894 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005895 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005898
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 FreeWild(num_p, p);
5900 }
5901 }
5902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903
5904 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905}
5906
5907#endif
5908
5909#if defined(FEAT_CMDHIST) || defined(PROTO)
5910
5911/*********************************
5912 * Command line history stuff *
5913 *********************************/
5914
5915/*
5916 * Translate a history character to the associated type number.
5917 */
5918 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005919hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 if (c == ':')
5922 return HIST_CMD;
5923 if (c == '=')
5924 return HIST_EXPR;
5925 if (c == '@')
5926 return HIST_INPUT;
5927 if (c == '>')
5928 return HIST_DEBUG;
5929 return HIST_SEARCH; /* must be '?' or '/' */
5930}
5931
5932/*
5933 * Table of history names.
5934 * These names are used in :history and various hist...() functions.
5935 * It is sufficient to give the significant prefix of a history name.
5936 */
5937
5938static char *(history_names[]) =
5939{
5940 "cmd",
5941 "search",
5942 "expr",
5943 "input",
5944 "debug",
5945 NULL
5946};
5947
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005948#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5949/*
5950 * Function given to ExpandGeneric() to obtain the possible first
5951 * arguments of the ":history command.
5952 */
5953 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005954get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005955{
5956 static char_u compl[2] = { NUL, NUL };
5957 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005958 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005959 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5960
5961 if (idx < short_names_count)
5962 {
5963 compl[0] = (char_u)short_names[idx];
5964 return compl;
5965 }
5966 if (idx < short_names_count + history_name_count)
5967 return (char_u *)history_names[idx - short_names_count];
5968 if (idx == short_names_count + history_name_count)
5969 return (char_u *)"all";
5970 return NULL;
5971}
5972#endif
5973
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974/*
5975 * init_history() - Initialize the command line history.
5976 * Also used to re-allocate the history when the size changes.
5977 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005978 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005979init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980{
5981 int newlen; /* new length of history table */
5982 histentry_T *temp;
5983 int i;
5984 int j;
5985 int type;
5986
5987 /*
5988 * If size of history table changed, reallocate it
5989 */
5990 newlen = (int)p_hi;
5991 if (newlen != hislen) /* history length changed */
5992 {
5993 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5994 {
5995 if (newlen)
5996 {
5997 temp = (histentry_T *)lalloc(
5998 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5999 if (temp == NULL) /* out of memory! */
6000 {
6001 if (type == 0) /* first one: just keep the old length */
6002 {
6003 newlen = hislen;
6004 break;
6005 }
6006 /* Already changed one table, now we can only have zero
6007 * length for all tables. */
6008 newlen = 0;
6009 type = -1;
6010 continue;
6011 }
6012 }
6013 else
6014 temp = NULL;
6015 if (newlen == 0 || temp != NULL)
6016 {
6017 if (hisidx[type] < 0) /* there are no entries yet */
6018 {
6019 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006020 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 }
6022 else if (newlen > hislen) /* array becomes bigger */
6023 {
6024 for (i = 0; i <= hisidx[type]; ++i)
6025 temp[i] = history[type][i];
6026 j = i;
6027 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006028 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 for ( ; j < hislen; ++i, ++j)
6030 temp[i] = history[type][j];
6031 }
6032 else /* array becomes smaller or 0 */
6033 {
6034 j = hisidx[type];
6035 for (i = newlen - 1; ; --i)
6036 {
6037 if (i >= 0) /* copy newest entries */
6038 temp[i] = history[type][j];
6039 else /* remove older entries */
6040 vim_free(history[type][j].hisstr);
6041 if (--j < 0)
6042 j = hislen - 1;
6043 if (j == hisidx[type])
6044 break;
6045 }
6046 hisidx[type] = newlen - 1;
6047 }
6048 vim_free(history[type]);
6049 history[type] = temp;
6050 }
6051 }
6052 hislen = newlen;
6053 }
6054}
6055
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006056 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006057clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006058{
6059 hisptr->hisnum = 0;
6060 hisptr->viminfo = FALSE;
6061 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006062 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006063}
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065/*
6066 * Check if command line 'str' is already in history.
6067 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6068 */
6069 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006070in_history(
6071 int type,
6072 char_u *str,
6073 int move_to_front, /* Move the entry to the front if it exists */
6074 int sep,
6075 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076{
6077 int i;
6078 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006079 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080
6081 if (hisidx[type] < 0)
6082 return FALSE;
6083 i = hisidx[type];
6084 do
6085 {
6086 if (history[type][i].hisstr == NULL)
6087 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006088
6089 /* For search history, check that the separator character matches as
6090 * well. */
6091 p = history[type][i].hisstr;
6092 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006093 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006094 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 {
6096 if (!move_to_front)
6097 return TRUE;
6098 last_i = i;
6099 break;
6100 }
6101 if (--i < 0)
6102 i = hislen - 1;
6103 } while (i != hisidx[type]);
6104
6105 if (last_i >= 0)
6106 {
6107 str = history[type][i].hisstr;
6108 while (i != hisidx[type])
6109 {
6110 if (++i >= hislen)
6111 i = 0;
6112 history[type][last_i] = history[type][i];
6113 last_i = i;
6114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006116 history[type][i].viminfo = FALSE;
6117 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006118 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 return TRUE;
6120 }
6121 return FALSE;
6122}
6123
6124/*
6125 * Convert history name (from table above) to its HIST_ equivalent.
6126 * When "name" is empty, return "cmd" history.
6127 * Returns -1 for unknown history name.
6128 */
6129 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006130get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131{
6132 int i;
6133 int len = (int)STRLEN(name);
6134
6135 /* No argument: use current history. */
6136 if (len == 0)
6137 return hist_char2type(ccline.cmdfirstc);
6138
6139 for (i = 0; history_names[i] != NULL; ++i)
6140 if (STRNICMP(name, history_names[i], len) == 0)
6141 return i;
6142
6143 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6144 return hist_char2type(name[0]);
6145
6146 return -1;
6147}
6148
6149static int last_maptick = -1; /* last seen maptick */
6150
6151/*
6152 * Add the given string to the given history. If the string is already in the
6153 * history then it is moved to the front. "histype" may be one of he HIST_
6154 * values.
6155 */
6156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006157add_to_history(
6158 int histype,
6159 char_u *new_entry,
6160 int in_map, /* consider maptick when inside a mapping */
6161 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162{
6163 histentry_T *hisptr;
6164 int len;
6165
6166 if (hislen == 0) /* no history */
6167 return;
6168
Bram Moolenaara939e432013-11-09 05:30:26 +01006169 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6170 return;
6171
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 /*
6173 * Searches inside the same mapping overwrite each other, so that only
6174 * the last line is kept. Be careful not to remove a line that was moved
6175 * down, only lines that were added.
6176 */
6177 if (histype == HIST_SEARCH && in_map)
6178 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006179 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 {
6181 /* Current line is from the same mapping, remove it */
6182 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6183 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006184 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006185 --hisnum[histype];
6186 if (--hisidx[HIST_SEARCH] < 0)
6187 hisidx[HIST_SEARCH] = hislen - 1;
6188 }
6189 last_maptick = -1;
6190 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006191 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 {
6193 if (++hisidx[histype] == hislen)
6194 hisidx[histype] = 0;
6195 hisptr = &history[histype][hisidx[histype]];
6196 vim_free(hisptr->hisstr);
6197
6198 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006199 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6201 if (hisptr->hisstr != NULL)
6202 hisptr->hisstr[len + 1] = sep;
6203
6204 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006205 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006206 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006207 if (histype == HIST_SEARCH && in_map)
6208 last_maptick = maptick;
6209 }
6210}
6211
6212#if defined(FEAT_EVAL) || defined(PROTO)
6213
6214/*
6215 * Get identifier of newest history entry.
6216 * "histype" may be one of the HIST_ values.
6217 */
6218 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006219get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220{
6221 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6222 || hisidx[histype] < 0)
6223 return -1;
6224
6225 return history[histype][hisidx[histype]].hisnum;
6226}
6227
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 * Calculate history index from a number:
6230 * num > 0: seen as identifying number of a history entry
6231 * num < 0: relative position in history wrt newest entry
6232 * "histype" may be one of the HIST_ values.
6233 */
6234 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006235calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236{
6237 int i;
6238 histentry_T *hist;
6239 int wrapped = FALSE;
6240
6241 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6242 || (i = hisidx[histype]) < 0 || num == 0)
6243 return -1;
6244
6245 hist = history[histype];
6246 if (num > 0)
6247 {
6248 while (hist[i].hisnum > num)
6249 if (--i < 0)
6250 {
6251 if (wrapped)
6252 break;
6253 i += hislen;
6254 wrapped = TRUE;
6255 }
6256 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6257 return i;
6258 }
6259 else if (-num <= hislen)
6260 {
6261 i += num + 1;
6262 if (i < 0)
6263 i += hislen;
6264 if (hist[i].hisstr != NULL)
6265 return i;
6266 }
6267 return -1;
6268}
6269
6270/*
6271 * Get a history entry by its index.
6272 * "histype" may be one of the HIST_ values.
6273 */
6274 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 idx = calc_hist_idx(histype, idx);
6278 if (idx >= 0)
6279 return history[histype][idx].hisstr;
6280 else
6281 return (char_u *)"";
6282}
6283
6284/*
6285 * Clear all entries of a history.
6286 * "histype" may be one of the HIST_ values.
6287 */
6288 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006289clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006290{
6291 int i;
6292 histentry_T *hisptr;
6293
6294 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6295 {
6296 hisptr = history[histype];
6297 for (i = hislen; i--;)
6298 {
6299 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006300 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006301 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302 }
6303 hisidx[histype] = -1; /* mark history as cleared */
6304 hisnum[histype] = 0; /* reset identifier counter */
6305 return OK;
6306 }
6307 return FAIL;
6308}
6309
6310/*
6311 * Remove all entries matching {str} from a history.
6312 * "histype" may be one of the HIST_ values.
6313 */
6314 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006315del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006316{
6317 regmatch_T regmatch;
6318 histentry_T *hisptr;
6319 int idx;
6320 int i;
6321 int last;
6322 int found = FALSE;
6323
6324 regmatch.regprog = NULL;
6325 regmatch.rm_ic = FALSE; /* always match case */
6326 if (hislen != 0
6327 && histype >= 0
6328 && histype < HIST_COUNT
6329 && *str != NUL
6330 && (idx = hisidx[histype]) >= 0
6331 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6332 != NULL)
6333 {
6334 i = last = idx;
6335 do
6336 {
6337 hisptr = &history[histype][i];
6338 if (hisptr->hisstr == NULL)
6339 break;
6340 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6341 {
6342 found = TRUE;
6343 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006344 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345 }
6346 else
6347 {
6348 if (i != last)
6349 {
6350 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006351 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 }
6353 if (--last < 0)
6354 last += hislen;
6355 }
6356 if (--i < 0)
6357 i += hislen;
6358 } while (i != idx);
6359 if (history[histype][idx].hisstr == NULL)
6360 hisidx[histype] = -1;
6361 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006362 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 return found;
6364}
6365
6366/*
6367 * Remove an indexed entry from a history.
6368 * "histype" may be one of the HIST_ values.
6369 */
6370 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006371del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 int i, j;
6374
6375 i = calc_hist_idx(histype, idx);
6376 if (i < 0)
6377 return FALSE;
6378 idx = hisidx[histype];
6379 vim_free(history[histype][i].hisstr);
6380
6381 /* When deleting the last added search string in a mapping, reset
6382 * last_maptick, so that the last added search string isn't deleted again.
6383 */
6384 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6385 last_maptick = -1;
6386
6387 while (i != idx)
6388 {
6389 j = (i + 1) % hislen;
6390 history[histype][i] = history[histype][j];
6391 i = j;
6392 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006393 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 if (--i < 0)
6395 i += hislen;
6396 hisidx[histype] = i;
6397 return TRUE;
6398}
6399
6400#endif /* FEAT_EVAL */
6401
6402#if defined(FEAT_CRYPT) || defined(PROTO)
6403/*
6404 * Very specific function to remove the value in ":set key=val" from the
6405 * history.
6406 */
6407 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006408remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006409{
6410 char_u *p;
6411 int i;
6412
6413 i = hisidx[HIST_CMD];
6414 if (i < 0)
6415 return;
6416 p = history[HIST_CMD][i].hisstr;
6417 if (p != NULL)
6418 for ( ; *p; ++p)
6419 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6420 {
6421 p = vim_strchr(p + 3, '=');
6422 if (p == NULL)
6423 break;
6424 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006425 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 if (p[i] == '\\' && p[i + 1])
6427 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006428 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 --p;
6430 }
6431}
6432#endif
6433
6434#endif /* FEAT_CMDHIST */
6435
Bram Moolenaar064154c2016-03-19 22:50:43 +01006436#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006437/*
6438 * Get pointer to the command line info to use. cmdline_paste() may clear
6439 * ccline and put the previous value in prev_ccline.
6440 */
6441 static struct cmdline_info *
6442get_ccline_ptr(void)
6443{
6444 if ((State & CMDLINE) == 0)
6445 return NULL;
6446 if (ccline.cmdbuff != NULL)
6447 return &ccline;
6448 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6449 return &prev_ccline;
6450 return NULL;
6451}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006452#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006453
Bram Moolenaar064154c2016-03-19 22:50:43 +01006454#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006455/*
6456 * Get the current command line in allocated memory.
6457 * Only works when the command line is being edited.
6458 * Returns NULL when something is wrong.
6459 */
6460 char_u *
6461get_cmdline_str(void)
6462{
6463 struct cmdline_info *p = get_ccline_ptr();
6464
6465 if (p == NULL)
6466 return NULL;
6467 return vim_strnsave(p->cmdbuff, p->cmdlen);
6468}
6469
6470/*
6471 * Get the current command line position, counted in bytes.
6472 * Zero is the first position.
6473 * Only works when the command line is being edited.
6474 * Returns -1 when something is wrong.
6475 */
6476 int
6477get_cmdline_pos(void)
6478{
6479 struct cmdline_info *p = get_ccline_ptr();
6480
6481 if (p == NULL)
6482 return -1;
6483 return p->cmdpos;
6484}
6485
6486/*
6487 * Set the command line byte position to "pos". Zero is the first position.
6488 * Only works when the command line is being edited.
6489 * Returns 1 when failed, 0 when OK.
6490 */
6491 int
6492set_cmdline_pos(
6493 int pos)
6494{
6495 struct cmdline_info *p = get_ccline_ptr();
6496
6497 if (p == NULL)
6498 return 1;
6499
6500 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6501 * changed the command line. */
6502 if (pos < 0)
6503 new_cmdpos = 0;
6504 else
6505 new_cmdpos = pos;
6506 return 0;
6507}
6508#endif
6509
6510#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6511/*
6512 * Get the current command-line type.
6513 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6514 * Only works when the command line is being edited.
6515 * Returns NUL when something is wrong.
6516 */
6517 int
6518get_cmdline_type(void)
6519{
6520 struct cmdline_info *p = get_ccline_ptr();
6521
6522 if (p == NULL)
6523 return NUL;
6524 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006525 return
6526# ifdef FEAT_EVAL
6527 (p->input_fn) ? '@' :
6528# endif
6529 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006530 return p->cmdfirstc;
6531}
6532#endif
6533
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6535/*
6536 * Get indices "num1,num2" that specify a range within a list (not a range of
6537 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6538 * Returns OK if parsed successfully, otherwise FAIL.
6539 */
6540 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006541get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542{
6543 int len;
6544 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006545 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547 *str = skipwhite(*str);
6548 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6549 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006550 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 *str += len;
6552 *num1 = (int)num;
6553 first = TRUE;
6554 }
6555 *str = skipwhite(*str);
6556 if (**str == ',') /* parse "to" part of range */
6557 {
6558 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006559 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 if (len > 0)
6561 {
6562 *num2 = (int)num;
6563 *str = skipwhite(*str + len);
6564 }
6565 else if (!first) /* no number given at all */
6566 return FAIL;
6567 }
6568 else if (first) /* only one number given */
6569 *num2 = *num1;
6570 return OK;
6571}
6572#endif
6573
6574#if defined(FEAT_CMDHIST) || defined(PROTO)
6575/*
6576 * :history command - print a history
6577 */
6578 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006579ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 histentry_T *hist;
6582 int histype1 = HIST_CMD;
6583 int histype2 = HIST_CMD;
6584 int hisidx1 = 1;
6585 int hisidx2 = -1;
6586 int idx;
6587 int i, j, k;
6588 char_u *end;
6589 char_u *arg = eap->arg;
6590
6591 if (hislen == 0)
6592 {
6593 MSG(_("'history' option is zero"));
6594 return;
6595 }
6596
6597 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6598 {
6599 end = arg;
6600 while (ASCII_ISALPHA(*end)
6601 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6602 end++;
6603 i = *end;
6604 *end = NUL;
6605 histype1 = get_histtype(arg);
6606 if (histype1 == -1)
6607 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006608 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
6610 histype1 = 0;
6611 histype2 = HIST_COUNT-1;
6612 }
6613 else
6614 {
6615 *end = i;
6616 EMSG(_(e_trailing));
6617 return;
6618 }
6619 }
6620 else
6621 histype2 = histype1;
6622 *end = i;
6623 }
6624 else
6625 end = arg;
6626 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6627 {
6628 EMSG(_(e_trailing));
6629 return;
6630 }
6631
6632 for (; !got_int && histype1 <= histype2; ++histype1)
6633 {
6634 STRCPY(IObuff, "\n # ");
6635 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6636 MSG_PUTS_TITLE(IObuff);
6637 idx = hisidx[histype1];
6638 hist = history[histype1];
6639 j = hisidx1;
6640 k = hisidx2;
6641 if (j < 0)
6642 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6643 if (k < 0)
6644 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6645 if (idx >= 0 && j <= k)
6646 for (i = idx + 1; !got_int; ++i)
6647 {
6648 if (i == hislen)
6649 i = 0;
6650 if (hist[i].hisstr != NULL
6651 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6652 {
6653 msg_putchar('\n');
6654 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6655 hist[i].hisnum);
6656 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6657 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006658 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006659 else
6660 STRCAT(IObuff, hist[i].hisstr);
6661 msg_outtrans(IObuff);
6662 out_flush();
6663 }
6664 if (i == idx)
6665 break;
6666 }
6667 }
6668}
6669#endif
6670
6671#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006672/*
6673 * Buffers for history read from a viminfo file. Only valid while reading.
6674 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006675static histentry_T *viminfo_history[HIST_COUNT] =
6676 {NULL, NULL, NULL, NULL, NULL};
6677static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6678static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679static int viminfo_add_at_front = FALSE;
6680
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006681static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682
6683/*
6684 * Translate a history type number to the associated character.
6685 */
6686 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006687hist_type2char(
6688 int type,
6689 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690{
6691 if (type == HIST_CMD)
6692 return ':';
6693 if (type == HIST_SEARCH)
6694 {
6695 if (use_question)
6696 return '?';
6697 else
6698 return '/';
6699 }
6700 if (type == HIST_EXPR)
6701 return '=';
6702 return '@';
6703}
6704
6705/*
6706 * Prepare for reading the history from the viminfo file.
6707 * This allocates history arrays to store the read history lines.
6708 */
6709 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006710prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711{
6712 int i;
6713 int num;
6714 int type;
6715 int len;
6716
6717 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006718 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 if (asklen > hislen)
6720 asklen = hislen;
6721
6722 for (type = 0; type < HIST_COUNT; ++type)
6723 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006724 /* Count the number of empty spaces in the history list. Entries read
6725 * from viminfo previously are also considered empty. If there are
6726 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006728 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 num++;
6730 len = asklen;
6731 if (num > len)
6732 len = num;
6733 if (len <= 0)
6734 viminfo_history[type] = NULL;
6735 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006736 viminfo_history[type] = (histentry_T *)lalloc(
6737 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 if (viminfo_history[type] == NULL)
6739 len = 0;
6740 viminfo_hislen[type] = len;
6741 viminfo_hisidx[type] = 0;
6742 }
6743}
6744
6745/*
6746 * Accept a line from the viminfo, store it in the history array when it's
6747 * new.
6748 */
6749 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006750read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751{
6752 int type;
6753 long_u len;
6754 char_u *val;
6755 char_u *p;
6756
6757 type = hist_char2type(virp->vir_line[0]);
6758 if (viminfo_hisidx[type] < viminfo_hislen[type])
6759 {
6760 val = viminfo_readstring(virp, 1, TRUE);
6761 if (val != NULL && *val != NUL)
6762 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006763 int sep = (*val == ' ' ? NUL : *val);
6764
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006766 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 {
6768 /* Need to re-allocate to append the separator byte. */
6769 len = STRLEN(val);
6770 p = lalloc(len + 2, TRUE);
6771 if (p != NULL)
6772 {
6773 if (type == HIST_SEARCH)
6774 {
6775 /* Search entry: Move the separator from the first
6776 * column to after the NUL. */
6777 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006778 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 }
6780 else
6781 {
6782 /* Not a search entry: No separator in the viminfo
6783 * file, add a NUL separator. */
6784 mch_memmove(p, val, (size_t)len + 1);
6785 p[len + 1] = NUL;
6786 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006787 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6788 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006789 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6790 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006791 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 }
6793 }
6794 }
6795 vim_free(val);
6796 }
6797 return viminfo_readline(virp);
6798}
6799
Bram Moolenaar07219f92013-04-14 23:19:36 +02006800/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006801 * Accept a new style history line from the viminfo, store it in the history
6802 * array when it's new.
6803 */
6804 void
6805handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006806 garray_T *values,
6807 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808{
6809 int type;
6810 long_u len;
6811 char_u *val;
6812 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006813 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006814
6815 /* Check the format:
6816 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006817 if (values->ga_len < 4
6818 || vp[0].bv_type != BVAL_NR
6819 || vp[1].bv_type != BVAL_NR
6820 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6821 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006822 return;
6823
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006824 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006825 if (type >= HIST_COUNT)
6826 return;
6827 if (viminfo_hisidx[type] < viminfo_hislen[type])
6828 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006829 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006830 if (val != NULL && *val != NUL)
6831 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006832 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6833 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006834 int idx;
6835 int overwrite = FALSE;
6836
6837 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6838 {
6839 /* If lines were written by an older Vim we need to avoid
6840 * getting duplicates. See if the entry already exists. */
6841 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6842 {
6843 p = viminfo_history[type][idx].hisstr;
6844 if (STRCMP(val, p) == 0
6845 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6846 {
6847 overwrite = TRUE;
6848 break;
6849 }
6850 }
6851
6852 if (!overwrite)
6853 {
6854 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006855 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006856 p = lalloc(len + 2, TRUE);
6857 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006858 else
6859 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006860 if (p != NULL)
6861 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006862 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006863 if (!overwrite)
6864 {
6865 mch_memmove(p, val, (size_t)len + 1);
6866 /* Put the separator after the NUL. */
6867 p[len + 1] = sep;
6868 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006869 viminfo_history[type][idx].hisnum = 0;
6870 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006871 viminfo_hisidx[type]++;
6872 }
6873 }
6874 }
6875 }
6876 }
6877}
6878
6879/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006880 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006881 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006882 static void
6883concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884{
6885 int idx;
6886 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006887
6888 idx = hisidx[type] + viminfo_hisidx[type];
6889 if (idx >= hislen)
6890 idx -= hislen;
6891 else if (idx < 0)
6892 idx = hislen - 1;
6893 if (viminfo_add_at_front)
6894 hisidx[type] = idx;
6895 else
6896 {
6897 if (hisidx[type] == -1)
6898 hisidx[type] = hislen - 1;
6899 do
6900 {
6901 if (history[type][idx].hisstr != NULL
6902 || history[type][idx].viminfo)
6903 break;
6904 if (++idx == hislen)
6905 idx = 0;
6906 } while (idx != hisidx[type]);
6907 if (idx != hisidx[type] && --idx < 0)
6908 idx = hislen - 1;
6909 }
6910 for (i = 0; i < viminfo_hisidx[type]; i++)
6911 {
6912 vim_free(history[type][idx].hisstr);
6913 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6914 history[type][idx].viminfo = TRUE;
6915 history[type][idx].time_set = viminfo_history[type][i].time_set;
6916 if (--idx < 0)
6917 idx = hislen - 1;
6918 }
6919 idx += 1;
6920 idx %= hislen;
6921 for (i = 0; i < viminfo_hisidx[type]; i++)
6922 {
6923 history[type][idx++].hisnum = ++hisnum[type];
6924 idx %= hislen;
6925 }
6926}
6927
6928#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6929 static int
6930#ifdef __BORLANDC__
6931_RTLENTRYF
6932#endif
6933sort_hist(const void *s1, const void *s2)
6934{
6935 histentry_T *p1 = *(histentry_T **)s1;
6936 histentry_T *p2 = *(histentry_T **)s2;
6937
6938 if (p1->time_set < p2->time_set) return -1;
6939 if (p1->time_set > p2->time_set) return 1;
6940 return 0;
6941}
6942#endif
6943
6944/*
6945 * Merge history lines from viminfo and lines typed in this Vim based on the
6946 * timestamp;
6947 */
6948 static void
6949merge_history(int type)
6950{
6951 int max_len;
6952 histentry_T **tot_hist;
6953 histentry_T *new_hist;
6954 int i;
6955 int len;
6956
6957 /* Make one long list with all entries. */
6958 max_len = hislen + viminfo_hisidx[type];
6959 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6960 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6961 if (tot_hist == NULL || new_hist == NULL)
6962 {
6963 vim_free(tot_hist);
6964 vim_free(new_hist);
6965 return;
6966 }
6967 for (i = 0; i < viminfo_hisidx[type]; i++)
6968 tot_hist[i] = &viminfo_history[type][i];
6969 len = i;
6970 for (i = 0; i < hislen; i++)
6971 if (history[type][i].hisstr != NULL)
6972 tot_hist[len++] = &history[type][i];
6973
6974 /* Sort the list on timestamp. */
6975 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6976
6977 /* Keep the newest ones. */
6978 for (i = 0; i < hislen; i++)
6979 {
6980 if (i < len)
6981 {
6982 new_hist[i] = *tot_hist[i];
6983 tot_hist[i]->hisstr = NULL;
6984 if (new_hist[i].hisnum == 0)
6985 new_hist[i].hisnum = ++hisnum[type];
6986 }
6987 else
6988 clear_hist_entry(&new_hist[i]);
6989 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006990 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006991
6992 /* Free what is not kept. */
6993 for (i = 0; i < viminfo_hisidx[type]; i++)
6994 vim_free(viminfo_history[type][i].hisstr);
6995 for (i = 0; i < hislen; i++)
6996 vim_free(history[type][i].hisstr);
6997 vim_free(history[type]);
6998 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006999 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007000}
7001
7002/*
7003 * Finish reading history lines from viminfo. Not used when writing viminfo.
7004 */
7005 void
7006finish_viminfo_history(vir_T *virp)
7007{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007009 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010
7011 for (type = 0; type < HIST_COUNT; ++type)
7012 {
7013 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007014 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007015
7016 if (merge)
7017 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007019 concat_history(type);
7020
Bram Moolenaard23a8232018-02-10 18:45:26 +01007021 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007022 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 }
7024}
7025
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007026/*
7027 * Write history to viminfo file in "fp".
7028 * When "merge" is TRUE merge history lines with a previously read viminfo
7029 * file, data is in viminfo_history[].
7030 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007033write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034{
7035 int i;
7036 int type;
7037 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007038 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039
7040 init_history();
7041 if (hislen == 0)
7042 return;
7043 for (type = 0; type < HIST_COUNT; ++type)
7044 {
7045 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7046 if (num_saved == 0)
7047 continue;
7048 if (num_saved < 0) /* Use default */
7049 num_saved = hislen;
7050 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7051 type == HIST_CMD ? _("Command Line") :
7052 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007053 type == HIST_EXPR ? _("Expression") :
7054 type == HIST_INPUT ? _("Input Line") :
7055 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 if (num_saved > hislen)
7057 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007058
7059 /*
7060 * Merge typed and viminfo history:
7061 * round 1: history of typed commands.
7062 * round 2: history from recently read viminfo.
7063 */
7064 for (round = 1; round <= 2; ++round)
7065 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007066 if (round == 1)
7067 /* start at newest entry, somewhere in the list */
7068 i = hisidx[type];
7069 else if (viminfo_hisidx[type] > 0)
7070 /* start at newest entry, first in the list */
7071 i = 0;
7072 else
7073 /* empty list */
7074 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007075 if (i >= 0)
7076 while (num_saved > 0
7077 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007079 char_u *p;
7080 time_t timestamp;
7081 int c = NUL;
7082
7083 if (round == 1)
7084 {
7085 p = history[type][i].hisstr;
7086 timestamp = history[type][i].time_set;
7087 }
7088 else
7089 {
7090 p = viminfo_history[type] == NULL ? NULL
7091 : viminfo_history[type][i].hisstr;
7092 timestamp = viminfo_history[type] == NULL ? 0
7093 : viminfo_history[type][i].time_set;
7094 }
7095
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007096 if (p != NULL && (round == 2
7097 || !merge
7098 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007100 --num_saved;
7101 fputc(hist_type2char(type, TRUE), fp);
7102 /* For the search history: put the separator in the
7103 * second column; use a space if there isn't one. */
7104 if (type == HIST_SEARCH)
7105 {
7106 c = p[STRLEN(p) + 1];
7107 putc(c == NUL ? ' ' : c, fp);
7108 }
7109 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007110
7111 {
7112 char cbuf[NUMBUFLEN];
7113
7114 /* New style history with a bar line. Format:
7115 * |{bartype},{histtype},{timestamp},{separator},"text" */
7116 if (c == NUL)
7117 cbuf[0] = NUL;
7118 else
7119 sprintf(cbuf, "%d", c);
7120 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7121 type, (long)timestamp, cbuf);
7122 barline_writestring(fp, p, LSIZE - 20);
7123 putc('\n', fp);
7124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007125 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007126 if (round == 1)
7127 {
7128 /* Decrement index, loop around and stop when back at
7129 * the start. */
7130 if (--i < 0)
7131 i = hislen - 1;
7132 if (i == hisidx[type])
7133 break;
7134 }
7135 else
7136 {
7137 /* Increment index. Stop at the end in the while. */
7138 ++i;
7139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007141 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007142 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007143 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007144 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007145 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007146 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 }
7148}
7149#endif /* FEAT_VIMINFO */
7150
7151#if defined(FEAT_FKMAP) || defined(PROTO)
7152/*
7153 * Write a character at the current cursor+offset position.
7154 * It is directly written into the command buffer block.
7155 */
7156 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007157cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158{
7159 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7160 {
7161 EMSG(_("E198: cmd_pchar beyond the command length"));
7162 return;
7163 }
7164 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7165 ccline.cmdbuff[ccline.cmdlen] = NUL;
7166}
7167
7168 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007169cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170{
7171 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7172 {
7173 /* EMSG(_("cmd_gchar beyond the command length")); */
7174 return NUL;
7175 }
7176 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7177}
7178#endif
7179
7180#if defined(FEAT_CMDWIN) || defined(PROTO)
7181/*
7182 * Open a window on the current command line and history. Allow editing in
7183 * the window. Returns when the window is closed.
7184 * Returns:
7185 * CR if the command is to be executed
7186 * Ctrl_C if it is to be abandoned
7187 * K_IGNORE if editing continues
7188 */
7189 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007190open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191{
7192 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007193 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007194 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007195 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 win_T *wp;
7197 int i;
7198 linenr_T lnum;
7199 int histtype;
7200 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 int save_restart_edit = restart_edit;
7202 int save_State = State;
7203 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007204#ifdef FEAT_RIGHTLEFT
7205 int save_cmdmsg_rl = cmdmsg_rl;
7206#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007207#ifdef FEAT_FOLDING
7208 int save_KeyTyped;
7209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007210
7211 /* Can't do this recursively. Can't do it when typing a password. */
7212 if (cmdwin_type != 0
7213# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7214 || cmdline_star > 0
7215# endif
7216 )
7217 {
7218 beep_flush();
7219 return K_IGNORE;
7220 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007221 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222
7223 /* Save current window sizes. */
7224 win_size_save(&winsizes);
7225
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007227 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007228
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007229 /* don't use a new tab page */
7230 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007231 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007232
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 /* Create a window for the command-line buffer. */
7234 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7235 {
7236 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007237 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007238 return K_IGNORE;
7239 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007240 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
7242 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007243 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007244 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007245 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007247#ifdef FEAT_FOLDING
7248 curwin->w_p_fen = FALSE;
7249#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007251 curwin->w_p_rl = cmdmsg_rl;
7252 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007254 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007257 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007258 /* But don't allow switching to another buffer. */
7259 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260
Bram Moolenaar46152342005-09-07 21:18:43 +00007261 /* Showing the prompt may have set need_wait_return, reset it. */
7262 need_wait_return = FALSE;
7263
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007264 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7266 {
7267 if (p_wc == TAB)
7268 {
7269 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7270 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7271 }
7272 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7273 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007274 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007276 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7277 * sets 'textwidth' to 78). */
7278 curbuf->b_p_tw = 0;
7279
Bram Moolenaar071d4272004-06-13 20:20:40 +00007280 /* Fill the buffer with the history. */
7281 init_history();
7282 if (hislen > 0)
7283 {
7284 i = hisidx[histtype];
7285 if (i >= 0)
7286 {
7287 lnum = 0;
7288 do
7289 {
7290 if (++i == hislen)
7291 i = 0;
7292 if (history[histtype][i].hisstr != NULL)
7293 ml_append(lnum++, history[histtype][i].hisstr,
7294 (colnr_T)0, FALSE);
7295 }
7296 while (i != hisidx[histtype]);
7297 }
7298 }
7299
7300 /* Replace the empty last line with the current command-line and put the
7301 * cursor there. */
7302 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7303 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7304 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007305 changed_line_abv_curs();
7306 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007307 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308
7309 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007310 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311
7312 /* No Ex mode here! */
7313 exmode_active = 0;
7314
7315 State = NORMAL;
7316# ifdef FEAT_MOUSE
7317 setmouse();
7318# endif
7319
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007321 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007322 if (restart_edit != 0) /* autocmd with ":startinsert" */
7323 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324
7325 i = RedrawingDisabled;
7326 RedrawingDisabled = 0;
7327
7328 /*
7329 * Call the main loop until <CR> or CTRL-C is typed.
7330 */
7331 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007332 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334 RedrawingDisabled = i;
7335
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007336# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007337 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007338# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007339
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007341 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007342
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007343# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007344 /* Restore KeyTyped in case it is modified by autocommands */
7345 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# endif
7347
Bram Moolenaarccc18222007-05-10 18:25:20 +00007348 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007349 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 cmdwin_type = 0;
7351
7352 exmode_active = save_exmode;
7353
Bram Moolenaarf9821062008-06-20 16:31:07 +00007354 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007356 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
7358 cmdwin_result = Ctrl_C;
7359 EMSG(_("E199: Active window or buffer deleted"));
7360 }
7361 else
7362 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007363# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007364 /* autocmds may abort script processing */
7365 if (aborting() && cmdwin_result != K_IGNORE)
7366 cmdwin_result = Ctrl_C;
7367# endif
7368 /* Set the new command line from the cmdline buffer. */
7369 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007370 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007372 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7373
7374 if (histtype == HIST_CMD)
7375 {
7376 /* Execute the command directly. */
7377 ccline.cmdbuff = vim_strsave((char_u *)p);
7378 cmdwin_result = CAR;
7379 }
7380 else
7381 {
7382 /* First need to cancel what we were doing. */
7383 ccline.cmdbuff = NULL;
7384 stuffcharReadbuff(':');
7385 stuffReadbuff((char_u *)p);
7386 stuffcharReadbuff(CAR);
7387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007388 }
7389 else if (cmdwin_result == K_XF2) /* :qa typed */
7390 {
7391 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7392 cmdwin_result = CAR;
7393 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007394 else if (cmdwin_result == Ctrl_C)
7395 {
7396 /* :q or :close, don't execute any command
7397 * and don't modify the cmd window. */
7398 ccline.cmdbuff = NULL;
7399 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007400 else
7401 ccline.cmdbuff = vim_strsave(ml_get_curline());
7402 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007403 {
7404 ccline.cmdbuff = vim_strsave((char_u *)"");
7405 ccline.cmdlen = 0;
7406 ccline.cmdbufflen = 1;
7407 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007408 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 else
7411 {
7412 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7413 ccline.cmdbufflen = ccline.cmdlen + 1;
7414 ccline.cmdpos = curwin->w_cursor.col;
7415 if (ccline.cmdpos > ccline.cmdlen)
7416 ccline.cmdpos = ccline.cmdlen;
7417 if (cmdwin_result == K_IGNORE)
7418 {
7419 set_cmdspos_cursor();
7420 redrawcmd();
7421 }
7422 }
7423
Bram Moolenaar071d4272004-06-13 20:20:40 +00007424 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007425 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007426# ifdef FEAT_CONCEAL
7427 /* Avoid command-line window first character being concealed. */
7428 curwin->w_p_cole = 0;
7429# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007431 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007432 win_goto(old_curwin);
7433 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007434
7435 /* win_close() may have already wiped the buffer when 'bh' is
7436 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007437 if (bufref_valid(&bufref))
7438 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439
7440 /* Restore window sizes. */
7441 win_size_restore(&winsizes);
7442
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007443 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007444 }
7445
7446 ga_clear(&winsizes);
7447 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007448# ifdef FEAT_RIGHTLEFT
7449 cmdmsg_rl = save_cmdmsg_rl;
7450# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452 State = save_State;
7453# ifdef FEAT_MOUSE
7454 setmouse();
7455# endif
7456
7457 return cmdwin_result;
7458}
7459#endif /* FEAT_CMDWIN */
7460
7461/*
7462 * Used for commands that either take a simple command string argument, or:
7463 * cmd << endmarker
7464 * {script}
7465 * endmarker
7466 * Returns a pointer to allocated memory with {script} or NULL.
7467 */
7468 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007469script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
7471 char_u *theline;
7472 char *end_pattern = NULL;
7473 char dot[] = ".";
7474 garray_T ga;
7475
7476 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7477 return NULL;
7478
7479 ga_init2(&ga, 1, 0x400);
7480
7481 if (cmd[2] != NUL)
7482 end_pattern = (char *)skipwhite(cmd + 2);
7483 else
7484 end_pattern = dot;
7485
7486 for (;;)
7487 {
7488 theline = eap->getline(
7489#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007490 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491#endif
7492 NUL, eap->cookie, 0);
7493
7494 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007495 {
7496 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
7500 ga_concat(&ga, theline);
7501 ga_append(&ga, '\n');
7502 vim_free(theline);
7503 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007504 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007505
7506 return (char_u *)ga.ga_data;
7507}