blob: 7928af7bffd7a7227fe9f9be52d59c386c70bd98 [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 Moolenaarb0acacd2018-08-11 16:40:43 +0200279 *skiplen = 0;
280 *patlen = ccline.cmdlen;
281
282 if (p_is && !cmd_silent)
283 {
284 // by default search all lines
285 search_first_line = 0;
286 search_last_line = MAXLNUM;
287
288 if (firstc == '/' || firstc == '?')
289 return TRUE;
290 if (firstc == ':')
291 {
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200292 char_u *cmd;
293 cmdmod_T save_cmdmod = cmdmod;
294 char_u *p;
295 int delim;
296 char_u *end;
297 char_u *dummy;
298 exarg_T ea;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200299
Bram Moolenaar33c4dbb2018-08-14 16:06:16 +0200300 vim_memset(&ea, 0, sizeof(ea));
301 ea.line1 = 1;
302 ea.line2 = 1;
303 ea.cmd = ccline.cmdbuff;
304 ea.addr_type = ADDR_LINES;
305
306 parse_command_modifiers(&ea, &dummy, TRUE);
307 cmdmod = save_cmdmod;
308
309 cmd = skip_range(ea.cmd, NULL);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200310 if (*cmd == 's' || *cmd == 'g' || *cmd == 'v')
311 {
312 // Skip over "substitute" to find the pattern separator.
313 for (p = cmd; ASCII_ISALPHA(*p); ++p)
314 ;
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200315 if (*skipwhite(p) != NUL
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200316 && (STRNCMP(cmd, "substitute", p - cmd) == 0
Bram Moolenaar167ae422018-08-14 21:32:21 +0200317 || STRNCMP(cmd, "smagic", p - cmd) == 0
318 || STRNCMP(cmd, "snomagic", MAX(p - cmd, 3)) == 0
Bram Moolenaar81f56532018-08-18 16:19:42 +0200319 || STRNCMP(cmd, "sort", p - cmd) == 0
Bram Moolenaar21f990e2018-08-11 19:20:49 +0200320 || STRNCMP(cmd, "global", p - cmd) == 0
321 || STRNCMP(cmd, "vglobal", p - cmd) == 0))
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200322 {
Bram Moolenaar167ae422018-08-14 21:32:21 +0200323 if (*cmd == 's' && cmd[1] == 'm')
324 p_magic = TRUE;
325 else if (*cmd == 's' && cmd[1] == 'n')
326 p_magic = FALSE;
327
Bram Moolenaardef7b1d2018-08-13 22:54:35 +0200328 // Check for "global!/".
329 if (*cmd == 'g' && *p == '!')
330 {
331 p++;
332 if (*skipwhite(p) == NUL)
333 return FALSE;
334 }
Bram Moolenaar81f56532018-08-18 16:19:42 +0200335
336 // For ":sort" skip over flags.
337 if (cmd[0] == 's' && cmd[1] == 'o')
338 {
339 while (ASCII_ISALPHA(*(p = skipwhite(p))))
340 ++p;
341 if (*p == NUL)
342 return FALSE;
343 }
344
Bram Moolenaar2b926fc2018-08-13 11:07:57 +0200345 p = skipwhite(p);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200346 delim = *p++;
347 end = skip_regexp(p, delim, p_magic, NULL);
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200348 if (end > p || *end == delim)
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200349 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200350 pos_T save_cursor = curwin->w_cursor;
351
352 // found a non-empty pattern
353 *skiplen = (int)(p - ccline.cmdbuff);
354 *patlen = (int)(end - p);
355
356 // parse the address range
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200357 curwin->w_cursor = is_state->search_start;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200358 parse_cmd_address(&ea, &dummy);
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200359 if (ea.addr_count > 0)
360 {
Bram Moolenaar60d08712018-08-12 21:53:15 +0200361 // Allow for reverse match.
362 if (ea.line2 < ea.line1)
363 {
364 search_first_line = ea.line2;
365 search_last_line = ea.line1;
366 }
367 else
368 {
369 search_first_line = ea.line1;
370 search_last_line = ea.line2;
371 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200372 }
Bram Moolenaar81f56532018-08-18 16:19:42 +0200373 else if (cmd[0] == 's' && cmd[1] != 'o')
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200374 {
375 // :s defaults to the current line
376 search_first_line = curwin->w_cursor.lnum;
377 search_last_line = curwin->w_cursor.lnum;
378 }
379
380 curwin->w_cursor = save_cursor;
381 return TRUE;
382 }
383 }
384 }
385 }
386 }
387
388 return FALSE;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200389}
390
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200391 static void
392finish_incsearch_highlighting(
393 int gotesc,
394 incsearch_state_T *is_state,
395 int call_update_screen)
396{
397 if (is_state->did_incsearch)
398 {
399 is_state->did_incsearch = FALSE;
400 if (gotesc)
401 curwin->w_cursor = is_state->save_cursor;
402 else
403 {
404 if (!EQUAL_POS(is_state->save_cursor, is_state->search_start))
405 {
406 // put the '" mark at the original position
407 curwin->w_cursor = is_state->save_cursor;
408 setpcmark();
409 }
410 curwin->w_cursor = is_state->search_start;
411 }
412 restore_viewstate(&is_state->old_viewstate);
413 highlight_match = FALSE;
414 validate_cursor(); /* needed for TAB */
415 if (call_update_screen)
416 update_screen(SOME_VALID);
417 else
418 redraw_all_later(SOME_VALID);
Bram Moolenaar167ae422018-08-14 21:32:21 +0200419 p_magic = is_state->magic_save;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200420 }
421}
422
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200423/*
424 * Do 'incsearch' highlighting if desired.
425 */
426 static void
427may_do_incsearch_highlighting(
428 int firstc,
429 long count,
430 incsearch_state_T *is_state)
431{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200432 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200433 int i;
434 pos_T end_pos;
435 struct cmdline_info save_ccline;
436#ifdef FEAT_RELTIME
437 proftime_T tm;
438#endif
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200439 int next_char;
440 int use_last_pat;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200441
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200442 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200443 {
444 finish_incsearch_highlighting(FALSE, is_state, TRUE);
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200445 return;
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200446 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200447
448 // If there is a character waiting, search and redraw later.
449 if (char_avail())
450 {
451 is_state->incsearch_postponed = TRUE;
452 return;
453 }
454 is_state->incsearch_postponed = FALSE;
455
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200456 if (search_first_line == 0)
457 // start at the original cursor position
458 curwin->w_cursor = is_state->search_start;
459 else
460 {
461 // start at the first line in the range
462 curwin->w_cursor.lnum = search_first_line;
463 curwin->w_cursor.col = 0;
464 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200465 save_last_search_pattern();
466
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200467 // Use the previous pattern for ":s//".
468 next_char = ccline.cmdbuff[skiplen + patlen];
469 use_last_pat = patlen == 0 && skiplen > 0
470 && ccline.cmdbuff[skiplen - 1] == next_char;
471
472 // If there is no pattern, don't do anything.
473 if (patlen == 0 && !use_last_pat)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200474 {
475 i = 0;
476 set_no_hlsearch(TRUE); // turn off previous highlight
477 redraw_all_later(SOME_VALID);
478 }
479 else
480 {
481 int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK;
482
483 cursor_off(); // so the user knows we're busy
484 out_flush();
485 ++emsg_off; // so it doesn't beep if bad expr
486#ifdef FEAT_RELTIME
487 // Set the time limit to half a second.
488 profile_setlimit(500L, &tm);
489#endif
490 if (!p_hls)
491 search_flags += SEARCH_KEEP;
Bram Moolenaar976b8472018-08-12 15:49:47 +0200492 if (search_first_line != 0)
493 search_flags += SEARCH_START;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200494 ccline.cmdbuff[skiplen + patlen] = NUL;
495 i = do_search(NULL, firstc == ':' ? '/' : firstc,
496 ccline.cmdbuff + skiplen, count, search_flags,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200497#ifdef FEAT_RELTIME
498 &tm, NULL
499#else
500 NULL, NULL
501#endif
502 );
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200503 ccline.cmdbuff[skiplen + patlen] = next_char;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200504 --emsg_off;
505
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200506 if (curwin->w_cursor.lnum < search_first_line
507 || curwin->w_cursor.lnum > search_last_line)
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200508 {
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200509 // match outside of address range
510 i = 0;
Bram Moolenaar2f6a3462018-08-14 18:16:52 +0200511 curwin->w_cursor = is_state->search_start;
512 }
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200513
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200514 // if interrupted while searching, behave like it failed
515 if (got_int)
516 {
517 (void)vpeekc(); // remove <C-C> from input stream
518 got_int = FALSE; // don't abandon the command line
519 i = 0;
520 }
521 else if (char_avail())
522 // cancelled searching because a char was typed
523 is_state->incsearch_postponed = TRUE;
524 }
525 if (i != 0)
526 highlight_match = TRUE; // highlight position
527 else
528 highlight_match = FALSE; // remove highlight
529
530 // First restore the old curwin values, so the screen is positioned in the
531 // same way as the actual search command.
532 restore_viewstate(&is_state->old_viewstate);
533 changed_cline_bef_curs();
534 update_topline();
535
536 if (i != 0)
537 {
538 pos_T save_pos = curwin->w_cursor;
539
540 is_state->match_start = curwin->w_cursor;
541 set_search_match(&curwin->w_cursor);
542 validate_cursor();
543 end_pos = curwin->w_cursor;
544 is_state->match_end = end_pos;
545 curwin->w_cursor = save_pos;
546 }
547 else
548 end_pos = curwin->w_cursor; // shutup gcc 4
549
550 // Disable 'hlsearch' highlighting if the pattern matches everything.
551 // Avoids a flash when typing "foo\|".
Bram Moolenaarc7f08b72018-08-12 17:39:14 +0200552 if (!use_last_pat)
553 {
554 next_char = ccline.cmdbuff[skiplen + patlen];
555 ccline.cmdbuff[skiplen + patlen] = NUL;
556 if (empty_pattern(ccline.cmdbuff))
557 set_no_hlsearch(TRUE);
558 ccline.cmdbuff[skiplen + patlen] = next_char;
559 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200560
561 validate_cursor();
562 // May redraw the status line to show the cursor position.
563 if (p_ru && curwin->w_status_height > 0)
564 curwin->w_redr_status = TRUE;
565
566 save_cmdline(&save_ccline);
567 update_screen(SOME_VALID);
568 restore_cmdline(&save_ccline);
569 restore_last_search_pattern();
570
571 // Leave it at the end to make CTRL-R CTRL-W work.
572 if (i != 0)
573 curwin->w_cursor = end_pos;
574
575 msg_starthere();
576 redrawcmdline();
577 is_state->did_incsearch = TRUE;
578}
579
580/*
581 * May adjust 'incsearch' highlighting for typing CTRL-G and CTRL-T, go to next
582 * or previous match.
583 * Returns FAIL when jumping to cmdline_not_changed;
584 */
585 static int
586may_adjust_incsearch_highlighting(
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200587 int firstc,
588 long count,
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200589 incsearch_state_T *is_state,
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200590 int c)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200591{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200592 int skiplen, patlen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200593 pos_T t;
594 char_u *pat;
595 int search_flags = SEARCH_NOOF;
596 int i;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200597 int save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200598
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200599 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200600 return OK;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200601 if (patlen == 0 && ccline.cmdbuff[skiplen] == NUL)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200602 return FAIL;
603
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200604 if (firstc == ccline.cmdbuff[skiplen])
Bram Moolenaaref73a282018-08-11 19:02:22 +0200605 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200606 pat = last_search_pattern();
Bram Moolenaaref73a282018-08-11 19:02:22 +0200607 skiplen = 0;
Bram Moolenaard7cc1632018-08-14 20:18:26 +0200608 patlen = (int)STRLEN(pat);
Bram Moolenaaref73a282018-08-11 19:02:22 +0200609 }
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200610 else
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200611 pat = ccline.cmdbuff + skiplen;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200612
613 save_last_search_pattern();
614 cursor_off();
615 out_flush();
616 if (c == Ctrl_G)
617 {
618 t = is_state->match_end;
619 if (LT_POS(is_state->match_start, is_state->match_end))
620 // Start searching at the end of the match not at the beginning of
621 // the next column.
622 (void)decl(&t);
623 search_flags += SEARCH_COL;
624 }
625 else
626 t = is_state->match_start;
627 if (!p_hls)
628 search_flags += SEARCH_KEEP;
629 ++emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200630 save = pat[patlen];
631 pat[patlen] = NUL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200632 i = searchit(curwin, curbuf, &t,
633 c == Ctrl_G ? FORWARD : BACKWARD,
634 pat, count, search_flags,
635 RE_SEARCH, 0, NULL, NULL);
636 --emsg_off;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200637 pat[patlen] = save;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200638 if (i)
639 {
640 is_state->search_start = is_state->match_start;
641 is_state->match_end = t;
642 is_state->match_start = t;
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200643 if (c == Ctrl_T && firstc != '?')
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200644 {
645 // Move just before the current match, so that when nv_search
646 // finishes the cursor will be put back on the match.
647 is_state->search_start = t;
648 (void)decl(&is_state->search_start);
649 }
650 else if (c == Ctrl_G && firstc == '?')
651 {
652 // Move just after the current match, so that when nv_search
653 // finishes the cursor will be put back on the match.
654 is_state->search_start = t;
655 (void)incl(&is_state->search_start);
656 }
657 if (LT_POS(t, is_state->search_start) && c == Ctrl_G)
658 {
659 // wrap around
660 is_state->search_start = t;
661 if (firstc == '?')
662 (void)incl(&is_state->search_start);
663 else
664 (void)decl(&is_state->search_start);
665 }
666
667 set_search_match(&is_state->match_end);
668 curwin->w_cursor = is_state->match_start;
669 changed_cline_bef_curs();
670 update_topline();
671 validate_cursor();
672 highlight_match = TRUE;
673 save_viewstate(&is_state->old_viewstate);
674 update_screen(NOT_VALID);
675 redrawcmdline();
676 }
677 else
678 vim_beep(BO_ERROR);
679 restore_last_search_pattern();
680 return FAIL;
681}
682
683/*
684 * When CTRL-L typed: add character from the match to the pattern.
685 * May set "*c" to the added character.
686 * Return OK when jumping to cmdline_not_changed.
687 */
688 static int
689may_add_char_to_search(int firstc, int *c, incsearch_state_T *is_state)
690{
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200691 int skiplen, patlen;
692
693 if (!do_incsearch_highlighting(firstc, is_state, &skiplen, &patlen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200694 return FAIL;
695
696 // Add a character from under the cursor for 'incsearch'.
697 if (is_state->did_incsearch)
698 {
699 curwin->w_cursor = is_state->match_end;
700 if (!EQUAL_POS(curwin->w_cursor, is_state->search_start))
701 {
702 *c = gchar_cursor();
703
704 // If 'ignorecase' and 'smartcase' are set and the
705 // command line has no uppercase characters, convert
706 // the character to lowercase.
Bram Moolenaarb0acacd2018-08-11 16:40:43 +0200707 if (p_ic && p_scs && !pat_has_uppercase(ccline.cmdbuff + skiplen))
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200708 *c = MB_TOLOWER(*c);
709 if (*c != NUL)
710 {
711 if (*c == firstc || vim_strchr((char_u *)(
712 p_magic ? "\\~^$.*[" : "\\^$"), *c) != NULL)
713 {
714 // put a backslash before special characters
715 stuffcharReadbuff(*c);
716 *c = '\\';
717 }
718 return FAIL;
719 }
720 }
721 }
722 return OK;
723}
Bram Moolenaar9b25af32018-04-28 13:56:09 +0200724#endif
725
Bram Moolenaar66216052017-12-16 16:33:44 +0100726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 * getcmdline() - accept a command line starting with firstc.
728 *
729 * firstc == ':' get ":" command line.
730 * firstc == '/' or '?' get search pattern
731 * firstc == '=' get expression
732 * firstc == '@' get text for input() function
733 * firstc == '>' get text for debug mode
734 * firstc == NUL get text for :insert command
735 * firstc == -1 like NUL, and break on CTRL-C
736 *
737 * The line is collected in ccline.cmdbuff, which is reallocated to fit the
738 * command line.
739 *
740 * Careful: getcmdline() can be called recursively!
741 *
742 * Return pointer to allocated string if there is a commandline, NULL
743 * otherwise.
744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +0100746getcmdline(
747 int firstc,
748 long count UNUSED, /* only used for incremental search */
749 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 int c;
752 int i;
753 int j;
754 int gotesc = FALSE; /* TRUE when <ESC> just typed */
755 int do_abbr; /* when TRUE check for abbr. */
756#ifdef FEAT_CMDHIST
757 char_u *lookfor = NULL; /* string to match */
758 int hiscnt; /* current history line in use */
759 int histype; /* history type to be used */
760#endif
761#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200762 incsearch_state_T is_state;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763#endif
764 int did_wild_list = FALSE; /* did wild_list() recently */
765 int wim_index = 0; /* index in wim_flags[] */
766 int res;
767 int save_msg_scroll = msg_scroll;
768 int save_State = State; /* remember State when called */
769 int some_key_typed = FALSE; /* one of the keys was typed */
770#ifdef FEAT_MOUSE
771 /* mouse drag and release events are ignored, unless they are
772 * preceded with a mouse down event */
773 int ignore_drag_release = TRUE;
774#endif
775#ifdef FEAT_EVAL
776 int break_ctrl_c = FALSE;
777#endif
778 expand_T xpc;
779 long *b_im_ptr = NULL;
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200780#if defined(FEAT_WILDMENU) || defined(FEAT_EVAL)
Bram Moolenaar111ff9f2005-03-08 22:40:03 +0000781 /* Everything that may work recursively should save and restore the
782 * current command line in save_ccline. That includes update_screen(), a
783 * custom status line may invoke ":normal". */
784 struct cmdline_info save_ccline;
785#endif
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200786 int cmdline_type;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef FEAT_EVAL
789 if (firstc == -1)
790 {
791 firstc = NUL;
792 break_ctrl_c = TRUE;
793 }
794#endif
795#ifdef FEAT_RIGHTLEFT
796 /* start without Hebrew mapping for a command line */
797 if (firstc == ':' || firstc == '=' || firstc == '>')
798 cmd_hkmap = 0;
799#endif
800
801 ccline.overstrike = FALSE; /* always start in insert mode */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +0200804 init_incsearch_state(&is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805#endif
806
807 /*
808 * set some variables for redrawcmd()
809 */
810 ccline.cmdfirstc = (firstc == '@' ? 0 : firstc);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000811 ccline.cmdindent = (firstc > 0 ? indent : 0);
812
813 /* alloc initial ccline.cmdbuff */
814 alloc_cmdbuff(exmode_active ? 250 : indent + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 if (ccline.cmdbuff == NULL)
816 return NULL; /* out of memory */
817 ccline.cmdlen = ccline.cmdpos = 0;
818 ccline.cmdbuff[0] = NUL;
Bram Moolenaarf2405ed2017-03-16 19:58:25 +0100819 sb_text_start_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000821 /* autoindent for :insert and :append */
822 if (firstc <= 0)
823 {
Bram Moolenaar2536d4f2015-07-17 13:22:51 +0200824 vim_memset(ccline.cmdbuff, ' ', indent);
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000825 ccline.cmdbuff[indent] = NUL;
826 ccline.cmdpos = indent;
827 ccline.cmdspos = indent;
828 ccline.cmdlen = indent;
829 }
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ExpandInit(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +0000832 ccline.xpc = &xpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833
834#ifdef FEAT_RIGHTLEFT
835 if (curwin->w_p_rl && *curwin->w_p_rlc == 's'
836 && (firstc == '/' || firstc == '?'))
837 cmdmsg_rl = TRUE;
838 else
839 cmdmsg_rl = FALSE;
840#endif
841
842 redir_off = TRUE; /* don't redirect the typed command */
843 if (!cmd_silent)
844 {
845 i = msg_scrolled;
846 msg_scrolled = 0; /* avoid wait_return message */
847 gotocmdline(TRUE);
848 msg_scrolled += i;
849 redrawcmdprompt(); /* draw prompt or indent */
850 set_cmdspos();
851 }
852 xpc.xp_context = EXPAND_NOTHING;
853 xpc.xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000854#ifndef BACKSLASH_IN_FILENAME
855 xpc.xp_shell = FALSE;
856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000858#if defined(FEAT_EVAL)
859 if (ccline.input_fn)
860 {
861 xpc.xp_context = ccline.xp_context;
862 xpc.xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000863# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000864 xpc.xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +0000865# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000866 }
867#endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 /*
870 * Avoid scrolling when called by a recursive do_cmdline(), e.g. when
871 * doing ":@0" when register 0 doesn't contain a CR.
872 */
873 msg_scroll = FALSE;
874
875 State = CMDLINE;
876
877 if (firstc == '/' || firstc == '?' || firstc == '@')
878 {
879 /* Use ":lmap" mappings for search pattern and input(). */
880 if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT)
881 b_im_ptr = &curbuf->b_p_iminsert;
882 else
883 b_im_ptr = &curbuf->b_p_imsearch;
884 if (*b_im_ptr == B_IMODE_LMAP)
885 State |= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100886#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 im_set_active(*b_im_ptr == B_IMODE_IM);
888#endif
889 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100890#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 else if (p_imcmdline)
892 im_set_active(TRUE);
893#endif
894
895#ifdef FEAT_MOUSE
896 setmouse();
897#endif
898#ifdef CURSOR_SHAPE
899 ui_cursor_shape(); /* may show different cursor shape */
900#endif
901
Bram Moolenaarf4d11452005-12-02 00:46:37 +0000902 /* When inside an autocommand for writing "exiting" may be set and
903 * terminal mode set to cooked. Need to set raw mode here then. */
904 settmode(TMODE_RAW);
905
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200906 /* Trigger CmdlineEnter autocommands. */
907 cmdline_type = firstc == NUL ? '-' : firstc;
908 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200909
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910#ifdef FEAT_CMDHIST
911 init_history();
912 hiscnt = hislen; /* set hiscnt to impossible history value */
913 histype = hist_char2type(firstc);
914#endif
915
916#ifdef FEAT_DIGRAPHS
Bram Moolenaar3c65e312009-04-29 16:47:23 +0000917 do_digraph(-1); /* init digraph typeahead */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#endif
919
Bram Moolenaar15a35c42014-06-25 12:26:46 +0200920 /* If something above caused an error, reset the flags, we do want to type
921 * and execute commands. Display may be messed up a bit. */
922 if (did_emsg)
923 redrawcmd();
924 did_emsg = FALSE;
925 got_int = FALSE;
926
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 /*
928 * Collect the command string, handling editing keys.
929 */
930 for (;;)
931 {
Bram Moolenaar29b2d262006-09-10 19:07:28 +0000932 redir_off = TRUE; /* Don't redirect the typed command.
933 Repeated, because a ":redir" inside
934 completion may switch it on. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935#ifdef USE_ON_FLY_SCROLL
936 dont_scroll = FALSE; /* allow scrolling here */
937#endif
938 quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */
939
Bram Moolenaar72532d32018-04-07 19:09:09 +0200940 did_emsg = FALSE; /* There can't really be a reason why an error
941 that occurs while typing a command should
942 cause the command not to be executed. */
943
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 cursorcmd(); /* set the cursor on the right spot */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000945
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100946 /* Get a character. Ignore K_IGNORE and K_NOP, they should not do
947 * anything, such as stop completion. */
Bram Moolenaar30405d32008-01-02 20:55:27 +0000948 do
949 {
950 c = safe_vgetc();
Bram Moolenaarc5aa55d2017-11-28 20:47:40 +0100951 } while (c == K_IGNORE || c == K_NOP);
Bram Moolenaar30405d32008-01-02 20:55:27 +0000952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (KeyTyped)
954 {
955 some_key_typed = TRUE;
956#ifdef FEAT_RIGHTLEFT
957 if (cmd_hkmap)
958 c = hkmap(c);
959# ifdef FEAT_FKMAP
960 if (cmd_fkmap)
961 c = cmdl_fkmap(c);
962# endif
963 if (cmdmsg_rl && !KeyStuffed)
964 {
965 /* Invert horizontal movements and operations. Only when
966 * typed by the user directly, not when the result of a
967 * mapping. */
968 switch (c)
969 {
970 case K_RIGHT: c = K_LEFT; break;
971 case K_S_RIGHT: c = K_S_LEFT; break;
972 case K_C_RIGHT: c = K_C_LEFT; break;
973 case K_LEFT: c = K_RIGHT; break;
974 case K_S_LEFT: c = K_S_RIGHT; break;
975 case K_C_LEFT: c = K_C_RIGHT; break;
976 }
977 }
978#endif
979 }
980
981 /*
982 * Ignore got_int when CTRL-C was typed here.
983 * Don't ignore it in :global, we really need to break then, e.g., for
984 * ":g/pat/normal /pat" (without the <CR>).
985 * Don't ignore it for the input() function.
986 */
987 if ((c == Ctrl_C
988#ifdef UNIX
989 || c == intr_char
990#endif
991 )
992#if defined(FEAT_EVAL) || defined(FEAT_CRYPT)
993 && firstc != '@'
994#endif
995#ifdef FEAT_EVAL
996 && !break_ctrl_c
997#endif
998 && !global_busy)
999 got_int = FALSE;
1000
1001#ifdef FEAT_CMDHIST
1002 /* free old command line when finished moving around in the history
1003 * list */
1004 if (lookfor != NULL
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001005 && c != K_S_DOWN && c != K_S_UP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001006 && c != K_DOWN && c != K_UP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 && c != K_PAGEDOWN && c != K_PAGEUP
1008 && c != K_KPAGEDOWN && c != K_KPAGEUP
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001009 && c != K_LEFT && c != K_RIGHT
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N)))
Bram Moolenaard23a8232018-02-10 18:45:26 +01001011 VIM_CLEAR(lookfor);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012#endif
1013
1014 /*
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001015 * When there are matching completions to select <S-Tab> works like
1016 * CTRL-P (unless 'wc' is <S-Tab>).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 */
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00001018 if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 c = Ctrl_P;
1020
1021#ifdef FEAT_WILDMENU
1022 /* Special translations for 'wildmenu' */
1023 if (did_wild_list && p_wmnu)
1024 {
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001025 if (c == K_LEFT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 c = Ctrl_P;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001027 else if (c == K_RIGHT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 c = Ctrl_N;
1029 }
1030 /* Hitting CR after "emenu Name.": complete submenu */
1031 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu
1032 && ccline.cmdpos > 1
1033 && ccline.cmdbuff[ccline.cmdpos - 1] == '.'
1034 && ccline.cmdbuff[ccline.cmdpos - 2] != '\\'
1035 && (c == '\n' || c == '\r' || c == K_KENTER))
1036 c = K_DOWN;
1037#endif
1038
1039 /* free expanded names when finished walking through matches */
1040 if (xpc.xp_numfiles != -1
1041 && !(c == p_wc && KeyTyped) && c != p_wcm
1042 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A
1043 && c != Ctrl_L)
1044 {
1045 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1046 did_wild_list = FALSE;
1047#ifdef FEAT_WILDMENU
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001048 if (!p_wmnu || (c != K_UP && c != K_DOWN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049#endif
1050 xpc.xp_context = EXPAND_NOTHING;
1051 wim_index = 0;
1052#ifdef FEAT_WILDMENU
1053 if (p_wmnu && wild_menu_showing != 0)
1054 {
1055 int skt = KeyTyped;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001056 int old_RedrawingDisabled = RedrawingDisabled;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001057
1058 if (ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001059 RedrawingDisabled = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060
1061 if (wild_menu_showing == WM_SCROLLED)
1062 {
1063 /* Entered command line, move it up */
1064 cmdline_row--;
1065 redrawcmd();
1066 }
1067 else if (save_p_ls != -1)
1068 {
1069 /* restore 'laststatus' and 'winminheight' */
1070 p_ls = save_p_ls;
1071 p_wmh = save_p_wmh;
1072 last_status(FALSE);
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001073 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 update_screen(VALID); /* redraw the screen NOW */
Bram Moolenaar111ff9f2005-03-08 22:40:03 +00001075 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 redrawcmd();
1077 save_p_ls = -1;
1078 }
1079 else
1080 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 win_redraw_last_status(topframe);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 redraw_statuslines();
1083 }
1084 KeyTyped = skt;
1085 wild_menu_showing = 0;
Bram Moolenaar1e015462005-09-25 22:16:38 +00001086 if (ccline.input_fn)
1087 RedrawingDisabled = old_RedrawingDisabled;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 }
1089#endif
1090 }
1091
1092#ifdef FEAT_WILDMENU
1093 /* Special translations for 'wildmenu' */
1094 if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu)
1095 {
1096 /* Hitting <Down> after "emenu Name.": complete submenu */
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001097 if (c == K_DOWN && ccline.cmdpos > 0
1098 && ccline.cmdbuff[ccline.cmdpos - 1] == '.')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 c = p_wc;
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001100 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
1102 /* Hitting <Up>: Remove one submenu name in front of the
1103 * cursor */
1104 int found = FALSE;
1105
1106 j = (int)(xpc.xp_pattern - ccline.cmdbuff);
1107 i = 0;
1108 while (--j > 0)
1109 {
1110 /* check for start of menu name */
1111 if (ccline.cmdbuff[j] == ' '
1112 && ccline.cmdbuff[j - 1] != '\\')
1113 {
1114 i = j + 1;
1115 break;
1116 }
1117 /* check for start of submenu name */
1118 if (ccline.cmdbuff[j] == '.'
1119 && ccline.cmdbuff[j - 1] != '\\')
1120 {
1121 if (found)
1122 {
1123 i = j + 1;
1124 break;
1125 }
1126 else
1127 found = TRUE;
1128 }
1129 }
1130 if (i > 0)
1131 cmdline_del(i);
1132 c = p_wc;
1133 xpc.xp_context = EXPAND_NOTHING;
1134 }
1135 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001136 if ((xpc.xp_context == EXPAND_FILES
Bram Moolenaar446cb832008-06-24 21:56:24 +00001137 || xpc.xp_context == EXPAND_DIRECTORIES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001138 || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 {
1140 char_u upseg[5];
1141
1142 upseg[0] = PATHSEP;
1143 upseg[1] = '.';
1144 upseg[2] = '.';
1145 upseg[3] = PATHSEP;
1146 upseg[4] = NUL;
1147
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001148 if (c == K_DOWN
1149 && ccline.cmdpos > 0
1150 && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP
1151 && (ccline.cmdpos < 3
1152 || ccline.cmdbuff[ccline.cmdpos - 2] != '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 || ccline.cmdbuff[ccline.cmdpos - 3] != '.'))
1154 {
1155 /* go down a directory */
1156 c = p_wc;
1157 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001158 else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 {
1160 /* If in a direct ancestor, strip off one ../ to go down */
1161 int found = FALSE;
1162
1163 j = ccline.cmdpos;
1164 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1165 while (--j > i)
1166 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001167#ifdef FEAT_MBYTE
1168 if (has_mbyte)
1169 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1170#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 if (vim_ispathsep(ccline.cmdbuff[j]))
1172 {
1173 found = TRUE;
1174 break;
1175 }
1176 }
1177 if (found
1178 && ccline.cmdbuff[j - 1] == '.'
1179 && ccline.cmdbuff[j - 2] == '.'
1180 && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2))
1181 {
1182 cmdline_del(j - 2);
1183 c = p_wc;
1184 }
1185 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001186 else if (c == K_UP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 {
1188 /* go up a directory */
1189 int found = FALSE;
1190
1191 j = ccline.cmdpos - 1;
1192 i = (int)(xpc.xp_pattern - ccline.cmdbuff);
1193 while (--j > i)
1194 {
1195#ifdef FEAT_MBYTE
1196 if (has_mbyte)
1197 j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j);
1198#endif
1199 if (vim_ispathsep(ccline.cmdbuff[j])
1200#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01001201 && vim_strchr((char_u *)" *?[{`$%#",
1202 ccline.cmdbuff[j + 1]) == NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#endif
1204 )
1205 {
1206 if (found)
1207 {
1208 i = j + 1;
1209 break;
1210 }
1211 else
1212 found = TRUE;
1213 }
1214 }
1215
1216 if (!found)
1217 j = i;
1218 else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0)
1219 j += 4;
1220 else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0
1221 && j == i)
1222 j += 3;
1223 else
1224 j = 0;
1225 if (j > 0)
1226 {
1227 /* TODO this is only for DOS/UNIX systems - need to put in
1228 * machine-specific stuff here and in upseg init */
1229 cmdline_del(j);
1230 put_on_cmdline(upseg + 1, 3, FALSE);
1231 }
1232 else if (ccline.cmdpos > i)
1233 cmdline_del(i);
Bram Moolenaar96a89642011-12-08 18:44:51 +01001234
1235 /* Now complete in the new directory. Set KeyTyped in case the
1236 * Up key came from a mapping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 c = p_wc;
Bram Moolenaar96a89642011-12-08 18:44:51 +01001238 KeyTyped = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 }
1240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241
1242#endif /* FEAT_WILDMENU */
1243
1244 /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert
1245 * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */
1246 if (c == Ctrl_BSL)
1247 {
1248 ++no_mapping;
1249 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001250 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 --no_mapping;
1252 --allow_keys;
Bram Moolenaarb7356812012-10-11 04:04:37 +02001253 /* CTRL-\ e doesn't work when obtaining an expression, unless it
1254 * is in a mapping. */
1255 if (c != Ctrl_N && c != Ctrl_G && (c != 'e'
1256 || (ccline.cmdfirstc == '=' && KeyTyped)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 {
1258 vungetc(c);
1259 c = Ctrl_BSL;
1260 }
1261#ifdef FEAT_EVAL
1262 else if (c == 'e')
1263 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02001264 char_u *p = NULL;
1265 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267 /*
1268 * Replace the command line with the result of an expression.
Bram Moolenaar4770d092006-01-12 23:22:24 +00001269 * Need to save and restore the current command line, to be
1270 * able to enter a new one...
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 */
1272 if (ccline.cmdpos == ccline.cmdlen)
1273 new_cmdpos = 99999; /* keep it at the end */
1274 else
1275 new_cmdpos = ccline.cmdpos;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001276
1277 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001279 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 if (c == '=')
1281 {
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001282 /* Need to save and restore ccline. And set "textlock"
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00001283 * to avoid nasty things like going to another buffer when
1284 * evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001285 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001286 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 p = get_expr_line();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001288 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001289 restore_cmdline(&save_ccline);
1290
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001291 if (p != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001293 len = (int)STRLEN(p);
1294 if (realloc_cmdbuff(len + 1) == OK)
1295 {
1296 ccline.cmdlen = len;
1297 STRCPY(ccline.cmdbuff, p);
1298 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001300 /* Restore the cursor or use the position set with
1301 * set_cmdline_pos(). */
1302 if (new_cmdpos > ccline.cmdlen)
1303 ccline.cmdpos = ccline.cmdlen;
1304 else
1305 ccline.cmdpos = new_cmdpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306
Bram Moolenaarfc3c83e2010-10-27 12:58:23 +02001307 KeyTyped = FALSE; /* Don't do p_wc completion. */
1308 redrawcmd();
1309 goto cmdline_changed;
1310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 }
1313 beep_flush();
Bram Moolenaar66b4bf82010-11-16 14:06:08 +01001314 got_int = FALSE; /* don't abandon the command line */
1315 did_emsg = FALSE;
1316 emsg_on_display = FALSE;
1317 redrawcmd();
1318 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 }
1320#endif
1321 else
1322 {
1323 if (c == Ctrl_G && p_im && restart_edit == 0)
1324 restart_edit = 'a';
1325 gotesc = TRUE; /* will free ccline.cmdbuff after putting it
1326 in history */
1327 goto returncmd; /* back to Normal mode */
1328 }
1329 }
1330
1331#ifdef FEAT_CMDWIN
1332 if (c == cedit_key || c == K_CMDWIN)
1333 {
Bram Moolenaar58da7072014-09-09 18:45:49 +02001334 if (ex_normal_busy == 0 && got_int == FALSE)
1335 {
1336 /*
1337 * Open a window to edit the command line (and history).
1338 */
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001339 c = open_cmdwin();
Bram Moolenaar58da7072014-09-09 18:45:49 +02001340 some_key_typed = TRUE;
1341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343# ifdef FEAT_DIGRAPHS
1344 else
1345# endif
1346#endif
1347#ifdef FEAT_DIGRAPHS
1348 c = do_digraph(c);
1349#endif
1350
1351 if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC
1352 && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL)))
1353 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001354 /* In Ex mode a backslash escapes a newline. */
1355 if (exmode_active
1356 && c != ESC
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001357 && ccline.cmdpos == ccline.cmdlen
Bram Moolenaar5f2c5db2007-07-17 16:15:36 +00001358 && ccline.cmdpos > 0
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001359 && ccline.cmdbuff[ccline.cmdpos - 1] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001361 if (c == K_KENTER)
1362 c = '\n';
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00001364 else
1365 {
1366 gotesc = FALSE; /* Might have typed ESC previously, don't
1367 truncate the cmdline now. */
1368 if (ccheck_abbr(c + ABBR_OFF))
1369 goto cmdline_changed;
1370 if (!cmd_silent)
1371 {
1372 windgoto(msg_row, 0);
1373 out_flush();
1374 }
1375 break;
1376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378
1379 /*
1380 * Completion for 'wildchar' or 'wildcharm' key.
1381 * - hitting <ESC> twice means: abandon command line.
1382 * - wildcard expansion is only done when the 'wildchar' key is really
1383 * typed, not when it comes from a macro
1384 */
1385 if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm)
1386 {
1387 if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */
1388 {
1389 /* if 'wildmode' contains "list" may still need to list */
1390 if (xpc.xp_numfiles > 1
1391 && !did_wild_list
1392 && (wim_flags[wim_index] & WIM_LIST))
1393 {
1394 (void)showmatches(&xpc, FALSE);
1395 redrawcmd();
1396 did_wild_list = TRUE;
1397 }
1398 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001399 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1400 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001402 res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1403 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404 else
1405 res = OK; /* don't insert 'wildchar' now */
1406 }
1407 else /* typed p_wc first time */
1408 {
1409 wim_index = 0;
1410 j = ccline.cmdpos;
1411 /* if 'wildmode' first contains "longest", get longest
1412 * common part */
1413 if (wim_flags[0] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001414 res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1415 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 else
Bram Moolenaarb3479632012-11-28 16:49:58 +01001417 res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP,
1418 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 /* if interrupted while completing, behave like it failed */
1421 if (got_int)
1422 {
1423 (void)vpeekc(); /* remove <C-C> from input stream */
1424 got_int = FALSE; /* don't abandon the command line */
1425 (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE);
1426#ifdef FEAT_WILDMENU
1427 xpc.xp_context = EXPAND_NOTHING;
1428#endif
1429 goto cmdline_changed;
1430 }
1431
1432 /* when more than one match, and 'wildmode' first contains
1433 * "list", or no change and 'wildmode' contains "longest,list",
1434 * list all matches */
1435 if (res == OK && xpc.xp_numfiles > 1)
1436 {
1437 /* a "longest" that didn't do anything is skipped (but not
1438 * "list:longest") */
1439 if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j)
1440 wim_index = 1;
1441 if ((wim_flags[wim_index] & WIM_LIST)
1442#ifdef FEAT_WILDMENU
1443 || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0)
1444#endif
1445 )
1446 {
1447 if (!(wim_flags[0] & WIM_LONGEST))
1448 {
1449#ifdef FEAT_WILDMENU
1450 int p_wmnu_save = p_wmnu;
1451 p_wmnu = 0;
1452#endif
Bram Moolenaarb3479632012-11-28 16:49:58 +01001453 /* remove match */
1454 nextwild(&xpc, WILD_PREV, 0, firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef FEAT_WILDMENU
1456 p_wmnu = p_wmnu_save;
1457#endif
1458 }
1459#ifdef FEAT_WILDMENU
1460 (void)showmatches(&xpc, p_wmnu
1461 && ((wim_flags[wim_index] & WIM_LIST) == 0));
1462#else
1463 (void)showmatches(&xpc, FALSE);
1464#endif
1465 redrawcmd();
1466 did_wild_list = TRUE;
1467 if (wim_flags[wim_index] & WIM_LONGEST)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001468 nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP,
1469 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 else if (wim_flags[wim_index] & WIM_FULL)
Bram Moolenaarb3479632012-11-28 16:49:58 +01001471 nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP,
1472 firstc != '@');
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 }
1474 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001475 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 }
1477#ifdef FEAT_WILDMENU
1478 else if (xpc.xp_numfiles == -1)
1479 xpc.xp_context = EXPAND_NOTHING;
1480#endif
1481 }
1482 if (wim_index < 3)
1483 ++wim_index;
1484 if (c == ESC)
1485 gotesc = TRUE;
1486 if (res == OK)
1487 goto cmdline_changed;
1488 }
1489
1490 gotesc = FALSE;
1491
1492 /* <S-Tab> goes to last match, in a clumsy way */
1493 if (c == K_S_TAB && KeyTyped)
1494 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01001495 if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK
1496 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK
1497 && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 goto cmdline_changed;
1499 }
1500
1501 if (c == NUL || c == K_ZERO) /* NUL is stored as NL */
1502 c = NL;
1503
1504 do_abbr = TRUE; /* default: check for abbreviation */
1505
1506 /*
1507 * Big switch for a typed command line character.
1508 */
1509 switch (c)
1510 {
1511 case K_BS:
1512 case Ctrl_H:
1513 case K_DEL:
1514 case K_KDEL:
1515 case Ctrl_W:
1516#ifdef FEAT_FKMAP
1517 if (cmd_fkmap && c == K_BS)
1518 c = K_DEL;
1519#endif
1520 if (c == K_KDEL)
1521 c = K_DEL;
1522
1523 /*
1524 * delete current character is the same as backspace on next
1525 * character, except at end of line
1526 */
1527 if (c == K_DEL && ccline.cmdpos != ccline.cmdlen)
1528 ++ccline.cmdpos;
1529#ifdef FEAT_MBYTE
1530 if (has_mbyte && c == K_DEL)
1531 ccline.cmdpos += mb_off_next(ccline.cmdbuff,
1532 ccline.cmdbuff + ccline.cmdpos);
1533#endif
1534 if (ccline.cmdpos > 0)
1535 {
1536 char_u *p;
1537
1538 j = ccline.cmdpos;
1539 p = ccline.cmdbuff + j;
1540#ifdef FEAT_MBYTE
1541 if (has_mbyte)
1542 {
1543 p = mb_prevptr(ccline.cmdbuff, p);
1544 if (c == Ctrl_W)
1545 {
1546 while (p > ccline.cmdbuff && vim_isspace(*p))
1547 p = mb_prevptr(ccline.cmdbuff, p);
1548 i = mb_get_class(p);
1549 while (p > ccline.cmdbuff && mb_get_class(p) == i)
1550 p = mb_prevptr(ccline.cmdbuff, p);
1551 if (mb_get_class(p) != i)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001552 p += (*mb_ptr2len)(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 }
1554 }
1555 else
1556#endif
1557 if (c == Ctrl_W)
1558 {
1559 while (p > ccline.cmdbuff && vim_isspace(p[-1]))
1560 --p;
1561 i = vim_iswordc(p[-1]);
1562 while (p > ccline.cmdbuff && !vim_isspace(p[-1])
1563 && vim_iswordc(p[-1]) == i)
1564 --p;
1565 }
1566 else
1567 --p;
1568 ccline.cmdpos = (int)(p - ccline.cmdbuff);
1569 ccline.cmdlen -= j - ccline.cmdpos;
1570 i = ccline.cmdpos;
1571 while (i < ccline.cmdlen)
1572 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1573
1574 /* Truncate at the end, required for multi-byte chars. */
1575 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001576#ifdef FEAT_SEARCH_EXTRA
1577 if (ccline.cmdlen == 0)
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001578 {
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001579 is_state.search_start = is_state.save_cursor;
Bram Moolenaardda933d2016-09-03 21:04:58 +02001580 /* save view settings, so that the screen
1581 * won't be restored at the wrong position */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001582 is_state.old_viewstate = is_state.init_viewstate;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001583 }
1584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 redrawcmd();
1586 }
1587 else if (ccline.cmdlen == 0 && c != Ctrl_W
1588 && ccline.cmdprompt == NULL && indent == 0)
1589 {
1590 /* In ex and debug mode it doesn't make sense to return. */
1591 if (exmode_active
1592#ifdef FEAT_EVAL
1593 || ccline.cmdfirstc == '>'
1594#endif
1595 )
1596 goto cmdline_not_changed;
1597
Bram Moolenaard23a8232018-02-10 18:45:26 +01001598 VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (!cmd_silent)
1600 {
1601#ifdef FEAT_RIGHTLEFT
1602 if (cmdmsg_rl)
1603 msg_col = Columns;
1604 else
1605#endif
1606 msg_col = 0;
1607 msg_putchar(' '); /* delete ':' */
1608 }
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001609#ifdef FEAT_SEARCH_EXTRA
1610 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001611 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 redraw_cmdline = TRUE;
1614 goto returncmd; /* back to cmd mode */
1615 }
1616 goto cmdline_changed;
1617
1618 case K_INS:
1619 case K_KINS:
1620#ifdef FEAT_FKMAP
1621 /* if Farsi mode set, we are in reverse insert mode -
1622 Do not change the mode */
1623 if (cmd_fkmap)
1624 beep_flush();
1625 else
1626#endif
1627 ccline.overstrike = !ccline.overstrike;
1628#ifdef CURSOR_SHAPE
1629 ui_cursor_shape(); /* may show different cursor shape */
1630#endif
1631 goto cmdline_not_changed;
1632
1633 case Ctrl_HAT:
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00001634 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 {
1636 /* ":lmap" mappings exists, toggle use of mappings. */
1637 State ^= LANGMAP;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001638#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 im_set_active(FALSE); /* Disable input method */
1640#endif
1641 if (b_im_ptr != NULL)
1642 {
1643 if (State & LANGMAP)
1644 *b_im_ptr = B_IMODE_LMAP;
1645 else
1646 *b_im_ptr = B_IMODE_NONE;
1647 }
1648 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001649#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 else
1651 {
1652 /* There are no ":lmap" mappings, toggle IM. When
1653 * 'imdisable' is set don't try getting the status, it's
1654 * always off. */
1655 if ((p_imdisable && b_im_ptr != NULL)
1656 ? *b_im_ptr == B_IMODE_IM : im_get_status())
1657 {
1658 im_set_active(FALSE); /* Disable input method */
1659 if (b_im_ptr != NULL)
1660 *b_im_ptr = B_IMODE_NONE;
1661 }
1662 else
1663 {
1664 im_set_active(TRUE); /* Enable input method */
1665 if (b_im_ptr != NULL)
1666 *b_im_ptr = B_IMODE_IM;
1667 }
1668 }
1669#endif
1670 if (b_im_ptr != NULL)
1671 {
1672 if (b_im_ptr == &curbuf->b_p_iminsert)
1673 set_iminsert_global();
1674 else
1675 set_imsearch_global();
1676 }
1677#ifdef CURSOR_SHAPE
1678 ui_cursor_shape(); /* may show different cursor shape */
1679#endif
Bram Moolenaar4033c552017-09-16 20:54:51 +02001680#if defined(FEAT_KEYMAP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 /* Show/unshow value of 'keymap' in status lines later. */
1682 status_redraw_curbuf();
1683#endif
1684 goto cmdline_not_changed;
1685
1686/* case '@': only in very old vi */
1687 case Ctrl_U:
1688 /* delete all characters left of the cursor */
1689 j = ccline.cmdpos;
1690 ccline.cmdlen -= j;
1691 i = ccline.cmdpos = 0;
1692 while (i < ccline.cmdlen)
1693 ccline.cmdbuff[i++] = ccline.cmdbuff[j++];
1694 /* Truncate at the end, required for multi-byte chars. */
1695 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001696#ifdef FEAT_SEARCH_EXTRA
1697 if (ccline.cmdlen == 0)
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02001698 is_state.search_start = is_state.save_cursor;
Bram Moolenaar4d6f32c2016-08-26 19:13:46 +02001699#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700 redrawcmd();
1701 goto cmdline_changed;
1702
1703#ifdef FEAT_CLIPBOARD
1704 case Ctrl_Y:
1705 /* Copy the modeless selection, if there is one. */
1706 if (clip_star.state != SELECT_CLEARED)
1707 {
1708 if (clip_star.state == SELECT_DONE)
1709 clip_copy_modeless_selection(TRUE);
1710 goto cmdline_not_changed;
1711 }
1712 break;
1713#endif
1714
1715 case ESC: /* get here if p_wc != ESC or when ESC typed twice */
1716 case Ctrl_C:
Bram Moolenaarf4d11452005-12-02 00:46:37 +00001717 /* In exmode it doesn't make sense to return. Except when
Bram Moolenaar7c626922005-02-07 22:01:03 +00001718 * ":normal" runs out of characters. */
1719 if (exmode_active
Bram Moolenaare2c38102016-01-31 14:55:40 +01001720 && (ex_normal_busy == 0 || typebuf.tb_len > 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 goto cmdline_not_changed;
1722
1723 gotesc = TRUE; /* will free ccline.cmdbuff after
1724 putting it in history */
1725 goto returncmd; /* back to cmd mode */
1726
1727 case Ctrl_R: /* insert register */
1728#ifdef USE_ON_FLY_SCROLL
1729 dont_scroll = TRUE; /* disallow scrolling here */
1730#endif
1731 putcmdline('"', TRUE);
1732 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001733 i = c = plain_vgetc(); /* CTRL-R <char> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734 if (i == Ctrl_O)
1735 i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */
1736 if (i == Ctrl_R)
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001737 c = plain_vgetc(); /* CTRL-R CTRL-R <char> */
Bram Moolenaara92522f2017-07-15 15:21:38 +02001738 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 --no_mapping;
1740#ifdef FEAT_EVAL
1741 /*
1742 * Insert the result of an expression.
1743 * Need to save the current command line, to be able to enter
1744 * a new one...
1745 */
1746 new_cmdpos = -1;
1747 if (c == '=')
1748 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (ccline.cmdfirstc == '=')/* can't do this recursively */
1750 {
1751 beep_flush();
1752 c = ESC;
1753 }
1754 else
1755 {
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001756 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 c = get_expr_register();
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00001758 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 }
1760 }
1761#endif
1762 if (c != ESC) /* use ESC to cancel inserting register */
1763 {
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001764 cmdline_paste(c, i == Ctrl_R, FALSE);
Bram Moolenaaracf53452005-12-17 22:06:52 +00001765
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001766#ifdef FEAT_EVAL
Bram Moolenaaracf53452005-12-17 22:06:52 +00001767 /* When there was a serious error abort getting the
1768 * command line. */
1769 if (aborting())
1770 {
1771 gotesc = TRUE; /* will free ccline.cmdbuff after
1772 putting it in history */
1773 goto returncmd; /* back to cmd mode */
1774 }
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 KeyTyped = FALSE; /* Don't do p_wc completion. */
1777#ifdef FEAT_EVAL
1778 if (new_cmdpos >= 0)
1779 {
1780 /* set_cmdline_pos() was used */
1781 if (new_cmdpos > ccline.cmdlen)
1782 ccline.cmdpos = ccline.cmdlen;
1783 else
1784 ccline.cmdpos = new_cmdpos;
1785 }
1786#endif
1787 }
1788 redrawcmd();
1789 goto cmdline_changed;
1790
1791 case Ctrl_D:
1792 if (showmatches(&xpc, FALSE) == EXPAND_NOTHING)
1793 break; /* Use ^D as normal char instead */
1794
1795 redrawcmd();
1796 continue; /* don't do incremental search now */
1797
1798 case K_RIGHT:
1799 case K_S_RIGHT:
1800 case K_C_RIGHT:
1801 do
1802 {
1803 if (ccline.cmdpos >= ccline.cmdlen)
1804 break;
1805 i = cmdline_charsize(ccline.cmdpos);
1806 if (KeyTyped && ccline.cmdspos + i >= Columns * Rows)
1807 break;
1808 ccline.cmdspos += i;
1809#ifdef FEAT_MBYTE
1810 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001811 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812 + ccline.cmdpos);
1813 else
1814#endif
1815 ++ccline.cmdpos;
1816 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001817 while ((c == K_S_RIGHT || c == K_C_RIGHT
1818 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 && ccline.cmdbuff[ccline.cmdpos] != ' ');
1820#ifdef FEAT_MBYTE
1821 if (has_mbyte)
1822 set_cmdspos_cursor();
1823#endif
1824 goto cmdline_not_changed;
1825
1826 case K_LEFT:
1827 case K_S_LEFT:
1828 case K_C_LEFT:
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001829 if (ccline.cmdpos == 0)
1830 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 do
1832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 --ccline.cmdpos;
1834#ifdef FEAT_MBYTE
1835 if (has_mbyte) /* move to first byte of char */
1836 ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff,
1837 ccline.cmdbuff + ccline.cmdpos);
1838#endif
1839 ccline.cmdspos -= cmdline_charsize(ccline.cmdpos);
1840 }
Bram Moolenaar49feabd2007-12-07 19:28:58 +00001841 while (ccline.cmdpos > 0
1842 && (c == K_S_LEFT || c == K_C_LEFT
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001843 || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 && ccline.cmdbuff[ccline.cmdpos - 1] != ' ');
1845#ifdef FEAT_MBYTE
1846 if (has_mbyte)
1847 set_cmdspos_cursor();
1848#endif
1849 goto cmdline_not_changed;
1850
1851 case K_IGNORE:
Bram Moolenaar3bab9392017-04-07 15:42:25 +02001852 /* Ignore mouse event or open_cmdwin() result. */
Bram Moolenaar30405d32008-01-02 20:55:27 +00001853 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
Bram Moolenaar4770d092006-01-12 23:22:24 +00001855#ifdef FEAT_GUI_W32
1856 /* On Win32 ignore <M-F4>, we get it when closing the window was
1857 * cancelled. */
1858 case K_F4:
1859 if (mod_mask == MOD_MASK_ALT)
1860 {
1861 redrawcmd(); /* somehow the cmdline is cleared */
1862 goto cmdline_not_changed;
1863 }
1864 break;
1865#endif
1866
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867#ifdef FEAT_MOUSE
1868 case K_MIDDLEDRAG:
1869 case K_MIDDLERELEASE:
1870 goto cmdline_not_changed; /* Ignore mouse */
1871
1872 case K_MIDDLEMOUSE:
1873# ifdef FEAT_GUI
1874 /* When GUI is active, also paste when 'mouse' is empty */
1875 if (!gui.in_use)
1876# endif
1877 if (!mouse_has(MOUSE_COMMAND))
1878 goto cmdline_not_changed; /* Ignore mouse */
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001879# ifdef FEAT_CLIPBOARD
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (clip_star.available)
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001881 cmdline_paste('*', TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 else
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001883# endif
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001884 cmdline_paste(0, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 redrawcmd();
1886 goto cmdline_changed;
1887
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001888# ifdef FEAT_DND
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 case K_DROP:
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00001890 cmdline_paste('~', TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 redrawcmd();
1892 goto cmdline_changed;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894
1895 case K_LEFTDRAG:
1896 case K_LEFTRELEASE:
1897 case K_RIGHTDRAG:
1898 case K_RIGHTRELEASE:
1899 /* Ignore drag and release events when the button-down wasn't
1900 * seen before. */
1901 if (ignore_drag_release)
1902 goto cmdline_not_changed;
1903 /* FALLTHROUGH */
1904 case K_LEFTMOUSE:
1905 case K_RIGHTMOUSE:
1906 if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE)
1907 ignore_drag_release = TRUE;
1908 else
1909 ignore_drag_release = FALSE;
1910# ifdef FEAT_GUI
1911 /* When GUI is active, also move when 'mouse' is empty */
1912 if (!gui.in_use)
1913# endif
1914 if (!mouse_has(MOUSE_COMMAND))
1915 goto cmdline_not_changed; /* Ignore mouse */
1916# ifdef FEAT_CLIPBOARD
1917 if (mouse_row < cmdline_row && clip_star.available)
1918 {
1919 int button, is_click, is_drag;
1920
1921 /*
1922 * Handle modeless selection.
1923 */
1924 button = get_mouse_button(KEY2TERMCAP1(c),
1925 &is_click, &is_drag);
1926 if (mouse_model_popup() && button == MOUSE_LEFT
1927 && (mod_mask & MOD_MASK_SHIFT))
1928 {
1929 /* Translate shift-left to right button. */
1930 button = MOUSE_RIGHT;
1931 mod_mask &= ~MOD_MASK_SHIFT;
1932 }
1933 clip_modeless(button, is_click, is_drag);
1934 goto cmdline_not_changed;
1935 }
1936# endif
1937
1938 set_cmdspos();
1939 for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen;
1940 ++ccline.cmdpos)
1941 {
1942 i = cmdline_charsize(ccline.cmdpos);
1943 if (mouse_row <= cmdline_row + ccline.cmdspos / Columns
1944 && mouse_col < ccline.cmdspos % Columns + i)
1945 break;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001946# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 if (has_mbyte)
1948 {
1949 /* Count ">" for double-wide char that doesn't fit. */
1950 correct_cmdspos(ccline.cmdpos, i);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001951 ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 + ccline.cmdpos) - 1;
1953 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001954# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 ccline.cmdspos += i;
1956 }
1957 goto cmdline_not_changed;
1958
1959 /* Mouse scroll wheel: ignored here */
1960 case K_MOUSEDOWN:
1961 case K_MOUSEUP:
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001962 case K_MOUSELEFT:
1963 case K_MOUSERIGHT:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 /* Alternate buttons ignored here */
1965 case K_X1MOUSE:
1966 case K_X1DRAG:
1967 case K_X1RELEASE:
1968 case K_X2MOUSE:
1969 case K_X2DRAG:
1970 case K_X2RELEASE:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001971 case K_MOUSEMOVE:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 goto cmdline_not_changed;
1973
1974#endif /* FEAT_MOUSE */
1975
1976#ifdef FEAT_GUI
1977 case K_LEFTMOUSE_NM: /* mousefocus click, ignored */
1978 case K_LEFTRELEASE_NM:
1979 goto cmdline_not_changed;
1980
1981 case K_VER_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001982 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 {
1984 gui_do_scroll();
1985 redrawcmd();
1986 }
1987 goto cmdline_not_changed;
1988
1989 case K_HOR_SCROLLBAR:
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00001990 if (msg_scrolled == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001992 gui_do_horiz_scroll(scrollbar_value, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 redrawcmd();
1994 }
1995 goto cmdline_not_changed;
1996#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001997#ifdef FEAT_GUI_TABLINE
1998 case K_TABLINE:
1999 case K_TABMENU:
2000 /* Don't want to change any tabs here. Make sure the same tab
2001 * is still selected. */
2002 if (gui_use_tabline())
2003 gui_mch_set_curtab(tabpage_index(curtab));
2004 goto cmdline_not_changed;
2005#endif
2006
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 case K_SELECT: /* end of Select mode mapping - ignore */
2008 goto cmdline_not_changed;
2009
2010 case Ctrl_B: /* begin of command line */
2011 case K_HOME:
2012 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 case K_S_HOME:
2014 case K_C_HOME:
2015 ccline.cmdpos = 0;
2016 set_cmdspos();
2017 goto cmdline_not_changed;
2018
2019 case Ctrl_E: /* end of command line */
2020 case K_END:
2021 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 case K_S_END:
2023 case K_C_END:
2024 ccline.cmdpos = ccline.cmdlen;
2025 set_cmdspos_cursor();
2026 goto cmdline_not_changed;
2027
2028 case Ctrl_A: /* all matches */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002029 if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 break;
2031 goto cmdline_changed;
2032
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002033 case Ctrl_L:
2034#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002035 if (may_add_char_to_search(firstc, &c, &is_state) == OK)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002036 goto cmdline_not_changed;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00002037#endif
2038
2039 /* completion: longest common part */
Bram Moolenaarb3479632012-11-28 16:49:58 +01002040 if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 break;
2042 goto cmdline_changed;
2043
2044 case Ctrl_N: /* next match */
2045 case Ctrl_P: /* previous match */
Bram Moolenaar7df0f632016-08-26 19:56:00 +02002046 if (xpc.xp_numfiles > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 {
Bram Moolenaarb3479632012-11-28 16:49:58 +01002048 if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT,
2049 0, firstc != '@') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 break;
Bram Moolenaar11956692016-08-27 16:26:56 +02002051 goto cmdline_not_changed;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053#ifdef FEAT_CMDHIST
Bram Moolenaar2f40d122017-10-24 21:49:36 +02002054 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 case K_UP:
2056 case K_DOWN:
2057 case K_S_UP:
2058 case K_S_DOWN:
2059 case K_PAGEUP:
2060 case K_KPAGEUP:
2061 case K_PAGEDOWN:
2062 case K_KPAGEDOWN:
2063 if (hislen == 0 || firstc == NUL) /* no history */
2064 goto cmdline_not_changed;
2065
2066 i = hiscnt;
2067
2068 /* save current command string so it can be restored later */
2069 if (lookfor == NULL)
2070 {
2071 if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL)
2072 goto cmdline_not_changed;
2073 lookfor[ccline.cmdpos] = NUL;
2074 }
2075
2076 j = (int)STRLEN(lookfor);
2077 for (;;)
2078 {
2079 /* one step backwards */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002080 if (c == K_UP|| c == K_S_UP || c == Ctrl_P
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002081 || c == K_PAGEUP || c == K_KPAGEUP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 if (hiscnt == hislen) /* first time */
2084 hiscnt = hisidx[histype];
2085 else if (hiscnt == 0 && hisidx[histype] != hislen - 1)
2086 hiscnt = hislen - 1;
2087 else if (hiscnt != hisidx[histype] + 1)
2088 --hiscnt;
2089 else /* at top of list */
2090 {
2091 hiscnt = i;
2092 break;
2093 }
2094 }
2095 else /* one step forwards */
2096 {
2097 /* on last entry, clear the line */
2098 if (hiscnt == hisidx[histype])
2099 {
2100 hiscnt = hislen;
2101 break;
2102 }
2103
2104 /* not on a history line, nothing to do */
2105 if (hiscnt == hislen)
2106 break;
2107 if (hiscnt == hislen - 1) /* wrap around */
2108 hiscnt = 0;
2109 else
2110 ++hiscnt;
2111 }
2112 if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL)
2113 {
2114 hiscnt = i;
2115 break;
2116 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +00002117 if ((c != K_UP && c != K_DOWN)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00002118 || hiscnt == i
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 || STRNCMP(history[histype][hiscnt].hisstr,
2120 lookfor, (size_t)j) == 0)
2121 break;
2122 }
2123
2124 if (hiscnt != i) /* jumped to other entry */
2125 {
2126 char_u *p;
2127 int len;
2128 int old_firstc;
2129
2130 vim_free(ccline.cmdbuff);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002131 xpc.xp_context = EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (hiscnt == hislen)
2133 p = lookfor; /* back to the old one */
2134 else
2135 p = history[histype][hiscnt].hisstr;
2136
2137 if (histype == HIST_SEARCH
2138 && p != lookfor
2139 && (old_firstc = p[STRLEN(p) + 1]) != firstc)
2140 {
2141 /* Correct for the separator character used when
2142 * adding the history entry vs the one used now.
2143 * First loop: count length.
2144 * Second loop: copy the characters. */
2145 for (i = 0; i <= 1; ++i)
2146 {
2147 len = 0;
2148 for (j = 0; p[j] != NUL; ++j)
2149 {
2150 /* Replace old sep with new sep, unless it is
2151 * escaped. */
2152 if (p[j] == old_firstc
2153 && (j == 0 || p[j - 1] != '\\'))
2154 {
2155 if (i > 0)
2156 ccline.cmdbuff[len] = firstc;
2157 }
2158 else
2159 {
2160 /* Escape new sep, unless it is already
2161 * escaped. */
2162 if (p[j] == firstc
2163 && (j == 0 || p[j - 1] != '\\'))
2164 {
2165 if (i > 0)
2166 ccline.cmdbuff[len] = '\\';
2167 ++len;
2168 }
2169 if (i > 0)
2170 ccline.cmdbuff[len] = p[j];
2171 }
2172 ++len;
2173 }
2174 if (i == 0)
2175 {
2176 alloc_cmdbuff(len);
2177 if (ccline.cmdbuff == NULL)
2178 goto returncmd;
2179 }
2180 }
2181 ccline.cmdbuff[len] = NUL;
2182 }
2183 else
2184 {
2185 alloc_cmdbuff((int)STRLEN(p));
2186 if (ccline.cmdbuff == NULL)
2187 goto returncmd;
2188 STRCPY(ccline.cmdbuff, p);
2189 }
2190
2191 ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
2192 redrawcmd();
2193 goto cmdline_changed;
2194 }
2195 beep_flush();
Bram Moolenaar11956692016-08-27 16:26:56 +02002196#endif
2197 goto cmdline_not_changed;
2198
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002199#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar11956692016-08-27 16:26:56 +02002200 case Ctrl_G: /* next match */
2201 case Ctrl_T: /* previous match */
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002202 if (may_adjust_incsearch_highlighting(
2203 firstc, count, &is_state, c) == FAIL)
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002204 goto cmdline_not_changed;
Bram Moolenaar349e7d92016-09-03 20:04:34 +02002205 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#endif
2207
2208 case Ctrl_V:
2209 case Ctrl_Q:
2210#ifdef FEAT_MOUSE
2211 ignore_drag_release = TRUE;
2212#endif
2213 putcmdline('^', TRUE);
2214 c = get_literal(); /* get next (two) character(s) */
2215 do_abbr = FALSE; /* don't do abbreviation now */
Bram Moolenaara92522f2017-07-15 15:21:38 +02002216 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217#ifdef FEAT_MBYTE
2218 /* may need to remove ^ when composing char was typed */
2219 if (enc_utf8 && utf_iscomposing(c) && !cmd_silent)
2220 {
2221 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
2222 msg_putchar(' ');
2223 cursorcmd();
2224 }
2225#endif
2226 break;
2227
2228#ifdef FEAT_DIGRAPHS
2229 case Ctrl_K:
2230#ifdef FEAT_MOUSE
2231 ignore_drag_release = TRUE;
2232#endif
2233 putcmdline('?', TRUE);
2234#ifdef USE_ON_FLY_SCROLL
2235 dont_scroll = TRUE; /* disallow scrolling here */
2236#endif
2237 c = get_digraph(TRUE);
Bram Moolenaara92522f2017-07-15 15:21:38 +02002238 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 if (c != NUL)
2240 break;
2241
2242 redrawcmd();
2243 goto cmdline_not_changed;
2244#endif /* FEAT_DIGRAPHS */
2245
2246#ifdef FEAT_RIGHTLEFT
2247 case Ctrl__: /* CTRL-_: switch language mode */
2248 if (!p_ari)
2249 break;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002250# ifdef FEAT_FKMAP
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 if (p_altkeymap)
2252 {
2253 cmd_fkmap = !cmd_fkmap;
2254 if (cmd_fkmap) /* in Farsi always in Insert mode */
2255 ccline.overstrike = FALSE;
2256 }
2257 else /* Hebrew is default */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02002258# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 cmd_hkmap = !cmd_hkmap;
2260 goto cmdline_not_changed;
2261#endif
2262
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002263 case K_PS:
2264 bracketed_paste(PASTE_CMDLINE, FALSE, NULL);
2265 goto cmdline_changed;
2266
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 default:
2268#ifdef UNIX
2269 if (c == intr_char)
2270 {
2271 gotesc = TRUE; /* will free ccline.cmdbuff after
2272 putting it in history */
2273 goto returncmd; /* back to Normal mode */
2274 }
2275#endif
2276 /*
2277 * Normal character with no special meaning. Just set mod_mask
2278 * to 0x0 so that typing Shift-Space in the GUI doesn't enter
2279 * the string <S-Space>. This should only happen after ^V.
2280 */
2281 if (!IS_SPECIAL(c))
2282 mod_mask = 0x0;
2283 break;
2284 }
2285 /*
2286 * End of switch on command line character.
2287 * We come here if we have a normal character.
2288 */
2289
Bram Moolenaarede3e632013-06-23 16:16:19 +02002290 if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291#ifdef FEAT_MBYTE
2292 /* Add ABBR_OFF for characters above 0x100, this is
2293 * what check_abbr() expects. */
2294 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
2295#endif
Bram Moolenaarede3e632013-06-23 16:16:19 +02002296 c) || c == Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 goto cmdline_changed;
2298
2299 /*
2300 * put the character in the command line
2301 */
2302 if (IS_SPECIAL(c) || mod_mask != 0)
2303 put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE);
2304 else
2305 {
2306#ifdef FEAT_MBYTE
2307 if (has_mbyte)
2308 {
2309 j = (*mb_char2bytes)(c, IObuff);
2310 IObuff[j] = NUL; /* exclude composing chars */
2311 put_on_cmdline(IObuff, j, TRUE);
2312 }
2313 else
2314#endif
2315 {
2316 IObuff[0] = c;
2317 put_on_cmdline(IObuff, 1, TRUE);
2318 }
2319 }
2320 goto cmdline_changed;
2321
2322/*
2323 * This part implements incremental searches for "/" and "?"
2324 * Jump to cmdline_not_changed when a character has been read but the command
2325 * line did not change. Then we only search and redraw if something changed in
2326 * the past.
2327 * Jump to cmdline_changed when the command line did change.
2328 * (Sorry for the goto's, I know it is ugly).
2329 */
2330cmdline_not_changed:
2331#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002332 if (!is_state.incsearch_postponed)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 continue;
2334#endif
2335
2336cmdline_changed:
Bram Moolenaar153b7042018-01-31 15:48:32 +01002337 /* Trigger CmdlineChanged autocommands. */
2338 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED);
Bram Moolenaar153b7042018-01-31 15:48:32 +01002339
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar0ee81cb2018-08-10 22:07:32 +02002341 may_do_incsearch_highlighting(firstc, count, &is_state);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342#endif
2343
2344#ifdef FEAT_RIGHTLEFT
2345 if (cmdmsg_rl
2346# ifdef FEAT_ARABIC
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002347 || (p_arshape && !p_tbidi && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348# endif
2349 )
2350 /* Always redraw the whole command line to fix shaping and
Bram Moolenaar58437e02012-02-22 17:58:04 +01002351 * right-left typing. Not efficient, but it works.
2352 * Do it only when there are no characters left to read
2353 * to avoid useless intermediate redraws. */
2354 if (vpeekc() == NUL)
2355 redrawcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356#endif
2357 }
2358
2359returncmd:
2360
2361#ifdef FEAT_RIGHTLEFT
2362 cmdmsg_rl = FALSE;
2363#endif
2364
2365#ifdef FEAT_FKMAP
2366 cmd_fkmap = 0;
2367#endif
2368
2369 ExpandCleanup(&xpc);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00002370 ccline.xpc = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaarc7f08b72018-08-12 17:39:14 +02002373 finish_incsearch_highlighting(gotesc, &is_state, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374#endif
2375
2376 if (ccline.cmdbuff != NULL)
2377 {
2378 /*
2379 * Put line in history buffer (":" and "=" only when it was typed).
2380 */
2381#ifdef FEAT_CMDHIST
2382 if (ccline.cmdlen && firstc != NUL
2383 && (some_key_typed || histype == HIST_SEARCH))
2384 {
2385 add_to_history(histype, ccline.cmdbuff, TRUE,
2386 histype == HIST_SEARCH ? firstc : NUL);
2387 if (firstc == ':')
2388 {
2389 vim_free(new_last_cmdline);
2390 new_last_cmdline = vim_strsave(ccline.cmdbuff);
2391 }
2392 }
2393#endif
2394
Bram Moolenaarf8e8c062017-10-22 14:44:17 +02002395 if (gotesc)
2396 abandon_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398
2399 /*
2400 * If the screen was shifted up, redraw the whole screen (later).
2401 * If the line is too long, clear it, so ruler and shown command do
2402 * not get printed in the middle of it.
2403 */
2404 msg_check();
2405 msg_scroll = save_msg_scroll;
2406 redir_off = FALSE;
2407
2408 /* When the command line was typed, no need for a wait-return prompt. */
2409 if (some_key_typed)
2410 need_wait_return = FALSE;
2411
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002412 /* Trigger CmdlineLeave autocommands. */
2413 trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE);
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02002414
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 State = save_State;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002416#ifdef HAVE_INPUT_METHOD
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP)
2418 im_save_status(b_im_ptr);
2419 im_set_active(FALSE);
2420#endif
2421#ifdef FEAT_MOUSE
2422 setmouse();
2423#endif
2424#ifdef CURSOR_SHAPE
2425 ui_cursor_shape(); /* may show different cursor shape */
2426#endif
Bram Moolenaarf2405ed2017-03-16 19:58:25 +01002427 sb_text_end_cmdline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002429 {
2430 char_u *p = ccline.cmdbuff;
2431
2432 /* Make ccline empty, getcmdline() may try to use it. */
2433 ccline.cmdbuff = NULL;
2434 return p;
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436}
2437
2438#if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO)
2439/*
2440 * Get a command line with a prompt.
2441 * This is prepared to be called recursively from getcmdline() (e.g. by
2442 * f_input() when evaluating an expression from CTRL-R =).
2443 * Returns the command line in allocated memory, or NULL.
2444 */
2445 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002446getcmdline_prompt(
2447 int firstc,
2448 char_u *prompt, /* command line prompt */
2449 int attr, /* attributes for prompt */
2450 int xp_context, /* type of expansion */
2451 char_u *xp_arg) /* user-defined expansion argument */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453 char_u *s;
2454 struct cmdline_info save_ccline;
2455 int msg_col_save = msg_col;
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002456 int msg_silent_save = msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002458 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 ccline.cmdprompt = prompt;
2460 ccline.cmdattr = attr;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002461# ifdef FEAT_EVAL
2462 ccline.xp_context = xp_context;
2463 ccline.xp_arg = xp_arg;
2464 ccline.input_fn = (firstc == '@');
2465# endif
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002466 msg_silent = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 s = getcmdline(firstc, 1L, 0);
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00002468 restore_cmdline(&save_ccline);
Bram Moolenaar6135d0d2016-03-22 20:31:13 +01002469 msg_silent = msg_silent_save;
Bram Moolenaar1db1f772011-08-17 16:25:48 +02002470 /* Restore msg_col, the prompt from input() may have changed it.
2471 * But only if called recursively and the commandline is therefore being
2472 * restored to an old one; if not, the input() prompt stays on the screen,
2473 * so we need its modified msg_col left intact. */
2474 if (ccline.cmdbuff != NULL)
2475 msg_col = msg_col_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476
2477 return s;
2478}
2479#endif
2480
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002481/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002482 * Return TRUE when the text must not be changed and we can't switch to
2483 * another window or buffer. Used when editing the command line, evaluating
2484 * 'balloonexpr', etc.
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002485 */
2486 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002487text_locked(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002488{
2489#ifdef FEAT_CMDWIN
2490 if (cmdwin_type != 0)
2491 return TRUE;
2492#endif
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002493 return textlock != 0;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002494}
2495
2496/*
2497 * Give an error message for a command that isn't allowed while the cmdline
2498 * window is open or editing the cmdline in another way.
2499 */
2500 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002501text_locked_msg(void)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002502{
Bram Moolenaar5a497892016-09-03 16:29:04 +02002503 EMSG(_(get_text_locked_msg()));
2504}
2505
2506 char_u *
2507get_text_locked_msg(void)
2508{
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002509#ifdef FEAT_CMDWIN
2510 if (cmdwin_type != 0)
Bram Moolenaar5a497892016-09-03 16:29:04 +02002511 return e_cmdwin;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002512#endif
Bram Moolenaar5a497892016-09-03 16:29:04 +02002513 return e_secure;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00002514}
2515
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002516/*
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002517 * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is
2518 * and give an error message.
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002519 */
2520 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002521curbuf_locked(void)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002522{
2523 if (curbuf_lock > 0)
2524 {
2525 EMSG(_("E788: Not allowed to edit another buffer now"));
2526 return TRUE;
2527 }
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002528 return allbuf_locked();
2529}
2530
2531/*
2532 * Check if "allbuf_lock" is set and return TRUE when it is and give an error
2533 * message.
2534 */
2535 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002536allbuf_locked(void)
Bram Moolenaarbf1b7a72009-03-05 02:15:53 +00002537{
2538 if (allbuf_lock > 0)
2539 {
2540 EMSG(_("E811: Not allowed to change buffer information now"));
2541 return TRUE;
2542 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002543 return FALSE;
2544}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002545
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002547cmdline_charsize(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548{
2549#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
2550 if (cmdline_star > 0) /* showing '*', always 1 position */
2551 return 1;
2552#endif
2553 return ptr2cells(ccline.cmdbuff + idx);
2554}
2555
2556/*
2557 * Compute the offset of the cursor on the command line for the prompt and
2558 * indent.
2559 */
2560 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002561set_cmdspos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00002563 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 ccline.cmdspos = 1 + ccline.cmdindent;
2565 else
2566 ccline.cmdspos = 0 + ccline.cmdindent;
2567}
2568
2569/*
2570 * Compute the screen position for the cursor on the command line.
2571 */
2572 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002573set_cmdspos_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574{
2575 int i, m, c;
2576
2577 set_cmdspos();
2578 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002579 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00002581 if (m < 0) /* overflow, Columns or Rows at weird value */
2582 m = MAXCOL;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 else
2585 m = MAXCOL;
2586 for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i)
2587 {
2588 c = cmdline_charsize(i);
2589#ifdef FEAT_MBYTE
2590 /* Count ">" for double-wide multi-byte char that doesn't fit. */
2591 if (has_mbyte)
2592 correct_cmdspos(i, c);
2593#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00002594 /* If the cmdline doesn't fit, show cursor on last visible char.
2595 * Don't move the cursor itself, so we can still append. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 if ((ccline.cmdspos += c) >= m)
2597 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 ccline.cmdspos -= c;
2599 break;
2600 }
2601#ifdef FEAT_MBYTE
2602 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002603 i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604#endif
2605 }
2606}
2607
2608#ifdef FEAT_MBYTE
2609/*
2610 * Check if the character at "idx", which is "cells" wide, is a multi-byte
2611 * character that doesn't fit, so that a ">" must be displayed.
2612 */
2613 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002614correct_cmdspos(int idx, int cells)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615{
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002616 if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1
2618 && ccline.cmdspos % Columns + cells > Columns)
2619 ccline.cmdspos++;
2620}
2621#endif
2622
2623/*
2624 * Get an Ex command line for the ":" command.
2625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002627getexline(
2628 int c, /* normally ':', NUL for ":append" */
2629 void *cookie UNUSED,
2630 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 /* When executing a register, remove ':' that's in front of each line. */
2633 if (exec_from_reg && vpeekc() == ':')
2634 (void)vgetc();
2635 return getcmdline(c, 1L, indent);
2636}
2637
2638/*
2639 * Get an Ex command line for Ex mode.
2640 * In Ex mode we only use the OS supplied line editing features and no
2641 * mappings or abbreviations.
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002642 * Returns a string in allocated memory or NULL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002645getexmodeline(
2646 int promptc, /* normally ':', NUL for ":append" and '?' for
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002647 :s prompt */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002648 void *cookie UNUSED,
2649 int indent) /* indent for inside conditionals */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002651 garray_T line_ga;
2652 char_u *pend;
2653 int startcol = 0;
Bram Moolenaar76624232007-07-28 12:21:47 +00002654 int c1 = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002655 int escaped = FALSE; /* CTRL-V typed */
2656 int vcol = 0;
2657 char_u *p;
Bram Moolenaar76624232007-07-28 12:21:47 +00002658 int prev_char;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002659 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660
2661 /* Switch cursor on now. This avoids that it happens after the "\n", which
2662 * confuses the system function that computes tabstops. */
2663 cursor_on();
2664
2665 /* always start in column 0; write a newline if necessary */
2666 compute_cmdrow();
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002667 if ((msg_col || msg_didout) && promptc != '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 msg_putchar('\n');
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002669 if (promptc == ':')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002671 /* indent that is only displayed, not in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002672 if (p_prompt)
2673 msg_putchar(':');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 while (indent-- > 0)
2675 msg_putchar(' ');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 startcol = msg_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 }
2678
2679 ga_init2(&line_ga, 1, 30);
2680
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002681 /* autoindent for :insert and :append is in the line itself */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002682 if (promptc <= 0)
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002683 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002684 vcol = indent;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002685 while (indent >= 8)
2686 {
2687 ga_append(&line_ga, TAB);
2688 msg_puts((char_u *)" ");
2689 indent -= 8;
2690 }
2691 while (indent-- > 0)
2692 {
2693 ga_append(&line_ga, ' ');
2694 msg_putchar(' ');
2695 }
2696 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002697 ++no_mapping;
2698 ++allow_keys;
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002699
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 /*
2701 * Get the line, one character at a time.
2702 */
2703 got_int = FALSE;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002704 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002706 long sw;
2707 char_u *s;
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 if (ga_grow(&line_ga, 40) == FAIL)
2710 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaarba748c82017-02-26 14:00:07 +01002712 /*
2713 * Get one character at a time.
2714 */
Bram Moolenaar76624232007-07-28 12:21:47 +00002715 prev_char = c1;
Bram Moolenaarba748c82017-02-26 14:00:07 +01002716
2717 /* Check for a ":normal" command and no more characters left. */
2718 if (ex_normal_busy > 0 && typebuf.tb_len == 0)
2719 c1 = '\n';
2720 else
2721 c1 = vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
2723 /*
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002724 * Handle line editing.
2725 * Previously this was left to the system, putting the terminal in
2726 * cooked mode, but then CTRL-D and CTRL-T can't be used properly.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 */
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002728 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002730 msg_putchar('\n');
2731 break;
2732 }
2733
Bram Moolenaarabbc4482017-01-24 15:57:55 +01002734 if (c1 == K_PS)
2735 {
2736 bracketed_paste(PASTE_EX, FALSE, &line_ga);
2737 goto redraw;
2738 }
2739
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002740 if (!escaped)
2741 {
2742 /* CR typed means "enter", which is NL */
2743 if (c1 == '\r')
2744 c1 = '\n';
2745
2746 if (c1 == BS || c1 == K_BS
2747 || c1 == DEL || c1 == K_DEL || c1 == K_KDEL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002749 if (line_ga.ga_len > 0)
2750 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002751#ifdef FEAT_MBYTE
2752 if (has_mbyte)
2753 {
2754 p = (char_u *)line_ga.ga_data;
2755 p[line_ga.ga_len] = NUL;
2756 len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1;
2757 line_ga.ga_len -= len;
2758 }
2759 else
2760#endif
2761 --line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002762 goto redraw;
2763 }
2764 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 }
2766
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002767 if (c1 == Ctrl_U)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002769 msg_col = startcol;
2770 msg_clr_eos();
2771 line_ga.ga_len = 0;
Bram Moolenaarda636572015-04-03 17:11:45 +02002772 goto redraw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002773 }
2774
2775 if (c1 == Ctrl_T)
2776 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002777 sw = get_sw_value(curbuf);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002778 p = (char_u *)line_ga.ga_data;
2779 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002780 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaar14f24742012-08-08 18:01:05 +02002781 indent += sw - indent % sw;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002782add_indent:
Bram Moolenaar597a4222014-06-25 14:39:50 +02002783 while (get_indent_str(p, 8, FALSE) < indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 {
Bram Moolenaarcde88542015-08-11 19:14:00 +02002785 (void)ga_grow(&line_ga, 2); /* one more for the NUL */
Bram Moolenaarda636572015-04-03 17:11:45 +02002786 p = (char_u *)line_ga.ga_data;
2787 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002788 mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1);
2789 *s = ' ';
2790 ++line_ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002791 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002792redraw:
2793 /* redraw the line */
2794 msg_col = startcol;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002795 vcol = 0;
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002796 p = (char_u *)line_ga.ga_data;
2797 p[line_ga.ga_len] = NUL;
2798 while (p < (char_u *)line_ga.ga_data + line_ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002800 if (*p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002802 do
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002804 msg_putchar(' ');
2805 } while (++vcol % 8);
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002806 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002808 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 {
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002810 len = MB_PTR2LEN(p);
2811 msg_outtrans_len(p, len);
2812 vcol += ptr2cells(p);
2813 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 }
2815 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002816 msg_clr_eos();
Bram Moolenaar76624232007-07-28 12:21:47 +00002817 windgoto(msg_row, msg_col);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002818 continue;
2819 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002820
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002821 if (c1 == Ctrl_D)
2822 {
2823 /* Delete one shiftwidth. */
2824 p = (char_u *)line_ga.ga_data;
2825 if (prev_char == '0' || prev_char == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002827 if (prev_char == '^')
2828 ex_keep_indent = TRUE;
2829 indent = 0;
2830 p[--line_ga.ga_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 }
2832 else
2833 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002834 p[line_ga.ga_len] = NUL;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002835 indent = get_indent_str(p, 8, FALSE);
Bram Moolenaarda636572015-04-03 17:11:45 +02002836 if (indent > 0)
2837 {
2838 --indent;
2839 indent -= indent % get_sw_value(curbuf);
2840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 }
Bram Moolenaar597a4222014-06-25 14:39:50 +02002842 while (get_indent_str(p, 8, FALSE) > indent)
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002843 {
Bram Moolenaarda636572015-04-03 17:11:45 +02002844 s = skipwhite(p);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002845 mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1);
2846 --line_ga.ga_len;
2847 }
2848 goto add_indent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002850
2851 if (c1 == Ctrl_V || c1 == Ctrl_Q)
2852 {
2853 escaped = TRUE;
2854 continue;
2855 }
2856
2857 /* Ignore special key codes: mouse movement, K_IGNORE, etc. */
2858 if (IS_SPECIAL(c1))
2859 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002861
2862 if (IS_SPECIAL(c1))
2863 c1 = '?';
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002864#ifdef FEAT_MBYTE
2865 if (has_mbyte)
2866 len = (*mb_char2bytes)(c1,
2867 (char_u *)line_ga.ga_data + line_ga.ga_len);
2868 else
2869#endif
2870 {
2871 len = 1;
2872 ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1;
2873 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002874 if (c1 == '\n')
2875 msg_putchar('\n');
2876 else if (c1 == TAB)
2877 {
2878 /* Don't use chartabsize(), 'ts' can be different */
2879 do
2880 {
2881 msg_putchar(' ');
2882 } while (++vcol % 8);
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885 {
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002886 msg_outtrans_len(
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002887 ((char_u *)line_ga.ga_data) + line_ga.ga_len, len);
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002888 vcol += char2cells(c1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 }
Bram Moolenaar2d54ec92014-06-12 19:44:48 +02002890 line_ga.ga_len += len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002891 escaped = FALSE;
2892
2893 windgoto(msg_row, msg_col);
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002894 pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002895
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002896 /* We are done when a NL is entered, but not when it comes after an
2897 * odd number of backslashes, that results in a NUL. */
2898 if (line_ga.ga_len > 0 && pend[-1] == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 {
Bram Moolenaar417f5e72010-09-29 15:50:30 +02002900 int bcount = 0;
2901
2902 while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\')
2903 ++bcount;
2904
2905 if (bcount > 0)
2906 {
2907 /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" ->
2908 * "\NL", etc. */
2909 line_ga.ga_len -= (bcount + 1) / 2;
2910 pend -= (bcount + 1) / 2;
2911 pend[-1] = '\n';
2912 }
2913
2914 if ((bcount & 1) == 0)
2915 {
2916 --line_ga.ga_len;
2917 --pend;
2918 *pend = NUL;
2919 break;
2920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 }
2922 }
2923
Bram Moolenaar26a60b42005-02-22 08:49:11 +00002924 --no_mapping;
2925 --allow_keys;
2926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 /* make following messages go to the next line */
2928 msg_didout = FALSE;
2929 msg_col = 0;
2930 if (msg_row < Rows - 1)
2931 ++msg_row;
2932 emsg_on_display = FALSE; /* don't want ui_delay() */
2933
2934 if (got_int)
2935 ga_clear(&line_ga);
2936
2937 return (char_u *)line_ga.ga_data;
2938}
2939
Bram Moolenaare344bea2005-09-01 20:46:49 +00002940# if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \
2941 || defined(FEAT_MOUSESHAPE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942/*
2943 * Return TRUE if ccline.overstrike is on.
2944 */
2945 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002946cmdline_overstrike(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 return ccline.overstrike;
2949}
2950
2951/*
2952 * Return TRUE if the cursor is at the end of the cmdline.
2953 */
2954 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002955cmdline_at_end(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956{
2957 return (ccline.cmdpos >= ccline.cmdlen);
2958}
2959#endif
2960
Bram Moolenaar9372a112005-12-06 19:59:18 +00002961#if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962/*
2963 * Return the virtual column number at the current cursor position.
2964 * This is used by the IM code to obtain the start of the preedit string.
2965 */
2966 colnr_T
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002967cmdline_getvcol_cursor(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen)
2970 return MAXCOL;
2971
2972# ifdef FEAT_MBYTE
2973 if (has_mbyte)
2974 {
2975 colnr_T col;
2976 int i = 0;
2977
2978 for (col = 0; i < ccline.cmdpos; ++col)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002979 i += (*mb_ptr2len)(ccline.cmdbuff + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981 return col;
2982 }
2983 else
2984# endif
2985 return ccline.cmdpos;
2986}
2987#endif
2988
2989#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
2990/*
2991 * If part of the command line is an IM preedit string, redraw it with
2992 * IM feedback attributes. The cursor position is restored after drawing.
2993 */
2994 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01002995redrawcmd_preedit(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996{
2997 if ((State & CMDLINE)
2998 && xic != NULL
Bram Moolenaar494c82a2006-09-14 08:25:49 +00002999 /* && im_get_status() doesn't work when using SCIM */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 && !p_imdisable
3001 && im_is_preediting())
3002 {
3003 int cmdpos = 0;
3004 int cmdspos;
3005 int old_row;
3006 int old_col;
3007 colnr_T col;
3008
3009 old_row = msg_row;
3010 old_col = msg_col;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003011 cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013# ifdef FEAT_MBYTE
3014 if (has_mbyte)
3015 {
3016 for (col = 0; col < preedit_start_col
3017 && cmdpos < ccline.cmdlen; ++col)
3018 {
3019 cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003020 cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 }
3022 }
3023 else
3024# endif
3025 {
3026 cmdspos += preedit_start_col;
3027 cmdpos += preedit_start_col;
3028 }
3029
3030 msg_row = cmdline_row + (cmdspos / (int)Columns);
3031 msg_col = cmdspos % (int)Columns;
3032 if (msg_row >= Rows)
3033 msg_row = Rows - 1;
3034
3035 for (col = 0; cmdpos < ccline.cmdlen; ++col)
3036 {
3037 int char_len;
3038 int char_attr;
3039
3040 char_attr = im_get_feedback_attr(col);
3041 if (char_attr < 0)
3042 break; /* end of preedit string */
3043
3044# ifdef FEAT_MBYTE
3045 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003046 char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 else
3048# endif
3049 char_len = 1;
3050
3051 msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr);
3052 cmdpos += char_len;
3053 }
3054
3055 msg_row = old_row;
3056 msg_col = old_col;
3057 }
3058}
3059#endif /* FEAT_XIM && FEAT_GUI_GTK */
3060
3061/*
3062 * Allocate a new command line buffer.
3063 * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen.
3064 * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen.
3065 */
3066 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003067alloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068{
3069 /*
3070 * give some extra space to avoid having to allocate all the time
3071 */
3072 if (len < 80)
3073 len = 100;
3074 else
3075 len += 20;
3076
3077 ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */
3078 ccline.cmdbufflen = len;
3079}
3080
3081/*
3082 * Re-allocate the command line to length len + something extra.
3083 * return FAIL for failure, OK otherwise
3084 */
3085 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003086realloc_cmdbuff(int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087{
3088 char_u *p;
3089
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003090 if (len < ccline.cmdbufflen)
3091 return OK; /* no need to resize */
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 p = ccline.cmdbuff;
3094 alloc_cmdbuff(len); /* will get some more */
3095 if (ccline.cmdbuff == NULL) /* out of memory */
3096 {
3097 ccline.cmdbuff = p; /* keep the old one */
3098 return FAIL;
3099 }
Bram Moolenaar35a34232010-08-13 16:51:26 +02003100 /* There isn't always a NUL after the command, but it may need to be
3101 * there, thus copy up to the NUL and add a NUL. */
3102 mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen);
3103 ccline.cmdbuff[ccline.cmdlen] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 vim_free(p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003105
3106 if (ccline.xpc != NULL
3107 && ccline.xpc->xp_pattern != NULL
3108 && ccline.xpc->xp_context != EXPAND_NOTHING
3109 && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL)
3110 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00003111 int i = (int)(ccline.xpc->xp_pattern - p);
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003112
3113 /* If xp_pattern points inside the old cmdbuff it needs to be adjusted
3114 * to point into the newly allocated memory. */
3115 if (i >= 0 && i <= ccline.cmdlen)
3116 ccline.xpc->xp_pattern = ccline.cmdbuff + i;
3117 }
3118
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 return OK;
3120}
3121
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003122#if defined(FEAT_ARABIC) || defined(PROTO)
3123static char_u *arshape_buf = NULL;
3124
3125# if defined(EXITFREE) || defined(PROTO)
3126 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003127free_cmdline_buf(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003128{
3129 vim_free(arshape_buf);
3130}
3131# endif
3132#endif
3133
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134/*
3135 * Draw part of the cmdline at the current cursor position. But draw stars
3136 * when cmdline_star is TRUE.
3137 */
3138 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003139draw_cmdline(int start, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140{
3141#if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
3142 int i;
3143
3144 if (cmdline_star > 0)
3145 for (i = 0; i < len; ++i)
3146 {
3147 msg_putchar('*');
3148# ifdef FEAT_MBYTE
3149 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003150 i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151# endif
3152 }
3153 else
3154#endif
3155#ifdef FEAT_ARABIC
3156 if (p_arshape && !p_tbidi && enc_utf8 && len > 0)
3157 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 static int buflen = 0;
3159 char_u *p;
3160 int j;
3161 int newlen = 0;
3162 int mb_l;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00003163 int pc, pc1 = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 int prev_c = 0;
3165 int prev_c1 = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003166 int u8c;
3167 int u8cc[MAX_MCO];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int nc = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169
3170 /*
3171 * Do arabic shaping into a temporary buffer. This is very
3172 * inefficient!
3173 */
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003174 if (len * 2 + 2 > buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
3176 /* Re-allocate the buffer. We keep it around to avoid a lot of
3177 * alloc()/free() calls. */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003178 vim_free(arshape_buf);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003179 buflen = len * 2 + 2;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003180 arshape_buf = alloc(buflen);
3181 if (arshape_buf == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182 return; /* out of memory */
3183 }
3184
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00003185 if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start)))
3186 {
3187 /* Prepend a space to draw the leading composing char on. */
3188 arshape_buf[0] = ' ';
3189 newlen = 1;
3190 }
3191
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 for (j = start; j < start + len; j += mb_l)
3193 {
3194 p = ccline.cmdbuff + j;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003195 u8c = utfc_ptr2char_len(p, u8cc, start + len - j);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003196 mb_l = utfc_ptr2len_len(p, start + len - j);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 if (ARABIC_CHAR(u8c))
3198 {
3199 /* Do Arabic shaping. */
3200 if (cmdmsg_rl)
3201 {
3202 /* displaying from right to left */
3203 pc = prev_c;
3204 pc1 = prev_c1;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003205 prev_c1 = u8cc[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 if (j + mb_l >= start + len)
3207 nc = NUL;
3208 else
3209 nc = utf_ptr2char(p + mb_l);
3210 }
3211 else
3212 {
3213 /* displaying from left to right */
3214 if (j + mb_l >= start + len)
3215 pc = NUL;
3216 else
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003217 {
3218 int pcc[MAX_MCO];
3219
3220 pc = utfc_ptr2char_len(p + mb_l, pcc,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 start + len - j - mb_l);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003222 pc1 = pcc[0];
3223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 nc = prev_c;
3225 }
3226 prev_c = u8c;
3227
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003228 u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003230 newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003231 if (u8cc[0] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003233 newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen);
3234 if (u8cc[1] != 0)
3235 newlen += (*mb_char2bytes)(u8cc[1],
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003236 arshape_buf + newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 }
3238 }
3239 else
3240 {
3241 prev_c = u8c;
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003242 mch_memmove(arshape_buf + newlen, p, mb_l);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 newlen += mb_l;
3244 }
3245 }
3246
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003247 msg_outtrans_len(arshape_buf, newlen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249 else
3250#endif
3251 msg_outtrans_len(ccline.cmdbuff + start, len);
3252}
3253
3254/*
3255 * Put a character on the command line. Shifts the following text to the
3256 * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc.
3257 * "c" must be printable (fit in one display cell)!
3258 */
3259 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003260putcmdline(int c, int shift)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003261{
3262 if (cmd_silent)
3263 return;
3264 msg_no_more = TRUE;
3265 msg_putchar(c);
3266 if (shift)
3267 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3268 msg_no_more = FALSE;
3269 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003270 extra_char = c;
3271 extra_char_shift = shift;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272}
3273
3274/*
3275 * Undo a putcmdline(c, FALSE).
3276 */
3277 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003278unputcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 if (cmd_silent)
3281 return;
3282 msg_no_more = TRUE;
3283 if (ccline.cmdlen == ccline.cmdpos)
3284 msg_putchar(' ');
Bram Moolenaar64fdf5c2012-06-06 12:03:06 +02003285#ifdef FEAT_MBYTE
3286 else if (has_mbyte)
3287 draw_cmdline(ccline.cmdpos,
3288 (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos));
3289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 else
3291 draw_cmdline(ccline.cmdpos, 1);
3292 msg_no_more = FALSE;
3293 cursorcmd();
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003294 extra_char = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297/*
3298 * Put the given string, of the given length, onto the command line.
3299 * If len is -1, then STRLEN() is used to calculate the length.
3300 * If 'redraw' is TRUE then the new part of the command line, and the remaining
3301 * part will be redrawn, otherwise it will not. If this function is called
3302 * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be
3303 * called afterwards.
3304 */
3305 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003306put_on_cmdline(char_u *str, int len, int redraw)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307{
3308 int retval;
3309 int i;
3310 int m;
3311 int c;
3312
3313 if (len < 0)
3314 len = (int)STRLEN(str);
3315
3316 /* Check if ccline.cmdbuff needs to be longer */
3317 if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen)
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003318 retval = realloc_cmdbuff(ccline.cmdlen + len + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 else
3320 retval = OK;
3321 if (retval == OK)
3322 {
3323 if (!ccline.overstrike)
3324 {
3325 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3326 ccline.cmdbuff + ccline.cmdpos,
3327 (size_t)(ccline.cmdlen - ccline.cmdpos));
3328 ccline.cmdlen += len;
3329 }
3330 else
3331 {
3332#ifdef FEAT_MBYTE
3333 if (has_mbyte)
3334 {
3335 /* Count nr of characters in the new string. */
3336 m = 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003337 for (i = 0; i < len; i += (*mb_ptr2len)(str + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 ++m;
3339 /* Count nr of bytes in cmdline that are overwritten by these
3340 * characters. */
3341 for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003342 i += (*mb_ptr2len)(ccline.cmdbuff + i))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 --m;
3344 if (i < ccline.cmdlen)
3345 {
3346 mch_memmove(ccline.cmdbuff + ccline.cmdpos + len,
3347 ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i));
3348 ccline.cmdlen += ccline.cmdpos + len - i;
3349 }
3350 else
3351 ccline.cmdlen = ccline.cmdpos + len;
3352 }
3353 else
3354#endif
3355 if (ccline.cmdpos + len > ccline.cmdlen)
3356 ccline.cmdlen = ccline.cmdpos + len;
3357 }
3358 mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len);
3359 ccline.cmdbuff[ccline.cmdlen] = NUL;
3360
3361#ifdef FEAT_MBYTE
3362 if (enc_utf8)
3363 {
3364 /* When the inserted text starts with a composing character,
3365 * backup to the character before it. There could be two of them.
3366 */
3367 i = 0;
3368 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3369 while (ccline.cmdpos > 0 && utf_iscomposing(c))
3370 {
3371 i = (*mb_head_off)(ccline.cmdbuff,
3372 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3373 ccline.cmdpos -= i;
3374 len += i;
3375 c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos);
3376 }
3377# ifdef FEAT_ARABIC
3378 if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c))
3379 {
3380 /* Check the previous character for Arabic combining pair. */
3381 i = (*mb_head_off)(ccline.cmdbuff,
3382 ccline.cmdbuff + ccline.cmdpos - 1) + 1;
3383 if (arabic_combine(utf_ptr2char(ccline.cmdbuff
3384 + ccline.cmdpos - i), c))
3385 {
3386 ccline.cmdpos -= i;
3387 len += i;
3388 }
3389 else
3390 i = 0;
3391 }
3392# endif
3393 if (i != 0)
3394 {
3395 /* Also backup the cursor position. */
3396 i = ptr2cells(ccline.cmdbuff + ccline.cmdpos);
3397 ccline.cmdspos -= i;
3398 msg_col -= i;
3399 if (msg_col < 0)
3400 {
3401 msg_col += Columns;
3402 --msg_row;
3403 }
3404 }
3405 }
3406#endif
3407
3408 if (redraw && !cmd_silent)
3409 {
3410 msg_no_more = TRUE;
3411 i = cmdline_row;
Bram Moolenaar73dc59a2011-09-30 17:46:21 +02003412 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos);
3414 /* Avoid clearing the rest of the line too often. */
3415 if (cmdline_row != i || ccline.overstrike)
3416 msg_clr_eos();
3417 msg_no_more = FALSE;
3418 }
3419#ifdef FEAT_FKMAP
3420 /*
3421 * If we are in Farsi command mode, the character input must be in
3422 * Insert mode. So do not advance the cmdpos.
3423 */
3424 if (!cmd_fkmap)
3425#endif
3426 {
3427 if (KeyTyped)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 m = Columns * Rows;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00003430 if (m < 0) /* overflow, Columns or Rows at weird value */
3431 m = MAXCOL;
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 else
3434 m = MAXCOL;
3435 for (i = 0; i < len; ++i)
3436 {
3437 c = cmdline_charsize(ccline.cmdpos);
3438#ifdef FEAT_MBYTE
3439 /* count ">" for a double-wide char that doesn't fit. */
3440 if (has_mbyte)
3441 correct_cmdspos(ccline.cmdpos, c);
3442#endif
Bram Moolenaarf9821062008-06-20 16:31:07 +00003443 /* Stop cursor at the end of the screen, but do increment the
3444 * insert position, so that entering a very long command
3445 * works, even though you can't see it. */
3446 if (ccline.cmdspos + c < m)
3447 ccline.cmdspos += c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448#ifdef FEAT_MBYTE
3449 if (has_mbyte)
3450 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003451 c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 if (c > len - i - 1)
3453 c = len - i - 1;
3454 ccline.cmdpos += c;
3455 i += c;
3456 }
3457#endif
3458 ++ccline.cmdpos;
3459 }
3460 }
3461 }
3462 if (redraw)
3463 msg_check();
3464 return retval;
3465}
3466
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003467static struct cmdline_info prev_ccline;
3468static int prev_ccline_used = FALSE;
3469
3470/*
3471 * Save ccline, because obtaining the "=" register may execute "normal :cmd"
3472 * and overwrite it. But get_cmdline_str() may need it, thus make it
3473 * available globally in prev_ccline.
3474 */
3475 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003476save_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003477{
3478 if (!prev_ccline_used)
3479 {
3480 vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info));
3481 prev_ccline_used = TRUE;
3482 }
3483 *ccp = prev_ccline;
3484 prev_ccline = ccline;
3485 ccline.cmdbuff = NULL;
3486 ccline.cmdprompt = NULL;
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00003487 ccline.xpc = NULL;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003488}
3489
3490/*
Bram Moolenaarccc18222007-05-10 18:25:20 +00003491 * Restore ccline after it has been saved with save_cmdline().
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003492 */
3493 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003494restore_cmdline(struct cmdline_info *ccp)
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003495{
3496 ccline = prev_ccline;
3497 prev_ccline = *ccp;
3498}
3499
Bram Moolenaar5a305422006-04-28 22:38:25 +00003500#if defined(FEAT_EVAL) || defined(PROTO)
3501/*
3502 * Save the command line into allocated memory. Returns a pointer to be
3503 * passed to restore_cmdline_alloc() later.
3504 * Returns NULL when failed.
3505 */
3506 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003507save_cmdline_alloc(void)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003508{
3509 struct cmdline_info *p;
3510
3511 p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info));
3512 if (p != NULL)
3513 save_cmdline(p);
3514 return (char_u *)p;
3515}
3516
3517/*
3518 * Restore the command line from the return value of save_cmdline_alloc().
3519 */
3520 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003521restore_cmdline_alloc(char_u *p)
Bram Moolenaar5a305422006-04-28 22:38:25 +00003522{
3523 if (p != NULL)
3524 {
3525 restore_cmdline((struct cmdline_info *)p);
3526 vim_free(p);
3527 }
3528}
3529#endif
3530
Bram Moolenaar8299df92004-07-10 09:47:34 +00003531/*
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003532 * Paste a yank register into the command line.
3533 * Used by CTRL-R command in command-line mode.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003534 * insert_reg() can't be used here, because special characters from the
3535 * register contents will be interpreted as commands.
3536 *
Bram Moolenaar6f62fed2015-12-17 14:04:24 +01003537 * Return FAIL for failure, OK otherwise.
Bram Moolenaar8299df92004-07-10 09:47:34 +00003538 */
3539 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003540cmdline_paste(
3541 int regname,
3542 int literally, /* Insert text literally instead of "as typed" */
3543 int remcr) /* remove trailing CR */
Bram Moolenaar8299df92004-07-10 09:47:34 +00003544{
3545 long i;
3546 char_u *arg;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003547 char_u *p;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003548 int allocated;
3549 struct cmdline_info save_ccline;
3550
3551 /* check for valid regname; also accept special characters for CTRL-R in
3552 * the command line */
3553 if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W
Bram Moolenaare2c8d832018-05-01 19:24:03 +02003554 && regname != Ctrl_A && regname != Ctrl_L
3555 && !valid_yank_reg(regname, FALSE))
Bram Moolenaar8299df92004-07-10 09:47:34 +00003556 return FAIL;
3557
3558 /* A register containing CTRL-R can cause an endless loop. Allow using
3559 * CTRL-C to break the loop. */
3560 line_breakcheck();
3561 if (got_int)
3562 return FAIL;
3563
3564#ifdef FEAT_CLIPBOARD
3565 regname = may_get_selection(regname);
3566#endif
3567
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003568 /* Need to save and restore ccline. And set "textlock" to avoid nasty
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00003569 * things like going to another buffer when evaluating an expression. */
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003570 save_cmdline(&save_ccline);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003571 ++textlock;
Bram Moolenaar8299df92004-07-10 09:47:34 +00003572 i = get_spec_reg(regname, &arg, &allocated, TRUE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00003573 --textlock;
Bram Moolenaar5f2bb9f2005-01-11 21:29:04 +00003574 restore_cmdline(&save_ccline);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003575
3576 if (i)
3577 {
3578 /* Got the value of a special register in "arg". */
3579 if (arg == NULL)
3580 return FAIL;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003581
3582 /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate
3583 * part of the word. */
3584 p = arg;
3585 if (p_is && regname == Ctrl_W)
3586 {
3587 char_u *w;
3588 int len;
3589
3590 /* Locate start of last word in the cmd buffer. */
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003591 for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; )
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003592 {
3593#ifdef FEAT_MBYTE
3594 if (has_mbyte)
3595 {
3596 len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1;
3597 if (!vim_iswordc(mb_ptr2char(w - len)))
3598 break;
3599 w -= len;
3600 }
3601 else
3602#endif
3603 {
3604 if (!vim_iswordc(w[-1]))
3605 break;
3606 --w;
3607 }
3608 }
Bram Moolenaar80ae7b22011-07-07 16:44:37 +02003609 len = (int)((ccline.cmdbuff + ccline.cmdpos) - w);
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00003610 if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0)
3611 p += len;
3612 }
3613
3614 cmdline_paste_str(p, literally);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003615 if (allocated)
3616 vim_free(arg);
3617 return OK;
3618 }
3619
Bram Moolenaar1769d5a2006-10-17 14:25:24 +00003620 return cmdline_paste_reg(regname, literally, remcr);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003621}
3622
3623/*
3624 * Put a string on the command line.
3625 * When "literally" is TRUE, insert literally.
3626 * When "literally" is FALSE, insert as typed, but don't leave the command
3627 * line.
3628 */
3629 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003630cmdline_paste_str(char_u *s, int literally)
Bram Moolenaar8299df92004-07-10 09:47:34 +00003631{
3632 int c, cv;
3633
3634 if (literally)
3635 put_on_cmdline(s, -1, TRUE);
3636 else
3637 while (*s != NUL)
3638 {
3639 cv = *s;
3640 if (cv == Ctrl_V && s[1])
3641 ++s;
3642#ifdef FEAT_MBYTE
3643 if (has_mbyte)
Bram Moolenaar48be32b2008-06-20 10:56:16 +00003644 c = mb_cptr2char_adv(&s);
Bram Moolenaar8299df92004-07-10 09:47:34 +00003645 else
3646#endif
3647 c = *s++;
Bram Moolenaare79abdd2012-06-29 13:44:41 +02003648 if (cv == Ctrl_V || c == ESC || c == Ctrl_C
3649 || c == CAR || c == NL || c == Ctrl_L
Bram Moolenaar8299df92004-07-10 09:47:34 +00003650#ifdef UNIX
3651 || c == intr_char
3652#endif
3653 || (c == Ctrl_BSL && *s == Ctrl_N))
3654 stuffcharReadbuff(Ctrl_V);
3655 stuffcharReadbuff(c);
3656 }
3657}
3658
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659#ifdef FEAT_WILDMENU
3660/*
3661 * Delete characters on the command line, from "from" to the current
3662 * position.
3663 */
3664 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003665cmdline_del(int from)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666{
3667 mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos,
3668 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3669 ccline.cmdlen -= ccline.cmdpos - from;
3670 ccline.cmdpos = from;
3671}
3672#endif
3673
3674/*
Bram Moolenaar89c79b92016-05-05 17:18:41 +02003675 * This function is called when the screen size changes and with incremental
3676 * search and in other situations where the command line may have been
3677 * overwritten.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 */
3679 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003680redrawcmdline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681{
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003682 redrawcmdline_ex(TRUE);
3683}
3684
3685 void
3686redrawcmdline_ex(int do_compute_cmdrow)
3687{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 if (cmd_silent)
3689 return;
3690 need_wait_return = FALSE;
Bram Moolenaar29ae3772017-04-30 19:39:39 +02003691 if (do_compute_cmdrow)
3692 compute_cmdrow();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 redrawcmd();
3694 cursorcmd();
3695}
3696
3697 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003698redrawcmdprompt(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699{
3700 int i;
3701
3702 if (cmd_silent)
3703 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003704 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 msg_putchar(ccline.cmdfirstc);
3706 if (ccline.cmdprompt != NULL)
3707 {
3708 msg_puts_attr(ccline.cmdprompt, ccline.cmdattr);
3709 ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns;
3710 /* do the reverse of set_cmdspos() */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00003711 if (ccline.cmdfirstc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 --ccline.cmdindent;
3713 }
3714 else
3715 for (i = ccline.cmdindent; i > 0; --i)
3716 msg_putchar(' ');
3717}
3718
3719/*
3720 * Redraw what is currently on the command line.
3721 */
3722 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003723redrawcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 if (cmd_silent)
3726 return;
3727
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00003728 /* when 'incsearch' is set there may be no command line while redrawing */
3729 if (ccline.cmdbuff == NULL)
3730 {
3731 windgoto(cmdline_row, 0);
3732 msg_clr_eos();
3733 return;
3734 }
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 msg_start();
3737 redrawcmdprompt();
3738
3739 /* Don't use more prompt, truncate the cmdline if it doesn't fit. */
3740 msg_no_more = TRUE;
3741 draw_cmdline(0, ccline.cmdlen);
3742 msg_clr_eos();
3743 msg_no_more = FALSE;
3744
3745 set_cmdspos_cursor();
Bram Moolenaara92522f2017-07-15 15:21:38 +02003746 if (extra_char != NUL)
Bram Moolenaar6a77d262017-07-16 15:24:01 +02003747 putcmdline(extra_char, extra_char_shift);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748
3749 /*
3750 * An emsg() before may have set msg_scroll. This is used in normal mode,
3751 * in cmdline mode we can reset them now.
3752 */
3753 msg_scroll = FALSE; /* next message overwrites cmdline */
3754
3755 /* Typing ':' at the more prompt may set skip_redraw. We don't want this
3756 * in cmdline mode */
3757 skip_redraw = FALSE;
3758}
3759
3760 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003761compute_cmdrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762{
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003763 if (exmode_active || msg_scrolled != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 cmdline_row = Rows - 1;
3765 else
3766 cmdline_row = W_WINROW(lastwin) + lastwin->w_height
Bram Moolenaare0de17d2017-09-24 16:24:34 +02003767 + lastwin->w_status_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768}
3769
3770 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003771cursorcmd(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772{
3773 if (cmd_silent)
3774 return;
3775
3776#ifdef FEAT_RIGHTLEFT
3777 if (cmdmsg_rl)
3778 {
3779 msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1));
3780 msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1;
3781 if (msg_row <= 0)
3782 msg_row = Rows - 1;
3783 }
3784 else
3785#endif
3786 {
3787 msg_row = cmdline_row + (ccline.cmdspos / (int)Columns);
3788 msg_col = ccline.cmdspos % (int)Columns;
3789 if (msg_row >= Rows)
3790 msg_row = Rows - 1;
3791 }
3792
3793 windgoto(msg_row, msg_col);
3794#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
Bram Moolenaar5c6dbcb2017-08-30 22:00:20 +02003795 if (p_imst == IM_ON_THE_SPOT)
3796 redrawcmd_preedit();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797#endif
3798#ifdef MCH_CURSOR_SHAPE
3799 mch_update_cursor();
3800#endif
3801}
3802
3803 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003804gotocmdline(int clr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805{
3806 msg_start();
3807#ifdef FEAT_RIGHTLEFT
3808 if (cmdmsg_rl)
3809 msg_col = Columns - 1;
3810 else
3811#endif
3812 msg_col = 0; /* always start in column 0 */
3813 if (clr) /* clear the bottom line(s) */
3814 msg_clr_eos(); /* will reset clear_cmdline */
3815 windgoto(cmdline_row, 0);
3816}
3817
3818/*
3819 * Check the word in front of the cursor for an abbreviation.
3820 * Called when the non-id character "c" has been entered.
3821 * When an abbreviation is recognized it is removed from the text with
3822 * backspaces and the replacement string is inserted, followed by "c".
3823 */
3824 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003825ccheck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826{
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003827 int spos = 0;
3828
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 if (p_paste || no_abbr) /* no abbreviations or in paste mode */
3830 return FALSE;
3831
Bram Moolenaar5e3423d2018-05-13 18:36:27 +02003832 /* Do not consider '<,'> be part of the mapping, skip leading whitespace.
3833 * Actually accepts any mark. */
3834 while (VIM_ISWHITE(ccline.cmdbuff[spos]) && spos < ccline.cmdlen)
3835 spos++;
3836 if (ccline.cmdlen - spos > 5
3837 && ccline.cmdbuff[spos] == '\''
3838 && ccline.cmdbuff[spos + 2] == ','
3839 && ccline.cmdbuff[spos + 3] == '\'')
3840 spos += 5;
3841 else
3842 /* check abbreviation from the beginning of the commandline */
3843 spos = 0;
3844
3845 return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, spos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846}
3847
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003848#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3849 static int
3850#ifdef __BORLANDC__
3851_RTLENTRYF
3852#endif
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003853sort_func_compare(const void *s1, const void *s2)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02003854{
3855 char_u *p1 = *(char_u **)s1;
3856 char_u *p2 = *(char_u **)s2;
3857
3858 if (*p1 != '<' && *p2 == '<') return -1;
3859 if (*p1 == '<' && *p2 != '<') return 1;
3860 return STRCMP(p1, p2);
3861}
3862#endif
3863
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864/*
3865 * Return FAIL if this is not an appropriate context in which to do
3866 * completion of anything, return OK if it is (even if there are no matches).
3867 * For the caller, this means that the character is just passed through like a
3868 * normal character (instead of being expanded). This allows :s/^I^D etc.
3869 */
3870 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01003871nextwild(
3872 expand_T *xp,
3873 int type,
3874 int options, /* extra options for ExpandOne() */
3875 int escape) /* if TRUE, escape the returned matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 int i, j;
3878 char_u *p1;
3879 char_u *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 int difflen;
3881 int v;
3882
3883 if (xp->xp_numfiles == -1)
3884 {
3885 set_expand_context(xp);
3886 cmd_showtail = expand_showtail(xp);
3887 }
3888
3889 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
3890 {
3891 beep_flush();
3892 return OK; /* Something illegal on command line */
3893 }
3894 if (xp->xp_context == EXPAND_NOTHING)
3895 {
3896 /* Caller can use the character as a normal char instead */
3897 return FAIL;
3898 }
3899
3900 MSG_PUTS("..."); /* show that we are busy */
3901 out_flush();
3902
3903 i = (int)(xp->xp_pattern - ccline.cmdbuff);
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003904 xp->xp_pattern_len = ccline.cmdpos - i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905
3906 if (type == WILD_NEXT || type == WILD_PREV)
3907 {
3908 /*
3909 * Get next/previous match for a previous expanded pattern.
3910 */
3911 p2 = ExpandOne(xp, NULL, NULL, 0, type);
3912 }
3913 else
3914 {
3915 /*
3916 * Translate string into pattern and expand it.
3917 */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003918 if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len,
3919 xp->xp_context)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 p2 = NULL;
3921 else
3922 {
Bram Moolenaar94950a92010-12-02 16:01:29 +01003923 int use_options = options |
Bram Moolenaarb3479632012-11-28 16:49:58 +01003924 WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT;
3925 if (escape)
3926 use_options |= WILD_ESCAPE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01003927
3928 if (p_wic)
3929 use_options += WILD_ICASE;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003930 p2 = ExpandOne(xp, p1,
3931 vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len),
Bram Moolenaar94950a92010-12-02 16:01:29 +01003932 use_options, type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 vim_free(p1);
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01003934 /* longest match: make sure it is not shorter, happens with :help */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 if (p2 != NULL && type == WILD_LONGEST)
3936 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003937 for (j = 0; j < xp->xp_pattern_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 if (ccline.cmdbuff[i + j] == '*'
3939 || ccline.cmdbuff[i + j] == '?')
3940 break;
3941 if ((int)STRLEN(p2) < j)
Bram Moolenaard23a8232018-02-10 18:45:26 +01003942 VIM_CLEAR(p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
3944 }
3945 }
3946
3947 if (p2 != NULL && !got_int)
3948 {
Bram Moolenaar67b891e2009-09-18 15:25:52 +00003949 difflen = (int)STRLEN(p2) - xp->xp_pattern_len;
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003950 if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaar673b87b2010-08-13 19:12:07 +02003952 v = realloc_cmdbuff(ccline.cmdlen + difflen + 4);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 xp->xp_pattern = ccline.cmdbuff + i;
3954 }
3955 else
3956 v = OK;
3957 if (v == OK)
3958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003959 mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen],
3960 &ccline.cmdbuff[ccline.cmdpos],
3961 (size_t)(ccline.cmdlen - ccline.cmdpos + 1));
3962 mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 ccline.cmdlen += difflen;
3964 ccline.cmdpos += difflen;
3965 }
3966 }
3967 vim_free(p2);
3968
3969 redrawcmd();
Bram Moolenaar009b2592004-10-24 19:18:58 +00003970 cursorcmd();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972 /* When expanding a ":map" command and no matches are found, assume that
3973 * the key is supposed to be inserted literally */
3974 if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL)
3975 return FAIL;
3976
3977 if (xp->xp_numfiles <= 0 && p2 == NULL)
3978 beep_flush();
3979 else if (xp->xp_numfiles == 1)
3980 /* free expanded pattern */
3981 (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE);
3982
3983 return OK;
3984}
3985
3986/*
3987 * Do wildcard expansion on the string 'str'.
3988 * Chars that should not be expanded must be preceded with a backslash.
Bram Moolenaarf9821062008-06-20 16:31:07 +00003989 * Return a pointer to allocated memory containing the new string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 * Return NULL for failure.
3991 *
Bram Moolenaarecf4de52007-09-30 20:11:26 +00003992 * "orig" is the originally expanded string, copied to allocated memory. It
3993 * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or
3994 * WILD_PREV "orig" should be NULL.
3995 *
Bram Moolenaarfc1421e2006-04-20 22:17:20 +00003996 * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode"
3997 * is WILD_EXPAND_FREE or WILD_ALL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 *
3999 * mode = WILD_FREE: just free previously expanded matches
4000 * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches
4001 * mode = WILD_EXPAND_KEEP: normal expansion, keep matches
4002 * mode = WILD_NEXT: use next match in multiple match, wrap to first
4003 * mode = WILD_PREV: use previous match in multiple match, wrap to first
4004 * mode = WILD_ALL: return all matches concatenated
4005 * mode = WILD_LONGEST: return longest matched part
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004006 * mode = WILD_ALL_KEEP: get all matches, keep matches
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 *
4008 * options = WILD_LIST_NOTFOUND: list entries without a match
4009 * options = WILD_HOME_REPLACE: do home_replace() for buffer names
4010 * options = WILD_USE_NL: Use '\n' for WILD_ALL
4011 * options = WILD_NO_BEEP: Don't beep for multiple matches
4012 * options = WILD_ADD_SLASH: add a slash after directory names
4013 * options = WILD_KEEP_ALL: don't remove 'wildignore' entries
4014 * options = WILD_SILENT: don't print warning messages
4015 * options = WILD_ESCAPE: put backslash before special chars
Bram Moolenaar94950a92010-12-02 16:01:29 +01004016 * options = WILD_ICASE: ignore case for files
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 *
4018 * The variables xp->xp_context and xp->xp_backslash must have been set!
4019 */
4020 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004021ExpandOne(
4022 expand_T *xp,
4023 char_u *str,
4024 char_u *orig, /* allocated copy of original of expanded string */
4025 int options,
4026 int mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 char_u *ss = NULL;
4029 static int findex;
4030 static char_u *orig_save = NULL; /* kept value of orig */
Bram Moolenaar96426642007-10-30 16:37:15 +00004031 int orig_saved = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int i;
4033 long_u len;
4034 int non_suf_match; /* number without matching suffix */
4035
4036 /*
4037 * first handle the case of using an old match
4038 */
4039 if (mode == WILD_NEXT || mode == WILD_PREV)
4040 {
4041 if (xp->xp_numfiles > 0)
4042 {
4043 if (mode == WILD_PREV)
4044 {
4045 if (findex == -1)
4046 findex = xp->xp_numfiles;
4047 --findex;
4048 }
4049 else /* mode == WILD_NEXT */
4050 ++findex;
4051
4052 /*
4053 * When wrapping around, return the original string, set findex to
4054 * -1.
4055 */
4056 if (findex < 0)
4057 {
4058 if (orig_save == NULL)
4059 findex = xp->xp_numfiles - 1;
4060 else
4061 findex = -1;
4062 }
4063 if (findex >= xp->xp_numfiles)
4064 {
4065 if (orig_save == NULL)
4066 findex = 0;
4067 else
4068 findex = -1;
4069 }
4070#ifdef FEAT_WILDMENU
4071 if (p_wmnu)
4072 win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files,
4073 findex, cmd_showtail);
4074#endif
4075 if (findex == -1)
4076 return vim_strsave(orig_save);
4077 return vim_strsave(xp->xp_files[findex]);
4078 }
4079 else
4080 return NULL;
4081 }
4082
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004083 /* free old names */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST)
4085 {
4086 FreeWild(xp->xp_numfiles, xp->xp_files);
4087 xp->xp_numfiles = -1;
Bram Moolenaard23a8232018-02-10 18:45:26 +01004088 VIM_CLEAR(orig_save);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 }
4090 findex = 0;
4091
4092 if (mode == WILD_FREE) /* only release file name */
4093 return NULL;
4094
4095 if (xp->xp_numfiles == -1)
4096 {
4097 vim_free(orig_save);
4098 orig_save = orig;
Bram Moolenaar96426642007-10-30 16:37:15 +00004099 orig_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
4101 /*
4102 * Do the expansion.
4103 */
4104 if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files,
4105 options) == FAIL)
4106 {
4107#ifdef FNAME_ILLEGAL
4108 /* Illegal file name has been silently skipped. But when there
4109 * are wildcards, the real problem is that there was no match,
4110 * causing the pattern to be added, which has illegal characters.
4111 */
4112 if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND))
4113 EMSG2(_(e_nomatch2), str);
4114#endif
4115 }
4116 else if (xp->xp_numfiles == 0)
4117 {
4118 if (!(options & WILD_SILENT))
4119 EMSG2(_(e_nomatch2), str);
4120 }
4121 else
4122 {
4123 /* Escape the matches for use on the command line. */
4124 ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options);
4125
4126 /*
4127 * Check for matching suffixes in file names.
4128 */
Bram Moolenaar146e9c32012-03-07 19:18:23 +01004129 if (mode != WILD_ALL && mode != WILD_ALL_KEEP
4130 && mode != WILD_LONGEST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 {
4132 if (xp->xp_numfiles)
4133 non_suf_match = xp->xp_numfiles;
4134 else
4135 non_suf_match = 1;
4136 if ((xp->xp_context == EXPAND_FILES
4137 || xp->xp_context == EXPAND_DIRECTORIES)
4138 && xp->xp_numfiles > 1)
4139 {
4140 /*
4141 * More than one match; check suffix.
4142 * The files will have been sorted on matching suffix in
4143 * expand_wildcards, only need to check the first two.
4144 */
4145 non_suf_match = 0;
4146 for (i = 0; i < 2; ++i)
4147 if (match_suffix(xp->xp_files[i]))
4148 ++non_suf_match;
4149 }
4150 if (non_suf_match != 1)
4151 {
4152 /* Can we ever get here unless it's while expanding
4153 * interactively? If not, we can get rid of this all
4154 * together. Don't really want to wait for this message
4155 * (and possibly have to hit return to continue!).
4156 */
4157 if (!(options & WILD_SILENT))
4158 EMSG(_(e_toomany));
4159 else if (!(options & WILD_NO_BEEP))
4160 beep_flush();
4161 }
4162 if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE))
4163 ss = vim_strsave(xp->xp_files[0]);
4164 }
4165 }
4166 }
4167
4168 /* Find longest common part */
4169 if (mode == WILD_LONGEST && xp->xp_numfiles > 0)
4170 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004171 int mb_len = 1;
4172 int c0, ci;
4173
4174 for (len = 0; xp->xp_files[0][len]; len += mb_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004176#ifdef FEAT_MBYTE
4177 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004179 mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]);
4180 c0 =(* mb_ptr2char)(&xp->xp_files[0][len]);
4181 }
4182 else
4183#endif
Bram Moolenaare4eda3b2015-11-21 16:28:50 +01004184 c0 = xp->xp_files[0][len];
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004185 for (i = 1; i < xp->xp_numfiles; ++i)
4186 {
4187#ifdef FEAT_MBYTE
4188 if (has_mbyte)
4189 ci =(* mb_ptr2char)(&xp->xp_files[i][len]);
4190 else
4191#endif
4192 ci = xp->xp_files[i][len];
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004193 if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 || xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004195 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004196 || xp->xp_context == EXPAND_BUFFERS))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004198 if (MB_TOLOWER(c0) != MB_TOLOWER(ci))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 break;
4200 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004201 else if (c0 != ci)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 break;
4203 }
4204 if (i < xp->xp_numfiles)
4205 {
4206 if (!(options & WILD_NO_BEEP))
Bram Moolenaar165bc692015-07-21 17:53:25 +02004207 vim_beep(BO_WILD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 break;
4209 }
4210 }
Bram Moolenaar4f8fa162015-11-19 19:00:05 +01004211
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 ss = alloc((unsigned)len + 1);
4213 if (ss)
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004214 vim_strncpy(ss, xp->xp_files[0], (size_t)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 findex = -1; /* next p_wc gets first one */
4216 }
4217
4218 /* Concatenate all matching names */
4219 if (mode == WILD_ALL && xp->xp_numfiles > 0)
4220 {
4221 len = 0;
4222 for (i = 0; i < xp->xp_numfiles; ++i)
4223 len += (long_u)STRLEN(xp->xp_files[i]) + 1;
4224 ss = lalloc(len, TRUE);
4225 if (ss != NULL)
4226 {
4227 *ss = NUL;
4228 for (i = 0; i < xp->xp_numfiles; ++i)
4229 {
4230 STRCAT(ss, xp->xp_files[i]);
4231 if (i != xp->xp_numfiles - 1)
4232 STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " ");
4233 }
4234 }
4235 }
4236
4237 if (mode == WILD_EXPAND_FREE || mode == WILD_ALL)
4238 ExpandCleanup(xp);
4239
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004240 /* Free "orig" if it wasn't stored in "orig_save". */
Bram Moolenaar96426642007-10-30 16:37:15 +00004241 if (!orig_saved)
Bram Moolenaarecf4de52007-09-30 20:11:26 +00004242 vim_free(orig);
4243
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 return ss;
4245}
4246
4247/*
4248 * Prepare an expand structure for use.
4249 */
4250 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004251ExpandInit(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252{
Bram Moolenaard6e7cc62008-09-14 12:42:29 +00004253 xp->xp_pattern = NULL;
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004254 xp->xp_pattern_len = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004256#ifndef BACKSLASH_IN_FILENAME
4257 xp->xp_shell = FALSE;
4258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 xp->xp_numfiles = -1;
4260 xp->xp_files = NULL;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00004261#if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL)
4262 xp->xp_arg = NULL;
4263#endif
Bram Moolenaarb7515462013-06-29 12:58:33 +02004264 xp->xp_line = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265}
4266
4267/*
4268 * Cleanup an expand structure after use.
4269 */
4270 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004271ExpandCleanup(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272{
4273 if (xp->xp_numfiles >= 0)
4274 {
4275 FreeWild(xp->xp_numfiles, xp->xp_files);
4276 xp->xp_numfiles = -1;
4277 }
4278}
4279
4280 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004281ExpandEscape(
4282 expand_T *xp,
4283 char_u *str,
4284 int numfiles,
4285 char_u **files,
4286 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287{
4288 int i;
4289 char_u *p;
4290
4291 /*
4292 * May change home directory back to "~"
4293 */
4294 if (options & WILD_HOME_REPLACE)
4295 tilde_replace(str, numfiles, files);
4296
4297 if (options & WILD_ESCAPE)
4298 {
4299 if (xp->xp_context == EXPAND_FILES
Bram Moolenaarcca92ec2011-04-28 17:21:53 +02004300 || xp->xp_context == EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004301 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 || xp->xp_context == EXPAND_BUFFERS
4303 || xp->xp_context == EXPAND_DIRECTORIES)
4304 {
4305 /*
4306 * Insert a backslash into a file name before a space, \, %, #
4307 * and wildmatch characters, except '~'.
4308 */
4309 for (i = 0; i < numfiles; ++i)
4310 {
4311 /* for ":set path=" we need to escape spaces twice */
4312 if (xp->xp_backslash == XP_BS_THREE)
4313 {
4314 p = vim_strsave_escaped(files[i], (char_u *)" ");
4315 if (p != NULL)
4316 {
4317 vim_free(files[i]);
4318 files[i] = p;
Bram Moolenaar4ea8fe12006-03-09 22:32:39 +00004319#if defined(BACKSLASH_IN_FILENAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 p = vim_strsave_escaped(files[i], (char_u *)" ");
4321 if (p != NULL)
4322 {
4323 vim_free(files[i]);
4324 files[i] = p;
4325 }
4326#endif
4327 }
4328 }
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004329#ifdef BACKSLASH_IN_FILENAME
4330 p = vim_strsave_fnameescape(files[i], FALSE);
4331#else
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004332 p = vim_strsave_fnameescape(files[i], xp->xp_shell);
Bram Moolenaar0356c8c2008-05-28 20:02:48 +00004333#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 if (p != NULL)
4335 {
4336 vim_free(files[i]);
4337 files[i] = p;
4338 }
4339
4340 /* If 'str' starts with "\~", replace "~" at start of
4341 * files[i] with "\~". */
4342 if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~')
Bram Moolenaar45360022005-07-21 21:08:21 +00004343 escape_fname(&files[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 xp->xp_backslash = XP_BS_NONE;
Bram Moolenaar45360022005-07-21 21:08:21 +00004346
4347 /* If the first file starts with a '+' escape it. Otherwise it
4348 * could be seen as "+cmd". */
4349 if (*files[0] == '+')
4350 escape_fname(&files[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 else if (xp->xp_context == EXPAND_TAGS)
4353 {
4354 /*
4355 * Insert a backslash before characters in a tag name that
4356 * would terminate the ":tag" command.
4357 */
4358 for (i = 0; i < numfiles; ++i)
4359 {
4360 p = vim_strsave_escaped(files[i], (char_u *)"\\|\"");
4361 if (p != NULL)
4362 {
4363 vim_free(files[i]);
4364 files[i] = p;
4365 }
4366 }
4367 }
4368 }
4369}
4370
4371/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004372 * Escape special characters in "fname" for when used as a file name argument
4373 * after a Vim command, or, when "shell" is non-zero, a shell command.
4374 * Returns the result in allocated memory.
4375 */
4376 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004377vim_strsave_fnameescape(char_u *fname, int shell)
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004378{
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004379 char_u *p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004380#ifdef BACKSLASH_IN_FILENAME
4381 char_u buf[20];
4382 int j = 0;
4383
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004384 /* Don't escape '[', '{' and '!' if they are in 'isfname'. */
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004385 for (p = PATH_ESC_CHARS; *p != NUL; ++p)
Bram Moolenaar8f5610d2013-11-12 05:28:26 +01004386 if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p))
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004387 buf[j++] = *p;
4388 buf[j] = NUL;
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004389 p = vim_strsave_escaped(fname, buf);
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004390#else
Bram Moolenaar7693ec62008-07-24 18:29:37 +00004391 p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS);
4392 if (shell && csh_like_shell() && p != NULL)
4393 {
4394 char_u *s;
4395
4396 /* For csh and similar shells need to put two backslashes before '!'.
4397 * One is taken by Vim, one by the shell. */
4398 s = vim_strsave_escaped(p, (char_u *)"!");
4399 vim_free(p);
4400 p = s;
4401 }
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004402#endif
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004403
4404 /* '>' and '+' are special at the start of some commands, e.g. ":edit" and
4405 * ":write". "cd -" has a special meaning. */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02004406 if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL)))
Bram Moolenaar1b24e4b2008-08-08 10:59:17 +00004407 escape_fname(&p);
4408
4409 return p;
Bram Moolenaaraebaf892008-05-28 14:49:58 +00004410}
4411
4412/*
Bram Moolenaar45360022005-07-21 21:08:21 +00004413 * Put a backslash before the file name in "pp", which is in allocated memory.
4414 */
4415 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004416escape_fname(char_u **pp)
Bram Moolenaar45360022005-07-21 21:08:21 +00004417{
4418 char_u *p;
4419
4420 p = alloc((unsigned)(STRLEN(*pp) + 2));
4421 if (p != NULL)
4422 {
4423 p[0] = '\\';
4424 STRCPY(p + 1, *pp);
4425 vim_free(*pp);
4426 *pp = p;
4427 }
4428}
4429
4430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 * For each file name in files[num_files]:
4432 * If 'orig_pat' starts with "~/", replace the home directory with "~".
4433 */
4434 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004435tilde_replace(
4436 char_u *orig_pat,
4437 int num_files,
4438 char_u **files)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439{
4440 int i;
4441 char_u *p;
4442
4443 if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1]))
4444 {
4445 for (i = 0; i < num_files; ++i)
4446 {
4447 p = home_replace_save(NULL, files[i]);
4448 if (p != NULL)
4449 {
4450 vim_free(files[i]);
4451 files[i] = p;
4452 }
4453 }
4454 }
4455}
4456
4457/*
4458 * Show all matches for completion on the command line.
4459 * Returns EXPAND_NOTHING when the character that triggered expansion should
4460 * be inserted like a normal character.
4461 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004463showmatches(expand_T *xp, int wildmenu UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464{
4465#define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m])
4466 int num_files;
4467 char_u **files_found;
4468 int i, j, k;
4469 int maxlen;
4470 int lines;
4471 int columns;
4472 char_u *p;
4473 int lastlen;
4474 int attr;
4475 int showtail;
4476
4477 if (xp->xp_numfiles == -1)
4478 {
4479 set_expand_context(xp);
4480 i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos,
4481 &num_files, &files_found);
4482 showtail = expand_showtail(xp);
4483 if (i != EXPAND_OK)
4484 return i;
4485
4486 }
4487 else
4488 {
4489 num_files = xp->xp_numfiles;
4490 files_found = xp->xp_files;
4491 showtail = cmd_showtail;
4492 }
4493
4494#ifdef FEAT_WILDMENU
4495 if (!wildmenu)
4496 {
4497#endif
4498 msg_didany = FALSE; /* lines_left will be set */
4499 msg_start(); /* prepare for paging */
4500 msg_putchar('\n');
4501 out_flush();
4502 cmdline_row = msg_row;
4503 msg_didany = FALSE; /* lines_left will be set again */
4504 msg_start(); /* prepare for paging */
4505#ifdef FEAT_WILDMENU
4506 }
4507#endif
4508
4509 if (got_int)
4510 got_int = FALSE; /* only int. the completion, not the cmd line */
4511#ifdef FEAT_WILDMENU
4512 else if (wildmenu)
Bram Moolenaaref8eb082017-03-30 22:04:55 +02004513 win_redr_status_matches(xp, num_files, files_found, -1, showtail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514#endif
4515 else
4516 {
4517 /* find the length of the longest file name */
4518 maxlen = 0;
4519 for (i = 0; i < num_files; ++i)
4520 {
4521 if (!showtail && (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004522 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 || xp->xp_context == EXPAND_BUFFERS))
4524 {
4525 home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE);
4526 j = vim_strsize(NameBuff);
4527 }
4528 else
4529 j = vim_strsize(L_SHOWFILE(i));
4530 if (j > maxlen)
4531 maxlen = j;
4532 }
4533
4534 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4535 lines = num_files;
4536 else
4537 {
4538 /* compute the number of columns and lines for the listing */
4539 maxlen += 2; /* two spaces between file names */
4540 columns = ((int)Columns + 2) / maxlen;
4541 if (columns < 1)
4542 columns = 1;
4543 lines = (num_files + columns - 1) / columns;
4544 }
4545
Bram Moolenaar8820b482017-03-16 17:23:31 +01004546 attr = HL_ATTR(HLF_D); /* find out highlighting for directories */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547
4548 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4549 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004550 MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 msg_clr_eos();
4552 msg_advance(maxlen - 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004553 MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555
4556 /* list the files line by line */
4557 for (i = 0; i < lines; ++i)
4558 {
4559 lastlen = 999;
4560 for (k = i; k < num_files; k += lines)
4561 {
4562 if (xp->xp_context == EXPAND_TAGS_LISTFILES)
4563 {
Bram Moolenaar8820b482017-03-16 17:23:31 +01004564 msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 p = files_found[k] + STRLEN(files_found[k]) + 1;
4566 msg_advance(maxlen + 1);
4567 msg_puts(p);
4568 msg_advance(maxlen + 3);
Bram Moolenaar8820b482017-03-16 17:23:31 +01004569 msg_puts_long_attr(p + 2, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571 }
4572 for (j = maxlen - lastlen; --j >= 0; )
4573 msg_putchar(' ');
4574 if (xp->xp_context == EXPAND_FILES
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004575 || xp->xp_context == EXPAND_SHELLCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 || xp->xp_context == EXPAND_BUFFERS)
4577 {
Bram Moolenaarb91e59b2010-03-17 19:13:27 +01004578 /* highlight directories */
Bram Moolenaar63fa5262010-03-23 18:06:52 +01004579 if (xp->xp_numfiles != -1)
4580 {
4581 char_u *halved_slash;
4582 char_u *exp_path;
4583
4584 /* Expansion was done before and special characters
4585 * were escaped, need to halve backslashes. Also
4586 * $HOME has been replaced with ~/. */
4587 exp_path = expand_env_save_opt(files_found[k], TRUE);
4588 halved_slash = backslash_halve_save(
4589 exp_path != NULL ? exp_path : files_found[k]);
4590 j = mch_isdir(halved_slash != NULL ? halved_slash
4591 : files_found[k]);
4592 vim_free(exp_path);
4593 vim_free(halved_slash);
4594 }
4595 else
4596 /* Expansion was done here, file names are literal. */
4597 j = mch_isdir(files_found[k]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 if (showtail)
4599 p = L_SHOWFILE(k);
4600 else
4601 {
4602 home_replace(NULL, files_found[k], NameBuff, MAXPATHL,
4603 TRUE);
4604 p = NameBuff;
4605 }
4606 }
4607 else
4608 {
4609 j = FALSE;
4610 p = L_SHOWFILE(k);
4611 }
4612 lastlen = msg_outtrans_attr(p, j ? attr : 0);
4613 }
4614 if (msg_col > 0) /* when not wrapped around */
4615 {
4616 msg_clr_eos();
4617 msg_putchar('\n');
4618 }
4619 out_flush(); /* show one line at a time */
4620 if (got_int)
4621 {
4622 got_int = FALSE;
4623 break;
4624 }
4625 }
4626
4627 /*
4628 * we redraw the command below the lines that we have just listed
4629 * This is a bit tricky, but it saves a lot of screen updating.
4630 */
4631 cmdline_row = msg_row; /* will put it back later */
4632 }
4633
4634 if (xp->xp_numfiles == -1)
4635 FreeWild(num_files, files_found);
4636
4637 return EXPAND_OK;
4638}
4639
4640/*
4641 * Private gettail for showmatches() (and win_redr_status_matches()):
4642 * Find tail of file name path, but ignore trailing "/".
4643 */
4644 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004645sm_gettail(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646{
4647 char_u *p;
4648 char_u *t = s;
4649 int had_sep = FALSE;
4650
4651 for (p = s; *p != NUL; )
4652 {
4653 if (vim_ispathsep(*p)
4654#ifdef BACKSLASH_IN_FILENAME
4655 && !rem_backslash(p)
4656#endif
4657 )
4658 had_sep = TRUE;
4659 else if (had_sep)
4660 {
4661 t = p;
4662 had_sep = FALSE;
4663 }
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004664 MB_PTR_ADV(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 }
4666 return t;
4667}
4668
4669/*
4670 * Return TRUE if we only need to show the tail of completion matches.
4671 * When not completing file names or there is a wildcard in the path FALSE is
4672 * returned.
4673 */
4674 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004675expand_showtail(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
4677 char_u *s;
4678 char_u *end;
4679
4680 /* When not completing file names a "/" may mean something different. */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004681 if (xp->xp_context != EXPAND_FILES
4682 && xp->xp_context != EXPAND_SHELLCMD
4683 && xp->xp_context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 return FALSE;
4685
4686 end = gettail(xp->xp_pattern);
4687 if (end == xp->xp_pattern) /* there is no path separator */
4688 return FALSE;
4689
4690 for (s = xp->xp_pattern; s < end; s++)
4691 {
4692 /* Skip escaped wildcards. Only when the backslash is not a path
4693 * separator, on DOS the '*' "path\*\file" must not be skipped. */
4694 if (rem_backslash(s))
4695 ++s;
4696 else if (vim_strchr((char_u *)"*?[", *s) != NULL)
4697 return FALSE;
4698 }
4699 return TRUE;
4700}
4701
4702/*
4703 * Prepare a string for expansion.
4704 * When expanding file names: The string will be used with expand_wildcards().
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01004705 * Copy "fname[len]" into allocated memory and add a '*' at the end.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 * When expanding other names: The string will be used with regcomp(). Copy
4707 * the name into allocated memory and prepend "^".
4708 */
4709 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004710addstar(
4711 char_u *fname,
4712 int len,
4713 int context) /* EXPAND_FILES etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 char_u *retval;
4716 int i, j;
4717 int new_len;
4718 char_u *tail;
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004719 int ends_in_star;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004721 if (context != EXPAND_FILES
Bram Moolenaarcc448b32010-07-14 16:52:17 +02004722 && context != EXPAND_FILES_IN_PATH
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004723 && context != EXPAND_SHELLCMD
4724 && context != EXPAND_DIRECTORIES)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 /*
4727 * Matching will be done internally (on something other than files).
4728 * So we convert the file-matching-type wildcards into our kind for
4729 * use with vim_regcomp(). First work out how long it will be:
4730 */
4731
4732 /* For help tags the translation is done in find_help_tags().
4733 * For a tag pattern starting with "/" no translation is needed. */
4734 if (context == EXPAND_HELP
4735 || context == EXPAND_COLORS
4736 || context == EXPAND_COMPILER
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02004737 || context == EXPAND_OWNSYNTAX
Bram Moolenaar883f5d02010-06-21 06:24:34 +02004738 || context == EXPAND_FILETYPE
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01004739 || context == EXPAND_PACKADD
Bram Moolenaarba47b512017-01-24 21:18:19 +01004740 || ((context == EXPAND_TAGS_LISTFILES
4741 || context == EXPAND_TAGS)
4742 && fname[0] == '/'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 retval = vim_strnsave(fname, len);
4744 else
4745 {
4746 new_len = len + 2; /* +2 for '^' at start, NUL at end */
4747 for (i = 0; i < len; i++)
4748 {
4749 if (fname[i] == '*' || fname[i] == '~')
4750 new_len++; /* '*' needs to be replaced by ".*"
4751 '~' needs to be replaced by "\~" */
4752
4753 /* Buffer names are like file names. "." should be literal */
4754 if (context == EXPAND_BUFFERS && fname[i] == '.')
4755 new_len++; /* "." becomes "\." */
4756
4757 /* Custom expansion takes care of special things, match
4758 * backslashes literally (perhaps also for other types?) */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004759 if ((context == EXPAND_USER_DEFINED
4760 || context == EXPAND_USER_LIST) && fname[i] == '\\')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 new_len++; /* '\' becomes "\\" */
4762 }
4763 retval = alloc(new_len);
4764 if (retval != NULL)
4765 {
4766 retval[0] = '^';
4767 j = 1;
4768 for (i = 0; i < len; i++, j++)
4769 {
4770 /* Skip backslash. But why? At least keep it for custom
4771 * expansion. */
4772 if (context != EXPAND_USER_DEFINED
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004773 && context != EXPAND_USER_LIST
4774 && fname[i] == '\\'
4775 && ++i == len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 break;
4777
4778 switch (fname[i])
4779 {
4780 case '*': retval[j++] = '.';
4781 break;
4782 case '~': retval[j++] = '\\';
4783 break;
4784 case '?': retval[j] = '.';
4785 continue;
4786 case '.': if (context == EXPAND_BUFFERS)
4787 retval[j++] = '\\';
4788 break;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00004789 case '\\': if (context == EXPAND_USER_DEFINED
4790 || context == EXPAND_USER_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 retval[j++] = '\\';
4792 break;
4793 }
4794 retval[j] = fname[i];
4795 }
4796 retval[j] = NUL;
4797 }
4798 }
4799 }
4800 else
4801 {
4802 retval = alloc(len + 4);
4803 if (retval != NULL)
4804 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004805 vim_strncpy(retval, fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806
4807 /*
Bram Moolenaarc6249bb2006-04-15 20:25:09 +00004808 * Don't add a star to *, ~, ~user, $var or `cmd`.
4809 * * would become **, which walks the whole tree.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 * ~ would be at the start of the file name, but not the tail.
4811 * $ could be anywhere in the tail.
4812 * ` could be anywhere in the file name.
Bram Moolenaar066b6222008-01-04 14:17:47 +00004813 * When the name ends in '$' don't add a star, remove the '$'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 */
4815 tail = gettail(retval);
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004816 ends_in_star = (len > 0 && retval[len - 1] == '*');
4817#ifndef BACKSLASH_IN_FILENAME
4818 for (i = len - 2; i >= 0; --i)
4819 {
4820 if (retval[i] != '\\')
4821 break;
4822 ends_in_star = !ends_in_star;
4823 }
4824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 if ((*retval != '~' || tail != retval)
Bram Moolenaar8cd213c2010-06-01 21:57:09 +02004826 && !ends_in_star
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 && vim_strchr(tail, '$') == NULL
4828 && vim_strchr(retval, '`') == NULL)
4829 retval[len++] = '*';
Bram Moolenaar066b6222008-01-04 14:17:47 +00004830 else if (len > 0 && retval[len - 1] == '$')
4831 --len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 retval[len] = NUL;
4833 }
4834 }
4835 return retval;
4836}
4837
4838/*
4839 * Must parse the command line so far to work out what context we are in.
4840 * Completion can then be done based on that context.
4841 * This routine sets the variables:
4842 * xp->xp_pattern The start of the pattern to be expanded within
4843 * the command line (ends at the cursor).
4844 * xp->xp_context The type of thing to expand. Will be one of:
4845 *
4846 * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on
4847 * the command line, like an unknown command. Caller
4848 * should beep.
4849 * EXPAND_NOTHING Unrecognised context for completion, use char like
4850 * a normal char, rather than for completion. eg
4851 * :s/^I/
4852 * EXPAND_COMMANDS Cursor is still touching the command, so complete
4853 * it.
4854 * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands.
4855 * EXPAND_FILES After command with XFILE set, or after setting
4856 * with P_EXPAND set. eg :e ^I, :w>>^I
4857 * EXPAND_DIRECTORIES In some cases this is used instead of the latter
4858 * when we know only directories are of interest. eg
4859 * :set dir=^I
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004860 * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 * EXPAND_SETTINGS Complete variable names. eg :set d^I
4862 * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I
4863 * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I
4864 * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect
4865 * EXPAND_HELP Complete tags from the file 'helpfile'/tags
4866 * EXPAND_EVENTS Complete event names
4867 * EXPAND_SYNTAX Complete :syntax command arguments
4868 * EXPAND_HIGHLIGHT Complete highlight (syntax) group names
4869 * EXPAND_AUGROUP Complete autocommand group names
4870 * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I
4871 * EXPAND_MAPPINGS Complete mapping and abbreviation names,
4872 * eg :unmap a^I , :cunab x^I
4873 * EXPAND_FUNCTIONS Complete internal or user defined function names,
4874 * eg :call sub^I
4875 * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I
4876 * EXPAND_EXPRESSION Complete internal or user defined function/variable
4877 * names in expressions, eg :while s^I
4878 * EXPAND_ENV_VARS Complete environment variable names
Bram Moolenaar24305862012-08-15 14:05:05 +02004879 * EXPAND_USER Complete user names
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 */
4881 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004882set_expand_context(expand_T *xp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004884 /* only expansion for ':', '>' and '=' command-lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 if (ccline.cmdfirstc != ':'
4886#ifdef FEAT_EVAL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004887 && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '='
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004888 && !ccline.input_fn
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889#endif
4890 )
4891 {
4892 xp->xp_context = EXPAND_NOTHING;
4893 return;
4894 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004895 set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896}
4897
4898 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004899set_cmd_context(
4900 expand_T *xp,
4901 char_u *str, /* start of command line */
4902 int len, /* length of command line (excl. NUL) */
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004903 int col, /* position of cursor */
4904 int use_ccline UNUSED) /* use ccline for info */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905{
4906 int old_char = NUL;
4907 char_u *nextcomm;
4908
4909 /*
4910 * Avoid a UMR warning from Purify, only save the character if it has been
4911 * written before.
4912 */
4913 if (col < len)
4914 old_char = str[col];
4915 str[col] = NUL;
4916 nextcomm = str;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004917
4918#ifdef FEAT_EVAL
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004919 if (use_ccline && ccline.cmdfirstc == '=')
Bram Moolenaar4f688582007-07-24 12:34:30 +00004920 {
4921# ifdef FEAT_CMDL_COMPL
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004922 /* pass CMD_SIZE because there is no real command */
4923 set_context_for_expression(xp, str, CMD_SIZE);
Bram Moolenaar4f688582007-07-24 12:34:30 +00004924# endif
4925 }
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02004926 else if (use_ccline && ccline.input_fn)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004927 {
4928 xp->xp_context = ccline.xp_context;
4929 xp->xp_pattern = ccline.cmdbuff;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004930# if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004931 xp->xp_arg = ccline.xp_arg;
Bram Moolenaar4f688582007-07-24 12:34:30 +00004932# endif
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00004933 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004934 else
4935#endif
4936 while (nextcomm != NULL)
4937 nextcomm = set_one_cmd_context(xp, nextcomm);
4938
Bram Moolenaara4c8dcb2013-06-30 12:21:24 +02004939 /* Store the string here so that call_user_expand_func() can get to them
4940 * easily. */
4941 xp->xp_line = str;
4942 xp->xp_col = col;
4943
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 str[col] = old_char;
4945}
4946
4947/*
4948 * Expand the command line "str" from context "xp".
4949 * "xp" must have been set by set_cmd_context().
4950 * xp->xp_pattern points into "str", to where the text that is to be expanded
4951 * starts.
4952 * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the
4953 * cursor.
4954 * Returns EXPAND_NOTHING when there is nothing to expand, might insert the
4955 * key that triggered expansion literally.
4956 * Returns EXPAND_OK otherwise.
4957 */
4958 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01004959expand_cmdline(
4960 expand_T *xp,
4961 char_u *str, /* start of command line */
4962 int col, /* position of cursor */
4963 int *matchcount, /* return: nr of matches */
4964 char_u ***matches) /* return: array of pointers to matches */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 char_u *file_str = NULL;
Bram Moolenaar94950a92010-12-02 16:01:29 +01004967 int options = WILD_ADD_SLASH|WILD_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 if (xp->xp_context == EXPAND_UNSUCCESSFUL)
4970 {
4971 beep_flush();
4972 return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */
4973 }
4974 if (xp->xp_context == EXPAND_NOTHING)
4975 {
4976 /* Caller can use the character as a normal char instead */
4977 return EXPAND_NOTHING;
4978 }
4979
4980 /* add star to file name, or convert to regexp if not exp. files. */
Bram Moolenaar67b891e2009-09-18 15:25:52 +00004981 xp->xp_pattern_len = (int)(str + col - xp->xp_pattern);
4982 file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 if (file_str == NULL)
4984 return EXPAND_UNSUCCESSFUL;
4985
Bram Moolenaar94950a92010-12-02 16:01:29 +01004986 if (p_wic)
4987 options += WILD_ICASE;
4988
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 /* find all files that match the description */
Bram Moolenaar94950a92010-12-02 16:01:29 +01004990 if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 {
4992 *matchcount = 0;
4993 *matches = NULL;
4994 }
4995 vim_free(file_str);
4996
4997 return EXPAND_OK;
4998}
4999
5000#ifdef FEAT_MULTI_LANG
5001/*
Bram Moolenaar61264d92016-03-28 19:59:02 +02005002 * Cleanup matches for help tags:
5003 * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first
5004 * tag matches it. Otherwise remove "@en" if "en" is the only language.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 */
Bram Moolenaard25c16e2016-01-29 22:13:30 +01005006static void cleanup_help_tags(int num_file, char_u **file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007
5008 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005009cleanup_help_tags(int num_file, char_u **file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010{
5011 int i, j;
5012 int len;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005013 char_u buf[4];
5014 char_u *p = buf;
5015
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005016 if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n'))
Bram Moolenaar61264d92016-03-28 19:59:02 +02005017 {
5018 *p++ = '@';
5019 *p++ = p_hlg[0];
5020 *p++ = p_hlg[1];
5021 }
5022 *p = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023
5024 for (i = 0; i < num_file; ++i)
5025 {
5026 len = (int)STRLEN(file[i]) - 3;
Bram Moolenaar61264d92016-03-28 19:59:02 +02005027 if (len <= 0)
5028 continue;
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005029 if (STRCMP(file[i] + len, "@en") == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
5031 /* Sorting on priority means the same item in another language may
5032 * be anywhere. Search all items for a match up to the "@en". */
5033 for (j = 0; j < num_file; ++j)
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005034 if (j != i && (int)STRLEN(file[j]) == len + 3
5035 && STRNCMP(file[i], file[j], len + 1) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 break;
5037 if (j == num_file)
Bram Moolenaar89c79b92016-05-05 17:18:41 +02005038 /* item only exists with @en, remove it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 file[i][len] = NUL;
5040 }
5041 }
Bram Moolenaar9ccaae02016-05-07 18:36:48 +02005042
5043 if (*buf != NUL)
5044 for (i = 0; i < num_file; ++i)
5045 {
5046 len = (int)STRLEN(file[i]) - 3;
5047 if (len <= 0)
5048 continue;
5049 if (STRCMP(file[i] + len, buf) == 0)
5050 {
5051 /* remove the default language */
5052 file[i][len] = NUL;
5053 }
5054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055}
5056#endif
5057
5058/*
5059 * Do the expansion based on xp->xp_context and "pat".
5060 */
5061 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005062ExpandFromContext(
5063 expand_T *xp,
5064 char_u *pat,
5065 int *num_file,
5066 char_u ***file,
5067 int options) /* EW_ flags */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068{
5069#ifdef FEAT_CMDL_COMPL
5070 regmatch_T regmatch;
5071#endif
5072 int ret;
5073 int flags;
5074
5075 flags = EW_DIR; /* include directories */
5076 if (options & WILD_LIST_NOTFOUND)
5077 flags |= EW_NOTFOUND;
5078 if (options & WILD_ADD_SLASH)
5079 flags |= EW_ADDSLASH;
5080 if (options & WILD_KEEP_ALL)
5081 flags |= EW_KEEPALL;
5082 if (options & WILD_SILENT)
5083 flags |= EW_SILENT;
Bram Moolenaara245bc72015-03-05 19:35:25 +01005084 if (options & WILD_ALLLINKS)
5085 flags |= EW_ALLLINKS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005087 if (xp->xp_context == EXPAND_FILES
5088 || xp->xp_context == EXPAND_DIRECTORIES
5089 || xp->xp_context == EXPAND_FILES_IN_PATH)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 /*
5092 * Expand file or directory names.
5093 */
5094 int free_pat = FALSE;
5095 int i;
5096
5097 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5098 * space */
5099 if (xp->xp_backslash != XP_BS_NONE)
5100 {
5101 free_pat = TRUE;
5102 pat = vim_strsave(pat);
5103 for (i = 0; pat[i]; ++i)
5104 if (pat[i] == '\\')
5105 {
5106 if (xp->xp_backslash == XP_BS_THREE
5107 && pat[i + 1] == '\\'
5108 && pat[i + 2] == '\\'
5109 && pat[i + 3] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005110 STRMOVE(pat + i, pat + i + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 if (xp->xp_backslash == XP_BS_ONE
5112 && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005113 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115 }
5116
5117 if (xp->xp_context == EXPAND_FILES)
5118 flags |= EW_FILE;
Bram Moolenaarcc448b32010-07-14 16:52:17 +02005119 else if (xp->xp_context == EXPAND_FILES_IN_PATH)
5120 flags |= (EW_FILE | EW_PATH);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else
5122 flags = (flags | EW_DIR) & ~EW_FILE;
Bram Moolenaar94950a92010-12-02 16:01:29 +01005123 if (options & WILD_ICASE)
5124 flags |= EW_ICASE;
5125
Bram Moolenaard7834d32009-12-02 16:14:36 +00005126 /* Expand wildcards, supporting %:h and the like. */
5127 ret = expand_wildcards_eval(&pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (free_pat)
5129 vim_free(pat);
5130 return ret;
5131 }
5132
5133 *file = (char_u **)"";
5134 *num_file = 0;
5135 if (xp->xp_context == EXPAND_HELP)
5136 {
Bram Moolenaarc62e2fe2008-08-06 13:03:07 +00005137 /* With an empty argument we would get all the help tags, which is
5138 * very slow. Get matches for "help" instead. */
5139 if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat,
5140 num_file, file, FALSE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
5142#ifdef FEAT_MULTI_LANG
5143 cleanup_help_tags(*num_file, *file);
5144#endif
5145 return OK;
5146 }
5147 return FAIL;
5148 }
5149
5150#ifndef FEAT_CMDL_COMPL
5151 return FAIL;
5152#else
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005153 if (xp->xp_context == EXPAND_SHELLCMD)
5154 return expand_shellcmd(pat, num_file, file, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 if (xp->xp_context == EXPAND_OLD_SETTING)
5156 return ExpandOldSetting(num_file, file);
5157 if (xp->xp_context == EXPAND_BUFFERS)
5158 return ExpandBufnames(pat, num_file, file, options);
5159 if (xp->xp_context == EXPAND_TAGS
5160 || xp->xp_context == EXPAND_TAGS_LISTFILES)
5161 return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file);
5162 if (xp->xp_context == EXPAND_COLORS)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005163 {
5164 char *directories[] = {"colors", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005165 return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file,
5166 directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 if (xp->xp_context == EXPAND_COMPILER)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005169 {
Bram Moolenaara627c962011-09-30 16:23:32 +02005170 char *directories[] = {"compiler", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005171 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005172 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005173 if (xp->xp_context == EXPAND_OWNSYNTAX)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005174 {
5175 char *directories[] = {"syntax", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005176 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005177 }
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005178 if (xp->xp_context == EXPAND_FILETYPE)
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005179 {
5180 char *directories[] = {"syntax", "indent", "ftplugin", NULL};
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005181 return ExpandRTDir(pat, 0, num_file, file, directories);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005182 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005183# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5184 if (xp->xp_context == EXPAND_USER_LIST)
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00005185 return ExpandUserList(xp, num_file, file);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005186# endif
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005187 if (xp->xp_context == EXPAND_PACKADD)
5188 return ExpandPackAddDir(pat, num_file, file);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189
5190 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
5191 if (regmatch.regprog == NULL)
5192 return FAIL;
5193
5194 /* set ignore-case according to p_ic, p_scs and pat */
5195 regmatch.rm_ic = ignorecase(pat);
5196
5197 if (xp->xp_context == EXPAND_SETTINGS
5198 || xp->xp_context == EXPAND_BOOL_SETTINGS)
5199 ret = ExpandSettings(xp, &regmatch, num_file, file);
5200 else if (xp->xp_context == EXPAND_MAPPINGS)
5201 ret = ExpandMappings(&regmatch, num_file, file);
5202# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5203 else if (xp->xp_context == EXPAND_USER_DEFINED)
5204 ret = ExpandUserDefined(xp, &regmatch, num_file, file);
5205# endif
5206 else
5207 {
5208 static struct expgen
5209 {
5210 int context;
Bram Moolenaard99df422016-01-29 23:20:40 +01005211 char_u *((*func)(expand_T *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 int ic;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005213 int escaped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 } tab[] =
5215 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005216 {EXPAND_COMMANDS, get_command_name, FALSE, TRUE},
5217 {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE},
Bram Moolenaarcae92dc2017-08-06 15:22:15 +02005218 {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE},
Bram Moolenaar9e507ca2016-10-15 15:39:39 +02005219 {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE},
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005220#ifdef FEAT_CMDHIST
5221 {EXPAND_HISTORY, get_history_arg, TRUE, TRUE},
5222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223#ifdef FEAT_USR_CMDS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005224 {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE},
Bram Moolenaarf1d6ccf2014-12-08 04:16:44 +01005225 {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005226 {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE},
5227 {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE},
5228 {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229#endif
5230#ifdef FEAT_EVAL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005231 {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE},
5232 {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE},
5233 {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE},
5234 {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235#endif
5236#ifdef FEAT_MENU
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005237 {EXPAND_MENUS, get_menu_name, FALSE, TRUE},
5238 {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239#endif
5240#ifdef FEAT_SYN_HL
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005241 {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242#endif
Bram Moolenaarcd9c4622013-06-08 15:24:48 +02005243#ifdef FEAT_PROFILE
5244 {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE},
5245#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005246 {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE},
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005247 {EXPAND_EVENTS, get_event_name, TRUE, TRUE},
5248 {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005249#ifdef FEAT_CSCOPE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005250 {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE},
Bram Moolenaarf4580d82009-03-18 11:52:53 +00005251#endif
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005252#ifdef FEAT_SIGNS
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005253 {EXPAND_SIGN, get_sign_name, TRUE, TRUE},
Bram Moolenaar3c65e312009-04-29 16:47:23 +00005254#endif
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005255#ifdef FEAT_PROFILE
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005256 {EXPAND_PROFILE, get_profile_name, TRUE, TRUE},
Bram Moolenaarf86f26c2010-02-03 15:14:22 +01005257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258#if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \
5259 && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE))
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005260 {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE},
5261 {EXPAND_LOCALES, get_locales, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262#endif
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005263 {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE},
Bram Moolenaar24305862012-08-15 14:05:05 +02005264 {EXPAND_USER, get_users, TRUE, FALSE},
Bram Moolenaarcd43eff2018-03-29 15:55:38 +02005265 {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE},
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 };
5267 int i;
5268
5269 /*
5270 * Find a context in the table and call the ExpandGeneric() with the
5271 * right function to do the expansion.
5272 */
5273 ret = FAIL;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00005274 for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (xp->xp_context == tab[i].context)
5276 {
5277 if (tab[i].ic)
5278 regmatch.rm_ic = TRUE;
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005279 ret = ExpandGeneric(xp, &regmatch, num_file, file,
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005280 tab[i].func, tab[i].escaped);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 break;
5282 }
5283 }
5284
Bram Moolenaar473de612013-06-08 18:19:48 +02005285 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
5287 return ret;
5288#endif /* FEAT_CMDL_COMPL */
5289}
5290
5291#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5292/*
5293 * Expand a list of names.
5294 *
5295 * Generic function for command line completion. It calls a function to
5296 * obtain strings, one by one. The strings are matched against a regexp
5297 * program. Matching strings are copied into an array, which is returned.
5298 *
5299 * Returns OK when no problems encountered, FAIL for error (out of memory).
5300 */
5301 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005302ExpandGeneric(
5303 expand_T *xp,
5304 regmatch_T *regmatch,
5305 int *num_file,
5306 char_u ***file,
5307 char_u *((*func)(expand_T *, int)),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 /* returns a string from the list */
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005309 int escaped)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310{
5311 int i;
5312 int count = 0;
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005313 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 char_u *str;
5315
5316 /* do this loop twice:
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005317 * round == 0: count the number of matching names
5318 * round == 1: copy the matching names into allocated memory
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005320 for (round = 0; round <= 1; ++round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 for (i = 0; ; ++i)
5323 {
5324 str = (*func)(xp, i);
5325 if (str == NULL) /* end of list */
5326 break;
5327 if (*str == NUL) /* skip empty strings */
5328 continue;
5329
5330 if (vim_regexec(regmatch, str, (colnr_T)0))
5331 {
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005332 if (round)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 {
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02005334 if (escaped)
5335 str = vim_strsave_escaped(str, (char_u *)" \t\\.");
5336 else
5337 str = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 (*file)[count] = str;
5339#ifdef FEAT_MENU
5340 if (func == get_menu_names && str != NULL)
5341 {
5342 /* test for separator added by get_menu_names() */
5343 str += STRLEN(str) - 1;
5344 if (*str == '\001')
5345 *str = '.';
5346 }
5347#endif
5348 }
5349 ++count;
5350 }
5351 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005352 if (round == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 {
5354 if (count == 0)
5355 return OK;
5356 *num_file = count;
5357 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
5358 if (*file == NULL)
5359 {
5360 *file = (char_u **)"";
5361 return FAIL;
5362 }
5363 count = 0;
5364 }
5365 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005366
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00005367 /* Sort the results. Keep menu's in the specified order. */
5368 if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS)
Bram Moolenaardb710ed2011-10-26 22:02:15 +02005369 {
5370 if (xp->xp_context == EXPAND_EXPRESSION
5371 || xp->xp_context == EXPAND_FUNCTIONS
5372 || xp->xp_context == EXPAND_USER_FUNC)
5373 /* <SNR> functions should be sorted to the end. */
5374 qsort((void *)*file, (size_t)*num_file, sizeof(char_u *),
5375 sort_func_compare);
5376 else
5377 sort_strings(*file, *num_file);
5378 }
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +00005379
Bram Moolenaar4f688582007-07-24 12:34:30 +00005380#ifdef FEAT_CMDL_COMPL
5381 /* Reset the variables used for special highlight names expansion, so that
5382 * they don't show up when getting normal highlight names by ID. */
5383 reset_expand_highlight();
5384#endif
5385
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 return OK;
5387}
5388
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005389/*
5390 * Complete a shell command.
5391 * Returns FAIL or OK;
5392 */
5393 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005394expand_shellcmd(
5395 char_u *filepat, /* pattern to match with command names */
5396 int *num_file, /* return: number of matches */
5397 char_u ***file, /* return: array with matches */
5398 int flagsarg) /* EW_ flags */
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005399{
5400 char_u *pat;
5401 int i;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005402 char_u *path = NULL;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005403 int mustfree = FALSE;
5404 garray_T ga;
5405 char_u *buf = alloc(MAXPATHL);
5406 size_t l;
5407 char_u *s, *e;
5408 int flags = flagsarg;
5409 int ret;
Bram Moolenaarb5971142015-03-21 17:32:19 +01005410 int did_curdir = FALSE;
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005411 hashtab_T found_ht;
5412 hashitem_T *hi;
5413 hash_T hash;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005414
5415 if (buf == NULL)
5416 return FAIL;
5417
5418 /* for ":set path=" and ":set tags=" halve backslashes for escaped
5419 * space */
5420 pat = vim_strsave(filepat);
5421 for (i = 0; pat[i]; ++i)
5422 if (pat[i] == '\\' && pat[i + 1] == ' ')
Bram Moolenaar446cb832008-06-24 21:56:24 +00005423 STRMOVE(pat + i, pat + i + 1);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005424
Bram Moolenaarb5971142015-03-21 17:32:19 +01005425 flags |= EW_FILE | EW_EXEC | EW_SHELLCMD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005426
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005427 if (pat[0] == '.' && (vim_ispathsep(pat[1])
5428 || (pat[1] == '.' && vim_ispathsep(pat[2]))))
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005429 path = (char_u *)".";
5430 else
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005431 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005432 /* For an absolute name we don't use $PATH. */
5433 if (!mch_isFullName(pat))
5434 path = vim_getenv((char_u *)"PATH", &mustfree);
Bram Moolenaar27d9ece2010-11-10 15:37:05 +01005435 if (path == NULL)
5436 path = (char_u *)"";
5437 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005438
5439 /*
5440 * Go over all directories in $PATH. Expand matches in that directory and
Bram Moolenaarb5971142015-03-21 17:32:19 +01005441 * collect them in "ga". When "." is not in $PATH also expand for the
5442 * current directory, to find "subdir/cmd".
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005443 */
5444 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005445 hash_init(&found_ht);
Bram Moolenaarb5971142015-03-21 17:32:19 +01005446 for (s = path; ; s = e)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005447 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005448#if defined(MSWIN)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005449 e = vim_strchr(s, ';');
5450#else
5451 e = vim_strchr(s, ':');
5452#endif
5453 if (e == NULL)
5454 e = s + STRLEN(s);
5455
Bram Moolenaar6ab9e422018-07-28 19:20:13 +02005456 if (*s == NUL)
5457 {
5458 if (did_curdir)
5459 break;
5460 // Find directories in the current directory, path is empty.
5461 did_curdir = TRUE;
5462 flags |= EW_DIR;
5463 }
5464 else if (STRNCMP(s, ".", (int)(e - s)) == 0)
5465 {
5466 did_curdir = TRUE;
5467 flags |= EW_DIR;
5468 }
5469 else
5470 // Do not match directories inside a $PATH item.
5471 flags &= ~EW_DIR;
5472
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005473 l = e - s;
5474 if (l > MAXPATHL - 5)
5475 break;
5476 vim_strncpy(buf, s, l);
5477 add_pathsep(buf);
5478 l = STRLEN(buf);
5479 vim_strncpy(buf + l, pat, MAXPATHL - 1 - l);
5480
5481 /* Expand matches in one directory of $PATH. */
5482 ret = expand_wildcards(1, &buf, num_file, file, flags);
5483 if (ret == OK)
5484 {
5485 if (ga_grow(&ga, *num_file) == FAIL)
5486 FreeWild(*num_file, *file);
5487 else
5488 {
5489 for (i = 0; i < *num_file; ++i)
5490 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005491 char_u *name = (*file)[i];
5492
5493 if (STRLEN(name) > l)
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005494 {
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005495 // Check if this name was already found.
5496 hash = hash_hash(name + l);
5497 hi = hash_lookup(&found_ht, name + l, hash);
5498 if (HASHITEM_EMPTY(hi))
5499 {
5500 // Remove the path that was prepended.
5501 STRMOVE(name, name + l);
5502 ((char_u **)ga.ga_data)[ga.ga_len++] = name;
5503 hash_add_item(&found_ht, hi, name, hash);
5504 name = NULL;
5505 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005506 }
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005507 vim_free(name);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005508 }
5509 vim_free(*file);
5510 }
5511 }
5512 if (*e != NUL)
5513 ++e;
5514 }
5515 *file = ga.ga_data;
5516 *num_file = ga.ga_len;
5517
5518 vim_free(buf);
5519 vim_free(pat);
5520 if (mustfree)
5521 vim_free(path);
Bram Moolenaar62fe66f2018-05-22 16:58:47 +02005522 hash_clear(&found_ht);
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005523 return OK;
5524}
5525
5526
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527# if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL)
5528/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01005529 * Call "user_expand_func()" to invoke a user defined Vim script function and
5530 * return the result (either a string or a List).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005532 static void *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005533call_user_expand_func(
Bram Moolenaarded27a12018-08-01 19:06:03 +02005534 void *(*user_expand_func)(char_u *, int, typval_T *),
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005535 expand_T *xp,
5536 int *num_file,
5537 char_u ***file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538{
Bram Moolenaar673b9a32013-06-30 22:43:27 +02005539 int keep = 0;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005540 typval_T args[4];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 int save_current_SID = current_SID;
Bram Moolenaarffa96842018-06-12 22:05:14 +02005542 char_u *pat = NULL;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005543 void *ret;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005544 struct cmdline_info save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaarb7515462013-06-29 12:58:33 +02005546 if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005547 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 *num_file = 0;
5549 *file = NULL;
5550
Bram Moolenaarb7515462013-06-29 12:58:33 +02005551 if (ccline.cmdbuff != NULL)
Bram Moolenaar21669c02008-01-18 12:16:16 +00005552 {
Bram Moolenaar21669c02008-01-18 12:16:16 +00005553 keep = ccline.cmdbuff[ccline.cmdlen];
5554 ccline.cmdbuff[ccline.cmdlen] = 0;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005555 }
Bram Moolenaarb7515462013-06-29 12:58:33 +02005556
Bram Moolenaarffa96842018-06-12 22:05:14 +02005557 pat = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len);
5558
5559 args[0].v_type = VAR_STRING;
5560 args[0].vval.v_string = pat;
5561 args[1].v_type = VAR_STRING;
5562 args[1].vval.v_string = xp->xp_line;
5563 args[2].v_type = VAR_NUMBER;
5564 args[2].vval.v_number = xp->xp_col;
5565 args[3].v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005567 /* Save the cmdline, we don't know what the function may do. */
5568 save_ccline = ccline;
5569 ccline.cmdbuff = NULL;
5570 ccline.cmdprompt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 current_SID = xp->xp_scriptID;
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005572
Bram Moolenaarded27a12018-08-01 19:06:03 +02005573 ret = user_expand_func(xp->xp_arg, 3, args);
Bram Moolenaar592e0a22004-07-03 16:05:59 +00005574
5575 ccline = save_ccline;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 current_SID = save_current_SID;
Bram Moolenaar21669c02008-01-18 12:16:16 +00005577 if (ccline.cmdbuff != NULL)
5578 ccline.cmdbuff[ccline.cmdlen] = keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005579
Bram Moolenaarffa96842018-06-12 22:05:14 +02005580 vim_free(pat);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005581 return ret;
5582}
5583
5584/*
5585 * Expand names with a function defined by the user.
5586 */
5587 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005588ExpandUserDefined(
5589 expand_T *xp,
5590 regmatch_T *regmatch,
5591 int *num_file,
5592 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005593{
5594 char_u *retstr;
5595 char_u *s;
5596 char_u *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005597 int keep;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005598 garray_T ga;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005599 int skip;
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005600
5601 retstr = call_user_expand_func(call_func_retstr, xp, num_file, file);
5602 if (retstr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 return FAIL;
5604
5605 ga_init2(&ga, (int)sizeof(char *), 3);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005606 for (s = retstr; *s != NUL; s = e)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 {
5608 e = vim_strchr(s, '\n');
5609 if (e == NULL)
5610 e = s + STRLEN(s);
5611 keep = *e;
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005612 *e = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005614 skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0;
5615 *e = keep;
5616
5617 if (!skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
Bram Moolenaar71a43c02018-02-11 15:20:20 +01005619 if (ga_grow(&ga, 1) == FAIL)
5620 break;
5621 ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s));
5622 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 if (*e != NUL)
5626 ++e;
5627 }
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005628 vim_free(retstr);
5629 *file = ga.ga_data;
5630 *num_file = ga.ga_len;
5631 return OK;
5632}
5633
5634/*
5635 * Expand names with a list returned by a function defined by the user.
5636 */
5637 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005638ExpandUserList(
5639 expand_T *xp,
5640 int *num_file,
5641 char_u ***file)
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005642{
5643 list_T *retlist;
5644 listitem_T *li;
5645 garray_T ga;
5646
5647 retlist = call_user_expand_func(call_func_retlist, xp, num_file, file);
5648 if (retlist == NULL)
5649 return FAIL;
5650
5651 ga_init2(&ga, (int)sizeof(char *), 3);
5652 /* Loop over the items in the list. */
5653 for (li = retlist->lv_first; li != NULL; li = li->li_next)
5654 {
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005655 if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL)
5656 continue; /* Skip non-string items and empty strings */
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005657
5658 if (ga_grow(&ga, 1) == FAIL)
5659 break;
5660
5661 ((char_u **)ga.ga_data)[ga.ga_len] =
Bram Moolenaar8d3b8c42009-06-24 15:05:00 +00005662 vim_strsave(li->li_tv.vval.v_string);
Bram Moolenaar35fdbb52005-07-09 21:08:57 +00005663 ++ga.ga_len;
5664 }
5665 list_unref(retlist);
5666
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667 *file = ga.ga_data;
5668 *num_file = ga.ga_len;
5669 return OK;
5670}
5671#endif
5672
5673/*
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005674 * Expand color scheme, compiler or filetype names.
5675 * Search from 'runtimepath':
5676 * 'runtimepath'/{dirnames}/{pat}.vim
5677 * When "flags" has DIP_START: search also from 'start' of 'packpath':
5678 * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim
5679 * When "flags" has DIP_OPT: search also from 'opt' of 'packpath':
5680 * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005681 * "dirnames" is an array with one or more directory names.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 */
5683 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005684ExpandRTDir(
5685 char_u *pat,
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005686 int flags,
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005687 int *num_file,
5688 char_u ***file,
5689 char *dirnames[])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 char_u *s;
5692 char_u *e;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005693 char_u *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 garray_T ga;
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005695 int i;
5696 int pat_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697
5698 *num_file = 0;
5699 *file = NULL;
Bram Moolenaar5cfe2d72011-07-07 15:04:52 +02005700 pat_len = (int)STRLEN(pat);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005701 ga_init2(&ga, (int)sizeof(char *), 10);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005703 for (i = 0; dirnames[i] != NULL; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005705 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7));
5706 if (s == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 {
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005708 ga_clear_strings(&ga);
5709 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005711 sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat);
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005712 globpath(p_rtp, s, &ga, 0);
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005713 vim_free(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005715
Bram Moolenaar52f9c192016-03-13 13:24:45 +01005716 if (flags & DIP_START) {
5717 for (i = 0; dirnames[i] != NULL; ++i)
5718 {
5719 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22));
5720 if (s == NULL)
5721 {
5722 ga_clear_strings(&ga);
5723 return FAIL;
5724 }
5725 sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat);
5726 globpath(p_pp, s, &ga, 0);
5727 vim_free(s);
5728 }
5729 }
5730
5731 if (flags & DIP_OPT) {
5732 for (i = 0; dirnames[i] != NULL; ++i)
5733 {
5734 s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20));
5735 if (s == NULL)
5736 {
5737 ga_clear_strings(&ga);
5738 return FAIL;
5739 }
5740 sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat);
5741 globpath(p_pp, s, &ga, 0);
5742 vim_free(s);
5743 }
5744 }
5745
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005746 for (i = 0; i < ga.ga_len; ++i)
5747 {
5748 match = ((char_u **)ga.ga_data)[i];
5749 s = match;
5750 e = s + STRLEN(s);
5751 if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0)
5752 {
5753 e -= 4;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005754 for (s = e; s > match; MB_PTR_BACK(match, s))
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005755 if (s < match || vim_ispathsep(*s))
5756 break;
5757 ++s;
5758 *e = NUL;
5759 mch_memmove(match, s, e - s + 1);
5760 }
5761 }
5762
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005763 if (ga.ga_len == 0)
Bram Moolenaare79abdd2012-06-29 13:44:41 +02005764 return FAIL;
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005765
5766 /* Sort and remove duplicates which can happen when specifying multiple
Bram Moolenaar0c7437a2011-06-26 19:40:23 +02005767 * directories in dirnames. */
Bram Moolenaar1587a1e2010-07-29 20:59:59 +02005768 remove_duplicates(&ga);
5769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 *file = ga.ga_data;
5771 *num_file = ga.ga_len;
5772 return OK;
5773}
5774
Bram Moolenaar35ca0e72016-03-05 17:41:49 +01005775/*
5776 * Expand loadplugin names:
5777 * 'packpath'/pack/ * /opt/{pat}
5778 */
5779 static int
5780ExpandPackAddDir(
5781 char_u *pat,
5782 int *num_file,
5783 char_u ***file)
5784{
5785 char_u *s;
5786 char_u *e;
5787 char_u *match;
5788 garray_T ga;
5789 int i;
5790 int pat_len;
5791
5792 *num_file = 0;
5793 *file = NULL;
5794 pat_len = (int)STRLEN(pat);
5795 ga_init2(&ga, (int)sizeof(char *), 10);
5796
5797 s = alloc((unsigned)(pat_len + 26));
5798 if (s == NULL)
5799 {
5800 ga_clear_strings(&ga);
5801 return FAIL;
5802 }
5803 sprintf((char *)s, "pack/*/opt/%s*", pat);
5804 globpath(p_pp, s, &ga, 0);
5805 vim_free(s);
5806
5807 for (i = 0; i < ga.ga_len; ++i)
5808 {
5809 match = ((char_u **)ga.ga_data)[i];
5810 s = gettail(match);
5811 e = s + STRLEN(s);
5812 mch_memmove(match, s, e - s + 1);
5813 }
5814
5815 if (ga.ga_len == 0)
5816 return FAIL;
5817
5818 /* Sort and remove duplicates which can happen when specifying multiple
5819 * directories in dirnames. */
5820 remove_duplicates(&ga);
5821
5822 *file = ga.ga_data;
5823 *num_file = ga.ga_len;
5824 return OK;
5825}
5826
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#endif
5828
5829#if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO)
5830/*
5831 * Expand "file" for all comma-separated directories in "path".
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005832 * Adds the matches to "ga". Caller must init "ga".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 */
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005834 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005835globpath(
5836 char_u *path,
5837 char_u *file,
5838 garray_T *ga,
5839 int expand_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840{
5841 expand_T xpc;
5842 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 int num_p;
5845 char_u **p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 buf = alloc(MAXPATHL);
5848 if (buf == NULL)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005849 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005851 ExpandInit(&xpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar8ada17c2006-01-19 22:16:24 +00005853
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 /* Loop over all entries in {path}. */
5855 while (*path != NUL)
5856 {
5857 /* Copy one item of the path to buf[] and concatenate the file name. */
5858 copy_option_part(&path, buf, MAXPATHL, ",");
5859 if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL)
5860 {
Bram Moolenaar48e330a2016-02-23 14:53:34 +01005861# if defined(MSWIN)
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005862 /* Using the platform's path separator (\) makes vim incorrectly
5863 * treat it as an escape character, use '/' instead. */
5864 if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf)))
5865 STRCAT(buf, "/");
5866# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 add_pathsep(buf);
Bram Moolenaar84f888a2010-08-05 21:40:16 +02005868# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 STRCAT(buf, file);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005870 if (ExpandFromContext(&xpc, buf, &num_p, &p,
5871 WILD_SILENT|expand_options) != FAIL && num_p > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00005873 ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005875 if (ga_grow(ga, num_p) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 for (i = 0; i < num_p; ++i)
5878 {
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005879 ((char_u **)ga->ga_data)[ga->ga_len] =
Bram Moolenaar7116aa02014-05-29 14:36:29 +02005880 vim_strnsave(p[i], (int)STRLEN(p[i]));
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005881 ++ga->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005883 }
Bram Moolenaar1b1063a2014-05-07 18:35:30 +02005884
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 FreeWild(num_p, p);
5886 }
5887 }
5888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889
5890 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891}
5892
5893#endif
5894
5895#if defined(FEAT_CMDHIST) || defined(PROTO)
5896
5897/*********************************
5898 * Command line history stuff *
5899 *********************************/
5900
5901/*
5902 * Translate a history character to the associated type number.
5903 */
5904 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005905hist_char2type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 if (c == ':')
5908 return HIST_CMD;
5909 if (c == '=')
5910 return HIST_EXPR;
5911 if (c == '@')
5912 return HIST_INPUT;
5913 if (c == '>')
5914 return HIST_DEBUG;
5915 return HIST_SEARCH; /* must be '?' or '/' */
5916}
5917
5918/*
5919 * Table of history names.
5920 * These names are used in :history and various hist...() functions.
5921 * It is sufficient to give the significant prefix of a history name.
5922 */
5923
5924static char *(history_names[]) =
5925{
5926 "cmd",
5927 "search",
5928 "expr",
5929 "input",
5930 "debug",
5931 NULL
5932};
5933
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005934#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
5935/*
5936 * Function given to ExpandGeneric() to obtain the possible first
5937 * arguments of the ":history command.
5938 */
5939 static char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005940get_history_arg(expand_T *xp UNUSED, int idx)
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005941{
5942 static char_u compl[2] = { NUL, NUL };
5943 char *short_names = ":=@>?/";
Bram Moolenaar17bd9dc2012-05-25 11:02:41 +02005944 int short_names_count = (int)STRLEN(short_names);
Bram Moolenaar5ae636b2012-04-30 18:48:53 +02005945 int history_name_count = sizeof(history_names) / sizeof(char *) - 1;
5946
5947 if (idx < short_names_count)
5948 {
5949 compl[0] = (char_u)short_names[idx];
5950 return compl;
5951 }
5952 if (idx < short_names_count + history_name_count)
5953 return (char_u *)history_names[idx - short_names_count];
5954 if (idx == short_names_count + history_name_count)
5955 return (char_u *)"all";
5956 return NULL;
5957}
5958#endif
5959
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960/*
5961 * init_history() - Initialize the command line history.
5962 * Also used to re-allocate the history when the size changes.
5963 */
Bram Moolenaar1ec484f2005-06-24 23:07:47 +00005964 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01005965init_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966{
5967 int newlen; /* new length of history table */
5968 histentry_T *temp;
5969 int i;
5970 int j;
5971 int type;
5972
5973 /*
5974 * If size of history table changed, reallocate it
5975 */
5976 newlen = (int)p_hi;
5977 if (newlen != hislen) /* history length changed */
5978 {
5979 for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */
5980 {
5981 if (newlen)
5982 {
5983 temp = (histentry_T *)lalloc(
5984 (long_u)(newlen * sizeof(histentry_T)), TRUE);
5985 if (temp == NULL) /* out of memory! */
5986 {
5987 if (type == 0) /* first one: just keep the old length */
5988 {
5989 newlen = hislen;
5990 break;
5991 }
5992 /* Already changed one table, now we can only have zero
5993 * length for all tables. */
5994 newlen = 0;
5995 type = -1;
5996 continue;
5997 }
5998 }
5999 else
6000 temp = NULL;
6001 if (newlen == 0 || temp != NULL)
6002 {
6003 if (hisidx[type] < 0) /* there are no entries yet */
6004 {
6005 for (i = 0; i < newlen; ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006006 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 }
6008 else if (newlen > hislen) /* array becomes bigger */
6009 {
6010 for (i = 0; i <= hisidx[type]; ++i)
6011 temp[i] = history[type][i];
6012 j = i;
6013 for ( ; i <= newlen - (hislen - hisidx[type]); ++i)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006014 clear_hist_entry(&temp[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 for ( ; j < hislen; ++i, ++j)
6016 temp[i] = history[type][j];
6017 }
6018 else /* array becomes smaller or 0 */
6019 {
6020 j = hisidx[type];
6021 for (i = newlen - 1; ; --i)
6022 {
6023 if (i >= 0) /* copy newest entries */
6024 temp[i] = history[type][j];
6025 else /* remove older entries */
6026 vim_free(history[type][j].hisstr);
6027 if (--j < 0)
6028 j = hislen - 1;
6029 if (j == hisidx[type])
6030 break;
6031 }
6032 hisidx[type] = newlen - 1;
6033 }
6034 vim_free(history[type]);
6035 history[type] = temp;
6036 }
6037 }
6038 hislen = newlen;
6039 }
6040}
6041
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006042 static void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006043clear_hist_entry(histentry_T *hisptr)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006044{
6045 hisptr->hisnum = 0;
6046 hisptr->viminfo = FALSE;
6047 hisptr->hisstr = NULL;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006048 hisptr->time_set = 0;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006049}
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051/*
6052 * Check if command line 'str' is already in history.
6053 * If 'move_to_front' is TRUE, matching entry is moved to end of history.
6054 */
6055 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006056in_history(
6057 int type,
6058 char_u *str,
6059 int move_to_front, /* Move the entry to the front if it exists */
6060 int sep,
6061 int writing) /* ignore entries read from viminfo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 int i;
6064 int last_i = -1;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006065 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066
6067 if (hisidx[type] < 0)
6068 return FALSE;
6069 i = hisidx[type];
6070 do
6071 {
6072 if (history[type][i].hisstr == NULL)
6073 return FALSE;
Bram Moolenaar4c402232011-07-27 17:58:46 +02006074
6075 /* For search history, check that the separator character matches as
6076 * well. */
6077 p = history[type][i].hisstr;
6078 if (STRCMP(str, p) == 0
Bram Moolenaar07219f92013-04-14 23:19:36 +02006079 && !(writing && history[type][i].viminfo)
Bram Moolenaar4c402232011-07-27 17:58:46 +02006080 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
6082 if (!move_to_front)
6083 return TRUE;
6084 last_i = i;
6085 break;
6086 }
6087 if (--i < 0)
6088 i = hislen - 1;
6089 } while (i != hisidx[type]);
6090
6091 if (last_i >= 0)
6092 {
6093 str = history[type][i].hisstr;
6094 while (i != hisidx[type])
6095 {
6096 if (++i >= hislen)
6097 i = 0;
6098 history[type][last_i] = history[type][i];
6099 last_i = i;
6100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 history[type][i].hisnum = ++hisnum[type];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006102 history[type][i].viminfo = FALSE;
6103 history[type][i].hisstr = str;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006104 history[type][i].time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 return TRUE;
6106 }
6107 return FALSE;
6108}
6109
6110/*
6111 * Convert history name (from table above) to its HIST_ equivalent.
6112 * When "name" is empty, return "cmd" history.
6113 * Returns -1 for unknown history name.
6114 */
6115 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006116get_histtype(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
6118 int i;
6119 int len = (int)STRLEN(name);
6120
6121 /* No argument: use current history. */
6122 if (len == 0)
6123 return hist_char2type(ccline.cmdfirstc);
6124
6125 for (i = 0; history_names[i] != NULL; ++i)
6126 if (STRNICMP(name, history_names[i], len) == 0)
6127 return i;
6128
6129 if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL)
6130 return hist_char2type(name[0]);
6131
6132 return -1;
6133}
6134
6135static int last_maptick = -1; /* last seen maptick */
6136
6137/*
6138 * Add the given string to the given history. If the string is already in the
6139 * history then it is moved to the front. "histype" may be one of he HIST_
6140 * values.
6141 */
6142 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006143add_to_history(
6144 int histype,
6145 char_u *new_entry,
6146 int in_map, /* consider maptick when inside a mapping */
6147 int sep) /* separator character used (search hist) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148{
6149 histentry_T *hisptr;
6150 int len;
6151
6152 if (hislen == 0) /* no history */
6153 return;
6154
Bram Moolenaara939e432013-11-09 05:30:26 +01006155 if (cmdmod.keeppatterns && histype == HIST_SEARCH)
6156 return;
6157
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 /*
6159 * Searches inside the same mapping overwrite each other, so that only
6160 * the last line is kept. Be careful not to remove a line that was moved
6161 * down, only lines that were added.
6162 */
6163 if (histype == HIST_SEARCH && in_map)
6164 {
Bram Moolenaar46643712016-09-09 21:42:36 +02006165 if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 {
6167 /* Current line is from the same mapping, remove it */
6168 hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]];
6169 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006170 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 --hisnum[histype];
6172 if (--hisidx[HIST_SEARCH] < 0)
6173 hisidx[HIST_SEARCH] = hislen - 1;
6174 }
6175 last_maptick = -1;
6176 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02006177 if (!in_history(histype, new_entry, TRUE, sep, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 {
6179 if (++hisidx[histype] == hislen)
6180 hisidx[histype] = 0;
6181 hisptr = &history[histype][hisidx[histype]];
6182 vim_free(hisptr->hisstr);
6183
6184 /* Store the separator after the NUL of the string. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006185 len = (int)STRLEN(new_entry);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 hisptr->hisstr = vim_strnsave(new_entry, len + 2);
6187 if (hisptr->hisstr != NULL)
6188 hisptr->hisstr[len + 1] = sep;
6189
6190 hisptr->hisnum = ++hisnum[histype];
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006191 hisptr->viminfo = FALSE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006192 hisptr->time_set = vim_time();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (histype == HIST_SEARCH && in_map)
6194 last_maptick = maptick;
6195 }
6196}
6197
6198#if defined(FEAT_EVAL) || defined(PROTO)
6199
6200/*
6201 * Get identifier of newest history entry.
6202 * "histype" may be one of the HIST_ values.
6203 */
6204 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006205get_history_idx(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206{
6207 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6208 || hisidx[histype] < 0)
6209 return -1;
6210
6211 return history[histype][hisidx[histype]].hisnum;
6212}
6213
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00006214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006215 * Calculate history index from a number:
6216 * num > 0: seen as identifying number of a history entry
6217 * num < 0: relative position in history wrt newest entry
6218 * "histype" may be one of the HIST_ values.
6219 */
6220 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006221calc_hist_idx(int histype, int num)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 int i;
6224 histentry_T *hist;
6225 int wrapped = FALSE;
6226
6227 if (hislen == 0 || histype < 0 || histype >= HIST_COUNT
6228 || (i = hisidx[histype]) < 0 || num == 0)
6229 return -1;
6230
6231 hist = history[histype];
6232 if (num > 0)
6233 {
6234 while (hist[i].hisnum > num)
6235 if (--i < 0)
6236 {
6237 if (wrapped)
6238 break;
6239 i += hislen;
6240 wrapped = TRUE;
6241 }
6242 if (hist[i].hisnum == num && hist[i].hisstr != NULL)
6243 return i;
6244 }
6245 else if (-num <= hislen)
6246 {
6247 i += num + 1;
6248 if (i < 0)
6249 i += hislen;
6250 if (hist[i].hisstr != NULL)
6251 return i;
6252 }
6253 return -1;
6254}
6255
6256/*
6257 * Get a history entry by its index.
6258 * "histype" may be one of the HIST_ values.
6259 */
6260 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006261get_history_entry(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 idx = calc_hist_idx(histype, idx);
6264 if (idx >= 0)
6265 return history[histype][idx].hisstr;
6266 else
6267 return (char_u *)"";
6268}
6269
6270/*
6271 * Clear all entries of a history.
6272 * "histype" may be one of the HIST_ values.
6273 */
6274 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006275clr_history(int histype)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006276{
6277 int i;
6278 histentry_T *hisptr;
6279
6280 if (hislen != 0 && histype >= 0 && histype < HIST_COUNT)
6281 {
6282 hisptr = history[histype];
6283 for (i = hislen; i--;)
6284 {
6285 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006286 clear_hist_entry(hisptr);
Bram Moolenaar119d4692016-03-05 21:21:24 +01006287 hisptr++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 }
6289 hisidx[histype] = -1; /* mark history as cleared */
6290 hisnum[histype] = 0; /* reset identifier counter */
6291 return OK;
6292 }
6293 return FAIL;
6294}
6295
6296/*
6297 * Remove all entries matching {str} from a history.
6298 * "histype" may be one of the HIST_ values.
6299 */
6300 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006301del_history_entry(int histype, char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006302{
6303 regmatch_T regmatch;
6304 histentry_T *hisptr;
6305 int idx;
6306 int i;
6307 int last;
6308 int found = FALSE;
6309
6310 regmatch.regprog = NULL;
6311 regmatch.rm_ic = FALSE; /* always match case */
6312 if (hislen != 0
6313 && histype >= 0
6314 && histype < HIST_COUNT
6315 && *str != NUL
6316 && (idx = hisidx[histype]) >= 0
6317 && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING))
6318 != NULL)
6319 {
6320 i = last = idx;
6321 do
6322 {
6323 hisptr = &history[histype][i];
6324 if (hisptr->hisstr == NULL)
6325 break;
6326 if (vim_regexec(&regmatch, hisptr->hisstr, (colnr_T)0))
6327 {
6328 found = TRUE;
6329 vim_free(hisptr->hisstr);
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006330 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331 }
6332 else
6333 {
6334 if (i != last)
6335 {
6336 history[histype][last] = *hisptr;
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006337 clear_hist_entry(hisptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 }
6339 if (--last < 0)
6340 last += hislen;
6341 }
6342 if (--i < 0)
6343 i += hislen;
6344 } while (i != idx);
6345 if (history[histype][idx].hisstr == NULL)
6346 hisidx[histype] = -1;
6347 }
Bram Moolenaar473de612013-06-08 18:19:48 +02006348 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 return found;
6350}
6351
6352/*
6353 * Remove an indexed entry from a history.
6354 * "histype" may be one of the HIST_ values.
6355 */
6356 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006357del_history_idx(int histype, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 int i, j;
6360
6361 i = calc_hist_idx(histype, idx);
6362 if (i < 0)
6363 return FALSE;
6364 idx = hisidx[histype];
6365 vim_free(history[histype][i].hisstr);
6366
6367 /* When deleting the last added search string in a mapping, reset
6368 * last_maptick, so that the last added search string isn't deleted again.
6369 */
6370 if (histype == HIST_SEARCH && maptick == last_maptick && i == idx)
6371 last_maptick = -1;
6372
6373 while (i != idx)
6374 {
6375 j = (i + 1) % hislen;
6376 history[histype][i] = history[histype][j];
6377 i = j;
6378 }
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006379 clear_hist_entry(&history[histype][i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380 if (--i < 0)
6381 i += hislen;
6382 hisidx[histype] = i;
6383 return TRUE;
6384}
6385
6386#endif /* FEAT_EVAL */
6387
6388#if defined(FEAT_CRYPT) || defined(PROTO)
6389/*
6390 * Very specific function to remove the value in ":set key=val" from the
6391 * history.
6392 */
6393 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006394remove_key_from_history(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395{
6396 char_u *p;
6397 int i;
6398
6399 i = hisidx[HIST_CMD];
6400 if (i < 0)
6401 return;
6402 p = history[HIST_CMD][i].hisstr;
6403 if (p != NULL)
6404 for ( ; *p; ++p)
6405 if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3]))
6406 {
6407 p = vim_strchr(p + 3, '=');
6408 if (p == NULL)
6409 break;
6410 ++p;
Bram Moolenaar1c465442017-03-12 20:10:05 +01006411 for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006412 if (p[i] == '\\' && p[i + 1])
6413 ++i;
Bram Moolenaar446cb832008-06-24 21:56:24 +00006414 STRMOVE(p, p + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006415 --p;
6416 }
6417}
6418#endif
6419
6420#endif /* FEAT_CMDHIST */
6421
Bram Moolenaar064154c2016-03-19 22:50:43 +01006422#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006423/*
6424 * Get pointer to the command line info to use. cmdline_paste() may clear
6425 * ccline and put the previous value in prev_ccline.
6426 */
6427 static struct cmdline_info *
6428get_ccline_ptr(void)
6429{
6430 if ((State & CMDLINE) == 0)
6431 return NULL;
6432 if (ccline.cmdbuff != NULL)
6433 return &ccline;
6434 if (prev_ccline_used && prev_ccline.cmdbuff != NULL)
6435 return &prev_ccline;
6436 return NULL;
6437}
Bram Moolenaar064154c2016-03-19 22:50:43 +01006438#endif
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006439
Bram Moolenaar064154c2016-03-19 22:50:43 +01006440#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006441/*
6442 * Get the current command line in allocated memory.
6443 * Only works when the command line is being edited.
6444 * Returns NULL when something is wrong.
6445 */
6446 char_u *
6447get_cmdline_str(void)
6448{
6449 struct cmdline_info *p = get_ccline_ptr();
6450
6451 if (p == NULL)
6452 return NULL;
6453 return vim_strnsave(p->cmdbuff, p->cmdlen);
6454}
6455
6456/*
6457 * Get the current command line position, counted in bytes.
6458 * Zero is the first position.
6459 * Only works when the command line is being edited.
6460 * Returns -1 when something is wrong.
6461 */
6462 int
6463get_cmdline_pos(void)
6464{
6465 struct cmdline_info *p = get_ccline_ptr();
6466
6467 if (p == NULL)
6468 return -1;
6469 return p->cmdpos;
6470}
6471
6472/*
6473 * Set the command line byte position to "pos". Zero is the first position.
6474 * Only works when the command line is being edited.
6475 * Returns 1 when failed, 0 when OK.
6476 */
6477 int
6478set_cmdline_pos(
6479 int pos)
6480{
6481 struct cmdline_info *p = get_ccline_ptr();
6482
6483 if (p == NULL)
6484 return 1;
6485
6486 /* The position is not set directly but after CTRL-\ e or CTRL-R = has
6487 * changed the command line. */
6488 if (pos < 0)
6489 new_cmdpos = 0;
6490 else
6491 new_cmdpos = pos;
6492 return 0;
6493}
6494#endif
6495
6496#if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO)
6497/*
6498 * Get the current command-line type.
6499 * Returns ':' or '/' or '?' or '@' or '>' or '-'
6500 * Only works when the command line is being edited.
6501 * Returns NUL when something is wrong.
6502 */
6503 int
6504get_cmdline_type(void)
6505{
6506 struct cmdline_info *p = get_ccline_ptr();
6507
6508 if (p == NULL)
6509 return NUL;
6510 if (p->cmdfirstc == NUL)
Bram Moolenaar064154c2016-03-19 22:50:43 +01006511 return
6512# ifdef FEAT_EVAL
6513 (p->input_fn) ? '@' :
6514# endif
6515 '-';
Bram Moolenaard293b2b2016-03-19 22:29:49 +01006516 return p->cmdfirstc;
6517}
6518#endif
6519
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520#if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO)
6521/*
6522 * Get indices "num1,num2" that specify a range within a list (not a range of
6523 * text lines in a buffer!) from a string. Used for ":history" and ":clist".
6524 * Returns OK if parsed successfully, otherwise FAIL.
6525 */
6526 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006527get_list_range(char_u **str, int *num1, int *num2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528{
6529 int len;
6530 int first = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006531 varnumber_T num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532
6533 *str = skipwhite(*str);
6534 if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */
6535 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006536 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 *str += len;
6538 *num1 = (int)num;
6539 first = TRUE;
6540 }
6541 *str = skipwhite(*str);
6542 if (**str == ',') /* parse "to" part of range */
6543 {
6544 *str = skipwhite(*str + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01006545 vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 if (len > 0)
6547 {
6548 *num2 = (int)num;
6549 *str = skipwhite(*str + len);
6550 }
6551 else if (!first) /* no number given at all */
6552 return FAIL;
6553 }
6554 else if (first) /* only one number given */
6555 *num2 = *num1;
6556 return OK;
6557}
6558#endif
6559
6560#if defined(FEAT_CMDHIST) || defined(PROTO)
6561/*
6562 * :history command - print a history
6563 */
6564 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006565ex_history(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566{
6567 histentry_T *hist;
6568 int histype1 = HIST_CMD;
6569 int histype2 = HIST_CMD;
6570 int hisidx1 = 1;
6571 int hisidx2 = -1;
6572 int idx;
6573 int i, j, k;
6574 char_u *end;
6575 char_u *arg = eap->arg;
6576
6577 if (hislen == 0)
6578 {
6579 MSG(_("'history' option is zero"));
6580 return;
6581 }
6582
6583 if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ','))
6584 {
6585 end = arg;
6586 while (ASCII_ISALPHA(*end)
6587 || vim_strchr((char_u *)":=@>/?", *end) != NULL)
6588 end++;
6589 i = *end;
6590 *end = NUL;
6591 histype1 = get_histtype(arg);
6592 if (histype1 == -1)
6593 {
Bram Moolenaarb9c1e962009-04-22 11:52:33 +00006594 if (STRNICMP(arg, "all", STRLEN(arg)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595 {
6596 histype1 = 0;
6597 histype2 = HIST_COUNT-1;
6598 }
6599 else
6600 {
6601 *end = i;
6602 EMSG(_(e_trailing));
6603 return;
6604 }
6605 }
6606 else
6607 histype2 = histype1;
6608 *end = i;
6609 }
6610 else
6611 end = arg;
6612 if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL)
6613 {
6614 EMSG(_(e_trailing));
6615 return;
6616 }
6617
6618 for (; !got_int && histype1 <= histype2; ++histype1)
6619 {
6620 STRCPY(IObuff, "\n # ");
6621 STRCAT(STRCAT(IObuff, history_names[histype1]), " history");
6622 MSG_PUTS_TITLE(IObuff);
6623 idx = hisidx[histype1];
6624 hist = history[histype1];
6625 j = hisidx1;
6626 k = hisidx2;
6627 if (j < 0)
6628 j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum;
6629 if (k < 0)
6630 k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum;
6631 if (idx >= 0 && j <= k)
6632 for (i = idx + 1; !got_int; ++i)
6633 {
6634 if (i == hislen)
6635 i = 0;
6636 if (hist[i].hisstr != NULL
6637 && hist[i].hisnum >= j && hist[i].hisnum <= k)
6638 {
6639 msg_putchar('\n');
6640 sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ',
6641 hist[i].hisnum);
6642 if (vim_strsize(hist[i].hisstr) > (int)Columns - 10)
6643 trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff),
Bram Moolenaar38f5f952012-01-26 13:01:59 +01006644 (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006645 else
6646 STRCAT(IObuff, hist[i].hisstr);
6647 msg_outtrans(IObuff);
6648 out_flush();
6649 }
6650 if (i == idx)
6651 break;
6652 }
6653 }
6654}
6655#endif
6656
6657#if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO)
Bram Moolenaarff1806f2013-06-15 16:31:47 +02006658/*
6659 * Buffers for history read from a viminfo file. Only valid while reading.
6660 */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006661static histentry_T *viminfo_history[HIST_COUNT] =
6662 {NULL, NULL, NULL, NULL, NULL};
6663static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0};
6664static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0};
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665static int viminfo_add_at_front = FALSE;
6666
Bram Moolenaard25c16e2016-01-29 22:13:30 +01006667static int hist_type2char(int type, int use_question);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668
6669/*
6670 * Translate a history type number to the associated character.
6671 */
6672 static int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006673hist_type2char(
6674 int type,
6675 int use_question) /* use '?' instead of '/' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
6677 if (type == HIST_CMD)
6678 return ':';
6679 if (type == HIST_SEARCH)
6680 {
6681 if (use_question)
6682 return '?';
6683 else
6684 return '/';
6685 }
6686 if (type == HIST_EXPR)
6687 return '=';
6688 return '@';
6689}
6690
6691/*
6692 * Prepare for reading the history from the viminfo file.
6693 * This allocates history arrays to store the read history lines.
6694 */
6695 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006696prepare_viminfo_history(int asklen, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 int i;
6699 int num;
6700 int type;
6701 int len;
6702
6703 init_history();
Bram Moolenaar07219f92013-04-14 23:19:36 +02006704 viminfo_add_at_front = (asklen != 0 && !writing);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 if (asklen > hislen)
6706 asklen = hislen;
6707
6708 for (type = 0; type < HIST_COUNT; ++type)
6709 {
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006710 /* Count the number of empty spaces in the history list. Entries read
6711 * from viminfo previously are also considered empty. If there are
6712 * more spaces available than we request, then fill them up. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713 for (i = 0, num = 0; i < hislen; i++)
Bram Moolenaarb3049f42013-04-05 18:58:47 +02006714 if (history[type][i].hisstr == NULL || history[type][i].viminfo)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 num++;
6716 len = asklen;
6717 if (num > len)
6718 len = num;
6719 if (len <= 0)
6720 viminfo_history[type] = NULL;
6721 else
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006722 viminfo_history[type] = (histentry_T *)lalloc(
6723 (long_u)(len * sizeof(histentry_T)), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724 if (viminfo_history[type] == NULL)
6725 len = 0;
6726 viminfo_hislen[type] = len;
6727 viminfo_hisidx[type] = 0;
6728 }
6729}
6730
6731/*
6732 * Accept a line from the viminfo, store it in the history array when it's
6733 * new.
6734 */
6735 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01006736read_viminfo_history(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737{
6738 int type;
6739 long_u len;
6740 char_u *val;
6741 char_u *p;
6742
6743 type = hist_char2type(virp->vir_line[0]);
6744 if (viminfo_hisidx[type] < viminfo_hislen[type])
6745 {
6746 val = viminfo_readstring(virp, 1, TRUE);
6747 if (val != NULL && *val != NUL)
6748 {
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006749 int sep = (*val == ' ' ? NUL : *val);
6750
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 if (!in_history(type, val + (type == HIST_SEARCH),
Bram Moolenaar07219f92013-04-14 23:19:36 +02006752 viminfo_add_at_front, sep, writing))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753 {
6754 /* Need to re-allocate to append the separator byte. */
6755 len = STRLEN(val);
6756 p = lalloc(len + 2, TRUE);
6757 if (p != NULL)
6758 {
6759 if (type == HIST_SEARCH)
6760 {
6761 /* Search entry: Move the separator from the first
6762 * column to after the NUL. */
6763 mch_memmove(p, val + 1, (size_t)len);
Bram Moolenaard87fbc22012-02-04 22:44:32 +01006764 p[len] = sep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 }
6766 else
6767 {
6768 /* Not a search entry: No separator in the viminfo
6769 * file, add a NUL separator. */
6770 mch_memmove(p, val, (size_t)len + 1);
6771 p[len + 1] = NUL;
6772 }
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006773 viminfo_history[type][viminfo_hisidx[type]].hisstr = p;
6774 viminfo_history[type][viminfo_hisidx[type]].time_set = 0;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006775 viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE;
6776 viminfo_history[type][viminfo_hisidx[type]].hisnum = 0;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006777 viminfo_hisidx[type]++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 }
6779 }
6780 }
6781 vim_free(val);
6782 }
6783 return viminfo_readline(virp);
6784}
6785
Bram Moolenaar07219f92013-04-14 23:19:36 +02006786/*
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006787 * Accept a new style history line from the viminfo, store it in the history
6788 * array when it's new.
6789 */
6790 void
6791handle_viminfo_history(
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006792 garray_T *values,
6793 int writing)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006794{
6795 int type;
6796 long_u len;
6797 char_u *val;
6798 char_u *p;
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006799 bval_T *vp = (bval_T *)values->ga_data;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006800
6801 /* Check the format:
6802 * |{bartype},{histtype},{timestamp},{separator},"text" */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006803 if (values->ga_len < 4
6804 || vp[0].bv_type != BVAL_NR
6805 || vp[1].bv_type != BVAL_NR
6806 || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY)
6807 || vp[3].bv_type != BVAL_STRING)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006808 return;
6809
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006810 type = vp[0].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006811 if (type >= HIST_COUNT)
6812 return;
6813 if (viminfo_hisidx[type] < viminfo_hislen[type])
6814 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006815 val = vp[3].bv_string;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006816 if (val != NULL && *val != NUL)
6817 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006818 int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR
6819 ? vp[2].bv_nr : NUL;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006820 int idx;
6821 int overwrite = FALSE;
6822
6823 if (!in_history(type, val, viminfo_add_at_front, sep, writing))
6824 {
6825 /* If lines were written by an older Vim we need to avoid
6826 * getting duplicates. See if the entry already exists. */
6827 for (idx = 0; idx < viminfo_hisidx[type]; ++idx)
6828 {
6829 p = viminfo_history[type][idx].hisstr;
6830 if (STRCMP(val, p) == 0
6831 && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1]))
6832 {
6833 overwrite = TRUE;
6834 break;
6835 }
6836 }
6837
6838 if (!overwrite)
6839 {
6840 /* Need to re-allocate to append the separator byte. */
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006841 len = vp[3].bv_len;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006842 p = lalloc(len + 2, TRUE);
6843 }
Bram Moolenaar72e697d2016-06-13 22:48:01 +02006844 else
6845 len = 0; /* for picky compilers */
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006846 if (p != NULL)
6847 {
Bram Moolenaar46bbb0c2016-06-11 21:04:39 +02006848 viminfo_history[type][idx].time_set = vp[1].bv_nr;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006849 if (!overwrite)
6850 {
6851 mch_memmove(p, val, (size_t)len + 1);
6852 /* Put the separator after the NUL. */
6853 p[len + 1] = sep;
6854 viminfo_history[type][idx].hisstr = p;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006855 viminfo_history[type][idx].hisnum = 0;
6856 viminfo_history[type][idx].viminfo = TRUE;
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02006857 viminfo_hisidx[type]++;
6858 }
6859 }
6860 }
6861 }
6862 }
6863}
6864
6865/*
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006866 * Concatenate history lines from viminfo after the lines typed in this Vim.
Bram Moolenaar07219f92013-04-14 23:19:36 +02006867 */
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006868 static void
6869concat_history(int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870{
6871 int idx;
6872 int i;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006873
6874 idx = hisidx[type] + viminfo_hisidx[type];
6875 if (idx >= hislen)
6876 idx -= hislen;
6877 else if (idx < 0)
6878 idx = hislen - 1;
6879 if (viminfo_add_at_front)
6880 hisidx[type] = idx;
6881 else
6882 {
6883 if (hisidx[type] == -1)
6884 hisidx[type] = hislen - 1;
6885 do
6886 {
6887 if (history[type][idx].hisstr != NULL
6888 || history[type][idx].viminfo)
6889 break;
6890 if (++idx == hislen)
6891 idx = 0;
6892 } while (idx != hisidx[type]);
6893 if (idx != hisidx[type] && --idx < 0)
6894 idx = hislen - 1;
6895 }
6896 for (i = 0; i < viminfo_hisidx[type]; i++)
6897 {
6898 vim_free(history[type][idx].hisstr);
6899 history[type][idx].hisstr = viminfo_history[type][i].hisstr;
6900 history[type][idx].viminfo = TRUE;
6901 history[type][idx].time_set = viminfo_history[type][i].time_set;
6902 if (--idx < 0)
6903 idx = hislen - 1;
6904 }
6905 idx += 1;
6906 idx %= hislen;
6907 for (i = 0; i < viminfo_hisidx[type]; i++)
6908 {
6909 history[type][idx++].hisnum = ++hisnum[type];
6910 idx %= hislen;
6911 }
6912}
6913
6914#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
6915 static int
6916#ifdef __BORLANDC__
6917_RTLENTRYF
6918#endif
6919sort_hist(const void *s1, const void *s2)
6920{
6921 histentry_T *p1 = *(histentry_T **)s1;
6922 histentry_T *p2 = *(histentry_T **)s2;
6923
6924 if (p1->time_set < p2->time_set) return -1;
6925 if (p1->time_set > p2->time_set) return 1;
6926 return 0;
6927}
6928#endif
6929
6930/*
6931 * Merge history lines from viminfo and lines typed in this Vim based on the
6932 * timestamp;
6933 */
6934 static void
6935merge_history(int type)
6936{
6937 int max_len;
6938 histentry_T **tot_hist;
6939 histentry_T *new_hist;
6940 int i;
6941 int len;
6942
6943 /* Make one long list with all entries. */
6944 max_len = hislen + viminfo_hisidx[type];
6945 tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *));
6946 new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T));
6947 if (tot_hist == NULL || new_hist == NULL)
6948 {
6949 vim_free(tot_hist);
6950 vim_free(new_hist);
6951 return;
6952 }
6953 for (i = 0; i < viminfo_hisidx[type]; i++)
6954 tot_hist[i] = &viminfo_history[type][i];
6955 len = i;
6956 for (i = 0; i < hislen; i++)
6957 if (history[type][i].hisstr != NULL)
6958 tot_hist[len++] = &history[type][i];
6959
6960 /* Sort the list on timestamp. */
6961 qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist);
6962
6963 /* Keep the newest ones. */
6964 for (i = 0; i < hislen; i++)
6965 {
6966 if (i < len)
6967 {
6968 new_hist[i] = *tot_hist[i];
6969 tot_hist[i]->hisstr = NULL;
6970 if (new_hist[i].hisnum == 0)
6971 new_hist[i].hisnum = ++hisnum[type];
6972 }
6973 else
6974 clear_hist_entry(&new_hist[i]);
6975 }
Bram Moolenaara890f5e2016-06-12 23:03:19 +02006976 hisidx[type] = (i < len ? i : len) - 1;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006977
6978 /* Free what is not kept. */
6979 for (i = 0; i < viminfo_hisidx[type]; i++)
6980 vim_free(viminfo_history[type][i].hisstr);
6981 for (i = 0; i < hislen; i++)
6982 vim_free(history[type][i].hisstr);
6983 vim_free(history[type]);
6984 history[type] = new_hist;
Bram Moolenaar62f8b4e2016-06-11 15:31:47 +02006985 vim_free(tot_hist);
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006986}
6987
6988/*
6989 * Finish reading history lines from viminfo. Not used when writing viminfo.
6990 */
6991 void
6992finish_viminfo_history(vir_T *virp)
6993{
Bram Moolenaar071d4272004-06-13 20:20:40 +00006994 int type;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02006995 int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
6997 for (type = 0; type < HIST_COUNT; ++type)
6998 {
6999 if (history[type] == NULL)
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007000 continue;
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007001
7002 if (merge)
7003 merge_history(type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 else
Bram Moolenaar1fd99c12016-06-09 20:24:28 +02007005 concat_history(type);
7006
Bram Moolenaard23a8232018-02-10 18:45:26 +01007007 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007008 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 }
7010}
7011
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007012/*
7013 * Write history to viminfo file in "fp".
7014 * When "merge" is TRUE merge history lines with a previously read viminfo
7015 * file, data is in viminfo_history[].
7016 * When "merge" is FALSE just write all history lines. Used for ":wviminfo!".
7017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007018 void
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007019write_viminfo_history(FILE *fp, int merge)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020{
7021 int i;
7022 int type;
7023 int num_saved;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007024 int round;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
7026 init_history();
7027 if (hislen == 0)
7028 return;
7029 for (type = 0; type < HIST_COUNT; ++type)
7030 {
7031 num_saved = get_viminfo_parameter(hist_type2char(type, FALSE));
7032 if (num_saved == 0)
7033 continue;
7034 if (num_saved < 0) /* Use default */
7035 num_saved = hislen;
7036 fprintf(fp, _("\n# %s History (newest to oldest):\n"),
7037 type == HIST_CMD ? _("Command Line") :
7038 type == HIST_SEARCH ? _("Search String") :
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007039 type == HIST_EXPR ? _("Expression") :
7040 type == HIST_INPUT ? _("Input Line") :
7041 _("Debug Line"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 if (num_saved > hislen)
7043 num_saved = hislen;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007044
7045 /*
7046 * Merge typed and viminfo history:
7047 * round 1: history of typed commands.
7048 * round 2: history from recently read viminfo.
7049 */
7050 for (round = 1; round <= 2; ++round)
7051 {
Bram Moolenaara8565fe2013-04-15 16:14:22 +02007052 if (round == 1)
7053 /* start at newest entry, somewhere in the list */
7054 i = hisidx[type];
7055 else if (viminfo_hisidx[type] > 0)
7056 /* start at newest entry, first in the list */
7057 i = 0;
7058 else
7059 /* empty list */
7060 i = -1;
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007061 if (i >= 0)
7062 while (num_saved > 0
7063 && !(round == 2 && i >= viminfo_hisidx[type]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064 {
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007065 char_u *p;
7066 time_t timestamp;
7067 int c = NUL;
7068
7069 if (round == 1)
7070 {
7071 p = history[type][i].hisstr;
7072 timestamp = history[type][i].time_set;
7073 }
7074 else
7075 {
7076 p = viminfo_history[type] == NULL ? NULL
7077 : viminfo_history[type][i].hisstr;
7078 timestamp = viminfo_history[type] == NULL ? 0
7079 : viminfo_history[type][i].time_set;
7080 }
7081
Bram Moolenaarff1806f2013-06-15 16:31:47 +02007082 if (p != NULL && (round == 2
7083 || !merge
7084 || !history[type][i].viminfo))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007085 {
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007086 --num_saved;
7087 fputc(hist_type2char(type, TRUE), fp);
7088 /* For the search history: put the separator in the
7089 * second column; use a space if there isn't one. */
7090 if (type == HIST_SEARCH)
7091 {
7092 c = p[STRLEN(p) + 1];
7093 putc(c == NUL ? ' ' : c, fp);
7094 }
7095 viminfo_writestring(fp, p);
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007096
7097 {
7098 char cbuf[NUMBUFLEN];
7099
7100 /* New style history with a bar line. Format:
7101 * |{bartype},{histtype},{timestamp},{separator},"text" */
7102 if (c == NUL)
7103 cbuf[0] = NUL;
7104 else
7105 sprintf(cbuf, "%d", c);
7106 fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY,
7107 type, (long)timestamp, cbuf);
7108 barline_writestring(fp, p, LSIZE - 20);
7109 putc('\n', fp);
7110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007112 if (round == 1)
7113 {
7114 /* Decrement index, loop around and stop when back at
7115 * the start. */
7116 if (--i < 0)
7117 i = hislen - 1;
7118 if (i == hisidx[type])
7119 break;
7120 }
7121 else
7122 {
7123 /* Increment index. Stop at the end in the while. */
7124 ++i;
7125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126 }
Bram Moolenaar6f852a52013-04-14 16:26:15 +02007127 }
Bram Moolenaar07219f92013-04-14 23:19:36 +02007128 for (i = 0; i < viminfo_hisidx[type]; ++i)
Bram Moolenaarf687cf32013-04-24 15:39:11 +02007129 if (viminfo_history[type] != NULL)
Bram Moolenaar45d2eea2016-06-06 21:07:52 +02007130 vim_free(viminfo_history[type][i].hisstr);
Bram Moolenaard23a8232018-02-10 18:45:26 +01007131 VIM_CLEAR(viminfo_history[type]);
Bram Moolenaarb70a4732013-04-15 22:22:57 +02007132 viminfo_hisidx[type] = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007133 }
7134}
7135#endif /* FEAT_VIMINFO */
7136
7137#if defined(FEAT_FKMAP) || defined(PROTO)
7138/*
7139 * Write a character at the current cursor+offset position.
7140 * It is directly written into the command buffer block.
7141 */
7142 void
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007143cmd_pchar(int c, int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7146 {
7147 EMSG(_("E198: cmd_pchar beyond the command length"));
7148 return;
7149 }
7150 ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c;
7151 ccline.cmdbuff[ccline.cmdlen] = NUL;
7152}
7153
7154 int
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007155cmd_gchar(int offset)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156{
7157 if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0)
7158 {
7159 /* EMSG(_("cmd_gchar beyond the command length")); */
7160 return NUL;
7161 }
7162 return (int)ccline.cmdbuff[ccline.cmdpos + offset];
7163}
7164#endif
7165
7166#if defined(FEAT_CMDWIN) || defined(PROTO)
7167/*
7168 * Open a window on the current command line and history. Allow editing in
7169 * the window. Returns when the window is closed.
7170 * Returns:
7171 * CR if the command is to be executed
7172 * Ctrl_C if it is to be abandoned
7173 * K_IGNORE if editing continues
7174 */
7175 static int
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007176open_cmdwin(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007177{
7178 struct cmdline_info save_ccline;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007179 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007181 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 win_T *wp;
7183 int i;
7184 linenr_T lnum;
7185 int histtype;
7186 garray_T winsizes;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 int save_restart_edit = restart_edit;
7188 int save_State = State;
7189 int save_exmode = exmode_active;
Bram Moolenaar46152342005-09-07 21:18:43 +00007190#ifdef FEAT_RIGHTLEFT
7191 int save_cmdmsg_rl = cmdmsg_rl;
7192#endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007193#ifdef FEAT_FOLDING
7194 int save_KeyTyped;
7195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196
7197 /* Can't do this recursively. Can't do it when typing a password. */
7198 if (cmdwin_type != 0
7199# if defined(FEAT_CRYPT) || defined(FEAT_EVAL)
7200 || cmdline_star > 0
7201# endif
7202 )
7203 {
7204 beep_flush();
7205 return K_IGNORE;
7206 }
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007207 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208
7209 /* Save current window sizes. */
7210 win_size_save(&winsizes);
7211
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212 /* Don't execute autocommands while creating the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007213 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007214
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007215 /* don't use a new tab page */
7216 cmdmod.tab = 0;
Bram Moolenaar3bab9392017-04-07 15:42:25 +02007217 cmdmod.noswapfile = 1;
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00007218
Bram Moolenaar071d4272004-06-13 20:20:40 +00007219 /* Create a window for the command-line buffer. */
7220 if (win_split((int)p_cwh, WSP_BOT) == FAIL)
7221 {
7222 beep_flush();
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007223 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 return K_IGNORE;
7225 }
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007226 cmdwin_type = get_cmdline_type();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
7228 /* Create the command-line buffer empty. */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00007229 (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL);
Bram Moolenaar446cb832008-06-24 21:56:24 +00007230 (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007232 curbuf->b_p_ma = TRUE;
Bram Moolenaar876f6d72009-04-29 10:05:51 +00007233#ifdef FEAT_FOLDING
7234 curwin->w_p_fen = FALSE;
7235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236# ifdef FEAT_RIGHTLEFT
Bram Moolenaar46152342005-09-07 21:18:43 +00007237 curwin->w_p_rl = cmdmsg_rl;
7238 cmdmsg_rl = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239# endif
Bram Moolenaar3368ea22010-09-21 16:56:35 +02007240 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242 /* Do execute autocommands for setting the filetype (load syntax). */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007243 unblock_autocmds();
Bram Moolenaar18141832017-06-25 21:17:25 +02007244 /* But don't allow switching to another buffer. */
7245 ++curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007246
Bram Moolenaar46152342005-09-07 21:18:43 +00007247 /* Showing the prompt may have set need_wait_return, reset it. */
7248 need_wait_return = FALSE;
7249
Bram Moolenaare8bd5ce2009-03-02 01:12:48 +00007250 histtype = hist_char2type(cmdwin_type);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 if (histtype == HIST_CMD || histtype == HIST_DEBUG)
7252 {
7253 if (p_wc == TAB)
7254 {
7255 add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT);
7256 add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL);
7257 }
7258 set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL);
7259 }
Bram Moolenaar18141832017-06-25 21:17:25 +02007260 --curbuf_lock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007261
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007262 /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin
7263 * sets 'textwidth' to 78). */
7264 curbuf->b_p_tw = 0;
7265
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 /* Fill the buffer with the history. */
7267 init_history();
7268 if (hislen > 0)
7269 {
7270 i = hisidx[histtype];
7271 if (i >= 0)
7272 {
7273 lnum = 0;
7274 do
7275 {
7276 if (++i == hislen)
7277 i = 0;
7278 if (history[histtype][i].hisstr != NULL)
7279 ml_append(lnum++, history[histtype][i].hisstr,
7280 (colnr_T)0, FALSE);
7281 }
7282 while (i != hisidx[histtype]);
7283 }
7284 }
7285
7286 /* Replace the empty last line with the current command-line and put the
7287 * cursor there. */
7288 ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE);
7289 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
7290 curwin->w_cursor.col = ccline.cmdpos;
Bram Moolenaar46152342005-09-07 21:18:43 +00007291 changed_line_abv_curs();
7292 invalidate_botline();
Bram Moolenaar600dddc2006-03-12 22:05:10 +00007293 redraw_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294
7295 /* Save the command line info, can be used recursively. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007296 save_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007297
7298 /* No Ex mode here! */
7299 exmode_active = 0;
7300
7301 State = NORMAL;
7302# ifdef FEAT_MOUSE
7303 setmouse();
7304# endif
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 /* Trigger CmdwinEnter autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007307 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER);
Bram Moolenaar5495cc92006-08-16 14:23:04 +00007308 if (restart_edit != 0) /* autocmd with ":startinsert" */
7309 stuffcharReadbuff(K_NOP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310
7311 i = RedrawingDisabled;
7312 RedrawingDisabled = 0;
7313
7314 /*
7315 * Call the main loop until <CR> or CTRL-C is typed.
7316 */
7317 cmdwin_result = 0;
Bram Moolenaar26a60b42005-02-22 08:49:11 +00007318 main_loop(TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319
7320 RedrawingDisabled = i;
7321
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007322# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007323 save_KeyTyped = KeyTyped;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007324# endif
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007325
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326 /* Trigger CmdwinLeave autocommands. */
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02007327 trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE);
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007328
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007329# ifdef FEAT_FOLDING
Bram Moolenaar42f06f92014-08-17 17:24:07 +02007330 /* Restore KeyTyped in case it is modified by autocommands */
7331 KeyTyped = save_KeyTyped;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332# endif
7333
Bram Moolenaarccc18222007-05-10 18:25:20 +00007334 /* Restore the command line info. */
Bram Moolenaar1d669c22017-01-11 22:40:19 +01007335 restore_cmdline(&save_ccline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007336 cmdwin_type = 0;
7337
7338 exmode_active = save_exmode;
7339
Bram Moolenaarf9821062008-06-20 16:31:07 +00007340 /* Safety check: The old window or buffer was deleted: It's a bug when
Bram Moolenaar071d4272004-06-13 20:20:40 +00007341 * this happens! */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007342 if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343 {
7344 cmdwin_result = Ctrl_C;
7345 EMSG(_("E199: Active window or buffer deleted"));
7346 }
7347 else
7348 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01007349# if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350 /* autocmds may abort script processing */
7351 if (aborting() && cmdwin_result != K_IGNORE)
7352 cmdwin_result = Ctrl_C;
7353# endif
7354 /* Set the new command line from the cmdline buffer. */
7355 vim_free(ccline.cmdbuff);
Bram Moolenaar46152342005-09-07 21:18:43 +00007356 if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007357 {
Bram Moolenaar46152342005-09-07 21:18:43 +00007358 char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!";
7359
7360 if (histtype == HIST_CMD)
7361 {
7362 /* Execute the command directly. */
7363 ccline.cmdbuff = vim_strsave((char_u *)p);
7364 cmdwin_result = CAR;
7365 }
7366 else
7367 {
7368 /* First need to cancel what we were doing. */
7369 ccline.cmdbuff = NULL;
7370 stuffcharReadbuff(':');
7371 stuffReadbuff((char_u *)p);
7372 stuffcharReadbuff(CAR);
7373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 }
7375 else if (cmdwin_result == K_XF2) /* :qa typed */
7376 {
7377 ccline.cmdbuff = vim_strsave((char_u *)"qa");
7378 cmdwin_result = CAR;
7379 }
Bram Moolenaar9bd1a7e2011-05-19 14:50:54 +02007380 else if (cmdwin_result == Ctrl_C)
7381 {
7382 /* :q or :close, don't execute any command
7383 * and don't modify the cmd window. */
7384 ccline.cmdbuff = NULL;
7385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007386 else
7387 ccline.cmdbuff = vim_strsave(ml_get_curline());
7388 if (ccline.cmdbuff == NULL)
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007389 {
7390 ccline.cmdbuff = vim_strsave((char_u *)"");
7391 ccline.cmdlen = 0;
7392 ccline.cmdbufflen = 1;
7393 ccline.cmdpos = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007394 cmdwin_result = Ctrl_C;
Bram Moolenaar5a15b6a2017-07-11 15:11:57 +02007395 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 else
7397 {
7398 ccline.cmdlen = (int)STRLEN(ccline.cmdbuff);
7399 ccline.cmdbufflen = ccline.cmdlen + 1;
7400 ccline.cmdpos = curwin->w_cursor.col;
7401 if (ccline.cmdpos > ccline.cmdlen)
7402 ccline.cmdpos = ccline.cmdlen;
7403 if (cmdwin_result == K_IGNORE)
7404 {
7405 set_cmdspos_cursor();
7406 redrawcmd();
7407 }
7408 }
7409
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410 /* Don't execute autocommands while deleting the window. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007411 block_autocmds();
Bram Moolenaarfa67fbe2015-06-25 18:20:36 +02007412# ifdef FEAT_CONCEAL
7413 /* Avoid command-line window first character being concealed. */
7414 curwin->w_p_cole = 0;
7415# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 wp = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007417 set_bufref(&bufref, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 win_goto(old_curwin);
7419 win_close(wp, TRUE);
Bram Moolenaar8006d692010-03-02 17:23:21 +01007420
7421 /* win_close() may have already wiped the buffer when 'bh' is
7422 * set to 'wipe' */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02007423 if (bufref_valid(&bufref))
7424 close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007425
7426 /* Restore window sizes. */
7427 win_size_restore(&winsizes);
7428
Bram Moolenaar78ab3312007-09-29 12:16:41 +00007429 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430 }
7431
7432 ga_clear(&winsizes);
7433 restart_edit = save_restart_edit;
Bram Moolenaar46152342005-09-07 21:18:43 +00007434# ifdef FEAT_RIGHTLEFT
7435 cmdmsg_rl = save_cmdmsg_rl;
7436# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437
7438 State = save_State;
7439# ifdef FEAT_MOUSE
7440 setmouse();
7441# endif
7442
7443 return cmdwin_result;
7444}
7445#endif /* FEAT_CMDWIN */
7446
7447/*
7448 * Used for commands that either take a simple command string argument, or:
7449 * cmd << endmarker
7450 * {script}
7451 * endmarker
7452 * Returns a pointer to allocated memory with {script} or NULL.
7453 */
7454 char_u *
Bram Moolenaar78c0b7d2016-01-30 15:52:46 +01007455script_get(exarg_T *eap, char_u *cmd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456{
7457 char_u *theline;
7458 char *end_pattern = NULL;
7459 char dot[] = ".";
7460 garray_T ga;
7461
7462 if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL)
7463 return NULL;
7464
7465 ga_init2(&ga, 1, 0x400);
7466
7467 if (cmd[2] != NUL)
7468 end_pattern = (char *)skipwhite(cmd + 2);
7469 else
7470 end_pattern = dot;
7471
7472 for (;;)
7473 {
7474 theline = eap->getline(
7475#ifdef FEAT_EVAL
Bram Moolenaar12805862005-01-05 22:16:17 +00007476 eap->cstack->cs_looplevel > 0 ? -1 :
Bram Moolenaar071d4272004-06-13 20:20:40 +00007477#endif
7478 NUL, eap->cookie, 0);
7479
7480 if (theline == NULL || STRCMP(end_pattern, theline) == 0)
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007481 {
7482 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 break;
Bram Moolenaarbe555e72008-08-06 12:19:26 +00007484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485
7486 ga_concat(&ga, theline);
7487 ga_append(&ga, '\n');
7488 vim_free(theline);
7489 }
Bram Moolenaar269ec652004-07-29 08:43:53 +00007490 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491
7492 return (char_u *)ga.ga_data;
7493}